Scan a WAL file and recover the committed prefix. Returns `RecoveryInfo` describing the state of the WAL, or an error if the file cannot be opened.
(path: &Path)
| 47 | /// Returns `RecoveryInfo` describing the state of the WAL, or an error |
| 48 | /// if the file cannot be opened. |
| 49 | pub fn recover(path: &Path) -> Result<RecoveryInfo> { |
| 50 | if !path.exists() { |
| 51 | return Ok(RecoveryInfo { |
| 52 | last_lsn: 0, |
| 53 | record_count: 0, |
| 54 | end_offset: 0, |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | let mut reader = WalReader::open(path)?; |
| 59 | let mut last_lsn = 0u64; |
| 60 | let mut record_count = 0u64; |
| 61 | let mut last_valid_offset = 0u64; |
| 62 | |
| 63 | loop { |
| 64 | let offset_before = reader.offset(); |
| 65 | match reader.next_record() { |
| 66 | Ok(Some(record)) => { |
| 67 | last_lsn = record.header.lsn; |
| 68 | record_count += 1; |
| 69 | last_valid_offset = |
| 70 | offset_before + HEADER_SIZE as u64 + record.header.payload_len as u64; |
| 71 | } |
| 72 | Ok(None) => { |
| 73 | // End of committed prefix (EOF or corruption). |
| 74 | break; |
| 75 | } |
| 76 | Err(e @ WalError::UnknownRequiredRecordType { .. }) => { |
| 77 | // Cannot proceed past unknown required records. |
| 78 | return Err(e); |
| 79 | } |
| 80 | Err(e) => return Err(e), |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | Ok(RecoveryInfo { |
| 85 | last_lsn, |
| 86 | record_count, |
| 87 | end_offset: last_valid_offset, |
| 88 | }) |
| 89 | } |
| 90 | |
| 91 | #[cfg(test)] |
| 92 | mod tests { |