Skip to content

Hierarchical path keys, format 1

A namespace holds trees of typed nodes. Its schema lists the node types in pre-order (every parent before its children, siblings in their intended order). Each type has:

  • a code: 0x0100 * (index + 1), a big-endian u16; the gaps allow a new type to be placed between two existing ones without rewriting keys;
  • a bucket length b (usually 0): a fixed-length placement prefix in front of the key (used for hashed placement of roots);
  • a key length k: the node's key field, raw bytes, compared bytewise, never normalized (0 for unkeyed types);
  • a twin rule: unique key, duplicate key or unkeyed. Types other than unique key carry an ordinal.

Components

A node is stored under the concatenation of one component per level, root first:

component = code:u16be  bucket:b bytes  key:k bytes  [ ordinal ]
ordinal   = digit+ 0x00          ; digit in 0x01..=0xFF

The type fixes the length of everything but the ordinal, and the ordinal ends at its terminator, so a key splits into components unambiguously given the schema. A component's type must be a child of the previous component's type (the first is the root type).

Order

Keys compare as byte strings. Then:

  • a node's key is a prefix of the keys of all its descendants, so a parent sorts before its subtree and every subtree is a contiguous key range;
  • siblings sort by type code, then bucket, then key, then ordinal;
  • the byte order of all keys is the pre-order traversal of the trees.

"Next node in pre-order" is "next key"; "children of P of type T" is the range of keys starting with P ‖ code(T); "skip the subtree of N" is "seek to the smallest key above every key that starts with N" (increment the last byte that is not 0xFF, dropping the 0xFF bytes after it).

Ordinals

Ordinals are fractional indexes. Read the digits as base-255 fractions (value = digit - 1); a terminator sorts before any digit, so a shorter digit string sorts before its extensions. A generated ordinal never ends with the digit 0x01, which keeps room between any two ordinals. Generation (oliviadb_core::pathkey::ordinal_between):

  • after the last sibling: increase the first digit by one (254 appends per byte of growth);
  • before the first sibling: decrease the first digit by one;
  • between two siblings: the midpoint of the first differing digit, or, when the digits are adjacent, the lower one followed by a digit above the remaining suffix.

Versions

Stored keys are (path key, version); the version is a hybrid logical clock timestamp (upper 48 bits milliseconds since the Unix epoch, lower 16 bits a counter). Stored entries sort by path key ascending, then version descending; a read at version v sees, for each path key, the newest entry with version at or below v (a tombstone hides the key).

Limits

Format 1 itself puts no bound on the length of a path key (lengths in the storage format are varints). What the implementation enforces today, and what is open, is listed in docs/limits.md.


Format 2: typed keys

Status: final (format 2, version 1), 2026-12-02: clients decode format-2 key components (native-protocol.md), so the bytes are a contract. The decisions that closed the draft's open list are in "Decisions" at the end; the bound on key length is not enforced by the core yet (it fails loudly once it is). Implemented in oliviadb_core::typedkey (parts) and oliviadb_core::pathkey / Schema::set_key_parts (components), with the vectors at the end as a test; the fold collation is refused until a simple case folding table is part of OliviaDB. The native typed-node API over them is oliviadb_core::typednodes::TypedTree (paths of typed key values; put, insert, get, delete of a subtree, children in key order with a key-part prefix and paging, reads as of a version; unique keys only for now). Format 1 stays the format of every DL/I database: IMS compares sequence fields as raw bytes (a packed decimal field included), so DL/I keys must never be re-encoded.

Format 2 is for node types declared through the schema language (spec/schema.md) and used by the native API, the agent layers and the SQL projection. It adds what format 1 lacks: keys made of several typed parts, numbers that sort by value, strings that sort by a named collation, descending parts, and variable-length parts, all in one byte string that still sorts as plain bytes.

Where it applies

The key format is a property of a node type (key_format: 1 | 2 in the schema IR). A format-2 component is

component = code:u16be  bucket:b bytes  part+  [ ordinal ]

code, bucket and ordinal are as in format 1. The type's schema lists its key parts (at least one for a keyed type) with a type and a direction each. There is no type tag in the bytes: the schema says what comes next. Every part is self-delimiting (its length can be read from its own bytes) and the encoding of each part type is prefix-free (no encoded value is a prefix of another). Both properties together keep the format-1 rules: a node's key is a prefix of its descendants' keys, a subtree is one key range, and byte order of all keys is pre-order.

Part encodings (ascending)

part type encoding order
int (signed, up to 128 bits), uint zero: 0x80. Positive v: 0x80+n then the n bytes of v, big-endian, n minimal (1..16). Negative v: 0x80-n then the n bytes of |v| with every bit inverted, n minimal for |v| numeric
decimal(p,s) the scale s is fixed by the schema; the part is the unscaled integer (value · 10^s) as int. Minus zero is zero. p ≤ 38 fits in 16 bytes numeric
date days since 1970-01-01 as int chronological
timestamp microseconds since 1970-01-01T00:00:00Z as int; time zones are converted to UTC before encoding chronological
float64 IEEE 754 bits, big-endian; positive values (sign bit 0): sign bit set; negative values: all bits inverted. -0 is encoded as +0; NaN is refused numeric
bool 0x00 false, 0x01 true false first
bytes the bytes, each 0x00 written as 0x00 0xFF, then the terminator 0x00 0x01 bytewise
string(collation) the collation key of the string (below), escaped and terminated as bytes by collation
fixed bytes(n) n raw bytes (no escaping: the length comes from the schema) bytewise

Why the terminator is two bytes: a following part may start with any byte. With 0x00 0x01 as the end and 0x00 0xFF as a literal zero, the reader is never in doubt, and "ab" < "ab\0" < "ab\x01" holds (61 62 00 01 < 61 62 00 FF 00 01 < 61 62 01 00 01).

Collations

  • binary: the UTF-8 bytes (code point order). Reversible.
  • ebcdic(ccsid): the string's bytes in that EBCDIC code page, compared as bytes (the order a migrated program expects of a character key). Reversible.
  • fold: Unicode simple case folding, then UTF-8 bytes. Not reversible: the key gives the order and equality, the value as written is kept in the node's data.
  • Named locale collations (ICU or another library) are open. A collation has a version; a key encoded under one version may sort differently under the next, so the schema records the version and a change of version is a key rebuild (an online index-style backfill), never silent.

A key part whose collation is not reversible cannot be decoded back from the key; tools that print keys show the data's value instead.

Descending parts

A part declared desc is the ascending encoding with every byte inverted (b → 0xFF - b), terminator included. Inverting a prefix-free code reverses its order, and the result is still self-delimiting (0xFF 0xFE ends an inverted bytes part, 0xFF 0x00 is an inverted literal zero).

Composite keys

A key of several parts is the concatenation of its parts in the declared order, each in its own direction: [posted: timestamp desc, seq: int] sorts newest first and, within one timestamp, by seq ascending. Key parts are mandatory (no nulls). Optional key parts are open.

Test vectors

(The test oliviadb_core::typedkey::tests::spec_vectors.)

value type bytes (hex)
0 int 80
1 int 81 01
255 int 81 FF
256 int 82 01 00
-1 int 7F FE
-255 int 7F 00
-256 int 7E FE FF
12.50 decimal(9,2) 82 04 E2 (1250)
"" string(binary) 00 01
"a" string(binary) 61 00 01
"a\0" string(binary) 61 00 FF 00 01
"a" string(binary) desc 9E FF FE

Order check: -256 < -255 < -1 < 0 < 1 < 255 < 256 holds bytewise (7E.. < 7F 00 < 7F FE < 80 < 81 01 < 81 FF < 82 ..).

Decisions (format 2, version 1)

The open list of the draft, settled so that the encoding can be a contract with clients:

  • Length. The encoded key parts of one component are at most 4,096 bytes, and a whole path key (namespace and every component) at most 8,192 bytes. A write whose key exceeds either is refused with an error that names the type and the bound, before anything is written. For comparison, FoundationDB's keys are at most 10,000 bytes (FoundationDB documentation, "Known Limitations", "Large keys and values": https://apple.github.io/foundationdb/known-limitations.html). The core does not enforce this yet (docs/limits.md in the core lists it as open). Until it does, a client must accept longer keys.
  • Nulls. Key parts are mandatory. A schema that declares an optional key part is refused. A nullable part would need its own place in the order and a marker byte, which would be a format 2 version 2.
  • Collations. binary, ebcdic(ccsid) and fold (refused until the case folding table is part of OliviaDB) are the collations of version 1. A locale collation, when one is added, is a new collation name that carries its version, and a change of version rebuilds the keys. Neither changes the format.
  • Decimal scale. Fixed by the schema. A decimal part without a fixed scale is refused.
  • Format 2 under a format-1 parent. The component rules allow it, and a reader decodes each component by its own type's format. The schema checker refuses it in version 1, and allowing it later needs no change of the bytes.
  • Format 1 components stay what they are: key_len raw bytes. A client decodes them by the type's key field description.

Open

Nothing in format 2, version 1.