block_db/options/mod.rs
1// Authors: Robert Lopez
2
3pub(crate) mod store;
4
5use serde::{Deserialize, Serialize};
6use std::num::{NonZero, NonZeroUsize};
7
8/// `chunk_size`: Defines the size (in bytes) of a single chunk
9/// within a `DataBlock`. This **cannot** be changed after the
10/// `BlockDB` is opened for the first time.
11/// _(Default: 4,096)_
12///
13/// `max_file_size`: Sets the **soft** maximum size (in bytes) of a `DataFile`.
14/// "Soft" means that it may be exceeded once per file (e.g., for large writes).
15/// This **can** be changed after initialization, but it will not retroactively
16/// affect the size of existing `DataFile`s.
17/// _(Default: 4,096 * 1,000,000)_
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct BlockDBOptions {
20 chunk_size: NonZeroUsize,
21 max_file_size: NonZeroUsize,
22}
23
24impl Default for BlockDBOptions {
25 fn default() -> Self {
26 unsafe {
27 Self {
28 chunk_size: NonZero::new_unchecked(4_096),
29 max_file_size: NonZero::new_unchecked(4_096 * 1_000_000),
30 }
31 }
32 }
33}
34
35impl BlockDBOptions {
36 /// Get `chunk_size` as `usize`
37 pub fn chunk_size(&self) -> usize {
38 usize::from(self.chunk_size)
39 }
40
41 /// Get `max_file_size` as `usize`
42 pub fn max_file_size(&self) -> usize {
43 usize::from(self.max_file_size)
44 }
45}
46
47#[derive(Debug, Default)]
48pub struct BlockDBOptionsBuilder {
49 pub chunk_size: Option<NonZeroUsize>,
50 pub max_file_size: Option<NonZeroUsize>,
51}
52
53impl BlockDBOptionsBuilder {
54 /// Defines the size (in bytes) of a single chunk
55 /// within a `DataBlock`. This **cannot** be changed after the
56 /// `BlockDB` is opened for the first time.
57 /// _(Default: 4,096)_
58 pub fn chunk_size(mut self, chunk_size: NonZeroUsize) -> Self {
59 self.chunk_size = Some(chunk_size);
60
61 self
62 }
63
64 /// Sets the **soft** maximum size (in bytes) of a `DataFile`.
65 /// "Soft" means that it may be exceeded once per file (e.g., for large writes).
66 /// This **can** be changed after initialization, but it will not retroactively
67 /// affect the size of existing `DataFile`s.
68 /// _(Default: 4,096 * 1,000,000)_
69 pub fn max_file_size(mut self, max_file_size: NonZeroUsize) -> Self {
70 self.max_file_size = Some(max_file_size);
71
72 self
73 }
74
75 pub fn build(self) -> BlockDBOptions {
76 unsafe {
77 BlockDBOptions {
78 chunk_size: self.chunk_size.unwrap_or(NonZero::new_unchecked(4_096)),
79 max_file_size: self
80 .max_file_size
81 .unwrap_or(NonZero::new_unchecked(4_096 * 1_000_000)),
82 }
83 }
84 }
85}