use std::path::PathBuf;
use std::time::Duration;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Fsync {
PerBatch,
OnFlush,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OpenMode {
ReadWrite,
ReadOnly,
}
#[derive(Clone, Debug)]
pub struct Config {
pub path: PathBuf,
pub dimension: usize,
pub fsync: Fsync,
pub open_mode: OpenMode,
pub auto_compact: Option<f32>,
pub lock_ttl: Duration,
}
impl Config {
pub fn new(path: impl Into<PathBuf>, dimension: usize) -> Self {
Self {
path: path.into(),
dimension,
fsync: Fsync::PerBatch,
open_mode: OpenMode::ReadWrite,
auto_compact: Some(0.5),
lock_ttl: Duration::from_secs(60),
}
}
pub fn fsync(mut self, f: Fsync) -> Self {
self.fsync = f;
self
}
pub fn open_mode(mut self, m: OpenMode) -> Self {
self.open_mode = m;
self
}
pub fn auto_compact(mut self, ratio: Option<f32>) -> Self {
self.auto_compact = ratio;
self
}
pub fn lock_ttl(mut self, ttl: Duration) -> Self {
self.lock_ttl = ttl;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_are_sane() {
let c = Config::new("/tmp/store", 1024);
assert_eq!(c.dimension, 1024);
assert_eq!(c.fsync, Fsync::PerBatch);
assert_eq!(c.open_mode, OpenMode::ReadWrite);
assert_eq!(c.auto_compact, Some(0.5));
assert_eq!(c.lock_ttl, Duration::from_secs(60));
}
#[test]
fn builder_overrides() {
let c = Config::new("/tmp/store", 8)
.fsync(Fsync::OnFlush)
.open_mode(OpenMode::ReadOnly)
.auto_compact(None)
.lock_ttl(Duration::from_secs(5));
assert_eq!(c.fsync, Fsync::OnFlush);
assert_eq!(c.open_mode, OpenMode::ReadOnly);
assert_eq!(c.auto_compact, None);
assert_eq!(c.lock_ttl, Duration::from_secs(5));
}
}