Search in sources :

Example 21 with DataDocument

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);
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 22 with DataDocument

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);
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) Test(org.junit.Test)

Example 23 with DataDocument

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;
}
Also used : Document(org.bson.Document) DataDocument(io.lumeer.engine.api.data.DataDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument) BsonDocument(org.bson.BsonDocument)

Example 24 with DataDocument

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;
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) LinkedList(java.util.LinkedList)

Example 25 with DataDocument

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));
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) BasicDBObject(com.mongodb.BasicDBObject) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions)

Aggregations

DataDocument (io.lumeer.engine.api.data.DataDocument)75 Test (org.junit.Test)42 Collection (io.lumeer.api.model.Collection)16 Document (io.lumeer.api.model.Document)16 JsonDocument (io.lumeer.api.dto.JsonDocument)14 Document (org.bson.Document)13 SoftAssertions (org.assertj.core.api.SoftAssertions)10 ReturnDocument (com.mongodb.client.model.ReturnDocument)9 JsonCollection (io.lumeer.api.dto.JsonCollection)9 DataFilter (io.lumeer.engine.api.data.DataFilter)9 ImportedCollection (io.lumeer.api.model.ImportedCollection)6 SearchQuery (io.lumeer.storage.api.query.SearchQuery)6 LocalDateTime (java.time.LocalDateTime)6 BsonDocument (org.bson.BsonDocument)6 LinkedList (java.util.LinkedList)5 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Response (javax.ws.rs.core.Response)4 DataStorageStats (io.lumeer.engine.api.data.DataStorageStats)3