use of org.hypertrace.core.documentstore.JSONDocument in project document-store by hypertrace.
the class MongoCollection method dbObjectToDocument.
private Document dbObjectToDocument(BasicDBObject dbObject) {
try {
// Hack: Remove the _id field since it's an unrecognized field for Proto layer.
// TODO: We should rather use separate DAO classes instead of using the
// DB document directly as proto message.
dbObject.removeField(ID_KEY);
String jsonString;
JsonWriterSettings relaxed = JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build();
jsonString = dbObject.toJson(relaxed);
JsonNode jsonNode = MAPPER.readTree(jsonString);
JsonNode decodedJsonNode = recursiveClone(jsonNode, MongoUtils::decodeKey);
return new JSONDocument(decodedJsonNode);
} catch (IOException e) {
// throwing exception is not very useful here.
return JSONDocument.errorDocument(e.getMessage());
}
}
use of org.hypertrace.core.documentstore.JSONDocument in project document-store by hypertrace.
the class MongoDocStoreTest method whenBulkUpdatingExistingRecords_thenExpectOnlyRecordsWhoseConditionsMatchToBeUpdated.
@Test
public void whenBulkUpdatingExistingRecords_thenExpectOnlyRecordsWhoseConditionsMatchToBeUpdated() throws Exception {
Collection collection = datastore.getCollection(COLLECTION_NAME);
ObjectNode persistedObject = OBJECT_MAPPER.createObjectNode();
persistedObject.put("foo1", "bar1");
persistedObject.put("timestamp", 90);
collection.create(new SingleValueKey("tenant-1", "testKey1"), new JSONDocument(persistedObject));
ObjectNode updatedObject = OBJECT_MAPPER.createObjectNode();
updatedObject.put("foo1", "bar1");
updatedObject.put("timestamp", 110);
List<BulkUpdateRequest> toUpdate = new ArrayList<>();
toUpdate.add(new BulkUpdateRequest(new SingleValueKey("tenant-1", "testKey1"), new JSONDocument(updatedObject), new Filter(Op.LT, "timestamp", 100)));
toUpdate.add(new BulkUpdateRequest(new SingleValueKey("tenant-1", "testKey2"), new JSONDocument(updatedObject), new Filter(Op.LT, "timestamp", 100)));
BulkUpdateResult result = collection.bulkUpdate(toUpdate);
Assertions.assertEquals(1, result.getUpdatedCount());
Query query = new Query();
query.setFilter(new Filter(Op.EQ, "_id", new SingleValueKey("tenant-1", "testKey1").toString()));
Iterator<Document> it = collection.search(query);
JsonNode root = OBJECT_MAPPER.readTree(it.next().toJson());
Long timestamp = root.findValue("timestamp").asLong();
Assertions.assertEquals(110, timestamp);
}
use of org.hypertrace.core.documentstore.JSONDocument in project document-store by hypertrace.
the class MongoDocStoreTest method testUpsertAndReturn.
@Test
public void testUpsertAndReturn() throws IOException {
Collection collection = datastore.getCollection(COLLECTION_NAME);
ObjectNode objectNode = OBJECT_MAPPER.createObjectNode();
objectNode.put("foo1", "bar1");
Document document = new JSONDocument(objectNode);
Document persistedDocument = collection.upsertAndReturn(new SingleValueKey("default", "testKey"), document);
Query query = new Query();
query.setFilter(Filter.eq("_id", "default:testKey"));
// Assert upsert and search results match
assertEquals(collection.search(query).next(), persistedDocument);
JsonNode node = OBJECT_MAPPER.readTree(persistedDocument.toJson());
String lastUpdatedTime = node.findValue(LAST_UPDATE_TIME_KEY).findValue("$date").asText();
long createdTime = node.findValue(LAST_CREATED_TIME_KEY).asLong();
objectNode = OBJECT_MAPPER.createObjectNode();
objectNode.put("foo2", "bar2");
document = new JSONDocument(objectNode);
// Upsert again and verify that createdTime does not change, while lastUpdatedTime
// has changed and values have merged
Document updatedDocument = collection.upsertAndReturn(new SingleValueKey("default", "testKey"), document);
node = OBJECT_MAPPER.readTree(updatedDocument.toJson());
String newLastUpdatedTime = node.findValue(LAST_UPDATE_TIME_KEY).findValue("$date").asText();
long newCreatedTime = node.findValue(LAST_CREATED_TIME_KEY).asLong();
assertEquals(createdTime, newCreatedTime);
assertNotEquals(lastUpdatedTime, newLastUpdatedTime);
assertEquals("bar1", node.get("foo1").asText());
assertEquals("bar2", node.get("foo2").asText());
}
use of org.hypertrace.core.documentstore.JSONDocument in project document-store by hypertrace.
the class MongoDocStoreTest method testBulkUpsertAndVerifyUpdatedTime.
@Test
public void testBulkUpsertAndVerifyUpdatedTime() throws IOException {
Collection collection = datastore.getCollection(COLLECTION_NAME);
ObjectNode objectNode = OBJECT_MAPPER.createObjectNode();
objectNode.put("foo1", "bar1");
Document document = new JSONDocument(objectNode);
collection.bulkUpsert(Map.of(new SingleValueKey("default", "testKey"), document));
Query query = new Query();
query.setFilter(Filter.eq("_id", "default:testKey"));
Iterator<Document> results = collection.search(query);
List<Document> documents = new ArrayList<>();
while (results.hasNext()) {
documents.add(results.next());
}
Assertions.assertFalse(documents.isEmpty());
String persistedDocument = documents.get(0).toJson();
// Assert _lastUpdateTime fields exists
Assertions.assertTrue(persistedDocument.contains(LAST_UPDATE_TIME_KEY));
Assertions.assertTrue(persistedDocument.contains(LAST_UPDATED_TIME_KEY));
Assertions.assertTrue(persistedDocument.contains(LAST_CREATED_TIME_KEY));
JsonNode node = OBJECT_MAPPER.readTree(persistedDocument);
String lastUpdateTime = node.findValue(LAST_UPDATE_TIME_KEY).findValue("$date").asText();
long updatedTime = node.findValue(LAST_UPDATED_TIME_KEY).asLong();
long createdTime = node.findValue(LAST_CREATED_TIME_KEY).asLong();
// Upsert again and verify that createdTime does not change, while lastUpdatedTime
// has changed
collection.bulkUpsert(Map.of(new SingleValueKey("default", "testKey"), document));
results = collection.search(query);
documents = new ArrayList<>();
while (results.hasNext()) {
documents.add(results.next());
}
Assertions.assertFalse(documents.isEmpty());
persistedDocument = documents.get(0).toJson();
node = OBJECT_MAPPER.readTree(persistedDocument);
String newLastUpdateTime = node.findValue(LAST_UPDATE_TIME_KEY).findValue("$date").asText();
long newUpdatedTime = node.findValue(LAST_UPDATED_TIME_KEY).asLong();
long newCreatedTime = node.findValue(LAST_CREATED_TIME_KEY).asLong();
Assertions.assertEquals(createdTime, newCreatedTime);
Assertions.assertFalse(newLastUpdateTime.equalsIgnoreCase(lastUpdateTime));
Assertions.assertNotEquals(newUpdatedTime, updatedTime);
}
use of org.hypertrace.core.documentstore.JSONDocument in project document-store by hypertrace.
the class Utils method createDocument.
public static Document createDocument(String key, String value) {
ObjectNode objectNode = OBJECT_MAPPER.createObjectNode();
objectNode.put(key, value);
return new JSONDocument(objectNode);
}
Aggregations