openraft/quorum/
coherent_impl.rs

1use crate::quorum::coherent::FindCoherent;
2use crate::quorum::Coherent;
3use crate::quorum::Joint;
4use crate::quorum::QuorumSet;
5
6impl<ID, QS> Coherent<ID, Joint<ID, QS, Vec<QS>>> for Joint<ID, QS, Vec<QS>>
7where
8    ID: PartialOrd + Ord + 'static,
9    QS: QuorumSet<ID> + PartialEq,
10{
11    /// Check if two `joint` are coherent.
12    ///
13    /// Read more about:
14    /// [Extended membership change](crate::docs::data::extended_membership)
15    fn is_coherent_with(&self, other: &Joint<ID, QS, Vec<QS>>) -> bool {
16        for a in self.children() {
17            for b in other.children() {
18                if a == b {
19                    return true;
20                }
21            }
22        }
23
24        false
25    }
26}
27
28impl<ID, QS> Coherent<ID, Joint<ID, QS, &[QS]>> for Joint<ID, QS, &[QS]>
29where
30    ID: PartialOrd + Ord + 'static,
31    QS: QuorumSet<ID> + PartialEq,
32{
33    fn is_coherent_with(&self, other: &Joint<ID, QS, &[QS]>) -> bool {
34        for a in self.children().iter() {
35            for b in other.children().iter() {
36                if a == b {
37                    return true;
38                }
39            }
40        }
41
42        false
43    }
44}
45
46impl<ID, QS> Coherent<ID, QS> for Joint<ID, QS, Vec<QS>>
47where
48    ID: PartialOrd + Ord + 'static,
49    QS: QuorumSet<ID> + PartialEq,
50{
51    fn is_coherent_with(&self, other: &QS) -> bool {
52        for a in self.children().iter() {
53            if a == other {
54                return true;
55            }
56        }
57
58        false
59    }
60}
61
62/// Impl to build a intermediate quorum set that is coherent with a joint and a uniform quorum set.
63impl<ID, QS> FindCoherent<ID, QS> for Joint<ID, QS, Vec<QS>>
64where
65    ID: PartialOrd + Ord + 'static,
66    QS: QuorumSet<ID> + PartialEq + Clone,
67{
68    fn find_coherent(&self, other: QS) -> Self {
69        if self.is_coherent_with(&other) {
70            Joint::from(vec![other])
71        } else {
72            let last = self.children().last();
73            if let Some(last) = last {
74                Joint::from(vec![last.clone(), other])
75            } else {
76                Joint::from(vec![other])
77            }
78        }
79    }
80}