WiredTiger: Packing and Unpacking Data

| Version 3.1.0

Fork me on GitHub Join my user group
Packing and Unpacking Data

WiredTiger's data packing uses format strings similar to those specified in the Python struct module: http://docs.python.org/library/struct

The first character of the format string can be used to indicate the byte order, size and alignment of the packed data, according to the following table:

Character | Byte order | Size | Alignment
--- | --- | --- | ---
`.` | big-endian | packed | none
`>` | big-endian | standard | none
`<` | little-endian | standard | none
`@` | native | native | native

If the first character is not one of these, '.' (big-endian, packed) is assumed: it naturally sorts in lexicographic order, and the packed format uses variable-sized encoding of values to reduce the data size.

Note: little-endian format not yet supported in WiredTiger. Only the default big-endian, packed format is currently supported.

The remaining characters in the format string specify the type of each field to be packed into or unpacked from a byte array. See Column types for the list of supported types.

# Code samples

The code below is taken from the complete example program ex_pack.c. It demonstrates how to pack three integer values into a buffer and then unpack them again.

size_t size;
char buf[50];
error_check(
wiredtiger_struct_size(session, &size, "iii", 42, 1000, -9));
if (size > sizeof(buf)) {
/* Allocate a bigger buffer. */
}
error_check(
wiredtiger_struct_pack(session, buf, size, "iii", 42, 1000, -9));
error_check(
wiredtiger_struct_unpack(session, buf, size, "iii", &i, &j, &k));