Skip to content

Commit

Permalink
Small refinements, use inlines.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoverbear committed Sep 11, 2018
1 parent 3364324 commit a68d4cb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,35 @@ impl ProgressSet {
),
configuration: Configuration {
voters: HashSet::with_capacity_and_hasher(
voters + learners,
voters,
FxBuildHasher::default(),
),
learners: HashSet::with_capacity_and_hasher(
voters + learners,
learners,
FxBuildHasher::default(),
),
},
}
}

/// Returns the status of voters.
#[inline]
pub fn voters(&self) -> impl Iterator<Item = (&u64, &Progress)> {
self.progress
.iter()
.filter(move |(k, _)| self.configuration.voters.contains(k))
}

/// Returns the status of learners.
#[inline]
pub fn learners(&self) -> impl Iterator<Item = (&u64, &Progress)> {
self.progress
.iter()
.filter(move |(k, _)| self.configuration.learners.contains(k))
}

/// Returns the mutable status of voters.
#[inline]
pub fn voters_mut(&mut self) -> impl Iterator<Item = (&u64, &mut Progress)> {
let configuration = &self.configuration;
self.progress
Expand All @@ -113,6 +116,7 @@ impl ProgressSet {
}

/// Returns the mutable status of learners.
#[inline]
pub fn learners_mut(&mut self) -> impl Iterator<Item = (&u64, &mut Progress)> {
let configuration = &self.configuration;
self.progress
Expand All @@ -121,61 +125,73 @@ impl ProgressSet {
}

/// Returns if the progress set contains a voter by the given id.
#[inline]
pub fn has_voter(&self, id: u64) -> bool {
self.configuration.voters.contains(&id)
}

/// Returns the number of voters in the current configuration.
#[inline]
pub fn num_voters(&self) -> usize {
self.configuration.voters.len()
}

/// Returns the number of learners in the current configuration.
#[inline]
pub fn num_learners(&self) -> usize {
self.configuration.learners.len()
}

/// Returns if the progress set contains a learner by the given id.
#[inline]
pub fn has_learner(&self, id: u64) -> bool {
self.configuration.learners.contains(&id)
}

/// Returns if the progress set contains a peer by the given id.
#[inline]
pub fn has_peer(&self, id: u64) -> bool {
self.progress.contains_key(&id)
}

/// Returns the ids of all known voters.
#[inline]
pub fn voter_ids(&self) -> impl Iterator<Item = &u64> {
self.configuration.voters.iter()
}

/// Returns the ids of all known learners.
#[inline]
pub fn learner_ids(&self) -> impl Iterator<Item = &u64> {
self.configuration.learners.iter()
}

/// Grabs a reference to the progress of a node.
#[inline]
pub fn get(&self, id: u64) -> Option<&Progress> {
self.progress.get(&id)
}

/// Grabs a mutable reference to the progress of a node.
#[inline]
pub fn get_mut(&mut self, id: u64) -> Option<&mut Progress> {
self.progress.get_mut(&id)
}

/// Returns an iterator across all the nodes and their progress.
#[inline]
pub fn iter(&self) -> impl ExactSizeIterator<Item = (&u64, &Progress)> {
self.progress.iter()
}

/// Returns a mutable iterator across all the nodes and their progress.
#[inline]
pub fn iter_mut(&mut self) -> impl ExactSizeIterator<Item = (&u64, &mut Progress)> {
self.progress.iter_mut()
}

/// Adds a voter node
#[inline]
pub fn insert_voter(&mut self, id: u64, mut pr: Progress) -> Result<(), Error> {
if self.has_learner(id) {
Err(Error::Exists(id, "learners"))?;
Expand Down
2 changes: 1 addition & 1 deletion src/raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ impl<T: Storage> Raft<T> {
pub fn maybe_commit(&mut self) -> bool {
let mut mis_arr = [0; 5];
let mut mis_vec;
let voters = self.prs().voters().count();
let voters = self.prs().num_voters();
let mis = if voters <= 5 {
&mut mis_arr[..voters]
} else {
Expand Down

0 comments on commit a68d4cb

Please sign in to comment.