Skip to main content

storage_kit/backend/
mod.rs

1//! Backend traits and implementations — the transport layer over this
2//! crate's own format-v3 storage engine ([`crate::store`]).
3//!
4//! **Why this trait is async and [`crate::store::ImmutableStore`] is not:**
5//! `ImmutableStore` is deliberately synchronous — it models local, blocking
6//! disk I/O, which is the honest cost of the local-first reference backend.
7//! `S3Backend`/`HttpBackend` are inherently network-bound; forcing them
8//! through a sync trait would mean either blocking a runtime thread on
9//! every call or silently spinning up a throwaway executor per call.
10//! Neither is acceptable for a Kit meant to be embedded in async host
11//! applications (Refraction, Quickring Courier).
12//!
13//! `AsyncImmutableStore` is the uniform contract all three backends here
14//! implement. `LocalBackend` is a thin async wrapper over
15//! [`crate::store::PartitionStore`] (dispatched via `spawn_blocking`, since
16//! local disk I/O is legitimately blocking); it does **not** reimplement
17//! the packfile/fragmentation engine — that lives once, in [`crate::store`]
18//! / [`crate::fragment`], and this module only wraps it. (Prior to
19//! 2026-08-23 this engine lived in `bitchain` and this crate depended on it
20//! cross-org; that dependency inverted when the engine relocated here.)
21
22// `http` and `s3` are feature-gated (A4, standalone-first partitions,
23// 2026-09-22): both cloud transports pull in real network-client
24// dependencies (`reqwest`, `aws-sdk-s3`/`aws-config`), so a local-only
25// consumer can build with `default-features = false` and link neither.
26// `local` stays unconditional — it has no cloud dependency and is the
27// default, no-network reference backend.
28#[cfg(feature = "http")]
29pub mod http;
30pub mod local;
31#[cfg(feature = "s3")]
32pub mod s3;
33
34#[cfg(feature = "http")]
35pub use http::HttpBackend;
36pub use local::LocalBackend;
37#[cfg(feature = "s3")]
38pub use s3::S3Backend;
39
40use crate::addressing::{Hash, PartitionId};
41use crate::error::StorageError;
42use async_trait::async_trait;
43
44/// The uniform async storage contract every backend in this crate honors.
45/// Mirrors [`crate::store::ImmutableStore`]'s semantics (content-addressed,
46/// partition-scoped, hash-verified-on-read, dedup-by-hash-on-write) but
47/// over `async fn` so remote transports don't have to fake synchrony.
48#[async_trait]
49pub trait AsyncImmutableStore: Send + Sync {
50    /// Fetch and verify content by hash. Implementations MUST verify the
51    /// returned bytes hash to `hash` before returning `Ok`.
52    async fn get(&self, hash: &Hash) -> Result<Vec<u8>, StorageError>;
53
54    /// Store `data`, returning its content hash. A `put` for content
55    /// already present is a no-op dedup hit, not a duplicate write.
56    async fn put(&self, data: &[u8]) -> Result<Hash, StorageError>;
57
58    async fn has(&self, hash: &Hash) -> Result<bool, StorageError>;
59
60    fn partition_id(&self) -> PartitionId;
61}