Expand description
Ordered async byte-oriented key-value storage.
This is the lowest storage layer for FASM state machines: an ordered map from byte keys to byte values, plus the two things a state machine needs on top of one — continuation-based range scans and an atomic commit boundary. It knows nothing about swaps, rows, or serialization; those live in the crates above it.
§The layering
state machine typed rows, indexes, transitions
▼
high-level state traits Result<Option<T>> getters over a key schema
▼
KvStore + Commit ← this crate: ordered async bytes
▼
BTreeMapStore · redb tx · FoundationDB tx · IndexedDB session (browser wasm32-unknown-unknown)§What this crate provides
KvStore— the backend trait: asyncget/set/delete/exists, a continuation-basedrangescan, andclear_range. Keys are ordered lexicographically by byte value, always.KvStream— the consuming continuation a scan returns, which enables incremental backends without requiring a particular fetching strategy.Commit— a separate, consuming trait marking the atomic boundary. One state-transition invocation is one session is one commit.RetryableStorageError— the contract every storage error answers, so the layer above can tell “rerun the transition” from “fail closed”.MaybeSendandMaybeSync— target-aware portability markers that preserve native thread-safety while permitting browser storage handles.ScopedKvStore— a directory-pinned view over any backend: the caller names keys only, every op lands in one directory; its inner-store escape hatches are deliberate, not a capability boundary.- the keyspace helpers
next_prefix,prefix_range,is_empty_range,bound_to_owned,bound_as_slice— the pure bound arithmetic every backend and key schema shares. - the directory-native key support:
Key(the owned(dir, key)form),validate_dir+KeyError(the trait-level UTF-8 directory contract),KvDirNav(directory navigation —list_dirs/dir_exists/remove_dir), theflatdirmodule (the flat directory layout — a verbatim port of FoundationDB’sDirectoryLayer: tuple encoding, row layouts, HCA, directory ops) and theflatenginemodule (the synchronousFlatEngineoverRawKvand asynchronousAsyncFlatEngineoverAsyncRawKvthat the flat backends share). kv_store_tests!(featuretest-utils) — the conformance suite every backend must pass, so key schemas can be written once against one set of ordering and bound answers.
§Backends
The trait and the plumbing live here; the concrete backends live in sibling
crates: fasm-storage-btreemap (in-memory, tests and simulations only),
fasm-storage-redb, fasm-storage-fdb, and fasm-storage-indexeddb
(browser, wasm32-unknown-unknown). The IndexedDB backend pins each
session or reader to the database revision on open, serves reads and
bounded range scans directly from IndexedDB against that revision fence
through AsyncFlatEngine, buffers session writes and range deletes
locally, and applies them in one revision-fenced readwrite commit using
?Send browser futures. The conformance suite runs in the browser through
its test_attr arm. The target-aware portability markers preserve the
native backends’ thread-safety contract while allowing browser storage
handles and futures.
§Example
The shape below is what every backend — and therefore every state machine
on top — must provide. It is written generically over the backend so this
documentation does not depend on any particular one; the concrete in-memory
usage lives in fasm-storage-btreemap.
use std::ops::Bound;
use fasm_storage::{Commit, KvStore, ScopedKvStore};
/// One invocation of a state transition is one session is one commit.
/// `swap` is pinned to one directory; the relative directory argument
/// is empty, so every operation addresses the pinned directory itself.
async fn transition<S>(mut swap: ScopedKvStore<S>) -> Result<(), <S as Commit>::Error>
where
S: KvStore + Commit,
{
// Multi-key writes under a single session.
let _ = swap.set(&[], b"status", b"funded").await;
let _ = swap.range(&[], Bound::Unbounded, Bound::Unbounded, false).collect().await;
// Finalize the session atomically.
swap.commit().await?;
Ok(())
}Re-exports§
pub use flatengine::AsyncFlatEngine;pub use flatengine::DataBounds;pub use flatengine::FlatEngine;pub use flatengine::FlatError;pub use rawkv::AsyncRawKv;pub use rawkv::RawKv;
Modules§
- flatdir
- The flat directory layout: a verbatim port of FoundationDB’s
DirectoryLayer(foundationdbcrate, pinned0.11.0). - flatengine
- The flat directory layout engines over
RawKvandAsyncRawKvviews. - rawkv
- The raw-byte views the flat directory layout works over.
Structs§
- Key
- The owned
(dir, key)form, for callers that hold a key across async boundaries, in contrast to the borrowed forms the store operations take.dirmust passvalidate_dir;keyis arbitrary bytes. - KvPair
- A key-value pair produced by a range scan.
- KvStream
- An async stream of key-value pairs.
- Scoped
KvStore - A directory-pinned view of another
KvStore.
Enums§
- KeyError
- A directory path failed validation.
Traits§
- Commit
- Finalizes the writes performed through a mutable storage handle.
- KvDir
Nav - Directory navigation over a
KvStore. - KvStore
- Async ordered key-value store, scoped by directory.
- Maybe
Send - A portability bound that requires
Sendon native targets. - Maybe
Sync - A portability bound that requires
Syncon native targets. - Retryable
Storage Error - Error contract for storage failures that can be safely retried by discarding the current logical attempt and rerunning it from the start.
Functions§
- bound_
as_ slice - Borrow an owned bound as a slice bound.
- bound_
to_ owned - Copy a borrowed bound into an owned one.
- is_
empty_ range - Whether a range is empty solely from its bounds.
- next_
prefix - Returns the shortest byte string that sorts strictly after every key
beginning with
prefix, orNonewhen no such string exists. - prefix_
range - Returns the bound pair that matches exactly the keys carrying
prefix. - validate_
dir - Validate a directory path: every segment must be valid UTF-8.