1mod block_size;
6mod compression;
7mod filter;
8mod hash_ratio;
9mod pinning;
10mod restart_interval;
11
12pub use block_size::BlockSizePolicy;
13pub use compression::CompressionPolicy;
14pub use filter::{BloomConstructionPolicy, FilterPolicy, FilterPolicyEntry};
15pub use hash_ratio::HashRatioPolicy;
16pub use pinning::PinningPolicy;
17pub use restart_interval::RestartIntervalPolicy;
18
19pub type PartioningPolicy = PinningPolicy;
21
22use crate::{
23 path::absolute_path, version::DEFAULT_LEVEL_COUNT, AnyTree, BlobTree, Cache, CompressionType,
24 DescriptorTable, Tree,
25};
26use std::{
27 path::{Path, PathBuf},
28 sync::Arc,
29};
30
31#[derive(Copy, Clone, Debug, PartialEq, Eq)]
33pub enum TreeType {
34 Standard,
36
37 Blob,
39}
40
41impl From<TreeType> for u8 {
42 fn from(val: TreeType) -> Self {
43 match val {
44 TreeType::Standard => 0,
45 TreeType::Blob => 1,
46 }
47 }
48}
49
50impl TryFrom<u8> for TreeType {
51 type Error = ();
52
53 fn try_from(value: u8) -> Result<Self, Self::Error> {
54 match value {
55 0 => Ok(Self::Standard),
56 1 => Ok(Self::Blob),
57 _ => Err(()),
58 }
59 }
60}
61
62const DEFAULT_FILE_FOLDER: &str = ".lsm.data";
63
64#[derive(Clone, Debug, PartialEq)]
66pub struct KvSeparationOptions {
67 #[doc(hidden)]
69 pub compression: CompressionType,
70
71 #[doc(hidden)]
73 pub file_target_size: u64,
74
75 #[doc(hidden)]
77 pub separation_threshold: u32,
78
79 #[doc(hidden)]
80 pub staleness_threshold: f32,
81
82 #[doc(hidden)]
83 pub age_cutoff: f32,
84}
85
86impl Default for KvSeparationOptions {
87 fn default() -> Self {
88 Self {
89 #[cfg(feature="lz4")]
90 compression: CompressionType::Lz4,
91
92 #[cfg(not(feature="lz4"))]
93 compression: CompressionType::None,
94
95 file_target_size: 64 * 1_024 * 1_024,
96 separation_threshold: 1_024,
97
98 staleness_threshold: 0.33,
99 age_cutoff: 0.20,
100 }
101 }
102}
103
104impl KvSeparationOptions {
105 #[must_use]
107 pub fn compression(mut self, compression: CompressionType) -> Self {
108 self.compression = compression;
109 self
110 }
111
112 #[must_use]
122 pub fn file_target_size(mut self, bytes: u64) -> Self {
123 self.file_target_size = bytes;
124 self
125 }
126
127 #[must_use]
134 pub fn separation_threshold(mut self, bytes: u32) -> Self {
135 self.separation_threshold = bytes;
136 self
137 }
138
139 #[must_use]
146 pub fn staleness_threshold(mut self, ratio: f32) -> Self {
147 self.staleness_threshold = ratio;
148 self
149 }
150
151 #[must_use]
155 pub fn age_cutoff(mut self, ratio: f32) -> Self {
156 self.age_cutoff = ratio;
157 self
158 }
159}
160
161#[derive(Clone)]
162pub struct Config {
164 #[doc(hidden)]
166 pub path: PathBuf,
167
168 #[doc(hidden)]
170 pub cache: Arc<Cache>,
171
172 #[doc(hidden)]
174 pub descriptor_table: Arc<DescriptorTable>,
175
176 pub level_count: u8,
180
181 pub data_block_compression_policy: CompressionPolicy,
183
184 pub index_block_compression_policy: CompressionPolicy,
186
187 pub data_block_restart_interval_policy: RestartIntervalPolicy,
189
190 pub index_block_restart_interval_policy: RestartIntervalPolicy,
192
193 pub data_block_size_policy: BlockSizePolicy,
195
196 pub index_block_size_policy: BlockSizePolicy,
198
199 pub index_block_pinning_policy: PinningPolicy,
201
202 pub filter_block_pinning_policy: PinningPolicy,
204
205 pub top_level_index_block_pinning_policy: PinningPolicy,
207
208 pub top_level_filter_block_pinning_policy: PinningPolicy,
210
211 pub data_block_hash_ratio_policy: HashRatioPolicy,
213
214 pub index_block_partitioning_policy: PartioningPolicy,
216
217 pub filter_block_partitioning_policy: PartioningPolicy,
219
220 pub index_block_partition_size_policy: BlockSizePolicy,
222
223 pub filter_block_partition_size_policy: BlockSizePolicy,
225
226 pub(crate) expect_point_read_hits: bool,
229
230 pub filter_policy: FilterPolicy,
232
233 #[doc(hidden)]
234 pub kv_separation_opts: Option<KvSeparationOptions>,
235}
236
237impl Default for Config {
238 fn default() -> Self {
239 Self {
240 path: absolute_path(Path::new(DEFAULT_FILE_FOLDER)),
241 descriptor_table: Arc::new(DescriptorTable::new(256)),
242
243 cache: Arc::new(Cache::with_capacity_bytes(
244 16 * 1_024 * 1_024,
245 )),
246
247 data_block_restart_interval_policy: RestartIntervalPolicy::all(16),
248 index_block_restart_interval_policy: RestartIntervalPolicy::all(1),
249
250 level_count: DEFAULT_LEVEL_COUNT,
251
252 data_block_size_policy: BlockSizePolicy::default(),
253 index_block_size_policy: BlockSizePolicy::default(),
254
255 index_block_pinning_policy: PinningPolicy::new(&[true, true, false]),
256 filter_block_pinning_policy: PinningPolicy::new(&[true, false]),
257
258 top_level_index_block_pinning_policy: PinningPolicy::all(true), top_level_filter_block_pinning_policy: PinningPolicy::all(true), index_block_partitioning_policy: PinningPolicy::new(&[false, false, false, true]),
262 filter_block_partitioning_policy: PinningPolicy::new(&[false, false, false, true]),
263
264 index_block_partition_size_policy: BlockSizePolicy::all(4_096), filter_block_partition_size_policy: BlockSizePolicy::all(4_096), data_block_compression_policy: CompressionPolicy::default(),
268 index_block_compression_policy: CompressionPolicy::all(CompressionType::None),
269
270 data_block_hash_ratio_policy: HashRatioPolicy::all(0.0),
271
272 filter_policy: FilterPolicy::default(),
273
274 expect_point_read_hits: false,
275
276 kv_separation_opts: None,
277 }
278 }
279}
280
281impl Config {
282 pub fn new<P: AsRef<Path>>(path: P) -> Self {
284 Self {
285 path: absolute_path(path.as_ref()),
286 ..Default::default()
287 }
288 }
289
290 #[must_use]
297 pub fn use_cache(mut self, cache: Arc<Cache>) -> Self {
298 self.cache = cache;
299 self
300 }
301
302 #[must_use]
303 #[doc(hidden)]
304 pub fn use_descriptor_table(mut self, descriptor_table: Arc<DescriptorTable>) -> Self {
305 self.descriptor_table = descriptor_table;
306 self
307 }
308
309 #[must_use]
314 pub fn expect_point_read_hits(mut self, b: bool) -> Self {
315 self.expect_point_read_hits = b;
316 self
317 }
318
319 #[must_use]
321 pub fn filter_block_partitioning_policy(mut self, policy: PinningPolicy) -> Self {
322 self.filter_block_partitioning_policy = policy;
323 self
324 }
325
326 #[must_use]
328 pub fn index_block_partitioning_policy(mut self, policy: PinningPolicy) -> Self {
329 self.index_block_partitioning_policy = policy;
330 self
331 }
332
333 #[must_use]
335 pub fn filter_block_pinning_policy(mut self, policy: PinningPolicy) -> Self {
336 self.filter_block_pinning_policy = policy;
337 self
338 }
339
340 #[must_use]
342 pub fn index_block_pinning_policy(mut self, policy: PinningPolicy) -> Self {
343 self.index_block_pinning_policy = policy;
344 self
345 }
346
347 #[must_use]
354 pub fn data_block_restart_interval_policy(mut self, policy: RestartIntervalPolicy) -> Self {
355 self.data_block_restart_interval_policy = policy;
356 self
357 }
358
359 #[must_use]
374 pub fn filter_policy(mut self, policy: FilterPolicy) -> Self {
375 self.filter_policy = policy;
376 self
377 }
378
379 #[must_use]
381 pub fn data_block_compression_policy(mut self, policy: CompressionPolicy) -> Self {
382 self.data_block_compression_policy = policy;
383 self
384 }
385
386 #[must_use]
388 pub fn index_block_compression_policy(mut self, policy: CompressionPolicy) -> Self {
389 self.index_block_compression_policy = policy;
390 self
391 }
392
393 #[must_use]
413 pub fn data_block_size_policy(mut self, policy: BlockSizePolicy) -> Self {
414 self.data_block_size_policy = policy;
415 self
416 }
417
418 #[must_use]
431 pub fn data_block_hash_ratio_policy(mut self, policy: HashRatioPolicy) -> Self {
432 self.data_block_hash_ratio_policy = policy;
433 self
434 }
435
436 #[must_use]
438 pub fn with_kv_separation(mut self, opts: Option<KvSeparationOptions>) -> Self {
439 self.kv_separation_opts = opts;
440 self
441 }
442
443 pub fn open(self) -> crate::Result<AnyTree> {
449 Ok(if self.kv_separation_opts.is_some() {
450 AnyTree::Blob(BlobTree::open(self)?)
451 } else {
452 AnyTree::Standard(Tree::open(self)?)
453 })
454 }
455}