Example usage for com.mongodb CommandResult throwOnError

List of usage examples for com.mongodb CommandResult throwOnError

Introduction

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

Prototype

public void throwOnError() 

Source Link

Document

Throws a CommandFailureException if the command failed.

Usage

From source file:com.edgytech.umongo.CollectionNode.java

License:Apache License

@Override
protected void refreshNode() {
    CommandResult res = collection.getStats();
    res.throwOnError();
    stats = res;
}

From source file:com.edgytech.umongo.CollectionPanel.java

License:Apache License

public void mapReduce(final ButtonBase button) {
    final DBCollection col = getCollectionNode().getCollection();
    String map = getStringFieldValue(Item.mrMap);
    String reduce = getStringFieldValue(Item.mrReduce);
    String finalize = getStringFieldValue(Item.mrFinalize);
    String stype = getStringFieldValue(Item.mrType);
    final OutputType type = OutputType.valueOf(stype.toUpperCase());
    String out = getStringFieldValue(Item.mrOut);
    if (type != OutputType.INLINE && (out.isEmpty())) {
        new InfoDialog(id, null, null, "Output collection cannot be empty if type is not inline.").show();
        return;/* w  w  w .  j  a  v  a  2s .  co m*/
    }

    String outDB = getStringFieldValue(Item.mrOutDB);
    DBObject query = ((DocBuilderField) getBoundUnit(Item.mrQuery)).getDBObject();
    int limit = getIntFieldValue(Item.mrLimit);
    final MapReduceCommand cmd = new MapReduceCommand(col, map, reduce, out, type, query);
    DBObject sort = ((DocBuilderField) getBoundUnit(Item.mrSort)).getDBObject();
    if (sort != null) {
        cmd.setSort(sort);
    }
    if (!outDB.isEmpty()) {
        cmd.setOutputDB(outDB);
    }
    if (!finalize.isEmpty()) {
        cmd.setFinalize(finalize);
    }
    if (limit > 0) {
        cmd.setLimit(limit);
    }

    if (getBooleanFieldValue(Item.mrJSMode)) {
        cmd.addExtraOption("jsMode", true);
    }

    final BasicDBObject cmdobj = (BasicDBObject) cmd.toDBObject();
    if (getBooleanFieldValue(Item.mrOutSharded)) {
        ((BasicDBObject) cmdobj.get("out")).put("sharded", true);
    }
    if (getBooleanFieldValue(Item.mrNonAtomic)) {
        ((BasicDBObject) cmdobj.get("out")).put("nonAtomic", true);
    }

    new DbJob() {
        MapReduceOutput output;

        @Override
        public Object doRun() {
            //                output = col.mapReduce(cmd);

            // if type in inline, then query options like slaveOk is fine
            CommandResult res = null;
            if (type == MapReduceCommand.OutputType.INLINE) {
                res = col.getDB().command(cmdobj, col.getOptions());
                return res;
            }

            res = col.getDB().command(cmdobj);
            res.throwOnError();
            output = new MapReduceOutput(col, cmdobj, res);
            return output;
        }

        @Override
        public void wrapUp(Object res) {
            if (output != null) {
                if (cmd.getOutputType() == OutputType.INLINE) {
                    res = output.results();
                } else {
                    // spawn a find
                    doFind(output.getOutputCollection(), null);
                    res = output.getRaw();
                }
            }
            super.wrapUp(res);
        }

        @Override
        public String getNS() {
            return col.getFullName();
        }

        @Override
        public String getShortName() {
            return "MR";
        }

        @Override
        public DBObject getRoot(Object result) {
            return cmdobj;
        }

        @Override
        public ButtonBase getButton() {
            return button;
        }

        @Override
        DBObject getCommand() {
            return cmdobj;
        }

        @Override
        DB getDB() {
            return col.getDB();
        }
    }.addJob();
}

From source file:com.edgytech.umongo.DbNode.java

License:Apache License

@Override
protected void refreshNode() {
    // db.getStats can be slow..
    // can't use driver's because doesnt use slaveOk
    CommandResult res = db.command(new BasicDBObject("dbstats", 1), db.getMongo().getOptions());
    //        CommandResult res = db.command(new BasicDBObject("profile", -1));
    res.throwOnError();
    stats = res;/*from  ww w.j a  v  a  2  s  . c om*/
    //        db.getCollection("foo").save(new BasicDBObject("a", 1));
}

From source file:com.edgytech.umongo.IndexNode.java

License:Apache License

@Override
protected void refreshNode() {
    CommandResult res = getStatsCollection().getStats();
    res.throwOnError();
    stats = res;
}

From source file:com.edgytech.umongo.ServerNode.java

License:Apache License

@Override
protected void refreshNode() {
    CommandResult res = getServerMongoClient().getDB("local").command("isMaster");
    res.throwOnError();
    stats = res;/*  w  w w  .  ja v a2 s  .c om*/
}

From source file:com.edgytech.umongo.ServerPanel.java

License:Apache License

public void getLog(ButtonBase button) {
    final DB db = getServerNode().getServerMongoClient().getDB("admin");
    final String type = getStringFieldValue(Item.getLogType);
    final DBObject cmd = new BasicDBObject("getLog", type);
    new DbJob() {

        @Override// w w w. j  a  va  2  s .  c o  m
        public Object doRun() throws Exception {
            CommandResult res = db.command(cmd);
            res.throwOnError();
            StringBuilder sb = new StringBuilder();
            BasicDBList list = (BasicDBList) res.get("log");
            for (Object str : list) {
                sb.append(str);
                sb.append("\n");
            }
            return sb.toString();
        }

        @Override
        public String getNS() {
            return db.getName();
        }

        @Override
        public String getShortName() {
            return cmd.keySet().iterator().next();
        }

    }.addJob();
}

From source file:com.github.maasdi.di.trans.steps.mongodbdelete.MongoDbDelete.java

License:Apache License

protected void commitDelete(DBObject deleteQuery, Object[] row) throws KettleException {
    int retrys = 0;
    MongoException lastEx = null;/*from   w w w.j  a v a 2 s .c  o m*/

    while (retrys <= m_writeRetries && !isStopped()) {
        WriteResult result = null;
        CommandResult cmd = null;
        try {
            logDetailed(BaseMessages.getString(PKG, "MongoDbDelete.Message.ExecutingQuery", deleteQuery));
            result = data.getCollection().drop(deleteQuery);

            cmd = result.getLastError();
            if (cmd != null && !cmd.ok()) {
                String message = cmd.getErrorMessage();
                logError(BaseMessages.getString(PKG, "MongoDbDelete.ErrorMessage.MongoReported", message));

                cmd.throwOnError();
            }
        } catch (MongoException me) {
            lastEx = me;
            retrys++;
            if (retrys <= m_writeRetries) {
                logError(BaseMessages.getString(PKG, "MongoDbDelete.ErrorMessage.ErrorWritingToMongo",
                        me.toString()));
                logBasic(BaseMessages.getString(PKG, "MongoDbDelete.Message.Retry", m_writeRetryDelay));
                try {
                    Thread.sleep(m_writeRetryDelay * 1000);
                    // CHECKSTYLE:OFF
                } catch (InterruptedException e) {
                    // CHECKSTYLE:ON
                }
            }
        }

        if (cmd != null && cmd.ok()) {
            break;
        }
    }

    if ((retrys > m_writeRetries || isStopped()) && lastEx != null) {

        // Send this one to the error stream if doing error handling
        if (getStepMeta().isDoingErrorHandling()) {
            putError(getInputRowMeta(), row, 1, lastEx.getMessage(), "", "MongoDbDelete");
        } else {
            throw new KettleException(lastEx);
        }
    }
}

From source file:com.icoin.trading.tradeengine.query.tradeexecuted.repositories.TradeExecutedQueryRepositoryImpl.java

License:Apache License

/**
 * Inspects the given {@link CommandResult} for erros and potentially throws an
 * {@link org.springframework.dao.InvalidDataAccessApiUsageException} for that error.
 *
 * @param result must not be {@literal null}.
 * @param source must not be {@literal null}.
 *///w  w w. j a va  2s .co m
private void handleCommandError(CommandResult result, DBObject source) {

    try {
        result.throwOnError();
    } catch (MongoException ex) {

        String error = result.getErrorMessage();
        error = error == null ? "NO MESSAGE" : error;

        throw new InvalidDataAccessApiUsageException(
                "Command execution failed:  Error [" + error + "], Command = " + source, ex);
    }
}

From source file:com.ninjas.movietime.health.MongoDbHealth.java

License:MIT License

@Override
public Health health() {
    try {//from w w w. ja  v a2  s . c o  m
        Preconditions.checkNotNull(mongoTemplate, "mongoTemplate is null");
        DBObject ping = new BasicDBObject("ping", "1");
        final CommandResult pingCommandResult = mongoTemplate.getDb().command(ping);
        pingCommandResult.throwOnError();
        //ping worked, now getting version
        DBObject serverStatus = new BasicDBObject("serverStatus", "1");
        final CommandResult serverStatusCommandResult = mongoTemplate.getDb().command(serverStatus);
        serverStatusCommandResult.throwOnError();

        final String server = pingCommandResult.getServerUsed().toString();
        final String version = serverStatusCommandResult.get("version").toString();

        Preconditions.checkNotNull(server, "Mongo DB Server address is null");
        Preconditions.checkNotNull(version, "Mongo DB version is null");

        return Health.up().withDetail("database", "mongodb").withDetail("server", server)
                .withDetail("version", version).build();
    } catch (Exception ex) {
        return Health.down(ex).build();
    }
}

From source file:de.otto.mongodb.profiler.op.DefaultOpProfiler.java

License:Apache License

@Override
public ProfilingLevel getProfilingLevel() {
    final CommandResult result = db.command(BasicDBObjectBuilder.start("profile", -1).get());
    result.throwOnError();
    return ProfilingLevel.forValue(result.getInt("was", Integer.MIN_VALUE));
}