use of io.lumeer.engine.api.data.DataDocument in project engine by Lumeer.
the class MongoDbStorageTest method testCreateDocumentsWithException.
@Test
public void testCreateDocumentsWithException() throws Exception {
mongoDbStorage.createCollection(COLLECTION_CREATE_DOCUMENTS_EXCEPTION);
mongoDbStorage.createIndex(COLLECTION_CREATE_DOCUMENTS_EXCEPTION, new DataDocument("b", 1), true);
List<DataDocument> documents = new LinkedList<>();
documents.add(new DataDocument("a", "a").append("b", "a"));
documents.add(new DataDocument("a", "a").append("b", "b"));
documents.add(new DataDocument("a", "a").append("b", "c"));
documents.add(new DataDocument("a", "a").append("b", "d"));
documents.add(new DataDocument("a", "a").append("b", "a"));
assertThatThrownBy(() -> mongoDbStorage.createDocuments(COLLECTION_CREATE_DOCUMENTS_EXCEPTION, documents)).isInstanceOf(MongoBulkWriteException.class);
List<DataDocument> search = mongoDbStorage.search(COLLECTION_CREATE_DOCUMENTS_EXCEPTION, null, null, 0, 0);
assertThat(search).hasSize(4);
}
use of io.lumeer.engine.api.data.DataDocument in project engine by Lumeer.
the class MongoDbStorageTest method testDropManyDocuments.
@Test
public void testDropManyDocuments() throws Exception {
mongoDbStorage.createCollection(COLLECTION_DROP_MANY);
String dropManyKey = "dropManyKey";
String value1 = "v1";
String value2 = "v2";
for (int i = 0; i < 100; i++) {
DataDocument insertedDocument = createDummyDocument();
insertedDocument.put(dropManyKey, i % 2 == 0 ? value1 : value2);
mongoDbStorage.createDocument(COLLECTION_DROP_MANY, insertedDocument);
}
List<DataDocument> docs = mongoDbStorage.search(COLLECTION_DROP_MANY, null, null, 0, 0);
assertThat(docs).hasSize(100);
mongoDbStorage.dropManyDocuments(COLLECTION_DROP_MANY, mongoDbStorageDialect.fieldValueFilter(dropManyKey, value1));
docs = mongoDbStorage.search(COLLECTION_DROP_MANY, null, null, 0, 0);
assertThat(docs).hasSize(50);
}
use of io.lumeer.engine.api.data.DataDocument in project engine by Lumeer.
the class MongoDbStorage method createDocument.
@Override
public String createDocument(final String collectionName, final DataDocument dataDocument) {
Document doc = new Document(dataDocument);
if (getCollectionCache() != null) {
collectionCacheRef.get().lock(COLLECTION_CACHE);
try {
final List<String> collections = getCollectionCache();
if (collections != null) {
collections.add(collectionName);
} else {
setCollectionCache(new ArrayList<>(Collections.singletonList(collectionName)));
}
database.getCollection(collectionName).insertOne(doc);
} finally {
collectionCacheRef.get().unlock(COLLECTION_CACHE);
}
} else {
database.getCollection(collectionName).insertOne(doc);
}
return doc.containsKey(DOCUMENT_ID) ? doc.getObjectId(DOCUMENT_ID).toString() : null;
}
use of io.lumeer.engine.api.data.DataDocument in project engine by Lumeer.
the class MongoDbStorage method query.
@Override
public List<DataDocument> query(final Query query) {
List<DataDocument> result = new LinkedList<>();
List<DataDocument> stages = new LinkedList<>();
if (query.getFilters().size() > 0) {
final DataDocument filters = new DataDocument();
filters.put("$match", query.getFilters());
stages.add(filters);
}
if (query.getGrouping().size() > 0) {
final DataDocument grouping = new DataDocument();
grouping.put("$group", query.getGrouping());
stages.add(grouping);
}
if (query.getProjections().size() > 0) {
final DataDocument projections = new DataDocument();
projections.put("$project", query.getProjections());
stages.add(projections);
}
if (query.getSorting().size() > 0) {
final DataDocument sorts = new DataDocument();
sorts.put("$sort", query.getSorting());
stages.add(sorts);
}
if (query.getSkip() != null && query.getSkip() > 0) {
final DataDocument skip = new DataDocument();
skip.put("$skip", query.getSkip());
stages.add(skip);
}
if (query.getLimit() != null && query.getLimit() > 0) {
final DataDocument limit = new DataDocument();
limit.put("$limit", query.getLimit());
stages.add(limit);
}
if (query.getOutput() != null && !query.getOutput().isEmpty()) {
final DataDocument output = new DataDocument();
output.put("$out", query.getOutput());
stages.add(output);
}
query.getCollections().forEach(collection -> {
result.addAll(aggregate(collection, stages.toArray(new DataDocument[stages.size()])));
});
return result;
}
use of io.lumeer.engine.api.data.DataDocument in project engine by Lumeer.
the class MongoDbStorage method updateDocument.
@Override
public void updateDocument(final String collectionName, final DataDocument updatedDocument, final DataFilter filter) {
DataDocument toUpdate = new DataDocument(updatedDocument);
if (toUpdate.containsKey(DOCUMENT_ID)) {
toUpdate.remove(DOCUMENT_ID);
}
BasicDBObject updateBson = new BasicDBObject("$set", new BasicDBObject(toUpdate));
database.getCollection(collectionName).updateOne(filter.<Bson>get(), updateBson, new UpdateOptions().upsert(true));
}
Aggregations