MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / recover

Function recover

nodedb-wal/src/recovery.rs:49–89  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

47/// Returns `RecoveryInfo` describing the state of the WAL, or an error
48/// if the file cannot be opened.
49pub 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)]
92mod tests {

Callers 12

recover_empty_walFunction · 0.85
recover_nonexistent_fileFunction · 0.85
recover_with_recordsFunction · 0.85
recover_truncated_walFunction · 0.85
openMethod · 0.85
openMethod · 0.85
torn_write_mid_headerFunction · 0.85
many_records_roundtripFunction · 0.85
validate_for_startupMethod · 0.85

Calls 4

openFunction · 0.50
existsMethod · 0.45
offsetMethod · 0.45
next_recordMethod · 0.45

Tested by 9

recover_empty_walFunction · 0.68
recover_nonexistent_fileFunction · 0.68
recover_with_recordsFunction · 0.68
recover_truncated_walFunction · 0.68
torn_write_mid_headerFunction · 0.68
many_records_roundtripFunction · 0.68