Skip to main content

nectar_primitives/store/
mod.rs

1//! Chunk storage traits and implementations.
2//!
3//! Async traits (`ChunkGet`, `ChunkPut`, `ChunkHas`) are the primary API.
4//! Sync traits (`SyncChunkGet`, `SyncChunkPut`, `SyncChunkHas`) are helpers
5//! for CPU-bound paths (splitter, mantaray). Blanket impls bridge sync → async
6//! automatically for types that are `Send + Sync`.
7
8mod memory;
9mod typed;
10
11pub use memory::MemoryStore;
12pub use typed::{ChunkGet, ChunkHas, ChunkPut, SyncChunkGet, SyncChunkHas, SyncChunkPut};
13
14use crate::bmt::DEFAULT_BODY_SIZE;
15use crate::chunk::{AnyChunk, ChunkAddress};
16
17/// Errors from chunk storage operations.
18#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
19pub enum ChunkStoreError {
20    /// Chunk not found at the given address.
21    #[error("chunk not found: {address_hex}")]
22    NotFound {
23        /// Hex-encoded address of the missing chunk.
24        address_hex: String,
25    },
26    /// Catch-all for backend-specific errors.
27    #[error("{0}")]
28    Other(String),
29}
30
31impl ChunkStoreError {
32    /// Create a `NotFound` error for the given address.
33    pub fn not_found(address: &ChunkAddress) -> Self {
34        Self::NotFound {
35            address_hex: format!("{address}"),
36        }
37    }
38}
39
40/// A no-op loader that always returns [`ChunkStoreError::NotFound`].
41///
42/// Used by `Node`'s public convenience methods to satisfy the generic
43/// constraint without requiring callers to specify a store type.
44#[derive(Debug)]
45pub struct NullLoader<const BODY_SIZE: usize = DEFAULT_BODY_SIZE>;
46
47impl<const BODY_SIZE: usize> SyncChunkGet<BODY_SIZE> for NullLoader<BODY_SIZE> {
48    type Error = ChunkStoreError;
49
50    fn get(&self, address: &ChunkAddress) -> Result<AnyChunk<BODY_SIZE>, Self::Error> {
51        Err(ChunkStoreError::not_found(address))
52    }
53}