Flush the write buffer to disk (group commit). This issues a single write + fsync for all records accumulated since the last flush. The DWB is also fsynced (one fsync for all deferred DWB writes in this batch).
(&mut self)
| 344 | /// since the last flush. The DWB is also fsynced (one fsync for all |
| 345 | /// deferred DWB writes in this batch). |
| 346 | pub fn sync(&mut self) -> Result<()> { |
| 347 | if self.buffer.is_empty() { |
| 348 | return Ok(()); |
| 349 | } |
| 350 | // Flush DWB first — records must be durable in DWB before WAL. |
| 351 | // If DWB flush fails, torn-write protection is lost for this batch. |
| 352 | // We log a warning and detach the DWB rather than silently continuing |
| 353 | // as if torn-write protection is active. |
| 354 | if let Some(dwb) = &mut self.double_write |
| 355 | && let Err(e) = dwb.flush() |
| 356 | { |
| 357 | tracing::warn!( |
| 358 | error = %e, |
| 359 | "DWB flush failed — torn-write protection lost for this batch, detaching DWB" |
| 360 | ); |
| 361 | self.double_write = None; |
| 362 | } |
| 363 | self.flush_buffer()?; |
| 364 | self.file.sync_all()?; |
| 365 | Ok(()) |
| 366 | } |
| 367 | |
| 368 | /// Seal the WAL — no more writes will be accepted. |
| 369 | /// |