Skip to content

Commit b93c837

Browse files
committed
gh-992 add submodule about encoding of non default types such as decimals
1 parent c8168e8 commit b93c837

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

doc/dev_guide/internals/box_protocol.rst

+74
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,80 @@ MsgPack data types:
4343
* **MP_OBJECT** - Any MsgPack object
4444
* **MP_BIN** - MsgPack binary format
4545

46+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
47+
Encoding of Tarantool-specific data types
48+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49+
50+
Some of the data types used in Tarantool are application-specific in terms of the MsgPack standard.
51+
For these data types, we use the following representation.
52+
53+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
54+
Decimals
55+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56+
57+
MsgPack EXT type ``MP_EXT`` together with a new extension type
58+
``MP_DECIMAL`` is used as a record header.
59+
60+
MP_DECIMAL is 1.
61+
62+
`MsgPack spec <https://github.com/msgpack/msgpack/blob/master/spec.md#ext-format-family>`_
63+
defines ``fixext 1/2/4/8/16`` and ``ext 8/16/32`` types. ``fixext``
64+
types have fixed length so it is not encoded explicitly, while ``ext`` types require
65+
the data length to be encoded. ``MP_EXP`` + optional ``length`` meant usage of one of those types.
66+
67+
The decimal MsgPack representation looks like this:
68+
69+
.. code-block:: none
70+
71+
+--------+-------------------+------------+===============+
72+
| MP_EXT | length (optional) | MP_DECIMAL | PackedDecimal |
73+
+--------+-------------------+------------+===============+
74+
75+
Here ``length`` is the length of PackedDecimal field, and it is of type
76+
``MP_UINT``, when encoded explicitly (i.e. when type is ``ext 8/16/32``).
77+
78+
PackedDecimal has the following structure:
79+
80+
.. code-block:: none
81+
82+
<--- length bytes -->
83+
+-------+=============+
84+
| scale | BCD |
85+
+-------+=============+
86+
87+
Here ``scale`` is either ``MP_INT`` or ``MP_UINT``
88+
``scale`` = -exponent (exponent negated(!))
89+
90+
``BCD`` is a sequence of bytes representing decimal digits of the encoded number
91+
(each byte represents two decimal digits each encoded using 4 bits),
92+
so ``byte >> 4`` is the first digit and ``byte & 0x0f`` is the second digit.
93+
The leftmost digit in the array is the most significant.
94+
The rightmost digit in the array is the least significant.
95+
96+
The first byte in the BCD array may have only second digit.
97+
The last byte in the BCD array has only first digit and a ``nibble``.
98+
99+
The ``nibble`` represents the number's sign. ``0x0a``, ``0x0c``, ``0x0e``, ``0x0f``
100+
stand for plus, ``0x0b`` and ``0x0d`` stand for minus.
101+
102+
**Example**
103+
104+
For example, decimal ``-12.34`` will be encoded as ``0xd6,0x01,0x02,0x01,0x23,0x4d``
105+
106+
.. code-block:: none
107+
108+
|MP_EXT (fixext 4) | MP_DECIMAL | scale | 1 | 2,3 | 4 (minus) |
109+
| 0xd6 | 0x01 | 0x02 | 0x01 | 0x23 | 0x4d |
110+
111+
Another example: decimal 0.000000000000000000000000000000000010 will be encoded
112+
as ``0xc7,0x03,0x01,0x24,0x01,0x0c``
113+
114+
.. code-block:: none
115+
116+
| MP_EXT (ext 8) | length | MP_DECIMAL | scale | 1 | 0 (plus) |
117+
| 0xc7 | 0x03 | 0x01 | 0x24 | 0x01 | 0x0c |
118+
119+
46120
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
47121
Greeting packet
48122
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)