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));
}
}
Aggregations