Search in sources :

Example 1 with Document

use of org.hypertrace.core.documentstore.Document 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 Document

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

the class MongoDocStoreTest method test_bulkOperationOnArrayValue_addOperation.

@Test
public void test_bulkOperationOnArrayValue_addOperation() throws Exception {
    datastore.createCollection(COLLECTION_NAME, null);
    Collection collection = datastore.getCollection(COLLECTION_NAME);
    Key key1 = new SingleValueKey("default", "testKey1");
    Document key1InsertedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey1"), ImmutablePair.of("attributes", Map.of("name", "testKey1", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label1"))))))));
    Document key1ExpectedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey1"), ImmutablePair.of("attributes", Map.of("name", "testKey1", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label1")), ImmutablePair.of("value", Map.of("string", "Label2")), ImmutablePair.of("value", Map.of("string", "Label3"))))))));
    collection.upsert(key1, key1InsertedDocument);
    Key key2 = new SingleValueKey("default", "testKey2");
    Document key2InsertedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey2"), ImmutablePair.of("attributes", Map.of("name", "testKey2", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label2"))))))));
    Document key2ExpectedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey2"), ImmutablePair.of("attributes", Map.of("name", "testKey2", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label2")), ImmutablePair.of("value", Map.of("string", "Label3"))))))));
    collection.upsert(key2, key2InsertedDocument);
    Key key3 = new SingleValueKey("default", "testKey3");
    Document key3InsertedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey3"), ImmutablePair.of("attributes", Map.of("name", "testKey3")));
    Document key3ExpectedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey3"), ImmutablePair.of("attributes", Map.of("name", "testKey3", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label2")), ImmutablePair.of("value", Map.of("string", "Label3"))))))));
    collection.upsert(key3, key3InsertedDocument);
    Key key4 = new SingleValueKey("default", "testKey4");
    Document key4InsertedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey4"), ImmutablePair.of("attributes", Map.of("name", "testKey4", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label1")), ImmutablePair.of("value", Map.of("string", "Label2")), ImmutablePair.of("value", Map.of("string", "Label3"))))))));
    Document key4ExpectedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey4"), ImmutablePair.of("attributes", Map.of("name", "testKey4", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label1")), ImmutablePair.of("value", Map.of("string", "Label2")), ImmutablePair.of("value", Map.of("string", "Label3"))))))));
    collection.upsert(key4, key4InsertedDocument);
    Document label2Document = Utils.createDocument(ImmutablePair.of("value", Map.of("string", "Label2")));
    Document label3Document = Utils.createDocument(ImmutablePair.of("value", Map.of("string", "Label3")));
    List<Document> subDocuments = List.of(label2Document, label3Document);
    BulkArrayValueUpdateRequest bulkArrayValueUpdateRequest = new BulkArrayValueUpdateRequest(Set.of(key1, key2, key3, key4), "attributes.labels.valueList.values", ADD, subDocuments);
    BulkUpdateResult bulkUpdateResult = collection.bulkOperationOnArrayValue(bulkArrayValueUpdateRequest);
    assertEquals(4, bulkUpdateResult.getUpdatedCount());
    // get all documents
    Query query = new Query();
    Iterator<Document> results = collection.search(query);
    List<Document> documents = new ArrayList<>();
    while (results.hasNext()) {
        documents.add(results.next());
    }
    assertEquals(4, documents.size());
    Map<String, JsonNode> actualDocs = convertToMap(documents, "id");
    Map<String, JsonNode> expectedDocs = convertToMap(List.of(key1ExpectedDocument, key2ExpectedDocument, key3ExpectedDocument, key4ExpectedDocument), "id");
    // Verify that the documents returned are as expected
    for (Map.Entry<String, JsonNode> entry : actualDocs.entrySet()) {
        String key = entry.getKey();
        JsonNode attributesJsonNode = entry.getValue().get("attributes");
        JsonNode expectedAttributesJsonNode = expectedDocs.get(key).get("attributes");
        assertEquals(expectedAttributesJsonNode, attributesJsonNode);
    }
}
Also used : Query(org.hypertrace.core.documentstore.Query) BulkUpdateResult(org.hypertrace.core.documentstore.BulkUpdateResult) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) BulkArrayValueUpdateRequest(org.hypertrace.core.documentstore.BulkArrayValueUpdateRequest) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) MongoCollection(com.mongodb.client.MongoCollection) Collection(org.hypertrace.core.documentstore.Collection) Map(java.util.Map) HashMap(java.util.HashMap) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Key(org.hypertrace.core.documentstore.Key) Test(org.junit.jupiter.api.Test)

Example 3 with Document

use of org.hypertrace.core.documentstore.Document 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 4 with Document

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

the class MongoDocStoreTest method test_bulkOperationOnArrayValue_removeOperation.

@Test
public void test_bulkOperationOnArrayValue_removeOperation() throws Exception {
    datastore.createCollection(COLLECTION_NAME, null);
    Collection collection = datastore.getCollection(COLLECTION_NAME);
    Key key1 = new SingleValueKey("default", "testKey1");
    Document key1InsertedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey1"), ImmutablePair.of("attributes", Map.of("name", "testKey1", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label1"))))))));
    Document key1ExpectedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey1"), ImmutablePair.of("attributes", Map.of("name", "testKey1", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label1"))))))));
    collection.upsert(key1, key1InsertedDocument);
    Key key2 = new SingleValueKey("default", "testKey2");
    Document key2InsertedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey2"), ImmutablePair.of("attributes", Map.of("name", "testKey2", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label1")), ImmutablePair.of("value", Map.of("string", "Label2"))))))));
    Document key2ExpectedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey2"), ImmutablePair.of("attributes", Map.of("name", "testKey2", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label1"))))))));
    collection.upsert(key2, key2InsertedDocument);
    Key key3 = new SingleValueKey("default", "testKey3");
    Document key3InsertedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey3"), ImmutablePair.of("attributes", Map.of("name", "testKey3")));
    Document key3ExpectedDocument = key3InsertedDocument;
    collection.upsert(key3, key3InsertedDocument);
    Key key4 = new SingleValueKey("default", "testKey4");
    Document key4InsertedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey4"), ImmutablePair.of("attributes", Map.of("name", "testKey4", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label1")), ImmutablePair.of("value", Map.of("string", "Label2")), ImmutablePair.of("value", Map.of("string", "Label3"))))))));
    Document key4ExpectedDocument = Utils.createDocument(ImmutablePair.of("id", "testKey4"), ImmutablePair.of("attributes", Map.of("name", "testKey4", "labels", ImmutablePair.of("valueList", ImmutablePair.of("values", List.of(ImmutablePair.of("value", Map.of("string", "Label1"))))))));
    collection.upsert(key4, key4InsertedDocument);
    Document label2Document = Utils.createDocument(ImmutablePair.of("value", Map.of("string", "Label2")));
    Document label3Document = Utils.createDocument(ImmutablePair.of("value", Map.of("string", "Label3")));
    List<Document> subDocuments = List.of(label2Document, label3Document);
    BulkArrayValueUpdateRequest bulkArrayValueUpdateRequest = new BulkArrayValueUpdateRequest(Set.of(key1, key2, key3, key4), "attributes.labels.valueList.values", REMOVE, subDocuments);
    BulkUpdateResult bulkUpdateResult = collection.bulkOperationOnArrayValue(bulkArrayValueUpdateRequest);
    assertEquals(4, bulkUpdateResult.getUpdatedCount());
    // get all documents
    Query query = new Query();
    Iterator<Document> results = collection.search(query);
    List<Document> documents = new ArrayList<>();
    while (results.hasNext()) {
        documents.add(results.next());
    }
    assertEquals(4, documents.size());
    Map<String, JsonNode> actualDocs = convertToMap(documents, "id");
    Map<String, JsonNode> expectedDocs = convertToMap(List.of(key1ExpectedDocument, key2ExpectedDocument, key3ExpectedDocument, key4ExpectedDocument), "id");
    // Verify that the documents returned are as expected
    for (Map.Entry<String, JsonNode> entry : actualDocs.entrySet()) {
        String key = entry.getKey();
        JsonNode attributesJsonNode = entry.getValue().get("attributes");
        JsonNode expectedAttributesJsonNode = expectedDocs.get(key).get("attributes");
        assertEquals(expectedAttributesJsonNode, attributesJsonNode);
    }
}
Also used : Query(org.hypertrace.core.documentstore.Query) BulkUpdateResult(org.hypertrace.core.documentstore.BulkUpdateResult) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) BulkArrayValueUpdateRequest(org.hypertrace.core.documentstore.BulkArrayValueUpdateRequest) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) MongoCollection(com.mongodb.client.MongoCollection) Collection(org.hypertrace.core.documentstore.Collection) Map(java.util.Map) HashMap(java.util.HashMap) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Key(org.hypertrace.core.documentstore.Key) Test(org.junit.jupiter.api.Test)

Example 5 with Document

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

the class MongoQueryExecutorIntegrationTest method testFindWithDuplicateSelections.

@Test
public void testFindWithDuplicateSelections() throws IOException {
    List<SelectionSpec> selectionSpecs = List.of(SelectionSpec.of(IdentifierExpression.of("item")), SelectionSpec.of(IdentifierExpression.of("item")), SelectionSpec.of(IdentifierExpression.of("price")), SelectionSpec.of(IdentifierExpression.of("quantity")), SelectionSpec.of(IdentifierExpression.of("quantity")), SelectionSpec.of(IdentifierExpression.of("date")));
    Selection selection = Selection.builder().selectionSpecs(selectionSpecs).build();
    Filter filter = Filter.builder().expression(RelationalExpression.of(IdentifierExpression.of("item"), NOT_IN, ConstantExpression.ofStrings(List.of("Soap", "Bottle")))).build();
    Query query = Query.builder().setSelection(selection).setFilter(filter).build();
    Iterator<Document> resultDocs = collection.find(query);
    assertDocsEqual(resultDocs, "mongo/simple_filter_response.json");
}
Also used : SelectionSpec(org.hypertrace.core.documentstore.query.SelectionSpec) Query(org.hypertrace.core.documentstore.query.Query) Filter(org.hypertrace.core.documentstore.query.Filter) Selection(org.hypertrace.core.documentstore.query.Selection) Document(org.hypertrace.core.documentstore.Document) Test(org.junit.jupiter.api.Test)

Aggregations

Document (org.hypertrace.core.documentstore.Document)66 JSONDocument (org.hypertrace.core.documentstore.JSONDocument)41 Test (org.junit.jupiter.api.Test)37 SingleValueKey (org.hypertrace.core.documentstore.SingleValueKey)22 Collection (org.hypertrace.core.documentstore.Collection)21 ArrayList (java.util.ArrayList)20 Key (org.hypertrace.core.documentstore.Key)18 Query (org.hypertrace.core.documentstore.query.Query)18 ServiceException (com.google.protobuf.ServiceException)14 HashMap (java.util.HashMap)14 Query (org.hypertrace.core.documentstore.Query)14 MongoCollection (com.mongodb.client.MongoCollection)12 Map (java.util.Map)12 IOException (java.io.IOException)10 BulkUpdateResult (org.hypertrace.core.documentstore.BulkUpdateResult)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 Filter (org.hypertrace.core.documentstore.Filter)8 Entity (org.hypertrace.entity.data.service.v1.Entity)7 BulkArrayValueUpdateRequest (org.hypertrace.core.documentstore.BulkArrayValueUpdateRequest)6 Filter (org.hypertrace.core.documentstore.query.Filter)6