use std::path::PathBuf;
use std::time::Duration;
pub use kevy_persist::Fsync as AppendFsync;
pub use kevy_store::EvictionPolicy;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TtlReaperMode {
Background,
Manual,
}
#[derive(Debug, Clone)]
pub struct Config {
pub maxmemory: u64,
pub eviction_policy: EvictionPolicy,
pub data_dir: Option<PathBuf>,
pub aof: bool,
pub appendfsync: AppendFsync,
pub snapshot_filename: String,
pub aof_filename: String,
pub ttl_reaper: TtlReaperMode,
pub reaper_interval: Duration,
pub reaper_samples: usize,
pub reaper_max_rounds: u32,
}
impl Default for Config {
fn default() -> Self {
Self {
maxmemory: 0,
eviction_policy: EvictionPolicy::NoEviction,
data_dir: None,
aof: true,
appendfsync: AppendFsync::EverySec,
snapshot_filename: String::from("dump-0.rdb"),
aof_filename: String::from("aof-0.aof"),
ttl_reaper: TtlReaperMode::Background,
reaper_interval: Duration::from_millis(100),
reaper_samples: 20,
reaper_max_rounds: 16,
}
}
}
impl Config {
pub fn with_persist(mut self, dir: impl Into<PathBuf>) -> Self {
self.data_dir = Some(dir.into());
self
}
pub fn without_aof(mut self) -> Self {
self.aof = false;
self
}
pub fn with_max_memory(mut self, bytes: u64) -> Self {
self.maxmemory = bytes;
self
}
pub fn with_eviction(mut self, policy: EvictionPolicy) -> Self {
self.eviction_policy = policy;
self
}
pub fn with_appendfsync(mut self, fsync: AppendFsync) -> Self {
self.appendfsync = fsync;
self
}
pub fn with_ttl_reaper_manual(mut self) -> Self {
self.ttl_reaper = TtlReaperMode::Manual;
self
}
pub fn with_reaper_interval(mut self, iv: Duration) -> Self {
self.reaper_interval = iv;
self
}
pub fn with_snapshot_filename(mut self, name: impl Into<String>) -> Self {
self.snapshot_filename = name.into();
self
}
pub fn with_aof_filename(mut self, name: impl Into<String>) -> Self {
self.aof_filename = name.into();
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_is_pure_in_memory() {
let c = Config::default();
assert_eq!(c.maxmemory, 0);
assert!(c.data_dir.is_none());
assert_eq!(c.ttl_reaper, TtlReaperMode::Background);
assert!(c.aof);
}
#[test]
fn builder_chains() {
let c = Config::default()
.with_persist("/tmp/foo")
.with_max_memory(1024)
.with_eviction(EvictionPolicy::AllKeysLru)
.with_ttl_reaper_manual()
.with_appendfsync(AppendFsync::Always);
assert_eq!(c.data_dir.as_deref(), Some(std::path::Path::new("/tmp/foo")));
assert_eq!(c.maxmemory, 1024);
assert_eq!(c.eviction_policy, EvictionPolicy::AllKeysLru);
assert_eq!(c.ttl_reaper, TtlReaperMode::Manual);
}
#[test]
fn without_aof_disables_logging_path() {
let c = Config::default().with_persist("/tmp/foo").without_aof();
assert!(c.data_dir.is_some());
assert!(!c.aof);
}
}