-
Notifications
You must be signed in to change notification settings - Fork 8
/
tree.go
225 lines (196 loc) · 4.63 KB
/
tree.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package service
import (
"strconv"
"strings"
)
const (
crdtRootKey = ""
crdtOrphanKey = "_orphan"
)
type crdtTree struct {
cache map[string]*node
addEventSet, deleteEventSet, positionEventSet map[string]EventLog
}
type node struct {
key string
originUUID uuid
parent, left, right *node
children childDll
latestLamportTimestamp int64
}
func (n *node) moreRecentThan(o *node) bool {
if n.key == "_orphan" {
return false
} else if o.key == "_orphan" {
return false
}
if n.latestLamportTimestamp > o.latestLamportTimestamp {
return true
} else if n.latestLamportTimestamp < o.latestLamportTimestamp {
return false
}
return n.originUUID > o.originUUID
}
// Seek out the next node in this precedence:
// - immediate child
// - right ptr
// - right ptr of next parent which is not root
func (n *node) getNext() *node {
if n.children.firstChild != nil {
return n.children.firstChild
}
if n.right != nil {
return n.right
}
cur := n.parent
for cur != nil {
if cur.right != nil {
return cur.right
}
cur = cur.parent
}
return nil
}
// Consider using https://pkg.go.dev/container/list
type childDll struct {
firstChild *node
}
func (l *childDll) insertInPlace(n *node) {
right := l.firstChild
var left *node
for right != nil {
if n.moreRecentThan(right) {
break
}
left = right
right = right.right
}
// insert into place
if left != nil {
left.right = n
} else {
l.firstChild = n
}
// If we traversed to the end
if right != nil {
right.left = n
}
n.left = left
n.right = right
}
func (l *childDll) removeChild(n *node) {
if n.left != nil {
n.left.right = n.right
} else {
// node is firstChild, so reset firstChild in dll
l.firstChild = n.right
}
if r := n.right; r != nil {
r.left = n.left
}
n.left = nil
n.right = nil
return
}
func newTree() *crdtTree {
orphan := &node{
key: crdtOrphanKey,
}
root := &node{
key: crdtRootKey,
children: childDll{orphan},
}
orphan.parent = root
return &crdtTree{
cache: map[string]*node{
crdtOrphanKey: orphan,
crdtRootKey: root,
},
addEventSet: make(map[string]EventLog),
deleteEventSet: make(map[string]EventLog),
positionEventSet: make(map[string]EventLog),
}
}
func (crdt *crdtTree) itemIsLive(key string) bool {
latestAddEvent, isAdded := crdt.addEventSet[key]
latestDeleteEvent, isDeleted := crdt.deleteEventSet[key]
if isAdded && (!isDeleted || (isAdded && latestDeleteEvent.before(latestAddEvent))) {
return true
}
return false
}
func (crdt *crdtTree) traverse(n *node) *node {
if n == nil {
n = crdt.cache[crdtRootKey]
}
n = n.getNext()
for n != nil {
if n.key != crdtRootKey && n.key != crdtOrphanKey && n.parent.key != crdtOrphanKey && crdt.itemIsLive(n.key) {
return n
}
n = n.getNext()
}
return nil
}
func (crdt *crdtTree) generateEvents() []EventLog {
events := []EventLog{}
// Add all PositionEvents
for _, e := range crdt.positionEventSet {
events = append(events, e)
}
// Add UpdateEvents for active items (deleted items don't need content state, as any update that overrides deleted state will
// come with it's own
for k, addEvent := range crdt.addEventSet {
if crdt.itemIsLive(k) {
events = append(events, addEvent)
}
}
// Add DeleteEvents
for _, e := range crdt.deleteEventSet {
events = append(events, e)
}
return events
}
func (crdt *crdtTree) addToTargetChildArray(item, target *node) {
item.parent = target
target.children.insertInPlace(item)
}
func (crdt *crdtTree) add(e EventLog) {
target, exists := crdt.cache[e.TargetListItemKey]
if !exists {
// TODO centralise
// TODO more efficient storage/inferrence of target item uuid/lamport
r := strings.Split(e.TargetListItemKey, ":")
var ts int64
var originUUID uuid
if len(r) > 0 {
i, _ := strconv.ParseInt(r[0], 10, 64)
originUUID = uuid(i)
if len(r) > 1 {
ts, _ = strconv.ParseInt(r[1], 10, 64)
}
}
target = &node{
key: e.TargetListItemKey,
originUUID: originUUID,
latestLamportTimestamp: ts,
}
crdt.cache[e.TargetListItemKey] = target
orphanParent := crdt.cache[crdtOrphanKey]
crdt.addToTargetChildArray(target, orphanParent)
}
item, exists := crdt.cache[e.ListItemKey]
if !exists {
item = &node{
key: e.ListItemKey,
originUUID: e.UUID,
latestLamportTimestamp: e.LamportTimestamp,
}
crdt.cache[e.ListItemKey] = item
} else {
// remove from parent.children if pre-existing
item.parent.children.removeChild(item)
}
item.latestLamportTimestamp = e.LamportTimestamp
crdt.addToTargetChildArray(item, target)
}