Open or create a WAL file at the given path.
(path: &Path, config: WalWriterConfig)
| 137 | impl WalWriter { |
| 138 | /// Open or create a WAL file at the given path. |
| 139 | pub fn open(path: &Path, config: WalWriterConfig) -> Result<Self> { |
| 140 | let mut opts = OpenOptions::new(); |
| 141 | opts.create(true).write(true).append(false); |
| 142 | |
| 143 | #[cfg(not(target_arch = "wasm32"))] |
| 144 | if config.use_direct_io { |
| 145 | // O_DIRECT: bypass page cache. |
| 146 | opts.custom_flags(libc::O_DIRECT); |
| 147 | } |
| 148 | |
| 149 | let file = opts.open(path)?; |
| 150 | |
| 151 | let buffer = AlignedBuf::new(config.write_buffer_size, config.alignment)?; |
| 152 | |
| 153 | // Scan existing WAL for recovery if the file has data. |
| 154 | let (file_offset, next_lsn) = if path.exists() && std::fs::metadata(path)?.len() > 0 { |
| 155 | let info = crate::recovery::recover(path)?; |
| 156 | (info.end_offset, info.next_lsn()) |
| 157 | } else { |
| 158 | (0, 1) |
| 159 | }; |
| 160 | |
| 161 | let double_write = open_dwb_for(&config, path); |
| 162 | |
| 163 | Ok(Self { |
| 164 | file, |
| 165 | buffer, |
| 166 | file_offset, |
| 167 | next_lsn: AtomicU64::new(next_lsn), |
| 168 | sealed: false, |
| 169 | config, |
| 170 | encryption_ring: None, |
| 171 | segment_preamble: None, |
| 172 | double_write, |
| 173 | }) |
| 174 | } |
| 175 | |
| 176 | /// Set the encryption key. When set, all subsequent records will have |
| 177 | /// their payloads encrypted with AES-256-GCM. |