use of com.google.cloud.videointelligence.v1p3beta1.Entity in project entity-service by hypertrace.
the class EntityKeyTest method doesNotMatchDifferentIdentifyingAttributesSameRequestContext.
@Test
void doesNotMatchDifferentIdentifyingAttributesSameRequestContext() {
Entity diffEntity = this.entityV1.toBuilder().putIdentifyingAttributes("key-2", buildValue("value-2")).build();
assertNotEquals(new EntityKey(REQUEST_CONTEXT_1, this.entityV1), new EntityKey(REQUEST_CONTEXT_1, diffEntity));
}
use of com.google.cloud.videointelligence.v1p3beta1.Entity in project entity-service by hypertrace.
the class EntityQueryServiceImpl method execute.
@Override
public void execute(EntityQueryRequest request, StreamObserver<ResultSetChunk> responseObserver) {
RequestContext requestContext = RequestContext.CURRENT.get();
Optional<String> tenantId = requestContext.getTenantId();
if (tenantId.isEmpty()) {
responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
return;
}
final Iterator<Document> documentIterator;
if (queryAggregationEnabled) {
final Converter<EntityQueryRequest, org.hypertrace.core.documentstore.query.Query> queryConverter = getQueryConverter();
final org.hypertrace.core.documentstore.query.Query query;
try {
query = queryConverter.convert(request, requestContext);
documentIterator = entitiesCollection.aggregate(query);
} catch (final Exception e) {
responseObserver.onError(new ServiceException(e));
return;
}
} else {
// TODO: Optimize this later. For now converting to EDS Query and then again to DocStore
// Query.
Query query = entityQueryConverter.convertToEDSQuery(requestContext, request);
/**
* {@link EntityQueryRequest} selections need to treated differently, since they don't
* transform one to one to {@link org.hypertrace.entity.data.service.v1.EntityDataRequest}
* selections
*/
List<String> docStoreSelections = entityQueryConverter.convertSelectionsToDocStoreSelections(requestContext, request.getSelectionList());
documentIterator = entitiesCollection.search(DocStoreConverter.transform(tenantId.get(), query, docStoreSelections));
}
final DocumentConverter rowConverter = injector.getInstance(DocumentConverter.class);
ResultSetMetadata resultSetMetadata;
try {
resultSetMetadata = this.buildMetadataForSelections(request.getSelectionList());
} catch (final ConversionException e) {
responseObserver.onError(new ServiceException(e));
return;
}
if (!documentIterator.hasNext()) {
ResultSetChunk.Builder resultBuilder = ResultSetChunk.newBuilder();
resultBuilder.setResultSetMetadata(resultSetMetadata);
resultBuilder.setIsLastChunk(true);
resultBuilder.setChunkId(0);
responseObserver.onNext(resultBuilder.build());
responseObserver.onCompleted();
return;
}
boolean isNewChunk = true;
int chunkId = 0, rowCount = 0;
ResultSetChunk.Builder resultBuilder = ResultSetChunk.newBuilder();
while (documentIterator.hasNext()) {
// Set metadata for new chunk
if (isNewChunk) {
resultBuilder.setResultSetMetadata(resultSetMetadata);
isNewChunk = false;
}
try {
final Row row;
if (queryAggregationEnabled) {
row = rowConverter.convertToRow(documentIterator.next(), resultSetMetadata);
resultBuilder.addRow(row);
rowCount++;
} else {
Optional<Entity> entity = DOCUMENT_PARSER.parseOrLog(documentIterator.next(), Entity.newBuilder());
if (entity.isPresent()) {
row = convertToEntityQueryResult(requestContext, entity.get(), request.getSelectionList());
resultBuilder.addRow(row);
rowCount++;
}
}
} catch (final Exception e) {
responseObserver.onError(new ServiceException(e));
return;
}
// current chunk is complete
if (rowCount >= CHUNK_SIZE || !documentIterator.hasNext()) {
resultBuilder.setChunkId(chunkId++);
resultBuilder.setIsLastChunk(!documentIterator.hasNext());
responseObserver.onNext(resultBuilder.build());
resultBuilder = ResultSetChunk.newBuilder();
isNewChunk = true;
rowCount = 0;
}
}
responseObserver.onCompleted();
}
use of com.google.cloud.videointelligence.v1p3beta1.Entity 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 com.google.cloud.videointelligence.v1p3beta1.Entity in project entity-service by hypertrace.
the class EntityDataServiceImpl method upsert.
/**
* Creates or Updates an Entity <br>
* If the entityId is provided it is used as is to update the Entity. If the identifying
* attributes are provided, then the entityId is generated from them If none of the above are
* provided, we error out. The ID of the entity is generated from its identifying attributes.
*
* @param request Entity to be created
* @param responseObserver Observer to be notified on about the Entity creation request
*/
@Override
public void upsert(Entity request, StreamObserver<Entity> 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 {
Entity normalizedEntity = this.entityNormalizer.normalize(tenantId, request);
java.util.Collection<Entity> existingEntityCollection = getExistingEntities(List.of(normalizedEntity));
upsertEntity(tenantId, normalizedEntity.getEntityId(), normalizedEntity.getEntityType(), normalizedEntity, Entity.newBuilder(), entitiesCollection, responseObserver);
entityChangeEventGenerator.sendChangeNotification(RequestContext.CURRENT.get(), existingEntityCollection, List.of(normalizedEntity));
} catch (Throwable throwable) {
LOG.warn("Failed to upsert: {}", request, throwable);
responseObserver.onError(throwable);
}
}
use of com.google.cloud.videointelligence.v1p3beta1.Entity 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);
}
}
Aggregations