use std::{sync::Arc, time::Duration};
use crate::clock::{BitcaskyClock, ClockImpl, DebugClock};
#[derive(Debug, Clone, Copy)]
pub enum SyncStrategy {
None,
OSync,
Interval(Duration),
}
#[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_strategy: SyncStrategy,
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_strategy: SyncStrategy::Interval(Duration::from_secs(60)),
}
}
}
#[derive(Debug)]
pub struct BitcaskyOptions {
pub database: DatabaseOptions,
pub max_key_size: usize,
pub max_value_size: usize,
pub clock: BitcaskyClock,
}
impl Default for BitcaskyOptions {
fn default() -> Self {
Self {
database: DatabaseOptions::default(),
max_key_size: 1024,
max_value_size: 100 * 1024,
clock: BitcaskyClock::default(),
}
}
}
impl BitcaskyOptions {
pub fn max_data_file_size(mut self, size: usize) -> BitcaskyOptions {
assert!(size > 0);
self.database.storage.max_data_file_size = size;
self
}
pub fn init_data_file_capacity(mut self, capacity: usize) -> BitcaskyOptions {
assert!(capacity > 0);
self.database.storage.init_data_file_capacity = capacity;
self
}
pub fn init_hint_file_capacity(mut self, capacity: usize) -> BitcaskyOptions {
assert!(capacity > 0);
self.database.init_hint_file_capacity = capacity;
self
}
pub fn max_key_size(mut self, size: usize) -> BitcaskyOptions {
assert!(size > 0);
self.max_key_size = size;
self
}
pub fn max_value_size(mut self, size: usize) -> BitcaskyOptions {
assert!(size > 0);
self.max_value_size = size;
self
}
pub fn storage_type(mut self, storage_type: DataSotrageType) -> BitcaskyOptions {
self.database.storage.storage_type = storage_type;
self
}
pub fn sync_strategy(mut self, sync_strategy: SyncStrategy) -> BitcaskyOptions {
self.database.sync_strategy = sync_strategy;
self
}
pub fn debug_clock(mut self, clock: Arc<DebugClock>) -> BitcaskyOptions {
self.clock = BitcaskyClock {
clock: ClockImpl::Debug(clock),
};
self
}
}