pub struct LogStore { /* private fields */ }Expand description
A directory of append-only log segments with a contiguous LSN sequence.
Implementations§
Source§impl LogStore
impl LogStore
Sourcepub fn open(
dir: &Path,
shard_id: u32,
segment_bytes: u64,
) -> Result<Self, LogError>
pub fn open( dir: &Path, shard_id: u32, segment_bytes: u64, ) -> Result<Self, LogError>
Open (creating if absent) a log directory and recover its state.
Existing segments are validated in order. A torn trailing record in the
last segment is truncated away (an interrupted append); the same
damage in a sealed segment is LogError::Corrupt. The next expected
LSN is one past the highest recovered record (1 for an empty log).
§Errors
Returns LogError if the directory cannot be created or read, or a
segment is corrupt.
Sourcepub fn durable_lsn(&self) -> u64
pub fn durable_lsn(&self) -> u64
The highest LSN appended so far (0 when empty). After Self::sync,
this is the durable LSN.
Sourcepub fn segments(&self) -> &[SegmentInfo]
pub fn segments(&self) -> &[SegmentInfo]
The segment descriptors in LSN order.
Sourcepub fn append_batch(
&mut self,
records: &[(u64, Vec<u8>)],
) -> Result<(), LogError>
pub fn append_batch( &mut self, records: &[(u64, Vec<u8>)], ) -> Result<(), LogError>
Append framed records (each produced by crate::wal::encode_wal_record
or crate::wal::encode_chained_ops_record) with contiguous, ascending
LSNs. Chaining (large-transaction support): several
consecutive entries may share one LSN — a chained commit’s fragments,
which must all be present in this same call (never split across two
append_batch calls: see the caller’s own invariant in
Store::append_log) — and are grouped and written as one atomic unit:
next_lsn advances only once per group, and the whole group is kept
in one segment file (rotating before its first fragment if it would
not otherwise fit, rather than splitting it across two segments).
Records are written to the active segment, rotating to a new segment
once the configured size is reached. The write is buffered by the OS;
call Self::sync to make it durable before acknowledging.
§Errors
Returns LogError::OutOfOrder if an LSN is not the expected next LSN,
LogError::Oversized if a record exceeds the ceiling, or
LogError::Io on write failure.
Sourcepub fn sync(&mut self) -> Result<(), LogError>
pub fn sync(&mut self) -> Result<(), LogError>
Make all appended records durable (fsync the active segment and the directory so a freshly created segment name survives a crash).
§Errors
Returns LogError::Io if the fsync fails.
Sourcepub fn segments_in(
&self,
from_lsn: u64,
until_lsn: u64,
) -> Vec<(SegmentInfo, bool)>
pub fn segments_in( &self, from_lsn: u64, until_lsn: u64, ) -> Vec<(SegmentInfo, bool)>
Descriptors of the segments that may hold LSNs in [from_lsn, until_lsn), each flagged when it is the active (last) segment. Callers
read them with read_segment_records after releasing the log
lock, so a retention sweep never blocks WAL appends while it reads.
Sourcepub fn truncate_before(&mut self, lsn: u64) -> Result<usize, LogError>
pub fn truncate_before(&mut self, lsn: u64) -> Result<usize, LogError>
Delete sealed segments that end before lsn. The active segment is
never removed, so the log always retains its live tail. Returns the
number of segments deleted.
§Errors
Returns LogError::Io if a segment file cannot be removed.