use std::time::Duration;
use super::prelude::*;
#[derive(Debug, Default)]
pub struct Builder {
config: Config,
iodriver: Option<IoDriver>,
}
const MAX_POSSIBLE_DATA_IN_BLOB: u64 = u32::MAX as u64;
impl Builder {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn build<K>(self) -> Result<Storage<K>>
where
for<'a> K: Key<'a> + 'static,
{
let mut error_params = String::new();
if self.config.work_dir().is_none() {
error_params.push_str("> work_dir\n");
} else if self.config.max_data_in_blob().is_none() {
error_params.push_str("> max_data_in_blob\n");
} else if self.config.max_data_in_blob().unwrap() > MAX_POSSIBLE_DATA_IN_BLOB {
error_params.push_str(&format!(
"> max_data_in_blob must be less than {}",
MAX_POSSIBLE_DATA_IN_BLOB
));
} else if self.config.max_blob_size().is_none() {
error_params.push_str("> max_blob_size\n");
} else if self.config.blob_file_name_prefix().is_none() {
error_params.push_str("> blob_file_name_prefix\n");
}
if error_params.is_empty() {
Ok(Storage::new(
self.config,
self.iodriver.unwrap_or_else(|| IoDriver::new_sync()),
))
} else {
error!("{}", error_params);
Err(Error::uninitialized().into())
}
}
pub fn work_dir<S: Into<PathBuf>>(mut self, work_dir: S) -> Self {
let path: PathBuf = work_dir.into();
debug!("work dir set to: {}", path.display());
self.config.set_work_dir(path);
self
}
pub fn corrupted_dir_name(mut self, name: impl Into<String>) -> Self {
let name = name.into();
debug!("corrupted dir name set to: {}", name);
self.config.set_corrupted_dir_name(name);
self
}
#[must_use]
pub fn max_blob_size(mut self, max_blob_size: u64) -> Self {
if max_blob_size > 0 {
self.config.set_max_blob_size(max_blob_size);
} else {
error!("zero size blobs is useless, not set");
}
self
}
#[must_use]
pub fn max_data_in_blob(mut self, max_data_in_blob: u64) -> Self {
if max_data_in_blob > 0 {
self.config.set_max_data_in_blob(max_data_in_blob);
} else {
error!("zero size blobs is useless, not set");
}
self
}
pub fn blob_file_name_prefix<U: Into<String>>(mut self, blob_file_name_prefix: U) -> Self {
let prefix = blob_file_name_prefix.into();
if prefix.is_empty() {
error!("passed empty file prefix, not set");
} else {
self.config.set_blob_file_name_prefix(prefix);
}
self
}
#[must_use]
pub fn allow_duplicates(mut self) -> Self {
self.config.set_allow_duplicates(true);
self
}
pub fn ignore_corrupted(mut self) -> Self {
self.config.set_ignore_corrupted(true);
self
}
#[must_use]
pub fn set_filter_config(mut self, config: BloomConfig) -> Self {
let mut index_config = self.config.index().clone();
index_config.bloom_config = Some(config);
self.config.set_index(index_config);
self
}
#[must_use]
pub fn set_validate_data_during_index_regen(mut self, value: bool) -> Self {
self.config.set_validate_data_during_index_regen(value);
self
}
#[must_use]
pub fn create_work_dir(mut self, create: bool) -> Self {
self.config.set_create_work_dir(create);
self
}
pub fn set_io_driver(mut self, iodriver: IoDriver) -> Self {
self.iodriver = Some(iodriver);
self
}
pub fn set_dump_sem(mut self, dump_sem: Arc<Semaphore>) -> Self {
self.config.set_dump_sem(dump_sem);
self
}
pub fn set_bloom_filter_group_size(mut self, size: usize) -> Self {
self.config.set_bloom_filter_group_size(size);
self
}
pub fn set_deferred_index_dump_times(mut self, min: Duration, max: Duration) -> Self {
self.config.set_deferred_index_dump_times(min, max);
self
}
pub fn set_max_dirty_bytes_before_sync(mut self, bytes: u64) -> Self {
self.config.set_max_dirty_bytes_before_sync(bytes);
self
}
}