Skip to content

Commit

Permalink
Feature: add Membership methods: voter_ids(), learner_ids(), get_node()
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer committed Feb 28, 2023
1 parent d1a97d3 commit b3c2ff7
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions openraft/src/membership/membership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,26 @@ where
N: Node,
NID: NodeId,
{
/// Returns an Iterator of all voter node ids. Learners are not included.
pub fn voter_ids(&self) -> impl Iterator<Item = NID> {
self.configs.as_joint().ids()
}

/// Returns an Iterator of all learner node ids. Voters are not included.
pub fn learner_ids(&self) -> impl Iterator<Item = NID> + '_ {
self.nodes.keys().filter(|x| !self.is_voter(x)).copied()
}

/// Returns an Iterator of all nodes(voters and learners).
pub fn nodes(&self) -> impl Iterator<Item = (&NID, &N)> {
self.nodes.iter()
}

/// Get a the node(either voter or learner) by node id.
pub fn get_node(&self, node_id: &NID) -> Option<&N> {
self.nodes.get(node_id)
}

/// Return if a node is a voter or learner, or not in this membership config at all.
#[allow(dead_code)]
pub(crate) fn get_node_role(&self, nid: &NID) -> Option<NodeRole> {
Expand All @@ -311,32 +331,11 @@ where
false
}

/// Returns an Iterator of all voter node ids. Learners are not included.
pub(crate) fn voter_ids(&self) -> impl Iterator<Item = NID> {
self.configs.as_joint().ids()
}

/// Returns an Iterator of all learner node ids. Voters are not included.
#[allow(dead_code)]
pub(crate) fn learner_ids(&self) -> impl Iterator<Item = NID> + '_ {
self.nodes.keys().filter(|x| !self.is_voter(x)).copied()
}

/// Returns if a voter or learner exists in this membership.
pub(crate) fn contains(&self, node_id: &NID) -> bool {
self.nodes.contains_key(node_id)
}

/// Get a the node(either voter or learner) by node id.
pub(crate) fn get_node(&self, node_id: &NID) -> Option<&N> {
self.nodes.get(node_id)
}

/// Returns an Iterator of all nodes(voters and learners).
pub fn nodes(&self) -> impl Iterator<Item = (&NID, &N)> {
self.nodes.iter()
}

/// Returns reference to the joint config.
///
/// Membership is defined by a joint of multiple configs.
Expand Down

0 comments on commit b3c2ff7

Please sign in to comment.