3030
3131class QueryResultIterator implements Cursor {
3232
33- private final DBApiLayer _db ;
3433 private final DBDecoder _decoder ;
35- private final DBCollectionImpl _collection ;
3634 private final ServerAddress _host ;
3735 private final int _limit ;
3836
37+ private DBApiLayer _db ;
38+ private DBCollection _collection ;
3939 private long _cursorId ;
4040 private Iterator <DBObject > _cur ;
4141 private int _curSize ;
@@ -52,26 +52,23 @@ class QueryResultIterator implements Cursor {
5252 private boolean batchSizeTrackingDisabled ;
5353
5454 // Constructor to use for normal queries
55- QueryResultIterator (DBApiLayer db , DBCollectionImpl collection , Response res , int batchSize , int limit , DBDecoder decoder ) {
56- this . _db = db ;
57- _collection = collection ;
55+ QueryResultIterator (String namespace , Mongo mongo , Response res , int batchSize , int limit , DBDecoder decoder ) {
56+ _db = ( DBApiLayer ) mongo . getDB ( getDatatabaseNameFromNamespace ( namespace )) ;
57+ _collection = _db . getCollection ( getCollectionNameFromNamespace ( namespace )) ;
5858 _batchSize = batchSize ;
5959 _limit = limit ;
6060 _host = res ._host ;
6161 _decoder = decoder ;
62- initFromQueryResponse (res );
62+ initFromQueryResponse (res , mongo );
6363 }
6464
65- // Constructor to use for aggregate queries
66- QueryResultIterator (DBObject cursorDocument , DBApiLayer db , DBCollectionImpl collection , int batchSize , DBDecoder decoder ,
67- final ServerAddress serverAddress ) {
68- this ._db = db ;
69- _collection = collection ;
65+ // Constructor to use for commands that return cursor documents
66+ QueryResultIterator (DBObject cursorDocument , Mongo mongo , int batchSize , DBDecoder decoder , final ServerAddress serverAddress ) {
7067 _batchSize = batchSize ;
7168 _host = serverAddress ;
7269 _limit = 0 ;
7370 _decoder = decoder ;
74- initFromCursorDocument (cursorDocument );
71+ initFromCursorDocument (cursorDocument , mongo );
7572 }
7673
7774 static int chooseBatchSize (int batchSize , int limit , int fetched ) {
@@ -145,11 +142,11 @@ public boolean hasNext() {
145142 }
146143
147144 private void getMore (){
148- Response res = _db ._connector .call (_collection . getDB () , _collection ,
145+ Response res = _db ._connector .call (_db , _collection ,
149146 OutMessage .getMore (_collection , _cursorId , getGetMoreBatchSize ()),
150147 _host , _decoder );
151148 _numGetMores ++;
152- initFromQueryResponse (res );
149+ initFromQueryResponse (res , _db . getMongo () );
153150 }
154151
155152 private int getGetMoreBatchSize () {
@@ -183,24 +180,35 @@ public void close(){
183180 }
184181 }
185182
186- private void initFromQueryResponse (final Response response ) {
187- init (response ._flags , response .cursor (), response .size (), response .iterator ());
183+ private void initFromQueryResponse (final Response response , final Mongo mongo ) {
184+ init (response ._flags , response .cursor (), response .size (), response .iterator (), mongo );
188185 }
189186
190187 @ SuppressWarnings ("unchecked" )
191- private void initFromCursorDocument (final DBObject cursorDocument ) {
188+ private void initFromCursorDocument (final DBObject cursorDocument , final Mongo mongo ) {
192189 Map cursor = (Map ) cursorDocument .get ("cursor" );
193190 if (cursor != null ) {
194191 long cursorId = (Long ) cursor .get ("id" );
195192 List <DBObject > firstBatch = (List <DBObject >) cursor .get ("firstBatch" );
196- init (0 , cursorId , firstBatch .size (), firstBatch .iterator ());
193+ String namespace = (String ) cursor .get ("ns" );
194+ _db = (DBApiLayer ) mongo .getDB (getDatatabaseNameFromNamespace (namespace ));
195+ _collection = _db .getCollection (getCollectionNameFromNamespace (namespace ));
196+ init (0 , cursorId , firstBatch .size (), firstBatch .iterator (), mongo );
197197 } else {
198198 List <DBObject > result = (List <DBObject >) cursorDocument .get ("result" );
199- init (0 , 0 , result .size (), result .iterator ());
199+ init (0 , 0 , result .size (), result .iterator (), mongo );
200200 }
201201 }
202202
203- private void init (int flags , long cursorId , int size , Iterator <DBObject > iterator ){
203+ private String getCollectionNameFromNamespace (final String namespace ) {
204+ return namespace .substring (namespace .indexOf ('.' ) + 1 );
205+ }
206+
207+ private String getDatatabaseNameFromNamespace (final String namespace ) {
208+ return namespace .substring (0 , namespace .indexOf ('.' ));
209+ }
210+
211+ private void init (int flags , long cursorId , int size , Iterator <DBObject > iterator , Mongo mongo ){
204212 _curSize = size ;
205213 _cur = iterator ;
206214 if (!batchSizeTrackingDisabled ) {
@@ -209,7 +217,7 @@ private void init(int flags, long cursorId, int size, Iterator<DBObject> iterato
209217 _numFetched += size ;
210218
211219 if (_optionalFinalizer == null ) {
212- _optionalFinalizer = createFinalizerIfNeeded (cursorId );
220+ _optionalFinalizer = createFinalizerIfNeeded (cursorId , mongo );
213221 }
214222
215223 setCursorIdOnFinalizer (cursorId );
@@ -271,8 +279,8 @@ boolean hasFinalizer() {
271279 return _optionalFinalizer != null ;
272280 }
273281
274- private OptionalFinalizer createFinalizerIfNeeded (final long cursorId ) {
275- return _collection . getDB (). getMongo () .getMongoOptions ().isCursorFinalizerEnabled () && cursorId != 0 ?
282+ private OptionalFinalizer createFinalizerIfNeeded (final long cursorId , Mongo mongo ) {
283+ return mongo .getMongoOptions ().isCursorFinalizerEnabled () && cursorId != 0 ?
276284 new OptionalFinalizer (_db , _host ) : null ;
277285 }
278286
0 commit comments