-
Notifications
You must be signed in to change notification settings - Fork 7
/
lww.go
54 lines (46 loc) · 1.14 KB
/
lww.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
package rdt
import (
"github.com/gritzko/ron"
)
// LWW is a last-write-wins replicated data type that may host a variety of user-land data types, like:
//
// * a dictionary,
// * a struct or
// * a simple 1D array (no splice, no index shifts),
// * a simple 2D array.
//
// This LWW employs client-side logical timestamps to decide which write wins, on a field-by-field basis.
// That is similar to e.g. Cassandra LWW.
//
type LWW struct {
}
var LWW_UUID = ron.NewName("lww")
var DELTA_UUID = ron.NewName("d")
func (lww LWW) Features() int {
return ron.ACID_FULL
}
func (lww LWW) Reduce(inputs ron.Batch) (res ron.Frame) {
heap := ron.MakeFrameHeap(ron.RefComparator, ron.EventComparatorDesc, len(inputs))
spec := inputs[0].Spec()
spec.SetEvent(inputs[len(inputs)-1].Event())
if inputs.HasFullState() {
spec.SetRef(ron.ZERO_UUID)
} else {
spec.SetRef(DELTA_UUID)
}
for k := 0; k < len(inputs); k++ {
heap.Put(&inputs[k])
}
res.AppendStateHeader(spec)
for !heap.EOF() {
res.AppendReduced(*heap.Current())
heap.NextPrim()
}
return
}
func MakeLWW() ron.Reducer {
return LWW{}
}
func init() {
ron.RDTYPES[LWW_UUID] = MakeLWW
}