bitcasky_common/
options.rs

1use std::{sync::Arc, time::Duration};
2
3use crate::clock::{BitcaskyClock, ClockImpl, DebugClock};
4
5#[derive(Debug, Clone, Copy)]
6pub enum DataSotrageType {
7    Mmap,
8}
9
10#[derive(Debug)]
11pub struct DataStorageOptions {
12    pub max_data_file_size: usize,
13    pub init_data_file_capacity: usize,
14    pub storage_type: DataSotrageType,
15}
16
17impl Default for DataStorageOptions {
18    fn default() -> Self {
19        Self {
20            max_data_file_size: 128 * 1024 * 1024,
21            init_data_file_capacity: 1024 * 1024,
22            storage_type: DataSotrageType::Mmap,
23        }
24    }
25}
26
27impl DataStorageOptions {
28    pub fn max_data_file_size(mut self, size: usize) -> DataStorageOptions {
29        assert!(size > 0);
30        self.max_data_file_size = size;
31        self
32    }
33
34    pub fn init_data_file_capacity(mut self, capacity: usize) -> DataStorageOptions {
35        assert!(capacity > 0);
36        self.init_data_file_capacity = capacity;
37        self
38    }
39
40    pub fn storage_type(mut self, storage_type: DataSotrageType) -> DataStorageOptions {
41        self.storage_type = storage_type;
42        self
43    }
44}
45
46#[derive(Debug)]
47pub struct DatabaseOptions {
48    pub storage: DataStorageOptions,
49    /// How frequent can we flush data
50    pub sync_interval_sec: u64,
51    pub init_hint_file_capacity: usize,
52}
53
54impl DatabaseOptions {
55    pub fn storage(mut self, storage: DataStorageOptions) -> Self {
56        self.storage = storage;
57        self
58    }
59}
60
61impl Default for DatabaseOptions {
62    fn default() -> Self {
63        Self {
64            storage: DataStorageOptions::default(),
65            init_hint_file_capacity: 1024 * 1024,
66            sync_interval_sec: 60,
67        }
68    }
69}
70
71/// Bitcask optional options. Used on opening Bitcask instance.
72#[derive(Debug)]
73pub struct BitcaskyOptions {
74    pub database: DatabaseOptions,
75    // maximum key size, default: 1 KB
76    pub max_key_size: usize,
77    // maximum value size, default: 100 KB
78    pub max_value_size: usize,
79    // clock to get time,
80    pub clock: BitcaskyClock,
81}
82
83/// Default Bitcask Options
84impl Default for BitcaskyOptions {
85    fn default() -> Self {
86        Self {
87            database: DatabaseOptions::default(),
88            max_key_size: 1024,
89            max_value_size: 100 * 1024,
90            clock: BitcaskyClock::default(),
91        }
92    }
93}
94
95impl BitcaskyOptions {
96    // maximum data file size, default: 128 MB
97    pub fn max_data_file_size(mut self, size: usize) -> BitcaskyOptions {
98        assert!(size > 0);
99        self.database.storage.max_data_file_size = size;
100        self
101    }
102
103    // data file initial capacity, default: 1 MB
104    pub fn init_data_file_capacity(mut self, capacity: usize) -> BitcaskyOptions {
105        assert!(capacity > 0);
106        self.database.storage.init_data_file_capacity = capacity;
107        self
108    }
109
110    // hint file initial capacity, default: 1 MB
111    pub fn init_hint_file_capacity(mut self, capacity: usize) -> BitcaskyOptions {
112        assert!(capacity > 0);
113        self.database.init_hint_file_capacity = capacity;
114        self
115    }
116
117    // maximum key size, default: 1 KB
118    pub fn max_key_size(mut self, size: usize) -> BitcaskyOptions {
119        assert!(size > 0);
120        self.max_key_size = size;
121        self
122    }
123
124    // maximum value size, default: 100 KB
125    pub fn max_value_size(mut self, size: usize) -> BitcaskyOptions {
126        assert!(size > 0);
127        self.max_value_size = size;
128        self
129    }
130
131    pub fn storage_type(mut self, storage_type: DataSotrageType) -> BitcaskyOptions {
132        self.database.storage.storage_type = storage_type;
133        self
134    }
135
136    // How frequent can we sync data to file. 0 to stop auto sync. default: 1 min
137    pub fn sync_interval(mut self, interval: Duration) -> BitcaskyOptions {
138        assert!(!interval.is_zero());
139        self.database.sync_interval_sec = interval.as_secs();
140        self
141    }
142
143    // Use debug clock
144    pub fn debug_clock(mut self, clock: Arc<DebugClock>) -> BitcaskyOptions {
145        self.clock = BitcaskyClock {
146            clock: ClockImpl::Debug(clock),
147        };
148        self
149    }
150}