-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboltq.go
355 lines (311 loc) · 7.14 KB
/
boltq.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// boltq is a very simple boltdb based embedded queue
//
// ```
// package main
//
// import "github.com/oliveagle/boltq"
//
// func main() {
// q, err := boltq.NewBoltQ("test_q.queue", 1000, boltq.ERROR_ON_FULL)
// defer q.Close()
//
// q.Enqueue([]byte("value"))
// value, _ := q.Dequeue([]byte("value"))
// }
// ```
//
// more infomation: http://github.com/oliveagle/boltq
package boltq
import (
"fmt"
"github.com/boltdb/bolt"
// "log"
"strconv"
"sync"
"time"
)
const (
ERROR_ON_FULL = iota // raise an error if the queue reached `max_queue_size`
POP_ON_FULL // popout oldest item if queue size above `max_queue_size`
)
const (
BUCKET_STATS = "~~stats~~"
BUCKET_QUEUE = "queue"
KEY_TOTALITEM = "TotalItemCount"
)
type BoltQ struct {
max_queue_size int64
filename string
db *bolt.DB
onfull int
total_item int64
last_nanosec int64
last_collision int
mutex sync.RWMutex
keylock sync.RWMutex
}
func NewBoltQ(filename string, max_queue_size int64, onfull int) (*BoltQ, error) {
db, err := bolt.Open(filename, 0660, nil)
if err != nil {
return nil, err
}
if onfull > POP_ON_FULL || onfull < ERROR_ON_FULL {
return nil, fmt.Errorf("onful can only be: ERROR_ON_FULL, POP_ON_FULL")
}
total_item, err := getTotalItemFromDB(db)
if err != nil {
return nil, err
}
return &BoltQ{
max_queue_size: max_queue_size,
filename: filename,
db: db,
onfull: onfull,
total_item: total_item,
}, nil
}
func getTotalItemFromDB(db *bolt.DB) (int64, error) {
total_item := int64(0)
err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(BUCKET_STATS))
if b == nil {
return nil
}
v := b.Get([]byte(KEY_TOTALITEM))
if v == nil {
return nil
}
tmp, err := strconv.ParseInt(string(v), 10, 64)
if err != nil {
// log.Printf("ERROR: parse count %v\n", err)
return err
}
total_item = tmp
return nil
})
if err != nil {
return 0, err
}
return total_item, nil
}
func (b *BoltQ) Close() {
b.db.Close()
}
func (b *BoltQ) Size() int64 {
return b.total_item
}
func (b *BoltQ) IsFull() bool {
return b.total_item >= b.max_queue_size
}
func (b *BoltQ) GetMaxQueueSize() int64 {
return b.max_queue_size
}
func (b *BoltQ) SetMaxQueueSize(size int64) {
if size > 0 {
b.max_queue_size = size
}
}
func (b *BoltQ) Push(value []byte) (err error) {
return b.push(value)
}
func (b *BoltQ) Enqueue(value []byte) (err error) {
return b.push(value)
}
func (b *BoltQ) Dequeue() (value []byte, err error) {
return b.pop(true)
}
func (b *BoltQ) Pop() (value []byte, err error) {
return b.pop(false)
}
func (b *BoltQ) PopBottom() (value []byte, err error) {
return b.pop(true)
}
func (b *BoltQ) push(value []byte) (err error) {
b.mutex.Lock()
defer b.mutex.Unlock()
if !b.IsFull() {
// b.newKey()
// 1182 ns/op
key := b.newKey()
err = b.db.Update(func(tx *bolt.Tx) error {
// 260214 ns/op
// tx.CreateBucketIfNotExists([]byte(BUCKET_QUEUE))
// 270968 ns/op
bkt, err := tx.CreateBucketIfNotExists([]byte(BUCKET_QUEUE))
if err != nil {
return err
}
err = bkt.Put(key, value)
if err != nil {
return err
}
// 392482 ns/op
return b.increaseTotalItem(tx)
})
} else {
switch b.onfull {
case ERROR_ON_FULL:
err = fmt.Errorf("Queue is full on size: %d >= %d", b.Size(), b.max_queue_size)
// log.Println(err)
case POP_ON_FULL:
// log.Println("pop_on_full: not implemented")
// err = fmt.Errorf("pop_on_full: not implemented")
key := b.newKey()
err = b.db.Update(func(tx *bolt.Tx) error {
bkt, err := tx.CreateBucketIfNotExists([]byte(BUCKET_QUEUE))
if err != nil {
return err
}
// delete first one
c := bkt.Cursor()
k, _ := c.First()
err = bkt.Delete(k)
if err != nil {
return err
}
err = bkt.Put(key, value)
if err != nil {
return err
}
return err
})
default:
err = fmt.Errorf("onful can only be: ERROR_ON_FULL, POP_ON_FULL")
}
}
return err
}
func (b *BoltQ) pop(bottom bool) (value []byte, err error) {
// pop from bottom when bottom is true, otherwise pop from top.
b.mutex.Lock()
defer b.mutex.Unlock()
err = b.db.Update(func(tx *bolt.Tx) error {
bkt, err := tx.CreateBucketIfNotExists([]byte(BUCKET_QUEUE))
if err != nil {
return err
}
c := bkt.Cursor()
var k []byte
var v []byte
if bottom == false {
k, v = c.Last()
} else {
k, v = c.First()
}
// log.Printf("k: %v, v: %v\n", k, v)
if len(k) == 0 && len(v) == 0 {
return fmt.Errorf("Queue is empty")
}
err = bkt.Delete(k)
if err != nil {
return err
}
err = b.decreaseTotalItem(tx)
if err != nil {
return err
}
value = v
return nil
})
return value, err
}
func (b *BoltQ) PopMany(fn func([]byte) bool) error {
return b.popmany(false, fn)
}
func (b *BoltQ) PopManyBottom(fn func([]byte) bool) error {
return b.popmany(true, fn)
}
func (b *BoltQ) popmany(bottom bool, fn func([]byte) bool) error {
// pop from bottom when bottom is true, otherwise pop from top.
b.mutex.Lock()
defer b.mutex.Unlock()
looping := false
err := b.db.Update(func(tx *bolt.Tx) error {
bkt, err := tx.CreateBucketIfNotExists([]byte(BUCKET_QUEUE))
if err != nil {
return err
}
c := bkt.Cursor()
var k []byte
var v []byte
if bottom == false {
k, v = c.Last()
} else {
k, v = c.First()
}
loop:
// log.Printf("k: %v, v: %v\n", k, v)
if len(k) == 0 && len(v) == 0 {
if looping {
return nil
} else {
return fmt.Errorf("Queue is empty")
}
}
matched := fn(v)
if matched == false {
return nil
}
err = bkt.Delete(k)
if err != nil {
return err
}
err = b.decreaseTotalItem(tx)
if err != nil {
return err
}
if bottom == false {
k, v = c.Prev()
} else {
k, v = c.Next()
}
looping = true
goto loop
})
return err
}
func (b *BoltQ) newKey() []byte {
b.keylock.Lock()
defer b.keylock.Unlock()
now := time.Now().UnixNano()
if now == b.last_nanosec {
b.last_collision += 1
} else {
b.last_collision = 0
b.last_nanosec = now
}
// generate a unique new key
return []byte(fmt.Sprintf("%d%04d", b.last_nanosec, b.last_collision))
}
func (b *BoltQ) increaseTotalItem(tx *bolt.Tx) (err error) {
stats_bucket, err := tx.CreateBucketIfNotExists([]byte(BUCKET_STATS))
tmp_count := stats_bucket.Get([]byte(KEY_TOTALITEM))
if tmp_count != nil {
tmp, err := strconv.ParseInt(string(tmp_count), 10, 64)
if err != nil {
// log.Printf("ERROR: %v\n", err)
return err
}
b.total_item = tmp
}
// remove above codes won't boost performance ...
b.total_item += 1
err = stats_bucket.Put([]byte(KEY_TOTALITEM), []byte(fmt.Sprintf("%d", b.total_item)))
return
}
func (b *BoltQ) decreaseTotalItem(tx *bolt.Tx) (err error) {
stats_bucket, err := tx.CreateBucketIfNotExists([]byte(BUCKET_STATS))
tmp_count := stats_bucket.Get([]byte(KEY_TOTALITEM))
if tmp_count != nil {
tmp, err := strconv.ParseInt(string(tmp_count), 10, 64)
if err != nil {
// log.Printf("ERROR: %v\n", err)
return err
}
b.total_item = tmp
}
// remove above codes won't boost performance ...
b.total_item -= 1
err = stats_bucket.Put([]byte(KEY_TOTALITEM), []byte(fmt.Sprintf("%d", b.total_item)))
return
}