Skip to main content

Module segment

Module segment 

Source
Expand description

Segments: immutable binary snapshots of a layer.

A segment is one object, <namespace>/segments/<lo>-<hi>/data.bin, holding the net effect of WAL entries lo through hi. The id carries the range, so the manifest can be checked for contiguity from the ids alone, and the bytes are a pure function of the range: any compactor building the same range produces the same object, which is what lets a rerun or a racing compactor reuse an existing one.

Three formats exist, and decode reads all of them. Version 1 is a flat run of sections. Version 2 (in the private v2 module) holds the same content in blocks with a directory and a Bloom filter at the end, so a reader can fetch pieces of it. Version 3 (the same module with columns and zone) stores a typed namespace’s attributes as columns per block with a zone map beside them. encode_with_schema writes any of them as compaction does, and the namespace’s segment_format level decides which one compaction writes.

Ids and tombstones are sorted, strictly ascending, and never overlap. decode checks every length against the bytes that remain before allocating and never panics.

Structs§

Matrix
A dense rows x dim matrix of f32, row-major, one allocation for the whole thing. RangedSegment::vectors_of returns this instead of Vec<Vec<f32>> (one heap allocation per row) so a rerank walks contiguous memory and the distance kernels see one slice per row of the same buffer. A scan hands one out per batch.

Constants§

DEFAULT_FORMAT
The segment level a namespace created by this build starts at, which can trail FORMAT_VERSION: a new version is readable first and written by default only once it pays off (version 3 is opt-in until its zone maps are used).
FORMAT_VERSION
The newest segment version this build reads, and the highest level Namespace::upgrade_segment_format accepts. decode rejects a version above it (and 0) with ItzmaError::UnsupportedFormat. A change to an encoding must add a version, because correctness relies on two builders producing identical bytes.

Functions§

decode
Decode a segment object (version 2 or 3) for a namespace whose vectors have length dim. path is only used to label errors.
encode
Encode a layer as a segment object of the version a new namespace writes (DEFAULT_FORMAT) for vectors of length dim. See encode_as.
encode_as
Encode a layer as a segment object of version (1, 2 or 3) for vectors of length dim, with attributes stored as JSON. At version 3 that is the layout of a free-form namespace; a typed namespace’s segments are what encode_with_schema writes.
encode_with_schema
Encode a layer as the segment object compact writes at version for a namespace with schema: at version 3 a typed namespace’s attributes become columns and zone maps. Versions 1 and 2 ignore the attribute schema, so this equals encode_as there.
parse_segment_id
Recover (lo, hi) from a segment id, or None unless the id is exactly what segment_id produces and 1 <= lo <= hi.
segment_id
The id of the segment covering WAL seqs lo through hi: both numbers zero-padded to 20 digits, so ids sort in range order.
segment_path
Object key of a segment: <namespace>/segments/<id>/data.bin. The directory leaves room for index files beside the data later.
validate_chain
Check the manifest invariant on its segment list: every id parses, the first range starts at 1, each next range starts one past the previous end, and the last range ends at folded_through. An empty list requires folded_through == 0.