Search in sources :

Example 6 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 7 with QueryBuilder

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

the class MongoBlobStore method getAllChunkIds.

@Override
public Iterator<String> getAllChunkIds(long maxLastModifiedTime) throws Exception {
    DBCollection collection = getBlobCollection();
    DBObject fields = new BasicDBObject();
    fields.put(MongoBlob.KEY_ID, 1);
    QueryBuilder builder = new QueryBuilder();
    if (maxLastModifiedTime != 0 && maxLastModifiedTime != -1) {
        builder.and(MongoBlob.KEY_LAST_MOD).lessThanEquals(maxLastModifiedTime);
    }
    final DBCursor cur = collection.find(builder.get(), fields).hint(fields).addOption(Bytes.QUERYOPTION_SLAVEOK);
    //TODO The cursor needs to be closed
    return new AbstractIterator<String>() {

        @Override
        protected String computeNext() {
            if (cur.hasNext()) {
                MongoBlob blob = (MongoBlob) cur.next();
                if (blob != null) {
                    return blob.getId();
                }
            }
            return endOfData();
        }
    };
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) QueryBuilder(com.mongodb.QueryBuilder) AbstractIterator(com.google.common.collect.AbstractIterator) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 8 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, String indexedProperty, long startValue, long endValue) throws DocumentStoreException {
    log("remove", collection, indexedProperty, startValue, endValue);
    int num = 0;
    DBCollection dbCollection = getDBCollection(collection);
    Stopwatch watch = startWatch();
    try {
        QueryBuilder queryBuilder = QueryBuilder.start(indexedProperty);
        queryBuilder.greaterThan(startValue);
        queryBuilder.lessThan(endValue);
        try {
            num = dbCollection.remove(queryBuilder.get()).getN();
        } catch (Exception e) {
            throw DocumentStoreException.convert(e, "Remove failed for " + collection + ": " + indexedProperty + " in (" + startValue + ", " + endValue + ")");
        } finally {
            if (collection == Collection.NODES) {
                // this method is currently being used only for Journal collection while GC.
                // But, to keep sanctity of the API, we need to acknowledge that Nodes collection
                // could've been used. But, in this signature, there's no useful way to invalidate
                // cache.
                // So, we use the hammer for this task
                invalidateCache();
            }
        }
    } finally {
        stats.doneRemove(watch.elapsed(TimeUnit.NANOSECONDS), collection, num);
    }
    return num;
}
Also used : DBCollection(com.mongodb.DBCollection) Stopwatch(com.google.common.base.Stopwatch) QueryBuilder(com.mongodb.QueryBuilder) 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)

Example 9 with QueryBuilder

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

the class MongoDocumentStore method findAndModify.

@SuppressWarnings("unchecked")
@CheckForNull
private <T extends Document> T findAndModify(Collection<T> collection, UpdateOp updateOp, boolean upsert, boolean checkConditions) {
    DBCollection dbCollection = getDBCollection(collection);
    // make sure we don't modify the original updateOp
    updateOp = updateOp.copy();
    DBObject update = createUpdate(updateOp, false);
    Lock lock = null;
    if (collection == Collection.NODES) {
        lock = nodeLocks.acquire(updateOp.getId());
    }
    final Stopwatch watch = startWatch();
    boolean newEntry = false;
    try {
        // get modCount of cached document
        Long modCount = null;
        T cachedDoc = null;
        if (collection == Collection.NODES) {
            cachedDoc = (T) nodesCache.getIfPresent(updateOp.getId());
            if (cachedDoc != null) {
                modCount = cachedDoc.getModCount();
            }
        }
        // if we have a matching modCount
        if (modCount != null) {
            QueryBuilder query = createQueryForUpdate(updateOp.getId(), updateOp.getConditions());
            query.and(Document.MOD_COUNT).is(modCount);
            WriteResult result = dbCollection.update(query.get(), update);
            if (result.getN() > 0) {
                // success, update cached document
                if (collection == Collection.NODES) {
                    NodeDocument newDoc = (NodeDocument) applyChanges(collection, cachedDoc, updateOp);
                    nodesCache.put(newDoc);
                }
                // return previously cached document
                return cachedDoc;
            }
        }
        // conditional update failed or not possible
        // perform operation and get complete document
        QueryBuilder query = createQueryForUpdate(updateOp.getId(), updateOp.getConditions());
        DBObject oldNode = dbCollection.findAndModify(query.get(), null, null, /*sort*/
        false, /*remove*/
        update, false, /*returnNew*/
        upsert);
        if (oldNode == null) {
            newEntry = true;
        }
        if (checkConditions && oldNode == null) {
            return null;
        }
        T oldDoc = convertFromDBObject(collection, oldNode);
        if (oldDoc != null) {
            if (collection == Collection.NODES) {
                NodeDocument newDoc = (NodeDocument) applyChanges(collection, oldDoc, updateOp);
                nodesCache.put(newDoc);
                updateLocalChanges(newDoc);
            }
            oldDoc.seal();
        } else if (upsert) {
            if (collection == Collection.NODES) {
                NodeDocument doc = (NodeDocument) collection.newDocument(this);
                UpdateUtils.applyChanges(doc, updateOp);
                nodesCache.putIfAbsent(doc);
                updateLocalChanges(doc);
            }
        } else {
        // updateOp without conditions and not an upsert
        // this means the document does not exist
        }
        return oldDoc;
    } catch (Exception e) {
        throw handleException(e, collection, updateOp.getId());
    } finally {
        if (lock != null) {
            lock.unlock();
        }
        stats.doneFindAndModify(watch.elapsed(TimeUnit.NANOSECONDS), collection, updateOp.getId(), newEntry, true, 0);
    }
}
Also used : DBCollection(com.mongodb.DBCollection) WriteResult(com.mongodb.WriteResult) BulkWriteResult(com.mongodb.BulkWriteResult) Stopwatch(com.google.common.base.Stopwatch) QueryBuilder(com.mongodb.QueryBuilder) NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) 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) Lock(java.util.concurrent.locks.Lock) CheckForNull(javax.annotation.CheckForNull)

Example 10 with QueryBuilder

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

the class MongoDbTest method updateDocument.

@Test
@Ignore
public void updateDocument() {
    DB db = MongoUtils.getConnection().getDB();
    MongoUtils.dropCollections(db);
    DBCollection nodes = db.getCollection(Collection.NODES.toString());
    DBObject index = new BasicDBObject();
    // modification time (descending)
    index.put("_mod", -1L);
    // and then id (ascending)
    index.put("_id", 1L);
    DBObject options = new BasicDBObject();
    // options.put("unique", Boolean.TRUE);
    nodes.createIndex(index, options);
    long time;
    time = System.currentTimeMillis();
    int nodeCount = 4500;
    String parent = "/parent/node/abc";
    DBObject[] inserts = new DBObject[nodeCount];
    for (int i = 0; i < nodeCount; i++) {
        BasicDBObject doc = new BasicDBObject();
        inserts[i] = doc;
        doc.put("_id", parent + "/node" + i);
        doc.put("_mod", 0);
        doc.put("_counter", 0);
        doc.put("x", 10);
    }
    nodes.insert(inserts, WriteConcern.SAFE);
    time = System.currentTimeMillis() - time;
    System.out.println("insert: " + time);
    time = System.currentTimeMillis();
    for (int i = 0; i < nodeCount; i++) {
        QueryBuilder queryBuilder = QueryBuilder.start(Document.ID).is(parent + "/node" + i);
        DBObject fields = new BasicDBObject();
        // return _id only
        fields.put("_id", 1);
        DBObject query = queryBuilder.get();
        BasicDBObject setUpdates = new BasicDBObject();
        BasicDBObject incUpdates = new BasicDBObject();
        BasicDBObject unsetUpdates = new BasicDBObject();
        setUpdates.append("_mod", i);
        incUpdates.append("_counter", 1);
        unsetUpdates.append("x", "1");
        BasicDBObject update = new BasicDBObject();
        if (!setUpdates.isEmpty()) {
            update.append("$set", setUpdates);
        }
        if (!incUpdates.isEmpty()) {
            update.append("$inc", incUpdates);
        }
        if (!unsetUpdates.isEmpty()) {
            update.append("$unset", unsetUpdates);
        }
        // 1087 ms (upsert true+false, returnNew = false)
        // 1100 ms (returnNew = true)
        //            DBObject oldNode =
        nodes.findAndModify(query, fields, null, /*sort*/
        false, /*remove*/
        update, false, /*returnNew*/
        true);
    // 250 ms WriteConcern.NORMAL, NONE
    // 891 ms WriteConvern.SAFE
    // > 10 s WriteConcern.JOURNAL_SAFE, FSYNC_SAFE
    //            WriteResult result =
    //            nodes.update(query, update, /* upsert */ true, /* multi */ false,
    //                    WriteConcern.NORMAL);
    }
    time = System.currentTimeMillis() - time;
    System.out.println("update: " + time);
    time = System.currentTimeMillis();
    db.getMongo().close();
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) QueryBuilder(com.mongodb.QueryBuilder) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DB(com.mongodb.DB) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

QueryBuilder (com.mongodb.QueryBuilder)17 BasicDBObject (com.mongodb.BasicDBObject)14 DBObject (com.mongodb.DBObject)14 DBCollection (com.mongodb.DBCollection)10 Stopwatch (com.google.common.base.Stopwatch)5 DBCursor (com.mongodb.DBCursor)5 BulkWriteException (com.mongodb.BulkWriteException)4 MongoException (com.mongodb.MongoException)4 Nonnull (javax.annotation.Nonnull)4 DocumentStoreException (org.apache.jackrabbit.oak.plugins.document.DocumentStoreException)4 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)3 IOException (java.io.IOException)3 ExecutionException (java.util.concurrent.ExecutionException)3 NodeDocument (org.apache.jackrabbit.oak.plugins.document.NodeDocument)3 BulkWriteResult (com.mongodb.BulkWriteResult)2 DB (com.mongodb.DB)2 WriteResult (com.mongodb.WriteResult)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Lock (java.util.concurrent.locks.Lock)2