forked from cosmos/iavl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
104 lines (89 loc) · 1.88 KB
/
util.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
package iavl
import (
"bytes"
"fmt"
"sort"
)
// PrintTree prints the whole tree in an indented form.
func PrintTree(tree *ImmutableTree) {
ndb, root := tree.ndb, tree.root
printNode(ndb, root, 0)
}
func printNode(ndb *nodeDB, node *Node, indent int) {
indentPrefix := ""
for i := 0; i < indent; i++ {
indentPrefix += " "
}
if node == nil {
fmt.Printf("%s<nil>\n", indentPrefix)
return
}
if node.rightNode != nil {
printNode(ndb, node.rightNode, indent+1)
} else if node.rightHash != nil {
rightNode := ndb.GetNode(node.rightHash)
printNode(ndb, rightNode, indent+1)
}
hash := node._hash()
fmt.Printf("%sh:%X\n", indentPrefix, hash)
if node.isLeaf() {
fmt.Printf("%s%X:%X (%v)\n", indentPrefix, node.key, node.value, node.height)
}
if node.leftNode != nil {
printNode(ndb, node.leftNode, indent+1)
} else if node.leftHash != nil {
leftNode := ndb.GetNode(node.leftHash)
printNode(ndb, leftNode, indent+1)
}
}
func maxInt8(a, b int8) int8 {
if a > b {
return a
}
return b
}
func cp(bz []byte) (ret []byte) {
ret = make([]byte, len(bz))
copy(ret, bz)
return ret
}
// Returns a slice of the same length (big endian)
// except incremented by one.
// Appends 0x00 if bz is all 0xFF.
// CONTRACT: len(bz) > 0
func cpIncr(bz []byte) (ret []byte) {
ret = cp(bz)
for i := len(bz) - 1; i >= 0; i-- {
if ret[i] < byte(0xFF) {
ret[i]++
return
}
ret[i] = byte(0x00)
if i == 0 {
return append(ret, 0x00)
}
}
return []byte{0x00}
}
type byteslices [][]byte
func (bz byteslices) Len() int {
return len(bz)
}
func (bz byteslices) Less(i, j int) bool {
switch bytes.Compare(bz[i], bz[j]) {
case -1:
return true
case 0, 1:
return false
default:
panic("should not happen")
}
}
func (bz byteslices) Swap(i, j int) {
bz[j], bz[i] = bz[i], bz[j]
}
func sortByteSlices(src [][]byte) [][]byte {
bzz := byteslices(src)
sort.Sort(bzz)
return bzz
}