1use std::fmt::{self, Debug, Formatter};
11use g_math::fixed_point::FixedPoint;
12
13#[derive(Clone)]
15pub struct HTTStorageConfig {
16 pub dimension: usize,
18 pub max_memory_nodes: usize,
20 pub cache_size: usize,
22 pub storage_path: Option<String>,
24 pub flush_interval: u64,
26 pub optimize_on_shutdown: bool,
28 pub grid_resolution: usize,
30 pub tau: FixedPoint,
32}
33
34impl HTTStorageConfig {
35 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 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
92pub struct HTTStorageConfigBuilder {
94 config: HTTStorageConfig,
95}
96
97impl HTTStorageConfigBuilder {
98 pub fn new() -> Self {
100 Self {
101 config: HTTStorageConfig::default(),
102 }
103 }
104
105 pub fn dimension(mut self, dimension: usize) -> Self {
107 self.config.dimension = dimension;
108 self
109 }
110
111 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 pub fn cache_size(mut self, cache_size: usize) -> Self {
119 self.config.cache_size = cache_size;
120 self
121 }
122
123 pub fn storage_path(mut self, storage_path: Option<String>) -> Self {
125 self.config.storage_path = storage_path;
126 self
127 }
128
129 pub fn flush_interval(mut self, flush_interval: u64) -> Self {
131 self.config.flush_interval = flush_interval;
132 self
133 }
134
135 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 pub fn grid_resolution(mut self, grid_resolution: usize) -> Self {
143 self.config.grid_resolution = grid_resolution;
144 self
145 }
146
147 pub fn tau(mut self, tau: FixedPoint) -> Self {
149 self.config.tau = tau;
150 self
151 }
152
153 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}