forked from cosmos/iavl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
554 lines (480 loc) · 13.7 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
package iavl
import (
"bytes"
"fmt"
"io"
"golang.org/x/crypto/ripemd160"
"github.com/tendermint/go-wire"
cmn "github.com/tendermint/tmlibs/common"
)
// Node represents a node in a Tree.
type Node struct {
key []byte
value []byte
version uint64
height int8
size int
hash []byte
leftHash []byte
leftNode *Node
rightHash []byte
rightNode *Node
persisted bool
}
// NewNode returns a new node from a key and value.
func NewNode(key []byte, value []byte) *Node {
return &Node{
key: key,
value: value,
height: 0,
size: 1,
version: 0,
}
}
// MakeNode constructs an *Node from an encoded byte slice.
//
// The new node doesn't have its hash saved or set. The caller must set it
// afterwards.
func MakeNode(buf []byte) (node *Node, err error) {
node = &Node{}
// Read node header.
node.height = int8(buf[0])
n := 1 // Keeps track of bytes read.
buf = buf[n:]
node.size, n, err = wire.GetVarint(buf)
if err != nil {
return nil, err
}
buf = buf[n:]
node.key, n, err = wire.GetByteSlice(buf)
if err != nil {
return nil, err
}
buf = buf[n:]
node.version = wire.GetUint64(buf)
buf = buf[8:]
// Read node body.
if node.isLeaf() {
node.value, _, err = wire.GetByteSlice(buf)
if err != nil {
return nil, err
}
} else { // Read children.
leftHash, n, err := wire.GetByteSlice(buf)
if err != nil {
return nil, err
}
buf = buf[n:]
rightHash, _, err := wire.GetByteSlice(buf)
if err != nil {
return nil, err
}
node.leftHash = leftHash
node.rightHash = rightHash
}
return node, nil
}
// String returns a string representation of the node.
func (node *Node) String() string {
if len(node.hash) == 0 {
return "<no hash>"
} else {
return fmt.Sprintf("%x", node.hash)
}
}
// clone creates a shallow copy of a node with its hash set to nil.
func (node *Node) clone() *Node {
if node.isLeaf() {
cmn.PanicSanity("Attempt to copy a leaf node")
}
return &Node{
key: node.key,
height: node.height,
version: node.version,
size: node.size,
hash: nil,
leftHash: node.leftHash,
leftNode: node.leftNode,
rightHash: node.rightHash,
rightNode: node.rightNode,
persisted: false,
}
}
func (node *Node) isLeaf() bool {
return node.height == 0
}
// Check if the node has a descendant with the given key.
func (node *Node) has(t *Tree, key []byte) (has bool) {
if bytes.Equal(node.key, key) {
return true
}
if node.isLeaf() {
return false
}
if bytes.Compare(key, node.key) < 0 {
return node.getLeftNode(t).has(t, key)
} else {
return node.getRightNode(t).has(t, key)
}
}
// Get a key under the node.
func (node *Node) get(t *Tree, key []byte) (index int, value []byte) {
if node.isLeaf() {
switch bytes.Compare(node.key, key) {
case -1:
return 1, nil
case 1:
return 0, nil
default:
return 0, node.value
}
}
if bytes.Compare(key, node.key) < 0 {
return node.getLeftNode(t).get(t, key)
} else {
rightNode := node.getRightNode(t)
index, value = rightNode.get(t, key)
index += node.size - rightNode.size
return index, value
}
}
func (node *Node) getByIndex(t *Tree, index int) (key []byte, value []byte) {
if node.isLeaf() {
if index == 0 {
return node.key, node.value
} else {
return nil, nil
}
} else {
// TODO: could improve this by storing the
// sizes as well as left/right hash.
leftNode := node.getLeftNode(t)
if index < leftNode.size {
return leftNode.getByIndex(t, index)
} else {
return node.getRightNode(t).getByIndex(t, index-leftNode.size)
}
}
}
// Computes the hash of the node without computing its descendants. Must be
// called on nodes which have descendant node hashes already computed.
func (node *Node) _hash() []byte {
if node.hash != nil {
return node.hash
}
hasher := ripemd160.New()
buf := new(bytes.Buffer)
if _, err := node.writeHashBytes(buf); err != nil {
cmn.PanicCrisis(err)
}
hasher.Write(buf.Bytes())
node.hash = hasher.Sum(nil)
return node.hash
}
// Hash the node and its descendants recursively. This usually mutates all
// descendant nodes. Returns the node hash and number of nodes hashed.
func (node *Node) hashWithCount() ([]byte, int) {
if node.hash != nil {
return node.hash, 0
}
hasher := ripemd160.New()
buf := new(bytes.Buffer)
_, hashCount, err := node.writeHashBytesRecursively(buf)
if err != nil {
cmn.PanicCrisis(err)
}
hasher.Write(buf.Bytes())
node.hash = hasher.Sum(nil)
return node.hash, hashCount + 1
}
// Writes the node's hash to the given io.Writer. This function expects
// child hashes to be already set.
func (node *Node) writeHashBytes(w io.Writer) (n int, err error) {
wire.WriteInt8(node.height, w, &n, &err)
wire.WriteVarint(node.size, w, &n, &err)
// Key is not written for inner nodes, unlike writeBytes.
if node.isLeaf() {
wire.WriteByteSlice(node.key, w, &n, &err)
wire.WriteByteSlice(node.value, w, &n, &err)
wire.WriteUint64(node.version, w, &n, &err)
} else {
if node.leftHash == nil || node.rightHash == nil {
cmn.PanicSanity("Found an empty child hash")
}
wire.WriteByteSlice(node.leftHash, w, &n, &err)
wire.WriteByteSlice(node.rightHash, w, &n, &err)
}
return
}
// Writes the node's hash to the given io.Writer.
// This function has the side-effect of calling hashWithCount.
func (node *Node) writeHashBytesRecursively(w io.Writer) (n int, hashCount int, err error) {
if node.leftNode != nil {
leftHash, leftCount := node.leftNode.hashWithCount()
node.leftHash = leftHash
hashCount += leftCount
}
if node.rightNode != nil {
rightHash, rightCount := node.rightNode.hashWithCount()
node.rightHash = rightHash
hashCount += rightCount
}
n, err = node.writeHashBytes(w)
return
}
// Writes the node as a serialized byte slice to the supplied io.Writer.
func (node *Node) writeBytes(w io.Writer) (n int, err error) {
wire.WriteInt8(node.height, w, &n, &err)
wire.WriteVarint(node.size, w, &n, &err)
// Unlike writeHashBytes, key is written for inner nodes.
wire.WriteByteSlice(node.key, w, &n, &err)
wire.WriteUint64(node.version, w, &n, &err)
if node.isLeaf() {
wire.WriteByteSlice(node.value, w, &n, &err)
} else {
if node.leftHash == nil {
cmn.PanicSanity("node.leftHash was nil in writeBytes")
}
wire.WriteByteSlice(node.leftHash, w, &n, &err)
if node.rightHash == nil {
cmn.PanicSanity("node.rightHash was nil in writeBytes")
}
wire.WriteByteSlice(node.rightHash, w, &n, &err)
}
return
}
func (node *Node) set(t *Tree, key []byte, value []byte) (
newSelf *Node, updated bool, orphaned []*Node,
) {
if node.isLeaf() {
switch bytes.Compare(key, node.key) {
case -1:
return &Node{
key: node.key,
height: 1,
size: 2,
leftNode: NewNode(key, value),
rightNode: node,
}, false, []*Node{}
case 1:
return &Node{
key: key,
height: 1,
size: 2,
leftNode: node,
rightNode: NewNode(key, value),
}, false, []*Node{}
default:
return NewNode(key, value), true, []*Node{node}
}
} else {
orphaned = append(orphaned, node)
node = node.clone()
if bytes.Compare(key, node.key) < 0 {
var leftOrphaned []*Node
node.leftNode, updated, leftOrphaned = node.getLeftNode(t).set(t, key, value)
node.leftHash = nil // leftHash is yet unknown
orphaned = append(orphaned, leftOrphaned...)
} else {
var rightOrphaned []*Node
node.rightNode, updated, rightOrphaned = node.getRightNode(t).set(t, key, value)
node.rightHash = nil // rightHash is yet unknown
orphaned = append(orphaned, rightOrphaned...)
}
if updated {
return node, updated, orphaned
} else {
node.calcHeightAndSize(t)
newNode, balanceOrphaned := node.balance(t)
return newNode, updated, append(orphaned, balanceOrphaned...)
}
}
}
// newHash/newNode: The new hash or node to replace node after remove.
// newKey: new leftmost leaf key for tree after successfully removing 'key' if changed.
// value: removed value.
func (node *Node) remove(t *Tree, key []byte) (
newHash []byte, newNode *Node, newKey []byte, value []byte, orphaned []*Node,
) {
if node.isLeaf() {
if bytes.Equal(key, node.key) {
return nil, nil, nil, node.value, []*Node{node}
}
return node.hash, node, nil, nil, orphaned
}
if bytes.Compare(key, node.key) < 0 {
var newLeftHash []byte
var newLeftNode *Node
newLeftHash, newLeftNode, newKey, value, orphaned =
node.getLeftNode(t).remove(t, key)
if len(orphaned) == 0 {
return node.hash, node, nil, value, orphaned
} else if newLeftHash == nil && newLeftNode == nil { // left node held value, was removed
return node.rightHash, node.rightNode, node.key, value, orphaned
}
orphaned = append(orphaned, node)
newNode := node.clone()
newNode.leftHash, newNode.leftNode = newLeftHash, newLeftNode
newNode.calcHeightAndSize(t)
newNode, balanceOrphaned := newNode.balance(t)
return newNode.hash, newNode, newKey, value, append(orphaned, balanceOrphaned...)
} else {
var newRightHash []byte
var newRightNode *Node
newRightHash, newRightNode, newKey, value, orphaned =
node.getRightNode(t).remove(t, key)
if len(orphaned) == 0 {
return node.hash, node, nil, value, orphaned
} else if newRightHash == nil && newRightNode == nil { // right node held value, was removed
return node.leftHash, node.leftNode, nil, value, orphaned
}
orphaned = append(orphaned, node)
newNode := node.clone()
newNode.rightHash, newNode.rightNode = newRightHash, newRightNode
if newKey != nil {
newNode.key = newKey
}
newNode.calcHeightAndSize(t)
newNode, balanceOrphaned := newNode.balance(t)
return newNode.hash, newNode, nil, value, append(orphaned, balanceOrphaned...)
}
}
func (node *Node) getLeftNode(t *Tree) *Node {
if node.leftNode != nil {
return node.leftNode
}
return t.ndb.GetNode(node.leftHash)
}
func (node *Node) getRightNode(t *Tree) *Node {
if node.rightNode != nil {
return node.rightNode
}
return t.ndb.GetNode(node.rightHash)
}
// Rotate right and return the new node and orphan.
func (node *Node) rotateRight(t *Tree) (newNode *Node, orphan *Node) {
// TODO: optimize balance & rotate.
node = node.clone()
l := node.getLeftNode(t)
_l := l.clone()
_lrHash, _lrCached := _l.rightHash, _l.rightNode
_l.rightHash, _l.rightNode = node.hash, node
node.leftHash, node.leftNode = _lrHash, _lrCached
node.calcHeightAndSize(t)
_l.calcHeightAndSize(t)
return _l, l
}
// Rotate left and return the new node and orphan.
func (node *Node) rotateLeft(t *Tree) (newNode *Node, orphan *Node) {
// TODO: optimize balance & rotate.
node = node.clone()
r := node.getRightNode(t)
_r := r.clone()
_rlHash, _rlCached := _r.leftHash, _r.leftNode
_r.leftHash, _r.leftNode = node.hash, node
node.rightHash, node.rightNode = _rlHash, _rlCached
node.calcHeightAndSize(t)
_r.calcHeightAndSize(t)
return _r, r
}
// NOTE: mutates height and size
func (node *Node) calcHeightAndSize(t *Tree) {
node.height = maxInt8(node.getLeftNode(t).height, node.getRightNode(t).height) + 1
node.size = node.getLeftNode(t).size + node.getRightNode(t).size
}
func (node *Node) calcBalance(t *Tree) int {
return int(node.getLeftNode(t).height) - int(node.getRightNode(t).height)
}
// NOTE: assumes that node can be modified
// TODO: optimize balance & rotate
func (node *Node) balance(t *Tree) (newSelf *Node, orphaned []*Node) {
if node.persisted {
panic("Unexpected balance() call on persisted node")
}
balance := node.calcBalance(t)
if balance > 1 {
if node.getLeftNode(t).calcBalance(t) >= 0 {
// Left Left Case
newNode, orphaned := node.rotateRight(t)
return newNode, []*Node{orphaned}
} else {
// Left Right Case
var leftOrphaned *Node
left := node.getLeftNode(t)
node.leftHash = nil
node.leftNode, leftOrphaned = left.rotateLeft(t)
newNode, rightOrphaned := node.rotateRight(t)
return newNode, []*Node{left, leftOrphaned, rightOrphaned}
}
}
if balance < -1 {
if node.getRightNode(t).calcBalance(t) <= 0 {
// Right Right Case
newNode, orphaned := node.rotateLeft(t)
return newNode, []*Node{orphaned}
} else {
// Right Left Case
var rightOrphaned *Node
right := node.getRightNode(t)
node.rightHash = nil
node.rightNode, rightOrphaned = right.rotateRight(t)
newNode, leftOrphaned := node.rotateLeft(t)
return newNode, []*Node{right, leftOrphaned, rightOrphaned}
}
}
// Nothing changed
return node, []*Node{}
}
// traverse is a wrapper over traverseInRange when we want the whole tree
func (node *Node) traverse(t *Tree, ascending bool, cb func(*Node) bool) bool {
return node.traverseInRange(t, nil, nil, ascending, false, cb)
}
func (node *Node) traverseInRange(t *Tree, start, end []byte, ascending bool, inclusive bool, cb func(*Node) bool) bool {
afterStart := start == nil || bytes.Compare(start, node.key) <= 0
beforeEnd := end == nil || bytes.Compare(node.key, end) < 0
if inclusive {
beforeEnd = end == nil || bytes.Compare(node.key, end) <= 0
}
stop := false
if afterStart && beforeEnd {
// IterateRange ignores this if not leaf
stop = cb(node)
}
if stop {
return stop
}
if node.isLeaf() {
return stop
}
if ascending {
// check lower nodes, then higher
if afterStart {
stop = node.getLeftNode(t).traverseInRange(t, start, end, ascending, inclusive, cb)
}
if stop {
return stop
}
if beforeEnd {
stop = node.getRightNode(t).traverseInRange(t, start, end, ascending, inclusive, cb)
}
} else {
// check the higher nodes first
if beforeEnd {
stop = node.getRightNode(t).traverseInRange(t, start, end, ascending, inclusive, cb)
}
if stop {
return stop
}
if afterStart {
stop = node.getLeftNode(t).traverseInRange(t, start, end, ascending, inclusive, cb)
}
}
return stop
}
// Only used in testing...
func (node *Node) lmd(t *Tree) *Node {
if node.isLeaf() {
return node
}
return node.getLeftNode(t).lmd(t)
}