memorable/bounded/
options.rs

1pub use skl::{
2  options::{CompressionPolicy, Freelist},
3  Height, KeySize,
4};
5
6use skl::Options as SklOptions;
7
8use super::sealed::Constructable;
9
10/// The options for the `Memtable`.
11#[derive(Debug, Clone, Copy)]
12pub struct Options {
13  capacity: u32,
14  maximum_height: Height,
15  maximum_key_size: KeySize,
16  maximum_value_size: u32,
17  freelist: Freelist,
18  policy: CompressionPolicy,
19  reserved: u32,
20}
21
22impl Default for Options {
23  #[inline]
24  fn default() -> Self {
25    Self::new()
26  }
27}
28
29impl Options {
30  /// Creates a new `Options` with default values.
31  #[inline]
32  pub const fn new() -> Self {
33    Self {
34      capacity: 8 * 1024 * 1024, // 8MB
35      maximum_height: Height::new(),
36      maximum_key_size: KeySize::new(),
37      maximum_value_size: u32::MAX,
38      freelist: Freelist::None,
39      policy: CompressionPolicy::Fast,
40      reserved: 0,
41    }
42  }
43
44  /// Sets the capacity of the memtable.
45  ///
46  /// The default value is `8MB`.
47  ///
48  /// The capacity is the maximum size in bytes of the memtable.
49  ///
50  /// ## Example
51  ///
52  /// ```rust
53  /// use memorable::bounded::Options;
54  ///
55  /// let options = Options::new().with_capacity(1024 * 1024);
56  /// ```
57  #[inline]
58  pub const fn with_capacity(mut self, capacity: u32) -> Self {
59    self.capacity = capacity;
60    self
61  }
62
63  /// Returns the capacity of the memtable.
64  ///
65  /// The default value is `8MB`.
66  ///
67  /// The capacity is the maximum size in bytes of the memtable.
68  ///
69  /// ## Example
70  ///
71  /// ```rust
72  /// use memorable::bounded::Options;
73  ///
74  /// let options = Options::new().with_capacity(1024 * 1024);
75  /// assert_eq!(options.capacity(), 1024 * 1024);
76  /// ```
77  #[inline]
78  pub const fn capacity(&self) -> u32 {
79    self.capacity
80  }
81
82  /// Sets the maximum height of the node of the skiplist.
83  ///
84  /// The default value is `20`.
85  ///
86  /// ## Example
87  ///
88  /// ```rust
89  /// use memorable::bounded::{Options, Height};
90  ///
91  /// let options = Options::new().with_maximum_height(Height::with(10));
92  /// ```
93  #[inline]
94  pub const fn with_maximum_height(mut self, maximum_height: Height) -> Self {
95    self.maximum_height = maximum_height;
96    self
97  }
98
99  /// Returns the maximum height of the node of the skiplist.
100  ///
101  /// The default value is `20`.
102  ///
103  /// ## Example
104  ///
105  /// ```rust
106  /// use memorable::bounded::{Options, Height};
107  ///
108  /// let options = Options::new().with_maximum_height(Height::with(10));
109  /// assert_eq!(options.maximum_height(), Height::with(10));
110  /// ```
111  #[inline]
112  pub const fn maximum_height(&self) -> Height {
113    self.maximum_height
114  }
115
116  /// Sets the maximum key size of the memtable.
117  ///
118  /// The default value is `u16::MAX`.
119  ///
120  /// ## Example
121  ///
122  /// ```rust
123  /// use memorable::bounded::{Options, KeySize};
124  ///
125  /// let options = Options::new().with_maximum_key_size(KeySize::with(1024));
126  /// ```
127  #[inline]
128  pub const fn with_maximum_key_size(mut self, maximum_key_size: KeySize) -> Self {
129    self.maximum_key_size = maximum_key_size;
130    self
131  }
132
133  /// Returns the maximum key size of the memtable.
134  ///
135  /// The default value is `u16::MAX`.
136  ///
137  /// ## Example
138  ///
139  /// ```rust
140  /// use memorable::bounded::{Options, KeySize};
141  ///
142  /// let options = Options::new().with_maximum_key_size(KeySize::with(1024));
143  /// assert_eq!(options.maximum_key_size(), KeySize::with(1024));
144  /// ```
145  #[inline]
146  pub const fn maximum_key_size(&self) -> KeySize {
147    self.maximum_key_size
148  }
149
150  /// Sets the maximum value size of the memtable.
151  ///
152  /// The default value is `u32::MAX`.
153  ///
154  /// ## Example
155  ///
156  /// ```rust
157  /// use memorable::bounded::Options;
158  ///
159  /// let options = Options::new().with_maximum_value_size(1024 * 1024);
160  /// assert_eq!(options.maximum_value_size(), 1024 * 1024);
161  /// ```
162  #[inline]
163  pub const fn with_maximum_value_size(mut self, maximum_value_size: u32) -> Self {
164    self.maximum_value_size = maximum_value_size;
165    self
166  }
167
168  /// Returns the maximum value size of the memtable.
169  ///
170  /// The default value is `u32::MAX`.
171  ///
172  /// ## Example
173  ///
174  /// ```rust
175  /// use memorable::bounded::Options;
176  ///
177  /// let options = Options::new().with_maximum_value_size(1024 * 1024);
178  /// assert_eq!(options.maximum_value_size(), 1024 * 1024);
179  /// ```
180  #[inline]
181  pub const fn maximum_value_size(&self) -> u32 {
182    self.maximum_value_size
183  }
184
185  /// Sets the freelist policy of the underlying ARENA allocator.
186  ///
187  /// Default is `Freelist::None`.
188  ///
189  /// ## Example
190  ///
191  /// ```rust
192  /// use memorable::bounded::{Options, Freelist};
193  ///
194  /// let options = Options::new().with_freelist(Freelist::Optimistic);
195  /// ```
196  #[inline]
197  pub const fn with_freelist(mut self, freelist: Freelist) -> Self {
198    self.freelist = freelist;
199    self
200  }
201
202  /// Returns the freelist policy of the underlying ARENA allocator.
203  ///
204  /// Default is `Freelist::None`.
205  ///
206  /// ## Example
207  ///
208  /// ```rust
209  /// use memorable::bounded::{Options, Freelist};
210  ///
211  /// let options = Options::new().with_freelist(Freelist::Optimistic);
212  /// assert_eq!(options.freelist(), Freelist::Optimistic);
213  /// ```
214  #[inline]
215  pub const fn freelist(&self) -> Freelist {
216    self.freelist
217  }
218
219  /// Sets the compression policy of the memtable.
220  ///
221  /// Default is `CompressionPolicy::Fast`.
222  ///
223  /// ## Example
224  ///
225  /// ```rust
226  /// use memorable::bounded::{Options, CompressionPolicy};
227  ///
228  /// let options = Options::new().with_compression_policy(CompressionPolicy::Fast);
229  /// ```
230  #[inline]
231  pub const fn with_compression_policy(mut self, policy: CompressionPolicy) -> Self {
232    self.policy = policy;
233    self
234  }
235
236  /// Returns the compression policy of the memtable.
237  ///
238  /// Default is `CompressionPolicy::Fast`.
239  ///
240  /// ## Example
241  ///
242  /// ```rust
243  /// use memorable::bounded::{Options, CompressionPolicy};
244  ///
245  /// let options = Options::new().with_compression_policy(CompressionPolicy::Fast);
246  /// assert_eq!(options.compression_policy(), CompressionPolicy::Fast);
247  /// ```
248  #[inline]
249  pub const fn compression_policy(&self) -> CompressionPolicy {
250    self.policy
251  }
252
253  /// Sets the reserved space of the memtable.
254  ///
255  /// Default is `0`.
256  ///
257  /// The reserved space is the space that is allocated for users to store some metadata,
258  /// and will not be used by the memtable.
259  ///
260  /// ## Example
261  ///
262  /// ```rust
263  /// use memorable::bounded::Options;
264  ///
265  /// let options = Options::new().with_reserved(1024);
266  /// ```
267  #[inline]
268  pub const fn with_reserved(mut self, reserved: u32) -> Self {
269    self.reserved = reserved;
270    self
271  }
272
273  /// Returns the reserved space of the memtable.
274  ///
275  /// Default is `0`.
276  ///
277  /// The reserved space is the space that is allocated for users to store some metadata,
278  /// and will not be used by the memtable.
279  ///
280  /// ## Example
281  ///
282  /// ```rust
283  /// use memorable::bounded::Options;
284  ///
285  /// let options = Options::new().with_reserved(1024);
286  /// assert_eq!(options.reserved(), 1024);
287  /// ```
288  #[inline]
289  pub const fn reserved(&self) -> u32 {
290    self.reserved
291  }
292
293  /// Allocates a new bounded memtable with the options.
294  ///
295  /// ## Example
296  ///
297  /// ```rust
298  /// use memorable::bounded::{Options, generic::Memtable};
299  ///
300  /// let table = Options::new().alloc::<Memtable<String, String>>().unwrap();
301  /// ```
302  #[inline]
303  pub fn alloc<M>(self) -> Result<M, skl::error::Error>
304  where
305    M: Constructable,
306  {
307    M::construct(self)
308  }
309
310  /// Allocates a new bounded memtable with the options, the backing memory is anonymous.
311  ///
312  /// ## Example
313  ///
314  /// ```rust
315  /// use memorable::bounded::{Options, generic::Memtable};
316  ///
317  /// let table = Options::new().map_anon::<Memtable<String, String>>().unwrap();
318  /// ```
319  #[cfg(all(feature = "memmap", not(target_family = "wasm")))]
320  #[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
321  #[inline]
322  pub fn map_anon<M>(self) -> std::io::Result<M>
323  where
324    M: Constructable,
325  {
326    M::map_anon(self)
327  }
328
329  /// Converts the options to the `SklOptions`.
330  #[allow(clippy::wrong_self_convention)]
331  #[inline]
332  pub(super) fn to_skl_options(&self) -> SklOptions {
333    SklOptions::new()
334      .with_capacity(self.capacity)
335      .with_max_height(self.maximum_height)
336      .with_max_key_size(self.maximum_key_size)
337      .with_max_value_size(self.maximum_value_size)
338      .with_freelist(self.freelist)
339      .with_compression_policy(self.policy)
340      .with_reserved(self.reserved)
341  }
342}