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

source

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);
source

pub fn from_iter<I>(prev_position: LogPosition, entries: I) -> Self
where 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) });
source

pub fn len(&self) -> usize

Returns the number of entries in this LogEntries instance.

source

pub fn is_empty(&self) -> bool

Returns true if the log entries is empty (i.e., the previous and last positions are the same).

source

pub fn prev_position(&self) -> LogPosition

Returns the position immediately before the first entry in this LogEntries instance.

source

pub fn last_position(&self) -> LogPosition

Returns the position of the last entry in this LogEntries instance.

source

pub fn iter(&self) -> impl '_ + Iterator<Item = LogEntry>

Returns an iterator over the entries in this LogEntries instance.

source

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);
source

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 range
source

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)));
source

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.

source

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);
source

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) });
source

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

source§

fn clone(&self) -> LogEntries

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for LogEntries

source§

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

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

impl Extend<LogEntry> for LogEntries

source§

fn extend<T: IntoIterator<Item = LogEntry>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl Hash for LogEntries

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for LogEntries

source§

fn eq(&self, other: &LogEntries) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for LogEntries

source§

impl StructuralPartialEq for LogEntries

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.