Skip to main content

horon_engine/
config.rs

1//! config.rs - Configuration System for horon-engine storage
2//!
3//! Configuration for the Hyperbolic Tree Tensor (HTT) storage system.
4//!
5//! Options for:
6//! - Hyperbolic space dimensionality
7//! - Memory management and caching
8//! - Persistence configuration
9
10use std::fmt::{self, Debug, Formatter};
11use g_math::fixed_point::FixedPoint;
12
13/// Configuration for HTT storage.
14#[derive(Clone)]
15pub struct HTTStorageConfig {
16    /// Dimension of the hyperbolic space
17    pub dimension: usize,
18    /// Maximum in-memory nodes before flushing to storage
19    pub max_memory_nodes: usize,
20    /// Cache size for frequently accessed nodes
21    pub cache_size: usize,
22    /// Persistent storage path (if used)
23    pub storage_path: Option<String>,
24    /// Flush interval in seconds (if persistence is enabled)
25    pub flush_interval: u64,
26    /// Whether to optimize on shutdown
27    pub optimize_on_shutdown: bool,
28    /// Sarkar embedding scale factor τ (zero = use default of 1.0)
29    pub tau: FixedPoint,
30}
31
32impl HTTStorageConfig {
33    /// Create a new HTT storage configuration.
34    pub fn new(
35        dimension: usize,
36        max_memory_nodes: usize,
37        cache_size: usize,
38        storage_path: Option<String>,
39        flush_interval: u64,
40        optimize_on_shutdown: bool,
41    ) -> Self {
42        Self {
43            dimension,
44            max_memory_nodes,
45            cache_size,
46            storage_path,
47            flush_interval,
48            optimize_on_shutdown,
49            tau: FixedPoint::from_int(0),
50        }
51    }
52
53    /// Check if persistence is enabled.
54    pub fn is_persistence_enabled(&self) -> bool {
55        self.storage_path.is_some()
56    }
57}
58
59impl Default for HTTStorageConfig {
60    fn default() -> Self {
61        Self {
62            dimension: 4,
63            max_memory_nodes: 1000,
64            cache_size: 100,
65            storage_path: None,
66            flush_interval: 60,
67            optimize_on_shutdown: true,
68            tau: FixedPoint::from_int(0),
69        }
70    }
71}
72
73impl Debug for HTTStorageConfig {
74    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
75        f.debug_struct("HTTStorageConfig")
76            .field("dimension", &self.dimension)
77            .field("max_memory_nodes", &self.max_memory_nodes)
78            .field("cache_size", &self.cache_size)
79            .field("storage_path", &self.storage_path)
80            .field("flush_interval", &self.flush_interval)
81            .field("optimize_on_shutdown", &self.optimize_on_shutdown)
82            .field("tau", &self.tau)
83            .finish()
84    }
85}
86
87/// Builder for HTT storage configuration.
88pub struct HTTStorageConfigBuilder {
89    config: HTTStorageConfig,
90}
91
92impl HTTStorageConfigBuilder {
93    /// Create a new builder with defaults.
94    pub fn new() -> Self {
95        Self {
96            config: HTTStorageConfig::default(),
97        }
98    }
99
100    /// Set the dimension.
101    pub fn dimension(mut self, dimension: usize) -> Self {
102        self.config.dimension = dimension;
103        self
104    }
105
106    /// Set the maximum in-memory nodes.
107    pub fn max_memory_nodes(mut self, max_memory_nodes: usize) -> Self {
108        self.config.max_memory_nodes = max_memory_nodes;
109        self
110    }
111
112    /// Set the cache size.
113    pub fn cache_size(mut self, cache_size: usize) -> Self {
114        self.config.cache_size = cache_size;
115        self
116    }
117
118    /// Set the storage path.
119    pub fn storage_path(mut self, storage_path: Option<String>) -> Self {
120        self.config.storage_path = storage_path;
121        self
122    }
123
124    /// Set the flush interval.
125    pub fn flush_interval(mut self, flush_interval: u64) -> Self {
126        self.config.flush_interval = flush_interval;
127        self
128    }
129
130    /// Set optimize on shutdown.
131    pub fn optimize_on_shutdown(mut self, optimize_on_shutdown: bool) -> Self {
132        self.config.optimize_on_shutdown = optimize_on_shutdown;
133        self
134    }
135
136    /// Set the Sarkar embedding scale factor τ (zero = use default of 1.0).
137    pub fn tau(mut self, tau: FixedPoint) -> Self {
138        self.config.tau = tau;
139        self
140    }
141
142    /// Build the configuration.
143    pub fn build(self) -> HTTStorageConfig {
144        self.config
145    }
146}
147
148#[cfg(test)]
149mod tests {
150    use super::*;
151
152    #[test]
153    fn test_config_default() {
154        let config = HTTStorageConfig::default();
155
156        assert_eq!(config.dimension, 4);
157        assert_eq!(config.max_memory_nodes, 1000);
158        assert_eq!(config.cache_size, 100);
159        assert_eq!(config.storage_path, None);
160        assert_eq!(config.flush_interval, 60);
161        assert_eq!(config.optimize_on_shutdown, true);
162    }
163
164    #[test]
165    fn test_config_builder() {
166        let config = HTTStorageConfigBuilder::new()
167            .dimension(8)
168            .max_memory_nodes(2000)
169            .cache_size(200)
170            .storage_path(Some("/tmp/htt".to_string()))
171            .flush_interval(120)
172            .optimize_on_shutdown(false)
173            .build();
174
175        assert_eq!(config.dimension, 8);
176        assert_eq!(config.max_memory_nodes, 2000);
177        assert_eq!(config.cache_size, 200);
178        assert_eq!(config.storage_path, Some("/tmp/htt".to_string()));
179        assert_eq!(config.flush_interval, 120);
180        assert_eq!(config.optimize_on_shutdown, false);
181    }
182
183    #[test]
184    fn test_persistence_enabled() {
185        let config1 = HTTStorageConfigBuilder::new()
186            .storage_path(Some("/tmp/htt".to_string()))
187            .build();
188        assert!(config1.is_persistence_enabled());
189
190        let config2 = HTTStorageConfigBuilder::new()
191            .storage_path(None)
192            .build();
193        assert!(!config2.is_persistence_enabled());
194    }
195}