-
Notifications
You must be signed in to change notification settings - Fork 580
/
tree.go
267 lines (236 loc) · 6.77 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
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
/// B+ tree implementation on KVStore
package store
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
store "github.com/cosmos/cosmos-sdk/store"
stypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Tree is an augmented B+ tree implementation.
// Branches have m sized key index slice. Each key index represents
// the starting index of the child node's index(inclusive), and the
// ending index of the previous node of the child node's index(exclusive).
// TODO: We should abstract out the leaves of this tree to allow more data aside from
// the accumulation value to go there.
type Tree struct {
store store.KVStore
m uint8
}
func NewTree(store store.KVStore, m uint8) Tree {
tree := Tree{store, m}
tree.Set(nil, sdk.ZeroInt())
return tree
}
func (t Tree) Set(key []byte, acc sdk.Int) {
node := t.nodeGet(0, key)
node.setLeaf(acc)
node.parent().push(child{key, acc})
}
func (t Tree) Remove(key []byte) {
node := t.nodeGet(0, key)
if !node.exists() {
return
}
parent := node.parent()
node.delete()
parent.pull(key)
}
// node is pointer to a specific node inside the tree
type node struct {
tree Tree
level uint16
key []byte
// XXX: cache stored value?
}
// nodeIterator iterates over nodes in a given level. It only iterates directly over the pointers
// to the nodes, not the actual nodes themselves, to save loading additional data into memory.
type nodeIterator struct {
tree Tree
level uint16
store.Iterator
}
func (iter nodeIterator) node() *node {
if !iter.Valid() {
return nil
}
res := node{
tree: iter.tree,
level: iter.level,
key: iter.Key()[7:],
}
return &res
}
type child struct {
Index []byte
Acc sdk.Int
}
type children []child // max length M slice of key bytes, sorted by index
// nodeKey takes in a nodes layer, and its key, and constructs the
// its key in the underlying datastore.
func (t Tree) nodeKey(level uint16, key []byte) []byte {
bz := make([]byte, 2)
binary.BigEndian.PutUint16(bz, level)
return append(append([]byte("node/"), bz...), key...)
}
// leafKey constructs a key for a node pointer representing a leaf node.
func (t Tree) leafKey(key []byte) []byte {
return t.nodeKey(0, key)
}
func (t Tree) root() *node {
iter := stypes.KVStoreReversePrefixIterator(t.store, []byte("node/"))
if !iter.Valid() {
return nil
}
key := iter.Key()[5:]
return &node{
tree: t,
level: binary.BigEndian.Uint16(key[:2]),
key: key[2:],
}
}
// Get returns the (sdk.Int) accumulation value at a given leaf.
func (t Tree) Get(key []byte) (res sdk.Int) {
keybz := t.leafKey(key)
if !t.store.Has(keybz) {
return sdk.ZeroInt()
}
err := json.Unmarshal(t.store.Get(keybz), &res)
if err != nil {
panic(err)
}
return
}
func (node *node) create(children children) {
keybz := node.tree.nodeKey(node.level, node.key)
bz, err := json.Marshal(children)
if err != nil {
panic(err)
}
node.tree.store.Set(keybz, bz)
}
func (t Tree) nodeGet(level uint16, key []byte) *node {
return &node{
tree: t,
level: level,
key: key,
}
}
// XXX: store.Iterator -> custom node iterator
func (t Tree) nodeIterator(level uint16, begin, end []byte) nodeIterator {
var endBytes []byte
if end != nil {
endBytes = t.nodeKey(level, end)
} else {
endBytes = stypes.PrefixEndBytes(t.nodeKey(level, nil))
}
return nodeIterator{
tree: t,
level: level,
Iterator: t.store.Iterator(t.nodeKey(level, begin), endBytes),
}
}
func (t Tree) nodeReverseIterator(level uint16, begin, end []byte) nodeIterator {
var endBytes []byte
if end != nil {
endBytes = t.nodeKey(level, end)
} else {
endBytes = stypes.PrefixEndBytes(t.nodeKey(level, nil))
}
return nodeIterator{
tree: t,
level: level,
Iterator: t.store.ReverseIterator(t.nodeKey(level, begin), endBytes),
}
}
func (t Tree) Iterator(begin, end []byte) store.Iterator {
return t.nodeIterator(0, begin, end)
}
func (t Tree) ReverseIterator(begin, end []byte) store.Iterator {
return t.nodeReverseIterator(0, begin, end)
}
// accumulationSplit returns the accumulated value for all of the following:
// left: all leaves under nodePointer with key < provided key
// exact: leaf with key = provided key
// right: all leaves under nodePointer with key > provided key
// Note that the equalities here are _exclusive_.
func (node *node) accumulationSplit(key []byte) (left sdk.Int, exact sdk.Int, right sdk.Int) {
left, exact, right = sdk.ZeroInt(), sdk.ZeroInt(), sdk.ZeroInt()
if node.isLeaf() {
accumulatedValue := sdk.ZeroInt()
bz := node.tree.store.Get(node.tree.leafKey(node.key))
err := json.Unmarshal(bz, &accumulatedValue)
if err != nil {
panic(err)
}
// Check if the leaf key is to the left of the input key,
// if so this value is on the left. Similar for the other cases.
// Recall that all of the output arguments default to 0, if unset internally.
switch bytes.Compare(node.key, key) {
case -1:
left = accumulatedValue
case 0:
exact = accumulatedValue
case 1:
right = accumulatedValue
}
return
}
children := node.children()
idx, match := children.find(key)
if !match {
idx--
}
left, exact, right = node.tree.nodeGet(node.level-1, children[idx].Index).accumulationSplit(key)
left = left.Add(children[:idx].accumulate())
right = right.Add(children[idx+1:].accumulate())
return
}
// TotalAccumulatedValue returns the sum of the weights for all leaves
func (t Tree) TotalAccumulatedValue() sdk.Int {
return t.SubsetAccumulation(nil, nil)
}
// Prefix sum returns the total weight of all leaves with keys <= to the provided key.
func (t Tree) PrefixSum(key []byte) sdk.Int {
return t.SubsetAccumulation(nil, key)
}
// SubsetAccumulation returns the total value of all leaves with keys
// between start and end (inclusive of both ends)
// if start is nil, it is the beginning of the tree.
// if end is nil, it is the end of the tree.
func (t Tree) SubsetAccumulation(start []byte, end []byte) sdk.Int {
if start == nil {
left, exact, _ := t.root().accumulationSplit(end)
return left.Add(exact)
}
if end == nil {
_, exact, right := t.root().accumulationSplit(start)
return exact.Add(right)
}
_, leftexact, leftrest := t.root().accumulationSplit(start)
_, _, rightest := t.root().accumulationSplit(end)
return leftexact.Add(leftrest).Sub(rightest)
}
func (t Tree) SplitAcc(key []byte) (sdk.Int, sdk.Int, sdk.Int) {
return t.root().accumulationSplit(key)
}
func (node *node) visualize(depth int, acc sdk.Int) {
if !node.exists() {
return
}
for i := 0; i < depth; i++ {
fmt.Printf(" ")
}
fmt.Printf("- ")
fmt.Printf("{%d %+v %d}\n", node.level, node.key, acc)
for i, child := range node.children() {
childnode := node.child(uint16(i))
childnode.visualize(depth+1, child.Acc)
}
}
// DebugVisualize prints the entire tree to stdout
func (t Tree) DebugVisualize() {
t.root().visualize(0, sdk.ZeroInt())
}