Struct raftbare::LogEntries
source · pub struct LogEntries { /* private fields */ }Expand description
Log entries.
This representation is compact and only requires O(|terms|) + O(|configs|) memory,
where |terms| is the number of LogEntry::Term entries and
|configs| is the number of LogEntry::ClusterConfig entries.
Implementations§
source§impl LogEntries
impl LogEntries
sourcepub const fn new(prev_position: LogPosition) -> Self
pub const fn new(prev_position: LogPosition) -> Self
Makes a new empty LogEntries instance at the given position.
§Examples
use raftbare::{LogEntries, LogEntry, LogPosition};
let entries = LogEntries::new(LogPosition::ZERO);
assert!(entries.is_empty());
assert_eq!(entries.len(), 0);
assert_eq!(entries.iter().count(), 0);
assert_eq!(entries.prev_position(), LogPosition::ZERO);
assert_eq!(entries.last_position(), LogPosition::ZERO);sourcepub fn from_iter<I>(prev_position: LogPosition, entries: I) -> Selfwhere
I: IntoIterator<Item = LogEntry>,
pub fn from_iter<I>(prev_position: LogPosition, entries: I) -> Selfwhere
I: IntoIterator<Item = LogEntry>,
Makes a new LogEntries instance with the given entries.
§Examples
use raftbare::{LogEntries, LogEntry, LogIndex, LogPosition, Term};
let entries = LogEntries::from_iter(
LogPosition::ZERO,
vec![
LogEntry::Term(Term::ZERO),
LogEntry::Command,
LogEntry::Command,
],
);
assert!(!entries.is_empty());
assert_eq!(entries.len(), 3);
assert_eq!(entries.iter().count(), 3);
assert_eq!(entries.prev_position(), LogPosition::ZERO);
assert_eq!(entries.last_position(), LogPosition { term: Term::ZERO, index: LogIndex::new(3) });sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of entries in this LogEntries instance.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the log entries is empty (i.e., the previous and last positions are the same).
sourcepub fn prev_position(&self) -> LogPosition
pub fn prev_position(&self) -> LogPosition
Returns the position immediately before the first entry in this LogEntries instance.
sourcepub fn last_position(&self) -> LogPosition
pub fn last_position(&self) -> LogPosition
Returns the position of the last entry in this LogEntries instance.
sourcepub fn iter(&self) -> impl '_ + Iterator<Item = LogEntry>
pub fn iter(&self) -> impl '_ + Iterator<Item = LogEntry>
Returns an iterator over the entries in this LogEntries instance.
sourcepub fn iter_with_positions(
&self,
) -> impl '_ + Iterator<Item = (LogPosition, LogEntry)>
pub fn iter_with_positions( &self, ) -> impl '_ + Iterator<Item = (LogPosition, LogEntry)>
Returns an iterator over the entries in this LogEntries instance with their positions.
§Examples
use raftbare::{LogEntries, LogEntry, LogIndex, LogPosition, Term};
let mut entries = LogEntries::new(LogPosition::ZERO);
entries.push(LogEntry::Command);
entries.push(LogEntry::Term(Term::new(1)));
entries.push(LogEntry::Command);
fn pos(term: u64, index: u64) -> LogPosition {
LogPosition { term: Term::new(term), index: LogIndex::new(index) }
}
let mut iter = entries.iter_with_positions();
assert_eq!(iter.next(), Some((pos(0, 1), LogEntry::Command)));
assert_eq!(iter.next(), Some((pos(1, 2), LogEntry::Term(Term::new(1)))));
assert_eq!(iter.next(), Some((pos(1, 3), LogEntry::Command)));
assert_eq!(iter.next(), None);sourcepub fn contains(&self, position: LogPosition) -> bool
pub fn contains(&self, position: LogPosition) -> bool
Returns true if the given position is within the range of this entries.
§Examples
use raftbare::{LogEntries, LogEntry, LogIndex, LogPosition, Term};
fn pos(term: u64, index: u64) -> LogPosition {
LogPosition { term: Term::new(term), index: LogIndex::new(index) }
}
let entries = LogEntries::from_iter(
LogPosition::ZERO,
vec![
LogEntry::Term(Term::ZERO),
LogEntry::Command,
LogEntry::Term(Term::new(1)),
LogEntry::Command,
],
);
assert!(entries.contains(pos(0, 0))); // Including the previous position
assert!(entries.contains(pos(1, 4))); // Including the last position
assert!(!entries.contains(pos(0, 4))); // Index is within the range but term is different
assert!(!entries.contains(pos(1, 5))); // Index is out of rangesourcepub fn contains_index(&self, index: LogIndex) -> bool
pub fn contains_index(&self, index: LogIndex) -> bool
Returns true if the given index is within the range of this entries.
Unlike LogEntries::contains(), this method does not check the term of the given index.
§Examples
use raftbare::{LogEntries, LogEntry, LogIndex, LogPosition, Term};
let entries = LogEntries::from_iter(
LogPosition::ZERO,
vec![
LogEntry::Term(Term::ZERO),
LogEntry::Command,
LogEntry::Term(Term::new(1)),
LogEntry::Command,
],
);
assert!(entries.contains_index(LogIndex::ZERO)); // Including the previous index
assert!(entries.contains_index(LogIndex::new(1)));
assert!(entries.contains_index(LogIndex::new(4))); // Including the last index
assert!(!entries.contains_index(LogIndex::new(5)));sourcepub fn get_term(&self, index: LogIndex) -> Option<Term>
pub fn get_term(&self, index: LogIndex) -> Option<Term>
Returns the term of the given index if it is within the range of this entries.
sourcepub fn get_entry(&self, index: LogIndex) -> Option<LogEntry>
pub fn get_entry(&self, index: LogIndex) -> Option<LogEntry>
Returns the entry at the given index if it is within the range of this entries.
Note that if the index is equal to the previous index, this method returns None.
§Examples
use raftbare::{LogEntries, LogEntry, LogIndex, LogPosition, Term};
let entries = LogEntries::from_iter(
LogPosition::ZERO,
vec![
LogEntry::Term(Term::ZERO),
LogEntry::Command,
LogEntry::Term(Term::new(1)),
],
);
assert_eq!(entries.get_entry(LogIndex::ZERO), None);
assert_eq!(entries.get_entry(LogIndex::new(1)), Some(LogEntry::Term(Term::ZERO)));
assert_eq!(entries.get_entry(LogIndex::new(2)), Some(LogEntry::Command));
assert_eq!(entries.get_entry(LogIndex::new(3)), Some(LogEntry::Term(Term::new(1))));
assert_eq!(entries.get_entry(LogIndex::new(4)), None);sourcepub fn push(&mut self, entry: LogEntry)
pub fn push(&mut self, entry: LogEntry)
Appends an entry to the back of this entries.
§Examples
use raftbare::{LogEntries, LogEntry, LogIndex, LogPosition, Term};
let mut entries = LogEntries::new(LogPosition::ZERO);
entries.push(LogEntry::Term(Term::ZERO));
entries.push(LogEntry::Command);
assert_eq!(entries.get_entry(LogIndex::new(1)), Some(LogEntry::Term(Term::ZERO)));
assert_eq!(entries.last_position(), LogPosition { term: Term::ZERO, index: LogIndex::new(2) });sourcepub fn truncate(&mut self, len: usize)
pub fn truncate(&mut self, len: usize)
Shortens the entries, keeping the first len entries and dropping the rest.
If len is greater or equal to LogEntries::len(), this has no effect.
§Examples
use raftbare::{LogEntries, LogEntry, LogIndex, LogPosition, Term};
let mut entries = LogEntries::new(LogPosition::ZERO);
entries.push(LogEntry::Term(Term::ZERO));
entries.push(LogEntry::Command);
entries.push(LogEntry::Term(Term::new(1)));
assert_eq!(entries.len(), 3);
// No effect.
entries.truncate(3);
assert_eq!(entries.len(), 3);
// Drop the last two entries.
entries.truncate(1);
assert_eq!(entries.len(), 1);
assert_eq!(entries.get_entry(LogIndex::new(1)), Some(LogEntry::Term(Term::ZERO)));
assert_eq!(entries.get_entry(LogIndex::new(2)), None);
// Drop all entries.
entries.truncate(0);
assert_eq!(entries.len(), 0);
assert_eq!(entries.get_entry(LogIndex::new(1)), None);Trait Implementations§
source§impl Clone for LogEntries
impl Clone for LogEntries
source§fn clone(&self) -> LogEntries
fn clone(&self) -> LogEntries
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl Debug for LogEntries
impl Debug for LogEntries
source§impl Extend<LogEntry> for LogEntries
impl Extend<LogEntry> for LogEntries
source§fn extend<T: IntoIterator<Item = LogEntry>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = LogEntry>>(&mut self, iter: T)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)source§impl Hash for LogEntries
impl Hash for LogEntries
source§impl PartialEq for LogEntries
impl PartialEq for LogEntries
impl Eq for LogEntries
impl StructuralPartialEq for LogEntries
Auto Trait Implementations§
impl Freeze for LogEntries
impl RefUnwindSafe for LogEntries
impl Send for LogEntries
impl Sync for LogEntries
impl Unpin for LogEntries
impl UnwindSafe for LogEntries
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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)