Esempio n. 1
0
    def __check_response_to_last_error(self, response):
        """Check a response to a lastError message for errors.

        `response` is a byte string representing a response to the message.
        If it represents an error response we raise DatabaseError.

        Return the response as a document.
        """
        response = helpers._unpack_response(response)
        assert response["number_returned"] == 1
        error = response["data"][0]

        helpers._check_command_response(error)

        error_msg = error.get("err", "")
        if error_msg is None:
            return error

        details = error
        # mongos returns the error code in an error object
        # for some errors.
        if "errObjects" in error:
            for errobj in error["errObjects"]:
                if errobj["err"] == error_msg:
                    details = errobj
                    break

        if "code" in details:
            if details["code"] in [11000, 11001, 12582]:
                raise IntegrityError(details["err"])
            else:
                raise DatabaseError(details["err"], details["code"])
        else:
            raise DatabaseError(details["err"])
Esempio n. 2
0
    def __check_response_to_last_error(self, response):
        """Check a response to a lastError message for errors.

        `response` is a byte string representing a response to the message.
        If it represents an error response we raise DatabaseError.

        Return the response as a document.
        """
        response = helpers._unpack_response(response)
        assert response["number_returned"] == 1
        error = response["data"][0]

        helpers._check_command_response(error)

        error_msg = error.get("err", "")
        if error_msg is None:
            return error

        details = error
        # mongos returns the error code in an error object
        # for some errors.
        if "errObjects" in error:
            for errobj in error["errObjects"]:
                if errobj["err"] == error_msg:
                    details = errobj
                    break

        if "code" in details:
            if details["code"] in [11000, 11001, 12582]:
                raise IntegrityError(details["err"])
            else:
                raise DatabaseError(details["err"], details["code"])
        else:
            raise DatabaseError(details["err"])
Esempio n. 3
0
    def test_send_test_message_to_mongo(self):
        """[ConnectionTestCase] - Send message to test driver connection"""

        object_id = ObjectId()
        message_test = message.query(0, 'mongotor_test.$cmd', 0, 1,
                                     {'driverOIDTest': object_id})

        self.conn.send_message_with_response(message_test, callback=self.stop)
        response, _ = self.wait()

        response = helpers._unpack_response(response)
        result = response['data'][0]

        result['oid'].should.be(object_id)
        result['ok'].should.be(1.0)
        result['str'].should.be(str(object_id))
Esempio n. 4
0
    def test_send_test_message_to_mongo(self):
        """[ConnectionTestCase] - Send message to test driver connection"""

        object_id = ObjectId()
        message_test = message.query(0, 'mongotor_test.$cmd', 0, 1,
            {'driverOIDTest': object_id})

        self.conn.send_message_with_response(message_test, callback=self.stop)
        response, _ = self.wait()

        response = helpers._unpack_response(response)
        result = response['data'][0]

        result['oid'].should.be(object_id)
        result['ok'].should.be(1.0)
        result['str'].should.be(str(object_id))
Esempio n. 5
0
    def test_send_test_message(self):
        """[DatabaseTestCase] - Send a test message to database"""

        Database.connect(["localhost:27027", "localhost:27028"], dbname="test")

        object_id = ObjectId()
        message_test = message.query(0, "mongotor_test.$cmd", 0, 1, {"driverOIDTest": object_id})

        Database().send_message(message_test, callback=self.stop)
        response, _ = self.wait()
        response = helpers._unpack_response(response)

        result = response["data"][0]

        result["oid"].should.be(object_id)
        result["ok"].should.be(1.0)
        result["str"].should.be(str(object_id))
Esempio n. 6
0
    def test_send_test_message(self):
        """[DatabaseTestCase] - Send a test message to database"""

        Database.init(["localhost:27027", "localhost:27028"], dbname='test')

        object_id = ObjectId()
        message_test = message.query(0, 'mongotor_test.$cmd', 0, 1,
                                     {'driverOIDTest': object_id})

        Database().send_message(message_test, callback=self.stop)
        response, _ = self.wait()
        response = helpers._unpack_response(response)

        result = response['data'][0]

        result['oid'].should.be(object_id)
        result['ok'].should.be(1.0)
        result['str'].should.be(str(object_id))
Esempio n. 7
0
    def test_send_test_message(self):
        """[DatabaseTestCase] - Send a test message to database"""

        Database.init(["localhost:27027", "localhost:27028"], dbname='test')

        object_id = ObjectId()
        message_test = message.query(0, 'mongotor_test.$cmd', 0, 1,
            {'driverOIDTest': object_id})

        Database().send_message(message_test, callback=self.stop)
        response, _ = self.wait()
        response = helpers._unpack_response(response)

        result = response['data'][0]

        result['oid'].should.be(object_id)
        result['ok'].should.be(1.0)
        result['str'].should.be(str(object_id))
Esempio n. 8
0
    def find(self, callback=None):
        message_query = message.query(self._query_options(), self._collection_name,
            self._skip, self._limit, self._query_spec(), self._fields)

        if not self._connection:
            node = yield gen.Task(self._database.get_node, self._read_preference)
            connection = yield gen.Task(node.connection)
        else:
            connection = self._connection

        response, _ = yield gen.Task(connection.send_message_with_response, message_query)
        response = helpers._unpack_response(response)

        # close cursor
        if response and response.get('cursor_id'):
            cursor_id = response['cursor_id']

            connection.send_message(message.kill_cursors([cursor_id]), callback=None)

        if self._limit == -1 and len(response['data']) == 1:
            callback((response['data'][0], None))
        else:
            callback((response['data'], None))
Esempio n. 9
0
    def find(self, callback=None):
        message_query = message.query(self._query_options(), self._collection_name,
            self._skip, self._limit, self._query_spec(), self._fields)

        if not self._connection:
            node = self._database.get_node(self._read_preference)
            connection = yield gen.Task(node.connection)
        else:
            connection = self._connection

        response, _ = yield gen.Task(connection.send_message_with_response, message_query)
        response = helpers._unpack_response(response)

        # close cursor
        if response and response.get('cursor_id'):
            cursor_id = response['cursor_id']

            connection.send_message(message.kill_cursors([cursor_id]), callback=None)

        if self._limit == -1 and len(response['data']) == 1:
            callback((response['data'][0], None))
        else:
            callback((response['data'], None))
Esempio n. 10
0
 def _parse_response(self, response, request_id):
     try:
         response = helpers._unpack_response(response, request_id)
     except Exception, e:
         logger.error('error %s' % e)
         return None, e
Esempio n. 11
0
 def _parse_response(self, response, request_id):
     try:
         response = helpers._unpack_response(response, request_id)
     except Exception, e:
         logger.error('error %s' % e)
         return None, e