Search in sources :

Example 1 with EntityRelationships

use of org.hypertrace.entity.data.service.v1.EntityRelationships in project entity-service by hypertrace.

the class EntityDataServiceImpl method upsertRelationships.

@Override
public void upsertRelationships(EntityRelationships 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;
    }
    Map<Key, Document> entityRelations = new HashMap<>();
    try {
        for (EntityRelationship relationship : request.getRelationshipList()) {
            if (StringUtils.isEmpty(relationship.getFromEntityId()) || StringUtils.isEmpty(relationship.getToEntityId()) || StringUtils.isEmpty(relationship.getEntityRelationshipType())) {
                LOG.warn("Invalid relationship upsert request:{}", relationship);
                continue;
            }
            Document document = convertEntityRelationshipToDocument(tenantId, relationship);
            Key key = new EntityRelationshipDocKey(tenantId, relationship.getEntityRelationshipType(), relationship.getFromEntityId(), relationship.getToEntityId());
            entityRelations.put(key, document);
        }
        boolean status = relationshipsCollection.bulkUpsert(entityRelations);
        if (status) {
            responseObserver.onNext(Empty.newBuilder().build());
            responseObserver.onCompleted();
        } else {
            responseObserver.onError(new RuntimeException("Could not bulk upsert relationships."));
        }
    } catch (IOException e) {
        LOG.error("Failed to bulk upsert relationships.", e);
        responseObserver.onError(e);
    }
}
Also used : ServiceException(com.google.protobuf.ServiceException) HashMap(java.util.HashMap) EntityRelationship(org.hypertrace.entity.data.service.v1.EntityRelationship) IOException(java.io.IOException) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Key(org.hypertrace.core.documentstore.Key)

Aggregations

ServiceException (com.google.protobuf.ServiceException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Document (org.hypertrace.core.documentstore.Document)1 JSONDocument (org.hypertrace.core.documentstore.JSONDocument)1 Key (org.hypertrace.core.documentstore.Key)1 EntityRelationship (org.hypertrace.entity.data.service.v1.EntityRelationship)1