Expand description
Order-preserving byte encoding for index keys.
See docs/format.md § Index key encoding for the authoritative
byte specification. This module is the reference implementation.
§Encoding contract
For two Dynamic values a and b,
encode_field(a) < encode_field(b) (lexicographic byte
comparison) iff a < b (semantic comparison) within their
shared type. Cross-type comparisons follow the tag ordering
documented in docs/format.md:
NULL < false < true < numeric < string < bytes.
The format is distinct from the tagged-Dynamic wire format in
crate::codec::dynamic: that format is round-trip-correct but
NOT order-preserving (negative integers, in particular, sort
wrong under varint encoding). The two encodings serve different
purposes — migration uses tagged-Dynamic for reflective access,
indexes use this module for lexicographic ordering.
§Non-unique key disambiguation
Standard, Each, and Composite indexes are non-unique: two
documents may share the same encoded user key. The M4 B+tree
rejects duplicate keys (Error::BTreeKeyExists); the index
maintenance path side-steps this by appending the document’s
Id (8 bytes big-endian) to the encoded user key before
writing into the B+tree. Unique indexes do not append
the suffix — a collision is the whole point.
encode_index_key returns the user key portion only; the
caller (the maintenance path in #58) is responsible for
appending the Id suffix for non-unique kinds. The
encoded_id_suffix_len constant is exported so range-scan
readers can trim the suffix back off.
§Power-of-ten posture
- Rule 1. No recursion: every encoder walks a flat
&[Dynamic]or a fixed-width primitive. - Rule 2. Per-field loops iterate over a slice whose length
was already validated against
IndexSpec::key_paths.len(). - Rule 5. Field-count mismatch is the runtime boundary; type
mismatch surfaces as
crate::Error::Codecon embedded NUL in aString. - Rule 7. No
unwrap/expecton the production path. Embedded-0x00in aStringfield surfaces ascrate::Error::InvalidArgument.
Structs§
- Encoded
Index Key - The byte representation of a single field encoded under the
order-preserving format documented in
docs/format.md.
Constants§
- COMPOSITE_
TAG - Tag byte distinguishing a composite key envelope from a single
primitive key. Pinned by
docs/format.md§ Index key encoding. - ENCODED_
ID_ SUFFIX_ LEN - Length in bytes of the
Idbig-endian suffix that the maintenance path appends to non-unique encoded keys. The suffix is the document’sIdas 8 bytes big-endian.
Functions§
- encode_
field - Encode a single
Dynamicvalue to its order-preserving byte representation. Used both by the publicencode_index_keyand by the composite encoder. - encode_
index_ key - Encode
fieldsinto a single index key under the kind-specific rules. - encode_
index_ key_ parts - Reference-based index-key encode (#84). Takes the index
kind(aCopydiscriminator) andkey_pathsBY REFERENCE so a caller holding ancrate::catalog::IndexDescriptorcan encode a lookup key WITHOUT cloningname/key_pathsinto a transientIndexSpec(and without re-runningIndexSpec::validate). - encoded_
id_ suffix_ len - Length of the trailing
Idsuffix appended to non-unique keys by the maintenance path. See module docs.