Skip to content

Commit fe26cc0

Browse files
committed
QueryResultIterator now relies on the namespace field in a cursor document instead of getting the namespace via the constructor.
JAVA-1588
1 parent 81ca6bb commit fe26cc0

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

src/main/com/mongodb/DBCollectionImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ QueryResultIterator find(DBObject ref, DBObject fields, int numToSkip, int batch
8383

8484
Response res = db.getConnector().call(_db, this, query, null, 2, readPref, decoder);
8585

86-
return new QueryResultIterator(db, this, res, batchSize, limit, decoder);
86+
return new QueryResultIterator(this.namespace, db.getMongo(), res, batchSize, limit, decoder);
8787
}
8888

8989
public Cursor aggregate(final List<DBObject> pipeline, final AggregationOptions options,
@@ -105,7 +105,7 @@ public Cursor aggregate(final List<DBObject> pipeline, final AggregationOptions
105105
return new DBCursor(collection, new BasicDBObject(), null, ReadPreference.primary());
106106
} else {
107107
Integer batchSize = options.getBatchSize();
108-
return new QueryResultIterator(res, db, this, batchSize == null ? 0 : batchSize, getDecoder(), res.getServerUsed());
108+
return new QueryResultIterator(res, db.getMongo(), batchSize == null ? 0 : batchSize, getDecoder(), res.getServerUsed());
109109
}
110110
}
111111

@@ -119,7 +119,7 @@ public List<Cursor> parallelScan(final ParallelScanOptions options) {
119119

120120
List<Cursor> cursors = new ArrayList<Cursor>();
121121
for (DBObject cursorDocument : (List<DBObject>) res.get("cursors")) {
122-
cursors.add(new QueryResultIterator(cursorDocument, db, this, options.getBatchSize(), getDecoder(), res.getServerUsed()));
122+
cursors.add(new QueryResultIterator(cursorDocument, db.getMongo(), options.getBatchSize(), getDecoder(), res.getServerUsed()));
123123
}
124124

125125
return cursors;

src/main/com/mongodb/QueryResultIterator.java

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030

3131
class 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

Comments
 (0)