/** * Throws a {@code CommandFailureException} if the command failed. Otherwise, returns normally. * * @throws MongoException with the exception from the failed command * @see #ok() */ public void throwOnError() { if (!ok()) { throw createException(); } }
/** * Utility method to create an exception from a failed command. * * @return The mongo exception, or null if the command was successful. */ @Nullable public MongoException getException() { if (!ok()) { return createException(); } return null; }
@Nullable private Version getMongoVersion(DB adminDb) { final CommandResult buildInfoResult = adminDb.command("buildInfo"); if (buildInfoResult.ok()) { final BasicDBList versionArray = (BasicDBList) buildInfoResult.get("versionArray"); if (versionArray == null || versionArray.size() < 3) { LOG.debug("Couldn't retrieve MongoDB version"); return null; } final int majorVersion = (int) versionArray.get(0); final int minorVersion = (int) versionArray.get(1); final int patchVersion = (int) versionArray.get(2); return Version.forIntegers(majorVersion, minorVersion, patchVersion); } else { LOG.debug("Couldn't retrieve MongoDB buildInfo: {}", buildInfoResult.getErrorMessage()); return null; } }
private static BuildInfo createBuildInfo(DB adminDb) { final BuildInfo buildInfo; final CommandResult buildInfoResult = adminDb.command("buildInfo"); if (buildInfoResult.ok()) { buildInfo = BuildInfo.create( buildInfoResult.getString("version"), buildInfoResult.getString("gitVersion"), buildInfoResult.getString("sysInfo"), buildInfoResult.getString("loaderFlags"), buildInfoResult.getString("compilerFlags"), buildInfoResult.getString("allocator"), (List<Integer>) buildInfoResult.get("versionArray"), buildInfoResult.getString("javascriptEngine"), buildInfoResult.getInt("bits"), buildInfoResult.getBoolean("debug"), buildInfoResult.getLong("maxBsonObjectSize") ); } else { LOG.debug("Couldn't retrieve MongoDB buildInfo: {}", buildInfoResult.getErrorMessage()); buildInfo = null; } return buildInfo; }
private static HostInfo createHostInfo(DB adminDb) { final HostInfo hostInfo; final CommandResult hostInfoResult = adminDb.command("hostInfo"); if (hostInfoResult.ok()) { final BasicDBObject systemMap = (BasicDBObject) hostInfoResult.get("system"); final HostInfo.System system = HostInfo.System.create(
if (dbStatsResult.ok()) { final BasicDBObject extentFreeListMap = (BasicDBObject) dbStatsResult.get("extentFreeList"); final DatabaseStats.ExtentFreeList extentFreeList; if (serverStatusResult.ok()) { final BasicDBObject connectionsMap = (BasicDBObject) serverStatusResult.get("connections"); final ServerStatus.Connections connections = ServerStatus.Connections.create(
if (!cr.ok()) { logger.warn("serverStatus returns error: {}", cr.getErrorMessage()); return false;
void process(final MappedClass mc, final Validation validation) { if (validation != null) { String collectionName = mc.getCollectionName(); CommandResult result = getDB() .command(new BasicDBObject("collMod", collectionName) .append("validator", parse(validation.value())) .append("validationLevel", validation.level().getValue()) .append("validationAction", validation.action().getValue()) ); if (!result.ok()) { if (result.getInt("code") == 26) { ValidationOptions options = new ValidationOptions() .validator(parse(validation.value())) .validationLevel(validation.level()) .validationAction(validation.action()); getDatabase().createCollection(collectionName, new CreateCollectionOptions().validationOptions(options)); } else { result.throwOnError(); } } } }
/** * Throws a {@code CommandFailureException} if the command failed. Otherwise, returns normally. * * @throws MongoException with the exception from the failed command * @see #ok() */ public void throwOnError() { if (!ok()) { throw createException(); } }
public BasicBSONObject getServerStatus() { Optional<CommandResult> result = runDBCommand("admin", "serverStatus"); if (result.isPresent() && result.get().ok()) { return result.get(); } else { return EMPTY_RESPONSE; } }
/** * Runs replSetReconfig with the given BasicBSONObject. Returns true if the result's * status is ok. */ private boolean reconfigureReplicaSet(BasicBSONObject newConfig) { BasicDBObject command = new BasicDBObject("replSetReconfig", newConfig); LOG.debug("Reconfiguring replica set to: " + command); Optional<CommandResult> result = runDBCommand("admin", command); return result.isPresent() && result.get().ok(); }
@Test public void shouldNotErrorWhenCallingRepairDatabase() throws UnknownHostException { // given MongoClient mongoClient = new MongoClient(); DB database = mongoClient.getDB("database"); // when CommandResult result = database.command(new BasicDBObject("repairDatabase", 1)); // then assertThat(result, is(notNullValue())); assertThat(result.ok(), is(true)); }
public boolean initializeReplicaSet(String replicaSetName, Integer id) { ServerAddress primary = client.getServerAddressList().get(0); BasicBSONObject config = ReplicaSetConfig.builder(replicaSetName) .member(primary, id) .build(); BasicDBObject dbObject = new BasicDBObject("replSetInitiate", config); LOG.debug("Initiating replica set with: " + dbObject); Optional<CommandResult> result = runDBCommand("admin", dbObject); if (result.isPresent() && result.get().ok() && LOG.isDebugEnabled()) { LOG.debug("Completed intiating MongoDB replica set {} on entity {}", replicaSetName, this); } return result.isPresent() && result.get().ok(); }
/** * Utility method to create an exception from a failed command. * * @return The mongo exception, or null if the command was successful. */ @Nullable public MongoException getException() { if (!ok()) { return createException(); } return null; }
@Override public boolean delete(ObjectId id) { DBObject query = new BasicDBObject(); query.put("oid", id.toString()); return collection.remove(query).getLastError().ok(); }
@Override public boolean put(final RevObject object) { DBObject query = new BasicDBObject(); query.put("oid", object.getId().toString()); DBObject record = toDocument(object); return collection.update(query, record, true, false).getLastError().ok(); }
public Iterable<DBObject> mapReduce(String collectionName, String map, String reduce, DBObject query) throws OperatorException { MapReduceOutput _output = getCollection(collectionName).mapReduce(map, reduce, null, OutputType.INLINE, query); CommandResult _result = _output.getCommandResult(); if (!_result.ok()) { throw new OperatorException(_result.getErrorMessage()); } return _output.results(); }
@Test public void test04() { MetaClass metadata = raptorMetaService.getMetaClass("ServiceInstance"); raptorContext.setMongoDataSource(ds); DBCollection coll = raptorContext.getDBCollection(metadata); DBObject firstOp = new BasicDBObject(); firstOp.put("_id", "$_i"); DBObject groupDbo = new BasicDBObject(); groupDbo.put("$group", firstOp); AggregationOutput output = coll.aggregate(groupDbo); Assert.assertTrue(output.getCommandResult().ok()); }
@Test public void test04() { MetaClass metadata = raptorMetaService.getMetaClass("ServiceInstance"); raptorContext.setMongoDataSource(ds); DBCollection coll = raptorContext.getDBCollection(metadata); DBObject firstOp = new BasicDBObject(); firstOp.put("_id", "$_i"); DBObject groupDbo = new BasicDBObject(); groupDbo.put("$group", firstOp); AggregationOutput output = coll.aggregate(groupDbo); Assert.assertTrue(output.getCommandResult().ok()); }
@Test public void test04() { MetaClass metadata = raptorMetaService.getMetaClass("ServiceInstance"); raptorContext.setMongoDataSource(ds); DBCollection coll = raptorContext.getDBCollection(metadata); DBObject firstOp = new BasicDBObject(); firstOp.put("_id", "$_i"); DBObject groupDbo = new BasicDBObject(); groupDbo.put("$group", firstOp); AggregationOutput output = coll.aggregate(groupDbo); Assert.assertTrue(output.getCommandResult().ok()); }