Exemplo n.º 1
0
    def __new__(cls, value):
        """
        Create new instance of Tarantool field (single tuple element)
        """
        # Since parent class is immutable, we should
        # override __new__, not __init__
        if isinstance(value, unicode):
            return super(field, cls).__new__(cls, value.encode("utf-8", "replace"))

        if sys.version_info.major < 3 and isinstance(value, str):
            return super(field, cls).__new__(cls, value)

        if isinstance(value, (bytearray, bytes)):
            return super(field, cls).__new__(cls, value)

        if isinstance(value, (int, long)):
            if 0 <= value <= 0xFFFFFFFF:
                # 32 bit integer
                return super(field, cls).__new__(cls, struct_L.pack(value))
            elif 0xFFFFFFFF < value <= 0xFFFFFFFFFFFFFFFF:
                # 64 bit integer
                return super(field, cls).__new__(cls, struct_Q.pack(value))
            else:
                raise ValueError("Integer argument out of range")

        # NOTE: It is posible to implement float
        raise TypeError("Unsupported argument type %s" % (type(value).__name__))
Exemplo n.º 2
0
    def __init__(self, proc_name, args, return_tuple):
        flags = 1 if return_tuple else 0
        assert isinstance(args, (list, tuple))
        # args explicitly casted to string due tarantool bug
        request_body = \
            struct_L.pack(flags) + \
            self.pack_field(proc_name) +\
            self.pack_tuple(map(str, args))

        self._bytes = self.header(len(request_body)) + request_body
Exemplo n.º 3
0
    def __init__(self, conn, proc_name, args, return_tuple):    # pylint: disable=W0231
        super(RequestCall, self).__init__(conn)
        flags = 1 if return_tuple else 0
        assert isinstance(args, (list, tuple))

        request_body = \
            struct_L.pack(flags) + \
            self.pack_field(proc_name) +\
            self.pack_tuple([k for k in args])

        self._bytes = self.header(len(request_body)) + request_body
Exemplo n.º 4
0
    def __init__(self, conn, space_name, key, op_list, return_tuple):    # pylint: disable=W0231
        super(RequestUpdate, self).__init__(conn)
        flags = 1 if return_tuple else 0
        assert isinstance(key, (int, basestring))

        space_no = self.conn.schema.space_no(space_name)
        request_body = \
            struct_LL.pack(space_no, flags) + \
            self.pack_key_tuple((key,), space_no, 0) + \
            struct_L.pack(len(op_list)) +\
            self.pack_operations(op_list)

        self._bytes = self.header(len(request_body)) + request_body
Exemplo n.º 5
0
    def pack_tuple(cls, values):
        """
        Pack tuple of values
        <tuple> ::= <cardinality><field>+

        :param value: tuple to be packed
        :type value: tuple of scalar values (bytes, str or int)

        :return: packed tuple
        :rtype: bytes
        """
        assert isinstance(values, (tuple, list))
        cardinality = struct_L.pack(len(values))
        packed_items = [cls.pack_field(v) for v in values]
        packed_items.insert(0, cardinality)
        return b''.join(packed_items)
Exemplo n.º 6
0
    def __init__(self, space_no, key, op_list, return_tuple):
        flags = 1 if return_tuple else 0

        if not isinstance(key, (int, bytes, basestring, tuple)):
            raise TypeError('Unsuppoted key type. Key must be instance '
                            'of int, bytes, str or tuple.')

        if isinstance(key, (int, bytes, basestring)):
            key = (key, )

        request_body = \
            struct_LL.pack(space_no, flags) + \
            self.pack_tuple(key) + \
            struct_L.pack(len(op_list)) +\
            self.pack_operations(op_list)

        self._bytes = self.header(len(request_body)) + request_body
Exemplo n.º 7
0
    def pack_fields(self, packed_values):
        '''\
        Pack tuple of values
        <tuple> ::= <cardinality><field>+

        :param value: tuple to be packed
        :type value: tuple of scalar values (bytes, str or int)

        :return: packed tuple
        :rtype: bytes
        '''
        assert isinstance(packed_values, (tuple, list))
        cardinality = struct_L.pack(len(packed_values))
        packed_items = []
        packed_items.append(cardinality)
        for value in packed_values:
            packed_items.append(self.pack_field(value))
        return b"".join(packed_items)