MongoQueryBuilder implements QueryBuilder<R> { @Override public QueryResult<R> result() { MongoFindQuery query = find(); List<R> results = new ArrayList<>(); Iterable<Document> iterable = query.getResults(); if (iterable != null) { for (Document document : iterable) { R result = this.connector.getTransformer().fromGenericDB(document, null, clazz); results.add(result); this.connector.getEntityManager().getCache().put(document.getObjectId("_id"), result); MongoEntity entity = this.connector.getTransformer().getOrMapEntity(result.getClass()); entity.set(LifecycleAction.ACCESSED_AT, result, LocalDateTime.now()); entity.call(LifecycleAction.AFTER_ACCESS, result); } } return new MongoQueryResult<>(results); } MongoQueryBuilder(Class<R> clazz, MongoCollection<Document> collection, MorphixMongoConnector connector); @Override MorphixMongoConnector getConnector(); @Override Class<R> getQueryType(); @Override MongoFieldQueryBuilder<R> where(String field); @Override MongoQuerySortBuilder<R> sort(); @Override void delete(); @Override void delete(boolean justOne); @Override QueryResult<R> result(); @Override MongoFindQuery find();  }
@Test void results() { String name = this.connector.getHelperManager().getCollectionHelper().getCollection(Item.class); MongoCollection<Document> collection = this.connector.getDatabase().getCollection(name); for (int k = 0; k < 5; k++) { int sum = 0; int runs = 100; long lowest = -1; long total = 0; long start = System.currentTimeMillis(); for (int j = 0; j < runs; j++) { int records = 100; sum += records; collection.deleteMany(new BsonDocument()); assertEquals(0, collection.count()); Item[] items = new Item[records]; for (int i = 0; i < items.length; i++) { Item item = new Item(this.connector, i + 1, String.format("%05d", i + 1)); items[i] = item; assertNull(item.objectId); } this.connector.getEntityManager().save(items); for (Item item : this.connector.createQuery(Item.class).result()) { assertEquals(items[item.id - 1], item); assertNotNull(item.objectId); } Document bson = new Document("$set", new Document("name", "updated")); new MongoUpdateQuery(collection, new Document(), bson, true, null).performQuery(); this.connector.getEntityManager().update(new FieldFilter(false, "name"), items); for (Item item : items) { assertEquals("updated", item.name); } long end = System.currentTimeMillis(); long time = end - start; total += time; if (lowest == -1 || lowest > time) { lowest = time; } StringBuilder builder = new StringBuilder(); builder.append(records).append(" inserted (").append(String.format("%03d", j + 1)).append("): "); builder.append(time).append("ms"); start = System.currentTimeMillis(); } double average = (double) total; average = average / runs; System.out.println("Records inserted: " + sum); System.out.println("Total time: " + total + "ms"); System.out.println("Lowest time: " + lowest + "ms"); System.out.println("Average time: " + String.format("%04f", average)); } }
@Test void references() { String name = this.connector.getHelperManager().getCollectionHelper().getCollection(Topic.class); MongoCollection<Document> collection = this.connector.getDatabase().getCollection(name); collection.deleteMany(new BsonDocument()); assertEquals(0, collection.count()); name = this.connector.getHelperManager().getCollectionHelper().getCollection(Index.class); collection = this.connector.getDatabase().getCollection(name); collection.deleteMany(new BsonDocument()); assertEquals(0, collection.count()); Topic topic = new Topic("test"); Index index = new Index(topic); topic.save(); index.save(); connector.getEntityManager().getCache().getIds().clear(); connector.getEntityManager().getCache().getCache().clear(); index = connector.createQuery(Index.class).result().first(); assertEquals("test", index.topic.name); }