use of org.hypertrace.core.documentstore.BulkUpdateResult in project document-store by hypertrace.
the class MongoDocStoreTest method test_bulkOperationOnArrayValue_addOperation.
@Test
public void test_bulkOperationOnArrayValue_addOperation() 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", "Label1")), 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", "Label1")), 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", ADD, 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);
}
}
use of org.hypertrace.core.documentstore.BulkUpdateResult 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.BulkUpdateResult in project document-store by hypertrace.
the class MongoDocStoreTest method test_bulkOperationOnArrayValue_removeOperation.
@Test
public void test_bulkOperationOnArrayValue_removeOperation() 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", "Label1"))))))));
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", "Label1")), 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", "Label1"))))))));
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 = key3InsertedDocument;
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", "Label1"))))))));
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", REMOVE, 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);
}
}
use of org.hypertrace.core.documentstore.BulkUpdateResult in project document-store by hypertrace.
the class PostgresCollection method bulkUpdateSubDocs.
@Override
public BulkUpdateResult bulkUpdateSubDocs(Map<Key, Map<String, Document>> documents) throws Exception {
String updateSubDocSQL = String.format("UPDATE %s SET %s=jsonb_set(%s, ?::text[], ?::jsonb) WHERE %s = ?", collectionName, DOCUMENT, DOCUMENT, ID);
try {
PreparedStatement preparedStatement = client.prepareStatement(updateSubDocSQL);
for (Key key : documents.keySet()) {
Map<String, Document> subDocuments = documents.get(key);
for (String subDocPath : subDocuments.keySet()) {
Document subDocument = subDocuments.get(subDocPath);
String jsonSubDocPath = getJsonSubDocPath(subDocPath);
String jsonString = subDocument.toJson();
preparedStatement.setString(1, jsonSubDocPath);
preparedStatement.setString(2, jsonString);
preparedStatement.setString(3, key.toString());
preparedStatement.addBatch();
}
}
int[] updateCounts = preparedStatement.executeBatch();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Write result: {}", updateCounts);
}
int totalUpdateCount = 0;
for (int update : updateCounts) {
totalUpdateCount += update;
}
return new BulkUpdateResult(totalUpdateCount);
} catch (SQLException e) {
LOGGER.error("SQLException updating sub document.", e);
throw e;
}
}
use of org.hypertrace.core.documentstore.BulkUpdateResult in project document-store by hypertrace.
the class MongoCollection method bulkOperationOnArrayValue.
@Override
public BulkUpdateResult bulkOperationOnArrayValue(BulkArrayValueUpdateRequest request) throws Exception {
List<BasicDBObject> basicDBObjects = new ArrayList<>();
try {
for (Document subDocument : request.getSubDocuments()) {
basicDBObjects.add(getSanitizedObject(subDocument));
}
} catch (Exception e) {
LOGGER.error("Exception updating document. keys: {} operation {} subDocPath {} subDocuments :{}", request.getKeys(), request.getOperation(), request.getSubDocPath(), request.getSubDocuments());
throw e;
}
BasicDBObject operationObject;
switch(request.getOperation()) {
case ADD:
operationObject = getAddOperationObject(request.getSubDocPath(), basicDBObjects);
break;
case REMOVE:
operationObject = getRemoveOperationObject(request.getSubDocPath(), basicDBObjects);
break;
case SET:
operationObject = getSetOperationObject(request.getSubDocPath(), basicDBObjects);
break;
default:
throw new UnsupportedOperationException("Unknown operation : " + request.getOperation());
}
List<UpdateManyModel<BasicDBObject>> bulkWriteUpdate = List.of(new UpdateManyModel(selectionCriteriaForKeys(request.getKeys()), operationObject, new UpdateOptions()));
BulkWriteResult writeResult = collection.bulkWrite(bulkWriteUpdate);
LOGGER.debug("Write result for bulkOperationOnArrayValue: {}", writeResult);
return new BulkUpdateResult(writeResult.getModifiedCount());
}
Aggregations