Search in sources :

Example 66 with Document

use of org.hypertrace.core.documentstore.Document 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

Document (org.hypertrace.core.documentstore.Document)66 JSONDocument (org.hypertrace.core.documentstore.JSONDocument)41 Test (org.junit.jupiter.api.Test)37 SingleValueKey (org.hypertrace.core.documentstore.SingleValueKey)22 Collection (org.hypertrace.core.documentstore.Collection)21 ArrayList (java.util.ArrayList)20 Key (org.hypertrace.core.documentstore.Key)18 Query (org.hypertrace.core.documentstore.query.Query)18 ServiceException (com.google.protobuf.ServiceException)14 HashMap (java.util.HashMap)14 Query (org.hypertrace.core.documentstore.Query)14 MongoCollection (com.mongodb.client.MongoCollection)12 Map (java.util.Map)12 IOException (java.io.IOException)10 BulkUpdateResult (org.hypertrace.core.documentstore.BulkUpdateResult)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 Filter (org.hypertrace.core.documentstore.Filter)8 Entity (org.hypertrace.entity.data.service.v1.Entity)7 BulkArrayValueUpdateRequest (org.hypertrace.core.documentstore.BulkArrayValueUpdateRequest)6 Filter (org.hypertrace.core.documentstore.query.Filter)6