-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket.go
183 lines (133 loc) · 3.62 KB
/
bucket.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
package tempdb
import (
"bytes"
"errors"
"github.com/btcsuite/btcwallet/walletdb"
)
type Bucket struct {
tx *Transaction
ID BucketID
Parent BucketID
Key []byte
Value map[string][]byte
sequence uint64
}
func (bkt *Bucket) Put(key, value []byte) error {
Logger.Debug("bucket put", "key", key, "value", value, "bucket ID", bkt.ID)
if key == nil {
return walletdb.ErrKeyRequired
}
bkt.Value[string(key)] = value
return nil
}
func (bkt *Bucket) Get(key []byte) []byte {
Logger.Debug("bucket get", "key", key, "bucket ID", bkt.ID)
return bkt.Value[string(key)]
}
func (bkt *Bucket) Delete(key []byte) error {
Logger.Debug("bucket delete", "key", key, "bucket ID", bkt.ID)
delete(bkt.Value, string(key))
return nil
}
func (bkt *Bucket) ForEach(fn func(k, v []byte) error) error {
Logger.Debug("bucket for each", "bucket ID", bkt.ID)
c := newCursor(bkt)
for k, v := c.First(); k != nil; k, v = c.Next() {
err := fn([]byte(k), v)
if err != nil {
return err
}
}
return nil
}
func (bkt *Bucket) NestedReadBucket(key []byte) walletdb.ReadBucket {
Logger.Debug("create nested read bucket", "key", key, "parent ID", bkt.ID)
return bkt.NestedReadWriteBucket(key)
}
func (bkt *Bucket) NestedReadWriteBucket(key []byte) walletdb.ReadWriteBucket {
Logger.Debug("create nested read/write bucket", "key", key, "parent ID", bkt.ID)
_, nbkt, ok := bkt.find(key)
if !ok {
return nil
}
return nbkt
}
func (bkt *Bucket) CreateBucket(key []byte) (walletdb.ReadWriteBucket, error) {
Logger.Debug("create bucket", "key", key, "parent ID", bkt.ID)
if key == nil {
return nil, walletdb.ErrBucketNameRequired
}
_, _, ok := bkt.find(key)
if ok {
return nil, walletdb.ErrBucketExists
}
// add a new empty value. used for iteration.
bkt.Put(key, nil)
return bkt.tx.createBucket(key, bkt.ID), nil
}
func (bkt *Bucket) CreateBucketIfNotExists(key []byte) (walletdb.ReadWriteBucket, error) {
Logger.Debug("create bucket (if not exists)", "key", key, "parent ID", bkt.ID)
nbkt := bkt.NestedReadWriteBucket(key)
if nbkt != nil {
return nbkt, nil
}
nbkt, err := bkt.CreateBucket(key)
if err != nil && !errors.Is(err, walletdb.ErrBucketExists) {
return nil, err
}
return nbkt, nil
}
func (bkt *Bucket) DeleteNestedBucket(key []byte) error {
Logger.Debug("delete nested bucket", "key", key, "parent ID", bkt.ID)
if key == nil {
return walletdb.ErrIncompatibleValue
}
i, _, ok := bkt.find(key)
if !ok {
return walletdb.ErrBucketNotFound
}
bkt.tx.State.Buckets = append(bkt.tx.State.Buckets[:i], bkt.tx.State.Buckets[i+1:]...)
// delete the empty value.
bkt.Delete(key)
return nil
}
func (bkt *Bucket) Tx() walletdb.ReadWriteTx {
return bkt.tx
}
func (bkt *Bucket) NextSequence() (uint64, error) {
bkt.sequence++
return bkt.sequence, nil
}
func (bkt *Bucket) SetSequence(v uint64) error {
bkt.sequence = v
return nil
}
func (bkt *Bucket) Sequence() uint64 {
return bkt.sequence
}
func (bkt *Bucket) ReadCursor() walletdb.ReadCursor {
Logger.Debug("create read cursor", "parent ID", bkt.ID)
return newCursor(bkt)
}
func (bkt *Bucket) ReadWriteCursor() walletdb.ReadWriteCursor {
Logger.Debug("create read/write cursor", "parent ID", bkt.ID)
return newCursor(bkt)
}
func (bkt *Bucket) find(key []byte) (int, *Bucket, bool) {
for i, nbkt := range bkt.tx.State.Buckets {
// ensure the bucket is not the root bucket.
if nbkt.Parent == RootBucketID {
continue
}
// ensure the bucket is nested in the current bucket.
if nbkt.Parent != bkt.ID {
continue
}
// ensure the key matches.
if !bytes.Equal(nbkt.Key, key) {
continue
}
return i, &nbkt, true
}
return 0, nil, false
}