Search in sources :

Example 1 with Filter

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

the class MongoDocStoreTest method testWithDifferentDataTypes.

/**
 * This is an example where same field is having different type values. e.g size field has
 * boolean, numeric and string values. This is a valid scenario for document store, and works fine
 * with mongodb. However, there is currently limitation on postgres as document store
 * implementation using jsonb, and it won't work.
 */
@Test
public void testWithDifferentDataTypes() throws IOException {
    datastore.createCollection(COLLECTION_NAME, null);
    Collection collection = datastore.getCollection(COLLECTION_NAME);
    // size field with integer value
    collection.upsert(new SingleValueKey("default", "testKey1"), Utils.createDocument(ImmutablePair.of("id", "testKey1"), ImmutablePair.of("name", "abc1"), ImmutablePair.of("size", -10)));
    collection.upsert(new SingleValueKey("default", "testKey2"), Utils.createDocument(ImmutablePair.of("id", "testKey2"), ImmutablePair.of("name", "abc2"), ImmutablePair.of("size", -20)));
    // size field with string value
    collection.upsert(new SingleValueKey("default", "testKey3"), Utils.createDocument(ImmutablePair.of("id", "testKey3"), ImmutablePair.of("name", "abc3"), ImmutablePair.of("size", false)));
    collection.upsert(new SingleValueKey("default", "testKey4"), Utils.createDocument(ImmutablePair.of("id", "testKey4"), ImmutablePair.of("name", "abc4"), ImmutablePair.of("size", true)));
    // size field with boolean value
    collection.upsert(new SingleValueKey("default", "testKey5"), Utils.createDocument(ImmutablePair.of("id", "testKey5"), ImmutablePair.of("name", "abc5"), ImmutablePair.of("size", "10")));
    collection.upsert(new SingleValueKey("default", "testKey6"), Utils.createDocument(ImmutablePair.of("id", "testKey6"), ImmutablePair.of("name", "abc6"), ImmutablePair.of("size", "20")));
    // query for size field with integer value
    Query queryGt = new Query();
    Filter filterGt = new Filter(Op.GT, "size", -30);
    queryGt.setFilter(filterGt);
    Iterator<Document> results = collection.search(queryGt);
    List<Document> documents = new ArrayList<>();
    while (results.hasNext()) {
        documents.add(results.next());
    }
    Assertions.assertEquals(2, documents.size());
    // query for size field with string value
    Query queryGtStr = new Query();
    Filter filterGtStr = new Filter(Op.GT, "size", "1");
    queryGtStr.setFilter(filterGtStr);
    Iterator<Document> resultsGtStr = collection.search(queryGtStr);
    List<Document> documentsGtStr = new ArrayList<>();
    while (resultsGtStr.hasNext()) {
        documentsGtStr.add(resultsGtStr.next());
    }
    Assertions.assertEquals(2, documentsGtStr.size());
    // query for size field with boolean value
    Query queryGtBool = new Query();
    Filter filterGtBool = new Filter(Op.GT, "size", false);
    queryGtStr.setFilter(filterGtBool);
    Iterator<Document> resultsGtBool = collection.search(queryGtStr);
    List<Document> documentsGtBool = new ArrayList<>();
    while (resultsGtBool.hasNext()) {
        documentsGtBool.add(resultsGtBool.next());
    }
    Assertions.assertEquals(1, documentsGtBool.size());
    datastore.deleteCollection(COLLECTION_NAME);
}
Also used : SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Query(org.hypertrace.core.documentstore.Query) Filter(org.hypertrace.core.documentstore.Filter) ArrayList(java.util.ArrayList) MongoCollection(com.mongodb.client.MongoCollection) Collection(org.hypertrace.core.documentstore.Collection) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Test(org.junit.jupiter.api.Test)

Example 2 with Filter

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

the class MongoDocStoreTest method whenBulkUpdatingNonExistentRecords_thenExpectNothingToBeUpdatedOrCreated.

@Test
public void whenBulkUpdatingNonExistentRecords_thenExpectNothingToBeUpdatedOrCreated() throws Exception {
    Collection collection = datastore.getCollection(COLLECTION_NAME);
    ObjectNode objectNode = OBJECT_MAPPER.createObjectNode();
    objectNode.put("foo1", "bar1");
    objectNode.put("timestamp", 100);
    List<BulkUpdateRequest> toUpdate = new ArrayList<>();
    toUpdate.add(new BulkUpdateRequest(new SingleValueKey("tenant-1", "testKey1"), new JSONDocument(objectNode), new Filter(Op.LT, "timestamp", 100)));
    toUpdate.add(new BulkUpdateRequest(new SingleValueKey("tenant-1", "testKey2"), new JSONDocument(objectNode), new Filter(Op.LT, "timestamp", 100)));
    BulkUpdateResult result = collection.bulkUpdate(toUpdate);
    Assertions.assertEquals(0, 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);
    assertFalse(it.hasNext());
}
Also used : SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Query(org.hypertrace.core.documentstore.Query) Filter(org.hypertrace.core.documentstore.Filter) ArrayList(java.util.ArrayList) BulkUpdateResult(org.hypertrace.core.documentstore.BulkUpdateResult) MongoCollection(com.mongodb.client.MongoCollection) Collection(org.hypertrace.core.documentstore.Collection) BulkUpdateRequest(org.hypertrace.core.documentstore.BulkUpdateRequest) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Test(org.junit.jupiter.api.Test)

Example 3 with Filter

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

the class CreateUpdateTestThread method updateRun.

private void updateRun() throws IOException {
    Document document = Utils.createDocument(ImmutablePair.of("id", documentKey.getValue()), ImmutablePair.of("name", String.format("thread-%s", this.testValue)), ImmutablePair.of("size", this.testValue));
    // do conditional update
    Filter condition = new Filter(Op.EQ, "size", this.testValue);
    updateTestResult = collection.update(documentKey, document, condition);
}
Also used : Filter(org.hypertrace.core.documentstore.Filter) Document(org.hypertrace.core.documentstore.Document)

Example 4 with Filter

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

the class MongoQueryParserTest method testParseNestedQuery.

@Test
void testParseNestedQuery() {
    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);
    Map<String, Object> query = MongoQueryParser.parseFilter(filter);
    assertEquals(2, ((List) query.get("$or")).size());
    Assertions.assertTrue(((List) ((Map) ((List) query.get("$or")).get(0)).get("$and")).containsAll(List.of(Map.of("key1", "val1"), Map.of("key2", "val2"))));
    Assertions.assertTrue(((List) ((Map) ((List) query.get("$or")).get(1)).get("$and")).containsAll(List.of(Map.of("key3", "val3"), Map.of("key4", "val4"))));
}
Also used : Filter(org.hypertrace.core.documentstore.Filter) BasicDBObject(com.mongodb.BasicDBObject) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 5 with Filter

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

the class PostgresQueryParserTest method testParseNestedQuery.

@Test
void testParseNestedQuery() {
    Filter filter1 = new Filter(Filter.Op.EQ, ID, "val1").and(new Filter(Filter.Op.EQ, "key2", "val2"));
    Filter filter2 = new Filter(Filter.Op.EQ, ID, "val3").and(new Filter(Filter.Op.EQ, "key4", "val4"));
    Filter filter = filter1.or(filter2);
    String query = PostgresQueryParser.parseFilter(filter, initParams());
    Assertions.assertEquals(String.format("((%s = ?) AND (document->>'key2' = ?)) " + "OR ((%s = ?) AND (document->>'key4' = ?))", ID, ID), query);
}
Also used : 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