Documents and schemas
The binary document format (types, layouts, sizes) and how a schema checks documents before they are stored.
The document format (raftdb-doc)#
A document is a typed value read in place without decoding. Every value is one tag byte followed by data; multi-byte numbers are little-endian.
| Type | Tag | Data |
|---|---|---|
| null | 0 | none |
| false, true | 1, 2 | none |
| int | 3 (i8), 4 (i16), 13 (i24), 5 (i32), 6 (i64); 0xC0 to 0xFF for -16 to 47 | smallest width that fits |
| float | 7 (raw f64), 14 (decimal: exponent byte 0..6 and zigzag varint mantissa) | decimal form only if it reproduces the exact bits |
| timestamp | 8 | i64 microseconds since the Unix epoch, UTC |
| bytes | 9 | varint length, bytes |
| string | 0x80 to 0xBF (0 to 63 bytes, length in the tag), 10 (longer) | UTF-8 |
| array | 11 | varint body length, values |
| object | 12 (id list), 15 (masked) | varint body length, then fields |
Field names are not stored in documents. Each table has a dictionary that maps a name to an id, and an object stores ids. In the id-list layout each field costs the difference from the expected id (previous id + 1), so consecutive ids cost one byte. The masked layout is used when every id is below 64 and the mask is no longer than the id list: a 12-field object pays 2 bytes of ids instead of 12. Measured: 111.7 bytes for the 12-field sample document against 252.6 for JSON and 250.6 for BSON.
The server keeps one dictionary per table; a new name gets the next id and is queued to every shard in order. Over the wire, FieldId returns the id of a name (allocating it), which a client needs to build binary documents or to ask for Project.
UUID, decimal128 and date types are not encoded yet; timestamps are always 9 bytes.
JSON in and out#
- In:
PutJsonand the HTTP write endpoints convert JSON text with the server's dictionary. Integers without.or exponent areInt; other numbers areF64. Duplicate names in one object are refused. - Out:
raftdb_doc::to_jsonwrites a document as JSON, naming fields with the dictionary (an unknown id is written"#id"). Types JSON lacks become one-field objects: a timestamp is{"$ts": microseconds}, bytes are{"$bytes": "hex"}, and a non-finite double becomesnull. - Binary: a client that holds binary documents can send them with
Putand read them withGetandFilter(default flags), skipping conversion on the server.
Strict comparison#
Value::cmp_strict compares only equal types (int with int, double with double, text with text, timestamp with timestamp, bool with bool, null with null); anything else is None. This is the rule under the filter language.
Schemas (raftdb-schema)#
A table may have a schema: a document that breaks it is rejected before it is stored, and a rejected write logs nothing. There is no coercion: an Int field never accepts 31.0 or "31".
Schema::new(strict) strict = true rejects fields that have no rule
.field(id, Field::new(Ty::Int).required().range(0, 100))
Ty: Bool, Int, F64, Ts, Str, Bytes, Arr(Ty), Obj(Schema), Any
Field: .required() .nullable() .range(min, max)range is a limit on the value for Int and Ts, on the byte length for Str and Bytes, and on the element count for Arr. An explicit null is accepted only for nullable fields (or Any); an absent field is accepted unless required. At most 64 required fields per object level. The first problem found is reported as Missing(id), Unknown(id), Type(id), Range(id) or Malformed.
What a client sees#
Test the_schema_is_enforced_over_the_network: a table has the strict rule "n is an integer, required, 0 to 100". Put of n = 50 succeeds; Put of n = 500 fails with error code 2 SCHEMA and a message that contains Range; Get of that key answers not found.
How a schema is set#
A schema is set from Rust, through the on_open hook of the server configuration (Config.on_open, run on every shard's table before connections are accepted) or Table::set_schema in raftdb-db. It is logged, so it is back after a restart. There is no statement and no HTTP endpoint to set a schema yet, the lsm engine refuses a schema through on_open, and changing a schema on a table that already holds data (migration, backfill) is not designed.
Cost#
raftdb-bench schema, 1 M documents, Windows 11: 3.16 M documents/s for 12 fields including a nested object and an array (about 316 ns each); no valid document wrongly rejected and 100 % of wrong-type documents caught.
Not supported yet#
unique and foreign-key relations, defaults, enums, string patterns and cross-field checks in schemas; column types in the HTTP table detail (types is empty because types are not tracked).