-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.go
207 lines (186 loc) · 3.96 KB
/
queue.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
// Persistent queue library designed for single process thread-safe job queue
// where data loss is not an option at cost of speed.
package spq
import (
"encoding"
"sync"
"github.com/juju/errors"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/storage"
"github.com/syndtr/goleveldb/leveldb/util"
)
const OnlyForTesting = "\x00"
type Queue struct { //nolint:maligned
db *leveldb.DB
dbROpt opt.ReadOptions
dbWOpt opt.WriteOptions
dbRangeAll *util.Range
mu sync.RWMutex
closed bool
next uint64
readch chan struct{}
stopch chan struct{}
rdonech chan struct{}
}
func Open(path string) (*Queue, error) {
q := &Queue{
readch: make(chan struct{}, 1),
stopch: make(chan struct{}),
rdonech: make(chan struct{}),
dbROpt: opt.ReadOptions{},
dbWOpt: opt.WriteOptions{
NoWriteMerge: true,
Sync: true,
},
dbRangeAll: &util.Range{
Start: itemKeyPrefix[:],
Limit: itemKeyLimit[:],
},
}
err := q.load(path)
return q, err
}
func (q *Queue) load(path string) error {
opt := &opt.Options{
BlockCacheCapacity: -1,
BlockRestartInterval: 1, // checksum each key, if I understood doc correctly
BlockSize: 1 << 10,
DisableBlockCache: true,
NoSync: false,
NoWriteMerge: true,
Strict: opt.StrictJournalChecksum | opt.StrictBlockChecksum,
WriteBuffer: 4 << 10,
}
var err error
if path == OnlyForTesting {
q.db, err = leveldb.Open(storage.NewMemStorage(), opt)
} else {
q.db, err = leveldb.RecoverFile(path, opt)
// q.db, err = leveldb.OpenFile(path, opt)
}
if err != nil {
return errors.Annotate(err, "leveldb open")
}
err = q.db.CompactRange(util.Range{})
if err != nil {
return errors.Annotate(err, "leveldb compact")
}
iter := q.db.NewIterator(q.dbRangeAll, &q.dbROpt)
defer iter.Release()
if iter.Last() {
q.next, err = unkey(iter.Key())
if err != nil {
return errors.Annotatef(err, "spq load key=%x", iter.Key())
}
}
q.next++
return nil
}
func (q *Queue) Close() error {
var err error
q.mu.Lock()
if !q.closed {
close(q.stopch)
err = q.db.Close()
q.closed = true
}
q.mu.Unlock()
return err
}
func (q *Queue) MarshalPush(item encoding.BinaryMarshaler) error {
b, err := item.MarshalBinary()
if err != nil {
return err
}
return q.Push(b)
}
func (q *Queue) Push(value []byte) error {
var key [keyLen]byte
q.mu.Lock()
defer q.mu.Unlock()
if q.closed {
return ErrClosed
}
encodeKey(key[:], q.next)
err := q.db.Put(key[:], value, &q.dbWOpt)
if err != nil {
return err
}
q.next++
signal(q.readch)
return nil
}
func (q *Queue) Peek() (Box, error) {
var box Box
for {
q.mu.RLock()
if !q.closed {
box = q.dbReadFirst()
} else {
box = Box{err: ErrClosed}
}
q.mu.RUnlock()
if !box.empty() {
return box, box.err
}
select {
case <-q.readch: // success path
case <-q.stopch:
return Box{}, ErrClosed
}
}
}
func (q *Queue) Delete(box Box) error {
if _, err := unkey(box.key[:]); err != nil {
return err
}
q.mu.Lock()
defer q.mu.Unlock()
if q.closed {
return ErrClosed
}
return q.db.Delete(box.key[:], &q.dbWOpt)
}
// Atomic Delete+Push
func (q *Queue) DeletePush(box Box) error {
if _, err := unkey(box.key[:]); err != nil {
return err
}
q.mu.Lock()
defer q.mu.Unlock()
if q.closed {
return ErrClosed
}
var newKey [keyLen]byte
encodeKey(newKey[:], q.next)
b := leveldb.Batch{}
b.Delete(box.key[:])
b.Put(newKey[:], box.value)
err := q.db.Write(&b, &q.dbWOpt)
if err != nil {
return err
}
q.next++
signal(q.readch)
return nil
}
func (q *Queue) dbReadFirst() Box {
iter := q.db.NewIterator(q.dbRangeAll, &q.dbROpt)
defer iter.Release()
if !iter.First() {
return Box{err: iter.Error()}
}
k := iter.Key()
v := iter.Value()
box := Box{value: make([]byte, len(v))}
copy(box.key[:], k)
copy(box.value, v)
return box
}
func signal(ch chan struct{}) {
select {
case ch <- struct{}{}:
default:
}
}