Skip to content

Commit f414207

Browse files
Onvemberlenkis
authored andcommitted
fix gh-992 Describe how decimals are encoded in the binary protocol (#1062)
1 parent 4bb378e commit f414207

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

doc/dev_guide/internals/box_protocol.rst

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,98 @@ 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
51+
the MsgPack standard.
52+
For these data types, we use the following representation.
53+
54+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55+
Decimals
56+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57+
58+
MsgPack EXT type ``MP_EXT`` together with the extension type
59+
``MP_DECIMAL`` is used as a record header.
60+
61+
MP_DECIMAL is 1.
62+
63+
`MsgPack spec <https://github.com/msgpack/msgpack/blob/master/spec.md#ext-format-family>`_
64+
defines two kinds of types:
65+
66+
* ``fixext 1/2/4/8/16`` types have fixed length so the length is not encoded explicitly;
67+
* ``ext 8/16/32`` types require the data length to be encoded.
68+
69+
``MP_EXP`` + optional ``length`` imply using one of these types.
70+
71+
The decimal MsgPack representation looks like this:
72+
73+
.. code-block:: none
74+
75+
+--------+-------------------+------------+===============+
76+
| MP_EXT | length (optional) | MP_DECIMAL | PackedDecimal |
77+
+--------+-------------------+------------+===============+
78+
79+
Here ``length`` is the length of ``PackedDecimal`` field, and it is of type
80+
``MP_UINT``, when encoded explicitly (i.e. when the type is ``ext 8/16/32``).
81+
82+
``PackedDecimal`` has the following structure:
83+
84+
.. code-block:: none
85+
86+
<--- length bytes -->
87+
+-------+=============+
88+
| scale | BCD |
89+
+-------+=============+
90+
91+
Here ``scale`` is either ``MP_INT`` or ``MP_UINT``. |br|
92+
``scale`` = -exponent (exponent negated!)
93+
94+
``BCD`` is a sequence of bytes representing decimal digits of the encoded number
95+
(each byte represents two decimal digits each encoded using 4 bits),
96+
so ``byte >> 4`` is the first digit and ``byte & 0x0f`` is the second digit.
97+
The leftmost digit in the array is the most significant.
98+
The rightmost digit in the array is the least significant.
99+
100+
The first byte of the ``BCD`` array contains the first digit of the number,
101+
represented as follows:
102+
103+
.. code-block:: none
104+
105+
| 4 bits | 4 bits |
106+
= 0x = the 1st digit
107+
108+
The last byte of the ``BCD`` array contains the last digit of the number and the
109+
``nibble``, represented as follows:
110+
111+
.. code-block:: none
112+
113+
| 4 bits | 4 bits |
114+
= the last digit = nibble
115+
116+
The ``nibble`` represents the number's sign:
117+
118+
* ``0x0a``, ``0x0c``, ``0x0e``, ``0x0f`` stand for plus,
119+
* ``0x0b`` and ``0x0d`` stand for minus.
120+
121+
**Examples**
122+
123+
The decimal ``-12.34`` will be encoded as ``0xd6,0x01,0x02,0x01,0x23,0x4d``:
124+
125+
.. code-block:: none
126+
127+
|MP_EXT (fixext 4) | MP_DECIMAL | scale | 1 | 2,3 | 4 (minus) |
128+
| 0xd6 | 0x01 | 0x02 | 0x01 | 0x23 | 0x4d |
129+
130+
The decimal 0.000000000000000000000000000000000010
131+
will be encoded as ``0xc7,0x03,0x01,0x24,0x01,0x0c``:
132+
133+
.. code-block:: none
134+
135+
| MP_EXT (ext 8) | length | MP_DECIMAL | scale | 1 | 0 (plus) |
136+
| 0xc7 | 0x03 | 0x01 | 0x24 | 0x01 | 0x0c |
137+
46138
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
47139
Greeting packet
48140
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)