Package com.mongodb

Examples of com.mongodb.DBCollection


    @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.ensureIndex(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();
View Full Code Here


    @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.ensureIndex(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 /*upsert*/);

            // 250 ms WriteConcern.NORMAL, NONE
            // 891 ms WriteConvern.SAFE
View Full Code Here

    @Ignore
    @Test
    public void performBenchMark_WriteConcern() throws UnknownHostException, InterruptedException {
        Mongo mongo = new Mongo(new DBAddress(remoteServer));
        final DB db = mongo.getDB(TEST_DB1);
        final DBCollection nodes = db.getCollection("nodes");
        final DBCollection blobs = db.getCollection("blobs");
        int readers = 0;
        int writers = 2;
        for(WriteConcern wc : namedConcerns.keySet()){
            prepareDB(nodes,blobs);
            final Benchmark b = new Benchmark(nodes, blobs);
View Full Code Here

    }

    private void run(Mongo mongo, boolean useSameDB, boolean remote) throws InterruptedException {
        final DB nodeDB = mongo.getDB(TEST_DB1);
        final DB blobDB = useSameDB ? mongo.getDB(TEST_DB1) : mongo.getDB(TEST_DB2);
        final DBCollection nodes = nodeDB.getCollection("nodes");
        final DBCollection blobs = blobDB.getCollection("blobs");

        for (int readers : READERS) {
            for (int writers : WRITERS) {
                prepareDB(nodes,blobs);
                final Benchmark b = new Benchmark(nodes, blobs);
View Full Code Here

        Message responseMessage = prepareResponseMessage(exchange, operation);
        responseMessage.setBody(result);
    }

    protected void doRemove(Exchange exchange) throws Exception {
        DBCollection dbCol = calculateCollection(exchange);
        DBObject removeObj = exchange.getIn().getMandatoryBody(DBObject.class);

        WriteConcern wc = extractWriteConcern(exchange);
        WriteResult result = wc == null ? dbCol.remove(removeObj) : dbCol.remove(removeObj, wc);

        Message resultMessage = prepareResponseMessage(exchange, MongoDbOperation.remove);
        // we always return the WriteResult, because whether the getLastError was called or not,
        // the user will have the means to call it or obtain the cached CommandResult
        processAndTransferWriteResult(result, exchange);
View Full Code Here

        resultMessage.setHeader(MongoDbConstants.RECORDS_AFFECTED, result.getN());
    }

    @SuppressWarnings("unchecked")
    protected void doUpdate(Exchange exchange) throws Exception {
        DBCollection dbCol = calculateCollection(exchange);
        List<DBObject> saveObj = exchange.getIn().getMandatoryBody((Class<List<DBObject>>)(Class<?>)List.class);
        if (saveObj.size() != 2) {
            throw new CamelMongoDbException("MongoDB operation = insert, failed because body is not a List of DBObject objects with size = 2");
        }

        DBObject updateCriteria = saveObj.get(0);
        DBObject objNew = saveObj.get(1);

        Boolean multi = exchange.getIn().getHeader(MongoDbConstants.MULTIUPDATE, Boolean.class);
        Boolean upsert = exchange.getIn().getHeader(MongoDbConstants.UPSERT, Boolean.class);

        WriteResult result;
        WriteConcern wc = extractWriteConcern(exchange);
        // In API 2.7, the default upsert and multi values of update(DBObject, DBObject) are false, false, so we unconditionally invoke the
        // full-signature method update(DBObject, DBObject, boolean, boolean). However, the default behaviour may change in the future,
        // so it's safer to be explicit at this level for full determinism
        if (multi == null && upsert == null) {
            // for update with no multi nor upsert but with specific WriteConcern there is no update signature without multi and upsert args,
            // so assume defaults
            result = wc == null ? dbCol.update(updateCriteria, objNew) : dbCol.update(updateCriteria, objNew, false, false, wc);
        } else {
            // we calculate the final boolean values so that if any of these
            // parameters is null, it is resolved to false
            result = wc == null ? dbCol.update(updateCriteria, objNew, calculateBooleanValue(upsert), calculateBooleanValue(multi)) : dbCol
                .update(updateCriteria, objNew, calculateBooleanValue(upsert), calculateBooleanValue(multi), wc);
        }

        Message resultMessage = prepareResponseMessage(exchange, MongoDbOperation.update);
        // we always return the WriteResult, because whether the getLastError was called or not, the user will have the means to call it or
View Full Code Here

        processAndTransferWriteResult(result, exchange);
        resultMessage.setHeader(MongoDbConstants.RECORDS_AFFECTED, result.getN());
    }

    protected void doSave(Exchange exchange) throws Exception {
        DBCollection dbCol = calculateCollection(exchange);
        DBObject saveObj = exchange.getIn().getMandatoryBody(DBObject.class);

        WriteConcern wc = extractWriteConcern(exchange);
        WriteResult result = wc == null ? dbCol.save(saveObj) : dbCol.save(saveObj, wc);

        prepareResponseMessage(exchange, MongoDbOperation.save);
        // we always return the WriteResult, because whether the getLastError was called or not, the user will have the means to call it or
        // obtain the cached CommandResult
        processAndTransferWriteResult(result, exchange);
View Full Code Here

        // obtain the cached CommandResult
        processAndTransferWriteResult(result, exchange);
    }

    protected void doFindById(Exchange exchange) throws Exception {
        DBCollection dbCol = calculateCollection(exchange);
        Object o = exchange.getIn().getMandatoryBody();
        DBObject ret;

        DBObject fieldFilter = exchange.getIn().getHeader(MongoDbConstants.FIELDS_FILTER, DBObject.class);
        if (fieldFilter == null) {
            ret = dbCol.findOne(o);
        } else {
            ret = dbCol.findOne(o, fieldFilter);
        }

        Message resultMessage = prepareResponseMessage(exchange, MongoDbOperation.save);
        resultMessage.setBody(ret);
        resultMessage.setHeader(MongoDbConstants.RESULT_TOTAL_SIZE, ret == null ? 0 : 1);
View Full Code Here

        resultMessage.setHeader(MongoDbConstants.RESULT_TOTAL_SIZE, ret == null ? 0 : 1);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    protected void doInsert(Exchange exchange) throws Exception {
        DBCollection dbCol = calculateCollection(exchange);
        boolean singleInsert = true;
        Object insert = exchange.getIn().getBody(DBObject.class);
        // body could not be converted to DBObject, check to see if it's of type List<DBObject>
        if (insert == null) {
            insert = exchange.getIn().getBody(List.class);
            // if the body of type List was obtained, ensure that all items are of type DBObject and cast the List to List<DBObject>
            if (insert != null) {
                singleInsert = false;
                insert = attemptConvertToList((List)insert, exchange);
            } else {
                throw new CamelMongoDbException("MongoDB operation = insert, Body is not conversible to type DBObject nor List<DBObject>");
            }
        }

        WriteResult result;
        WriteConcern wc = extractWriteConcern(exchange);
        if (singleInsert) {
            result = wc == null ? dbCol.insert((DBObject)insert) : dbCol.insert((DBObject)insert, wc);
        } else {
            result = wc == null ? dbCol.insert((List<DBObject>)insert) : dbCol.insert((List<DBObject>)insert, wc);
        }

        Message resultMessage = prepareResponseMessage(exchange, MongoDbOperation.insert);
        // we always return the WriteResult, because whether the getLastError was called or not, the user will have the means to call it or
        // obtain the cached CommandResult
View Full Code Here

        processAndTransferWriteResult(result, exchange);
        resultMessage.setBody(result);
    }

    protected void doFindAll(Exchange exchange) throws Exception {
        DBCollection dbCol = calculateCollection(exchange);
        // do not use getMandatoryBody, because if the body is empty we want to retrieve all objects in the collection
        DBObject query = null;
        // do not run around looking for a type converter unless there is a need for it
        if (exchange.getIn().getBody() != null) {
            query = exchange.getIn().getBody(DBObject.class);
        }
        DBObject fieldFilter = exchange.getIn().getHeader(MongoDbConstants.FIELDS_FILTER, DBObject.class);

        // get the batch size and number to skip
        Integer batchSize = exchange.getIn().getHeader(MongoDbConstants.BATCH_SIZE, Integer.class);
        Integer numToSkip = exchange.getIn().getHeader(MongoDbConstants.NUM_TO_SKIP, Integer.class);
        Integer limit = exchange.getIn().getHeader(MongoDbConstants.LIMIT, Integer.class);
        DBObject sortBy = exchange.getIn().getHeader(MongoDbConstants.SORT_BY, DBObject.class);
        DBCursor ret = null;
        try {
            if (query == null && fieldFilter == null) {
                ret = dbCol.find(new BasicDBObject());
            } else if (fieldFilter == null) {
                ret = dbCol.find(query);
            } else {
                ret = dbCol.find(query, fieldFilter);
            }

            if (sortBy != null) {
                ret.sort(sortBy);
            }
View Full Code Here

TOP

Related Classes of com.mongodb.DBCollection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.