Skip to content

Commit 0ff90ef

Browse files
p7novandreyaksenov
andauthored
Add varbinary docs (#4405)
Resolves #3551 Co-authored-by: Andrey Aksenov <38073144+andreyaksenov@users.noreply.github.com>
1 parent bcaaecf commit 0ff90ef

File tree

8 files changed

+373
-55
lines changed

8 files changed

+373
-55
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
local varbinary = require('varbinary')
2+
3+
-- Create a varbinary object
4+
local bin = varbinary.new('data')
5+
local bin_hex = varbinary.new('\xFF\xFE')
6+
7+
-- Check whether a value is a varbinary object
8+
varbinary.is(bin) -- true
9+
varbinary.is(bin_hex) -- true
10+
varbinary.is(100) -- false
11+
varbinary.is('data') -- false
12+
13+
-- Check varbinary objects equality
14+
print(bin == varbinary.new('data')) -- true
15+
print(bin == 'data') -- true
16+
print(bin ~= 'data1') -- true
17+
print(bin_hex ~= '\xFF\xFE') -- false
18+
19+
-- Check varbinary objects length
20+
print(#bin) -- 4
21+
print(#bin_hex) -- 2
22+
23+
-- Print string representation
24+
print(tostring(bin)) -- data
25+
26+
local bin2 = varbinary.new(ffi.cast('const char *', 'data'), 4)
27+
varbinary.is(bin2) -- true
28+
print(bin2) -- data
29+
30+
local luatest = require('luatest')
31+
local test_group = luatest.group()
32+
test_group.test_varbinary = function()
33+
luatest.assert_equals(varbinary.is(bin), true)
34+
luatest.assert_equals(bin, 'data')
35+
luatest.assert_equals(#bin, 4)
36+
luatest.assert_equals(#varbinary.new('\xFF\xFE'), 2)
37+
end

doc/platform/app/cookbook.rst

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -249,56 +249,6 @@ a metatable).
249249
local b = a + point(0.5, 8)
250250
print(#b) --> 12.5
251251
252-
.. _cookbook-ffi_varbinary_insert:
253-
254-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
255-
ffi_varbinary_insert.lua
256-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
257-
258-
Use the `LuaJIT ffi library <http://luajit.org/ext_ffi.html>`_ to
259-
insert a tuple which has a VARBINARY field.
260-
261-
Note that it is allowed only inside a memtx transaction: when ``box_insert()``
262-
does not yield.
263-
264-
Lua does not have direct support for VARBINARY, so using C
265-
is one way to put in data which in MessagePack is stored as bin
266-
(MP_BIN). If the tuple is retrieved later, field "b" will have type = 'cdata'.
267-
268-
.. code-block:: lua
269-
270-
#!/usr/bin/env tarantool
271-
272-
-- box.cfg{} should be here
273-
274-
s = box.schema.space.create('withdata')
275-
s:format({{"b", "varbinary"}})
276-
s:create_index('pk', {parts = {1, "varbinary"}})
277-
278-
buffer = require('buffer')
279-
ffi = require('ffi')
280-
281-
function varbinary_insert(space, bytes)
282-
local tmpbuf = buffer.ibuf()
283-
local p = tmpbuf:alloc(3 + #bytes)
284-
p[0] = 0x91 -- MsgPack code for "array-1"
285-
p[1] = 0xC4 -- MsgPack code for "bin-8" so up to 256 bytes
286-
p[2] = #bytes
287-
for i, c in pairs(bytes) do p[i + 3 - 1] = c end
288-
ffi.cdef[[int box_insert(uint32_t space_id,
289-
const char *tuple,
290-
const char *tuple_end,
291-
box_tuple_t **result);]]
292-
ffi.C.box_insert(space.id, tmpbuf.rpos, tmpbuf.wpos, nil)
293-
tmpbuf:recycle()
294-
end
295-
296-
varbinary_insert(s, {0xDE, 0xAD, 0xBE, 0xAF})
297-
varbinary_insert(s, {0xFE, 0xED, 0xFA, 0xCE})
298-
299-
-- if successful, Tarantool enters the event loop now
300-
301-
302252
.. _cookbook-print_arrays:
303253

304254
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

doc/platform/ddl_dml/value_store.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,8 @@ bin
345345
***
346346

347347
A bin (binary) value is not directly supported by Lua but there is
348-
a Tarantool type ``varbinary`` which is encoded as MsgPack binary.
349-
For an (advanced) example showing how to insert varbinary into a database,
350-
see the Cookbook Recipe for :ref:`ffi_varbinary_insert <cookbook-ffi_varbinary_insert>`.
348+
a Tarantool type ``varbinary``. See the :ref:`varbinary module reference <varbinary-module>`
349+
for details.
351350

352351
**Example:** ``"\65 \66 \67"``.
353352

doc/reference/configuration/configuration_reference.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,8 @@ The ``compat`` section defines values of the :ref:`compat <compat-module>` modul
431431
- ``new``: as varbinary objects
432432
- ``old``: as plain strings
433433

434+
See also: :ref:`compat-option-binary-decoding`
435+
434436
|
435437
| Type: string
436438
| Possible values: 'new', 'old'

doc/reference/reference_lua/compat.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Below are the available ``compat`` options:
7272
* :doc:`box_cfg_replication_sync_timeout <./compat/box_cfg_replication_sync_timeout>`
7373
* :doc:`sql_seq_scan_default <./compat/sql_seq_scan_default>`
7474
* :doc:`fiber_slice_default <./compat/fiber_slice_default>`
75+
* :doc:`binary_data_decoding <./compat/binary_data_decoding>`
7576

7677
.. toctree::
7778
:hidden:
@@ -82,4 +83,5 @@ Below are the available ``compat`` options:
8283
compat/box_cfg_replication_sync_timeout
8384
compat/sql_seq_scan_default
8485
compat/fiber_slice_default
86+
compat/binary_data_decoding
8587
compat/compat_tutorial
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
.. _compat-option-binary-decoding:
2+
3+
Decoding binary objects
4+
=======================
5+
6+
Option: ``binary_data_decoding``
7+
8+
Starting from version 3.0, Tarantool has the :ref:`varbinary <varbinary-module>` module
9+
for handling binary objects of arbitrary lengths.
10+
The ``binary_data_decoding`` compat option allows to define the format in which
11+
varbinary field values are returned for handling in Lua: plain strings or ``varbinary``
12+
objects.
13+
14+
Old and new behavior
15+
--------------------
16+
17+
New behavior: ``varbinary`` field values are returned as ``varbinary`` objects.
18+
19+
.. code-block:: tarantoolsession
20+
21+
tarantool> compat.binary_data_decoding = 'new'
22+
---
23+
...
24+
25+
tarantool> varbinary.is(msgpack.decode('\xC4\x02\xFF\xFE'))
26+
---
27+
- true
28+
...
29+
30+
Old behavior: ``varbinary`` field values are returned as plain strings.
31+
32+
.. code-block:: tarantoolsession
33+
34+
tarantool> compat.binary_data_decoding = 'old'
35+
---
36+
...
37+
38+
tarantool> varbinary.is(msgpack.decode('\xC4\x02\xFF\xFE'))
39+
---
40+
- false
41+
...
42+
43+
tarantool> varbinary.is(yaml.decode('!!binary //4='))
44+
---
45+
- false
46+
...
47+
48+
Known compatibility issues
49+
--------------------------
50+
51+
At this point, no incompatible modules are known.
52+
53+
Detecting issues in your codebase
54+
---------------------------------
55+
56+
String manipulation methods, such as ``string.sub()`` or ``string.match()`` are not
57+
defined for ``varbinary`` objects. Thus, if you use such methods on results of
58+
binary data decoding from MsgPack or YAML, convert them to strings
59+
explicitly using the ``tostring()`` method.

doc/reference/reference_lua/index.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ This reference covers Tarantool's built-in Lua modules.
5858
table
5959
tap
6060
tarantool
61-
uuid
62-
utf8
6361
uri
62+
utf8
63+
uuid
64+
varbinary
6465
xlog
6566
yaml
6667
other

0 commit comments

Comments
 (0)