Skip to content

Commit 1126b6e

Browse files
author
mdbmes
authored
CDRIVER-5641: BSON Binary Vector Subtype Support (#1868)
* Resolves CDRIVER-5641 by documenting and implementing new BSON Binary Vector APIs * Synced bson_binary_vector and bson_corpus spec tests from specifications commit 585b797c110b6709f81def6200b946b94c8d9c55 * tests: Full support for the existing version of the spec test suite. * tests: TODOs mark spec test improvements that depend on DRIVERS-3095 and DRIVERS-3097. * tests: Additional API usage and fuzzing. * New BSON Binary APIs: bson_append_binary_uninit, bson_iter_binary_equal, bson_iter_binary_subtype, bson_iter_overwrite_binary * Extend existing big-endian byte swapping API for 32-bit float * Compiler support for restricted pointer aliasing using BSON_RESTRICT. * Added note about the differing public and private API of bson_iter_binary and friends * ASSERT_MEMCMP improvements: eval inputs once inside parens, show a hex dump on mismatch, include error location.
1 parent 3c349a4 commit 1126b6e

File tree

95 files changed

+6404
-30
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+6404
-30
lines changed

src/libbson/doc/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ API Reference
2525
bson_writer_t
2626
bson_get_monotonic_time
2727
bson_memory
28+
binary_vector
2829
version
2930
legacy_extended_json

src/libbson/doc/binary_vector.rst

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
:man_page: libbson_binary_vector
2+
3+
BSON Binary Vector subtype
4+
==========================
5+
6+
In Libbson, we use the term *Vector* to refer to a data representation for compact storage of uniform elements, defined by the `BSON Binary Subtype 9 - Vector <https://github.com/mongodb/specifications/blob/master/source/bson-binary-vector/bson-binary-vector.md>`_ specification.
7+
8+
Libbson includes API support for Vectors:
9+
10+
* The *view* APIs provide an efficient way to access elements of Vector fields that reside within :symbol:`bson_t` storage.
11+
* Integration between *views* and other Libbson features: append, array builder, iter.
12+
* Vectors can be converted to and from a plain BSON Array, subject to the specification's type conversion rules.
13+
14+
The specification currently defines three element types, which Libbson interprets as:
15+
16+
* ``int8``: signed integer elements, equivalent to C ``int8_t``.
17+
* ``float32``: IEEE 754 floating point, 32 bits per element, least-significant byte first. After alignment and byte swapping, elements are equivalent to C ``float``.
18+
* ``packed_bit``: single-bit integer elements, packed most-significant bit first. Accessible in packed form as C ``uint8_t`` or as unpacked elements using C ``bool``.
19+
20+
Vector Views
21+
------------
22+
23+
.. toctree::
24+
:titlesonly:
25+
:maxdepth: 1
26+
27+
bson_vector_int8_view_t
28+
bson_vector_int8_const_view_t
29+
bson_vector_float32_view_t
30+
bson_vector_float32_const_view_t
31+
bson_vector_packed_bit_view_t
32+
bson_vector_packed_bit_const_view_t
33+
34+
Integration
35+
-----------
36+
37+
* Allocating Vectors inside :symbol:`bson_t`:
38+
39+
.. toctree::
40+
:titlesonly:
41+
:maxdepth: 1
42+
43+
bson_append_vector_int8_uninit
44+
bson_append_vector_float32_uninit
45+
bson_append_vector_packed_bit_uninit
46+
47+
* Accessing an existing Vector via :symbol:`bson_iter_t`:
48+
49+
.. code-block:: c
50+
51+
#define BSON_ITER_HOLDS_VECTOR(iter) /* ... */
52+
#define BSON_ITER_HOLDS_VECTOR_INT8(iter) /* ... */
53+
#define BSON_ITER_HOLDS_VECTOR_FLOAT32(iter) /* ... */
54+
#define BSON_ITER_HOLDS_VECTOR_PACKED_BIT(iter) /* ... */
55+
56+
.. toctree::
57+
:titlesonly:
58+
:maxdepth: 1
59+
60+
bson_vector_int8_view_from_iter
61+
bson_vector_int8_const_view_from_iter
62+
bson_vector_float32_view_from_iter
63+
bson_vector_float32_const_view_from_iter
64+
bson_vector_packed_bit_view_from_iter
65+
bson_vector_packed_bit_const_view_from_iter
66+
67+
Array Conversion
68+
----------------
69+
70+
* Polymorphic array-from-vector:
71+
72+
.. toctree::
73+
:titlesonly:
74+
:maxdepth: 1
75+
76+
bson_append_array_from_vector
77+
78+
* Type specific array-from-vector:
79+
80+
.. toctree::
81+
:titlesonly:
82+
:maxdepth: 1
83+
84+
bson_append_array_from_vector_int8
85+
bson_append_array_from_vector_float32
86+
bson_append_array_from_vector_packed_bit
87+
88+
* Using :symbol:`bson_array_builder_t` for array-from-vector:
89+
90+
.. toctree::
91+
:titlesonly:
92+
:maxdepth: 1
93+
94+
bson_array_builder_append_vector_int8_elements
95+
bson_array_builder_append_vector_float32_elements
96+
bson_array_builder_append_vector_packed_bit_elements
97+
bson_array_builder_append_vector_elements
98+
99+
* Type specific vector-from-array:
100+
101+
.. toctree::
102+
:titlesonly:
103+
:maxdepth: 1
104+
105+
bson_append_vector_int8_from_array
106+
bson_append_vector_float32_from_array
107+
bson_append_vector_packed_bit_from_array
108+
109+
Additional Definitions
110+
----------------------
111+
112+
* Binary subtype:
113+
114+
.. code-block:: c
115+
116+
typedef enum {
117+
BSON_SUBTYPE_VECTOR = 0x09,
118+
/* ... */
119+
} bson_subtype_t;
120+
121+
* Byte length of the Vector header:
122+
123+
.. code-block:: c
124+
125+
// Length of the required header for BSON_SUBTYPE_VECTOR, in bytes
126+
#define BSON_VECTOR_HEADER_LEN 2
127+
128+
* Byte length for a Vector with specific element type and count:
129+
130+
.. toctree::
131+
:titlesonly:
132+
:maxdepth: 1
133+
134+
bson_vector_int8_binary_data_length
135+
bson_vector_float32_binary_data_length
136+
bson_vector_packed_bit_binary_data_length
137+
138+
* Errors:
139+
140+
.. code-block:: c
141+
142+
// Error "domain"
143+
#define BSON_ERROR_VECTOR 4
144+
145+
.. toctree::
146+
:titlesonly:
147+
:maxdepth: 1
148+
149+
bson_vector_error_code_t
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
:man_page: bson_append_array_from_vector
2+
3+
bson_append_array_from_vector()
4+
===============================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
#define BSON_APPEND_ARRAY_FROM_VECTOR(b, key, iter) \
12+
bson_append_array_from_vector (b, key, (int) strlen (key), iter)
13+
14+
bool
15+
bson_append_array_from_vector (bson_t *bson,
16+
const char *key,
17+
int key_length,
18+
const bson_iter_t *iter);
19+
20+
Parameters
21+
----------
22+
23+
* ``bson``: A :symbol:`bson_t`.
24+
* ``key``: An ASCII C string containing the name of the field.
25+
* ``key_length``: The length of ``key`` in bytes, or -1 to determine the length with ``strlen()``.
26+
* ``iter``: A :symbol:`bson_iter_t` pointing to any supported :doc:`binary_vector` field.
27+
28+
Description
29+
-----------
30+
31+
Converts the Vector pointed to by ``iter`` into a plain BSON Array, written to ``bson`` under the name ``key``.
32+
33+
Returns
34+
-------
35+
36+
Returns ``true`` if the operation was applied successfully. The function fails if appending the array grows ``bson`` larger than INT32_MAX, or if ``iter`` doesn't point to a valid recognized Vector type.
37+
38+
.. seealso::
39+
40+
| :symbol:`bson_array_builder_append_vector_elements`
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
:man_page: bson_append_array_from_vector_float32
2+
3+
bson_append_array_from_vector_float32()
4+
=======================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
#define BSON_APPEND_ARRAY_FROM_VECTOR_FLOAT32(b, key, view) \
12+
bson_append_array_from_vector_float32 (b, key, (int) strlen (key), view)
13+
14+
bool
15+
bson_append_array_from_vector_float32 (bson_t *bson,
16+
const char *key,
17+
int key_length,
18+
bson_vector_float32_const_view_t view);
19+
20+
Parameters
21+
----------
22+
23+
* ``bson``: A :symbol:`bson_t`.
24+
* ``key``: An ASCII C string containing the name of the field.
25+
* ``key_length``: The length of ``key`` in bytes, or -1 to determine the length with ``strlen()``.
26+
* ``view``: A :symbol:`bson_vector_float32_const_view_t` pointing to validated ``float32`` :doc:`binary_vector` data.
27+
28+
Description
29+
-----------
30+
31+
Converts the Vector pointed to by ``view`` into a plain BSON Array, written to ``bson`` under the name ``key``.
32+
33+
Returns
34+
-------
35+
36+
Returns ``true`` if the operation was applied successfully. The function fails if appending the array grows ``bson`` larger than INT32_MAX.
37+
38+
.. seealso::
39+
40+
| :symbol:`bson_array_builder_append_vector_float32_elements`
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
:man_page: bson_append_array_from_vector_int8
2+
3+
bson_append_array_from_vector_int8()
4+
====================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
#define BSON_APPEND_ARRAY_FROM_VECTOR_INT8(b, key, view) \
12+
bson_append_array_from_vector_int8 (b, key, (int) strlen (key), view)
13+
14+
bool
15+
bson_append_array_from_vector_int8 (bson_t *bson,
16+
const char *key,
17+
int key_length,
18+
bson_vector_int8_const_view_t view);
19+
20+
Parameters
21+
----------
22+
23+
* ``bson``: A :symbol:`bson_t`.
24+
* ``key``: An ASCII C string containing the name of the field.
25+
* ``key_length``: The length of ``key`` in bytes, or -1 to determine the length with ``strlen()``.
26+
* ``view``: A :symbol:`bson_vector_int8_const_view_t` pointing to validated ``int8`` :doc:`binary_vector` data.
27+
28+
Description
29+
-----------
30+
31+
Converts the Vector pointed to by ``view`` into a plain BSON Array, written to ``bson`` under the name ``key``.
32+
33+
Returns
34+
-------
35+
36+
Returns ``true`` if the operation was applied successfully. The function fails if appending the array grows ``bson`` larger than INT32_MAX.
37+
38+
.. seealso::
39+
40+
| :symbol:`bson_array_builder_append_vector_int8_elements`
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
:man_page: bson_append_array_from_vector_packed_bit
2+
3+
bson_append_array_from_vector_packed_bit()
4+
==========================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
#define BSON_APPEND_ARRAY_FROM_VECTOR_PACKED_BIT(b, key, view) \
12+
bson_append_array_from_vector_packed_bit (b, key, (int) strlen (key), view)
13+
14+
bool
15+
bson_append_array_from_vector_packed_bit (bson_t *bson,
16+
const char *key,
17+
int key_length,
18+
bson_vector_packed_bit_const_view_t view);
19+
20+
Parameters
21+
----------
22+
23+
* ``bson``: A :symbol:`bson_t`.
24+
* ``key``: An ASCII C string containing the name of the field.
25+
* ``key_length``: The length of ``key`` in bytes, or -1 to determine the length with ``strlen()``.
26+
* ``view``: A :symbol:`bson_vector_packed_bit_const_view_t` pointing to validated ``packed_bit`` :doc:`binary_vector` data.
27+
28+
Description
29+
-----------
30+
31+
Converts the Vector pointed to by ``view`` into a plain BSON Array, written to ``bson`` under the name ``key``.
32+
33+
Returns
34+
-------
35+
36+
Returns ``true`` if the operation was applied successfully. The function fails if appending the array grows ``bson`` larger than INT32_MAX.
37+
38+
.. seealso::
39+
40+
| :symbol:`bson_array_builder_append_vector_packed_bit_elements`

src/libbson/doc/bson_append_binary.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ Returns
3838
-------
3939

4040
Returns ``true`` if the operation was applied successfully. The function will fail if appending ``binary`` grows ``bson`` larger than INT32_MAX.
41+
42+
.. seealso::
43+
44+
| :symbol:`bson_append_binary_uninit`
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
:man_page: bson_append_binary_uninit
2+
3+
bson_append_binary_uninit()
4+
===========================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
#define BSON_APPEND_BINARY_UNINIT(b, key, subtype, val, len) \
12+
bson_append_binary_uninit (b, key, (int) strlen (key), subtype, val, len)
13+
14+
bool
15+
bson_append_binary_uninit (bson_t *bson,
16+
const char *key,
17+
int key_length,
18+
bson_subtype_t subtype,
19+
uint8_t **binary,
20+
uint32_t length);
21+
22+
Parameters
23+
----------
24+
25+
* ``bson``: A :symbol:`bson_t`.
26+
* ``key``: The key name.
27+
* ``key_length``: The length of ``key`` in bytes or -1 to use strlen().
28+
* ``subtype``: A bson_subtype_t indicating the binary subtype.
29+
* ``binary``: Location for a pointer that will receive a writable pointer to the uninitialized binary data block.
30+
* ``length``: The length of ``buffer`` in bytes.
31+
32+
Description
33+
-----------
34+
35+
The :symbol:`bson_append_binary_uninit()` function is an alternative to :symbol:`bson_append_binary()` which allows applications to assemble the contents of the binary field within the :symbol:`bson_t`, without an additional temporary buffer.
36+
On success, the caller MUST write to every byte of the binary data block if the resulting :symbol:`bson_t` is to be used.
37+
The buffer that ``binary`` points to is only valid until the iterator's :symbol:`bson_t` is otherwise modified or freed.
38+
39+
40+
Returns
41+
-------
42+
43+
Returns ``true`` if the uninitialized ``binary`` item was appended. The function will fail if appending ``binary`` grows ``bson`` larger than INT32_MAX.

0 commit comments

Comments
 (0)