Skip to content

Commit 5e2aff2

Browse files
committed
Document the new _{un,}pack() interface
1 parent 777cf30 commit 5e2aff2

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

README.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,19 @@ about 0.0015%. The type is `binary_fuse16_t` and you may use it with
5454
functions such as `binary_fuse16_allocate`, `binary_fuse16_populate`,
5555
`binary_fuse8_contain` and `binary_fuse8_free`.
5656
57-
You may serialize the data as follows:
57+
For serialization, there is a choice between an unpacked and a packed format.
58+
59+
The unpacked format is roughly of the same size as in-core data, but uses most
60+
efficient memory copy operations.
61+
62+
The packed format avoids storing zero bytes and is considered near optimal (it
63+
can not be compressed further by zlib and its required space is very close to
64+
the theoretical lower limit), but it needs to copy individual words, so it
65+
should be expected to be somewhat slower.
66+
67+
The two formats use slightly different APIs.
68+
69+
You may serialize and deserialize in unpacked format as follows:
5870
5971
```C
6072
size_t buffer_size = binary_fuse16_serialization_bytes(&filter);
@@ -65,9 +77,31 @@ You may serialize the data as follows:
6577
free(buffer);
6678
```
6779

68-
The serialization does not handle endianess: it is expected that you will serialize
69-
and deserialize on the little endian systems. (Big endian systems are vanishingly rare.)
80+
To serialize and deserialize in packed format, use the `_pack_bytes()`,
81+
`_pack()` and `_unpack()` functions. The latter two have an additional `size_t`
82+
argument for the buffer length. `_pack()` can be used with a buffer of arbitrary
83+
size, it returns the used space if serialization fit into the buffer or 0
84+
otherwise.
85+
86+
For example:
87+
88+
```C
89+
size_t buffer_size = binary_fuse16_pack_bytes(&filter);
90+
char *buffer = (char*)malloc(buffer_size);
91+
if (binary_fuse16_pack(&filter, buffer, buffer_size) != buffer_size) {
92+
printf("pack failed\n");
93+
free(buffer);
94+
return;
95+
}
96+
binary_fuse16_free(&filter);
97+
if (! binary_fuse16_unpack(&filter, buffer, buffer_size)) {
98+
printf("unpack failed\n");
99+
}
100+
free(buffer);
101+
```
70102
103+
Either serialization does not handle endianess changes: it is expected that you
104+
serialize and deserialize with equal byte order.
71105
72106
## C++ wrapper
73107

tests/unit.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,34 @@ void failure_rate_binary_fuse16() {
268268
free(big_set);
269269
}
270270

271+
// test code from the example in the README
272+
void readme_pack() {
273+
binary_fuse16_t filter = {0};
274+
if (! binary_fuse16_allocate(64, &filter)) {
275+
printf("allocation failed\n");
276+
return;
277+
}
278+
279+
// begin example snippet
280+
size_t buffer_size = binary_fuse16_pack_bytes(&filter);
281+
char *buffer = (char*)malloc(buffer_size);
282+
if (binary_fuse16_pack(&filter, buffer, buffer_size) != buffer_size) {
283+
printf("pack failed\n");
284+
free(buffer);
285+
return;
286+
}
287+
binary_fuse16_free(&filter);
288+
if (! binary_fuse16_unpack(&filter, buffer, buffer_size)) {
289+
printf("unpack failed\n");
290+
}
291+
free(buffer);
292+
// end example snippet
293+
294+
binary_fuse16_free(&filter);
295+
}
296+
271297
int main() {
298+
readme_pack();
272299
failure_rate_binary_fuse16();
273300
for(size_t size = 1000; size <= 1000000; size *= 300) {
274301
printf("== size = %zu \n", size);

0 commit comments

Comments
 (0)