use of me.retrodaredevil.couchdbjava.json.JsonData in project solarthing by wildmountainfarms.
the class CouchDbPacketSaver method handle.
@Override
public void handle(PacketCollection packetCollection) throws PacketHandleException {
// Normally we would try and create the database, but that doesn't work with non-admin cookie authenticated users
String id = packetCollection.getDbId();
String revision = idMap == null ? null : idMap.get(id);
final JsonData jsonData;
try {
jsonData = new StringJsonData(MAPPER.writeValueAsString(packetCollection));
} catch (JsonProcessingException e) {
throw new RuntimeException("Cannot serialize packet collection! This is bad!", e);
}
try {
final DocumentResponse response;
if (revision == null) {
response = database.putDocument(id, jsonData);
} else {
response = database.updateDocument(id, revision, jsonData);
}
LOGGER.debug("Now revision is: " + response.getRev() + ". It was: " + revision);
if (idMap != null) {
// Currently, if we have a new document ID, we never, ever, need to worry about using an older document ID, so we can clear the map to avoid keeping unnecessary memory
idMap.clear();
idMap.put(id, response.getRev());
}
} catch (CouchDbNotFoundException ex) {
throw new PacketHandleException("Got 'not found'. Does the database exist? Make sure to run the couchdb-setup!", ex);
} catch (CouchDbUpdateConflictException ex) {
if (idMap == null) {
// we are ignoring conflicts
LOGGER.debug("Got update conflict exception. Ignoring...");
return;
}
try {
String actualRev = database.getCurrentRevision(id);
idMap.put(id, actualRev);
LOGGER.debug("We were able to get the actual Revision ID for id=" + id + " actual rev=" + actualRev);
} catch (CouchDbException revEx) {
LOGGER.debug("Unable to get the actual Revision ID for id=" + id, revEx);
}
throw new PacketHandleException("Conflict while saving something to couchdb. id=" + id + " rev=" + revision + ". This usually means we put a packet in the database, but we weren't able to cache its rev id.", ex);
} catch (CouchDbException ex) {
if (ex.getCause() instanceof IOException) {
throw new PacketHandleException("We got a DbAccessException probably meaning we couldn't reach the database.", ex);
} else {
throw new PacketHandleException("Got a DbAccessException without IOException as a cause. Something must be wrong.", ex);
}
}
}
use of me.retrodaredevil.couchdbjava.json.JsonData in project solarthing by wildmountainfarms.
the class CouchDbSetupMain method doCouchDbSetupMain.
public int doCouchDbSetupMain() throws CouchDbException {
out.println("You will now setup your CouchDB instance! Some databases will be automatically created (enter)");
prompt.promptContinue();
for (SolarThingDatabaseType databaseType : SolarThingDatabaseType.values()) {
createDatabase(databaseType.getName());
}
out.println("All necessary databases have been created.");
out.println();
out.println("Now views and security will be configured for each database. Please enter the name of the user to be added as an admin to each database.");
out.println("This user is commonly named 'uploader'. (Leave blank to not configure)");
out.print("Name of user: ");
String uploaderUser = prompt.promptUserName(SolarThingDatabaseType.UserType.UPLOADER);
if (uploaderUser == null) {
out.println("No user will be added as an admin, but members will still be cleared. (Enter to confirm)");
} else {
out.println("User: " + uploaderUser + " will be used. (Enter to confirm)");
}
prompt.promptContinue();
if (uploaderUser != null) {
createUserIfNotExists(uploaderUser, SolarThingDatabaseType.UserType.UPLOADER);
}
out.println("You can also enter the name of the user to manage the solarthing_cache and solarthing_alter databases.");
out.println("This user is commonly named 'manager'. (Leave blank to not configure)" + (uploaderUser == null ? "" : " (Use '" + uploaderUser + "' to use same user to manage the cache database)"));
String managerUser = prompt.promptUserName(SolarThingDatabaseType.UserType.MANAGER);
if (managerUser == null) {
out.println("No user will be configured to manage the solarthing_cache and solarthing_alter database. (Enter to confirm)");
} else {
out.println("User: " + managerUser + " will be used to manage solarthing_cache and solarthing_alter. (Enter to confirm)");
}
prompt.promptContinue();
if (managerUser != null && !managerUser.equals(uploaderUser)) {
createUserIfNotExists(managerUser, SolarThingDatabaseType.UserType.MANAGER);
}
out.println();
for (SolarThingDatabaseType databaseType : SolarThingDatabaseType.values()) {
CouchDbDatabase database = instance.getDatabase(databaseType.getName());
if (databaseType.needsAnyViews()) {
out.println("Adding packets design to database " + databaseType.getName());
MutablePacketsDesign design = new MutablePacketsDesign();
if (databaseType.needsMillisView()) {
out.println("This database will have the millisNull view");
design.addMillisNullView();
}
if (databaseType.needsSimpleAllDocsView()) {
out.println("This database will have the simpleAllDocs view");
design.addSimpleAllDocsView();
}
if (databaseType.needsReadonlyValidateFunction()) {
out.println("This database will be readonly");
design.setReadonlyAuth();
}
final JsonData jsonData;
try {
jsonData = new StringJsonData(MAPPER.writeValueAsString(design));
} catch (JsonProcessingException e) {
throw new RuntimeException("Couldn't serialize json! Report this!", e);
}
try {
database.putDocument("_design/packets", jsonData);
} catch (CouchDbUpdateConflictException e) {
String revision = database.getCurrentRevision("_design/packets");
database.updateDocument("_design/packets", revision, jsonData);
out.println("updated _design/packets document on database: " + databaseType.getName());
}
}
out.println("Configuring security for database " + databaseType.getName());
DatabaseSecurity oldSecurity = database.getSecurity();
// First initialize newAdmins to the old admins
SecurityGroup newAdmins = oldSecurity.getAdminsOrBlank();
Set<SolarThingDatabaseType.UserType> usersWithWritePermission = databaseType.getUsersWithWritePermission();
if (usersWithWritePermission.contains(SolarThingDatabaseType.UserType.MANAGER)) {
newAdmins = newAdmins.withName(managerUser);
}
if (usersWithWritePermission.contains(SolarThingDatabaseType.UserType.UPLOADER)) {
newAdmins = newAdmins.withName(uploaderUser);
}
database.setSecurity(new DatabaseSecurity(// update the list of admins
newAdmins, // if database is public, this has no members, if private, keep old members which should include an _admin role
databaseType.isPublic() ? SecurityGroup.BLANK : oldSecurity.getMembers()));
out.println();
}
out.println("Completed successfully!");
return 0;
}
use of me.retrodaredevil.couchdbjava.json.JsonData in project solarthing by wildmountainfarms.
the class CouchDbSetupMain method createUserIfNotExists.
private void createUserIfNotExists(String username, SolarThingDatabaseType.UserType userType) throws CouchDbException {
CouchDbDatabase usersDatabase = instance.getUsersDatabase();
String documentId = "org.couchdb.user:" + username;
DocumentData userDocumentData = null;
try {
userDocumentData = usersDatabase.getDocument(documentId);
} catch (CouchDbNotFoundException ignored) {
}
final UserEntry user;
if (userDocumentData != null) {
try {
user = CouchDbJacksonUtil.readValue(MAPPER, userDocumentData.getJsonData(), UserEntry.class);
} catch (JsonProcessingException e) {
throw new RuntimeException("Could not parse user document data. Please report this bug", e);
}
} else {
user = null;
}
if (user != null) {
out.println("User: " + username + " exists! Continuing.");
} else {
out.println("Please enter a password for '" + username + "'.");
String password = prompt.promptUserPassword(userType);
final JsonData jsonData;
try {
jsonData = new StringJsonData(MAPPER.writeValueAsString(new UserEntry(username, password)));
} catch (JsonProcessingException e) {
throw new RuntimeException("Couldn't serialize json! Report this!", e);
}
usersDatabase.createIfNotExists();
usersDatabase.putDocument(documentId, jsonData);
}
}
use of me.retrodaredevil.couchdbjava.json.JsonData in project solarthing by wildmountainfarms.
the class CacheHandler method queryOrCalculateCaches.
private <T extends CacheDataPacket> List<T> queryOrCalculateCaches(TypeReference<T> typeReference, String cacheName, String sourceId, long startPeriodNumber, long endPeriodNumber) {
// the document IDs needed to return data
List<String> documentIds = new ArrayList<>();
// a map from a document ID to a period number
Map<String, Long> documentIdPeriodNumberMap = new HashMap<>();
for (long periodNumber = startPeriodNumber; periodNumber <= endPeriodNumber; periodNumber++) {
Instant periodStart = getPeriodStartFromNumber(periodNumber);
String documentId = CacheUtil.getDocumentId(periodStart, duration, sourceId, cacheName);
documentIds.add(documentId);
documentIdPeriodNumberMap.put(documentId, periodNumber);
}
BulkGetRequest request = BulkGetRequest.from(documentIds);
final BulkGetResponse response;
try {
response = cacheDatabase.getDocumentsBulk(request);
} catch (CouchDbException e) {
throw new DatabaseException("CouchDB exception | message: " + e.getMessage(), e);
}
// map for documents that need to be updated. The value represents the revision that needs to be used to update it
Map<String, String> documentIdRevisionMapForUpdate = new HashMap<>();
// Map for period number -> cached data. This helps us make sure we only return a single piece of data for each period
Map<Long, T> periodNumberPacketMap = new TreeMap<>();
// Set for document IDs that we already have and do not need to be updated
Set<String> doNotUpdateDocumentIdsSet = new HashSet<>();
Long queryStartPeriodNumber = null;
Long queryEndPeriodNumber = null;
for (BulkGetResponse.Result result : response.getResults()) {
if (result.hasConflicts()) {
// replication going on with their databases.
throw new UnexpectedResponseException("cache document with conflict! doc id: " + result.getDocumentId());
}
Long periodNumber = documentIdPeriodNumberMap.get(result.getDocumentId());
if (periodNumber == null) {
throw new IllegalStateException("Could not get period number for doc id: " + result.getDocumentId() + ". This should never happen.");
}
T value = null;
if (!result.isError()) {
JsonData jsonData = result.getJsonDataAssertNotConflicted();
try {
value = CouchDbJacksonUtil.readValue(mapper, jsonData, typeReference);
if (value.getSourceId().equals(sourceId) && value.getCacheName().equals(cacheName)) {
periodNumberPacketMap.put(periodNumber, value);
}
doNotUpdateDocumentIdsSet.add(value.getDbId());
} catch (JsonProcessingException ex) {
// If we are in this catch block, one of two things has happened:
// The JSON is invalid (unlikely), we updated how a given cache is serialized/deserialized, or we're dumb and never tested to see if the deserialization works.
// If the JSON is actually invalid, getRevisionFromJsonData will throw an exception. Otherwise, we need to calculate the given cache again
String revision = getRevisionFromJsonData(jsonData);
documentIdRevisionMapForUpdate.put(result.getDocumentId(), revision);
}
}
if (value == null) {
if (queryStartPeriodNumber == null) {
queryStartPeriodNumber = periodNumber;
queryEndPeriodNumber = periodNumber;
} else {
queryStartPeriodNumber = Math.min(queryStartPeriodNumber, periodNumber);
queryEndPeriodNumber = Math.max(queryEndPeriodNumber, periodNumber);
}
}
}
if (queryStartPeriodNumber != null) {
List<CacheDataPacket> calculatedPackets = calculatePeriod(queryStartPeriodNumber, queryEndPeriodNumber);
List<JsonData> calculatedPacketsJsonDataList = new ArrayList<>();
int updateAttemptCount = 0;
for (CacheDataPacket packet : calculatedPackets) {
if (doNotUpdateDocumentIdsSet.contains(packet.getDbId())) {
continue;
}
if (!sourceId.equals(packet.getSourceId()) || !cacheName.equals(packet.getCacheName())) {
// This is the same with different cache names, we don't know which ones we need to update and which ones we cannot
continue;
}
JsonData json;
try {
String revision = documentIdRevisionMapForUpdate.get(packet.getDbId());
if (revision == null) {
json = new StringJsonData(mapper.writeValueAsString(packet));
} else {
json = new StringJsonData(mapper.writeValueAsString(new DocumentRevisionWrapper(revision, packet)));
updateAttemptCount++;
}
} catch (JsonProcessingException e) {
throw new RuntimeException("Should be able to serialize!", e);
}
calculatedPacketsJsonDataList.add(json);
}
final List<BulkDocumentResponse> postResponse;
try {
postResponse = cacheDatabase.postDocumentsBulk(new BulkPostRequest(calculatedPacketsJsonDataList));
} catch (CouchDbException e) {
throw new DatabaseException("Could not update cache", e);
}
int successCount = 0;
int failCount = 0;
for (BulkDocumentResponse documentResponse : postResponse) {
if (documentResponse.isOk()) {
successCount++;
} else {
failCount++;
LOGGER.info("Error: " + documentResponse.getError() + " reason: " + documentResponse.getReason() + " on id: " + documentResponse.getId());
}
}
LOGGER.debug("(Cache updating) Success: " + successCount + " fail: " + failCount + ". Tried to update: " + updateAttemptCount);
int numberOfWantedType = 0;
for (CacheDataPacket cacheDataPacket : calculatedPackets) {
if (cacheDataPacket.getSourceId().equals(sourceId) && cacheDataPacket.getCacheName().equals(cacheName)) {
@SuppressWarnings("unchecked") T packet = (T) cacheDataPacket;
Long periodNumber = documentIdPeriodNumberMap.get(cacheDataPacket.getDbId());
if (periodNumber == null) {
throw new NullPointerException("No period number for id: " + cacheDataPacket.getDbId());
}
periodNumberPacketMap.put(periodNumber, packet);
numberOfWantedType++;
}
}
LOGGER.debug("Calculated " + calculatedPackets.size() + " and " + numberOfWantedType + " were of type " + cacheName);
} else {
LOGGER.trace("Didn't have to get any data");
}
return new ArrayList<>(periodNumberPacketMap.values());
}
use of me.retrodaredevil.couchdbjava.json.JsonData in project solarthing by wildmountainfarms.
the class CouchDbMillisDatabase method query.
@Override
public List<StoredPacketGroup> query(MillisQuery query) throws SolarThingDatabaseException {
final ViewResponse response;
try {
response = database.queryView(SolarThingCouchDb.createMillisNullView(CouchDbQueryUtil.createMillisNullParams(query)));
} catch (CouchDbException e) {
throw ExceptionUtil.createFromCouchDbException(e);
}
List<ViewResponse.DocumentEntry> rows = response.getRows();
List<StoredPacketGroup> r = new ArrayList<>(rows.size());
for (ViewResponse.DocumentEntry row : rows) {
// When using includeDocs=true, we want to use the doc, not its value (which is null with millisNull)
JsonData jsonData = row.getDoc();
StoredPacketGroup storedPacketGroup = jsonDataToStoredPacketGroup(jsonData).getPacket();
r.add(storedPacketGroup);
}
return r;
}
Aggregations