-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathboltstore.go
More file actions
165 lines (152 loc) · 3.47 KB
/
boltstore.go
File metadata and controls
165 lines (152 loc) · 3.47 KB
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
package kvbench
import (
"errors"
"sync"
"github.com/boltdb/bolt"
"github.com/tidwall/match"
)
var boltBucket = []byte("keys")
type boltStore struct {
mu sync.RWMutex
db *bolt.DB
}
func boltKey(key []byte) []byte {
r := make([]byte, len(key)+1)
r[0] = 'k'
copy(r[1:], key)
return r
}
func newBoltStore(path string, fsync bool) (*boltStore, error) {
if path == ":memory:" {
return nil, errMemoryNotAllowed
}
db, err := bolt.Open(path, 0666, nil)
if err != nil {
return nil, err
}
db.NoSync = !fsync
if err := db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(boltBucket)
return err
}); err != nil {
db.Close()
return nil, err
}
return &boltStore{
db: db,
}, nil
}
func (s *boltStore) Close() error {
s.db.Close()
return nil
}
func (s *boltStore) PSet(keys, values [][]byte) error {
return s.db.Batch(func(tx *bolt.Tx) error {
b := tx.Bucket(boltBucket)
for i := 0; i < len(keys); i++ {
if err := b.Put(boltKey(keys[i]), values[i]); err != nil {
return err
}
}
return nil
})
}
func (s *boltStore) PGet(keys [][]byte) ([][]byte, []bool, error) {
var values [][]byte
var oks []bool
err := s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(boltBucket)
for i := 0; i < len(keys); i++ {
v := b.Get(boltKey(keys[i]))
if v == nil {
values = append(values, nil)
oks = append(oks, false)
} else {
values = append(values, bcopy(v))
oks = append(oks, true)
}
}
return nil
})
if err != nil {
return nil, nil, err
}
return values, oks, nil
}
func (s *boltStore) Set(key, value []byte) error {
return s.db.Update(func(tx *bolt.Tx) error {
return tx.Bucket(boltBucket).Put(boltKey(key), value)
})
}
func (s *boltStore) Get(key []byte) ([]byte, bool, error) {
var v []byte
err := s.db.View(func(tx *bolt.Tx) error {
v = tx.Bucket(boltBucket).Get(boltKey(key))
return nil
})
return v, v != nil, err
}
func (s *boltStore) Del(key []byte) (bool, error) {
var v []byte
err := s.db.Update(func(tx *bolt.Tx) error {
bkey := boltKey(key)
v = tx.Bucket(boltBucket).Get(bkey)
return tx.Bucket(boltBucket).Delete(bkey)
})
return v != nil, err
}
func (s *boltStore) Keys(pattern []byte, limit int, withvalues bool) ([][]byte, [][]byte, error) {
spattern := string(pattern)
min, max := match.Allowable(spattern)
bmin := []byte(min)
var keys [][]byte
var vals [][]byte
err := s.db.View(func(tx *bolt.Tx) error {
if len(spattern) > 0 && spattern[0] == '*' {
err := tx.Bucket(boltBucket).ForEach(func(key, value []byte) error {
if limit > -1 && len(keys) >= limit {
return errors.New("done")
}
skey := string(key[1:])
if match.Match(skey, spattern) {
keys = append(keys, []byte(skey))
if withvalues {
vals = append(vals, bcopy(value))
}
}
return nil
})
if err != nil && err.Error() == "done" {
err = nil
}
return err
}
c := tx.Bucket(boltBucket).Cursor()
for key, value := c.Seek(bmin); key != nil; key, value = c.Next() {
if limit > -1 && len(keys) >= limit {
break
}
skey := string(key[1:])
if skey >= max {
break
}
if match.Match(skey, spattern) {
keys = append(keys, []byte(skey))
if withvalues {
vals = append(vals, bcopy(value))
}
}
}
return nil
})
return keys, vals, err
}
func (s *boltStore) FlushDB() error {
return s.db.Update(func(tx *bolt.Tx) error {
if err := tx.DeleteBucket(boltBucket); err != nil {
return err
}
_, err := tx.CreateBucket(boltBucket)
return err
})
}