Skip to content

Commit da14fee

Browse files
committed
JAVA-1063: Deprecated command methods that take the options flag; removed duplicated tests.
1 parent 9ded78d commit da14fee

2 files changed

Lines changed: 53 additions & 23 deletions

File tree

src/main/com/mongodb/DB.java

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,37 +259,48 @@ public CommandResult command( DBObject cmd, DBEncoder encoder ){
259259
}
260260

261261
/**
262-
* Executes a database command.
263-
* This method calls {@link DB#command(com.mongodb.DBObject, int, com.mongodb.ReadPreference, com.mongodb.DBEncoder) } with a null readPrefs.
262+
* Executes a database command. This method calls
263+
* {@link DB#command(com.mongodb.DBObject, int, com.mongodb.ReadPreference, com.mongodb.DBEncoder) } with the database default read
264+
* preference. The only option used by this method was "slave ok", therefore this method has been replaced with
265+
* {@link com.mongodb.DB#command(DBObject, ReadPreference, DBEncoder)}.
264266
*
265267
* @param cmd {@code DBObject} representation the command to be executed
266268
* @param options query options to use
267269
* @param encoder {@link DBEncoder} to be used for command encoding
268270
* @return result of the command execution
269271
* @throws MongoException
270272
* @dochub commands
273+
* @mongodb.driver.manual reference/method/db.runCommand/ runCommand
274+
* @deprecated Use {@link com.mongodb.DB#command(DBObject, ReadPreference, DBEncoder)} instead
271275
*/
276+
@Deprecated
272277
public CommandResult command( DBObject cmd , int options, DBEncoder encoder ){
273278
return command(cmd, options, getReadPreference(), encoder);
274279
}
275280

276281
/**
277-
* Executes a database command.
278-
* This method calls {@link DB#command(com.mongodb.DBObject, int, com.mongodb.ReadPreference, com.mongodb.DBEncoder) } with a default encoder.
282+
* Executes a database command. This method calls
283+
* {@link DB#command(com.mongodb.DBObject, int, com.mongodb.ReadPreference, com.mongodb.DBEncoder) } with a default encoder. The only
284+
* option used by this method was "slave ok", therefore this method has been replaced
285+
* with {@link com.mongodb.DB#command(DBObject, ReadPreference)}.
279286
*
280287
* @param cmd {@code DBObject} representation the command to be executed
281288
* @param options query options to use
282289
* @param readPrefs {@link ReadPreference} for this command (nodes selection is the biggest part of this)
283290
* @return result of the command execution
284291
* @throws MongoException
285292
* @dochub commands
293+
* @mongodb.driver.manual reference/method/db.runCommand/ runCommand
294+
* @deprecated Use {@link com.mongodb.DB#command(DBObject, ReadPreference)} instead
286295
*/
296+
@Deprecated
287297
public CommandResult command( DBObject cmd , int options, ReadPreference readPrefs ){
288298
return command(cmd, options, readPrefs, DefaultDBEncoder.FACTORY.create());
289299
}
290300

291301
/**
292-
* Executes a database command.
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)}.
293304
*
294305
* @param cmd {@code DBObject} representation the command to be executed
295306
* @param options query options to use
@@ -298,7 +309,10 @@ public CommandResult command( DBObject cmd , int options, ReadPreference readPre
298309
* @return result of the command execution
299310
* @throws MongoException
300311
* @dochub commands
312+
* @mongodb.driver.manual reference/method/db.runCommand/ runCommand
313+
* @deprecated Use {@link com.mongodb.DB#command(DBObject, ReadPreference, DBEncoder)} instead
301314
*/
315+
@Deprecated
302316
public CommandResult command( DBObject cmd , int options, ReadPreference readPrefs, DBEncoder encoder ){
303317
ReadPreference effectiveReadPrefs = getCommandReadPreference(cmd, readPrefs);
304318
cmd = wrapCommand(cmd, effectiveReadPrefs);
@@ -316,6 +330,20 @@ public CommandResult command( DBObject cmd , int options, ReadPreference readPre
316330
return cr;
317331
}
318332

333+
/**
334+
* Executes a database command with the selected readPreference, and encodes the command using the given encoder.
335+
*
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
341+
* @since 2.12
342+
*/
343+
public CommandResult command( final DBObject cmd , final ReadPreference readPrefs, final DBEncoder encoder ){
344+
return command(cmd, 0, readPrefs, encoder);
345+
}
346+
319347
// Only append $readPreference meta-operator if connected to a mongos, read preference is not primary
320348
// or secondary preferred,
321349
// and command is an instance of BasicDBObject. The last condition is unfortunate, but necessary in case
@@ -352,13 +380,13 @@ public CommandResult command(DBObject cmd, int options) {
352380
* Executes the command against the database with the given read preference. This method is the preferred way of setting read
353381
* preference, use this instead of {@link DB#command(com.mongodb.DBObject, int) }
354382
*
355-
* @param cmd{@code DBObject} representation the command to be executed
356-
* @param readPreference where to execute the command
357-
* @return the result of executing the command, success or failure
383+
* @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
358386
* @mongodb.driver.manual reference/method/db.runCommand/ runCommand
359387
* @since 2.12
360388
*/
361-
public CommandResult command(DBObject cmd, ReadPreference readPreference) {
389+
public CommandResult command(final DBObject cmd, final ReadPreference readPreference) {
362390
return command(cmd, 0, readPreference);
363391
}
364392

src/test/com/mongodb/DBTest.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ public void shouldReturnOKWhenASimpleCommandExecutesSuccessfully() {
179179
}
180180

181181
@Test
182+
@SuppressWarnings("deprecation") // this functionality needs testing, but the tests will be removed/replaced in 3.0
182183
public void shouldRunCommandAgainstSecondaryWhenSlaveOkOrReadPreferenceSecondaryOrBothAreBothSet() throws UnknownHostException {
183184
// Given
184185
assumeTrue(isReplicaSet(cleanupMongo));
@@ -222,6 +223,20 @@ public void shouldRunCommandAgainstSecondaryWhenOnlySecondaryReadPreferenceSpeci
222223
assertThat((String) commandResult.get("serverUsed"), not(containsString(":27017")));
223224
}
224225

226+
@Test
227+
public void shouldRunCommandAgainstSecondaryWhenOnlySecondaryReadPreferenceSpecifiedAlongWithEncoder() throws UnknownHostException {
228+
// Given
229+
assumeTrue(isReplicaSet(cleanupMongo));
230+
DB database = getReplicaSetDB();
231+
232+
// When
233+
CommandResult commandResult = database.command(new BasicDBObject("dbstats", 1), secondary(), DefaultDBEncoder.FACTORY.create());
234+
235+
// Then
236+
assertThat(commandResult.ok(), is(true));
237+
assertThat((String) commandResult.get("serverUsed"), not(containsString(":27017")));
238+
}
239+
225240
@Test
226241
public void shouldRunCommandAgainstPrimaryWhenOnlyPrimaryReadPreferenceSpecified() throws UnknownHostException {
227242
// Given
@@ -364,6 +379,7 @@ public void shouldCreateUncappedCollection() {
364379
}
365380

366381
@Test
382+
@SuppressWarnings("deprecation")
367383
public void shouldDoEval() {
368384
assumeFalse(getDatabase().isAuthenticated());
369385
String code = "function(name, incAmount) {\n"
@@ -403,20 +419,6 @@ public void shouldExecuteCommand() {
403419
assertThat(commandResult, hasFields(new String[]{"ismaster", "maxBsonObjectSize", "ok", "serverUsed"}));
404420
}
405421

406-
@Test
407-
public void shouldExecuteCommandWithOptions() {
408-
CommandResult isMaster = getDatabase().command(new BasicDBObject("isMaster", 1), Bytes.QUERYOPTION_SLAVEOK);
409-
System.out.println(isMaster);
410-
411-
getDatabase().command(new BasicDBObject("isMaster", 1), Bytes.QUERYOPTION_SLAVEOK);
412-
}
413-
414-
@Test
415-
public void shouldExecuteCommandWithReadPreference() throws UnknownHostException {
416-
CommandResult commandResult = getReplicaSetDB().command(new BasicDBObject("dbStats", 1).append("scale", 1), 0, secondary());
417-
assertThat(commandResult, hasFields(new String[]{"collections", "avgObjSize", "indexes", "db", "indexSize", "storageSize"}));
418-
}
419-
420422
@Test
421423
public void shouldNotThrowAnExceptionOnCommandFailure() {
422424
CommandResult commandResult = getDatabase().command(new BasicDBObject("collStats", "a" + System.currentTimeMillis()));

0 commit comments

Comments
 (0)