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());
}
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)));
}
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);
}
Aggregations