-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
630 lines (574 loc) · 14 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
package art
import (
"bytes"
"sort"
"unsafe"
)
const (
// Inner nodes of type Node4 must have between 2 and 4 children.
node4Min = 2
node4Max = 4
// Inner nodes of type Node16 must have between 5 and 16 children.
node16Min = 5
node16Max = 16
// Inner nodes of type Node48 must have between 17 and 48 children.
node48Min = 17
node48Max = 48
// Inner nodes of type Node256 must have between 49 and 256 children.
node256Min = 49
node256Max = 256
maxPrefixLen = 10
)
// nullNode represent for nil value, so as not to make redundant allocations.
var nullNode *artNode = nil
// node includes metadata of art tree node.
type node struct {
size int
prefixLen int
prefix [maxPrefixLen]byte
}
// node4 is of type Node4
type node4 struct {
node
keys [node4Max]byte
children [node4Max]*artNode
}
// node16 is of type Node16
type node16 struct {
node
keys [node16Max]byte
children [node16Max]*artNode
}
// node48 is of type Node48
type node48 struct {
node
keys [node256Max]byte // keys[$(prefix_char)] = $(idx in children)
children [node48Max + 1]*artNode // Do not use children[0] as 0 is the default value of keys[$(prefix_char)]
}
// node256 is of type Node256
type node256 struct {
node
children [node256Max]*artNode // children[$(char)] = $(child pointer)
}
// leafNode contains the real key value data.
type leafNode struct {
key Key
value interface{}
}
// artNode is an embedded node type used for art.
type artNode struct {
nodeType NodeType
nodePtr unsafe.Pointer
}
// // newLeafNode creates an embedded artNode of leafNode
func newLeafNode(key []byte, value interface{}) *artNode {
newKey := make([]byte, len(key))
copy(newKey, key)
return &artNode{
nodeType: LeafNode,
nodePtr: unsafe.Pointer(&leafNode{key: newKey, value: value}),
}
}
// newNode4 creates an embedded artNode of node4
func newNode4() *artNode {
return &artNode{nodeType: Node4, nodePtr: unsafe.Pointer(&node4{})}
}
// newNode16 creates an embedded artNode of node16
func newNode16() *artNode {
return &artNode{nodeType: Node16, nodePtr: unsafe.Pointer(&node16{})}
}
// newNode48 creates an embedded artNode of node48
func newNode48() *artNode {
return &artNode{nodeType: Node48, nodePtr: unsafe.Pointer(&node48{})}
}
// newNode256 creates an embedded artNode of node256
func newNode256() *artNode {
return &artNode{nodeType: Node256, nodePtr: unsafe.Pointer(&node256{})}
}
// Key returns the key of the given node, or nil if it is not a leafNode.
func (n *artNode) Key() Key {
if n.isLeaf() {
return n.leafNode().key
}
return nil
}
// Value returns the value of the given node, or nil if it is not a leafNode.
func (n *artNode) Value() interface{} {
if n.nodeType != LeafNode {
return nil
}
return n.leafNode().value
}
// NodeType returns the nodeType of the given node
func (n *artNode) NodeType() NodeType {
return n.nodeType
}
// isFull returns whether this particular artNode is full or not .
func (n *artNode) isFull() bool {
return n.node().size == n.maxSize()
}
// isLeaf returns whether this particular artNode is a leafNode or not .
func (n *artNode) isLeaf() bool { return n.nodeType == LeafNode }
// isMatch returns whether the key stored in the leafNode matches the passed in key or not .
func (n *artNode) isMatch(key []byte) bool {
if n.nodeType != LeafNode {
return false
}
if len(n.leafNode().key) != len(key) {
return false
}
return bytes.Compare(n.leafNode().key, key) == 0
}
// prefixMismatch returns the position of first byte that differ between the passed in key
// and the compressed path of the current node at the specified depth.
func (n *artNode) prefixMismatch(key []byte, depth int) int {
var idx int
var keyChar byte
for idx = 0; idx < min(maxPrefixLen, n.node().prefixLen); idx++ {
if depth+idx < 0 || depth+idx >= len(key) {
keyChar = byte(0)
} else {
keyChar = key[depth+idx]
}
if keyChar != n.node().prefix[idx] {
return idx
}
}
if n.node().prefixLen > maxPrefixLen {
minKey := n.minimum().leafNode().key
for ; idx < n.node().prefixLen; idx++ {
if key[depth+idx] != minKey[depth+idx] {
return idx
}
}
}
return idx
}
// index returns the position of the given key byte's child pointer in the children array.
// If not found, return -1.
func (n *artNode) index(key byte) int {
switch n.nodeType {
case Node4:
return bytes.IndexByte(n.node4().keys[:], key)
case Node16:
return bytes.IndexByte(n.node16().keys[:], key)
case Node48:
return int(n.node48().keys[key])
case Node256:
return int(key)
}
return -1
}
// findChild returns a pointer to the child that matches the passed in key,
// or nil if not present.
func (n *artNode) findChild(key byte) **artNode {
if n == nil {
return &nullNode
}
var idx int
switch n.nodeType {
case Node4, Node16, Node48:
idx = n.index(key)
case Node256:
idx = int(key)
}
// Not found.
if idx < 0 {
return &nullNode
}
switch n.nodeType {
case Node4:
return &n.node4().children[idx]
case Node16:
return &n.node16().children[idx]
case Node48:
if idx == 0 { // children[0] is not used in Node48
return &nullNode
}
return &n.node48().children[idx]
case Node256:
if n.node256().children[idx] == nil {
return &nullNode
}
return &n.node256().children[idx]
}
return &nullNode
}
// addChild adds the passed in artNode to the current artNode's children at the specified key.
// The current node will grow if necessary when the insertion to take place.
func (n *artNode) addChild(key byte, node *artNode) {
switch n.nodeType {
case Node4:
if n.isFull() {
n.grow()
n.addChild(key, node)
break
}
n4 := n.node4()
var idx int
if n4.size == 0 {
idx = 0
} else {
for idx = 0; idx < n4.size; idx++ {
if key < n4.keys[idx] {
break
}
}
}
for i := n4.size; i > idx; i-- {
n4.keys[i] = n4.keys[i-1]
n4.children[i] = n4.children[i-1]
}
n4.keys[idx] = key
n4.children[idx] = node
n4.size++
case Node16:
if n.isFull() {
n.grow()
n.addChild(key, node)
break
}
n16 := n.node16()
var idx int
if n16.size == 0 {
idx = 0
} else {
idx = sort.Search(n16.size, func(i int) bool {
return key <= n16.keys[byte(i)]
})
}
for i := n16.size; i > idx; i-- {
n16.keys[i] = n16.keys[i-1]
n16.children[i] = n16.children[i-1]
}
n16.keys[idx] = key
n16.children[idx] = node
n16.size++
case Node48:
if n.isFull() {
n.grow()
n.addChild(key, node)
break
}
n48 := n.node48()
idx := 1
for n48.children[idx] != nil {
idx++
}
n48.children[idx] = node
n48.keys[key] = byte(idx)
n48.size++
case Node256:
if n.isFull() {
break
}
n.node256().children[key] = node
n.node().size++
}
}
// RemoveChild removes the child of the passed in key,
// and will shrink if it falls below its minimum size.
func (n *artNode) RemoveChild(key byte) {
switch n.nodeType {
case Node4:
n4 := n.node4()
idx := n.index(key)
if idx < 0 {
break
}
n4.keys[idx] = byte(0)
n4.children[idx] = nil
for i := idx; i < n4.size-1; i++ {
n4.keys[i] = n4.keys[i+1]
n4.children[i] = n4.children[i+1]
}
n4.keys[n4.size-1] = byte(0)
n4.children[n4.size-1] = nil
n4.size--
case Node16:
n16 := n.node16()
idx := n.index(key)
if idx < 0 {
break
}
n16.keys[idx] = 0
n16.children[idx] = nil
for i := idx; i < n16.size-1; i++ {
n16.keys[i] = n16.keys[i+1]
n16.children[i] = n16.children[i+1]
}
n16.keys[n16.size-1] = 0
n16.children[n16.size-1] = nil
n16.size--
case Node48:
n48 := n.node48()
idx := n.index(key)
if idx <= 0 {
break
}
n48.children[idx] = nil
n48.keys[key] = byte(0)
n48.size--
case Node256:
n256 := n.node256()
n256.children[n.index(key)] = nil
n256.size--
}
if n.node().size < n.minSize() {
n.shrink()
}
}
// grow upgrades the current artNode to contain more children.
func (n *artNode) grow() {
switch n.nodeType {
case Node4:
newNode := newNode16()
newNode.copyMeta(n)
newNode16 := newNode.node16()
n4 := n.node4()
for i := 0; i < n4.size; i++ {
newNode16.keys[i] = n4.keys[i]
newNode16.children[i] = n4.children[i]
}
n.replaceWith(newNode)
case Node16:
newNode := newNode48()
newNode.copyMeta(n)
newNode48 := newNode.node48()
n16 := n.node16()
for i := 0; i < n16.size; i++ {
newNode48.keys[n16.keys[i]] = byte(i + 1)
newNode48.children[i+1] = n16.children[i]
}
n.replaceWith(newNode)
case Node48:
newNode := newNode256()
newNode.copyMeta(n)
newNode256 := newNode.node256()
n48 := n.node48()
for i := 0; i < len(n48.keys); i++ {
if n48.keys[i] == byte(0) {
continue
}
if n48.children[n48.keys[i]] != nil && n48.children[n48.keys[i]] != nullNode {
newNode256.children[byte(i)] = n48.children[n48.keys[i]]
}
}
n.replaceWith(newNode)
case Node256:
// Can not get bigger
}
}
// shrink downgrades the current artNode to reduce the memory cost.
func (n *artNode) shrink() {
switch n.nodeType {
case Node4:
n4 := n.node4()
newNode := n4.children[0]
if !newNode.isLeaf() {
currentPrefixLen := n4.prefixLen
if currentPrefixLen < maxPrefixLen {
n4.prefix[currentPrefixLen] = n4.keys[0]
currentPrefixLen++
}
if currentPrefixLen < maxPrefixLen {
childPrefixLen := min(newNode.node().prefixLen, maxPrefixLen-currentPrefixLen)
memcpy(n4.prefix[currentPrefixLen:], newNode.node().prefix[:], childPrefixLen)
currentPrefixLen += childPrefixLen
}
memcpy(newNode.node().prefix[:], n4.prefix[:], min(currentPrefixLen, maxPrefixLen))
newNode.node().prefixLen += n4.prefixLen + 1
}
n.replaceWith(newNode)
case Node16:
n16 := n.node16()
newNode := newNode4()
newNode.copyMeta(n)
newNode4 := newNode.node4()
newNode4.size = 0
for i := 0; i < n16.size; i++ {
newNode4.keys[newNode4.size] = n16.keys[i]
newNode4.children[newNode4.size] = n16.children[i]
newNode4.size++
}
n.replaceWith(newNode)
case Node48:
n48 := n.node48()
newNode := newNode16()
newNode.copyMeta(n)
newNode16 := newNode.node16()
newNode16.size = 0
for i := 0; i < len(n48.keys); i++ {
idx := n48.keys[byte(i)]
if idx <= 0 {
continue
}
newNode16.keys[newNode16.size] = byte(i)
newNode16.children[newNode16.size] = n48.children[idx]
newNode16.size++
}
n.replaceWith(newNode)
case Node256:
n256 := n.node256()
newNode := newNode48()
newNode.copyMeta(n)
newNode48 := newNode.node48()
newNode48.size = 0
for i := 0; i < len(n256.children); i++ {
if n256.children[byte(i)] == nil {
continue
}
newNode48.children[newNode48.size+1] = n256.children[byte(i)]
newNode48.keys[byte(i)] = byte(newNode48.size + 1)
newNode48.size++
}
n.replaceWith(newNode)
}
}
// longestCommonPrefix returns the longest number of bytes
// that match between the current artNode's prefix
// and the passed in artNode at the specified depth.
func (n *artNode) longestCommonPrefix(other *artNode, depth int) int {
limit := min(len(n.leafNode().key), len(other.leafNode().key)) - depth
for i := 0; i < limit; i++ {
if n.leafNode().key[depth+i] != other.leafNode().key[depth+i] {
return i
}
}
return limit
}
// minSize returns the minimum number of children for the current artNode.
func (n *artNode) minSize() int {
switch n.nodeType {
case Node4:
return node4Min
case Node16:
return node16Min
case Node48:
return node48Min
case Node256:
return node256Min
}
return 0
}
// maxSize returns the maximum number of children for the current artNode.
func (n *artNode) maxSize() int {
switch n.nodeType {
case Node4:
return node4Max
case Node16:
return node16Max
case Node48:
return node48Max
case Node256:
return node256Max
}
return 0
}
// minimum returns the minimum child at the current artNode.
func (n *artNode) minimum() *artNode {
if n == nil {
return nil
}
switch n.nodeType {
case LeafNode:
return n
case Node4:
return n.node4().children[0].minimum()
case Node16:
return n.node16().children[0].minimum()
case Node48:
i := 0
for n.node48().keys[i] == 0 {
i++
}
return n.node48().children[n.node48().keys[i]].minimum()
case Node256:
i := 0
for n.node256().children[i] == nil {
i++
}
return n.node256().children[i].minimum()
}
return n
}
//maximum returns the maximum child at the current artNode.
func (n *artNode) maximum() *artNode {
if n == nil {
return nil
}
switch n.nodeType {
case LeafNode:
return n
case Node4:
n4 := n.node4()
return n4.children[n4.size-1].maximum()
case Node16:
n16 := n.node16()
return n16.children[n16.size-1].maximum()
case Node48:
n48 := n.node48()
i := len(n48.keys) - 1
for n48.keys[i] == 0 {
i--
}
return n48.children[n48.keys[i]].maximum()
case Node256:
n256 := n.node256()
i := len(n256.children) - 1
for i > 0 && n256.children[byte(i)] == nil {
i--
}
return n256.children[i].maximum()
}
return nil
}
// node returns the metadata node of the current artNode.
func (n *artNode) node() *node {
return (*node)(n.nodePtr)
}
// node4 returns the metadata node4 of the current artNode.
func (n *artNode) node4() *node4 {
return (*node4)(n.nodePtr)
}
// node16 returns the metadata node16 of the current artNode.
func (n *artNode) node16() *node16 {
return (*node16)(n.nodePtr)
}
// node48 returns the metadata node48 of the current artNode.
func (n *artNode) node48() *node48 {
return (*node48)(n.nodePtr)
}
// node256 returns the metadata node256 of the current artNode.
func (n *artNode) node256() *node256 {
return (*node256)(n.nodePtr)
}
// leafNode returns the metadata leafNode of the current artNode.
func (n *artNode) leafNode() *leafNode {
return (*leafNode)(n.nodePtr)
}
// replaceWith replaces the current artNode with the passed in artNode.
func (n *artNode) replaceWith(other *artNode) {
*n = *other
}
// copyMeta copies the prefix and size metadata from the passed in artNode
// to the current artNode.
func (n *artNode) copyMeta(src *artNode) {
if src == nil {
return
}
to := n.node()
from := src.node()
to.size = from.size
to.prefixLen = from.prefixLen
for i, limit := 0, min(from.prefixLen, maxPrefixLen); i < limit; i++ {
to.prefix[i] = from.prefix[i]
}
}
// min returns the smallest of the two passed in integers.
func min(a int, b int) int {
if a < b {
return a
}
return b
}