@@ -184,29 +184,38 @@ def _check_command_response(response, msg=None, allowable_errors=None):
184184 raise OperationFailure (msg % errmsg , code , response )
185185
186186
187- def _command (client , namespace , command , read_preference ,
188- codec_options , check = True , allowable_errors = None ):
189- """Internal command helper."""
190-
187+ def _first_batch (client , namespace , query ,
188+ limit , read_preference , codec_options ):
189+ """Simple query helper for retrieving a first (and possibly only) batch."""
191190 query_opts = 0
192191 # XXX: Set slaveOkay flag when read preference mode is anything other
193192 # than primary (0). Make this more clear when we finish refactoring
194193 # read preferences.
195194 if read_preference .mode :
196195 query_opts = 4
197196
198- query = _Query (query_opts , namespace , 0 , - 1 , command , None , codec_options )
197+ query = _Query (
198+ query_opts , namespace , 0 , limit , query , None , codec_options )
199199 response = client ._send_message_with_response (query , read_preference )
200- result = _unpack_response (response .data , None , codec_options )['data' ][0 ]
200+ return _unpack_response (
201+ response .data , None , codec_options ), response .address
202+
203+
204+ def _command (client , namespace , command , read_preference ,
205+ codec_options , check = True , allowable_errors = None ):
206+ """Internal command helper."""
207+ result , addr = _first_batch (
208+ client , namespace , command , - 1 , read_preference , codec_options )
209+ result = result ["data" ][0 ]
201210 if check :
202211 msg = "command %s on namespace %s failed: %%s" % (
203212 repr (command ).replace ("%" , "%%" ), namespace )
204213 try :
205214 _check_command_response (result , msg , allowable_errors )
206215 except NotMasterError :
207- client ._reset_server_and_request_check (response . address )
216+ client ._reset_server_and_request_check (addr )
208217 raise
209- return result , response . address
218+ return result , addr
210219
211220
212221def _check_write_command_response (results ):
0 commit comments