Search in sources :

Example 1 with BulkUpdateRequest

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

the class MongoDocStoreTest method whenBulkUpdatingNonExistentRecords_thenExpectNothingToBeUpdatedOrCreated.

@Test
public void whenBulkUpdatingNonExistentRecords_thenExpectNothingToBeUpdatedOrCreated() throws Exception {
    Collection collection = datastore.getCollection(COLLECTION_NAME);
    ObjectNode objectNode = OBJECT_MAPPER.createObjectNode();
    objectNode.put("foo1", "bar1");
    objectNode.put("timestamp", 100);
    List<BulkUpdateRequest> toUpdate = new ArrayList<>();
    toUpdate.add(new BulkUpdateRequest(new SingleValueKey("tenant-1", "testKey1"), new JSONDocument(objectNode), new Filter(Op.LT, "timestamp", 100)));
    toUpdate.add(new BulkUpdateRequest(new SingleValueKey("tenant-1", "testKey2"), new JSONDocument(objectNode), new Filter(Op.LT, "timestamp", 100)));
    BulkUpdateResult result = collection.bulkUpdate(toUpdate);
    Assertions.assertEquals(0, 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);
    assertFalse(it.hasNext());
}
Also used : SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Query(org.hypertrace.core.documentstore.Query) Filter(org.hypertrace.core.documentstore.Filter) ArrayList(java.util.ArrayList) BulkUpdateResult(org.hypertrace.core.documentstore.BulkUpdateResult) MongoCollection(com.mongodb.client.MongoCollection) Collection(org.hypertrace.core.documentstore.Collection) BulkUpdateRequest(org.hypertrace.core.documentstore.BulkUpdateRequest) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Test(org.junit.jupiter.api.Test)

Example 2 with BulkUpdateRequest

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

the class MongoCollection method bulkUpdateImpl.

private BulkWriteResult bulkUpdateImpl(List<BulkUpdateRequest> bulkUpdateRequests) throws JsonProcessingException {
    List<UpdateOneModel<BasicDBObject>> bulkCollection = new ArrayList<>();
    for (BulkUpdateRequest bulkUpdateRequest : bulkUpdateRequests) {
        Key key = bulkUpdateRequest.getKey();
        Map<String, Object> conditionMap = bulkUpdateRequest.getFilter() == null ? new HashMap<>() : MongoQueryParser.parseFilter(bulkUpdateRequest.getFilter());
        conditionMap.put(ID_KEY, key.toString());
        BasicDBObject conditionObject = new BasicDBObject(conditionMap);
        // update if filter condition is satisfied
        bulkCollection.add(new UpdateOneModel<>(conditionObject, prepareUpsert(key, bulkUpdateRequest.getDocument()), new UpdateOptions().upsert(false)));
    }
    return Failsafe.with(bulkWriteRetryPolicy).get(() -> collection.bulkWrite(bulkCollection, new BulkWriteOptions().ordered(false)));
}
Also used : ArrayList(java.util.ArrayList) BulkUpdateRequest(org.hypertrace.core.documentstore.BulkUpdateRequest) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) BasicDBObject(com.mongodb.BasicDBObject) UpdateOneModel(com.mongodb.client.model.UpdateOneModel) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) BasicDBObject(com.mongodb.BasicDBObject) Key(org.hypertrace.core.documentstore.Key)

Example 3 with BulkUpdateRequest

use of org.hypertrace.core.documentstore.BulkUpdateRequest 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)

Aggregations

ArrayList (java.util.ArrayList)3 BulkUpdateRequest (org.hypertrace.core.documentstore.BulkUpdateRequest)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 MongoCollection (com.mongodb.client.MongoCollection)2 BulkUpdateResult (org.hypertrace.core.documentstore.BulkUpdateResult)2 Collection (org.hypertrace.core.documentstore.Collection)2 Document (org.hypertrace.core.documentstore.Document)2 Filter (org.hypertrace.core.documentstore.Filter)2 JSONDocument (org.hypertrace.core.documentstore.JSONDocument)2 Query (org.hypertrace.core.documentstore.Query)2 SingleValueKey (org.hypertrace.core.documentstore.SingleValueKey)2 Test (org.junit.jupiter.api.Test)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 BasicDBObject (com.mongodb.BasicDBObject)1 BulkWriteOptions (com.mongodb.client.model.BulkWriteOptions)1 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)1 UpdateOneModel (com.mongodb.client.model.UpdateOneModel)1 UpdateOptions (com.mongodb.client.model.UpdateOptions)1 Key (org.hypertrace.core.documentstore.Key)1