Skip to content

Commit 9e25f38

Browse files
author
Brendan W. McAdams
committed
JAVA-428 - Support new ReadPreference semantics, deprecate SlaveOK
* Like WriteConcern, each read method now takes an optional ReadPreference arg, defaulting to default otherwise
1 parent 12b603c commit 9e25f38

File tree

4 files changed

+146
-11
lines changed

4 files changed

+146
-11
lines changed

src/main/com/mongodb/DB.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ public CommandResult command( DBObject cmd )
156156
public CommandResult command( DBObject cmd , int options )
157157
throws MongoException {
158158

159+
// TODO - Is ReadPreference OK for commands?
159160
Iterator<DBObject> i = getCollection("$cmd").__find(cmd, new BasicDBObject(), 0, -1, 0, options);
160161
if ( i == null || ! i.hasNext() )
161162
return null;
@@ -263,6 +264,7 @@ public Set<String> getCollectionNames()
263264
if (namespaces == null)
264265
throw new RuntimeException("this is impossible");
265266

267+
// TODO - Is ReadPreference OK for collection Names?
266268
Iterator<DBObject> i = namespaces.__find(new BasicDBObject(), null, 0, 0, 0, getOptions());
267269
if (i == null)
268270
return new HashSet<String>();

src/main/com/mongodb/DBApiLayer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public WriteResult remove( DBObject o , com.mongodb.WriteConcern concern )
290290
}
291291

292292
@Override
293-
Iterator<DBObject> __find( DBObject ref , DBObject fields , int numToSkip , int batchSize, int limit , int options )
293+
Iterator<DBObject> __find( DBObject ref , DBObject fields , int numToSkip , int batchSize, int limit , int options, ReadPreference readPref )
294294
throws MongoException {
295295

296296
if ( ref == null )

src/main/com/mongodb/DBCollection.java

Lines changed: 117 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,11 @@ public WriteResult remove( DBObject o )
291291
/**
292292
* Finds objects
293293
*/
294-
abstract Iterator<DBObject> __find( DBObject ref , DBObject fields , int numToSkip , int batchSize , int limit, int options ) throws MongoException ;
294+
Iterator<DBObject> __find( DBObject ref , DBObject fields , int numToSkip , int batchSize , int limit, int options ) throws MongoException {
295+
return __find( ref, fields, numToSkip, batchSize, limit, options, _readPref );
296+
}
297+
298+
abstract Iterator<DBObject> __find( DBObject ref , DBObject fields , int numToSkip , int batchSize , int limit, int options, ReadPreference readPref ) throws MongoException ;
295299

296300
/**
297301
* Calls {@link DBCollection#find(com.mongodb.DBObject, com.mongodb.DBObject, int, int)} and applies the query options
@@ -340,7 +344,20 @@ public final DBCursor find( DBObject query , DBObject fields , int numToSkip , i
340344
*/
341345
public final DBObject findOne( Object obj )
342346
throws MongoException {
343-
return findOne(obj, null);
347+
return findOne(obj, null, _readPref);
348+
}
349+
350+
/**
351+
* Finds an object by its id.
352+
* This compares the passed in value to the _id field of the document
353+
*
354+
* @param obj any valid object
355+
* @return the object, if found, otherwise <code>null</code>
356+
* @throws MongoException
357+
*/
358+
public final DBObject findOne( Object obj , ReadPreference readPref )
359+
throws MongoException {
360+
return findOne(obj, null, readPref);
344361
}
345362

346363
/**
@@ -353,7 +370,20 @@ public final DBObject findOne( Object obj )
353370
* @dochub find
354371
*/
355372
public final DBObject findOne( Object obj, DBObject fields ) {
356-
Iterator<DBObject> iterator = __find(new BasicDBObject("_id", obj), fields, 0, -1, 0, getOptions() );
373+
return findOne( obj, fields, _readPref );
374+
}
375+
376+
/**
377+
* Finds an object by its id.
378+
* This compares the passed in value to the _id field of the document
379+
*
380+
* @param obj any valid object
381+
* @param fields fields to return
382+
* @return the object, if found, otherwise <code>null</code>
383+
* @dochub find
384+
*/
385+
public final DBObject findOne( Object obj, DBObject fields, ReadPreference readPref ) {
386+
Iterator<DBObject> iterator = __find(new BasicDBObject("_id", obj), fields, 0, -1, 0, getOptions(), readPref );
357387
return (iterator != null ? iterator.next() : null);
358388
}
359389

@@ -578,7 +608,18 @@ public void setHintFields( List<DBObject> lst ){
578608
* @dochub find
579609
*/
580610
public final DBCursor find( DBObject ref ){
581-
return new DBCursor( this, ref, null );
611+
return find( ref, _readPref );
612+
}
613+
614+
/**
615+
* Queries for an object in this collection.
616+
* @param ref object for which to search
617+
* @param preference Read Preference for this write
618+
* @return an iterator over the results
619+
* @dochub find
620+
*/
621+
public final DBCursor find( DBObject ref, ReadPreference preference ){
622+
return new DBCursor( this, ref, null, preference );
582623
}
583624

584625
/**
@@ -605,7 +646,34 @@ public final DBCursor find( DBObject ref ){
605646
* @dochub find
606647
*/
607648
public final DBCursor find( DBObject ref , DBObject keys ){
608-
return new DBCursor( this, ref, keys );
649+
return find( ref, keys, _readPref );
650+
}
651+
652+
/**
653+
* Queries for an object in this collection.
654+
*
655+
* <p>
656+
* An empty DBObject will match every document in the collection.
657+
* Regardless of fields specified, the _id fields are always returned.
658+
* </p>
659+
* <p>
660+
* An example that returns the "x" and "_id" fields for every document
661+
* in the collection that has an "x" field:
662+
* </p>
663+
* <blockquote><pre>
664+
* BasicDBObject keys = new BasicDBObject();
665+
* keys.put("x", 1);
666+
*
667+
* DBCursor cursor = collection.find(new BasicDBObject(), keys);
668+
* </pre></blockquote>
669+
*
670+
* @param ref object for which to search
671+
* @param keys fields to return
672+
* @return a cursor to iterate over results
673+
* @dochub find
674+
*/
675+
public final DBCursor find( DBObject ref , DBObject keys, ReadPreference readPref ){
676+
return new DBCursor( this, ref, keys, readPref );
609677
}
610678

611679
/**
@@ -614,7 +682,16 @@ public final DBCursor find( DBObject ref , DBObject keys ){
614682
* @dochub find
615683
*/
616684
public final DBCursor find(){
617-
return new DBCursor( this, new BasicDBObject(), null );
685+
return find( _readPref );
686+
}
687+
688+
/**
689+
* Queries for all objects in this collection.
690+
* @return a cursor which will iterate over every object
691+
* @dochub find
692+
*/
693+
public final DBCursor find( ReadPreference readPref ){
694+
return new DBCursor( this, new BasicDBObject(), null, readPref );
618695
}
619696

620697
/**
@@ -627,6 +704,16 @@ public final DBObject findOne()
627704
return findOne( new BasicDBObject() );
628705
}
629706

707+
/**
708+
* Returns a single object from this collection.
709+
* @return the object found, or <code>null</code> if the collection is empty
710+
* @throws MongoException
711+
*/
712+
public final DBObject findOne( ReadPreference readPref )
713+
throws MongoException {
714+
return findOne( new BasicDBObject(), readPref );
715+
}
716+
630717
/**
631718
* Returns a single object from this collection matching the query.
632719
* @param o the query object
@@ -635,7 +722,18 @@ public final DBObject findOne()
635722
*/
636723
public final DBObject findOne( DBObject o )
637724
throws MongoException {
638-
return findOne(o, null);
725+
return findOne( o, null, _readPref );
726+
}
727+
728+
/**
729+
* Returns a single object from this collection matching the query.
730+
* @param o the query object
731+
* @return the object found, or <code>null</code> if no such object exists
732+
* @throws MongoException
733+
*/
734+
public final DBObject findOne( DBObject o, ReadPreference readPref )
735+
throws MongoException {
736+
return findOne( o, null, readPref );
639737
}
640738

641739
/**
@@ -646,14 +744,25 @@ public final DBObject findOne( DBObject o )
646744
* @dochub find
647745
*/
648746
public final DBObject findOne( DBObject o, DBObject fields ) {
649-
Iterator<DBObject> i = __find( o , fields , 0 , -1 , 0, getOptions() );
747+
return findOne( o, fields, _readPref );
748+
}
749+
/**
750+
* Returns a single object from this collection matching the query.
751+
* @param o the query object
752+
* @param fields fields to return
753+
* @return the object found, or <code>null</code> if no such object exists
754+
* @dochub find
755+
*/
756+
public final DBObject findOne( DBObject o, DBObject fields, ReadPreference readPref ) {
757+
Iterator<DBObject> i = __find( o , fields , 0 , -1 , 0, getOptions(), readPref );
650758
DBObject obj = (i == null ? null : i.next());
651759
if ( obj != null && ( fields != null && fields.keySet().size() > 0 ) ){
652760
obj.markAsPartialObject();
653761
}
654762
return obj;
655763
}
656764

765+
657766
/**
658767
* calls {@link DBCollection#apply(com.mongodb.DBObject, boolean)} with ensureID=true
659768
* @param o <code>DBObject</code> to which to add fields

src/main/com/mongodb/DBCursor.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,16 @@ public class DBCursor implements Iterator<DBObject> , Iterable<DBObject> {
5959
* @param collection collection to use
6060
* @param q query to perform
6161
* @param k keys to return from the query
62+
* @param preference the Read Preference for this query
6263
*/
63-
public DBCursor( DBCollection collection , DBObject q , DBObject k ){
64+
public DBCursor( DBCollection collection , DBObject q , DBObject k, ReadPreference preference ){
6465
_collection = collection;
6566
_query = q == null ? new BasicDBObject() : q;
6667
_keysWanted = k;
6768
if ( _collection != null ){
6869
_options = _collection.getOptions();
6970
}
71+
_readPref = preference;
7072
}
7173

7274
/**
@@ -82,7 +84,7 @@ static enum CursorType { ITERATOR , ARRAY };
8284
* @return the new cursor
8385
*/
8486
public DBCursor copy() {
85-
DBCursor c = new DBCursor(_collection, _query, _keysWanted);
87+
DBCursor c = new DBCursor(_collection, _query, _keysWanted, _readPref);
8688
c._orderBy = _orderBy;
8789
c._hint = _hint;
8890
c._hintDBObj = _hintDBObj;
@@ -685,6 +687,25 @@ public ServerAddress getServerAddress() {
685687
return null;
686688
}
687689

690+
/**
691+
* Sets the read preference for this cursor.
692+
* See the * documentation for {@link ReadPreference}
693+
* for more information.
694+
*
695+
* @param preference Read Preference to use
696+
*/
697+
public void setReadPreference( ReadPreference preference ){
698+
_readPref = preference;
699+
}
700+
701+
/**
702+
* Gets the default read preference
703+
* @return
704+
*/
705+
public ReadPreference getReadPreference(){
706+
return _readPref;
707+
}
708+
688709
@Override
689710
public String toString() {
690711
StringBuilder sb = new StringBuilder();
@@ -704,6 +725,8 @@ public String toString() {
704725
ServerAddress addr = getServerAddress();
705726
if (addr != null)
706727
sb.append(", addr=").append(addr);
728+
729+
sb.append(", readPreference=").append( _readPref.toString() );
707730
return sb.toString();
708731
}
709732

@@ -721,6 +744,7 @@ public String toString() {
721744
private int _skip = 0;
722745
private boolean _snapshot = false;
723746
private int _options = 0;
747+
private ReadPreference _readPref;
724748

725749
private DBObject _specialFields;
726750

0 commit comments

Comments
 (0)