Skip to content

Commit

Permalink
Added support for encoding arbtrary string objects (with encode method)
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcoding committed Feb 15, 2017
1 parent bf531db commit 499ba7a
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion asynctnt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .connection import Connection, connect
from .iproto.protocol import Iterator, Response

__version__ = '0.0.8'
__version__ = '0.0.9'
9 changes: 0 additions & 9 deletions asynctnt/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,6 @@ def ping(self, *, timeout=-1):
"""
Ping request coroutine
:param timeout: Request timeout
:rtype: asynctnt.Response
"""
return self._db.ping(timeout=timeout)

Expand All @@ -478,7 +477,6 @@ def call16(self, func_name, args=None, *, timeout=-1):
:param func_name: function name to call
:param args: arguments to pass to the function (list object)
:param timeout: Request timeout
:rtype: asynctnt.Response
"""
return self._db.call16(func_name, args,
timeout=timeout)
Expand All @@ -493,7 +491,6 @@ def call(self, func_name, args=None, *, timeout=-1):
:param func_name: function name to call
:param args: arguments to pass to the function (list object)
:param timeout: Request timeout
:rtype: asynctnt.Response
"""
return self._db.call(func_name, args,
timeout=timeout)
Expand All @@ -506,7 +503,6 @@ def eval(self, expression, args=None, *, timeout=-1):
:param args: arguments to pass to the function, that will
execute your expression (list object)
:param timeout: Request timeout
:rtype: asynctnt.Response
"""
return self._db.eval(expression, args,
timeout=timeout)
Expand All @@ -526,7 +522,6 @@ def select(self, space, key=None, **kwargs):
* asynctnt.Iterator object
* string with an iterator name
:param timeout: Request timeout
:rtype: asynctnt.Response
"""
return self._db.select(space, key, **kwargs)

Expand All @@ -538,7 +533,6 @@ def insert(self, space, t, *, replace=False, timeout=-1):
:param t: tuple to insert (list object)
:param replace: performs replace request instead of insert
:param timeout: Request timeout
:rtype: asynctnt.Response
"""
return self._db.insert(space, t,
replace=replace, timeout=timeout)
Expand All @@ -550,7 +544,6 @@ def replace(self, space, t, *, timeout=-1):
:param space: space id or space name.
:param t: tuple to insert (list object)
:param timeout: Request timeout
:rtype: asynctnt.Response
"""
return self._db.replace(space, t,
timeout=timeout)
Expand All @@ -563,7 +556,6 @@ def delete(self, space, key, **kwargs):
:param key: key to delete
:param index: index id or name
:param timeout: Request timeout
:rtype: asynctnt.Response
"""
return self._db.delete(space, key, **kwargs)

Expand Down Expand Up @@ -592,7 +584,6 @@ def upsert(self, space, t, operations, **kwargs):
Please refer to
https://tarantool.org/doc/book/box/box_space.html?highlight=update#lua-function.space_object.update
:param timeout: Request timeout
:rtype: asynctnt.Response
"""
return self._db.upsert(space, t, operations, **kwargs)

Expand Down
3 changes: 2 additions & 1 deletion asynctnt/iproto/buffer.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ cdef class WriteBuffer:
int _view_count # Number of memoryviews attached to the buffer

bytes _encoding
str _encoding_str

ssize_t __op_offset
ssize_t __sync_offset
ssize_t __schema_id_offset

@staticmethod
cdef WriteBuffer new(bytes encoding=*)
cdef WriteBuffer new(bytes encoding, str encoding_str)

cdef inline _check_readonly(self)
cdef inline len(self)
Expand Down
6 changes: 4 additions & 2 deletions asynctnt/iproto/buffer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ cdef class WriteBuffer:
self._size = _BUFFER_INITIAL_SIZE
self._length = 0
self._encoding = None
self._encoding_str = None
self.__op_offset = -1
self.__sync_offset = -1
self.__schema_id_offset = -1
Expand Down Expand Up @@ -54,10 +55,11 @@ cdef class WriteBuffer:
self._view_count -= 1

@staticmethod
cdef WriteBuffer new(bytes encoding=None):
cdef WriteBuffer new(bytes encoding, str encoding_str):
cdef WriteBuffer buf
buf = WriteBuffer.__new__(WriteBuffer)
buf._encoding = encoding
buf._encoding_str = encoding_str
return buf

cdef inline _check_readonly(self):
Expand Down Expand Up @@ -323,7 +325,7 @@ cdef class WriteBuffer:
return self._encode_bin(p, o_string_str, <uint32_t>o_string_len)

elif isinstance(o, str):
o_string_temp = encode_unicode_string(o, self._encoding)
o_string_temp = o.encode(self._encoding_str, 'strict')
o_string_str = NULL
o_string_len = 0
cpython.bytes.PyBytes_AsStringAndSize(o_string_temp,
Expand Down
1 change: 1 addition & 0 deletions asynctnt/iproto/db.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ cdef class Db:
cdef:
BaseProtocol _protocol
bytes _encoding
str _encoding_str

@staticmethod
cdef inline Db new(BaseProtocol protocol)
Expand Down
22 changes: 12 additions & 10 deletions asynctnt/iproto/db.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ cdef class Db:
def __cinit__(self):
self._protocol = None
self._encoding = None
self._encoding_str = None

@staticmethod
cdef inline Db new(BaseProtocol protocol):
cdef Db db = Db.__new__(Db)
db._protocol = protocol
db._encoding = protocol.encoding
db._encoding_str = decode_string(db._encoding, b'ascii')
return db

cdef inline uint64_t next_sync(self):
Expand Down Expand Up @@ -44,7 +46,7 @@ cdef class Db:
op = tnt.TP_PING
sync = self.next_sync()
schema_id = self._protocol._schema_id
buf = WriteBuffer.new(self._encoding)
buf = WriteBuffer.new(self._encoding, self._encoding_str)
buf.write_header(sync, op, schema_id)
buf.write_length()
return Request.new(op, sync, schema_id, buf)
Expand All @@ -59,7 +61,7 @@ cdef class Db:
op = tnt.TP_CALL_16
sync = self.next_sync()
schema_id = self._protocol._schema_id
buf = WriteBuffer.new(self._encoding)
buf = WriteBuffer.new(self._encoding, self._encoding_str)
buf.write_header(sync, op, schema_id)
buf.encode_request_call(func_name, args)
buf.write_length()
Expand All @@ -75,7 +77,7 @@ cdef class Db:
op = tnt.TP_CALL
sync = self.next_sync()
schema_id = self._protocol._schema_id
buf = WriteBuffer.new(self._encoding)
buf = WriteBuffer.new(self._encoding, self._encoding_str)
buf.write_header(sync, op, schema_id)
buf.encode_request_call(func_name, args)
buf.write_length()
Expand All @@ -91,7 +93,7 @@ cdef class Db:
op = tnt.TP_EVAL
sync = self.next_sync()
schema_id = self._protocol._schema_id
buf = WriteBuffer.new(self._encoding)
buf = WriteBuffer.new(self._encoding, self._encoding_str)
buf.write_header(sync, op, schema_id)
buf.encode_request_eval(expression, args)
buf.write_length()
Expand All @@ -108,7 +110,7 @@ cdef class Db:
op = tnt.TP_SELECT
sync = self.next_sync()
schema_id = self._protocol._schema_id
buf = WriteBuffer.new(self._encoding)
buf = WriteBuffer.new(self._encoding, self._encoding_str)
buf.write_header(sync, op, schema_id)
buf.encode_request_select(space, index, key,
offset, limit, iterator)
Expand All @@ -125,7 +127,7 @@ cdef class Db:
op = tnt.TP_INSERT if not replace else tnt.TP_REPLACE
sync = self.next_sync()
schema_id = self._protocol._schema_id
buf = WriteBuffer.new(self._encoding)
buf = WriteBuffer.new(self._encoding, self._encoding_str)
buf.write_header(sync, op, schema_id)
buf.encode_request_insert(space, t)
buf.write_length()
Expand All @@ -141,7 +143,7 @@ cdef class Db:
op = tnt.TP_DELETE
sync = self.next_sync()
schema_id = self._protocol._schema_id
buf = WriteBuffer.new(self._encoding)
buf = WriteBuffer.new(self._encoding, self._encoding_str)
buf.write_header(sync, op, schema_id)
buf.encode_request_delete(space, index, key)
buf.write_length()
Expand All @@ -158,7 +160,7 @@ cdef class Db:
op = tnt.TP_UPDATE
sync = self.next_sync()
schema_id = self._protocol._schema_id
buf = WriteBuffer.new(self._encoding)
buf = WriteBuffer.new(self._encoding, self._encoding_str)
buf.write_header(sync, op, schema_id)
buf.encode_request_update(space, index, key, operations)
buf.write_length()
Expand All @@ -174,7 +176,7 @@ cdef class Db:
op = tnt.TP_UPSERT
sync = self.next_sync()
schema_id = self._protocol._schema_id
buf = WriteBuffer.new(self._encoding)
buf = WriteBuffer.new(self._encoding, self._encoding_str)
buf.write_header(sync, op, schema_id)
buf.encode_request_upsert(space, t, operations)
buf.write_length()
Expand All @@ -193,7 +195,7 @@ cdef class Db:
op = tnt.TP_AUTH
sync = self.next_sync()
schema_id = self._protocol._schema_id
buf = WriteBuffer.new(self._encoding)
buf = WriteBuffer.new(self._encoding, self._encoding_str)
buf.write_header(sync, op, schema_id)

username_bytes = encode_unicode_string(username, self._encoding)
Expand Down
1 change: 1 addition & 0 deletions asynctnt/iproto/unicode.pxd
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
cdef bytes encode_unicode_string(str s, bytes encoding=*)
cdef str decode_string(bytes b, bytes encoding=*)
6 changes: 6 additions & 0 deletions asynctnt/iproto/unicode.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ cdef bytes encode_unicode_string(str s, bytes encoding=b'utf-8'):
s, encoding, b'strict'
)
return b


cdef str decode_string(bytes b, bytes encoding=b'utf-8'):
return <str><object>cpython.unicode.PyUnicode_FromEncodedObject(
b, encoding, b'strict'
)
2 changes: 1 addition & 1 deletion temp/bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async def bench_asynctnt(n, b, loop=None):
loop = loop or asyncio.get_event_loop()

conn = asynctnt.Connection(host='127.0.0.1',
port=3303,
port=3305,
username='t1',
password='t1',
reconnect_timeout=1, loop=loop)
Expand Down
2 changes: 1 addition & 1 deletion temp/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
box.cfg{
listen = 3303,
listen = 3305,
wal_mode = 'none'
}

Expand Down
24 changes: 10 additions & 14 deletions temp/t.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async def main(loop):
conn = None
try:
coro = asyncio.ensure_future(
asynctnt.connect(host='127.0.0.1', port=3303,
asynctnt.connect(host='127.0.0.1', port=3305,
username='t1', password='t1',
fetch_schema=True,
auto_refetch_schema=True,
Expand Down Expand Up @@ -69,23 +69,19 @@ async def main(loop):
# res = await conn.select('tester')
# print(res.body)
# res = await conn.call('test', timeout=0)
res = await conn.call('long', [10])
class A():
def __init__(self, a):
super(A, self).__init__()
self.a = a

def __str__(self):
return "A({})".format(self.a)

res = await conn.call('func_param', [A(100)])
print(res)
print(res.body)
print(res.schema_id)
return

res = await conn.call('long', [2])
print(res)
if res is not None:
print(res.body)
print(res.schema_id)

res = await conn.call('long', [5])
print(res)
if res is not None:
print(res.body)
print(res.schema_id)
# res = await conn.refetch_schema()
# res = await conn.insert('tester', (2, 'hello', 3))
# res = await conn.update('tester', [2], [(':', 1, 1, 3, 'yo!')])
Expand Down

0 comments on commit 499ba7a

Please sign in to comment.