fasm_storage/flatdir/mod.rs
1//! The flat directory layout: a verbatim port of FoundationDB's
2//! `DirectoryLayer` (`foundationdb` crate, pinned `0.11.0`).
3//!
4//! The layout and every algorithm here replicate what
5//! `DirectoryLayer::default()` does in a real FoundationDB keyspace —
6//! the node tree, the High Contention Allocator, the version row, the
7//! tuple byte encoding, the default layer placement — so that a store
8//! written by this layout and one written by the native layer agree on
9//! every row. The flat backends (btreemap, redb) execute it over a
10//! [`crate::rawkv::RawKv`] view; the FDB backend uses the native layer
11//! over the identical layouts.
12//!
13//! ## Row layouts
14//!
15//! | Row | Key | Value |
16//! |-----|-----|-------|
17//! | version | [`layout::VERSION_KEY`] | 12 bytes: major, minor, patch, little-endian `u32` (1.0.0) |
18//! | HCA counter | [`layout::COUNTERS_BASE`] ‖ packed `i64` window start | 8-byte LE allocation count |
19//! | HCA recent | [`layout::RECENT_BASE`] ‖ packed `i64` candidate | empty |
20//! | child | [`layout::child_key`] = node ‖ `i64(0)` ‖ STRING(segment) | the child's allocated prefix (raw bytes) |
21//! | layer | [`layout::layer_key`] = node ‖ BYTES("layer") | layer bytes (always empty here) |
22//! | data | allocated prefix ‖ key | the value |
23//!
24//! Key regions: `0x00`–`0xFD` data, `0xFE` directory nodes
25//! ([`layout::NODE_PREFIX`], root node [`layout::ROOT_NODE`]), `0xFF`
26//! reserved. Allocated prefixes are packed `i64`s (prefix-free, first
27//! byte `0x0c..=0x1c`), so data keys need no separator and cannot
28//! collide with meta or reserved rows.
29//!
30//! ## Single-writer adaptations
31//!
32//! The flat backends have exactly one writer, so the layer's
33//! transactional machinery degrades in documented ways (per module:
34//! [`hca`], [`ops`]): no write-conflict ranges, the counter increment is
35//! a read-modify-write landing at the atomic op's byte values, the RNG
36//! is caller-supplied (`thread_rng` in production), and a candidate
37//! claimed by a leftover recent row is retried rather than version-vector
38//! resolved. The row bytes and the operation order are unchanged.
39//!
40//! ## Fresh stores and versions
41//!
42//! A store with no version row is fresh: reads answer empty, and the
43//! first create writes the version row lazily (the layer's
44//! `check_version` behaviour). A store whose version row this layout
45//! does not read (newer major or minor) fails closed with
46//! [`error::FlatError::Foreign`].
47
48pub mod error;
49pub mod hca;
50pub mod layout;
51pub mod ops;
52pub mod tuple;
53
54/// Re-exports for the backends' direct touches (the open-time probe and
55/// the anchor mapping).
56pub use error::FlatError;
57pub use layout::{LAYOUT_VERSION, ROOT_PATH_SEGMENT, VERSION_KEY, parse_version};