forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bolt.go
186 lines (155 loc) · 3.46 KB
/
bolt.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
package storage
import (
"bytes"
"github.com/boltdb/bolt"
)
// Bolt implementation of Store
type Bolt struct {
db *bolt.DB
bucket []byte
}
func NewBolt(db *bolt.DB, bucket string) *Bolt {
return &Bolt{
db: db,
bucket: []byte(bucket),
}
}
func (b *Bolt) View(f func(tx ReadOnlyTx) error) error {
return DoView(b, f)
}
func (b *Bolt) Update(f func(tx Tx) error) error {
return DoUpdate(b, f)
}
func (b *Bolt) put(tx *bolt.Tx, key string, value []byte) error {
bucket, err := tx.CreateBucketIfNotExists(b.bucket)
if err != nil {
return err
}
err = bucket.Put([]byte(key), value)
if err != nil {
return err
}
return nil
}
func (b *Bolt) Put(key string, value []byte) error {
return b.db.Update(func(tx *bolt.Tx) error {
return b.put(tx, key, value)
})
}
func (b *Bolt) get(tx *bolt.Tx, key string) (*KeyValue, error) {
bucket := tx.Bucket(b.bucket)
if bucket == nil {
return nil, ErrNoKeyExists
}
val := bucket.Get([]byte(key))
if val == nil {
return nil, ErrNoKeyExists
}
value := make([]byte, len(val))
copy(value, val)
return &KeyValue{
Key: key,
Value: value,
}, nil
}
func (b *Bolt) Get(key string) (kv *KeyValue, err error) {
err = b.db.View(func(tx *bolt.Tx) error {
kv, err = b.get(tx, key)
return err
})
return
}
func (b *Bolt) delete(tx *bolt.Tx, key string) error {
bucket := tx.Bucket(b.bucket)
if bucket == nil {
return nil
}
return bucket.Delete([]byte(key))
}
func (b *Bolt) Delete(key string) error {
return b.db.Update(func(tx *bolt.Tx) error {
return b.delete(tx, key)
})
}
func (b *Bolt) exists(tx *bolt.Tx, key string) (bool, error) {
bucket := tx.Bucket(b.bucket)
if bucket == nil {
return false, nil
}
val := bucket.Get([]byte(key))
exists := val != nil
return exists, nil
}
func (b *Bolt) Exists(key string) (exists bool, err error) {
err = b.db.View(func(tx *bolt.Tx) error {
exists, err = b.exists(tx, key)
return err
})
return
}
func (b *Bolt) list(tx *bolt.Tx, prefixStr string) (kvs []*KeyValue, err error) {
bucket := tx.Bucket(b.bucket)
if bucket == nil {
return
}
cursor := bucket.Cursor()
prefix := []byte(prefixStr)
for key, v := cursor.Seek(prefix); bytes.HasPrefix(key, prefix); key, v = cursor.Next() {
value := make([]byte, len(v))
copy(value, v)
kvs = append(kvs, &KeyValue{
Key: string(key),
Value: value,
})
}
return
}
func (b *Bolt) List(prefix string) (kvs []*KeyValue, err error) {
err = b.db.View(func(tx *bolt.Tx) error {
kvs, err = b.list(tx, prefix)
return err
})
return
}
func (b *Bolt) BeginTx() (Tx, error) {
return b.newTx(true)
}
func (b *Bolt) BeginReadOnlyTx() (ReadOnlyTx, error) {
return b.newTx(false)
}
func (b *Bolt) newTx(write bool) (*boltTx, error) {
tx, err := b.db.Begin(write)
if err != nil {
return nil, err
}
return &boltTx{
b: b,
tx: tx,
}, nil
}
// BoltTx wraps an underlying bolt.Tx type to implement the Tx interface.
type boltTx struct {
b *Bolt
tx *bolt.Tx
}
func (t *boltTx) Get(key string) (*KeyValue, error) {
return t.b.get(t.tx, key)
}
func (t *boltTx) Exists(key string) (bool, error) {
return t.b.exists(t.tx, key)
}
func (t *boltTx) List(prefix string) ([]*KeyValue, error) {
return t.b.list(t.tx, prefix)
}
func (t *boltTx) Put(key string, value []byte) error {
return t.b.put(t.tx, key, value)
}
func (t *boltTx) Delete(key string) error {
return t.b.delete(t.tx, key)
}
func (t *boltTx) Commit() error {
return t.tx.Commit()
}
func (t *boltTx) Rollback() error {
return t.tx.Rollback()
}