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