Tarantool Lua API

buffer

The buffer module returns a dynamically resizable buffer which is solely for optional use by methods of the net.box module or the msgpack module.

Ordinarily the net.box methods return a Lua table. If a buffer option is used, the response body is written to the provided buffer and the method returns the number of bytes written. This saves time on the server, if the client application has its own routine for decoding raw MsgPack data.

The buffer uses four pointers to manage its capacity:

  • buf -- a pointer to the beginning of the buffer
  • rpos -- a pointer to the beginning of the range; available for reading
  • data ("read position")

  • wpos -- a pointer to the end of the range; available for reading data,
  • and to the beginning of the range for writing new data ("write position")

  • epos -- a pointer to the end of the range; available for writing new
  • data ("end position")

Module buffer and skip_header

The buffer.ibuf() example shows that, ordinarily, the response from net.box includes a header -- 48 (hexadecimal 30) that is the key for IPROTO_DATA. But in some situations, for example when passing the buffer to a C function that expects a MsgPack byte array without a header, the header can be skipped. This is done by specifying skip_header=true as an option to conn.space.space-name:select{...} or conn.space.space-name:insert{...} or conn.space.space-name:replace{...} or conn.space.space-name:update{...} or conn.space.space-name:upsert{...} or conn.space.space-name:delete{...}. The default is skip_header=false.

Now here is the end of the same example, except that skip_header=true is used.

ibuf = buffer.ibuf()

conn = net_box.connect('usr1:pwd1@localhost:3301')
conn.space.tester:select({}, {buffer=ibuf, skip_header=true})

msgpack.decode_unchecked(ibuf.rpos)

The result of the final request looks like this:

tarantool> msgpack.decode_unchecked(ibuf.rpos)
    ---
    - [['ABCDE', 12345]]
    - 'cdata<char *>: 0x7f8fd102803f'
    ...

Notice that the IPROTO_DATA header (48) is gone.

The result is still inside an array, as is clear from the fact that it is shown inside square brackets. It is possible to skip the array header too, with msgpack.decode_array_header().

Methods1

function M.ibuf(arg: nil | integer) -> buf buffer_object

Create a new buffer.

Example:

In this example we will show that using buffer allows you to keep the data in the format that you get from the server. So if you get the data only for sending it somewhere else, buffer fastens this a lot.

box.cfg{listen = 3301}
buffer = require('buffer')
net_box = require('net.box')
msgpack = require('msgpack')

box.schema.space.create('tester')
box.space.tester:create_index('primary')
box.space.tester:insert({1, 'ABCDE', 12345})

box.schema.user.create('usr1', {password = 'pwd1'})
box.schema.user.grant('usr1', 'read,write,execute', 'space', 'tester')

ibuf = buffer.ibuf()

conn = net_box.connect('usr1:pwd1@localhost:3301')
conn.space.tester:select({}, {buffer=ibuf})

msgpack.decode_unchecked(ibuf.rpos)

The result of the final request looks like this:

tarantool> msgpack.decode_unchecked(ibuf.rpos)
    ---
    - {48: [['ABCDE', 12345]]}
    - 'cdata<char *>: 0x7f97ba10c041'
    ...

Note: before Tarantool 1.7.7, the function to use for this case is msgpack.ibuf_decode(ibuf.rpos). Starting with Tarantool 1.7.7, ibuf_decode is deprecated.

Parameters
argnil | integer

buffer size in bytes.

Returns
bufbuffer_object

the new buffer.