-
Notifications
You must be signed in to change notification settings - Fork 44
/
lithium.go
202 lines (164 loc) · 3.69 KB
/
lithium.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
package kv
import (
"bytes"
"os"
"sync"
"time"
"github.com/cockroachdb/errors"
"github.com/projecteru2/core/types"
"go.etcd.io/bbolt"
)
// Lithium .
type Lithium struct {
sync.Mutex
// Name of the root bucket.
RootBucketKey []byte
bolt *bbolt.DB
path string
mode os.FileMode
timeout time.Duration
}
// NewLithium initializes a new Lithium instance.
func NewLithium() *Lithium {
return &Lithium{
RootBucketKey: []byte("root"),
}
}
// Reopen re-open a kvdb file.
func (l *Lithium) Reopen() error {
l.Lock()
defer l.Unlock()
if err := l.close(); err != nil {
return err
}
return l.open()
}
// Open opens a kvdb file.
func (l *Lithium) Open(path string, mode os.FileMode, timeout time.Duration) (err error) {
l.Lock()
defer l.Unlock()
l.path = path
l.mode = mode
l.timeout = timeout
return l.open()
}
// Close closes the kvdb file.
func (l *Lithium) Close() error {
l.Lock()
defer l.Unlock()
return l.close()
}
// Put creates/updates a key/value pair.
func (l *Lithium) Put(key []byte, value []byte) (err error) {
return l.update(func(bkt *bbolt.Bucket) error {
return bkt.Put(key, value)
})
}
// Get read a key's value.
func (l *Lithium) Get(key []byte) (dst []byte, err error) {
err = l.view(func(bkt *bbolt.Bucket) error {
src := bkt.Get(key)
dst = make([]byte, len(src))
for n := 0; n < len(dst); {
n += copy(dst, src)
}
return nil
})
return
}
// Delete deletes a key.
func (l *Lithium) Delete(key []byte) error {
return l.update(func(bkt *bbolt.Bucket) error {
return bkt.Delete(key)
})
}
// Scan scans all the key/value pairs.
func (l *Lithium) Scan(prefix []byte) (<-chan ScanEntry, func()) {
ch := make(chan ScanEntry)
exit := make(chan struct{})
abort := func() {
close(exit)
}
go func() {
defer close(ch)
scan := func(bkt *bbolt.Bucket) error {
c := bkt.Cursor()
for key, value := c.Seek(prefix); key != nil && bytes.HasPrefix(key, prefix); key, value = c.Next() {
select {
case <-exit:
return nil
case ch <- LithiumScanEntry{key: key, value: value}:
}
}
return nil
}
if err := l.view(scan); err != nil {
select {
case <-exit:
case ch <- LithiumScanEntry{err: err}:
}
}
}()
return ch, abort
}
// NextSequence generates a new sequence.
func (l *Lithium) NextSequence() (uint64, error) {
var seq uint64
err := l.update(func(bkt *bbolt.Bucket) (ue error) {
seq, ue = bkt.NextSequence()
return
})
return seq, err
}
func (l *Lithium) open() (err error) {
if l.bolt, err = bbolt.Open(l.path, l.mode, &bbolt.Options{Timeout: l.timeout}); err != nil {
return
}
err = l.bolt.Update(func(tx *bbolt.Tx) error {
_, ce := tx.CreateBucketIfNotExists(l.RootBucketKey)
return ce
})
return
}
func (l *Lithium) close() error {
return l.bolt.Close()
}
func (l *Lithium) view(fn func(*bbolt.Bucket) error) error {
return l.bolt.Update(func(tx *bbolt.Tx) error {
bkt, err := l.getBucket(tx, l.RootBucketKey)
if err != nil {
return err
}
return fn(bkt)
})
}
func (l *Lithium) update(fn func(*bbolt.Bucket) error) error {
return l.bolt.Update(func(tx *bbolt.Tx) error {
bkt, err := l.getBucket(tx, l.RootBucketKey)
if err != nil {
return err
}
return fn(bkt)
})
}
func (l *Lithium) getBucket(tx *bbolt.Tx, key []byte) (bkt *bbolt.Bucket, err error) {
bkt = tx.Bucket(l.RootBucketKey)
if bkt == nil {
err = errors.Wrapf(types.ErrInvalidWALBucket, "%+v", key)
}
return
}
// LithiumScanEntry indicates an entry of scanning.
type LithiumScanEntry struct {
err error
key []byte
value []byte
}
// Pair means a pair of key/value.
func (e LithiumScanEntry) Pair() ([]byte, []byte) {
return e.key, e.value
}
// Error .
func (e LithiumScanEntry) Error() error {
return e.err
}