polodb_core/
config.rs

1// Copyright 2024 Vincent Chan
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//	http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15///
16/// Config builder for the database
17///
18/// ```rust
19/// use polodb_core::ConfigBuilder;
20/// let mut config_builder = ConfigBuilder::new();
21/// config_builder
22///     .set_init_block_count(100)
23///     .set_journal_full_size(1000);
24/// let config = config_builder.take();
25/// ```
26pub struct ConfigBuilder {
27    inner: Config,
28}
29
30impl ConfigBuilder {
31
32    pub fn new() -> ConfigBuilder {
33        ConfigBuilder {
34            inner: Config::default(),
35        }
36    }
37
38    pub fn get_init_block_count(&self) -> u64 {
39        self.inner.init_block_count
40    }
41
42    pub fn set_init_block_count(&mut self, v: u64) -> &mut Self {
43        self.inner.init_block_count = v;
44        self
45    }
46
47    pub fn get_journal_full_size(&self) -> u64 {
48        self.inner.journal_full_size
49    }
50
51    pub fn set_journal_full_size(&mut self, v: u64) -> &mut Self {
52        self.inner.journal_full_size = v;
53        self
54    }
55
56    pub fn get_lsm_page_size(&self) -> u32 {
57        self.inner.lsm_page_size
58    }
59
60    pub fn set_lsm_page_size(&mut self, v: u32) -> &mut Self {
61        self.inner.lsm_page_size = v;
62        self
63    }
64
65    pub fn get_lsm_block_size(&self) -> u32 {
66        self.inner.lsm_block_size
67    }
68
69    pub fn set_lsm_block_size(&mut self, v: u32) -> &mut Self {
70        self.inner.lsm_block_size = v;
71        self
72    }
73
74    pub fn get_sync_log_count(&self) -> u64 {
75        self.inner.sync_log_count
76    }
77
78    pub fn set_sync_log_count(&mut self, v: u64) -> &mut Self {
79        self.inner.sync_log_count = v;
80        self
81    }
82
83    pub fn take(self) -> Config {
84        self.inner
85    }
86
87}
88
89impl Default for ConfigBuilder {
90    fn default() -> Self {
91        Self::new()
92    }
93}
94
95pub struct Config {
96    pub init_block_count:  u64,
97    pub journal_full_size: u64,
98    pub lsm_page_size:     u32,
99    pub lsm_block_size:    u32,
100    pub sync_log_count:    u64,
101}
102
103const SYNC_LOG_COUNT: u64 = 1000;
104
105impl Default for Config {
106
107    fn default() -> Self {
108        Config {
109            init_block_count: 16,
110            journal_full_size: 1000,
111            lsm_page_size: 4096,
112            lsm_block_size: 4 * 1024 * 1024,
113            sync_log_count: SYNC_LOG_COUNT,
114        }
115    }
116
117}