Search in sources :

Example 26 with Filter

use of org.hypertrace.core.documentstore.Filter in project document-store by hypertrace.

the class MongoDocStoreTest method whenBulkUpdatingExistingRecords_thenExpectOnlyRecordsWhoseConditionsMatchToBeUpdated.

@Test
public void whenBulkUpdatingExistingRecords_thenExpectOnlyRecordsWhoseConditionsMatchToBeUpdated() throws Exception {
    Collection collection = datastore.getCollection(COLLECTION_NAME);
    ObjectNode persistedObject = OBJECT_MAPPER.createObjectNode();
    persistedObject.put("foo1", "bar1");
    persistedObject.put("timestamp", 90);
    collection.create(new SingleValueKey("tenant-1", "testKey1"), new JSONDocument(persistedObject));
    ObjectNode updatedObject = OBJECT_MAPPER.createObjectNode();
    updatedObject.put("foo1", "bar1");
    updatedObject.put("timestamp", 110);
    List<BulkUpdateRequest> toUpdate = new ArrayList<>();
    toUpdate.add(new BulkUpdateRequest(new SingleValueKey("tenant-1", "testKey1"), new JSONDocument(updatedObject), new Filter(Op.LT, "timestamp", 100)));
    toUpdate.add(new BulkUpdateRequest(new SingleValueKey("tenant-1", "testKey2"), new JSONDocument(updatedObject), new Filter(Op.LT, "timestamp", 100)));
    BulkUpdateResult result = collection.bulkUpdate(toUpdate);
    Assertions.assertEquals(1, result.getUpdatedCount());
    Query query = new Query();
    query.setFilter(new Filter(Op.EQ, "_id", new SingleValueKey("tenant-1", "testKey1").toString()));
    Iterator<Document> it = collection.search(query);
    JsonNode root = OBJECT_MAPPER.readTree(it.next().toJson());
    Long timestamp = root.findValue("timestamp").asLong();
    Assertions.assertEquals(110, timestamp);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Query(org.hypertrace.core.documentstore.Query) ArrayList(java.util.ArrayList) BulkUpdateResult(org.hypertrace.core.documentstore.BulkUpdateResult) BulkUpdateRequest(org.hypertrace.core.documentstore.BulkUpdateRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Filter(org.hypertrace.core.documentstore.Filter) MongoCollection(com.mongodb.client.MongoCollection) Collection(org.hypertrace.core.documentstore.Collection) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Test(org.junit.jupiter.api.Test)

Example 27 with Filter

use of org.hypertrace.core.documentstore.Filter in project document-store by hypertrace.

the class PostgresQueryParserTest method testJSONFieldParseNestedQuery.

@Test
void testJSONFieldParseNestedQuery() {
    Filter filter1 = new Filter(Filter.Op.EQ, "key1", "val1").and(new Filter(Filter.Op.EQ, "key2", "val2"));
    Filter filter2 = new Filter(Filter.Op.EQ, "key3", "val3").and(new Filter(Filter.Op.EQ, "key4", "val4"));
    Filter filter = filter1.or(filter2);
    String query = PostgresQueryParser.parseFilter(filter, initParams());
    Assertions.assertEquals("((document->>'key1' = ?) AND (document->>'key2' = ?)) " + "OR ((document->>'key3' = ?) AND (document->>'key4' = ?))", query);
}
Also used : Filter(org.hypertrace.core.documentstore.Filter) Test(org.junit.jupiter.api.Test)

Example 28 with Filter

use of org.hypertrace.core.documentstore.Filter in project entity-service by hypertrace.

the class EntityDataServiceImpl method searchByIdAndStreamSingleResponse.

private <T extends Message> void searchByIdAndStreamSingleResponse(String tenantId, String entityId, String entityType, Collection collection, Message.Builder builder, StreamObserver<T> responseObserver) {
    org.hypertrace.core.documentstore.Query query = new org.hypertrace.core.documentstore.Query();
    String docId = this.entityNormalizer.getEntityDocKey(tenantId, entityType, entityId).toString();
    query.setFilter(new Filter(Filter.Op.EQ, EntityServiceConstants.ID, docId));
    Iterator<Document> result = collection.search(query);
    List<T> entities = new ArrayList<>();
    while (result.hasNext()) {
        PARSER.<T>parseOrLog(result.next(), builder.clone()).map(entity -> {
            // Populate the tenant id field with the tenant id that's received for backward
            // compatibility.
            Descriptors.FieldDescriptor fieldDescriptor = entity.getDescriptorForType().findFieldByName("tenant_id");
            if (fieldDescriptor != null) {
                return (T) entity.toBuilder().setField(fieldDescriptor, tenantId).build();
            }
            return entity;
        }).ifPresent(entities::add);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Docstore query has returned the result: {}", entities);
    }
    if (entities.size() == 1) {
        responseObserver.onNext(entities.get(0));
        responseObserver.onCompleted();
    } else if (entities.size() > 1) {
        responseObserver.onError(new IllegalStateException("Multiple entities with same id are found."));
    } else {
        // When there is no result, we should return the default instance, which is a way
        // of saying it's null.
        // TODO : Not convinced with the default instance
        responseObserver.onNext((T) builder.build());
        responseObserver.onCompleted();
    }
}
Also used : ByIdRequest(org.hypertrace.entity.data.service.v1.ByIdRequest) ServiceException(com.google.protobuf.ServiceException) LoggerFactory(org.slf4j.LoggerFactory) Channel(io.grpc.Channel) EntityTypeClient(org.hypertrace.entity.type.service.rxclient.EntityTypeClient) StreamObserver(io.grpc.stub.StreamObserver) Map(java.util.Map) Objects.isNull(java.util.Objects.isNull) ENRICHED_ENTITIES_COLLECTION(org.hypertrace.entity.service.constants.EntityCollectionConstants.ENRICHED_ENTITIES_COLLECTION) DocStoreConverter(org.hypertrace.entity.service.util.DocStoreConverter) InvalidRequestException(org.hypertrace.entity.service.exception.InvalidRequestException) StringUtils(org.hypertrace.entity.service.util.StringUtils) Streams(com.google.common.collect.Streams) Empty(org.hypertrace.entity.data.service.v1.Empty) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) EntityRelationship(org.hypertrace.entity.data.service.v1.EntityRelationship) EntityRelationships(org.hypertrace.entity.data.service.v1.EntityRelationships) MergeAndUpsertEntityRequest(org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityRequest) Optional(java.util.Optional) EntityChangeEventGenerator(org.hypertrace.entity.service.change.event.api.EntityChangeEventGenerator) EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Builder(org.hypertrace.entity.data.service.v1.Entity.Builder) Document(org.hypertrace.core.documentstore.Document) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) ByTypeAndIdentifyingAttributes(org.hypertrace.entity.data.service.v1.ByTypeAndIdentifyingAttributes) DocStoreJsonFormat(org.hypertrace.entity.service.util.DocStoreJsonFormat) Descriptors(com.google.protobuf.Descriptors) Filter(org.hypertrace.core.documentstore.Filter) HashMap(java.util.HashMap) Function(java.util.function.Function) Collection(org.hypertrace.core.documentstore.Collection) Entities(org.hypertrace.entity.data.service.v1.Entities) ArrayList(java.util.ArrayList) Datastore(org.hypertrace.core.documentstore.Datastore) Key(org.hypertrace.core.documentstore.Key) EntityServiceConstants(org.hypertrace.entity.service.constants.EntityServiceConstants) Entity(org.hypertrace.entity.data.service.v1.Entity) EntityDataServiceImplBase(org.hypertrace.entity.data.service.v1.EntityDataServiceGrpc.EntityDataServiceImplBase) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) IOException(java.io.IOException) ENTITY_RELATIONSHIPS_COLLECTION(org.hypertrace.entity.service.constants.EntityCollectionConstants.ENTITY_RELATIONSHIPS_COLLECTION) EnrichedEntities(org.hypertrace.entity.data.service.v1.EnrichedEntities) Query(org.hypertrace.entity.data.service.v1.Query) RAW_ENTITIES_COLLECTION(org.hypertrace.entity.service.constants.EntityCollectionConstants.RAW_ENTITIES_COLLECTION) Message(com.google.protobuf.Message) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) MergeAndUpsertEntityResponse(org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityResponse) Collections(java.util.Collections) RelationshipsQuery(org.hypertrace.entity.data.service.v1.RelationshipsQuery) Query(org.hypertrace.entity.data.service.v1.Query) RelationshipsQuery(org.hypertrace.entity.data.service.v1.RelationshipsQuery) ArrayList(java.util.ArrayList) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Filter(org.hypertrace.core.documentstore.Filter)

Example 29 with Filter

use of org.hypertrace.core.documentstore.Filter in project entity-service by hypertrace.

the class IdentifyingAttributeCache method loadEntityTypes.

private Map<String, List<AttributeType>> loadEntityTypes(String tenantId) {
    Query query = new Query();
    query.setFilter(new Filter(Op.IN, EntityServiceConstants.TENANT_ID, TenantUtils.getTenantHierarchy(tenantId)));
    return Streams.stream(entityTypesCollection.search(query)).flatMap(this::buildEntityType).collect(Collectors.toUnmodifiableMap(EntityType::getName, this::getIdentifyingAttributesFromType));
}
Also used : Query(org.hypertrace.core.documentstore.Query) Filter(org.hypertrace.core.documentstore.Filter)

Example 30 with Filter

use of org.hypertrace.core.documentstore.Filter in project entity-service by hypertrace.

the class DocStoreConverterTest method testEntityFieldsQueryConversion.

@Test
public void testEntityFieldsQueryConversion() {
    Query query = Query.newBuilder().addEntityId("some id").build();
    org.hypertrace.core.documentstore.Query transformedQuery = DocStoreConverter.transform(TENANT_ID, query, Collections.emptyList());
    Filter transformedFilter = transformedQuery.getFilter();
    Assertions.assertEquals(Filter.Op.AND, transformedFilter.getOp());
    // Verify that the first filter is based on tenant id.
    Filter tenantIdFilter = transformedFilter.getChildFilters()[0];
    Assertions.assertEquals(Op.EQ, tenantIdFilter.getOp());
    Assertions.assertEquals(TENANT_ID, tenantIdFilter.getValue());
    Assertions.assertEquals(EntityServiceConstants.TENANT_ID, tenantIdFilter.getFieldName());
    Assertions.assertEquals(EntityServiceConstants.ENTITY_ID, transformedFilter.getChildFilters()[1].getFieldName());
    Assertions.assertEquals(Collections.singletonList("some id"), transformedFilter.getChildFilters()[1].getValue());
    Assertions.assertEquals(Filter.Op.IN, transformedFilter.getChildFilters()[1].getOp());
    query = Query.newBuilder().addEntityId("some id").setEntityName("some name").build();
    transformedQuery = DocStoreConverter.transform(TENANT_ID, query, Collections.emptyList());
    transformedFilter = transformedQuery.getFilter();
    Assertions.assertEquals(Filter.Op.AND, transformedFilter.getOp());
    Assertions.assertEquals(3, transformedFilter.getChildFilters().length);
    Assertions.assertEquals(EntityServiceConstants.ENTITY_ID, transformedFilter.getChildFilters()[1].getFieldName());
    Assertions.assertEquals(Collections.singletonList("some id"), transformedFilter.getChildFilters()[1].getValue());
    Assertions.assertEquals(Filter.Op.IN, transformedFilter.getChildFilters()[1].getOp());
    Assertions.assertEquals(EntityServiceConstants.ENTITY_NAME, transformedFilter.getChildFilters()[2].getFieldName());
    Assertions.assertEquals("some name", transformedFilter.getChildFilters()[2].getValue());
    Assertions.assertEquals(Filter.Op.EQ, transformedFilter.getChildFilters()[2].getOp());
}
Also used : Query(org.hypertrace.entity.data.service.v1.Query) AttributeFilter(org.hypertrace.entity.data.service.v1.AttributeFilter) Filter(org.hypertrace.core.documentstore.Filter) Test(org.junit.jupiter.api.Test)

Aggregations

Filter (org.hypertrace.core.documentstore.Filter)38 AttributeFilter (org.hypertrace.entity.data.service.v1.AttributeFilter)20 Test (org.junit.jupiter.api.Test)19 Query (org.hypertrace.entity.data.service.v1.Query)18 ArrayList (java.util.ArrayList)12 Map (java.util.Map)9 Query (org.hypertrace.core.documentstore.Query)9 JSONDocument (org.hypertrace.core.documentstore.JSONDocument)7 Document (org.hypertrace.core.documentstore.Document)6 Collection (org.hypertrace.core.documentstore.Collection)5 MongoCollection (com.mongodb.client.MongoCollection)4 IOException (java.io.IOException)4 List (java.util.List)4 SingleValueKey (org.hypertrace.core.documentstore.SingleValueKey)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)3 BasicDBObject (com.mongodb.BasicDBObject)3 Collections (java.util.Collections)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3