Skip to main content

Crate fasm_storage

Crate fasm_storage 

Source
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: async get/set/delete/exists, a continuation-based range scan, and clear_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”.
  • MaybeSend and MaybeSync — 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), the flatdir module (the flat directory layout — a verbatim port of FoundationDB’s DirectoryLayer: tuple encoding, row layouts, HCA, directory ops) and the flatengine module (the layout engine over a RawKv view that the flat backends share).
  • kv_store_tests! (feature test-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 captures a full snapshot per session or reader, reuses FlatEngine, and applies each write buffer 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::DataBounds;
pub use flatengine::FlatEngine;
pub use flatengine::FlatError;
pub use rawkv::RawKv;

Modules§

flatdir
The flat directory layout: a verbatim port of FoundationDB’s DirectoryLayer (foundationdb crate, pinned 0.11.0).
flatengine
The flat directory layout engine over a RawKv view.
rawkv
The raw-byte view 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. dir must pass validate_dir; key is arbitrary bytes.
KvPair
A key-value pair produced by a range scan.
KvStream
An async stream of key-value pairs.
ScopedKvStore
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.
KvDirNav
Directory navigation over a KvStore.
KvStore
Async ordered key-value store, scoped by directory.
MaybeSend
A portability bound that requires Send on native targets.
MaybeSync
A portability bound that requires Sync on native targets.
RetryableStorageError
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, or None when 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.