Skip to main content

MemoryLog

Struct MemoryLog 

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

An in-memory RaftLog backed by a Vec.

This is the default store and the one RaftNode::new uses. It keeps entries in a vector (entry at index i lives at slot i - 1) and the hard state in a field. Nothing is durable across a process restart — it is for tests, examples, and the single-node path, not production. Its operations never fail except on a misuse that would corrupt the log (a non-contiguous append or a truncate(0)).

After a snapshot is installed the log is compacted: entries up to the snapshot’s index are dropped and base_index / base_term become the log’s new starting boundary, so reads below the boundary return None while term_at still answers for the boundary index itself.

§Examples

use raft_io::{HardState, LogEntry, MemoryLog, RaftLog};

let mut log = MemoryLog::new();
assert_eq!(log.last_index(), 0);

log.append(&[LogEntry::new(1, 1, b"x".to_vec())]).unwrap();
log.set_hard_state(HardState { term: 1, voted_for: Some(1) }).unwrap();
log.sync().unwrap();

assert_eq!(log.last_index(), 1);
assert_eq!(log.hard_state().voted_for, Some(1));

Implementations§

Source§

impl MemoryLog

Source

pub fn new() -> Self

Creates an empty in-memory log.

§Examples
use raft_io::{MemoryLog, RaftLog};

let log = MemoryLog::new();
assert_eq!(log.last_index(), 0);
Source

pub fn len(&self) -> usize

Returns the number of entries currently stored.

§Examples
use raft_io::{LogEntry, MemoryLog, RaftLog};

let mut log = MemoryLog::new();
log.append(&[LogEntry::new(1, 1, vec![])]).unwrap();
assert_eq!(log.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns true if the log holds no entries.

§Examples
use raft_io::MemoryLog;

assert!(MemoryLog::new().is_empty());

Trait Implementations§

Source§

impl Clone for MemoryLog

Source§

fn clone(&self) -> MemoryLog

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MemoryLog

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for MemoryLog

Source§

fn default() -> MemoryLog

Returns the “default value” for a type. Read more
Source§

impl RaftLog for MemoryLog

Source§

fn last_index(&self) -> Index

Returns the index of the last entry, or 0 if the log is empty.
Source§

fn last_term(&self) -> Term

Returns the term of the last entry, or 0 if the log is empty.
Source§

fn term_at(&self, index: Index) -> Option<Term>

Returns the term of the entry at index. Read more
Source§

fn entry(&self, index: Index) -> Option<LogEntry>

Returns the entry at index, or None if there is none.
Source§

fn entries(&self, from: Index, to: Index) -> Vec<LogEntry>

Returns the entries in the inclusive index range [from, to]. Read more
Source§

fn append(&mut self, entries: &[LogEntry]) -> Result<()>

Appends entries to the end of the log. Read more
Source§

fn truncate(&mut self, from: Index) -> Result<()>

Removes every entry whose index is >= from. Read more
Source§

fn hard_state(&self) -> HardState

Returns the persisted HardState (current term and vote).
Source§

fn set_hard_state(&mut self, state: HardState) -> Result<()>

Persists state as the new HardState. Read more
Source§

fn sync(&mut self) -> Result<()>

Flushes all preceding writes to durable storage. Read more
Source§

fn snapshot_index(&self) -> Index

Returns the index the log has been compacted up to — the last index a snapshot includes — or 0 if there is no snapshot. Read more
Source§

fn snapshot(&self) -> Option<Snapshot>

Returns the current snapshot, if one exists. Read more
Source§

fn apply_snapshot(&mut self, snapshot: &Snapshot) -> Result<()>

Installs snapshot, replacing the prefix it subsumes. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<E> WithErrorCode<E> for E

Source§

fn with_code(self, code: impl Into<String>) -> CodedError<E>

Attach an error code to an error