Expand description
B+tree node encode / decode.
See docs/format.md § B+tree for the on-disk layout. This module
is the reference implementation; the tests at the bottom validate
round-trip equality and rejection of every documented invariant
violation.
§Power-of-ten posture
- Rule 2. Every loop iterates over
key_count, the slot directory length, or the page-byte range — all bounded byPAGE_SIZE. - Rule 3. The in-memory
DecodedNodeusesVecs, notheapless::Vecs of the worst-case slot count: the worst-case slot count times the worst-case per-key buffer (~1 MiB) would overflow a 2 MiB stack. The B+tree’s traversal stack usesheapless::Vecto satisfy Rule 3 on the hot read path; the decoded representation of a single node is a transient per-page artifact whose heap allocation is unavoidable and is not on a real-time path. - Rule 5.
validate_nodeis the central invariant check; it isdebug_assert!-ed at every mutation site and surfaces asError::Corruptionon decode of a malformed page. - Rule 7. No
unwrap/expecton any error-bearing path; length-prefixed varint reads returnError::Corruptionon underflow.
Structs§
- Decoded
Node - Decoded in-memory representation of a B+tree node.
- Internal
Entry - One internal-node pivot entry in the decoded in-memory
representation. The corresponding right child is stored in
DecodedNode::childrenat indexi + 1. - Leaf
Entry - One leaf slot in the decoded in-memory representation.
Enums§
- Node
Kind - Whether a node is an internal node or a leaf.
Constants§
- INTERNAL_
LEFTMOST_ CHILD_ BYTES - Bytes the leftmost-child page-id occupies in an internal node, immediately after the node header.
- INTERNAL_
SLOT_ BYTES - Bytes per slot in an internal slot directory. The entry is a u32 LE offset followed by a u64 LE right-child page-id.
- INTERNAL_
SLOT_ CAP - Maximum number of slots an internal node can hold at the worst case. Used to size the in-memory slot vector.
- LEAF_
SLOT_ BYTES - Bytes per slot in a leaf slot directory. The entry is a single u32 LE giving the byte offset of the corresponding heap entry.
- LEAF_
SLOT_ CAP - Maximum number of slots a leaf can hold at the worst-case empty key/value sizes. Used to size the in-memory slot vector.
- NODE_
HEADER_ SIZE - Fixed-size B+tree node header. See
docs/format.md§ Node header. - PAGE_
TYPE_ BTREE_ INTERNAL - Page-type tag for an internal B+tree node. See
docs/format.md§ Page types. - PAGE_
TYPE_ BTREE_ LEAF - Page-type tag for a leaf B+tree node.
Functions§
- decode_
node - Decode a B+tree page into a
DecodedNode. - encode_
node - Encode
nodeintopage. Stamps the page trailer. - max_
inline_ value - Maximum value length that still fits inline in a leaf alongside
at least one slot. See
docs/format.md§ Key and value encoding. - max_
key_ len - Maximum key length the format permits. See
docs/format.md§ Key and value encoding. - validate_
node - Validate
nodeagainst the format-version-0 invariants. - varint_
len - Number of bytes an unsigned LEB128 varint would use for
v.
Type Aliases§
- Child
Entry - Alias for “one of the right-child page-ids in an internal node”.
We store children as raw
u64s; the on-disk format permits0only as the leafnext_siblingsentinel, never as an internal child pointer.