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
impl MemoryLog
Sourcepub fn new() -> Self
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);Trait Implementations§
Source§impl RaftLog for MemoryLog
impl RaftLog for MemoryLog
Source§fn last_index(&self) -> Index
fn last_index(&self) -> Index
Returns the index of the last entry, or
0 if the log is empty.Source§fn term_at(&self, index: Index) -> Option<Term>
fn term_at(&self, index: Index) -> Option<Term>
Returns the term of the entry at
index. Read moreSource§fn entry(&self, index: Index) -> Option<LogEntry>
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>
fn entries(&self, from: Index, to: Index) -> Vec<LogEntry>
Returns the entries in the inclusive index range
[from, to]. Read moreSource§fn append(&mut self, entries: &[LogEntry]) -> Result<()>
fn append(&mut self, entries: &[LogEntry]) -> Result<()>
Appends
entries to the end of the log. Read moreSource§fn truncate(&mut self, from: Index) -> Result<()>
fn truncate(&mut self, from: Index) -> Result<()>
Removes every entry whose index is
>= from. Read moreSource§fn hard_state(&self) -> HardState
fn hard_state(&self) -> HardState
Returns the persisted
HardState (current term and vote).Source§fn snapshot_index(&self) -> Index
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 moreAuto Trait Implementations§
impl Freeze for MemoryLog
impl RefUnwindSafe for MemoryLog
impl Send for MemoryLog
impl Sync for MemoryLog
impl Unpin for MemoryLog
impl UnsafeUnpin for MemoryLog
impl UnwindSafe for MemoryLog
Blanket Implementations§
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
Mutably borrows from an owned value. Read more