Skip to main content

Module coldstate

Module coldstate 

Source
Expand description

Version index over cold history: for every object key, the ledgers whose state patches changed it, so that the object as of an old ledger can be read from the cold segments without a database.

The object as of ledger N is the patch of the last ledger at or before N that changed its key; an empty patch means the object was deleted there. A ledger number is enough to find its cold segment (Cold::find), so the index keeps no pointers into segments, only ledger numbers.

Layout of an index directory, written by build:

  • bucket-XX.xver for each first key byte XX: MAGIC, then blocks of zstd-compressed entries sorted by key, then a directory of the blocks, then a footer. An entry is the key (32 bytes), the number of ledgers (varint) and the ledgers as ascending deltas (varints, the first absolute). The directory holds, per block, its first key (32 bytes), offset (u64), stored length (u32), decoded length (u32) and the sha256 of its stored bytes (32 bytes); the footer holds the block count (u32), the directory offset (u64), the sha256 of the directory (32 bytes) and MAGIC. All integers are little-endian.
  • base.bin: the objects of the base state at ledger first - 1, which the cold patches do not carry: MAGIC, the base ledger (u32), the first and last indexed cold ledger (u32 each), the count (u32), then per object the key, the length (u32) and the bytes, in key order.
  • manifest.json, written last: format, first and last cold ledger, base ledger, and the sha256 of every file. Without it the index does not open, so a build that stopped half way is never read.

Damage is refused, never answered: opening checks every bucket’s directory against the sha256 in its footer, base.bin against the manifest, and the manifest’s ledger range against the copy in base.bin; every block read checks the block against its sha256 in the directory. verify checks every file against the manifest in full, for use before an index is attached.

The served range is from the base ledger to last. Building it is a single pass over the cold patches; pairs of key and ledger are held per bucket in memory and written to temporary files past spill_after pairs, then each bucket is sorted on its own, so memory stays bounded and the result does not depend on the threshold.

Structs§

Build
What to build an index of.
VersionIndex
An index opened for reading.

Constants§

FORMAT
Format version recorded in the manifest.
MAGIC
Magic and format version of every index file.

Functions§

build
Builds the index of b into dir and returns its manifest.
build_from_dirs
The command-line entry: builds from a cold directory into out with the given base state. Same result as build.
verify
Checks every file of the index in dir against the sha256 in its manifest, reading each file in full. Meant to run once before an index is attached; open and every read check their own parts on top of this.