diff options
| author | Heather Miller <heather.miller@epfl.ch> | 2017-01-02 16:10:10 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-01-02 16:10:10 +0100 |
| commit | 8428c40f79060169e85bdf740a9432b933a47160 (patch) | |
| tree | 9950fdb91b93a3c37c4de8e03d4e853493b59deb /chapter/6/resources/code/counters | |
| parent | 67118db13c3acb378da9a853d8119e82e20d306a (diff) | |
| parent | f27ba2bd06be195a06fe11b571aebd9a7c1ef930 (diff) | |
Merge pull request #17 from aviralg/master
Added images for counter example, need to work on text
Diffstat (limited to 'chapter/6/resources/code/counters')
6 files changed, 160 insertions, 0 deletions
diff --git a/chapter/6/resources/code/counters/python/operation-based-increment-and-decrement-counter.py b/chapter/6/resources/code/counters/python/operation-based-increment-and-decrement-counter.py new file mode 100644 index 0000000..5182ab1 --- /dev/null +++ b/chapter/6/resources/code/counters/python/operation-based-increment-and-decrement-counter.py @@ -0,0 +1,20 @@ +class CmRDT: + pass + +class Counter(CmRDT): + + def __init__(self): # constructor function + self._count = 0 + + def value(self): # query function + return self._count + + def increment(self): # update function + self._count += 1 + for replica in self.replicas(): + self.transmit("increment", replica) + + def decrement(self): # update function + self._count -= 1 + for replica in self.replicas(): + self.transmit("decrement", replica) diff --git a/chapter/6/resources/code/counters/python/operation-based-increment-only-counter.py b/chapter/6/resources/code/counters/python/operation-based-increment-only-counter.py new file mode 100644 index 0000000..b10cd98 --- /dev/null +++ b/chapter/6/resources/code/counters/python/operation-based-increment-only-counter.py @@ -0,0 +1,15 @@ +class CmRDT: + pass + +class Counter(CmRDT): + + def __init__(self): # constructor function + self._count = 0 + + def value(self): # query function + return self._count + + def increment(self): # update function + self._count += 1 + for replica in self.replicas(): + self.transmit("increment", replica) diff --git a/chapter/6/resources/code/counters/python/state-based-increment-and-decrement-counter-correct.py b/chapter/6/resources/code/counters/python/state-based-increment-and-decrement-counter-correct.py new file mode 100644 index 0000000..1ea726d --- /dev/null +++ b/chapter/6/resources/code/counters/python/state-based-increment-and-decrement-counter-correct.py @@ -0,0 +1,47 @@ +class CvRDT: + pass + +class Counter(CvRDT): + + def __init__(self, + increments = None, + decrements = None): # constructor function + if increments is None: + self._increments = [0] * length(replicas()) + else: + self._increments = increments + if decrements is None: + self._decrements = [0] * length(replicas()) + else: + self._decrements = decrements + + def increments(self): # query function + return list(self._increments) # return a clone + + def decrements(self): # query function + return list(self._decrements) # return a clone + + def value(self): # query function + return (sum(self.increments()) - + sum(self.decrements())) + + def increment(self): # update function + self._increments[self.replicaId()] += 1 + + def decrement(self): # update function + self._decrements[self.replicaId()] += 1 + + def compare(self, other): # comparison function + return (all(v1 <= v2 for (v1, v2) in + zip(self.increments(), + other.increments())) + and + all(v1 <= v2 for (v1, v2) in + zip(self.decrements(), + other.decrements()))) + + def merge(self, other): # merge function + return Counter(increments = map(max, zip(self.increments(), + other.increments())), + decrements = map(max, zip(self.decrements(), + other.decrements()))) diff --git a/chapter/6/resources/code/counters/python/state-based-increment-and-decrement-counter-incorrect.py b/chapter/6/resources/code/counters/python/state-based-increment-and-decrement-counter-incorrect.py new file mode 100644 index 0000000..6f0cbde --- /dev/null +++ b/chapter/6/resources/code/counters/python/state-based-increment-and-decrement-counter-incorrect.py @@ -0,0 +1,31 @@ +class CvRDT: + pass + +class Counter(CvRDT): + + def __init__(self, counts = None): # constructor function + if counts is None: + self._counts = [0] * length(self.replicas()) + else: + self._counts = counts + + def value(self): # query function + return sum(self._counts) + + def counts(self): # query function + return list(self._counts) # return a clone + + def increment(self): # update function + self._counts[self.replicaId()] += 1 + + def decrement(self): # update function + self._counts[self.replicaId()] -= 1 + + def compare(self, other): # comparison function + return all(v1 <= v2 for (v1, v2) in + zip(self.counts(), + other.counts())) + + def merge(self, other): # merge function + return Counter(map(max, zip(self.counts(), + other.counts()))) diff --git a/chapter/6/resources/code/counters/python/state-based-increment-only-counter-correct.py b/chapter/6/resources/code/counters/python/state-based-increment-only-counter-correct.py new file mode 100644 index 0000000..a3d9069 --- /dev/null +++ b/chapter/6/resources/code/counters/python/state-based-increment-only-counter-correct.py @@ -0,0 +1,28 @@ +class CvRDT: + pass + +class Counter(CvRDT): + + def __init__(self, counts = None): # constructor function + if counts is None: + self._counts = [0] * length(self.replicas()) + else: + self._counts = counts + + def value(self): # query function + return sum(self._counts) + + def counts(self): # query function + return list(self._counts) # return a clone + + def increment(self): # update function + self._counts[self.replicaId()] += 1 + + def compare(self, other): # comparison function + return all(v1 <= v2 for (v1, v2) in + zip(self.counts(), + other.counts())) + + def merge(self, other): # merge function + return Counter(map(max, zip(self.counts(), + other.counts()))) diff --git a/chapter/6/resources/code/counters/python/state-based-increment-only-counter-incorrect.py b/chapter/6/resources/code/counters/python/state-based-increment-only-counter-incorrect.py new file mode 100644 index 0000000..9971c65 --- /dev/null +++ b/chapter/6/resources/code/counters/python/state-based-increment-only-counter-incorrect.py @@ -0,0 +1,19 @@ +class CvRDT: + pass + +class Counter(CvRDT): + + def __init__(self, count = 0): # constructor function + self._count = count + + def value(self): # query function + return self._count + + def increment(self): # update function + self._count += 1 + + def compare(self, other): # comparison function + return self.value() <= other.value() + + def merge(self, other): # merge function + return Counter(max(self.value(), other.value())) |
