def test_gset(): """ {},{} / \ A{eric},{} B{glenn},{} +eric +glenn | | A{eric mark},{} | +mark | / \ | / \ \ / \ \ / B2{glenn tom} +tom \ / \ AB{eric mark glenn} \ <<merge>> \ / \ / ABB2{eric mark tom glenn} <<merge>> """ A = GSet() B = GSet() A.add("eric") A.add("mark") B.add("glenn") B2 = B.clone() AB = GSet.merge(A, B) B2.add("tom") ABB2 = GSet.merge(AB, B2) assert ABB2.value == {"eric", "mark", "tom", "glenn"}, ABB2.value
class TwoPSet(SetStateCRDT): def __init__(self): self.A = GSet() self.R = GSet() @classmethod def merge(cls, X, Y): merged_A = GSet.merge(X.A, Y.A) merged_R = GSet.merge(X.R, Y.R) merged_payload = { "A": merged_A, "R": merged_R, } return TwoPSet.from_payload(merged_payload) def compare(self, other): """ (S.A ⊆ T.A ∨ S.R ⊆ T.R) """ A_compare = self.A.compare(other.A) R_compare = self.R.compare(other.R) return A_compare or R_compare @property def value(self): return self.A.value - self.R.value def get_payload(self): return { "A": self.A.payload, "R": self.R.payload, } def set_payload(self, payload): self.A = GSet.from_payload(payload['A']) self.R = GSet.from_payload(payload['R']) payload = property(get_payload, set_payload) def __contains__(self, element): return element in self.A and element not in self.R def __iter__(self): return self.value.__iter__(element) def __len__(self): return self.value.__len__(element) def add(self, element): self.A.add(element) def discard(self, element): if element in self: self.R.add(element)
def __init__(self): self.A = GSet() self.R = GSet()
def merge(cls, X, Y): merged = GSet() merged._payload = X._payload.union(Y._payload) return merged