codeprism_storage/
config.rs

1//! Storage configuration
2
3use serde::{Deserialize, Serialize};
4use std::path::PathBuf;
5use std::time::Duration;
6
7use crate::StorageBackend;
8
9/// Storage configuration
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct StorageConfig {
12    /// Storage backend type
13    pub backend: StorageBackend,
14    /// Data storage path (for file-based backends)
15    pub data_path: PathBuf,
16    /// Cache size in megabytes
17    pub cache_size_mb: usize,
18    /// Persistence interval for periodic saves
19    pub persistence_interval: Duration,
20    /// Enable compression for stored data
21    pub compression_enabled: bool,
22    /// Retention period for analysis results
23    pub retention_period: Duration,
24    /// Connection string for database backends
25    pub connection_string: Option<String>,
26}
27
28impl Default for StorageConfig {
29    fn default() -> Self {
30        Self {
31            backend: StorageBackend::InMemory,
32            data_path: PathBuf::from("./codeprism_data"),
33            cache_size_mb: 256,
34            persistence_interval: Duration::from_secs(300), // 5 minutes
35            compression_enabled: true,
36            retention_period: Duration::from_secs(86400 * 7), // 1 week
37            connection_string: None,
38        }
39    }
40}
41
42impl StorageConfig {
43    /// Create a new storage configuration
44    pub fn new(backend: StorageBackend, data_path: PathBuf) -> Self {
45        Self {
46            backend,
47            data_path,
48            ..Default::default()
49        }
50    }
51
52    /// Create configuration for in-memory storage
53    pub fn in_memory() -> Self {
54        Self {
55            backend: StorageBackend::InMemory,
56            ..Default::default()
57        }
58    }
59
60    /// Create configuration for file-based storage
61    pub fn file_based(data_path: PathBuf) -> Self {
62        Self {
63            backend: StorageBackend::File,
64            data_path,
65            ..Default::default()
66        }
67    }
68
69    /// Create configuration for SQLite storage
70    pub fn sqlite(data_path: PathBuf) -> Self {
71        Self {
72            backend: StorageBackend::Sqlite,
73            data_path,
74            ..Default::default()
75        }
76    }
77
78    /// Set cache size in megabytes
79    pub fn with_cache_size(mut self, size_mb: usize) -> Self {
80        self.cache_size_mb = size_mb;
81        self
82    }
83
84    /// Set persistence interval
85    pub fn with_persistence_interval(mut self, interval: Duration) -> Self {
86        self.persistence_interval = interval;
87        self
88    }
89
90    /// Enable or disable compression
91    pub fn with_compression(mut self, enabled: bool) -> Self {
92        self.compression_enabled = enabled;
93        self
94    }
95
96    /// Set retention period for analysis results
97    pub fn with_retention_period(mut self, period: Duration) -> Self {
98        self.retention_period = period;
99        self
100    }
101
102    #[cfg(test)]
103    pub fn default_for_testing(data_path: &std::path::Path) -> Self {
104        Self {
105            backend: StorageBackend::InMemory,
106            data_path: data_path.to_path_buf(),
107            cache_size_mb: 64,
108            persistence_interval: Duration::from_secs(60),
109            compression_enabled: false,
110            retention_period: Duration::from_secs(86400),
111            connection_string: None,
112        }
113    }
114}