Search in sources :

Example 1 with DocumentConverter

use of org.hypertrace.entity.query.service.converter.response.DocumentConverter 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();
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) Query(org.hypertrace.entity.data.service.v1.Query) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) ConversionException(org.hypertrace.entity.query.service.converter.ConversionException) ResultSetMetadata(org.hypertrace.entity.query.service.v1.ResultSetMetadata) ServiceException(com.google.protobuf.ServiceException) ConversionException(org.hypertrace.entity.query.service.converter.ConversionException) ServiceException(com.google.protobuf.ServiceException) Row(org.hypertrace.entity.query.service.v1.Row) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) DocumentConverter(org.hypertrace.entity.query.service.converter.response.DocumentConverter) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk)

Example 2 with DocumentConverter

use of org.hypertrace.entity.query.service.converter.response.DocumentConverter in project entity-service by hypertrace.

the class EntityQueryServiceImpl method convertDocumentsToResultSetChunk.

private ResultSetChunk convertDocumentsToResultSetChunk(final List<Document> documents, final List<Expression> selections) throws ConversionException {
    final DocumentConverter documentConverter = injector.getInstance(DocumentConverter.class);
    final ResultSetMetadata resultSetMetadata = this.buildMetadataForSelections(selections);
    ResultSetChunk.Builder resultBuilder = ResultSetChunk.newBuilder();
    // Build metadata
    resultBuilder.setResultSetMetadata(resultSetMetadata);
    // Build data
    for (final Document document : documents) {
        final Row row = documentConverter.convertToRow(document, resultSetMetadata);
        resultBuilder.addRow(row);
    }
    return resultBuilder.build();
}
Also used : ResultSetMetadata(org.hypertrace.entity.query.service.v1.ResultSetMetadata) Row(org.hypertrace.entity.query.service.v1.Row) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) DocumentConverter(org.hypertrace.entity.query.service.converter.response.DocumentConverter) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk)

Aggregations

Document (org.hypertrace.core.documentstore.Document)2 JSONDocument (org.hypertrace.core.documentstore.JSONDocument)2 DocumentConverter (org.hypertrace.entity.query.service.converter.response.DocumentConverter)2 ResultSetChunk (org.hypertrace.entity.query.service.v1.ResultSetChunk)2 ResultSetMetadata (org.hypertrace.entity.query.service.v1.ResultSetMetadata)2 Row (org.hypertrace.entity.query.service.v1.Row)2 ServiceException (com.google.protobuf.ServiceException)1 RequestContext (org.hypertrace.core.grpcutils.context.RequestContext)1 Entity (org.hypertrace.entity.data.service.v1.Entity)1 Query (org.hypertrace.entity.data.service.v1.Query)1 ConversionException (org.hypertrace.entity.query.service.converter.ConversionException)1 EntityQueryRequest (org.hypertrace.entity.query.service.v1.EntityQueryRequest)1