fasm-storage 0.1.1

Ordered async byte-oriented key-value storage: the KvStore/Commit traits and the Storage Scoping adapter shared by FASM state machines and their storage backends
Documentation
//! The raw-byte views the flat directory layout works over.
//!
//! A [`RawKv`] (synchronous) or [`AsyncRawKv`] (asynchronous) is a read/write
//! view over one store's raw (unstructured) byte map — no keyspace semantics,
//! just bytes. Synchronous flat backends implement [`RawKv`] over their
//! transaction views (the btreemap backend's buffered base+delta view; the
//! redb backend's read-write transaction) and route every directory-layout
//! operation through [`FlatEngine`](crate::flatengine::FlatEngine).
//! Asynchronous flat backends (such as browser IndexedDB) implement
//! [`AsyncRawKv`] and route through
//! [`AsyncFlatEngine`](crate::flatengine::AsyncFlatEngine), so the layout
//! logic is shared and tested once.
//!
//! All reads return **owned** bytes: the layout machinery decodes and
//! collects before returning, and the backends stream from that owned
//! result (which they keep alive in their stream handle), so no borrow
//! from the engine view escapes.

use core::future::Future;
use core::ops::Bound;

use crate::maybe_send::{MaybeSend, MaybeSync};
use crate::stream::KvPair;

/// A synchronous read/write view over a raw byte map.
///
/// Implementations must honour the empty-range contract: an `is_empty_range`
/// range (see [`crate::is_empty_range`]) yields no rows and performs no
/// mutation.
pub trait RawKv {
    /// The engine's own error type.
    type Error;

    /// Get one row.
    fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error>;

    /// Scan `start <= k < end` (both bounds per `Bound`), ascending or
    /// descending, returning owned raw rows as pairs.
    fn scan(
        &self,
        start: Bound<&[u8]>,
        end: Bound<&[u8]>,
        forward: bool,
    ) -> Result<Vec<KvPair>, Self::Error>;

    /// Insert (or overwrite) one row.
    fn insert(&mut self, key: &[u8], value: &[u8]) -> Result<(), Self::Error>;

    /// Delete one row (a no-op if absent).
    fn delete(&mut self, key: &[u8]) -> Result<(), Self::Error>;

    /// Delete every row in `start <= k < end`.
    fn clear_range(&mut self, start: Bound<&[u8]>, end: Bound<&[u8]>) -> Result<(), Self::Error>;
}

/// An asynchronous read/write view over a raw byte map.
///
/// Follows the same portability markers as [`KvStore`](crate::KvStore):
/// implementations and returned futures are `Send`/`Sync` on native targets
/// while remaining thread-local on browser `wasm32-unknown-unknown`.
///
/// Implementations must honour the empty-range contract: an `is_empty_range`
/// range (see [`crate::is_empty_range`]) yields no rows and performs no
/// mutation. A `limit` of `Some(0)` yields no rows.
pub trait AsyncRawKv: MaybeSend + MaybeSync {
    /// The engine's own error type.
    type Error: MaybeSend + MaybeSync;

    /// Get one row.
    fn get(
        &self,
        key: &[u8],
    ) -> impl Future<Output = Result<Option<Vec<u8>>, Self::Error>> + MaybeSend;

    /// Check whether one row exists.
    ///
    /// The default implementation goes through [`get`](Self::get). Backends
    /// with a key-only probe should override it to avoid transferring value
    /// bytes.
    fn exists(&self, key: &[u8]) -> impl Future<Output = Result<bool, Self::Error>> + MaybeSend {
        async move { Ok(self.get(key).await?.is_some()) }
    }

    /// Scan `start <= k < end` (both bounds per `Bound`), ascending or
    /// descending, returning at most `limit` owned raw rows as pairs
    /// (`None` means unbounded).
    fn scan(
        &self,
        start: Bound<&[u8]>,
        end: Bound<&[u8]>,
        forward: bool,
        limit: Option<usize>,
    ) -> impl Future<Output = Result<Vec<KvPair>, Self::Error>> + MaybeSend;

    /// Insert (or overwrite) one row.
    fn insert(
        &mut self,
        key: &[u8],
        value: &[u8],
    ) -> impl Future<Output = Result<(), Self::Error>> + MaybeSend;

    /// Delete one row (a no-op if absent).
    fn delete(&mut self, key: &[u8]) -> impl Future<Output = Result<(), Self::Error>> + MaybeSend;

    /// Delete every row in `start <= k < end`.
    fn clear_range(
        &mut self,
        start: Bound<&[u8]>,
        end: Bound<&[u8]>,
    ) -> impl Future<Output = Result<(), Self::Error>> + MaybeSend;
}