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