Skip to main content

LogStore

Struct LogStore 

Source
pub struct LogStore { /* private fields */ }
Expand description

A directory of append-only log segments with a contiguous LSN sequence.

Implementations§

Source§

impl LogStore

Source

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.

Source

pub fn shard_id(&self) -> u32

The physical shard this log belongs to.

Source

pub fn next_lsn(&self) -> u64

The LSN the next appended record must carry.

Source

pub fn durable_lsn(&self) -> u64

The highest LSN appended so far (0 when empty). After Self::sync, this is the durable LSN.

Source

pub fn segments(&self) -> &[SegmentInfo]

The segment descriptors in LSN order.

Source

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.

Source

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.

Source

pub fn read_from(&self, from_lsn: u64) -> Result<Vec<(u64, Vec<u8>)>, LogError>

Read every record with LSN >= from_lsn, in order, as framed bytes.

§Errors

Returns LogError if a segment cannot be read or is corrupt.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for LogStore

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.