Initialize WAL by scanning existing files
(&mut self)
| 429 | |
| 430 | /// Initialize WAL by scanning existing files |
| 431 | fn initialize(&mut self) -> Result<(), WALError> { |
| 432 | let mut max_file_number = 0u64; |
| 433 | let mut max_global_sequence = 0u64; |
| 434 | |
| 435 | // Scan existing WAL files |
| 436 | if let Ok(entries) = std::fs::read_dir(&self.wal_dir) { |
| 437 | for entry in entries.flatten() { |
| 438 | if let Some(filename) = entry.file_name().to_str() { |
| 439 | if filename.starts_with("wal_") && filename.ends_with(".log") { |
| 440 | // Extract file number from filename (wal_NNNNNN.log) |
| 441 | if let Some(number_str) = filename |
| 442 | .strip_prefix("wal_") |
| 443 | .and_then(|s| s.strip_suffix(".log")) |
| 444 | { |
| 445 | if let Ok(file_number) = number_str.parse::<u64>() { |
| 446 | max_file_number = max_file_number.max(file_number); |
| 447 | |
| 448 | // Scan this file for the highest sequence number |
| 449 | if let Ok(entries) = self.read_wal_file(file_number) { |
| 450 | for entry in entries { |
| 451 | max_global_sequence = |
| 452 | max_global_sequence.max(entry.global_sequence); |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | // Set initial values |
| 463 | *self.current_file_number.lock().unwrap() = max_file_number; |
| 464 | *self.global_sequence.lock().unwrap() = max_global_sequence; |
| 465 | |
| 466 | // Open current WAL file for writing |
| 467 | self.rotate_wal_file()?; |
| 468 | |
| 469 | Ok(()) |
| 470 | } |
| 471 | |
| 472 | /// Write a WAL entry to persistent storage |
| 473 | pub fn write_entry(&self, entry: WALEntry) -> Result<(), WALError> { |
no test coverage detected