Search in sources :

Example 1 with EntityTypeDocument

use of org.hypertrace.entity.type.service.v2.model.EntityTypeDocument in project entity-service by hypertrace.

the class EntityTypeServiceImpl method upsertEntityType.

@Override
public void upsertEntityType(org.hypertrace.entity.type.service.v2.UpsertEntityTypeRequest request, io.grpc.stub.StreamObserver<org.hypertrace.entity.type.service.v2.UpsertEntityTypeResponse> responseObserver) {
    Optional<String> tenantId = RequestContext.CURRENT.get().getTenantId();
    if (tenantId.isEmpty()) {
        responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
        return;
    }
    // validate the request.
    if (!isValidUpsert(request.getEntityType())) {
        responseObserver.onError(new ServiceException("Invalid upsert request."));
        return;
    }
    try {
        // Set tenant id.
        EntityTypeDocument document = EntityTypeDocument.fromProto(tenantId.get(), request.getEntityType());
        entityTypeCollection.upsert(new SingleValueKey(tenantId.get(), request.getEntityType().getName()), document);
        // Query the entity type again and return that.
        Iterator<Document> entityTypes = entityTypeCollection.search(transform(tenantId.get(), List.of(request.getEntityType().getName())));
        if (entityTypes.hasNext()) {
            String json = entityTypes.next().toJson();
            responseObserver.onNext(UpsertEntityTypeResponse.newBuilder().setEntityType(EntityTypeDocument.fromJson(json).toProto()).build());
            responseObserver.onCompleted();
        } else {
            // This could happen if the upsert has failed to make the doc store.
            // Return an error for now.
            responseObserver.onError(new RuntimeException("Error upserting EntityType:" + request));
        }
    } catch (IOException e) {
        responseObserver.onError(new RuntimeException("Error upserting EntityType:" + request, e));
    }
}
Also used : SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) ServiceException(com.google.protobuf.ServiceException) EntityTypeDocument(org.hypertrace.entity.type.service.v2.model.EntityTypeDocument) IOException(java.io.IOException) Document(org.hypertrace.core.documentstore.Document) EntityTypeDocument(org.hypertrace.entity.type.service.v2.model.EntityTypeDocument)

Aggregations

ServiceException (com.google.protobuf.ServiceException)1 IOException (java.io.IOException)1 Document (org.hypertrace.core.documentstore.Document)1 SingleValueKey (org.hypertrace.core.documentstore.SingleValueKey)1 EntityTypeDocument (org.hypertrace.entity.type.service.v2.model.EntityTypeDocument)1