pub struct WalWriteAheadLog { /* private fields */ }Expand description
In-memory, crash-recovery Write-Ahead Log.
All entries are serialised into a Vec<u8> segment buffer using a
length-framed binary format with FNV-1a-64 checksums. The buffer can be
exported, persisted externally, and then fed back into Self::recover on
restart.
§Example
use ipfrs_storage::write_ahead_log::{WalWriteAheadLog, WalConfig, WalOpType};
let mut wal = WalWriteAheadLog::new(WalConfig::default());
let seq = wal.write(b"hello".to_vec(), b"world".to_vec(), WalOpType::Put).unwrap();
assert!(seq > 0);
let data = wal.export_segment();
assert!(!data.is_empty());Implementations§
Source§impl WalWriteAheadLog
impl WalWriteAheadLog
Sourcepub fn write(
&mut self,
key: Vec<u8>,
value: Vec<u8>,
op: WalOpType,
) -> Result<u64, WalError>
pub fn write( &mut self, key: Vec<u8>, value: Vec<u8>, op: WalOpType, ) -> Result<u64, WalError>
Writes a standalone (non-transactional) entry and returns its sequence number. Auto-checkpoints when the configured interval is reached.
Sourcepub fn begin_transaction(&mut self) -> u64
pub fn begin_transaction(&mut self) -> u64
Begins a new transaction. Returns the transaction id.
Sourcepub fn write_tx(
&mut self,
tx_id: u64,
key: Vec<u8>,
value: Vec<u8>,
op: WalOpType,
) -> Result<u64, WalError>
pub fn write_tx( &mut self, tx_id: u64, key: Vec<u8>, value: Vec<u8>, op: WalOpType, ) -> Result<u64, WalError>
Writes an entry within a transaction. Returns the entry sequence number.
Sourcepub fn commit_transaction(&mut self, tx_id: u64) -> Result<(), WalError>
pub fn commit_transaction(&mut self, tx_id: u64) -> Result<(), WalError>
Commits a transaction: writes the Commit marker and marks it committed.
Sourcepub fn rollback_transaction(&mut self, tx_id: u64) -> Result<(), WalError>
pub fn rollback_transaction(&mut self, tx_id: u64) -> Result<(), WalError>
Rolls back a transaction: writes the Rollback marker and marks it rolled back.
Sourcepub fn checkpoint(&mut self) -> Result<u64, WalError>
pub fn checkpoint(&mut self) -> Result<u64, WalError>
Creates a checkpoint entry and returns its sequence number.
All entries whose seq_num is less than or equal to the previous
checkpoint sequence, and which do not belong to an active
transaction, are removed from the in-memory entry list and the segment
is rebuilt.
Sourcepub fn recover(data: &[u8]) -> Result<RecoveryResult, WalError>
pub fn recover(data: &[u8]) -> Result<RecoveryResult, WalError>
Scans a raw byte buffer, decodes all valid entries, validates checksums,
and returns a RecoveryResult.
Entries belonging to transactions that were never committed (i.e. no matching Commit entry appears) are discarded from the result.
Sourcepub fn replay(entries: &[WalEntry], handler: &mut dyn FnMut(&WalEntry)) -> usize
pub fn replay(entries: &[WalEntry], handler: &mut dyn FnMut(&WalEntry)) -> usize
Iterates over the provided entries and calls handler for each committed
Put or Delete entry. Returns the number of entries passed to the handler.
Standalone (tx_id == 0) Put/Delete entries are always replayed.
Transactional entries are only replayed when their transaction’s Commit
marker is present in entries.
Sourcepub fn export_segment(&self) -> Vec<u8> ⓘ
pub fn export_segment(&self) -> Vec<u8> ⓘ
Returns a clone of the current segment buffer.
Sourcepub fn truncate_before(&mut self, seq_num: u64) -> usize
pub fn truncate_before(&mut self, seq_num: u64) -> usize
Removes all entries with seq_num < seq_num from the in-memory list and
rebuilds the segment buffer. Returns the number of entries removed.
Sourcepub fn entry_count(&self) -> usize
pub fn entry_count(&self) -> usize
Returns the total number of in-memory entries.
Auto Trait Implementations§
impl Freeze for WalWriteAheadLog
impl RefUnwindSafe for WalWriteAheadLog
impl Send for WalWriteAheadLog
impl Sync for WalWriteAheadLog
impl Unpin for WalWriteAheadLog
impl UnsafeUnpin for WalWriteAheadLog
impl UnwindSafe for WalWriteAheadLog
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more