Example usage for com.mongodb CommandResult ok

List of usage examples for com.mongodb CommandResult ok

Introduction

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

Prototype

public boolean ok() 

Source Link

Document

Gets the "ok" field, which is whether this command executed correctly or not.

Usage

From source file:brooklyn.entity.nosql.mongodb.MongoDBClientSupport.java

License:Apache License

private Optional<CommandResult> runDBCommand(String database, DBObject command) {
    MongoClient client = client();//from  w w w  .  j  a  v a2s. c  o m
    try {
        DB db = client.getDB(database);
        CommandResult status;
        try {
            status = db.command(command);
        } catch (MongoException e) {
            LOG.warn("Command " + command + " on " + getServerAddress() + " failed", e);
            return Optional.absent();
        }
        if (!status.ok()) {
            LOG.debug("Unexpected result of {} on {}: {}",
                    new Object[] { command, getServerAddress(), status.getErrorMessage() });
        }
        return Optional.of(status);
    } finally {
        client.close();
    }
}

From source file:com.appdynamics.monitors.mongo.MongoDBMonitor.java

License:Apache License

private void fetchAndPrintCollectionStats() {
    for (String databaseName : mongoClient.listDatabaseNames()) {
        DB db = mongoClient.getDB(databaseName);
        Set<String> collectionNames = db.getCollectionNames();
        if (collectionNames != null && collectionNames.size() > 0) {
            for (String collectionName : collectionNames) {
                DBCollection collection = db.getCollection(collectionName);
                CommandResult collectionStatsResult = collection.getStats();
                if (collectionStatsResult != null && collectionStatsResult.ok()) {
                    DBObject collectionStats = (DBObject) JSON.parse(collectionStatsResult.toString());
                    printCollectionStats(db.getName(), collectionName, collectionStats);
                } else {
                    String errorMessage = "Retrieving stats for collection " + collectionName + " of "
                            + db.getName() + " failed";
                    if (collectionStatsResult != null) {
                        errorMessage = errorMessage
                                .concat(" with error message " + collectionStatsResult.getErrorMessage());
                    }/*www. ja v  a  2s.  co m*/
                    logger.error(errorMessage);
                }
            }
        }
    }
}

From source file:com.bugull.mongo.AdvancedDao.java

License:Apache License

public Iterable<DBObject> mapReduce(MapReduceCommand cmd) throws MapReduceException {
    MapReduceOutput output = coll.mapReduce(cmd);
    CommandResult cr = output.getCommandResult();
    if (!cr.ok()) {
        throw new MapReduceException(cr.getErrorMessage());
    }//from ww w . j a  v  a 2s .  co  m
    return output.results();
}

From source file:com.bugull.mongo.AdvancedDao.java

License:Apache License

public Iterable<DBObject> mapReduce(String map, String reduce) throws MapReduceException {
    MapReduceOutput output = coll.mapReduce(map, reduce, null, OutputType.INLINE, null);
    CommandResult cr = output.getCommandResult();
    if (!cr.ok()) {
        throw new MapReduceException(cr.getErrorMessage());
    }/*from w  w  w. j  a  v  a 2  s. co  m*/
    return output.results();
}

From source file:com.bugull.mongo.AdvancedDao.java

License:Apache License

private Iterable<DBObject> mapReduce(String map, String reduce, DBObject query) throws MapReduceException {
    MapReduceOutput output = coll.mapReduce(map, reduce, null, OutputType.INLINE, query);
    CommandResult cr = output.getCommandResult();
    if (!cr.ok()) {
        throw new MapReduceException(cr.getErrorMessage());
    }/* w w w .  ja va  2 s  . c om*/
    return output.results();
}

From source file:com.bugull.mongo.AdvancedDao.java

License:Apache License

private synchronized Iterable<DBObject> mapReduce(String map, String reduce, String outputTarget,
        MapReduceCommand.OutputType outputType, String orderBy, DBObject query) throws MapReduceException {
    MapReduceOutput output = coll.mapReduce(map, reduce, outputTarget, outputType, query);
    CommandResult cr = output.getCommandResult();
    if (!cr.ok()) {
        throw new MapReduceException(cr.getErrorMessage());
    }//from w w  w.  j  av  a  2 s.c  o m
    DBCollection c = output.getOutputCollection();
    DBCursor cursor = null;
    if (orderBy != null) {
        cursor = c.find().sort(MapperUtil.getSort(orderBy));
    } else {
        cursor = c.find();
    }
    List<DBObject> list = new ArrayList<DBObject>();
    for (Iterator<DBObject> it = cursor.iterator(); it.hasNext();) {
        list.add(it.next());
    }
    return list;
}

From source file:com.bugull.mongo.AdvancedDao.java

License:Apache License

private synchronized Iterable<DBObject> mapReduce(String map, String reduce, String outputTarget,
        MapReduceCommand.OutputType outputType, String orderBy, int pageNum, int pageSize, DBObject query)
        throws MapReduceException {
    MapReduceOutput output = coll.mapReduce(map, reduce, outputTarget, outputType, query);
    CommandResult cr = output.getCommandResult();
    if (!cr.ok()) {
        throw new MapReduceException(cr.getErrorMessage());
    }/*from w  ww. j a va  2  s.  c o  m*/
    DBCollection c = output.getOutputCollection();
    DBCursor cursor = null;
    if (orderBy != null) {
        cursor = c.find().sort(MapperUtil.getSort(orderBy)).skip((pageNum - 1) * pageSize).limit(pageSize);
    } else {
        cursor = c.find().skip((pageNum - 1) * pageSize).limit(pageSize);
    }
    List<DBObject> list = new ArrayList<DBObject>();
    for (Iterator<DBObject> it = cursor.iterator(); it.hasNext();) {
        list.add(it.next());
    }
    return list;
}

From source file:com.bugull.mongo.BuguAggregation.java

License:Apache License

public Iterable<DBObject> results() throws AggregationException {
    int size = stages.size();
    if (size <= 0) {
        throw new AggregationException("Empty stage in aggregation pipeline!");
    }/*from   w w w . j a  v  a2  s . c om*/
    AggregationOutput output = coll.aggregate(stages);
    CommandResult cr = output.getCommandResult();
    if (!cr.ok()) {
        throw new AggregationException(cr.getErrorMessage());
    }
    return output.results();
}

From source file:com.ca.apm.mongo.Collector.java

License:Open Source License

private boolean isInShardCluster(final CommandResult cr) throws Exception {

    MongoServer ms = new MongoServer(getMyself(cr));
    final String host = ms.getHost();
    final int port = ms.getPort();

    boolean sharded = false;

    final String msg = cr.getString("msg");
    if ((msg != null) && msg.contains("isdbgrid")) {
        sharded = true;//  www .  ja va2s  .  c o m
    } else if (cr.getBoolean("ismaster")) {
        final CommandResult shardState = dbAdminCmd(host, port, "shardingState");
        // shardingState command only returns OK when server is in a sharded
        // cluster
        if (shardState.ok()) {
            if (shardState.getBoolean("enabled")) {
                sharded = true;
            }
        }
    } else if (cr.containsField("primary")) {
        // we are in a replica set but not the primary,
        // check the primary to see if it is a shard member
        final String primary = cr.getString("primary");
        ms = new MongoServer(primary);
        final CommandResult priIsMaster = dbAdminCmd(ms.getHost(), ms.getPort(), "isMaster");
        sharded = isInShardCluster(priIsMaster);
    }
    return sharded;
}

From source file:com.ca.apm.mongo.Collector.java

License:Open Source License

private String getClusterNodeType() throws Exception {

    final String host = getStringProp(DB_HOST_PROP);
    final int port = getIntProp(DB_PORT_PROP);

    String nodeType = null;/*from  www  .ja  va  2  s. com*/

    final CommandResult isMaster = dbAdminCmd(host, port, "isMaster");
    if (isMaster.getBoolean("ismaster")) {
        final String msg = isMaster.getString("msg");
        if (msg != null && msg.contains("isdbgrid")) {
            nodeType = "shardRouter";
        } else {
            final CommandResult shardState = dbAdminCmd(host, port, "shardingState");
            if (shardState.ok() && shardState.getBoolean("enabled")) {
                if (isConfigServer(host, port)) {
                    nodeType = "shardConfigServer";
                } else {
                    nodeType = "shardMember";
                }
            }
        }
    } else if (isReplicaMember(isMaster)) {
        nodeType = "shardMember";
    }

    return nodeType;
}