-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathnode.go
438 lines (414 loc) · 13.9 KB
/
node.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package radix
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/zond/god/murmur"
"strings"
"time"
)
const (
byteValue = 1 << iota
treeValue
)
const (
zombieLifetime = int64(time.Hour * 24)
)
type nodeIndexIterator func(key, byteValue []byte, treeValue *Tree, use int, timestamp int64, index int) (cont bool)
type nodeIterator func(key, byteValue []byte, treeValue *Tree, use int, timestamp int64) (cont bool)
// node is the generic implementation of a combined radix/merkle tree with size for each subtree (both regarding bytes and inner trees) cached.
// it also contains both byte slices and inner trees in each node.
// node.use == 0 && !node.empty => node is a tombstone
// node.use == 0 && node.empty => node is only structural (a branch in the keyspace)
// node.use & byteValue == byteValue => node contains a byte value
// node.use & treeValue == treeValue => node contains a tree value
// node.use != 0 && node.empty => node is invalid?
// node.empty && node.timestamp == 0 => node is invalid?
type node struct {
segment []Nibble // the bit of the key for this node that separates it from its parent
byteValue []byte
byteHash []byte // cached hash of the byteValue
treeValue *Tree
timestamp int64 // only used in regard to byteValues. treeValues ignore them (since they have their own timestamps inside them). a timestamp of 0 will be considered REALLY empty
hash []byte // cached hash of the entire node
children []*node
empty bool // this node only serves a structural purpose (ie remove it if it is no longer useful for that)
use int // the values in this node that are to be considered 'present'. even if this is a zero, do not remove the node if empty is false - it is still a tombstone.
treeSize int // size of the tree in this node and those of all of its children
byteSize int // number of byte values in this node and all of its children
realSize int // number of actual values, including tombstones
}
func newNode(segment []Nibble, byteValue []byte, treeValue *Tree, timestamp int64, empty bool, use int) *node {
return &node{
segment: segment,
byteValue: byteValue,
byteHash: murmur.HashBytes(byteValue),
treeValue: treeValue,
timestamp: timestamp,
hash: make([]byte, murmur.Size),
children: make([]*node, 1<<(8/parts)),
empty: empty,
use: use,
}
}
// setSegment copies the given part to be our segment.
func (self *node) setSegment(part []Nibble) {
new_segment := make([]Nibble, len(part))
copy(new_segment, part)
self.segment = new_segment
}
// rehash will recount the size of this node by summing the sizes of its own data and
// the data of its children.
//
// It will also rehash the hash of this node by recalculating the hash sum of its own
// data and the hashes of its children.
//
// Finally it will remove any children that are timed out tombstones.
func (self *node) rehash(key []Nibble, now int64) {
self.treeSize = 0
self.byteSize = 0
self.realSize = 0
self.realSize += self.treeValue.RealSize()
if self.timestamp != 0 {
self.realSize++
}
if self.use&treeValue != 0 {
self.treeSize = self.treeValue.Size()
}
if self.use&byteValue != 0 {
self.byteSize = 1
}
h := murmur.NewBytes(toBytes(key))
h.Write(self.byteHash)
h.Write(self.treeValue.Hash())
var child *node
for index := 0; index < len(self.children); index++ {
self.children[index] = self.children[index].gc(key, now)
child = self.children[index]
if child != nil {
self.treeSize += child.treeSize
self.byteSize += child.byteSize
self.realSize += child.realSize
h.Write(child.hash)
}
}
h.Extrude(self.hash)
}
// gc will garbage collect old tombstones.
// If this node is an old tombstone, a replacement child will be returned.
// If this node contains a tree which is empty and too old, it will be removed.
func (self *node) gc(prefix []Nibble, now int64) (result *node) {
if self == nil {
return self
}
if !self.empty && self.use&treeValue == treeValue && self.treeValue.Size() == 0 && self.treeValue.dataTimestamp < now-zombieLifetime {
self.treeValue, self.use = nil, self.use&^treeValue
}
if !self.empty && self.use == 0 && self.timestamp < now-zombieLifetime {
result, _, _, _, _ = self.del(prefix, self.segment, 0, now)
} else {
result = self
}
return
}
func (self *node) describe(indent int, buffer *bytes.Buffer) {
if self == nil {
return
}
indentation := &bytes.Buffer{}
for i := 0; i < indent; i++ {
fmt.Fprint(indentation, " ")
}
encodedSegment := stringEncode(toBytes(self.segment))
keyHeader := fmt.Sprintf("%v%#v (%v/%v/%v, %v, %v, %v, %v) => ", string(indentation.Bytes()), encodedSegment, self.byteSize, self.treeSize, self.realSize, self.empty, self.use, self.timestamp, hex.EncodeToString(self.hash))
if self.empty {
fmt.Fprintf(buffer, "%v\n", keyHeader)
} else {
fmt.Fprintf(buffer, "%v%v\n", keyHeader, strings.Trim(self.treeValue.describeIndented(0, len(keyHeader)), "\n"))
fmt.Fprintf(buffer, "%v%v\n", keyHeader, self.byteValue)
}
for _, child := range self.children {
child.describe(indent+len(encodedSegment), buffer)
}
}
// finger will return a finger print of this node to use when comparing trees in Sync.
func (self *node) finger(allocated *Print, segment []Nibble) (result *Print) {
result = allocated
if self == nil {
return
}
allocated.push(self)
beyond_self := false
beyond_segment := false
for i := 0; ; i++ {
beyond_self = i >= len(self.segment)
beyond_segment = i >= len(segment)
if beyond_self && beyond_segment {
allocated.set(self)
return
} else if beyond_segment {
return
} else if beyond_self {
return self.children[segment[i]].finger(allocated, segment[i:])
} else if segment[i] != self.segment[i] {
return
}
}
panic("Shouldn't happen")
}
// indexOf will return the index of the given segment, considering the data type defined by use (byteValue and/or treeValue).
// It will count from the start if up, else from the end.
func (self *node) indexOf(count int, segment []Nibble, use int, up bool) (index int, existed int) {
beyond_self := false
beyond_segment := false
for i := 0; ; i++ {
beyond_self = i >= len(self.segment)
beyond_segment = i >= len(segment)
if beyond_self && beyond_segment {
index, existed = count, self.use
return
} else if beyond_segment {
return
} else if beyond_self {
if !self.empty {
if use == 0 || use&byteValue&self.use != 0 {
count++
}
if use == 0 || use&treeValue&self.use != 0 {
count += self.treeValue.Size()
}
}
start, step, stop := 0, 1, len(self.children)
if !up {
start, step, stop = len(self.children)-1, -1, -1
}
var child *node
for j := start; j != stop; j += step {
child = self.children[j]
if child != nil {
if (up && j < int(segment[i])) || (!up && j > int(segment[i])) {
if use == 0 {
count += child.realSize
} else {
if use&byteValue != 0 {
count += child.byteSize
}
if use&treeValue != 0 {
count += child.treeSize
}
}
} else {
index, existed = child.indexOf(count, segment[i:], use, up)
return
}
}
}
index, existed = count, 0
return
} else if segment[i] != self.segment[i] {
if up {
if segment[i] < self.segment[i] {
index, existed = count, 0
} else {
index, existed = count+1, 0
}
} else {
if segment[i] > self.segment[i] {
index, existed = count, 0
} else {
for _, child := range self.children {
if child != nil {
if use == 0 {
count += child.realSize
} else {
if use&byteValue != 0 {
count += child.byteSize
}
if use&treeValue != 0 {
count += child.treeSize
}
}
}
}
index, existed = count, 0
}
}
return
}
}
panic("Shouldn't happen")
}
// get will return values for the given key, if it exists
func (self *node) get(segment []Nibble) (byteValue []byte, treeValue *Tree, timestamp int64, existed int) {
if self == nil {
return
}
beyond_self := false
beyond_segment := false
for i := 0; ; i++ {
beyond_self = i >= len(self.segment)
beyond_segment = i >= len(segment)
if beyond_self && beyond_segment {
byteValue, treeValue, timestamp, existed = self.byteValue, self.treeValue, self.timestamp, self.use
return
} else if beyond_segment {
return
} else if beyond_self {
byteValue, treeValue, timestamp, existed = self.children[segment[i]].get(segment[i:])
return
} else if segment[i] != self.segment[i] {
return
}
}
panic("Shouldn't happen")
}
// del will return this node or a child replacement after removing the value type defined by use (byteValue and/or treeValue).
func (self *node) del(prefix, segment []Nibble, use int, now int64) (result *node, oldBytes []byte, oldTree *Tree, timestamp int64, existed int) {
if self == nil {
return
}
beyond_segment := false
beyond_self := false
for i := 0; ; i++ {
beyond_segment = i >= len(segment)
beyond_self = i >= len(self.segment)
if beyond_segment && beyond_self {
if self.use&^use != 0 {
if self.use&use&byteValue != 0 {
oldBytes = self.byteValue
existed |= byteValue
self.byteValue, self.byteHash, self.use = nil, murmur.HashBytes(nil), self.use&^byteValue
}
if self.use&use&treeValue != 0 {
oldTree = self.treeValue
existed |= treeValue
self.treeValue, self.use = nil, self.use&^treeValue
}
result, timestamp = self, self.timestamp
self.rehash(append(prefix, segment...), now)
} else {
n_children := 0
var a_child *node
for _, child := range self.children {
if child != nil {
n_children++
a_child = child
}
}
if n_children > 1 || self.segment == nil {
result, oldBytes, oldTree, timestamp, existed = self, self.byteValue, self.treeValue, self.timestamp, self.use
self.byteValue, self.byteHash, self.treeValue, self.empty, self.use, self.timestamp = nil, murmur.HashBytes(nil), nil, true, 0, 0
self.rehash(append(prefix, segment...), now)
} else if n_children == 1 {
a_child.setSegment(append(self.segment, a_child.segment...))
result, oldBytes, oldTree, timestamp, existed = a_child, self.byteValue, self.treeValue, self.timestamp, self.use
} else {
result, oldBytes, oldTree, timestamp, existed = nil, self.byteValue, self.treeValue, self.timestamp, self.use
}
}
return
} else if beyond_segment {
result, oldBytes, oldTree, timestamp, existed = self, nil, nil, 0, 0
return
} else if beyond_self {
prefix = append(prefix, self.segment...)
self.children[segment[i]], oldBytes, oldTree, timestamp, existed = self.children[segment[i]].del(prefix, segment[i:], use, now)
if self.empty && prefix != nil {
n_children := 0
for _, child := range self.children {
if child != nil {
n_children++
}
}
if n_children == 0 {
result = nil
} else {
result = self
self.rehash(prefix, now)
}
} else {
result = self
self.rehash(prefix, now)
}
return
} else if self.segment[i] != segment[i] {
result, oldBytes, oldTree, timestamp, existed = self, nil, nil, 0, 0
return
}
}
panic("Shouldn't happen")
}
// fakeDel will replace the given key with a tombstone
func (self *node) fakeDel(prefix, segment []Nibble, use int, timestamp, now int64) (result *node, oldBytes []byte, oldTree *Tree, oldTimestamp int64, existed int) {
return self.insertHelp(prefix, newNode(segment, nil, nil, timestamp, false, 0), use, now)
}
// insert will insert the given node.
func (self *node) insert(prefix []Nibble, n *node, now int64) (result *node, oldBytes []byte, oldTree *Tree, timestamp int64, existed int) {
return self.insertHelp(prefix, n, n.use, now)
}
// insertHelp will insert the given node, allowing the caller to define what values of any current node to replace by providing the use parameter.
func (self *node) insertHelp(prefix []Nibble, n *node, use int, now int64) (result *node, oldBytes []byte, oldTree *Tree, timestamp int64, existed int) {
if self == nil {
n.rehash(append(prefix, n.segment...), now)
result = n
return
}
beyond_n := false
beyond_self := false
for i := 0; ; i++ {
beyond_n = i >= len(n.segment)
beyond_self = i >= len(self.segment)
if beyond_n && beyond_self {
result, oldBytes, oldTree, timestamp, existed = self, self.byteValue, self.treeValue, self.timestamp, self.use
if use&byteValue != 0 {
self.byteValue, self.byteHash = n.byteValue, n.byteHash
if n.use&byteValue == 0 {
self.use &^= byteValue
} else {
self.use |= byteValue
}
}
if use&treeValue != 0 {
self.treeValue = n.treeValue
if n.use&treeValue == 0 {
self.use &^= treeValue
} else {
self.use |= treeValue
}
}
self.empty, self.timestamp = n.empty, n.timestamp
self.rehash(append(prefix, self.segment...), now)
return
} else if beyond_n {
self.setSegment(self.segment[i:])
n.children[self.segment[0]] = self
result, oldBytes, oldTree, timestamp, existed = n, nil, nil, 0, 0
prefix = append(prefix, self.segment...)
self.rehash(prefix, now)
n.rehash(append(prefix, n.segment...), now)
return
} else if beyond_self {
n.setSegment(n.segment[i:])
// k is pre-calculated here because n.segment may change when n is inserted
k := n.segment[0]
prefix = append(prefix, self.segment...)
self.children[k], oldBytes, oldTree, timestamp, existed = self.children[k].insertHelp(prefix, n, use, now)
self.rehash(prefix, now)
result = self
return
} else if n.segment[i] != self.segment[i] {
result, oldBytes, oldTree, timestamp, existed = newNode(nil, nil, nil, 0, true, 0), nil, nil, 0, 0
result.setSegment(n.segment[:i])
n.setSegment(n.segment[i:])
result.children[n.segment[0]] = n
self.setSegment(self.segment[i:])
result.children[self.segment[0]] = self
prefix = append(prefix, result.segment...)
n.rehash(append(prefix, n.segment...), now)
self.rehash(append(prefix, self.segment...), now)
result.rehash(prefix, now)
return
}
}
panic("Shouldn't happen")
}