Expand description
Reader for the Erigon 3 seg state file format.
Erigon stores a snapshot of domain state (accounts, storage, code, …) as a triple
of sibling files sharing one base name, e.g. v1.1-accounts.0-1024.{kv,bt,kvei}:
.kv— the data: aseg-compressed stream of words. For a domain file the words alternatekey,value,key,value, … and the keys are sorted..bt— a B-tree index whose payload is an Elias-Fano array giving the.kvbyte offset of every key, enabling anO(log n)point lookup. Its newer layout also carries the key at everyM-th position, which narrows a lookup to a singleM-key block before any decompression happens (seeBtreeIndex::nodes)..kvei— an existence (bloom) filter: a negative accelerator. If it says a key is absent, the.btsearch can be skipped entirely. It never reports a real key as absent (no false negatives), so it is safe to trust for the negative case.
This crate currently implements reading and querying. Writing and merging are planned as later additions.
A domain’s state usually spans several files covering successive step ranges, where a
newer file overrides keys carried by an older one. KvStack wraps an ordered set of
KvReaders and resolves point lookups newest-first so overrides win.
§Quick start
use erigon_seg::{KvReader, Salt};
// Open a .kv and (if present) its sibling .bt / .kvei.
let mut r = KvReader::open("v1.1-accounts.0-1024.kv")?;
// The .kvei bloom needs the index salt to be useful; resolve it once.
r.enable_bloom(Salt::Find(8));
// Only worth setting when the data is large relative to RAM — see the method docs.
let _ = r.advise_random();
// Point lookup (bloom-accelerated if enabled, else B-tree binary search).
if let Some(value) = r.get(b"\x00\x01\x02")? {
println!("value = {} bytes", value.len());
}
// Or scan every key/value pair sequentially.
for kv in r.iter() {
let (key, value) = kv?;
let _ = (key, value);
}§Format notes
The reader handles both released on-disk layouts:
.kv: the legacyv0header (body at offset 0) and thev1header (a leading[version, feature-flags]pair, an optional page-compression byte, and optional out-of-band metadata)..bt: the legacy layout (Elias-Fano at offset 0) and the newer footer layout (a trailingerigon\0\0magic locating the Elias-Fano section)..kvei: theholiman/bloomfilterlayout (v02\nmagic). The newer “fuse filter” layout is detected and skipped (lookups remain correct, just unaccelerated).
Structs§
- BtOptions
- Options for building a
.btindex. - Btree
Index - A
.btindex: the Elias-Fano offset array plus, when known, the B-tree fanoutMand the di-node array used to narrow lookups. - Domain
Options - Options for
DomainWriter. - Domain
Paths - Paths written by
DomainWriter::finish. - Domain
Writer - Builds a domain file set from sorted
(key, value)pairs. - Elias
Fano - A read-only view of an Elias-Fano monotone sequence.
- Existence
Filter - A
.kveiexistence filter. - Getter
- A cursor over a
Seg’s words. Cheap to create; not thread-safe, so each thread makes its own from a shared&Seg. - KvIter
- Iterator over the
(key, value)pairs of aKvReader, in stored order. - KvReader
- A reader over one seg file set (
.kvdata, optional.btindex, optional.kveiexistence filter). - KvStack
- A stack of seg files for one domain, queried newest-first so overrides win.
- Kvei
Builder - Accumulates set bits for a
.kveibloom filter. - Merge
Options - Options for
merge. - Nodes
- The
.btdi-node array: the key at everyM-th position. - Open
Options - Options controlling how a
.kvis opened. - Seg
- A
seg-compressed file (.kv). Owns its mmap and dictionaries; create aGetterto read words. - SegWriter
- Builds a
seg.kvfile from a sequence of words.
Enums§
- BtLayout
- Which
.bton-disk layout to emit. - Error
- Anything that can go wrong while opening or reading a seg file set.
- Filter
Kind - What kind of filter a
.kveiturned out to be. - Salt
- How to obtain the
.kveiindex salt.
Constants§
- DEFAULT_
BTREE_ M - Default B-tree fanout (
DefaultBtreeM), the number of keys per leaf.
Functions§
- build_
bt - Build a
.btindex for a.kvfile, writing it tobt_path. - build_
bt_ from_ seg - Build a
.btindex from an already-openSeg. - build_
kvei - Build a
.kveiexistence filter for a.kvfile, writing it toout_path. - build_
kvei_ from_ seg - Build a
.kveifor every key in an already-openSeg, hashing withsalt. - merge
- Merge
inputs(domain.kvpaths) intoout_kv, also building the sibling.btand—ifopts.domain.saltis set—.kvei. - murmur3_
x64_ 128_ h1 - MurmurHash3 x64 128-bit, returning only the first 64-bit half (
h1) — the value Erigon feeds to the existence filter as the hashed key. - salt_
from_ file - Read an Erigon salt file (
salt-state.txt/salt-blocks.txt): a 4-byte big-endianu32. ReturnsNoneif the file is missing or too short.
Type Aliases§
- Result
- The crate result type.