Skip to main content

modelvault_core/
config.rs

1//! Open and recovery options for [`crate::db::Database`].
2
3/// Metadata about recovery actions applied during open.
4#[derive(Debug, Clone, Default, PartialEq, Eq)]
5pub struct OpenRecoveryInfo {
6    /// Bytes truncated from the file tail during open (0 if none).
7    pub truncated_bytes: u64,
8    /// Human-readable reason when truncation occurred.
9    pub truncate_reason: Option<String>,
10}
11
12/// How to open a database when the append log tail may be torn or hold an uncommitted transaction.
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum RecoveryMode {
15    /// Truncate the store to the last committed prefix (see `docs/migration_0.7_to_0.8.md`).
16    AutoTruncate,
17    /// Return [`crate::error::FormatError::UncleanLogTail`] if the tail is not fully committed.
18    Strict,
19}
20
21/// Open mode for on-disk databases.
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum OpenMode {
24    /// Read-only handle; does not create files and does not write.
25    ReadOnly,
26    /// Read/write handle; creates the file if missing.
27    ReadWrite,
28}
29
30/// Options for [`crate::db::Database::open_with_options`] and in-memory open helpers.
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub struct OpenOptions {
33    pub recovery: RecoveryMode,
34    pub mode: OpenMode,
35}
36
37impl Default for OpenOptions {
38    fn default() -> Self {
39        Self {
40            recovery: RecoveryMode::AutoTruncate,
41            mode: OpenMode::ReadWrite,
42        }
43    }
44}
45
46/// Builder for [`OpenOptions`].
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub struct OpenOptionsBuilder {
49    recovery: RecoveryMode,
50    mode: OpenMode,
51}
52
53impl Default for OpenOptionsBuilder {
54    fn default() -> Self {
55        let opts = OpenOptions::default();
56        Self {
57            recovery: opts.recovery,
58            mode: opts.mode,
59        }
60    }
61}
62
63impl OpenOptions {
64    /// Start building open options (defaults match [`OpenOptions::default`]).
65    pub fn builder() -> OpenOptionsBuilder {
66        OpenOptionsBuilder::default()
67    }
68}
69
70impl OpenOptionsBuilder {
71    pub fn recovery(mut self, recovery: RecoveryMode) -> Self {
72        self.recovery = recovery;
73        self
74    }
75
76    pub fn mode(mut self, mode: OpenMode) -> Self {
77        self.mode = mode;
78        self
79    }
80
81    pub fn read_only(self) -> Self {
82        self.mode(OpenMode::ReadOnly)
83    }
84
85    pub fn read_write(self) -> Self {
86        self.mode(OpenMode::ReadWrite)
87    }
88
89    pub fn build(self) -> OpenOptions {
90        OpenOptions {
91            recovery: self.recovery,
92            mode: self.mode,
93        }
94    }
95}