Search in sources :

Example 11 with Collection

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

the class MongoDocStoreTest method testSelections.

@Test
public void testSelections() throws IOException {
    datastore.createCollection(COLLECTION_NAME, null);
    Collection collection = datastore.getCollection(COLLECTION_NAME);
    collection.upsert(new SingleValueKey("default", "testKey1"), Utils.createDocument("testKey1", "abc1"));
    collection.upsert(new SingleValueKey("default", "testKey2"), Utils.createDocument("testKey2", "abc2"));
    assertEquals(2, collection.count());
    Query query = new Query();
    query.addSelection("testKey1");
    Iterator<Document> iterator = collection.search(query);
    List<Document> documents = new ArrayList<>();
    while (iterator.hasNext()) {
        documents.add(iterator.next());
    }
    assertEquals(2, documents.size());
    Assertions.assertFalse(documents.isEmpty());
    String document1 = documents.get(0).toJson();
    Assertions.assertTrue(document1.contains("testKey1"));
    JsonNode node1 = OBJECT_MAPPER.readTree(document1);
    String value = node1.findValue("testKey1").asText();
    Assertions.assertEquals("abc1", value);
    String document2 = documents.get(1).toJson();
    Assertions.assertFalse(document2.contains("testKey1"));
    JsonNode node2 = OBJECT_MAPPER.readTree(document2);
    assertTrue(node2.isEmpty());
}
Also used : SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Query(org.hypertrace.core.documentstore.Query) ArrayList(java.util.ArrayList) MongoCollection(com.mongodb.client.MongoCollection) Collection(org.hypertrace.core.documentstore.Collection) JsonNode(com.fasterxml.jackson.databind.JsonNode) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Test(org.junit.jupiter.api.Test)

Example 12 with Collection

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

the class MongoDocStoreTest method testContains.

@Test
public void testContains() throws IOException {
    datastore.createCollection(COLLECTION_NAME, null);
    Collection collection = datastore.getCollection(COLLECTION_NAME);
    // https://docs.mongodb.com/manual/reference/operator/query/elemMatch/
    collection.upsert(new SingleValueKey("default", "testKey1"), Utils.createDocument(ImmutablePair.of("id", "testKey1"), ImmutablePair.of("products", List.of(Map.of("product", "abc", "score", 10), Map.of("product", "xyz", "score", 5)))));
    collection.upsert(new SingleValueKey("default", "testKey2"), Utils.createDocument(ImmutablePair.of("id", "testKey2"), ImmutablePair.of("products", List.of(Map.of("product", "abc", "score", 8), Map.of("product", "xyz", "score", 7)))));
    collection.upsert(new SingleValueKey("default", "testKey3"), Utils.createDocument(ImmutablePair.of("id", "testKey3"), ImmutablePair.of("products", List.of(Map.of("product", "abc", "score", 7), Map.of("product", "xyz", "score", 8)))));
    collection.upsert(new SingleValueKey("default", "testKey4"), Utils.createDocument(ImmutablePair.of("id", "testKey4"), ImmutablePair.of("products", List.of(Map.of("product", "abc", "score", 7), Map.of("product", "def", "score", 8)))));
    // try with contains filter
    Query query = new Query();
    Filter filter = new Filter(Op.CONTAINS, "products", Map.of("product", "xyz"));
    query.setFilter(filter);
    Iterator<Document> results = collection.search(query);
    List<Document> documents = new ArrayList<>();
    while (results.hasNext()) {
        documents.add(results.next());
    }
    Assertions.assertEquals(3, documents.size());
    documents.forEach(document -> {
        String jsonStr = document.toJson();
        assertTrue(jsonStr.contains("\"id\":\"testKey1\"") || document.toJson().contains("\"id\":\"testKey2\"") || document.toJson().contains("\"id\":\"testKey3\""));
    });
}
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 13 with Collection

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

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

the class MongoDocStoreTest method testSelectAll.

@Test
public void testSelectAll() throws IOException {
    datastore.createCollection(COLLECTION_NAME, null);
    Collection collection = datastore.getCollection(COLLECTION_NAME);
    collection.upsert(new SingleValueKey("default", "testKey1"), Utils.createDocument("testKey1", "abc1"));
    collection.upsert(new SingleValueKey("default", "testKey2"), Utils.createDocument("testKey2", "abc2"));
    assertEquals(2, collection.count());
    Iterator<Document> iterator = collection.search(new Query());
    List<Document> documents = new ArrayList<>();
    while (iterator.hasNext()) {
        documents.add(iterator.next());
    }
    assertEquals(2, documents.size());
    // Delete one of the documents and test again.
    collection.delete(new SingleValueKey("default", "testKey1"));
    assertEquals(1, collection.count());
}
Also used : SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Query(org.hypertrace.core.documentstore.Query) 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 15 with Collection

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

the class MongoDocStoreTest method testUpsertAndReturn.

@Test
public void testUpsertAndReturn() throws IOException {
    Collection collection = datastore.getCollection(COLLECTION_NAME);
    ObjectNode objectNode = OBJECT_MAPPER.createObjectNode();
    objectNode.put("foo1", "bar1");
    Document document = new JSONDocument(objectNode);
    Document persistedDocument = collection.upsertAndReturn(new SingleValueKey("default", "testKey"), document);
    Query query = new Query();
    query.setFilter(Filter.eq("_id", "default:testKey"));
    // Assert upsert and search results match
    assertEquals(collection.search(query).next(), persistedDocument);
    JsonNode node = OBJECT_MAPPER.readTree(persistedDocument.toJson());
    String lastUpdatedTime = node.findValue(LAST_UPDATE_TIME_KEY).findValue("$date").asText();
    long createdTime = node.findValue(LAST_CREATED_TIME_KEY).asLong();
    objectNode = OBJECT_MAPPER.createObjectNode();
    objectNode.put("foo2", "bar2");
    document = new JSONDocument(objectNode);
    // Upsert again and verify that createdTime does not change, while lastUpdatedTime
    // has changed and values have merged
    Document updatedDocument = collection.upsertAndReturn(new SingleValueKey("default", "testKey"), document);
    node = OBJECT_MAPPER.readTree(updatedDocument.toJson());
    String newLastUpdatedTime = node.findValue(LAST_UPDATE_TIME_KEY).findValue("$date").asText();
    long newCreatedTime = node.findValue(LAST_CREATED_TIME_KEY).asLong();
    assertEquals(createdTime, newCreatedTime);
    assertNotEquals(lastUpdatedTime, newLastUpdatedTime);
    assertEquals("bar1", node.get("foo1").asText());
    assertEquals("bar2", node.get("foo2").asText());
}
Also used : SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Query(org.hypertrace.core.documentstore.Query) MongoCollection(com.mongodb.client.MongoCollection) Collection(org.hypertrace.core.documentstore.Collection) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) JsonNode(com.fasterxml.jackson.databind.JsonNode) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Test(org.junit.jupiter.api.Test)

Aggregations

Collection (org.hypertrace.core.documentstore.Collection)20 Test (org.junit.jupiter.api.Test)19 Document (org.hypertrace.core.documentstore.Document)18 JSONDocument (org.hypertrace.core.documentstore.JSONDocument)17 SingleValueKey (org.hypertrace.core.documentstore.SingleValueKey)15 ArrayList (java.util.ArrayList)13 MongoCollection (com.mongodb.client.MongoCollection)12 Query (org.hypertrace.core.documentstore.Query)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 HashMap (java.util.HashMap)6 Key (org.hypertrace.core.documentstore.Key)6 Map (java.util.Map)5 BulkUpdateResult (org.hypertrace.core.documentstore.BulkUpdateResult)5 Filter (org.hypertrace.core.documentstore.Filter)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 ResultSetChunk (org.hypertrace.entity.query.service.v1.ResultSetChunk)4 BulkArrayValueUpdateRequest (org.hypertrace.core.documentstore.BulkArrayValueUpdateRequest)3 EntityQueryRequest (org.hypertrace.entity.query.service.v1.EntityQueryRequest)3 BulkUpdateRequest (org.hypertrace.core.documentstore.BulkUpdateRequest)2 Entity (org.hypertrace.entity.data.service.v1.Entity)2