Skip to main content

ubiquity_database/
config.rs

1//! Database configuration
2
3use serde::{Deserialize, Serialize};
4use std::path::PathBuf;
5
6/// Database configuration
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct DatabaseConfig {
9    /// Backend to use
10    pub backend: crate::DatabaseBackend,
11    
12    /// SQLite configuration
13    pub sqlite: SqliteConfig,
14    
15    /// Astra DB configuration
16    #[cfg(feature = "astra")]
17    pub astra: AstraConfig,
18    
19    /// Embedding configuration
20    pub embeddings: EmbeddingConfig,
21    
22    /// Memory pool configuration
23    pub memory_pools: MemoryPoolConfig,
24}
25
26impl Default for DatabaseConfig {
27    fn default() -> Self {
28        Self {
29            backend: crate::DatabaseBackend::Sqlite,
30            sqlite: SqliteConfig::default(),
31            #[cfg(feature = "astra")]
32            astra: AstraConfig::default(),
33            embeddings: EmbeddingConfig::default(),
34            memory_pools: MemoryPoolConfig::default(),
35        }
36    }
37}
38
39/// SQLite configuration
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct SqliteConfig {
42    /// Database path
43    pub path: PathBuf,
44    
45    /// Maximum connections
46    pub max_connections: u32,
47    
48    /// Enable WAL mode
49    pub wal_mode: bool,
50    
51    /// Memory pool directory
52    pub pool_directory: PathBuf,
53}
54
55impl Default for SqliteConfig {
56    fn default() -> Self {
57        Self {
58            path: PathBuf::from("ubiquity.db"),
59            max_connections: 10,
60            wal_mode: true,
61            pool_directory: PathBuf::from(".ubiquity/pools"),
62        }
63    }
64}
65
66/// Astra DB configuration
67#[cfg(feature = "astra")]
68#[derive(Debug, Clone, Serialize, Deserialize)]
69pub struct AstraConfig {
70    /// API endpoint
71    pub endpoint: String,
72    
73    /// Application token
74    pub token: String,
75    
76    /// Keyspace name
77    pub keyspace: String,
78    
79    /// Collections configuration
80    pub collections: AstraCollections,
81    
82    /// Streaming configuration
83    pub streaming: AstraStreamingConfig,
84}
85
86#[cfg(feature = "astra")]
87impl Default for AstraConfig {
88    fn default() -> Self {
89        Self {
90            endpoint: String::new(),
91            token: String::new(),
92            keyspace: "ubiquity".to_string(),
93            collections: AstraCollections::default(),
94            streaming: AstraStreamingConfig::default(),
95        }
96    }
97}
98
99/// Astra collections configuration
100#[cfg(feature = "astra")]
101#[derive(Debug, Clone, Serialize, Deserialize)]
102pub struct AstraCollections {
103    /// Consciousness states collection
104    pub consciousness: String,
105    
106    /// Consciousness ripples collection
107    pub ripples: String,
108    
109    /// Tasks collection
110    pub tasks: String,
111    
112    /// Memory pool prefix for collections
113    pub pool_prefix: String,
114}
115
116#[cfg(feature = "astra")]
117impl Default for AstraCollections {
118    fn default() -> Self {
119        Self {
120            consciousness: "consciousness_states".to_string(),
121            ripples: "consciousness_ripples".to_string(),
122            tasks: "tasks".to_string(),
123            pool_prefix: "memory_pool_".to_string(),
124        }
125    }
126}
127
128/// Astra Streaming configuration
129#[cfg(feature = "astra")]
130#[derive(Debug, Clone, Serialize, Deserialize)]
131pub struct AstraStreamingConfig {
132    /// Streaming endpoint
133    pub endpoint: String,
134    
135    /// Tenant name
136    pub tenant: String,
137    
138    /// Namespace
139    pub namespace: String,
140    
141    /// Topic prefix
142    pub topic_prefix: String,
143}
144
145#[cfg(feature = "astra")]
146impl Default for AstraStreamingConfig {
147    fn default() -> Self {
148        Self {
149            endpoint: String::new(),
150            tenant: String::new(),
151            namespace: "ubiquity".to_string(),
152            topic_prefix: "ubiquity_".to_string(),
153        }
154    }
155}
156
157/// Embedding configuration
158#[derive(Debug, Clone, Serialize, Deserialize)]
159pub struct EmbeddingConfig {
160    /// Embedding dimension
161    pub dimension: usize,
162    
163    /// Model to use for embeddings
164    pub model: String,
165    
166    /// Provider for vectorization
167    pub provider: String,
168    
169    /// Cache embeddings
170    pub cache_enabled: bool,
171    
172    /// Maximum cache size
173    pub cache_size: usize,
174}
175
176impl Default for EmbeddingConfig {
177    fn default() -> Self {
178        Self {
179            dimension: 1536,
180            model: "text-embedding-3-small".to_string(),
181            provider: "openai".to_string(),
182            cache_enabled: true,
183            cache_size: 10000,
184        }
185    }
186}
187
188/// Memory pool configuration
189#[derive(Debug, Clone, Serialize, Deserialize)]
190pub struct MemoryPoolConfig {
191    /// Number of pools (default: 7)
192    pub pool_count: usize,
193    
194    /// Maximum pool size in MB
195    pub max_pool_size_mb: usize,
196    
197    /// Eviction policy
198    pub eviction_policy: EvictionPolicy,
199    
200    /// Enable compression
201    pub compression: bool,
202}
203
204impl Default for MemoryPoolConfig {
205    fn default() -> Self {
206        Self {
207            pool_count: 7,
208            max_pool_size_mb: 100,
209            eviction_policy: EvictionPolicy::Lru,
210            compression: true,
211        }
212    }
213}
214
215/// Eviction policy for memory pools
216#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
217#[serde(rename_all = "lowercase")]
218pub enum EvictionPolicy {
219    /// Least Recently Used
220    Lru,
221    /// Least Frequently Used
222    Lfu,
223    /// Time To Live
224    Ttl,
225    /// No eviction
226    None,
227}