pub struct SegmentedWal { /* private fields */ }Expand description
A segmented write-ahead log.
Manages a directory of WAL segment files. Records are appended to the active
segment. When the active segment exceeds segment_target_size, a new segment
is created automatically on the next append.
Implementations§
Source§impl SegmentedWal
impl SegmentedWal
Sourcepub fn open(config: SegmentedWalConfig) -> Result<Self>
pub fn open(config: SegmentedWalConfig) -> Result<Self>
Open or create a segmented WAL in the given directory.
On first startup, creates the directory and the first segment. On subsequent startups, discovers existing segments and opens the last one for continued appending.
Sourcepub fn set_encryption_ring(&mut self, ring: KeyRing) -> Result<()>
pub fn set_encryption_ring(&mut self, ring: KeyRing) -> Result<()>
Set the encryption key ring. All subsequent records will be encrypted.
Must be called before the first append. Returns an error if records
have already been written to the active segment.
Sourcepub fn encryption_ring(&self) -> Option<&KeyRing>
pub fn encryption_ring(&self) -> Option<&KeyRing>
Get the encryption key ring (for replay decryption).
Sourcepub fn append(
&mut self,
record_type: u32,
tenant_id: u64,
vshard_id: u32,
database_id: u64,
payload: &[u8],
) -> Result<u64>
pub fn append( &mut self, record_type: u32, tenant_id: u64, vshard_id: u32, database_id: u64, payload: &[u8], ) -> Result<u64>
Append a record to the WAL. Returns the assigned LSN.
If the active segment has exceeded the target size, a new segment is created before appending. The rollover is transparent to the caller.
database_id is stored in header bytes 34-41. Pass 0 for the
default database (backward-compatible with pre-existing records).
Sourcepub fn active_segment_first_lsn(&self) -> u64
pub fn active_segment_first_lsn(&self) -> u64
First LSN of the active (currently written) segment.
Sourcepub fn truncate_before(&self, checkpoint_lsn: u64) -> Result<TruncateResult>
pub fn truncate_before(&self, checkpoint_lsn: u64) -> Result<TruncateResult>
Truncate old WAL segments that are fully below the checkpoint LSN.
Only sealed (non-active) segments are eligible for deletion. The active segment is never deleted.
Returns the number of segments deleted and bytes reclaimed.
Sourcepub fn replay(&self) -> Result<Vec<WalRecord>>
pub fn replay(&self) -> Result<Vec<WalRecord>>
Replay all committed records across all segments, in LSN order.
Used for crash recovery on startup.
Sourcepub fn replay_from(&self, from_lsn: u64) -> Result<Vec<WalRecord>>
pub fn replay_from(&self, from_lsn: u64) -> Result<Vec<WalRecord>>
Replay only records with LSN >= from_lsn.
Used when recovering from a checkpoint — records before the checkpoint LSN have already been applied from the snapshot.
Sourcepub fn replay_from_limit(
&self,
from_lsn: u64,
max_records: usize,
) -> Result<(Vec<WalRecord>, bool)>
pub fn replay_from_limit( &self, from_lsn: u64, max_records: usize, ) -> Result<(Vec<WalRecord>, bool)>
Paginated replay (delegates to standalone replay_from_limit_dir).
Sourcepub fn list_segments(&self) -> Result<Vec<SegmentMeta>>
pub fn list_segments(&self) -> Result<Vec<SegmentMeta>>
List all segment metadata (for monitoring / operational tooling).
Sourcepub fn total_size_bytes(&self) -> Result<u64>
pub fn total_size_bytes(&self) -> Result<u64>
Total WAL size on disk across all segments.