Search in sources :

Example 76 with DBCollection

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

the class MongoDocumentStoreTest method doInsert.

private void doInsert(int n, boolean batch) throws Exception {
    dropCollections();
    DBCollection collection = connectionFactory.getConnection().getDB().getCollection("batchInsertTest");
    DBObject index = new BasicDBObject();
    index.put("_path", 1L);
    DBObject options = new BasicDBObject();
    options.put("unique", Boolean.TRUE);
    collection.createIndex(index, options);
    log("Inserting " + n + " batch? " + batch);
    long start = System.currentTimeMillis();
    if (batch) {
        DBObject[] arr = new BasicDBObject[n];
        for (int i = 0; i < n; i++) {
            arr[i] = new BasicDBObject("_path", "/a" + i);
        }
        collection.insert(arr);
    } else {
        for (int i = 0; i < n; i++) {
            collection.insert(new BasicDBObject("_path", "/a" + i));
        }
    }
    long end = System.currentTimeMillis();
    log("Done: " + (end - start) + "ms");
    dropCollections();
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 77 with DBCollection

use of com.mongodb.DBCollection 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)

Example 78 with DBCollection

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

the class MongoBlobGCTest method deleteFromMongo.

private void deleteFromMongo(String nodeId) {
    DBCollection coll = mongoConnection.getDB().getCollection("nodes");
    BasicDBObject blobNodeObj = new BasicDBObject();
    blobNodeObj.put("_id", "1:/" + nodeId);
    coll.remove(blobNodeObj);
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject)

Example 79 with DBCollection

use of com.mongodb.DBCollection in project sling by apache.

the class MongoDBResourceProvider method commit.

/**
     * @see org.apache.sling.api.resource.ModifyingResourceProvider#commit(ResourceResolver)
     */
public void commit(final ResourceResolver resolver) throws PersistenceException {
    try {
        for (final String deleted : this.deletedResources) {
            final String[] info = this.extractResourceInfo(deleted);
            // check if the collection still exists
            final DBCollection col = this.getCollection(info[0]);
            if (col != null) {
                if (col.findAndRemove(QueryBuilder.start(getPROP_PATH()).is(info[1]).get()) != null) {
                    this.context.notifyRemoved(info);
                }
            }
        }
        for (final MongoDBResource changed : this.changedResources.values()) {
            final DBCollection col = this.context.getDatabase().getCollection(changed.getCollection());
            if (col != null) {
                final String[] info = new String[] { changed.getCollection(), changed.getProperties().get(getPROP_PATH()).toString() };
                // create or update?
                if (changed.getProperties().get(PROP_ID) != null) {
                    col.update(QueryBuilder.start(getPROP_PATH()).is(changed.getProperties().get(getPROP_PATH())).get(), changed.getProperties());
                    this.context.notifyUpdated(info);
                } else {
                    // create
                    col.save(changed.getProperties());
                    this.context.notifyUpdated(info);
                }
            } else {
                throw new PersistenceException("Unable to create collection " + changed.getCollection(), null, changed.getPath(), null);
            }
        }
    } finally {
        this.revert(resolver);
    }
}
Also used : DBCollection(com.mongodb.DBCollection) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Example 80 with DBCollection

use of com.mongodb.DBCollection in project sling by apache.

the class MongoDBResourceProvider method findResources.

public Iterator<Resource> findResources(final ResourceResolver resolver, String query, String language) {
    if (!language.equals("mongodb") || query == null || query.length() == 0 || query.indexOf(".find(") <= 0) {
        return null;
    }
    Iterator<Resource> returnValue = null;
    final String collectionName = query.substring(0, query.indexOf(".find("));
    DBCollection col = this.getCollection(collectionName);
    if (col != null) {
        String criteria = query.trim().substring(query.indexOf(".find(") + 6, query.length() - 1);
        DBObject dbObject = (DBObject) JSON.parse(criteria);
        final DBCursor cur = col.find(dbObject);
        final String rootPath = context.getRootWithSlash();
        return new Iterator<Resource>() {

            public boolean hasNext() {
                return cur.hasNext();
            }

            public Resource next() {
                final DBObject obj = cur.next();
                final String objPath = obj.get(getPROP_PATH()).toString();
                final int lastSlash = objPath.lastIndexOf('/');
                final String name;
                if (lastSlash == -1) {
                    name = objPath;
                } else {
                    name = objPath.substring(lastSlash + 1);
                }
                return new MongoDBResource(resolver, rootPath + collectionName + "/" + name, collectionName, obj, MongoDBResourceProvider.this);
            }

            public void remove() {
                throw new UnsupportedOperationException("remove");
            }
        };
    }
    return returnValue;
}
Also used : DBCollection(com.mongodb.DBCollection) DBCursor(com.mongodb.DBCursor) Resource(org.apache.sling.api.resource.Resource) Iterator(java.util.Iterator) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Aggregations

DBCollection (com.mongodb.DBCollection)174 DBObject (com.mongodb.DBObject)92 BasicDBObject (com.mongodb.BasicDBObject)91 Test (org.junit.Test)70 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)29 DBCursor (com.mongodb.DBCursor)25 DB (com.mongodb.DB)23 MongoException (com.mongodb.MongoException)22 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)17 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)12 JSONObject (org.json.JSONObject)12 MongoClientURI (com.mongodb.MongoClientURI)11 List (java.util.List)11 Map (java.util.Map)11 QueryBuilder (com.mongodb.QueryBuilder)10 HashMap (java.util.HashMap)10 Stopwatch (com.google.common.base.Stopwatch)9 WriteResult (com.mongodb.WriteResult)9 MongoClient (com.mongodb.MongoClient)8 IOException (java.io.IOException)8