use ipfrs_core::Cid;
use ipfrs_interface::metrics::IpfrsMetrics;
use ipfrs_network::NetworkNode;
use ipfrs_semantic::{DistanceMetric, SemanticRouter};
use ipfrs_storage::{CachedBlockStore, SledBlockStore};
use ipfrs_tensorlogic::TensorLogicStore;
use once_cell::sync::OnceCell;
use std::sync::Arc;
use std::time::SystemTime;
use crate::auth::AuthManager;
use crate::pin::PinManager;
use crate::tls::TlsManager;
mod auth_ops;
mod block_ops;
mod core;
mod dag_ops;
mod network_ops;
mod pin_ops;
mod repo_ops;
mod semantic_ops;
mod tensorlogic_ops;
pub use core::{NodeConfig, NodeStatus};
pub use tensorlogic_ops::DistributedInferResult;
pub(super) type NodeStore = CachedBlockStore<SledBlockStore>;
pub struct Node {
pub(super) config: NodeConfig,
pub(super) network: Option<NetworkNode>,
pub(super) storage: Option<Arc<NodeStore>>,
pub(super) semantic: OnceCell<Arc<SemanticRouter>>,
pub(super) tensorlogic: OnceCell<Arc<TensorLogicStore<NodeStore>>>,
pub(super) auth_manager: Option<Arc<AuthManager>>,
pub(super) tls_manager: Option<Arc<TlsManager>>,
pub(super) pin_manager: Arc<PinManager>,
pub(super) startup_time: Option<SystemTime>,
pub metrics: Arc<IpfrsMetrics>,
}
impl Node {
pub(super) fn storage(&self) -> ipfrs_core::Result<&Arc<NodeStore>> {
self.storage.as_ref().ok_or_else(|| {
ipfrs_core::Error::Initialization("Node not started - call start() first".to_string())
})
}
pub(super) fn semantic(&self) -> ipfrs_core::Result<&Arc<SemanticRouter>> {
use ipfrs_semantic::SemanticRouter as SR;
if !self.config.enable_semantic {
return Err(ipfrs_core::Error::Initialization(
"Semantic routing not enabled - set enable_semantic=true in config".to_string(),
));
}
self.semantic
.get_or_try_init(|| SR::new(self.config.semantic.clone()).map(Arc::new))
}
pub(super) fn tensorlogic(&self) -> ipfrs_core::Result<&Arc<TensorLogicStore<NodeStore>>> {
if !self.config.enable_tensorlogic {
return Err(ipfrs_core::Error::Initialization(
"TensorLogic not enabled - set enable_tensorlogic=true in config".to_string(),
));
}
let storage = self.storage()?;
self.tensorlogic
.get_or_try_init(|| TensorLogicStore::new(storage.clone()).map(Arc::new))
}
pub(super) fn network(&self) -> ipfrs_core::Result<&NetworkNode> {
self.network.as_ref().ok_or_else(|| {
ipfrs_core::Error::Initialization("Node not started - call start() first".to_string())
})
}
pub(super) fn network_mut(&mut self) -> ipfrs_core::Result<&mut NetworkNode> {
self.network.as_mut().ok_or_else(|| {
ipfrs_core::Error::Initialization("Node not started - call start() first".to_string())
})
}
}
#[derive(Debug, Clone)]
pub struct StorageStats {
pub num_blocks: usize,
pub is_empty: bool,
pub dedup: ipfrs_storage::DeduplicationStatsSnapshot,
}
#[derive(Debug, Clone)]
pub struct BlockStat {
pub cid: Cid,
pub size: usize,
}
#[derive(Debug, Clone)]
pub struct SemanticStats {
pub num_vectors: usize,
pub dimension: usize,
pub metric: DistanceMetric,
pub cache_size: usize,
pub cache_capacity: usize,
}
#[derive(Debug, Clone)]
pub struct TensorLogicStats {
pub enabled: bool,
pub num_facts: usize,
pub num_rules: usize,
}
pub use ipfrs_storage::OrphanGcResult;
#[derive(Debug, Clone)]
pub struct GcResult {
pub blocks_collected: u64,
pub bytes_freed: u64,
pub blocks_marked: u64,
pub blocks_scanned: u64,
pub duration: std::time::Duration,
pub cancelled: bool,
}
#[derive(Debug, Clone)]
pub struct FsckResult {
pub blocks_checked: u64,
pub blocks_valid: u64,
pub blocks_corrupt: Vec<Cid>,
pub blocks_missing: Vec<Cid>,
}
#[derive(Debug, Clone)]
pub struct DagExportStats {
pub blocks_exported: u64,
pub bytes_exported: u64,
}
#[derive(Debug, Clone)]
pub struct DagImportStats {
pub blocks_imported: u64,
pub bytes_imported: u64,
}