Skip to content

Commit 1497db1

Browse files
committed
JAVA-549: Removed unnecessary fake iterator for queries with 0 results. Simplifies the code, and also fixes this bug, because now every DBCursor will have a server address, even one with no results. Note that you still have to call hasNext() or next() at least once to get a non-null server address, since queries are done lazily
1 parent 3590bba commit 1497db1

File tree

3 files changed

+21
-38
lines changed

3 files changed

+21
-38
lines changed

src/main/com/mongodb/DBApiLayer.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,6 @@ Iterator<DBObject> __find( DBObject ref , DBObject fields , int numToSkip , int
312312

313313
Response res = _connector.call( _db , this , query , null , 2, readPref, decoder );
314314

315-
if ( res.size() == 0 )
316-
return null;
317-
318315
if ( res.size() == 1 ){
319316
BSONObject foo = res.get(0);
320317
MongoException e = MongoException.parse( foo );

src/main/com/mongodb/DBCollection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ public DBObject findOne( Object obj )
334334
*/
335335
public DBObject findOne( Object obj, DBObject fields ) {
336336
Iterator<DBObject> iterator = __find( new BasicDBObject("_id", obj), fields, 0, -1, 0, getOptions(), getReadPreference(), getDecoder() );
337-
return (iterator != null ? iterator.next() : null);
337+
return (iterator.hasNext() ? iterator.next() : null);
338338
}
339339

340340
/**
@@ -649,7 +649,7 @@ public DBObject findOne( DBObject o, DBObject fields ) {
649649
*/
650650
public DBObject findOne( DBObject o, DBObject fields, ReadPreference readPref ) {
651651
Iterator<DBObject> i = __find( o , fields , 0 , -1 , 0, getOptions(), readPref, getDecoder() );
652-
DBObject obj = (i == null ? null : i.next());
652+
DBObject obj = (i.hasNext() ? i.next() : null);
653653
if ( obj != null && ( fields != null && fields.keySet().size() > 0 ) ){
654654
obj.markAsPartialObject();
655655
}

src/main/com/mongodb/DBCursor.java

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ public class DBCursor implements Iterator<DBObject> , Iterable<DBObject>, Closea
5959
* @param preference the Read Preference for this query
6060
*/
6161
public DBCursor( DBCollection collection , DBObject q , DBObject k, ReadPreference preference ){
62+
if (collection == null) {
63+
throw new IllegalArgumentException("collection is null");
64+
}
6265
_collection = collection;
6366
_query = q == null ? new BasicDBObject() : q;
6467
_keysWanted = k;
65-
if ( _collection != null ){
66-
_options = _collection.getOptions();
67-
}
68+
_options = _collection.getOptions();
6869
_readPref = preference;
6970
_decoderFact = collection.getDBDecoderFactory();
7071
}
@@ -346,34 +347,26 @@ private void _check()
346347
if ( _it != null )
347348
return;
348349

349-
if ( _collection != null && _query != null ){
350-
351-
_lookForHints();
352-
353-
DBObject foo = _query;
354-
if ( hasSpecialQueryFields() ){
355-
foo = _specialFields == null ? new BasicDBObject() : _specialFields;
350+
_lookForHints();
356351

357-
_addToQueryObject( foo , "query" , _query , true );
358-
_addToQueryObject( foo , "orderby" , _orderBy , false );
359-
if(_hint != null)
360-
_addToQueryObject( foo , "$hint" , _hint );
361-
if(_hintDBObj != null)
362-
_addToQueryObject( foo , "$hint" , _hintDBObj);
352+
DBObject foo = _query;
353+
if (hasSpecialQueryFields()) {
354+
foo = _specialFields == null ? new BasicDBObject() : _specialFields;
363355

364-
if ( _explain )
365-
foo.put( "$explain" , true );
366-
if ( _snapshot )
367-
foo.put( "$snapshot", true );
368-
}
356+
_addToQueryObject(foo, "query", _query, true);
357+
_addToQueryObject(foo, "orderby", _orderBy, false);
358+
if (_hint != null)
359+
_addToQueryObject(foo, "$hint", _hint);
360+
if (_hintDBObj != null)
361+
_addToQueryObject(foo, "$hint", _hintDBObj);
369362

370-
_it = _collection.__find( foo, _keysWanted, _skip, _batchSize, _limit , _options , _readPref , getDecoder());
363+
if (_explain)
364+
foo.put("$explain", true);
365+
if (_snapshot)
366+
foo.put("$snapshot", true);
371367
}
372368

373-
if ( _it == null ){
374-
_it = (new LinkedList<DBObject>()).iterator();
375-
_fake = true;
376-
}
369+
_it = _collection.__find(foo, _keysWanted, _skip, _batchSize, _limit, _options, _readPref, getDecoder());
377370
}
378371

379372
// Only create a new decoder if there is a decoder factory explicitly set on the collection. Otherwise return null
@@ -477,9 +470,6 @@ private DBObject _next()
477470
* @return
478471
*/
479472
public int numGetMores(){
480-
if ( _fake )
481-
return 0;
482-
483473
if ( _it instanceof DBApiLayer.Result )
484474
return ((DBApiLayer.Result)_it).numGetMores();
485475

@@ -491,9 +481,6 @@ public int numGetMores(){
491481
* @return
492482
*/
493483
public List<Integer> getSizes(){
494-
if ( _fake )
495-
return new LinkedList<Integer>();
496-
497484
if ( _it instanceof DBApiLayer.Result )
498485
return ((DBApiLayer.Result)_it).getSizes();
499486

@@ -765,7 +752,6 @@ public String toString() {
765752

766753
// ---- result info ----
767754
private Iterator<DBObject> _it = null;
768-
private boolean _fake = false;
769755

770756
private CursorType _cursorType = null;
771757
private DBObject _cur = null;

0 commit comments

Comments
 (0)