Skip to main content

quorum_set/quorum/
coherent_impl.rs

1use std::collections::BTreeSet;
2
3use crate::quorum::Coherent;
4use crate::quorum::coherent::FindCoherent;
5
6/// Two joint configs are coherent iff they share at least one config: then every quorum of one
7/// intersects every quorum of the other.
8impl<NID> Coherent<NID, Vec<BTreeSet<NID>>> for Vec<BTreeSet<NID>>
9where NID: PartialOrd + Ord + Clone + 'static
10{
11    /// Return `true` when two joint quorum sets share a config.
12    ///
13    /// Read more about extended membership change in OpenRaft:
14    /// <https://docs.rs/openraft/latest/openraft/docs/data/extended_membership/index.html>
15    fn is_coherent_with(&self, other: &Vec<BTreeSet<NID>>) -> bool {
16        for a in self {
17            for b in other {
18                if a == b {
19                    return true;
20                }
21            }
22        }
23        false
24    }
25}
26
27/// Builds an intermediate joint quorum set for a target flat config.
28impl<NID> FindCoherent<NID, BTreeSet<NID>> for Vec<BTreeSet<NID>>
29where NID: PartialOrd + Ord + Clone + 'static
30{
31    fn find_coherent(&self, other: BTreeSet<NID>) -> Self {
32        if self.is_coherent_with(&vec![other.clone()]) {
33            vec![other]
34        } else if let Some(last) = self.last() {
35            vec![last.clone(), other]
36        } else {
37            vec![other]
38        }
39    }
40}