Skip to main content

Module blocks

Module blocks 

Source
Expand description

Block storage for ledgers: random access to any ledger without decompressing the whole archive.

Compressing each ledger separately is expensive: zstd loses the repetition between neighbouring ledgers (measured on the validation archive: 32% larger than a whole segment). Compressing a whole segment loses random access. The compromise is a block of block_ledgers consecutive ledgers, optionally with a zstd dictionary: reading a ledger means decompressing one block.

Segment file blk-<first>.xwlb: magic XWLB\0\1, then blocks: u32 compressed length | compressed. A decompressed block: u32 n, n pairs (u32 sequence, u32 length), then the records back to back.

Index blk-<first>.xwlb.idx: magic XWLI\0\1, then for each block u32 first sequence | u32 n | u64 offset | u32 compressed length. The index is kept in memory: for a million ledgers in blocks of 16 it is under 2 MB.

The reader keeps the last few decompressed blocks: the history of an active account touches consecutive ledgers, and decompressing a block (32 ledgers, a few megabytes) costs more than searching within it.

The dictionary, if any, is dict.zdict in the same directory. Blocks cannot be read without it, so it is written before the first block.

A block goes into the index only after the block itself is on disk (fsync), so a segment tail torn by a crash has no index entry and is never read.

Structs§

BlockReader
Reads by ledger sequence.
BlockWriter
Writer: collects ledgers into a block and flushes the block when it is full.

Functions§

train_dictionary
A zstd dictionary trained on sample ledgers.