use std::{sync::Arc, time::Duration};
use crate::clock::{BitcaskClock, ClockImpl, DebugClock};
#[derive(Debug, Clone, Copy)]
pub enum DataSotrageType {
Mmap,
}
#[derive(Debug)]
pub struct DataStorageOptions {
pub max_data_file_size: usize,
pub init_data_file_capacity: usize,
pub storage_type: DataSotrageType,
}
impl Default for DataStorageOptions {
fn default() -> Self {
Self {
max_data_file_size: 128 * 1024 * 1024,
init_data_file_capacity: 1024 * 1024,
storage_type: DataSotrageType::Mmap,
}
}
}
impl DataStorageOptions {
pub fn max_data_file_size(mut self, size: usize) -> DataStorageOptions {
assert!(size > 0);
self.max_data_file_size = size;
self
}
pub fn init_data_file_capacity(mut self, capacity: usize) -> DataStorageOptions {
assert!(capacity > 0);
self.init_data_file_capacity = capacity;
self
}
pub fn storage_type(mut self, storage_type: DataSotrageType) -> DataStorageOptions {
self.storage_type = storage_type;
self
}
}
#[derive(Debug)]
pub struct DatabaseOptions {
pub storage: DataStorageOptions,
pub sync_interval_sec: u64,
pub init_hint_file_capacity: usize,
}
impl DatabaseOptions {
pub fn storage(mut self, storage: DataStorageOptions) -> Self {
self.storage = storage;
self
}
}
impl Default for DatabaseOptions {
fn default() -> Self {
Self {
storage: DataStorageOptions::default(),
init_hint_file_capacity: 1024 * 1024,
sync_interval_sec: 60,
}
}
}
#[derive(Debug)]
pub struct BitcaskOptions {
pub database: DatabaseOptions,
pub max_key_size: usize,
pub max_value_size: usize,
pub clock: BitcaskClock,
}
impl Default for BitcaskOptions {
fn default() -> Self {
Self {
database: DatabaseOptions::default(),
max_key_size: 1024,
max_value_size: 100 * 1024,
clock: BitcaskClock::default(),
}
}
}
impl BitcaskOptions {
pub fn max_data_file_size(mut self, size: usize) -> BitcaskOptions {
assert!(size > 0);
self.database.storage.max_data_file_size = size;
self
}
pub fn init_data_file_capacity(mut self, capacity: usize) -> BitcaskOptions {
assert!(capacity > 0);
self.database.storage.init_data_file_capacity = capacity;
self
}
pub fn init_hint_file_capacity(mut self, capacity: usize) -> BitcaskOptions {
assert!(capacity > 0);
self.database.init_hint_file_capacity = capacity;
self
}
pub fn max_key_size(mut self, size: usize) -> BitcaskOptions {
assert!(size > 0);
self.max_key_size = size;
self
}
pub fn max_value_size(mut self, size: usize) -> BitcaskOptions {
assert!(size > 0);
self.max_value_size = size;
self
}
pub fn storage_type(mut self, storage_type: DataSotrageType) -> BitcaskOptions {
self.database.storage.storage_type = storage_type;
self
}
pub fn sync_interval(mut self, interval: Duration) -> BitcaskOptions {
assert!(!interval.is_zero());
self.database.sync_interval_sec = interval.as_secs();
self
}
pub fn debug_clock(mut self, clock: Arc<DebugClock>) -> BitcaskOptions {
self.clock = BitcaskClock {
clock: ClockImpl::Debug(clock),
};
self
}
}