Skip to content

Commit b7a878e

Browse files
committed
[JAVA-273: many db commands (stats, list db, group, etc) should succeed if slaveOk=true and at least one server is up]
1 parent bda4c06 commit b7a878e

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

src/main/com/mongodb/DB.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,21 @@ public final DBCollection getCollection( String name ){
8080
/**
8181
* Creates a collection with a given name and options.
8282
* If the collection does not exist, a new collection is created.
83+
* Note that if the options parameter is null, the creation will be deferred to when the collection is written to.
8384
* Possible options:
8485
* <dl>
8586
* <dt>capped</dt><dd><i>boolean</i>: if the collection is capped</dd>
8687
* <dt>size</dt><dd><i>int</i>: collection size (in bytes)</dd>
8788
* <dt>max</dt><dd><i>int</i>: max number of documents</dd>
8889
* </dl>
8990
* @param name the name of the collection to return
90-
* @param o options
91+
* @param options options
9192
* @return the collection
9293
*/
93-
public final DBCollection createCollection( String name, DBObject o ){
94-
if ( o != null ){
94+
public final DBCollection createCollection( String name, DBObject options ){
95+
if ( options != null ){
9596
DBObject createCmd = new BasicDBObject("create", name);
96-
createCmd.putAll(o);
97+
createCmd.putAll(options);
9798
CommandResult result = command(createCmd);
9899
result.throwOnError();
99100
}
@@ -173,6 +174,20 @@ public CommandResult command( String cmd )
173174
return command( new BasicDBObject( cmd , Boolean.TRUE ) );
174175
}
175176

177+
/**
178+
* Executes a database command.
179+
* This method constructs a simple dbobject and calls {@link DB#command(com.mongodb.DBObject, int) }
180+
* @see <a href="http://mongodb.onconfluence.com/display/DOCS/List+of+Database+Commands">List of Commands</a>
181+
* @param cmd command to execute
182+
* @param options query options to use
183+
* @return result of command from the database
184+
* @throws MongoException
185+
*/
186+
public CommandResult command( String cmd, int options )
187+
throws MongoException {
188+
return command( new BasicDBObject( cmd , Boolean.TRUE ), options );
189+
}
190+
176191
/**
177192
* evaluates a function on the database.
178193
* This is useful if you need to touch a lot of data lightly, in which case network transfer could be a bottleneck.
@@ -495,13 +510,13 @@ static DBObject _authCommand( String nonce , String username , byte[] hash ){
495510
}
496511

497512
private CommandResult _doauth( String username , byte[] hash ){
498-
CommandResult res = command(new BasicDBObject("getnonce", 1));
513+
CommandResult res = command(new BasicDBObject("getnonce", 1), getOptions());
499514
if ( ! res.ok() ){
500515
throw new MongoException(res);
501516
}
502517

503518
DBObject cmd = _authCommand( res.getString( "nonce" ) , username , hash );
504-
return command(cmd);
519+
return command(cmd, getOptions());
505520
}
506521

507522
/**

src/main/com/mongodb/DBCollection.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ public DBObject group( DBObject key , DBObject cond , DBObject initial , String
857857
* @see <a href="http://www.mongodb.org/display/DOCS/Aggregation">http://www.mongodb.org/display/DOCS/Aggregation</a>
858858
*/
859859
public DBObject group( GroupCommand cmd ) {
860-
CommandResult res = _db.command( cmd.toDBObject() );
860+
CommandResult res = _db.command( cmd.toDBObject(), getOptions() );
861861
res.throwOnError();
862862
return (DBObject)res.get( "retval" );
863863
}
@@ -875,7 +875,7 @@ public DBObject group( GroupCommand cmd ) {
875875
public DBObject group( DBObject args )
876876
throws MongoException {
877877
args.put( "ns" , getName() );
878-
CommandResult res = _db.command( new BasicDBObject( "group" , args ) );
878+
CommandResult res = _db.command( new BasicDBObject( "group" , args ), getOptions() );
879879
res.throwOnError();
880880
return (DBObject)res.get( "retval" );
881881
}
@@ -902,7 +902,7 @@ public List distinct( String key , DBObject query ){
902902
.add( "query" , query )
903903
.get();
904904

905-
CommandResult res = _db.command( c );
905+
CommandResult res = _db.command( c, getOptions() );
906906
res.throwOnError();
907907
return (List)(res.get( "values" ));
908908
}
@@ -965,7 +965,12 @@ public MapReduceOutput mapReduce( String map , String reduce , String outputTarg
965965
*/
966966
public MapReduceOutput mapReduce( MapReduceCommand command ) throws MongoException{
967967
DBObject cmd = command.toDBObject();
968-
CommandResult res = _db.command( cmd );
968+
// if type in inline, then query options like slaveOk is fine
969+
CommandResult res = null;
970+
if (command.getOutputType() == MapReduceCommand.OutputType.INLINE)
971+
res = _db.command( cmd, getOptions() );
972+
else
973+
res = _db.command( cmd );
969974
res.throwOnError();
970975
return new MapReduceOutput( this , cmd, res );
971976
}
@@ -1032,7 +1037,7 @@ public void dropIndex( String name )
10321037
* @return
10331038
*/
10341039
public CommandResult getStats() {
1035-
return(getDB().command(new BasicDBObject("collstats", getName())));
1040+
return getDB().command(new BasicDBObject("collstats", getName()), getOptions());
10361041
}
10371042

10381043
/**

src/main/com/mongodb/Mongo.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,16 @@ public DB getDB( String dbname ){
307307
return temp;
308308
return db;
309309
}
310-
310+
311+
/**
312+
* gets a collection of DBs used by the driver since this Mongo instance was created.
313+
* This may include DBs that exist in the client but not yet on the server.
314+
* @return
315+
*/
316+
public Collection<DB> getUsedDatabases(){
317+
return _dbs.values();
318+
}
319+
311320
/**
312321
* gets a list of all database names present on the server
313322
* @return
@@ -320,7 +329,7 @@ public List<String> getDatabaseNames()
320329
cmd.put("listDatabases", 1);
321330

322331

323-
BasicDBObject res = (BasicDBObject)getDB( "admin" ).command(cmd);
332+
BasicDBObject res = (BasicDBObject)getDB( "admin" ).command(cmd, getOptions());
324333

325334
if (res.getInt("ok" , 0 ) != 1 )
326335
throw new MongoException( "error listing databases : " + res );

0 commit comments

Comments
 (0)