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.
| Tag | Meaning | Data | Total bytes |
|---|---|---|---|
| 0 | null | 1 | |
| 1 / 2 | false / true | 1 | |
| 3, 4, 13, 5, 6 | i8, i16, i24, i32, i64 | 1, 2, 3, 4, 8 bytes | 2, 3, 4, 5, 9 |
| 7 | f64 (raw) | 8 bytes | 9 |
| 14 | f64 (decimal) | exponent byte e (0..6), zigzag varint m; value = m / 10^e | 3 or more |
| 8 | timestamp | i64 microseconds since the Unix epoch, UTC | 9 |
| 9 | bytes | varint length, bytes | |
| 10 | string of 64 bytes or more | varint length, UTF-8 bytes | |
| 11 | array | varint body length, values back to back | |
| 12 | object, id list | varint body length, then per field: varint (id - expected), value | |
| 15 | object, masked | varint body length, varint presence mask, values of the set bits in id order | |
| 0x80..0xBF | string of 0..63 bytes | length in the tag's low 6 bits, then the bytes | 1 + length |
| 0xC0..0xFF | integer -16..47 | value = tag - 0xC0 - 16, no data | 1 |
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 usebegin(tag)andend(start). Field ids must ascend (Unsortederror otherwise). - Reading:
read(bytes)returns aValuethat borrows from the document and the number of bytes used;len(bytes)gives the size without decoding;get(doc, id)andObj::getfind a field. Every read returnsOption: truncated or corrupt data givesNone, never a panic. Value::cmp_strictcompares only equal types (Int with Int, F64 with F64, Str with Str and so on); anything else isNone. There is no coercion anywhere in the engine: an integer field never matches31.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 becomesnull.project(doc, ids, out)copies the named fields of an object as they are stored, without decoding. The server uses it forProjectrequests.
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.