aboutsummaryrefslogtreecommitdiff
path: root/chapter/6/resources/code
diff options
context:
space:
mode:
Diffstat (limited to 'chapter/6/resources/code')
-rw-r--r--chapter/6/resources/code/counters/python/operation-based-increment-and-decrement-counter.py20
-rw-r--r--chapter/6/resources/code/counters/python/operation-based-increment-only-counter.py15
-rw-r--r--chapter/6/resources/code/counters/python/state-based-increment-and-decrement-counter-correct.py47
-rw-r--r--chapter/6/resources/code/counters/python/state-based-increment-and-decrement-counter-incorrect.py31
-rw-r--r--chapter/6/resources/code/counters/python/state-based-increment-only-counter-correct.py28
-rw-r--r--chapter/6/resources/code/counters/python/state-based-increment-only-counter-incorrect.py19
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()))