concorde/
cfg.rs

1// Copyright 2020 Graydon Hoare <graydon@pobox.com>
2// Licensed under the MIT and Apache-2.0 licenses.
3
4use im::OrdSet as ArcOrdSet;
5/// `Cfg` is one of the two base lattices we work with (the other is the
6/// user-provided so-called `Obj` object-value lattice, not defined in this
7/// crate).
8///
9/// Cfg represents the state of the group of peers _doing_ the lattice
10/// agreement. Abstractly it's a 2P-SET that stores the set of peers who have
11/// been added and the set who have been removed; the set of "current members"
12/// is just the adds minus the removes.
13///
14/// This lattice is parameterized by a user-provided notion of a Peer. This is
15/// anything Ord+Clone but probably ought to be something small, like an integer
16/// or UUID or string. Something that identifies a peer, and something you don't
17/// mind transmitting sets of, serialized, in messages.
18use pergola::{ArcOrdSetWithUnion, DefTraits, LatticeElt, Tuple2};
19
20pub type PeerSetLD<Peer> = ArcOrdSetWithUnion<Peer>;
21pub type PeerSetLE<Peer> = LatticeElt<PeerSetLD<Peer>>;
22pub type CfgLD<Peer> = Tuple2<PeerSetLD<Peer>, PeerSetLD<Peer>>;
23pub type CfgLE<Peer> = LatticeElt<CfgLD<Peer>>;
24
25// Helper methods on the Cfg lattice elements.
26pub trait CfgLEExt<Peer: DefTraits> {
27    fn added_peers_elt(&self) -> &PeerSetLE<Peer>;
28    fn added_peers_elt_mut(&mut self) -> &mut PeerSetLE<Peer>;
29    fn added_peers(&self) -> &ArcOrdSet<Peer>;
30    fn added_peers_mut(&mut self) -> &mut ArcOrdSet<Peer>;
31
32    fn removed_peers_elt(&self) -> &PeerSetLE<Peer>;
33    fn removed_peers_elt_mut(&mut self) -> &mut PeerSetLE<Peer>;
34    fn removed_peers(&self) -> &ArcOrdSet<Peer>;
35    fn removed_peers_mut(&mut self) -> &mut ArcOrdSet<Peer>;
36
37    fn members(&self) -> ArcOrdSet<Peer>;
38}
39
40impl<Peer: DefTraits> CfgLEExt<Peer> for CfgLE<Peer> {
41    fn added_peers_elt(&self) -> &PeerSetLE<Peer> {
42        &self.value.0
43    }
44    fn added_peers_elt_mut(&mut self) -> &mut PeerSetLE<Peer> {
45        &mut self.value.0
46    }
47    fn added_peers(&self) -> &ArcOrdSet<Peer> {
48        &self.added_peers_elt().value
49    }
50    fn added_peers_mut(&mut self) -> &mut ArcOrdSet<Peer> {
51        &mut self.added_peers_elt_mut().value
52    }
53
54    fn removed_peers_elt(&self) -> &PeerSetLE<Peer> {
55        &self.value.1
56    }
57    fn removed_peers_elt_mut(&mut self) -> &mut PeerSetLE<Peer> {
58        &mut self.value.1
59    }
60    fn removed_peers(&self) -> &ArcOrdSet<Peer> {
61        &self.removed_peers_elt().value
62    }
63    fn removed_peers_mut(&mut self) -> &mut ArcOrdSet<Peer> {
64        &mut self.removed_peers_elt_mut().value
65    }
66
67    fn members(&self) -> ArcOrdSet<Peer> {
68        self.added_peers()
69            .clone()
70            .difference(self.removed_peers().clone())
71    }
72}