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    /// Point location grid resolution (cells per axis, 0 = use default)
29    pub grid_resolution: usize,
30    /// Sarkar embedding scale factor τ (zero = use default of 1.0)
31    pub tau: FixedPoint,
32}
33
34impl HTTStorageConfig {
35    /// Create a new HTT storage configuration.
36    pub fn new(
37        dimension: usize,
38        max_memory_nodes: usize,
39        cache_size: usize,
40        storage_path: Option<String>,
41        flush_interval: u64,
42        optimize_on_shutdown: bool,
43    ) -> Self {
44        Self {
45            dimension,
46            max_memory_nodes,
47            cache_size,
48            storage_path,
49            flush_interval,
50            optimize_on_shutdown,
51            grid_resolution: 0,
52            tau: FixedPoint::from_int(0),
53        }
54    }
55
56    /// Check if persistence is enabled.
57    pub fn is_persistence_enabled(&self) -> bool {
58        self.storage_path.is_some()
59    }
60}
61
62impl Default for HTTStorageConfig {
63    fn default() -> Self {
64        Self {
65            dimension: 4,
66            max_memory_nodes: 1000,
67            cache_size: 100,
68            storage_path: None,
69            flush_interval: 60,
70            optimize_on_shutdown: true,
71            grid_resolution: 0,
72            tau: FixedPoint::from_int(0),
73        }
74    }
75}
76
77impl Debug for HTTStorageConfig {
78    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
79        f.debug_struct("HTTStorageConfig")
80            .field("dimension", &self.dimension)
81            .field("max_memory_nodes", &self.max_memory_nodes)
82            .field("cache_size", &self.cache_size)
83            .field("storage_path", &self.storage_path)
84            .field("flush_interval", &self.flush_interval)
85            .field("optimize_on_shutdown", &self.optimize_on_shutdown)
86            .field("grid_resolution", &self.grid_resolution)
87            .field("tau", &self.tau)
88            .finish()
89    }
90}
91
92/// Builder for HTT storage configuration.
93pub struct HTTStorageConfigBuilder {
94    config: HTTStorageConfig,
95}
96
97impl HTTStorageConfigBuilder {
98    /// Create a new builder with defaults.
99    pub fn new() -> Self {
100        Self {
101            config: HTTStorageConfig::default(),
102        }
103    }
104
105    /// Set the dimension.
106    pub fn dimension(mut self, dimension: usize) -> Self {
107        self.config.dimension = dimension;
108        self
109    }
110
111    /// Set the maximum in-memory nodes.
112    pub fn max_memory_nodes(mut self, max_memory_nodes: usize) -> Self {
113        self.config.max_memory_nodes = max_memory_nodes;
114        self
115    }
116
117    /// Set the cache size.
118    pub fn cache_size(mut self, cache_size: usize) -> Self {
119        self.config.cache_size = cache_size;
120        self
121    }
122
123    /// Set the storage path.
124    pub fn storage_path(mut self, storage_path: Option<String>) -> Self {
125        self.config.storage_path = storage_path;
126        self
127    }
128
129    /// Set the flush interval.
130    pub fn flush_interval(mut self, flush_interval: u64) -> Self {
131        self.config.flush_interval = flush_interval;
132        self
133    }
134
135    /// Set optimize on shutdown.
136    pub fn optimize_on_shutdown(mut self, optimize_on_shutdown: bool) -> Self {
137        self.config.optimize_on_shutdown = optimize_on_shutdown;
138        self
139    }
140
141    /// Set point location grid resolution (0 = use default of 64).
142    pub fn grid_resolution(mut self, grid_resolution: usize) -> Self {
143        self.config.grid_resolution = grid_resolution;
144        self
145    }
146
147    /// Set the Sarkar embedding scale factor τ (zero = use default of 1.0).
148    pub fn tau(mut self, tau: FixedPoint) -> Self {
149        self.config.tau = tau;
150        self
151    }
152
153    /// Build the configuration.
154    pub fn build(self) -> HTTStorageConfig {
155        self.config
156    }
157}
158
159#[cfg(test)]
160mod tests {
161    use super::*;
162
163    #[test]
164    fn test_config_default() {
165        let config = HTTStorageConfig::default();
166
167        assert_eq!(config.dimension, 4);
168        assert_eq!(config.max_memory_nodes, 1000);
169        assert_eq!(config.cache_size, 100);
170        assert_eq!(config.storage_path, None);
171        assert_eq!(config.flush_interval, 60);
172        assert_eq!(config.optimize_on_shutdown, true);
173    }
174
175    #[test]
176    fn test_config_builder() {
177        let config = HTTStorageConfigBuilder::new()
178            .dimension(8)
179            .max_memory_nodes(2000)
180            .cache_size(200)
181            .storage_path(Some("/tmp/htt".to_string()))
182            .flush_interval(120)
183            .optimize_on_shutdown(false)
184            .build();
185
186        assert_eq!(config.dimension, 8);
187        assert_eq!(config.max_memory_nodes, 2000);
188        assert_eq!(config.cache_size, 200);
189        assert_eq!(config.storage_path, Some("/tmp/htt".to_string()));
190        assert_eq!(config.flush_interval, 120);
191        assert_eq!(config.optimize_on_shutdown, false);
192    }
193
194    #[test]
195    fn test_persistence_enabled() {
196        let config1 = HTTStorageConfigBuilder::new()
197            .storage_path(Some("/tmp/htt".to_string()))
198            .build();
199        assert!(config1.is_persistence_enabled());
200
201        let config2 = HTTStorageConfigBuilder::new()
202            .storage_path(None)
203            .build();
204        assert!(!config2.is_persistence_enabled());
205    }
206}