use crate::config::{BINCODE_CONFIG, DEFAULT_BLOOM_FILTER_ERROR_PROBABILITY, DEFAULT_DB_PATH, DEFAULT_MEM_TABLE_SIZE};
use crate::tree::{CompressionConfig, Compressor};
use std::path::PathBuf;
#[derive(Clone)]
pub struct TreeSettings {
pub db_path: PathBuf,
pub bincode_config: bincode::config::Configuration,
pub mem_table_max_size: usize,
pub bloom_filter_error_probability: f64,
pub enable_bloom_filter_cache: bool,
pub enable_index_cache: bool,
pub enable_value_cache: bool,
pub compressor: Compressor,
}
impl Default for TreeSettings {
fn default() -> Self {
Self {
db_path: PathBuf::from(DEFAULT_DB_PATH),
bincode_config: BINCODE_CONFIG,
mem_table_max_size: DEFAULT_MEM_TABLE_SIZE as usize,
bloom_filter_error_probability: DEFAULT_BLOOM_FILTER_ERROR_PROBABILITY,
enable_bloom_filter_cache: true,
enable_index_cache: true,
enable_value_cache: true,
compressor: Compressor::new(CompressionConfig::balanced()),
}
}
}
pub struct TreeSettingsBuilder {
db_path: Option<PathBuf>,
bincode_config: Option<bincode::config::Configuration>,
mem_table_max_size: Option<usize>,
bloom_filter_error_probability: Option<f64>,
enable_bloom_filter_cache: Option<bool>,
enable_index_cache: Option<bool>,
enable_value_cache: Option<bool>,
compressor: Option<Compressor>,
}
impl TreeSettingsBuilder {
pub fn new() -> Self {
Self {
db_path: None,
bincode_config: None,
mem_table_max_size: None,
bloom_filter_error_probability: None,
enable_bloom_filter_cache: None,
enable_index_cache: None,
enable_value_cache: None,
compressor: None
}
}
pub fn db_path<P: Into<PathBuf>>(mut self, path: P) -> Self {
self.db_path = Some(path.into());
self
}
pub fn bincode_config(mut self, config: bincode::config::Configuration) -> Self {
self.bincode_config = Some(config);
self
}
pub fn mem_table_max_size(mut self, size: usize) -> Self {
self.mem_table_max_size = Some(size);
self
}
pub fn bloom_filter_error_probability(mut self, percent: f64) -> Self {
self.bloom_filter_error_probability = Some(percent);
self
}
pub fn bloom_filter_cache(mut self, is_enabled: bool) -> Self {
self.enable_bloom_filter_cache = Some(is_enabled);
self
}
pub fn index_cache(mut self, is_enabled: bool) -> Self {
self.enable_index_cache = Some(is_enabled);
self
}
pub fn value_cache(mut self, is_enabled: bool) -> Self {
self.enable_value_cache = Some(is_enabled);
self
}
pub fn compressor(mut self, config: CompressionConfig) -> Self {
self.compressor = Some(Compressor::new(config));
self
}
pub fn build(self) -> TreeSettings {
TreeSettings {
db_path: self
.db_path
.unwrap_or_else(|| PathBuf::from(DEFAULT_DB_PATH)),
bincode_config: self.bincode_config.unwrap_or(BINCODE_CONFIG),
mem_table_max_size: self
.mem_table_max_size
.unwrap_or(DEFAULT_MEM_TABLE_SIZE as usize),
bloom_filter_error_probability: self.bloom_filter_error_probability
.unwrap_or(DEFAULT_BLOOM_FILTER_ERROR_PROBABILITY),
enable_bloom_filter_cache: self.enable_bloom_filter_cache.unwrap_or(true),
enable_index_cache: self.enable_index_cache.unwrap_or(true),
enable_value_cache: self.enable_value_cache.unwrap_or(true),
compressor: self.compressor.unwrap_or(Compressor::new(CompressionConfig::balanced())),
}
}
}