Skip to content

Commit 57966bb

Browse files
committed
JAVA-1063: Added missing command method, command(String, ReadPreference), and corresponding test; renamed readPref to readPreference; improved Javadoc on command methods;
1 parent da14fee commit 57966bb

2 files changed

Lines changed: 90 additions & 41 deletions

File tree

src/main/com/mongodb/DB.java

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ public DBCollection getCollectionFromString( String s ){
238238
* @return result of the command execution
239239
* @throws MongoException
240240
* @dochub commands
241+
* @mongodb.driver.manual tutorial/use-database-commands Commands
241242
*/
242243
public CommandResult command( DBObject cmd ){
243244
return command( cmd, 0 );
@@ -253,6 +254,7 @@ public CommandResult command( DBObject cmd ){
253254
* @return result of the command execution
254255
* @throws MongoException
255256
* @dochub commands
257+
* @mongodb.driver.manual tutorial/use-database-commands Commands
256258
*/
257259
public CommandResult command( DBObject cmd, DBEncoder encoder ){
258260
return command( cmd, 0, encoder );
@@ -270,8 +272,8 @@ public CommandResult command( DBObject cmd, DBEncoder encoder ){
270272
* @return result of the command execution
271273
* @throws MongoException
272274
* @dochub commands
273-
* @mongodb.driver.manual reference/method/db.runCommand/ runCommand
274-
* @deprecated Use {@link com.mongodb.DB#command(DBObject, ReadPreference, DBEncoder)} instead
275+
* @mongodb.driver.manual tutorial/use-database-commands Commands
276+
* @deprecated Use {@link com.mongodb.DB#command(DBObject, ReadPreference, DBEncoder)} instead. This method will be removed in 3.0.
275277
*/
276278
@Deprecated
277279
public CommandResult command( DBObject cmd , int options, DBEncoder encoder ){
@@ -284,37 +286,37 @@ public CommandResult command( DBObject cmd , int options, DBEncoder encoder ){
284286
* option used by this method was "slave ok", therefore this method has been replaced
285287
* with {@link com.mongodb.DB#command(DBObject, ReadPreference)}.
286288
*
287-
* @param cmd {@code DBObject} representation the command to be executed
288-
* @param options query options to use
289-
* @param readPrefs {@link ReadPreference} for this command (nodes selection is the biggest part of this)
289+
* @param cmd A {@code DBObject} representation the command to be executed
290+
* @param options The query options to use
291+
* @param readPreference The {@link ReadPreference} for this command (nodes selection is the biggest part of this)
290292
* @return result of the command execution
291293
* @throws MongoException
292294
* @dochub commands
293-
* @mongodb.driver.manual reference/method/db.runCommand/ runCommand
294-
* @deprecated Use {@link com.mongodb.DB#command(DBObject, ReadPreference)} instead
295+
* @mongodb.driver.manual tutorial/use-database-commands Commands
296+
* @deprecated Use {@link com.mongodb.DB#command(DBObject, ReadPreference)} instead. This method will be removed in 3.0.
295297
*/
296298
@Deprecated
297-
public CommandResult command( DBObject cmd , int options, ReadPreference readPrefs ){
298-
return command(cmd, options, readPrefs, DefaultDBEncoder.FACTORY.create());
299+
public CommandResult command( DBObject cmd , int options, ReadPreference readPreference ){
300+
return command(cmd, options, readPreference, DefaultDBEncoder.FACTORY.create());
299301
}
300302

301303
/**
302-
* Executes a database command. The only option used by this method was "slave ok", therefore this method
303-
* has been replaced with {@link com.mongodb.DB#command(DBObject, ReadPreference, DBEncoder)}.
304+
* Executes a database command. The only option used by this method was "slave ok", therefore this method has been replaced with {@link
305+
* com.mongodb.DB#command(DBObject, ReadPreference, DBEncoder)}.
304306
*
305-
* @param cmd {@code DBObject} representation the command to be executed
306-
* @param options query options to use
307-
* @param readPrefs {@link ReadPreference} for this command (nodes selection is the biggest part of this)
308-
* @param encoder {@link DBEncoder} to be used for command encoding
307+
* @param cmd A {@code DBObject} representation the command to be executed
308+
* @param options The query options to use
309+
* @param readPreference The {@link ReadPreference} for this command (nodes selection is the biggest part of this)
310+
* @param encoder A {@link DBEncoder} to be used for command encoding
309311
* @return result of the command execution
310312
* @throws MongoException
311313
* @dochub commands
312-
* @mongodb.driver.manual reference/method/db.runCommand/ runCommand
313-
* @deprecated Use {@link com.mongodb.DB#command(DBObject, ReadPreference, DBEncoder)} instead
314+
* @mongodb.driver.manual tutorial/use-database-commands Commands
315+
* @deprecated Use {@link com.mongodb.DB#command(DBObject, ReadPreference, DBEncoder)} instead. This method will be removed in 3.0.
314316
*/
315317
@Deprecated
316-
public CommandResult command( DBObject cmd , int options, ReadPreference readPrefs, DBEncoder encoder ){
317-
ReadPreference effectiveReadPrefs = getCommandReadPreference(cmd, readPrefs);
318+
public CommandResult command( DBObject cmd , int options, ReadPreference readPreference, DBEncoder encoder ){
319+
ReadPreference effectiveReadPrefs = getCommandReadPreference(cmd, readPreference);
318320
cmd = wrapCommand(cmd, effectiveReadPrefs);
319321

320322
Iterator<DBObject> i =
@@ -333,15 +335,15 @@ public CommandResult command( DBObject cmd , int options, ReadPreference readPre
333335
/**
334336
* Executes a database command with the selected readPreference, and encodes the command using the given encoder.
335337
*
336-
* @param cmd The {@code DBObject} representation the command to be executed
337-
* @param readPrefs Where to execute the command - this will only be applied for a subset of commands
338-
* @param encoder The DBEncoder that knows how to serialise the cmd
339-
* @return The result of executing the command, success or failure
340-
* @mongodb.driver.manual reference/method/db.runCommand/ runCommand
338+
* @param cmd The {@code DBObject} representation the command to be executed
339+
* @param readPreference Where to execute the command - this will only be applied for a subset of commands
340+
* @param encoder The DBEncoder that knows how to serialise the cmd
341+
* @return The result of executing the command, success or failure
342+
* @mongodb.driver.manual tutorial/use-database-commands Commands
341343
* @since 2.12
342344
*/
343-
public CommandResult command( final DBObject cmd , final ReadPreference readPrefs, final DBEncoder encoder ){
344-
return command(cmd, 0, readPrefs, encoder);
345+
public CommandResult command( final DBObject cmd , final ReadPreference readPreference, final DBEncoder encoder ){
346+
return command(cmd, 0, readPreference, encoder);
345347
}
346348

347349
// Only append $readPreference meta-operator if connected to a mongos, read preference is not primary
@@ -350,12 +352,12 @@ public CommandResult command( final DBObject cmd , final ReadPreference readPref
350352
// the encoder is not capable of encoding a BasicDBObject
351353
// Due to issues with compatibility between different versions of mongos, also wrap the command in a
352354
// $query field, so that the $readPreference is not rejected
353-
private DBObject wrapCommand(DBObject cmd, final ReadPreference readPrefs) {
355+
private DBObject wrapCommand(DBObject cmd, final ReadPreference readPreference) {
354356
if (getMongo().isMongosConnection() &&
355-
!(ReadPreference.primary().equals(readPrefs) || ReadPreference.secondaryPreferred().equals(readPrefs)) &&
357+
!(ReadPreference.primary().equals(readPreference) || ReadPreference.secondaryPreferred().equals(readPreference)) &&
356358
cmd instanceof BasicDBObject) {
357359
cmd = new BasicDBObject("$query", cmd)
358-
.append(QueryOpBuilder.READ_PREFERENCE_META_OPERATOR, readPrefs.toDBObject());
360+
.append(QueryOpBuilder.READ_PREFERENCE_META_OPERATOR, readPreference.toDBObject());
359361
}
360362
return cmd;
361363
}
@@ -369,9 +371,10 @@ private DBObject wrapCommand(DBObject cmd, final ReadPreference readPrefs) {
369371
* @return The result of the command execution
370372
* @throws MongoException
371373
* @dochub commands
372-
* @mongodb.driver.manual reference/method/db.runCommand/ runCommand
373-
* @deprecated Use {@link com.mongodb.DB#command(DBObject, ReadPreference)} instead
374+
* @mongodb.driver.manual tutorial/use-database-commands Commands
375+
* @deprecated Use {@link com.mongodb.DB#command(DBObject, ReadPreference)} instead. This method will be removed in 3.0.
374376
*/
377+
@Deprecated
375378
public CommandResult command(DBObject cmd, int options) {
376379
return command(cmd, options, getReadPreference());
377380
}
@@ -381,42 +384,61 @@ public CommandResult command(DBObject cmd, int options) {
381384
* preference, use this instead of {@link DB#command(com.mongodb.DBObject, int) }
382385
*
383386
* @param cmd The {@code DBObject} representation the command to be executed
384-
* @param readPreference Where to execute the command
385-
* @return The result of executing the command, success or failure
386-
* @mongodb.driver.manual reference/method/db.runCommand/ runCommand
387+
* @param readPreference Where to execute the command - this will only be applied for a subset of commands
388+
* @return The result of executing the command, success or failure
389+
* @mongodb.driver.manual tutorial/use-database-commands Commands
387390
* @since 2.12
388391
*/
389392
public CommandResult command(final DBObject cmd, final ReadPreference readPreference) {
390393
return command(cmd, 0, readPreference);
391394
}
392395

393396
/**
394-
* Executes a database command.
395-
* This method constructs a simple dbobject and calls {@link DB#command(com.mongodb.DBObject) }
397+
* Executes a database command. This method constructs a simple dbobject and calls {@link DB#command(com.mongodb.DBObject) }
396398
*
397399
* @param cmd name of the command to be executed
398400
* @return result of the command execution
399401
* @throws MongoException
400402
* @dochub commands
403+
* @mongodb.driver.manual tutorial/use-database-commands Commands
401404
*/
402405
public CommandResult command( String cmd ){
403406
return command( new BasicDBObject( cmd , Boolean.TRUE ) );
404407
}
405408

406409
/**
407-
* Executes a database command.
408-
* This method constructs a simple dbobject and calls {@link DB#command(com.mongodb.DBObject, int) }
410+
* Executes a database command. This method constructs a simple dbobject and calls {@link DB#command(com.mongodb.DBObject, int) }
409411
*
410412
* @param cmd name of the command to be executed
411413
* @param options query options to use
412414
* @return result of the command execution
413415
* @throws MongoException
414416
* @dochub commands
417+
* @mongodb.driver.manual tutorial/use-database-commands Commands
418+
* @deprecated Use {@link com.mongodb.DB#command(String, ReadPreference)} instead. This method will be removed in 3.0.
415419
*/
420+
@Deprecated
416421
public CommandResult command( String cmd, int options ){
417422
return command( new BasicDBObject( cmd , Boolean.TRUE ), options );
418423
}
419424

425+
/**
426+
* Executes a database command. This method constructs a simple dbobject and calls {@link DB#command(com.mongodb.DBObject, int,
427+
* com.mongodb.ReadPreference) }. The only option used by this method was "slave ok", therefore this method has been replaced with
428+
* {@link com.mongodb.DB#command(DBObject, ReadPreference)}.
429+
*
430+
* @param cmd The name of the command to be executed
431+
* @param readPreference Where to execute the command - this will only be applied for a subset of commands
432+
* @return The result of the command execution
433+
* @throws MongoException
434+
* @dochub commands
435+
* @mongodb.driver.manual tutorial/use-database-commands Commands
436+
* @since 2.12
437+
*/
438+
public CommandResult command(final String cmd, final ReadPreference readPreference) {
439+
return command(new BasicDBObject(cmd, Boolean.TRUE), 0, readPreference);
440+
}
441+
420442
/**
421443
* Evaluates JavaScript functions on the database server.
422444
* This is useful if you need to touch a lot of data lightly, in which case network transfer could be a bottleneck.

src/test/com/mongodb/DBTest.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ public void shouldNotThrowAnErrorWhenEnsureConnectionCalledAfterRequestStart() t
112112

113113
Mongo m = new MongoClient(Arrays.asList(new ServerAddress("localhost")));
114114

115-
if (isStandalone(m)) {
116-
return;
117-
}
115+
assumeFalse(isStandalone(cleanupMongo));
118116
try {
119117
DB db = m.getDB("com_mongodb_unittest_DBTest");
120118
db.requestStart();
@@ -223,6 +221,35 @@ public void shouldRunCommandAgainstSecondaryWhenOnlySecondaryReadPreferenceSpeci
223221
assertThat((String) commandResult.get("serverUsed"), not(containsString(":27017")));
224222
}
225223

224+
@Test
225+
@SuppressWarnings("deprecation") // this functionality needs testing, but the tests will be removed/replaced in 3.0
226+
public void shouldRunStringCommandAgainstSecondaryWhenSlaveOkOptionSpecified() throws UnknownHostException {
227+
// Given
228+
assumeTrue(isReplicaSet(cleanupMongo));
229+
DB database = getReplicaSetDB();
230+
231+
// When
232+
CommandResult commandResult = database.command("dbstats", Bytes.QUERYOPTION_SLAVEOK);
233+
234+
// Then
235+
assertThat(commandResult.ok(), is(true));
236+
assertThat((String) commandResult.get("serverUsed"), not(containsString(":27017")));
237+
}
238+
239+
@Test
240+
public void shouldRunStringCommandAgainstSecondaryWhenSecondaryReadPreferenceSpecified() throws UnknownHostException {
241+
// Given
242+
assumeTrue(isReplicaSet(cleanupMongo));
243+
DB database = getReplicaSetDB();
244+
245+
// When
246+
CommandResult commandResult = database.command("dbstats", secondary());
247+
248+
// Then
249+
assertThat(commandResult.ok(), is(true));
250+
assertThat((String) commandResult.get("serverUsed"), not(containsString(":27017")));
251+
}
252+
226253
@Test
227254
public void shouldRunCommandAgainstSecondaryWhenOnlySecondaryReadPreferenceSpecifiedAlongWithEncoder() throws UnknownHostException {
228255
// Given

0 commit comments

Comments
 (0)