Search in sources :

Example 11 with JSONDocument

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());
    }
}
Also used : JsonWriterSettings(org.bson.json.JsonWriterSettings) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 12 with JSONDocument

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);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Query(org.hypertrace.core.documentstore.Query) ArrayList(java.util.ArrayList) BulkUpdateResult(org.hypertrace.core.documentstore.BulkUpdateResult) BulkUpdateRequest(org.hypertrace.core.documentstore.BulkUpdateRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Filter(org.hypertrace.core.documentstore.Filter) MongoCollection(com.mongodb.client.MongoCollection) Collection(org.hypertrace.core.documentstore.Collection) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Test(org.junit.jupiter.api.Test)

Example 13 with JSONDocument

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());
}
Also used : SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Query(org.hypertrace.core.documentstore.Query) MongoCollection(com.mongodb.client.MongoCollection) Collection(org.hypertrace.core.documentstore.Collection) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) JsonNode(com.fasterxml.jackson.databind.JsonNode) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Test(org.junit.jupiter.api.Test)

Example 14 with JSONDocument

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);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Query(org.hypertrace.core.documentstore.Query) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) MongoCollection(com.mongodb.client.MongoCollection) Collection(org.hypertrace.core.documentstore.Collection) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Test(org.junit.jupiter.api.Test)

Example 15 with JSONDocument

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);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JSONDocument(org.hypertrace.core.documentstore.JSONDocument)

Aggregations

JSONDocument (org.hypertrace.core.documentstore.JSONDocument)17 Document (org.hypertrace.core.documentstore.Document)10 Test (org.junit.jupiter.api.Test)10 SingleValueKey (org.hypertrace.core.documentstore.SingleValueKey)9 Collection (org.hypertrace.core.documentstore.Collection)8 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 MongoCollection (com.mongodb.client.MongoCollection)4 Query (org.hypertrace.core.documentstore.Query)4 ResultSetChunk (org.hypertrace.entity.query.service.v1.ResultSetChunk)4 ServiceException (com.google.protobuf.ServiceException)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Entity (org.hypertrace.entity.data.service.v1.Entity)3 EntityQueryRequest (org.hypertrace.entity.query.service.v1.EntityQueryRequest)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BulkUpdateRequest (org.hypertrace.core.documentstore.BulkUpdateRequest)2 BulkUpdateResult (org.hypertrace.core.documentstore.BulkUpdateResult)2 Filter (org.hypertrace.core.documentstore.Filter)2