noxu_db/environment_mutable_config.rs
1//! Runtime-mutable environment configuration.
2//!
3//! Implements `EnvironmentMutableConfig`.
4
5use crate::durability::Durability;
6
7/// The subset of environment properties that can be changed after the
8/// environment has been opened.
9///
10/// Obtain via [`Environment::get_mutable_config`][crate::environment::Environment::get_mutable_config]
11/// and apply via [`Environment::set_mutable_config`][crate::environment::Environment::set_mutable_config].
12///
13/// Implements `EnvironmentMutableConfig`.
14///
15/// # Example
16/// ```ignore
17/// let mut cfg = env.get_mutable_config()?;
18/// cfg.cache_size = Some(256 * 1024 * 1024); // 256 MiB
19/// env.set_mutable_config(cfg)?;
20/// ```
21#[derive(Clone, Debug, Default)]
22pub struct EnvironmentMutableConfig {
23 /// Override the B-tree cache size in bytes. `None` means unchanged.
24 ///
25 /// Implements `EnvironmentMutableConfig.setCacheSize()`.
26 pub cache_size: Option<usize>,
27
28 /// Override the default transaction durability for this environment.
29 /// `None` means unchanged.
30 ///
31 /// Implements `EnvironmentMutableConfig.setDurability()`.
32 pub durability: Option<Durability>,
33
34 /// If `true`, committed transactions do not flush to disk (no-sync).
35 ///
36 /// **Deprecated since 2.4.1** — use [`durability`][Self::durability]
37 /// with `Durability::commit_no_sync()` instead.
38 pub txn_no_sync: bool,
39
40 /// If `true`, committed transactions flush to the OS buffer but do not
41 /// call `fdatasync` (write-no-sync).
42 ///
43 /// **Deprecated since 2.4.1** — use [`durability`][Self::durability]
44 /// with `Durability::commit_write_no_sync()` instead.
45 pub txn_write_no_sync: bool,
46
47 /// Enable or disable the cleaner daemon. `None` means unchanged.
48 pub run_cleaner: Option<bool>,
49
50 /// Enable or disable the checkpointer daemon. `None` means unchanged.
51 pub run_checkpointer: Option<bool>,
52
53 /// Enable or disable the evictor daemon. `None` means unchanged.
54 pub run_evictor: Option<bool>,
55
56 /// Lock timeout in milliseconds. `None` means unchanged.
57 ///
58 /// To explicitly clear a previously-configured timeout, set
59 /// `Some(0)` (which JE interprets as "no timeout"). v1.5.0 used a
60 /// `u64` with `0` as the unchanged sentinel which made it
61 /// impossible to clear a timeout; see
62 /// (Transaction-Env F19/F20).
63 pub lock_timeout_ms: Option<u64>,
64
65 /// Transaction timeout in milliseconds. `None` means unchanged.
66 ///
67 /// `Some(0)` clears any previously-configured timeout.
68 pub txn_timeout_ms: Option<u64>,
69}
70
71impl EnvironmentMutableConfig {
72 /// Creates a new `EnvironmentMutableConfig` with no changes pending.
73 pub fn new() -> Self {
74 Self::default()
75 }
76
77 /// Sets the cache size override.
78 pub fn with_cache_size(mut self, bytes: usize) -> Self {
79 self.cache_size = Some(bytes);
80 self
81 }
82
83 /// Sets the durability override.
84 pub fn with_durability(mut self, durability: Durability) -> Self {
85 self.durability = Some(durability);
86 self
87 }
88
89 /// Sets the `txn_no_sync` flag.
90 ///
91 /// **Deprecated** — use
92 /// [`with_durability`][Self::with_durability] with
93 /// `Durability::commit_no_sync()` instead.
94 #[deprecated(
95 since = "2.4.1",
96 note = "use with_durability(Durability::commit_no_sync()) instead"
97 )]
98 pub fn with_txn_no_sync(mut self, no_sync: bool) -> Self {
99 self.txn_no_sync = no_sync;
100 self
101 }
102
103 /// Sets the `txn_write_no_sync` flag.
104 ///
105 /// **Deprecated** — use
106 /// [`with_durability`][Self::with_durability] with
107 /// `Durability::commit_write_no_sync()` instead.
108 #[deprecated(
109 since = "2.4.1",
110 note = "use with_durability(Durability::commit_write_no_sync()) instead"
111 )]
112 pub fn with_txn_write_no_sync(mut self, write_no_sync: bool) -> Self {
113 self.txn_write_no_sync = write_no_sync;
114 self
115 }
116
117 /// Enables/disables the cleaner daemon.
118 pub fn with_run_cleaner(mut self, run: bool) -> Self {
119 self.run_cleaner = Some(run);
120 self
121 }
122
123 /// Enables/disables the checkpointer daemon.
124 pub fn with_run_checkpointer(mut self, run: bool) -> Self {
125 self.run_checkpointer = Some(run);
126 self
127 }
128
129 /// Enables/disables the evictor daemon.
130 pub fn with_run_evictor(mut self, run: bool) -> Self {
131 self.run_evictor = Some(run);
132 self
133 }
134
135 /// Sets the lock timeout (milliseconds).
136 ///
137 /// Pass `Some(0)` to clear a previously-configured timeout, or
138 /// `None` to leave it unchanged.
139 pub fn with_lock_timeout_ms(mut self, ms: Option<u64>) -> Self {
140 self.lock_timeout_ms = ms;
141 self
142 }
143
144 /// Sets the transaction timeout (milliseconds).
145 ///
146 /// Pass `Some(0)` to clear a previously-configured timeout, or
147 /// `None` to leave it unchanged.
148 pub fn with_txn_timeout_ms(mut self, ms: Option<u64>) -> Self {
149 self.txn_timeout_ms = ms;
150 self
151 }
152}
153
154#[cfg(test)]
155mod tests {
156 use super::*;
157
158 #[test]
159 fn default_leaves_timeouts_unchanged() {
160 let cfg = EnvironmentMutableConfig::new();
161 assert_eq!(cfg.lock_timeout_ms, None);
162 assert_eq!(cfg.txn_timeout_ms, None);
163 }
164
165 #[test]
166 fn with_lock_timeout_some_zero_means_clear() {
167 // Wave 1C audit cleanup (Transaction-Env F19/F20): the
168 // previous `u64` shape used 0 as the unchanged sentinel and
169 // could not distinguish "clear the timeout" from "unchanged".
170 let cfg = EnvironmentMutableConfig::new().with_lock_timeout_ms(Some(0));
171 assert_eq!(cfg.lock_timeout_ms, Some(0));
172 }
173
174 #[test]
175 fn with_txn_timeout_none_means_unchanged() {
176 let cfg = EnvironmentMutableConfig::new()
177 .with_txn_timeout_ms(Some(1_000))
178 .with_txn_timeout_ms(None);
179 assert_eq!(cfg.txn_timeout_ms, None);
180 }
181}