Skip to main content

ant_node/storage/
mod.rs

1//! Storage subsystem for chunk persistence.
2//!
3//! This module provides content-addressed LMDB storage for chunks,
4//! along with a protocol handler that integrates with saorsa-core's
5//! `Protocol` trait for automatic message routing.
6//!
7//! # Architecture
8//!
9//! ```text
10//! ┌─────────────────────────────────────────────────────────┐
11//! │        AntProtocol (implements Protocol trait)        │
12//! ├─────────────────────────────────────────────────────────┤
13//! │  protocol_id() = "autonomi.ant.chunk.v1"                  │
14//! │                                                         │
15//! │  handle(peer_id, data) ──▶ decode AntProtocolMessage │
16//! │                                   │                     │
17//! │         ┌─────────────────────────┼─────────────────┐  │
18//! │         ▼                         ▼                 ▼  │
19//! │   QuoteRequest           ChunkPutRequest    ChunkGetRequest
20//! │         │                         │                 │  │
21//! │         ▼                         ▼                 ▼  │
22//! │   QuoteGenerator          PaymentVerifier   LmdbStorage│
23//! │         │                         │                 │  │
24//! │         └─────────────────────────┴─────────────────┘  │
25//! │                           │                             │
26//! │                 return Ok(Some(response_bytes))         │
27//! └─────────────────────────────────────────────────────────┘
28//! ```
29//!
30//! # Example
31//!
32//! ```rust,ignore
33//! use std::sync::Arc;
34//! use ant_node::storage::{AntProtocol, LmdbStorage, LmdbStorageConfig};
35//!
36//! // Create storage
37//! let config = LmdbStorageConfig::default();
38//! let storage = Arc::new(LmdbStorage::new(config).await?);
39//!
40//! // Create protocol handler
41//! let protocol = AntProtocol::new(storage, Arc::new(payment_verifier), Arc::new(quote_generator));
42//!
43//! // Register with saorsa-core
44//! listener.register_protocol(protocol).await?;
45//! ```
46
47pub(crate) mod chunk_store;
48#[cfg(any(test, feature = "test-utils"))]
49pub mod file_store;
50#[cfg(not(any(test, feature = "test-utils")))]
51pub(crate) mod file_store;
52mod handler;
53pub(crate) mod lmdb;
54pub mod migration;
55pub(crate) mod migration_signal;
56
57pub use crate::ant_protocol::XorName;
58pub use chunk_store::{ChunkStore, ChunkStoreConfig, VerifyReport, LEGACY_ENV_DIR};
59pub use file_store::{FileStore, FileStoreConfig, StoreLayout};
60pub use handler::AntProtocol;
61pub(crate) use handler::ChunkRequestContext;
62pub(crate) use lmdb::CapacityVerdict;
63pub use lmdb::{LmdbStorage, LmdbStorageConfig};
64pub use migration::{MigrationConfig, MigrationPhase, MigrationState};
65
66/// Bytes in one MiB.
67pub const MIB: u64 = 1024 * 1024;
68
69/// Bytes in one GiB.
70pub const GIB: u64 = 1024 * MIB;
71
72/// Default free disk space to keep unused on the storage partition.
73pub const DEFAULT_DISK_RESERVE: u64 = 500 * MIB;
74
75/// Statistics about storage operations.
76///
77/// Counters other than `current_chunks` are cumulative for the lifetime of the
78/// process; `current_chunks` is the live count.
79#[derive(Debug, Clone, Default)]
80pub struct StorageStats {
81    /// Total number of chunks stored.
82    pub chunks_stored: u64,
83    /// Total number of chunks retrieved.
84    pub chunks_retrieved: u64,
85    /// Total bytes stored.
86    pub bytes_stored: u64,
87    /// Total bytes retrieved.
88    pub bytes_retrieved: u64,
89    /// Number of duplicate writes (already exists).
90    pub duplicates: u64,
91    /// Number of verification failures on read.
92    pub verification_failures: u64,
93    /// Number of chunks currently persisted.
94    pub current_chunks: u64,
95}