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