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;
56pub(crate) mod traffic;
57
58pub use crate::ant_protocol::XorName;
59pub use chunk_store::{ChunkStore, ChunkStoreConfig, VerifyReport, LEGACY_ENV_DIR};
60pub(crate) use file_store::{rename_with_retry, Reservation};
61pub use file_store::{FileStore, FileStoreConfig, StoreLayout};
62pub use handler::AntProtocol;
63pub(crate) use handler::ChunkRequestContext;
64pub use handler::SELF_CLOSENESS_GATE_WIDTH;
65pub(crate) use lmdb::CapacityVerdict;
66pub use lmdb::{LmdbStorage, LmdbStorageConfig};
67pub use migration::{MigrationConfig, MigrationPhase, MigrationState};
68
69/// Bytes in one MiB.
70pub const MIB: u64 = 1024 * 1024;
71
72/// Bytes in one GiB.
73pub const GIB: u64 = 1024 * MIB;
74
75/// Default free disk space to keep unused on the storage partition.
76pub const DEFAULT_DISK_RESERVE: u64 = 500 * MIB;
77
78/// Statistics about storage operations.
79///
80/// Counters other than `current_chunks` are cumulative for the lifetime of the
81/// process; `current_chunks` is the live count.
82#[derive(Debug, Clone, Default)]
83pub struct StorageStats {
84    /// Total number of chunks stored.
85    pub chunks_stored: u64,
86    /// Total number of chunks retrieved.
87    pub chunks_retrieved: u64,
88    /// Total bytes stored.
89    pub bytes_stored: u64,
90    /// Total bytes retrieved.
91    pub bytes_retrieved: u64,
92    /// Number of duplicate writes (already exists).
93    pub duplicates: u64,
94    /// Number of verification failures on read.
95    pub verification_failures: u64,
96    /// Number of chunks currently persisted.
97    pub current_chunks: u64,
98}