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