use crate::config::{
BINCODE_CONFIG, DEFAULT_BLOOM_FILTER_ERROR_PROBABILITY, DEFAULT_DB_PATH,
DEFAULT_INDEX_CACHE_LRU_MAX_CAPACITY, DEFAULT_INDEX_CACHE_MEMORY_LIMIT, DEFAULT_MEM_TABLE_SIZE,
DEFAULT_VALUE_CACHE_LRU_MAX_CAPACITY, DEFAULT_VALUE_CACHE_MEMORY_LIMIT, DEFAULT_WAL_MAX_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 index_cache_memory_limit: usize,
pub index_cache_max_capacity: usize,
pub enable_value_cache: bool,
pub value_cache_memory_limit: usize,
pub value_cache_max_capacity: usize,
pub enable_wal: bool,
pub wal_max_size: u64,
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,
index_cache_memory_limit: DEFAULT_INDEX_CACHE_MEMORY_LIMIT,
index_cache_max_capacity: DEFAULT_INDEX_CACHE_LRU_MAX_CAPACITY,
enable_value_cache: true,
value_cache_memory_limit: DEFAULT_VALUE_CACHE_MEMORY_LIMIT,
value_cache_max_capacity: DEFAULT_VALUE_CACHE_LRU_MAX_CAPACITY,
enable_wal: true,
wal_max_size: DEFAULT_WAL_MAX_SIZE,
compressor: Compressor::new(CompressionConfig::none()),
}
}
}
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>,
index_cache_memory_limit: Option<usize>,
index_cache_max_capacity: Option<usize>,
enable_value_cache: Option<bool>,
value_cache_memory_limit: Option<usize>,
value_cache_max_capacity: Option<usize>,
enable_wal: Option<bool>,
wal_max_size: Option<u64>,
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,
index_cache_memory_limit: None,
index_cache_max_capacity: None,
enable_value_cache: None,
value_cache_memory_limit: None,
value_cache_max_capacity: None,
enable_wal: None,
wal_max_size: 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 wal(mut self, is_enabled: bool) -> Self {
self.enable_wal = Some(is_enabled);
self
}
pub fn wal_max_size(mut self, size: u64) -> Self {
self.wal_max_size = Some(size);
self
}
pub fn index_cache_memory_limit(mut self, memory_limit: usize) -> Self {
self.index_cache_memory_limit = Some(memory_limit);
self
}
pub fn index_cache_max_capacity(mut self, size: usize) -> Self {
self.index_cache_max_capacity = Some(size);
self
}
pub fn value_cache_memory_limit(mut self, memory_limit: usize) -> Self {
self.value_cache_memory_limit = Some(memory_limit);
self
}
pub fn value_cache_max_capacity(mut self, size: usize) -> Self {
self.value_cache_max_capacity = Some(size);
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(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),
index_cache_memory_limit: self
.index_cache_memory_limit
.unwrap_or(DEFAULT_INDEX_CACHE_MEMORY_LIMIT),
index_cache_max_capacity: self
.index_cache_max_capacity
.unwrap_or(DEFAULT_INDEX_CACHE_LRU_MAX_CAPACITY),
enable_value_cache: self.enable_value_cache.unwrap_or(true),
value_cache_memory_limit: self
.value_cache_memory_limit
.unwrap_or(DEFAULT_VALUE_CACHE_MEMORY_LIMIT),
value_cache_max_capacity: self
.value_cache_max_capacity
.unwrap_or(DEFAULT_VALUE_CACHE_LRU_MAX_CAPACITY),
enable_wal: self.enable_wal.unwrap_or(true),
wal_max_size: self.wal_max_size.unwrap_or(DEFAULT_WAL_MAX_SIZE),
compressor: self
.compressor
.unwrap_or(Compressor::new(CompressionConfig::none())),
}
}
}