use std::time::Duration;
use crate::constants::*;
use crate::core::ttl_sweep::TtlConfig;
use crate::error::Result;
use super::FeoxStore;
pub struct StoreConfig {
pub hash_bits: u32,
pub memory_only: bool,
pub enable_caching: bool,
pub device_path: Option<String>,
pub file_size: Option<u64>,
pub max_memory: Option<usize>,
pub enable_ttl: bool,
pub ttl_config: Option<TtlConfig>,
}
pub struct StoreBuilder {
hash_bits: u32,
device_path: Option<String>,
file_size: Option<u64>,
max_memory: Option<usize>,
enable_caching: Option<bool>,
enable_ttl: bool,
ttl_config: Option<TtlConfig>,
}
impl StoreBuilder {
pub fn new() -> Self {
Self {
hash_bits: DEFAULT_HASH_BITS,
device_path: None,
file_size: None,
max_memory: Some(DEFAULT_MAX_MEMORY),
enable_caching: None, enable_ttl: false,
ttl_config: None,
}
}
pub fn device_path(mut self, path: impl Into<String>) -> Self {
self.device_path = Some(path.into());
self
}
pub fn file_size(mut self, size: u64) -> Self {
self.file_size = Some(size);
self
}
pub fn max_memory(mut self, limit: usize) -> Self {
self.max_memory = Some(limit);
self
}
pub fn no_memory_limit(mut self) -> Self {
self.max_memory = None;
self
}
pub fn hash_bits(mut self, bits: u32) -> Self {
self.hash_bits = bits;
self
}
pub fn enable_caching(mut self, enable: bool) -> Self {
self.enable_caching = Some(enable);
self
}
pub fn enable_ttl(mut self, enable: bool) -> Self {
self.enable_ttl = enable;
if enable {
let mut config = self.ttl_config.unwrap_or_default();
config.enabled = true;
self.ttl_config = Some(config);
}
self
}
pub fn enable_ttl_cleaner(mut self, enable: bool) -> Self {
let mut config = self.ttl_config.unwrap_or_default();
config.enabled = enable;
self.ttl_config = Some(config);
self.enable_ttl = enable; self
}
pub fn ttl_sweeper_config(
mut self,
sample_size: usize,
threshold: f32,
max_time_ms: u64,
interval_ms: u64,
) -> Self {
self.ttl_config = Some(TtlConfig {
sample_size,
expiry_threshold: threshold,
max_iterations: 16,
max_time_per_run: Duration::from_millis(max_time_ms),
sleep_interval: Duration::from_millis(interval_ms),
enabled: true,
});
self
}
pub fn build(self) -> Result<FeoxStore> {
let memory_only = self.device_path.is_none();
let enable_caching = self.enable_caching.unwrap_or(!memory_only);
let config = StoreConfig {
hash_bits: self.hash_bits,
memory_only,
enable_caching,
device_path: self.device_path,
file_size: self.file_size,
max_memory: self.max_memory,
enable_ttl: self.enable_ttl,
ttl_config: self.ttl_config,
};
FeoxStore::with_config(config)
}
}
impl Default for StoreBuilder {
fn default() -> Self {
Self::new()
}
}