use of org.hypertrace.core.documentstore.Document in project entity-service by hypertrace.
the class EntityQueryServiceImpl method doUpdate.
private void doUpdate(RequestContext requestContext, EntityUpdateRequest request) throws Exception {
if (request.getOperation().hasSetAttribute()) {
SetAttribute setAttribute = request.getOperation().getSetAttribute();
String attributeId = setAttribute.getAttribute().getColumnName();
String subDocPath = entityAttributeMapping.getDocStorePathByAttributeId(requestContext, attributeId).orElseThrow(() -> new IllegalArgumentException("Unknown attribute FQN " + attributeId));
JSONDocument jsonDocument = convertToJsonDocument(setAttribute.getValue());
Map<Key, Map<String, Document>> entitiesUpdateMap = new HashMap<>();
for (String entityId : request.getEntityIdsList()) {
SingleValueKey key = new SingleValueKey(requestContext.getTenantId().orElseThrow(), entityId);
if (entitiesUpdateMap.containsKey(key)) {
entitiesUpdateMap.get(key).put(subDocPath, jsonDocument);
} else {
Map<String, Document> subDocument = new HashMap<>();
subDocument.put(subDocPath, jsonDocument);
entitiesUpdateMap.put(key, subDocument);
}
}
try {
entitiesCollection.bulkUpdateSubDocs(entitiesUpdateMap);
} catch (Exception e) {
LOG.error("Failed to update entities {}, subDocPath {}, with new doc {}.", entitiesUpdateMap, subDocPath, jsonDocument, e);
throw e;
}
}
}
use of org.hypertrace.core.documentstore.Document in project entity-service by hypertrace.
the class EntityQueryServiceImpl method transformUpdateOperations.
private Map<String, Document> transformUpdateOperations(List<UpdateOperation> updateOperationList, RequestContext requestContext) throws Exception {
Map<String, Document> documentMap = new HashMap<>();
for (UpdateOperation updateOperation : updateOperationList) {
if (!updateOperation.hasSetAttribute()) {
continue;
}
SetAttribute setAttribute = updateOperation.getSetAttribute();
String attributeId = setAttribute.getAttribute().getColumnName();
String subDocPath = entityAttributeMapping.getDocStorePathByAttributeId(requestContext, attributeId).orElseThrow(() -> new IllegalArgumentException("Unknown attribute FQN " + attributeId));
try {
documentMap.put(subDocPath, convertToJsonDocument(setAttribute.getValue()));
} catch (Exception e) {
LOG.error("Failed to put update corresponding to {} in the documentMap", subDocPath, e);
throw e;
}
}
return Collections.unmodifiableMap(documentMap);
}
use of org.hypertrace.core.documentstore.Document in project entity-service by hypertrace.
the class EntityDataServiceImpl method upsertEntity.
private Entity upsertEntity(String tenantId, Entity entity) throws IOException {
Document document = convertEntityToDocument(entity);
Document result = entitiesCollection.upsertAndReturn(this.entityNormalizer.getEntityDocKey(tenantId, entity), document);
return this.entityFromDocument(result).orElseThrow();
}
use of org.hypertrace.core.documentstore.Document 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());
}
use of org.hypertrace.core.documentstore.Document 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)));
}
Aggregations