use std::path::{Path, PathBuf};
use std::time::Duration;
use super::db::{CheckMode, DB};
use crate::consts::{DEFAULT_MAX_BATCH_DELAY, DEFAULT_MAX_BATCH_SIZE};
use crate::errors::Error;
pub(super) struct Options {
pub(super) no_grow_sync: bool,
pub(super) read_only: bool,
pub(super) initial_mmap_size: usize,
pub(super) autoremove: bool,
pub(super) checkmode: CheckMode,
pub(super) max_batch_delay: Duration,
pub(super) max_batch_size: usize,
pub(super) page_size: usize,
}
pub struct DBBuilder {
path: PathBuf,
no_grow_sync: bool,
read_only: bool,
initial_mmap_size: usize,
autoremove: bool,
checkmode: CheckMode,
max_batch_delay: Duration,
max_batch_size: usize,
page_size: usize,
}
impl DBBuilder {
pub fn new<P: AsRef<Path>>(path: P) -> Self {
Self {
path: path.as_ref().to_owned(),
no_grow_sync: false,
read_only: false,
initial_mmap_size: 0,
autoremove: false,
checkmode: CheckMode::NO,
max_batch_delay: DEFAULT_MAX_BATCH_DELAY,
max_batch_size: DEFAULT_MAX_BATCH_SIZE,
page_size: page_size::get(),
}
}
pub fn path<P: AsRef<Path>>(mut self, path: P) -> Self {
self.path = path.as_ref().to_owned();
self
}
pub fn no_grow_sync(mut self, v: bool) -> Self {
self.no_grow_sync = v;
self
}
pub fn read_only(mut self, v: bool) -> Self {
self.read_only = v;
self
}
pub fn initial_mmap_size(mut self, v: usize) -> Self {
self.initial_mmap_size = v;
self
}
pub fn autoremove(mut self, v: bool) -> Self {
self.autoremove = v;
self
}
pub fn checkmode(mut self, v: CheckMode) -> Self {
self.checkmode = v;
self
}
pub fn batch_delay(mut self, v: Duration) -> Self {
self.max_batch_delay = v;
self
}
pub fn batch_size(mut self, v: usize) -> Self {
self.max_batch_size = v;
self
}
pub fn page_size(mut self, v: usize) -> Self {
self.page_size = v;
self
}
pub fn build(self) -> Result<DB, Error> {
let options = Options {
no_grow_sync: self.no_grow_sync,
read_only: self.read_only,
initial_mmap_size: self.initial_mmap_size,
autoremove: self.autoremove,
checkmode: self.checkmode,
max_batch_delay: self.max_batch_delay,
max_batch_size: self.max_batch_size,
page_size: self.page_size,
};
DB::open(self.path, options)
}
}