Skip to main content

alopex_embedded/
options.rs

1//! Database configuration options for the embedded API.
2
3use alopex_core::StorageMode;
4use std::path::Path;
5
6/// Options for configuring how a database instance is opened.
7#[derive(Debug, Clone, Default)]
8pub struct DatabaseOptions {
9    /// Whether to use in-memory mode.
10    memory_mode: bool,
11    /// Optional memory limit for in-memory mode (bytes).
12    memory_limit: Option<usize>,
13}
14
15impl DatabaseOptions {
16    /// Returns disk-backed options (default).
17    pub fn new() -> Self {
18        Self::default()
19    }
20
21    /// Creates options configured for in-memory mode without a memory cap.
22    pub fn in_memory() -> Self {
23        Self {
24            memory_mode: true,
25            memory_limit: None,
26        }
27    }
28
29    /// Sets an optional memory limit (bytes) and enables in-memory mode.
30    pub fn with_memory_limit(mut self, bytes: usize) -> Self {
31        self.memory_mode = true;
32        self.memory_limit = Some(bytes);
33        self
34    }
35
36    /// Returns true when in-memory mode is enabled.
37    pub fn memory_mode(&self) -> bool {
38        self.memory_mode
39    }
40
41    /// Returns the optional memory limit when configured.
42    pub fn memory_limit(&self) -> Option<usize> {
43        self.memory_limit
44    }
45
46    /// Convert the options to a core StorageMode.
47    pub(crate) fn to_storage_mode(&self, path: Option<&Path>) -> StorageMode {
48        if self.memory_mode {
49            StorageMode::Memory {
50                max_size: self.memory_limit,
51            }
52        } else {
53            let disk_path = path.expect("disk mode requires a path");
54            StorageMode::Disk {
55                path: crate::disk_data_dir_path(disk_path),
56                config: None,
57            }
58        }
59    }
60}