Skip to content

Commit b852810

Browse files
aherlihybehackett
authored andcommitted
PYTHON-978 - Use getMore command for CommandCursor
1 parent 2942d50 commit b852810

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

pymongo/command_cursor.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,17 @@ def __send_message(self, operation):
106106

107107
cmd_duration = response.duration
108108
rqst_id = response.request_id
109+
from_command = response.from_command
110+
109111
if publish:
110112
start = datetime.datetime.now()
111113
try:
112114
doc = helpers._unpack_response(response.data,
113115
self.__id,
114116
self.__collection.codec_options)
115-
# TODO: check_command_response when getMore works with agg cursor
117+
if from_command:
118+
helpers._check_command_response(doc['data'][0])
119+
116120
except OperationFailure as exc:
117121
self.__killed = True
118122

@@ -142,22 +146,30 @@ def __send_message(self, operation):
142146
self.__address)
143147
raise
144148

149+
if from_command:
150+
cursor = doc['data'][0]['cursor']
151+
documents = cursor['nextBatch']
152+
self.__id = cursor['id']
153+
self.__retrieved += len(documents)
154+
else:
155+
documents = doc["data"]
156+
self.__id = doc["cursor_id"]
157+
self.__retrieved += doc["number_returned"]
158+
145159
if publish:
146160
duration = (datetime.datetime.now() - start) + cmd_duration
147161
# Must publish in getMore command response format.
148-
res = {"cursor": {"id": doc["cursor_id"],
162+
res = {"cursor": {"id": self.__id,
149163
"ns": self.__collection.full_name,
150-
"nextBatch": doc["data"]},
164+
"nextBatch": documents},
151165
"ok": 1}
152166
listeners.publish_command_success(
153167
duration, res, "getMore", rqst_id, self.__address)
154168

155-
self.__id = doc["cursor_id"]
156169
if self.__id == 0:
157170
self.__killed = True
171+
self.__data = deque(documents)
158172

159-
self.__retrieved += doc["number_returned"]
160-
self.__data = deque(doc["data"])
161173

162174
def _refresh(self):
163175
"""Refreshes the cursor with more data from the server.
@@ -175,8 +187,7 @@ def _refresh(self):
175187
self.__collection.name,
176188
self.__batch_size,
177189
self.__id,
178-
self.__collection.codec_options,
179-
cmd_cursor=True))
190+
self.__collection.codec_options))
180191

181192
else: # Cursor id is zero nothing else to return
182193
self.__killed = True

pymongo/message.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,21 +275,18 @@ class _GetMore(object):
275275
"""A getmore operation."""
276276

277277
__slots__ = ('db', 'coll', 'ntoreturn', 'cursor_id', 'max_time_ms',
278-
'codec_options', 'cmd_cursor')
278+
'codec_options')
279279

280280
name = 'getMore'
281281

282282
def __init__(self, db, coll, ntoreturn, cursor_id, codec_options,
283-
max_time_ms=None, cmd_cursor=False):
283+
max_time_ms=None):
284284
self.db = db
285285
self.coll = coll
286286
self.ntoreturn = ntoreturn
287287
self.cursor_id = cursor_id
288288
self.codec_options = codec_options
289289
self.max_time_ms = max_time_ms
290-
# XXX: Temporarily keep track of if this getMore is for a command cursor
291-
# so we can use OP_KILLCURSORS until find support for mongos is completed.
292-
self.cmd_cursor = cmd_cursor
293290

294291
def as_command(self):
295292
"""Return a getMore command document for this query."""
@@ -301,7 +298,7 @@ def get_message(self, dummy0, dummy1, use_cmd=False):
301298

302299
ns = _UJOIN % (self.db, self.coll)
303300

304-
if use_cmd and not self.cmd_cursor:
301+
if use_cmd:
305302
ns = _UJOIN % (self.db, "$cmd")
306303
spec = self.as_command()[0]
307304

0 commit comments

Comments
 (0)