Internals

raftdb-doc, the document format

The binary layout of a stored document, how fields are identified, how documents are read without decoding, and the measured size.

raftdb-doc defines how a document is stored in memory, in the log and on the wire. It has no dependencies and #![forbid(unsafe_code)]. Values are typed and are read in place, without decoding the whole document first.

Byte layout#

A value is one tag byte followed by data. All multi-byte numbers are little-endian.

TagMeaningDataTotal bytes
0null1
1 / 2false / true1
3, 4, 13, 5, 6i8, i16, i24, i32, i641, 2, 3, 4, 8 bytes2, 3, 4, 5, 9
7f64 (raw)8 bytes9
14f64 (decimal)exponent byte e (0..6), zigzag varint m; value = m / 10^e3 or more
8timestampi64 microseconds since the Unix epoch, UTC9
9bytesvarint length, bytes
10string of 64 bytes or morevarint length, UTF-8 bytes
11arrayvarint body length, values back to back
12object, id listvarint body length, then per field: varint (id - expected), value
15object, maskedvarint body length, varint presence mask, values of the set bits in id order
0x80..0xBFstring of 0..63 byteslength in the tag's low 6 bits, then the bytes1 + length
0xC0..0xFFinteger -16..47value = tag - 0xC0 - 16, no data1

Integers are written in the smallest width that holds them. Small integers and short strings need no length or data bytes at all.

Field names are not stored#

A per-collection dictionary (Dict) maps a field name to an id; documents store only ids. In the id-list layout each field costs one varint of id - expected, where expected is the previous id plus one, so consecutive ids cost one byte each. The masked layout is used when every id is below 64 and the mask is not longer than the id list (use_mask): a 12-field object pays 2 bytes for ids instead of 12.

Decimal floats (tag 14) are used only if m / 10^e reproduces the exact bits of the value, checked on write. So -0.0, NaN, infinities and values such as 0.1 + 0.2 keep the raw form.

Reading and writing#

  • Writing appends to a Vec<u8>: put_null, put_bool, put_int, put_f64, put_ts, put_str, put_bytes, put_varint; containers use begin(tag) and end(start). Field ids must ascend (Unsorted error otherwise).
  • Reading: read(bytes) returns a Value that borrows from the document and the number of bytes used; len(bytes) gives the size without decoding; get(doc, id) and Obj::get find a field. Every read returns Option: truncated or corrupt data gives None, never a panic.
  • Value::cmp_strict compares only equal types (Int with Int, F64 with F64, Str with Str and so on); anything else is None. There is no coercion anywhere in the engine: an integer field never matches 31.0.
  • to_json(doc, &Dict, &mut String) writes a document as JSON. Types JSON lacks become one-field objects: timestamps {"$ts": microseconds}, bytes {"$bytes": "hex"}; a non-finite double becomes null.
  • project(doc, ids, out) copies the named fields of an object as they are stored, without decoding. The server uses it for Project requests.

Measured#

111.7 bytes for the 12-field sample document, against 252.6 for JSON and 250.6 for BSON; 29.6 M field reads per second per thread.

Tests#

Nine tests: every integer width boundary; float round trips for 400,000 patterns including NaN and -0.0; objects in both layouts; the mask-or-list choice; unsorted rejection; strict comparison; every truncation and 60,000 random corruptions never panic.

Not supported yet#

UUID, decimal128 and date types are not encoded; timestamps are always 9 bytes; strings are validated when written, not on read.