Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide interface for conversion binary data to formatted tuples #5334

Open
olegrok opened this issue Sep 25, 2020 · 0 comments
Open

Provide interface for conversion binary data to formatted tuples #5334

olegrok opened this issue Sep 25, 2020 · 0 comments
Labels
feature A new functionality
Milestone

Comments

@olegrok
Copy link
Collaborator

olegrok commented Sep 25, 2020

All we know a problem - netbox:call converts tuples to simple Lua tables.
And it's OK. In general I don't know source space and structure of received tuples.
But if I receive data from the space I know it will be great to have an interface
to convert result of my call directly to tuples with format.

Currently it looks like

netbox = require('net.box')
netboxlib = require('net.box.lib')
buffer = require('buffer')
msgpackffi = require('msgpackffi')
msgpack = require('msgpack')
ffi = require('ffi')

-- Intro
box.cfg{listen = 3301}
box.schema.user.passwd('admin', 'test')

s = box.schema.space.create('test')
s:format({{name = 'id', type = 'string'}, {name = 'value', type = 'string'}})
s:create_index('pk')
s:replace({'key', 'value'})
c = netbox.connect('admin:test@localhost:3301')
function get()
    return s:select()
end

-- Byte juggling
call_buffer = buffer.ibuf()
c:call('get', {}, {buffer = call_buffer, skip_header = true})
-- After that I have a buffer
-- And I want to have an interface to directly deserialize this buffer
-- to tuples.
-- But I should wrap it with [IPROTO_DATA] header into map.
len, call_buffer.rpos = msgpack.decode_array_header(call_buffer.rpos, call_buffer:size())
buf = buffer.ibuf()
encode_fix = msgpackffi.internal.encode_fix
encode_r = msgpackffi.internal.encode_r
encode_fix(buf, 0x80, 1)
encode_r(buf, 0x30, 1)
wpos = buf:alloc(call_buffer:size())
ffi.copy(wpos, call_buffer.rpos, call_buffer:size())

tarantool> netboxlib.decode_select(buf.rpos, nil, c.space.test._format_cdata)
---
- - ['key', 'value']
- 'cdata<char *>: 0x010586803e'
...
tarantool> tuples = netboxlib.decode_select(buf.rpos, nil, c.space.test._format_cdata)
---
...

tarantool> tuples 
---
- - ['key', 'value']
...

tarantool> tuples[1]
---
- ['key', 'value']
...

tarantool> tuples[1]:tomap()
---
- 1: key
  2: value
  value: value
  id: key
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A new functionality
Projects
None yet
Development

No branches or pull requests

3 participants