(self, operation)
Send a getmore message and handle the response.
Send a getmore message and handle the response.
Send a getmore message and handle the response .
def __send_message(self, operation):
        """Send a getmore message and handle the response.
        """
        client = self.__collection.database.client
        listeners = client._event_listeners
        publish = listeners.enabled_for_commands
        try:
            response = client._send_message_with_response(
                operation, address=self.__address)
        except AutoReconnect:
            # Don't try to send kill cursors on another socket
            # or to another server. It can cause a _pinValue
            # assertion on some server releases if we get here
            # due to a socket timeout.
            self.__killed = True
            raise

        cmd_duration = response.duration
        rqst_id = response.request_id
        from_command = response.from_command

        if publish:
            start = datetime.datetime.now()
        try:
            doc = helpers._unpack_response(response.data,
                                           self.__id,
                                           self.__collection.codec_options)
            if from_command:
                helpers._check_command_response(doc['data'][0])

        except OperationFailure as exc:
            self.__killed = True

            if publish:
                duration = (datetime.datetime.now() - start) + cmd_duration
                listeners.publish_command_failure(
                    duration, exc.details, "getMore", rqst_id, self.__address)

            raise
        except NotMasterError as exc:
            # Don't send kill cursors to another server after a "not master"
            # error. It's completely pointless.
            self.__killed = True

            if publish:
                duration = (datetime.datetime.now() - start) + cmd_duration
                listeners.publish_command_failure(
                    duration, exc.details, "getMore", rqst_id, self.__address)

            client._reset_server_and_request_check(self.address)
            raise
        except Exception as exc:
            if publish:
                duration = (datetime.datetime.now() - start) + cmd_duration
                listeners.publish_command_failure(
                    duration, _convert_exception(exc), "getMore", rqst_id,
                    self.__address)
            raise

        if from_command:
            cursor = doc['data'][0]['cursor']
            documents = cursor['nextBatch']
            self.__id = cursor['id']
            self.__retrieved += len(documents)
        else:
            documents = doc["data"]
            self.__id = doc["cursor_id"]
            self.__retrieved += doc["number_returned"]

        if publish:
            duration = (datetime.datetime.now() - start) + cmd_duration
            # Must publish in getMore command response format.
            res = {"cursor": {"id": self.__id,
                              "ns": self.__collection.full_name,
                              "nextBatch": documents},
                   "ok": 1}
            listeners.publish_command_success(
                duration, res, "getMore", rqst_id, self.__address)

        if self.__id == 0:
            self.__killed = True
        self.__data = deque(documents)
def __send_message ( self , operation ) : client = self . __collection . database . client listeners = client . _event_listeners publish = listeners . enabled_for_commands try : response = client . _send_message_with_response ( operation , address = self . __address ) except AutoReconnect : # Don't try to send kill cursors on another socket # or to another server. It can cause a _pinValue # assertion on some server releases if we get here # due to a socket timeout. self . __killed = True raise cmd_duration = response . duration rqst_id = response . request_id from_command = response . from_command if publish : start = datetime . datetime . now ( ) try : doc = helpers . _unpack_response ( response . data , self . __id , self . __collection . codec_options ) if from_command : helpers . _check_command_response ( doc [ 'data' ] [ 0 ] ) except OperationFailure as exc : self . __killed = True if publish : duration = ( datetime . datetime . now ( ) - start ) + cmd_duration listeners . publish_command_failure ( duration , exc . details , "getMore" , rqst_id , self . __address ) raise except NotMasterError as exc : # Don't send kill cursors to another server after a "not master" # error. It's completely pointless. self . __killed = True if publish : duration = ( datetime . datetime . now ( ) - start ) + cmd_duration listeners . publish_command_failure ( duration , exc . details , "getMore" , rqst_id , self . __address ) client . _reset_server_and_request_check ( self . address ) raise except Exception as exc : if publish : duration = ( datetime . datetime . now ( ) - start ) + cmd_duration listeners . publish_command_failure ( duration , _convert_exception ( exc ) , "getMore" , rqst_id , self . __address ) raise if from_command : cursor = doc [ 'data' ] [ 0 ] [ 'cursor' ] documents = cursor [ 'nextBatch' ] self . __id = cursor [ 'id' ] self . __retrieved += len ( documents ) else : documents = doc [ "data" ] self . __id = doc [ "cursor_id" ] self . __retrieved += doc [ "number_returned" ] if publish : duration = ( datetime . datetime . now ( ) - start ) + cmd_duration # Must publish in getMore command response format. res = { "cursor" : { "id" : self . __id , "ns" : self . __collection . full_name , "nextBatch" : documents } , "ok" : 1 } listeners . publish_command_success ( duration , res , "getMore" , rqst_id , self . __address ) if self . __id == 0 : self . __killed = True self . __data = deque ( documents )