Search in sources :

Example 1 with QueryBuilder

use of com.mongodb.QueryBuilder in project jackrabbit-oak by apache.

the class MongoDocumentStore method remove.

@Override
public <T extends Document> int remove(Collection<T> collection, Map<String, Map<Key, Condition>> toRemove) {
    log("remove", toRemove);
    int num = 0;
    DBCollection dbCollection = getDBCollection(collection);
    Stopwatch watch = startWatch();
    try {
        List<String> batchIds = Lists.newArrayList();
        List<DBObject> batch = Lists.newArrayList();
        Iterator<Entry<String, Map<Key, Condition>>> it = toRemove.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, Map<Key, Condition>> entry = it.next();
            QueryBuilder query = createQueryForUpdate(entry.getKey(), entry.getValue());
            batchIds.add(entry.getKey());
            batch.add(query.get());
            if (!it.hasNext() || batch.size() == IN_CLAUSE_BATCH_SIZE) {
                DBObject q = new BasicDBObject();
                q.put(QueryOperators.OR, batch);
                try {
                    num += dbCollection.remove(q).getN();
                } catch (Exception e) {
                    throw DocumentStoreException.convert(e, "Remove failed for " + batch);
                } finally {
                    if (collection == Collection.NODES) {
                        invalidateCache(batchIds);
                    }
                }
                batchIds.clear();
                batch.clear();
            }
        }
    } finally {
        stats.doneRemove(watch.elapsed(TimeUnit.NANOSECONDS), collection, num);
    }
    return num;
}
Also used : Condition(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Condition) Stopwatch(com.google.common.base.Stopwatch) QueryBuilder(com.mongodb.QueryBuilder) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MongoException(com.mongodb.MongoException) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) BulkWriteException(com.mongodb.BulkWriteException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) JournalEntry(org.apache.jackrabbit.oak.plugins.document.JournalEntry) Entry(java.util.Map.Entry) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) Key(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Key)

Example 2 with QueryBuilder

use of com.mongodb.QueryBuilder in project jackrabbit-oak by apache.

the class MongoBlobStore method countDeleteChunks.

@Override
public long countDeleteChunks(List<String> chunkIds, long maxLastModifiedTime) throws Exception {
    DBCollection collection = getBlobCollection();
    QueryBuilder queryBuilder = new QueryBuilder();
    if (chunkIds != null) {
        queryBuilder = queryBuilder.and(MongoBlob.KEY_ID).in(chunkIds.toArray(new String[0]));
        if (maxLastModifiedTime > 0) {
            queryBuilder = queryBuilder.and(MongoBlob.KEY_LAST_MOD).lessThan(maxLastModifiedTime);
        }
    }
    WriteResult result = collection.remove(queryBuilder.get());
    return result.getN();
}
Also used : DBCollection(com.mongodb.DBCollection) WriteResult(com.mongodb.WriteResult) QueryBuilder(com.mongodb.QueryBuilder)

Example 3 with QueryBuilder

use of com.mongodb.QueryBuilder in project spring-data-mongodb by spring-projects.

the class QueryMapperUnitTests method handlesNativelyBuiltQueryCorrectly.

// DATAMONGO-373
@Test
public void handlesNativelyBuiltQueryCorrectly() {
    DBObject query = new QueryBuilder().or(new BasicDBObject("foo", "bar")).get();
    mapper.getMappedObject(new org.bson.Document(query.toMap()), Optional.empty());
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) QueryBuilder(com.mongodb.QueryBuilder) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 4 with QueryBuilder

use of com.mongodb.QueryBuilder in project records-management by Alfresco.

the class RecordService method getRecordCountInSpecifiedPaths.

/**
 * Helper for counting the records with specified execution state, that has the parent path in specified list or from all existent records if listOfParentPaths is null or empty.
 *
 * @param state - record execution state to search for
 * @param listOfParentPaths - list of parent paths to search in
 * @return for counting the records with specified execution state, that has the parent path in specified list or from all existent records if listOfParentPaths is null or empty.
 */
public long getRecordCountInSpecifiedPaths(String state, List<String> listOfParentPaths) {
    if (state == null) {
        throw new IllegalArgumentException();
    }
    QueryBuilder queryObjBuilder = QueryBuilder.start();
    queryObjBuilder.and(FIELD_STATE).is(state);
    if (listOfParentPaths != null && listOfParentPaths.size() > 0) {
        queryObjBuilder.and(FIELD_PARENT_PATH).in(listOfParentPaths);
    }
    DBObject queryObj = queryObjBuilder.get();
    long count = collection.count(queryObj);
    return count;
}
Also used : QueryBuilder(com.mongodb.QueryBuilder) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 5 with QueryBuilder

use of com.mongodb.QueryBuilder in project incubator-rya by apache.

the class GeoTemporalMongoDBStorageStrategy method getFilterQuery.

public DBObject getFilterQuery(final Collection<IndexingExpr> geoFilters, final Collection<IndexingExpr> temporalFilters) throws GeoTemporalIndexException {
    final QueryBuilder builder = QueryBuilder.start();
    if (!geoFilters.isEmpty()) {
        final DBObject[] geo = getGeoObjs(geoFilters);
        if (!temporalFilters.isEmpty()) {
            final DBObject[] temporal = getTemporalObjs(temporalFilters);
            builder.and(oneOrAnd(geo), oneOrAnd(temporal));
            return builder.get();
        } else {
            return oneOrAnd(geo);
        }
    } else if (!temporalFilters.isEmpty()) {
        final DBObject[] temporal = getTemporalObjs(temporalFilters);
        return oneOrAnd(temporal);
    } else {
        return builder.get();
    }
}
Also used : QueryBuilder(com.mongodb.QueryBuilder) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Aggregations

QueryBuilder (com.mongodb.QueryBuilder)17 BasicDBObject (com.mongodb.BasicDBObject)15 DBObject (com.mongodb.DBObject)15 DBCollection (com.mongodb.DBCollection)5 Test (org.junit.Test)3 TranslatorException (org.teiid.translator.TranslatorException)3 Stopwatch (com.google.common.base.Stopwatch)2 BasicDBList (com.mongodb.BasicDBList)2 DB (com.mongodb.DB)2 DBCursor (com.mongodb.DBCursor)2 MongoException (com.mongodb.MongoException)2 Ignore (org.junit.Ignore)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 BulkWriteException (com.mongodb.BulkWriteException)1 WriteResult (com.mongodb.WriteResult)1 MongoQueryBuilder (fr.wseduc.mongodb.MongoQueryBuilder)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1