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

Function try_read_preamble

nodedb-wal/src/reader.rs:171–195  ·  view source on GitHub ↗

Attempt to read a WAL segment preamble from the start of a file. Returns `(Some(preamble), PREAMBLE_SIZE)` if a valid `WALP` preamble is found, or `(None, 0)` if the file does not start with the preamble magic (unencrypted segment — seek back to 0).

(file: &mut File)

Source from the content-addressed store, hash-verified

169/// found, or `(None, 0)` if the file does not start with the preamble magic
170/// (unencrypted segment — seek back to 0).
171fn try_read_preamble(file: &mut File) -> Result<(Option<SegmentPreamble>, u64)> {
172 use std::io::Seek;
173
174 let mut buf = [0u8; PREAMBLE_SIZE];
175 match file.read_exact(&mut buf) {
176 Ok(()) => {}
177 Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
178 // File is too short to hold a preamble — no preamble present.
179 file.seek(std::io::SeekFrom::Start(0))?;
180 return Ok((None, 0));
181 }
182 Err(e) => return Err(WalError::Io(e)),
183 }
184
185 if buf[0..4] == WAL_PREAMBLE_MAGIC {
186 // Parse and validate the preamble. An unsupported version is a hard
187 // error — do not silently fall through to record scanning.
188 let preamble = SegmentPreamble::from_bytes(&buf, &WAL_PREAMBLE_MAGIC)?;
189 Ok((Some(preamble), PREAMBLE_SIZE as u64))
190 } else {
191 // First bytes are not the preamble magic — rewind and read as records.
192 file.seek(std::io::SeekFrom::Start(0))?;
193 Ok((None, 0))
194 }
195}
196
197/// Iterator over WAL records.
198pub struct WalRecordIter {

Callers 1

openMethod · 0.85

Calls 2

kindMethod · 0.80
read_exactMethod · 0.45

Tested by

no test coverage detected