-
Notifications
You must be signed in to change notification settings - Fork 1
/
bonsai.go
120 lines (99 loc) · 2.59 KB
/
bonsai.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
package state
import (
"math/rand"
"github.com/tendermint/iavl"
)
// store nonce as it's own type so no one can even try to fake it
type nonce int64
// Bonsai is a deformed tree forced to fit in a small pot
type Bonsai struct {
id nonce
Tree *iavl.VersionedTree
}
func (b *Bonsai) String() string {
return "Bonsai{" + b.Tree.String() + "}"
}
var _ SimpleDB = &Bonsai{}
// NewBonsai wraps a merkle tree and tags it to track children
func NewBonsai(tree *iavl.VersionedTree) *Bonsai {
return &Bonsai{
id: nonce(rand.Int63()),
Tree: tree,
}
}
// Get matches the signature of KVStore
func (b *Bonsai) Get(key []byte) []byte {
_, value := b.Tree.Get(key)
return value
}
// Get matches the signature of KVStore
func (b *Bonsai) Has(key []byte) bool {
return b.Tree.Has(key)
}
// Set matches the signature of KVStore
func (b *Bonsai) Set(key, value []byte) {
b.Tree.Set(key, value)
}
func (b *Bonsai) Remove(key []byte) (value []byte) {
value, _ = b.Tree.Remove(key)
return
}
func (b *Bonsai) GetWithProof(key []byte) ([]byte, *iavl.RangeProof, error) {
return b.Tree.GetWithProof(key)
}
func (b *Bonsai) GetVersioned(key []byte, version int64) (int, []byte) {
return b.Tree.GetVersioned(key, version)
}
func (b *Bonsai) GetVersionedWithProof(key []byte, version int64) ([]byte, *iavl.RangeProof, error) {
return b.Tree.GetVersionedWithProof(key, version)
}
func (b *Bonsai) List(start, end []byte, limit int) []Model {
res := []Model{}
stopAtCount := func(key []byte, value []byte) (stop bool) {
m := Model{key, value}
res = append(res, m)
return limit > 0 && len(res) >= limit
}
b.Tree.IterateRange(start, end, true, stopAtCount)
return res
}
func (b *Bonsai) First(start, end []byte) Model {
var m Model
stopAtFirst := func(key []byte, value []byte) (stop bool) {
m = Model{key, value}
return true
}
b.Tree.IterateRange(start, end, true, stopAtFirst)
return m
}
func (b *Bonsai) Last(start, end []byte) Model {
var m Model
stopAtFirst := func(key []byte, value []byte) (stop bool) {
m = Model{key, value}
return true
}
b.Tree.IterateRange(start, end, false, stopAtFirst)
return m
}
func (b *Bonsai) Checkpoint() SimpleDB {
return NewMemKVCache(b)
}
func (b *Bonsai) Commit(sub SimpleDB) error {
cache, ok := sub.(*MemKVCache)
if !ok {
return ErrNotASubTransaction()
}
// see if it was wrapping this struct
bb, ok := cache.store.(*Bonsai)
if !ok || (b.id != bb.id) {
return ErrNotASubTransaction()
}
// apply the cached data to the Bonsai
cache.applyCache()
return nil
}
// Discard will remove reference to this
func (b *Bonsai) Discard() {
b.id = 0
b.Tree = nil
}