Skip to main content

WalWriteAheadLog

Struct WalWriteAheadLog 

Source
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

Source

pub fn new(config: WalConfig) -> Self

Creates a new WAL with the given configuration.

Source

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.

Source

pub fn begin_transaction(&mut self) -> u64

Begins a new transaction. Returns the transaction id.

Source

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.

Source

pub fn commit_transaction(&mut self, tx_id: u64) -> Result<(), WalError>

Commits a transaction: writes the Commit marker and marks it committed.

Source

pub fn rollback_transaction(&mut self, tx_id: u64) -> Result<(), WalError>

Rolls back a transaction: writes the Rollback marker and marks it rolled back.

Source

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.

Source

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.

Source

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.

Source

pub fn export_segment(&self) -> Vec<u8>

Returns a clone of the current segment buffer.

Source

pub fn stats(&self) -> WalStats

Returns a snapshot of current statistics.

Source

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.

Source

pub fn entry_count(&self) -> usize

Returns the total number of in-memory entries.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more