Search in sources :

Example 16 with QueryBuilder

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

the class MongoDocumentStore method findDocuments.

private <T extends Document> Map<String, T> findDocuments(Collection<T> collection, Set<String> keys) {
    Map<String, T> docs = new HashMap<String, T>();
    if (!keys.isEmpty()) {
        DBObject[] conditions = new DBObject[keys.size()];
        int i = 0;
        for (String key : keys) {
            conditions[i++] = getByKeyQuery(key).get();
        }
        QueryBuilder builder = new QueryBuilder();
        builder.or(conditions);
        DBCursor cursor = getDBCollection(collection).find(builder.get());
        while (cursor.hasNext()) {
            T foundDoc = convertFromDBObject(collection, cursor.next());
            docs.put(foundDoc.getId(), foundDoc);
        }
    }
    return docs;
}
Also used : DBCursor(com.mongodb.DBCursor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) QueryBuilder(com.mongodb.QueryBuilder) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 17 with QueryBuilder

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

the class MongoDbTest method manyChildNodes.

@Test
@Ignore
public void manyChildNodes() {
    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);
    // index on (_id, _mod):
    // Query plan: { "cursor" : "BtreeCursor _id_1__mod_-1" ,
    // "isMultiKey" : false , "n" : 2000 , "nscannedObjects" : 2000 ,
    // "nscanned" : 954647 , "nscannedObjectsAllPlans" : 1907080 ,
    // "nscannedAllPlans" : 2859727 , "scanAndOrder" : false ,
    // "indexOnly" : true , "nYields" : 5 , "nChunkSkips" : 0 ,
    // "millis" : 5112 ,...
    // Time: 2229 ms
    // Count: 2000
    // index on (_mod, _id)
    // Query plan: { "cursor" : "BtreeCursor _mod_-1__id_1" ,
    // "isMultiKey" : false , "n" : 2000 , "nscannedObjects" : 2000 ,
    // "nscanned" : 2000 , "nscannedObjectsAllPlans" : 2203 ,
    // "nscannedAllPlans" : 2203 , "scanAndOrder" : false ,
    // "indexOnly" : true , "nYields" : 0 , "nChunkSkips" : 0 ,
    // "millis" : 3 ,...
    // Time: 43 ms
    // Count: 2000
    int children = 1000000;
    int perInsert = 1000;
    int group = 0;
    String parent = "/parent/node/abc";
    for (int i = 0; i < children; ) {
        DBObject[] inserts = new DBObject[perInsert];
        group++;
        for (int j = 0; j < perInsert; j++, i++) {
            BasicDBObject doc = new BasicDBObject();
            inserts[j] = doc;
            doc.put("_id", parent + "/node" + i);
            doc.put("_mod", group);
        }
        nodes.insert(inserts, WriteConcern.SAFE);
        log("inserted " + i + "/" + children);
    }
    QueryBuilder queryBuilder = QueryBuilder.start("_mod");
    queryBuilder.greaterThanEquals(group - 1);
    queryBuilder.and("_id").greaterThan(parent + "/");
    queryBuilder.and("_id").lessThanEquals(parent + "0");
    DBObject query = queryBuilder.get();
    BasicDBObject keys = new BasicDBObject();
    keys.put("_id", 1);
    DBCursor cursor = nodes.find(query, keys);
    int count = 0;
    log("Query plan: " + cursor.explain());
    long time = System.currentTimeMillis();
    while (cursor.hasNext()) {
        DBObject obj = cursor.next();
        // dummy read operation (to ensure we did get the data)
        obj.get("_id");
        count++;
    // log(" read " + obj);
    }
    time = System.currentTimeMillis() - time;
    log("Time: " + time + " ms");
    log("Count: " + count);
    db.getMongo().close();
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) 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