use of org.hypertrace.core.documentstore.Key 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.Key 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.Key in project entity-service by hypertrace.
the class EntityDataServiceImpl method delete.
/**
* Deletes an Entity by the EntityId and EntityType
*
* @param request ID of the entity to be deleted
* @param responseObserver Observer to be notified on about the Entity delete request
*/
@Override
public void delete(ByIdRequest request, StreamObserver<Empty> responseObserver) {
if (StringUtils.isEmpty(request.getEntityId())) {
LOG.info("{}. Invalid delete request:{}", request, ErrorMessages.ENTITY_ID_EMPTY);
responseObserver.onError(new RuntimeException(ErrorMessages.ENTITY_ID_EMPTY));
return;
}
Optional<String> tenantId = RequestContext.CURRENT.get().getTenantId();
if (tenantId.isEmpty()) {
responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
return;
}
Optional<Entity> existingEntity = getExistingEntity(tenantId.get(), request.getEntityType(), request.getEntityId());
Key key = this.entityNormalizer.getEntityDocKey(tenantId.get(), request.getEntityType(), request.getEntityId());
if (entitiesCollection.delete(key)) {
responseObserver.onNext(Empty.newBuilder().build());
responseObserver.onCompleted();
existingEntity.ifPresent(entity -> entityChangeEventGenerator.sendDeleteNotification(RequestContext.CURRENT.get(), List.of(entity)));
} else {
responseObserver.onError(new RuntimeException("Could not delete the entity."));
}
}
use of org.hypertrace.core.documentstore.Key in project entity-service by hypertrace.
the class EntityDataServiceImpl method upsertEntities.
@Override
public void upsertEntities(Entities request, StreamObserver<Empty> responseObserver) {
String tenantId = RequestContext.CURRENT.get().getTenantId().orElse(null);
if (tenantId == null) {
responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
return;
}
try {
Map<Key, Entity> entities = request.getEntityList().stream().map(entity -> this.entityNormalizer.normalize(tenantId, entity)).collect(Collectors.toUnmodifiableMap(entity -> this.entityNormalizer.getEntityDocKey(tenantId, entity), Function.identity()));
java.util.Collection<Entity> existingEntities = getExistingEntities(entities.values());
upsertEntities(entities, entitiesCollection, responseObserver);
entityChangeEventGenerator.sendChangeNotification(RequestContext.CURRENT.get(), existingEntities, entities.values());
} catch (Throwable throwable) {
LOG.warn("Failed to upsert: {}", request, throwable);
responseObserver.onError(throwable);
}
}
use of org.hypertrace.core.documentstore.Key in project entity-service by hypertrace.
the class EntityDataServiceImpl method upsertEnrichedEntities.
@Override
public void upsertEnrichedEntities(EnrichedEntities request, StreamObserver<Empty> responseObserver) {
String tenantId = RequestContext.CURRENT.get().getTenantId().orElse(null);
if (isNull(tenantId)) {
responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
return;
}
for (EnrichedEntity entity : request.getEntitiesList()) {
if (StringUtils.isEmpty(entity.getEntityType())) {
LOG.info("{}. Invalid upsertEnrichedEntities request:{}", entity, ErrorMessages.ENTITY_TYPE_EMPTY);
responseObserver.onError(new RuntimeException(ErrorMessages.ENTITY_TYPE_EMPTY));
return;
}
if (StringUtils.isEmpty(entity.getEntityId())) {
LOG.info("{}. Invalid upsertEnrichedEntities request:{}", entity, ErrorMessages.ENTITY_ID_EMPTY);
responseObserver.onError(new RuntimeException(ErrorMessages.ENTITY_ID_EMPTY));
return;
}
}
Map<Key, EnrichedEntity> entities = request.getEntitiesList().stream().collect(Collectors.toUnmodifiableMap(entity -> this.entityNormalizer.getEntityDocKey(tenantId, entity.getEntityType(), entity.getEntityId()), Function.identity()));
upsertEntities(entities, enrichedEntitiesCollection, responseObserver);
}
Aggregations