Example usage for com.mongodb CommandResult getBoolean

List of usage examples for com.mongodb CommandResult getBoolean

Introduction

In this page you can find the example usage for com.mongodb CommandResult getBoolean.

Prototype

public boolean getBoolean(final String key, final boolean def) 

Source Link

Document

Returns the value of a field as a boolean

Usage

From source file:com.zjy.mongo.splitter.MongoSplitterFactory.java

License:Apache License

public static MongoCollectionSplitter getSplitterByStats(final MongoClientURI uri, final Configuration config) {
    /* Looks at the collection in mongo.input.uri
     * and choose an implementation based on what's in there.  */

    MongoCollectionSplitter returnVal;//w  w  w.  ja v  a2s .co m

    // If the split calculation is totally disabled, just make one
    // big split for the whole collection.
    if (!MongoConfigUtil.createInputSplits(config)) {
        returnVal = new SingleMongoSplitter(config);
    } else {
        MongoClientURI authURI = MongoConfigUtil.getAuthURI(config);
        CommandResult stats;
        DBCollection coll = null;
        try {
            if (authURI != null) {
                coll = MongoConfigUtil.getCollectionWithAuth(uri, authURI);
                stats = coll.getStats();
                LOG.info("Retrieved Collection stats:" + stats);
            } else {
                coll = MongoConfigUtil.getCollection(uri);
                stats = coll.getStats();
            }
        } finally {
            if (coll != null) {
                MongoConfigUtil.close(coll.getDB().getMongo());
            }
        }

        if (!stats.getBoolean("ok", false)) {
            throw new RuntimeException(
                    "Unable to calculate input splits from collection stats: " + stats.getString("errmsg"));
        }

        if (!stats.getBoolean("sharded", false)) {
            returnVal = new StandaloneMongoSplitter(config);
        } else {
            // Collection is sharded
            if (MongoConfigUtil.isShardChunkedSplittingEnabled(config)) {
                // Creates one split per chunk. 
                returnVal = new ShardChunkMongoSplitter(config);
            } else if (MongoConfigUtil.canReadSplitsFromShards(config)) {
                // Creates one split per shard, but ignores chunk bounds. 
                // Reads from shards directly (bypassing mongos).
                // Not usually recommended.
                returnVal = new ShardMongoSplitter(config);
            } else {
                //Not configured to use chunks or shards -
                //so treat this the same as if it was an unsharded collection
                returnVal = new StandaloneMongoSplitter(config);
            }
        }
    }
    return returnVal;
}

From source file:org.mule.module.mongo.tools.OplogCollection.java

License:Open Source License

private boolean isMaster() throws IOException {
    // Validate we are on master or replica
    CommandResult commandResult = admin.command(new BasicDBObject(IS_MASTER_FIELD, 1));
    boolean isMaster = commandResult.getBoolean(IS_MASTER_FIELD, false);

    // Replica set member
    if (commandResult.containsField("hosts")) {
        return false;
    } else {//from  w  w  w.j  a v  a 2s  .c o  m
        if (!isMaster) {
            throw new IOException("oplog mode is only supported on master or replica set member");
        }
        return true;
    }

}