-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathg_counter.go
62 lines (54 loc) · 1.6 KB
/
g_counter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package crdt
import "github.com/satori/go.uuid"
// GCounter represent a G-counter in CRDT, which is
// a state-based grow-only counter that only supports
// increments.
type GCounter struct {
// ident provides a unique identity to each replica.
ident string
// counter maps identity of each replica to their
// entry values i.e. the counter value they individually
// have.
counter map[string]int
}
// NewGCounter returns a *GCounter by pre-assigning a unique
// identity to it.
func NewGCounter() *GCounter {
return &GCounter{
ident: uuid.NewV4().String(),
counter: make(map[string]int),
}
}
// Inc increments the GCounter by the value of 1 everytime it
// is called.
func (g *GCounter) Inc() {
g.IncVal(1)
}
// IncVal allows passing in an arbitrary delta to increment the
// current value of counter by. Only positive values are accepted.
// If a negative value is provided the implementation will panic.
func (g *GCounter) IncVal(incr int) {
if incr < 0 {
panic("cannot decrement a gcounter")
}
g.counter[g.ident] += incr
}
// Count returns the total count of this counter across all the
// present replicas.
func (g *GCounter) Count() (total int) {
for _, val := range g.counter {
total += val
}
return
}
// Merge combines the counter values across multiple replicas.
// The property of idempotency is preserved here across
// multiple merges as when no state is changed across any replicas,
// the result should be exactly the same everytime.
func (g *GCounter) Merge(c *GCounter) {
for ident, val := range c.counter {
if v, ok := g.counter[ident]; !ok || v < val {
g.counter[ident] = val
}
}
}