@Test public void testFindByFieldsDocuments() throws Exception { List<Document> documents = new ArrayList<>(); for (int i = 0; i < 10; i++) { XContentBuilder builder = jsonBuilder() .startObject() .field(VitamDocument.ID, GUIDFactory.newGUID().toString()) .field(VitamDocument.TENANT_ID, 0) .field(TITLE, TEST_SAVE + i + " " + RandomUtils.nextDouble()) .field("TestField", String.valueOf(i)) .field("OtherTestField", String.valueOf("Toto")) .endObject(); documents.add(Document.parse(Strings.toString(builder))); } repository.save(documents); Map<String, String> filter = new HashMap<>(); filter.put("OtherTestField", "Toto"); filter.put("TestField", "5"); FindIterable<Document> iterable = repository.findByFieldsDocuments(filter, 5, 0); MongoCursor<Document> cursor; cursor = iterable.iterator(); List<Document> docs = getDocuments(cursor, 5); assertThat(docs.get(0).get("TestField")).isEqualTo("5"); assertThat(docs.get(0).get("OtherTestField")).isEqualTo("Toto"); assertThat(docs.size()).isEqualTo(1); while (!docs.isEmpty()) { docs = getDocuments(cursor, 1); } assertThat(docs.size()).isEqualTo(0); }
@Override public FindIterable<Document> findByFieldsDocuments(Map<String, String> fields, int mongoBatchSize, Integer tenant) { ParametersChecker.checkParameter(ALL_PARAMS_REQUIRED, tenant); if (fields == null || fields.isEmpty()) { return findDocuments(mongoBatchSize, tenant); } BasicDBObject filter = new BasicDBObject(); fields.forEach((key, value) -> filter.append(key, value)); filter.put(VitamDocument.TENANT_ID, tenant); return collection.find(filter).batchSize(mongoBatchSize); }
VitamMongoRepository implements VitamRepository { @Override public FindIterable<Document> findByFieldsDocuments(Map<String, String> fields, int mongoBatchSize, Integer tenant) { ParametersChecker.checkParameter(ALL_PARAMS_REQUIRED, tenant); if (fields == null || fields.isEmpty()) { return findDocuments(mongoBatchSize, tenant); } BasicDBObject filter = new BasicDBObject(); fields.forEach((key, value) -> filter.append(key, value)); filter.put(VitamDocument.TENANT_ID, tenant); return collection.find(filter).batchSize(mongoBatchSize); } }
VitamMongoRepository implements VitamRepository { @Override public FindIterable<Document> findByFieldsDocuments(Map<String, String> fields, int mongoBatchSize, Integer tenant) { ParametersChecker.checkParameter(ALL_PARAMS_REQUIRED, tenant); if (fields == null || fields.isEmpty()) { return findDocuments(mongoBatchSize, tenant); } BasicDBObject filter = new BasicDBObject(); fields.forEach((key, value) -> filter.append(key, value)); filter.put(VitamDocument.TENANT_ID, tenant); return collection.find(filter).batchSize(mongoBatchSize); } VitamMongoRepository(MongoCollection<Document> collection); }
VitamMongoRepository implements VitamRepository { @Override public FindIterable<Document> findByFieldsDocuments(Map<String, String> fields, int mongoBatchSize, Integer tenant) { ParametersChecker.checkParameter(ALL_PARAMS_REQUIRED, tenant); if (fields == null || fields.isEmpty()) { return findDocuments(mongoBatchSize, tenant); } BasicDBObject filter = new BasicDBObject(); fields.forEach((key, value) -> filter.append(key, value)); filter.put(VitamDocument.TENANT_ID, tenant); return collection.find(filter).batchSize(mongoBatchSize); } VitamMongoRepository(MongoCollection<Document> collection); @Override void save(Document document); @Override VitamRepositoryStatus saveOrUpdate(Document document); @Override void save(List<Document> documents); @Override void saveOrUpdate(List<Document> documents); @Override void update(List<WriteModel<Document>> queries); @Override void remove(String id, Integer tenant); @Override long remove(Bson query); @Override void removeByNameAndTenant(String name, Integer tenant); @Override long purge(Integer tenant); @Override long purge(); @Override Optional<Document> getByID(String id, Integer tenant); @Override Optional<Document> findByIdentifierAndTenant(String identifier, Integer tenant); @Override Optional<Document> findByIdentifier(String identifier); @Override FindIterable<Document> findByFieldsDocuments(Map<String, String> fields, int mongoBatchSize,
        Integer tenant); @Override FindIterable<Document> findDocuments(Collection<String> ids, Bson projection); @Override FindIterable<Document> findDocuments(int mongoBatchSize, Integer tenant); @Override FindIterable<Document> findDocuments(int mongoBatchSize); @Override FindIterable<Document> findDocuments(Bson query, int mongoBatchSize); @Override void delete(List<String> ids, int tenant); }
VitamMongoRepository implements VitamRepository { @Override public FindIterable<Document> findByFieldsDocuments(Map<String, String> fields, int mongoBatchSize, Integer tenant) { ParametersChecker.checkParameter(ALL_PARAMS_REQUIRED, tenant); if (fields == null || fields.isEmpty()) { return findDocuments(mongoBatchSize, tenant); } BasicDBObject filter = new BasicDBObject(); fields.forEach((key, value) -> filter.append(key, value)); filter.put(VitamDocument.TENANT_ID, tenant); return collection.find(filter).batchSize(mongoBatchSize); } VitamMongoRepository(MongoCollection<Document> collection); @Override void save(Document document); @Override VitamRepositoryStatus saveOrUpdate(Document document); @Override void save(List<Document> documents); @Override void saveOrUpdate(List<Document> documents); @Override void update(List<WriteModel<Document>> queries); @Override void remove(String id, Integer tenant); @Override long remove(Bson query); @Override void removeByNameAndTenant(String name, Integer tenant); @Override long purge(Integer tenant); @Override long purge(); @Override Optional<Document> getByID(String id, Integer tenant); @Override Optional<Document> findByIdentifierAndTenant(String identifier, Integer tenant); @Override Optional<Document> findByIdentifier(String identifier); @Override FindIterable<Document> findByFieldsDocuments(Map<String, String> fields, int mongoBatchSize,
        Integer tenant); @Override FindIterable<Document> findDocuments(Collection<String> ids, Bson projection); @Override FindIterable<Document> findDocuments(int mongoBatchSize, Integer tenant); @Override FindIterable<Document> findDocuments(int mongoBatchSize); @Override FindIterable<Document> findDocuments(Bson query, int mongoBatchSize); @Override void delete(List<String> ids, int tenant); static final String NAME; }