From b3c2ff7e37ce5996f572f43bc574cb50b2d0cdc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=82=8E=E6=B3=BC?= Date: Tue, 28 Feb 2023 15:11:51 +0800 Subject: [PATCH] Feature: add Membership methods: voter_ids(), learner_ids(), get_node() --- openraft/src/membership/membership.rs | 41 +++++++++++++-------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/openraft/src/membership/membership.rs b/openraft/src/membership/membership.rs index 942e74a94..e7fec4cb1 100644 --- a/openraft/src/membership/membership.rs +++ b/openraft/src/membership/membership.rs @@ -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 { + self.configs.as_joint().ids() + } + + /// Returns an Iterator of all learner node ids. Voters are not included. + pub fn learner_ids(&self) -> impl Iterator + '_ { + 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 { + 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 { @@ -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 { - 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 + '_ { - 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 { - self.nodes.iter() - } - /// Returns reference to the joint config. /// /// Membership is defined by a joint of multiple configs.