Search in sources :

Example 11 with Key

use of org.hypertrace.core.documentstore.Key in project document-store by hypertrace.

the class MongoCollection method bulkUpsertImpl.

private BulkWriteResult bulkUpsertImpl(Map<Key, Document> documents) throws JsonProcessingException {
    List<UpdateOneModel<BasicDBObject>> bulkCollection = new ArrayList<>();
    for (Entry<Key, Document> entry : documents.entrySet()) {
        Key key = entry.getKey();
        // insert or overwrite
        bulkCollection.add(new UpdateOneModel<>(this.selectionCriteriaForKey(key), prepareUpsert(key, entry.getValue()), new UpdateOptions().upsert(true)));
    }
    return Failsafe.with(bulkWriteRetryPolicy).get(() -> collection.bulkWrite(bulkCollection, new BulkWriteOptions().ordered(false)));
}
Also used : UpdateOneModel(com.mongodb.client.model.UpdateOneModel) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) ArrayList(java.util.ArrayList) ReturnDocument(com.mongodb.client.model.ReturnDocument) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Key(org.hypertrace.core.documentstore.Key) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions)

Example 12 with Key

use of org.hypertrace.core.documentstore.Key in project document-store by hypertrace.

the class MongoCollection method bulkUpdateSubDocs.

@Override
public BulkUpdateResult bulkUpdateSubDocs(Map<Key, Map<String, Document>> documents) throws Exception {
    List<UpdateManyModel<BasicDBObject>> bulkWriteUpdate = new ArrayList<>();
    for (Key key : documents.keySet()) {
        Map<String, Document> subDocuments = documents.get(key);
        List<BasicDBObject> updateOperations = new ArrayList<>();
        for (String subDocPath : subDocuments.keySet()) {
            Document subDocument = subDocuments.get(subDocPath);
            try {
                /* Wrapping the subDocument with $literal to be able to provide empty object "{}" as value
           *  Throws error otherwise if empty object is provided as value.
           *  https://jira.mongodb.org/browse/SERVER-54046 */
                BasicDBObject literalObject = new BasicDBObject("$literal", getSanitizedObject(subDocument));
                BasicDBObject dbObject = new BasicDBObject(subDocPath, literalObject);
                dbObject.append(LAST_UPDATED_TIME, System.currentTimeMillis());
                BasicDBObject setObject = new BasicDBObject("$set", dbObject);
                updateOperations.add(setObject);
            } catch (Exception e) {
                LOGGER.error("Exception updating document. key: {} content:{}", key, subDocument);
                throw e;
            }
        }
        bulkWriteUpdate.add(new UpdateManyModel(selectionCriteriaForKey(key), updateOperations, new UpdateOptions()));
    }
    if (bulkWriteUpdate.isEmpty()) {
        return new BulkUpdateResult(0);
    }
    BulkWriteResult writeResult = collection.bulkWrite(bulkWriteUpdate);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Write result: " + writeResult);
    }
    return new BulkUpdateResult(writeResult.getModifiedCount());
}
Also used : ArrayList(java.util.ArrayList) BulkUpdateResult(org.hypertrace.core.documentstore.BulkUpdateResult) ReturnDocument(com.mongodb.client.model.ReturnDocument) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) MongoBulkWriteException(com.mongodb.MongoBulkWriteException) MongoServerException(com.mongodb.MongoServerException) MongoCommandException(com.mongodb.MongoCommandException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) BasicDBObject(com.mongodb.BasicDBObject) UpdateManyModel(com.mongodb.client.model.UpdateManyModel) Key(org.hypertrace.core.documentstore.Key)

Example 13 with Key

use of org.hypertrace.core.documentstore.Key in project document-store by hypertrace.

the class MongoQueryExecutorIntegrationTest method init.

@BeforeAll
public static void init() throws IOException {
    mongo = new GenericContainer<>(DockerImageName.parse("mongo:4.4.0")).withExposedPorts(27017).waitingFor(Wait.forListeningPort());
    mongo.start();
    DatastoreProvider.register("MONGO", MongoDatastore.class);
    Map<String, String> mongoConfig = new HashMap<>();
    mongoConfig.putIfAbsent("host", "localhost");
    mongoConfig.putIfAbsent("port", mongo.getMappedPort(27017).toString());
    Config config = ConfigFactory.parseMap(mongoConfig);
    Datastore datastore = DatastoreProvider.getDatastore("Mongo", config);
    datastore.deleteCollection(COLLECTION_NAME);
    datastore.createCollection(COLLECTION_NAME, null);
    collection = datastore.getCollection(COLLECTION_NAME);
    Map<Key, Document> documents = createDocumentsFromResource("mongo/collection_data.json");
    collection.bulkUpsert(documents);
}
Also used : Datastore(org.hypertrace.core.documentstore.Datastore) HashMap(java.util.HashMap) Config(com.typesafe.config.Config) Document(org.hypertrace.core.documentstore.Document) Key(org.hypertrace.core.documentstore.Key) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 14 with Key

use of org.hypertrace.core.documentstore.Key in project document-store by hypertrace.

the class MongoDocStoreTest method testReturnAndBulkUpsert.

@Test
public void testReturnAndBulkUpsert() throws IOException {
    datastore.createCollection(COLLECTION_NAME, null);
    Collection collection = datastore.getCollection(COLLECTION_NAME);
    Map<Key, Document> documentMapV1 = Map.of(new SingleValueKey("default", "testKey1"), Utils.createDocument("id", "1", "testKey1", "abc-v1"), new SingleValueKey("default", "testKey2"), Utils.createDocument("id", "2", "testKey2", "xyz-v1"));
    Iterator<Document> iterator = collection.bulkUpsertAndReturnOlderDocuments(documentMapV1);
    // Initially there shouldn't be any documents.
    Assertions.assertFalse(iterator.hasNext());
    // Add more details to the document and bulk upsert again.
    Map<Key, Document> documentMapV2 = Map.of(new SingleValueKey("default", "testKey1"), Utils.createDocument("id", "1", "testKey1", "abc-v2"), new SingleValueKey("default", "testKey2"), Utils.createDocument("id", "2", "testKey2", "xyz-v2"));
    iterator = collection.bulkUpsertAndReturnOlderDocuments(documentMapV2);
    assertEquals(2, collection.count());
    List<Document> documents = new ArrayList<>();
    while (iterator.hasNext()) {
        documents.add(iterator.next());
    }
    assertEquals(2, documents.size());
    Map<String, JsonNode> expectedDocs = convertToMap(documentMapV1.values(), "id");
    Map<String, JsonNode> actualDocs = convertToMap(documents, "id");
    // Verify that the documents returned were previous copies.
    for (Map.Entry<String, JsonNode> entry : expectedDocs.entrySet()) {
        JsonNode expected = entry.getValue();
        JsonNode actual = actualDocs.get(entry.getKey());
        Assertions.assertNotNull(actual);
        JsonNode patch = JsonDiff.asJson(expected, actual);
        // Verify that there are only additions and "no" removals in this new node.
        Set<String> ops = new HashSet<>();
        patch.elements().forEachRemaining(e -> {
            if (e.has("op")) {
                ops.add(e.get("op").asText());
            }
        });
        Assertions.assertTrue(ops.contains("add"));
        Assertions.assertEquals(1, ops.size());
    }
    // Delete one of the documents and test again.
    collection.delete(new SingleValueKey("default", "testKey1"));
    assertEquals(1, collection.count());
}
Also used : 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) Map(java.util.Map) HashMap(java.util.HashMap) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Key(org.hypertrace.core.documentstore.Key) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 15 with Key

use of org.hypertrace.core.documentstore.Key in project document-store by hypertrace.

the class MongoDocStoreTest method test_bulkOperationOnArrayValue_setOperation.

@Test
public void test_bulkOperationOnArrayValue_setOperation() throws Exception {
    datastore.createCollection(COLLECTION_NAME, null);
    Collection collection = datastore.getCollection(COLLECTION_NAME);
    Key key1 = new SingleValueKey("default", "testKey1");
    Document key1InsertedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey1"), ImmutablePair.of("attributes", Map.of("name", "testKey1", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label1"))))))));
    Document key1ExpectedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey1"), ImmutablePair.of("attributes", Map.of("name", "testKey1", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label2")), ImmutablePair.of("value", Map.of("string", "Label3"))))))));
    collection.upsert(key1, key1InsertedDocument);
    Key key2 = new SingleValueKey("default", "testKey2");
    Document key2InsertedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey2"), ImmutablePair.of("attributes", Map.of("name", "testKey2", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label2"))))))));
    Document key2ExpectedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey2"), ImmutablePair.of("attributes", Map.of("name", "testKey2", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label2")), ImmutablePair.of("value", Map.of("string", "Label3"))))))));
    collection.upsert(key2, key2InsertedDocument);
    Key key3 = new SingleValueKey("default", "testKey3");
    Document key3InsertedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey3"), ImmutablePair.of("attributes", Map.of("name", "testKey3")));
    Document key3ExpectedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey3"), ImmutablePair.of("attributes", Map.of("name", "testKey3", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label2")), ImmutablePair.of("value", Map.of("string", "Label3"))))))));
    collection.upsert(key3, key3InsertedDocument);
    Key key4 = new SingleValueKey("default", "testKey4");
    Document key4InsertedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey4"), ImmutablePair.of("attributes", Map.of("name", "testKey4", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label1")), ImmutablePair.of("value", Map.of("string", "Label2")), ImmutablePair.of("value", Map.of("string", "Label3"))))))));
    Document key4ExpectedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey4"), ImmutablePair.of("attributes", Map.of("name", "testKey4", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label2")), ImmutablePair.of("value", Map.of("string", "Label3"))))))));
    collection.upsert(key4, key4InsertedDocument);
    Document label2Document = Utils.createDocument(ImmutablePair.of("value", Map.of("string", "Label2")));
    Document label3Document = Utils.createDocument(ImmutablePair.of("value", Map.of("string", "Label3")));
    List<Document> subDocuments = List.of(label2Document, label3Document);
    BulkArrayValueUpdateRequest bulkArrayValueUpdateRequest = new BulkArrayValueUpdateRequest(Set.of(key1, key2, key3, key4), "attributes.labels.valueList.values", SET, subDocuments);
    BulkUpdateResult bulkUpdateResult = collection.bulkOperationOnArrayValue(bulkArrayValueUpdateRequest);
    assertEquals(4, bulkUpdateResult.getUpdatedCount());
    // get all documents
    Query query = new Query();
    Iterator<Document> results = collection.search(query);
    List<Document> documents = new ArrayList<>();
    while (results.hasNext()) {
        documents.add(results.next());
    }
    assertEquals(4, documents.size());
    Map<String, JsonNode> actualDocs = convertToMap(documents, "id");
    Map<String, JsonNode> expectedDocs = convertToMap(List.of(key1ExpectedDocument, key2ExpectedDocument, key3ExpectedDocument, key4ExpectedDocument), "id");
    // Verify that the documents returned are as expected
    for (Map.Entry<String, JsonNode> entry : actualDocs.entrySet()) {
        String key = entry.getKey();
        JsonNode attributesJsonNode = entry.getValue().get("attributes");
        JsonNode expectedAttributesJsonNode = expectedDocs.get(key).get("attributes");
        assertEquals(expectedAttributesJsonNode, attributesJsonNode);
    }
}
Also used : Query(org.hypertrace.core.documentstore.Query) BulkUpdateResult(org.hypertrace.core.documentstore.BulkUpdateResult) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) BulkArrayValueUpdateRequest(org.hypertrace.core.documentstore.BulkArrayValueUpdateRequest) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) MongoCollection(com.mongodb.client.MongoCollection) Collection(org.hypertrace.core.documentstore.Collection) Map(java.util.Map) HashMap(java.util.HashMap) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Key(org.hypertrace.core.documentstore.Key) Test(org.junit.jupiter.api.Test)

Aggregations

Key (org.hypertrace.core.documentstore.Key)22 Document (org.hypertrace.core.documentstore.Document)19 JSONDocument (org.hypertrace.core.documentstore.JSONDocument)17 HashMap (java.util.HashMap)14 ArrayList (java.util.ArrayList)12 Map (java.util.Map)12 Collection (org.hypertrace.core.documentstore.Collection)9 SingleValueKey (org.hypertrace.core.documentstore.SingleValueKey)9 ServiceException (com.google.protobuf.ServiceException)8 IOException (java.io.IOException)6 BulkUpdateResult (org.hypertrace.core.documentstore.BulkUpdateResult)6 BulkArrayValueUpdateRequest (org.hypertrace.core.documentstore.BulkArrayValueUpdateRequest)5 Query (org.hypertrace.core.documentstore.Query)5 Entity (org.hypertrace.entity.data.service.v1.Entity)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 MongoCollection (com.mongodb.client.MongoCollection)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 Datastore (org.hypertrace.core.documentstore.Datastore)4 Test (org.junit.jupiter.api.Test)4