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