Skip to main content

nodedb_wal/double_write/
mode.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! I/O mode selection for the double-write buffer file.
4
5/// I/O mode for the double-write buffer file.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum DwbMode {
8    /// DWB disabled — no torn-write protection. `DoubleWriteBuffer::open`
9    /// returns `Err(WalError::DwbOffNotOpenable)`.
10    Off,
11    /// Buffered I/O (page cache + `fsync`). Default when the parent WAL
12    /// does not use `O_DIRECT`.
13    Buffered,
14    /// `O_DIRECT` I/O via an aligned buffer. The intended companion to an
15    /// `O_DIRECT` WAL: keeps DWB bytes out of the page cache.
16    Direct,
17}
18
19impl DwbMode {
20    /// Choose the DWB mode that mirrors the parent writer's O_DIRECT setting
21    /// when no explicit override is configured. With `O_DIRECT` on, the DWB
22    /// should also be `O_DIRECT`, otherwise it undoes the cache-bypass.
23    pub fn default_for_parent(parent_uses_direct_io: bool) -> Self {
24        if parent_uses_direct_io {
25            Self::Direct
26        } else {
27            Self::Buffered
28        }
29    }
30}