-
Notifications
You must be signed in to change notification settings - Fork 24
/
filefanoutqueue.go
356 lines (280 loc) · 7.18 KB
/
filefanoutqueue.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
356
package bigqueue
import (
"errors"
"os"
"strconv"
"sync"
)
const (
// FanoutFrontFileName Fanout FrontFileName file name
FanoutFrontFileName = "front_index_"
)
// FileFanoutQueue file fanout queue implements
type FileFanoutQueue struct {
fileQueue *FileQueue
// queue options
options *Options
// front index by fanout id
frontIndexMap map[int64]*QueueFront
queueGetLock sync.Mutex
path string
opened bool
}
// QueueFront queue front struct
type QueueFront struct {
// fanout id
fanoutID int64
// front index of the big queue,
frontIndex int64
fanoutDatafile *DB
// locks for queue front write management
queueFrontWriteLock sync.Mutex
subscribLock sync.Mutex
subscriber func(int64, []byte, error)
}
// Open the queue files
func (q *FileFanoutQueue) Open(dir string, queueName string, options *Options) error {
if !q.opened {
q.opened = true
} else {
return errors.New("FileFanoutQueue already opened")
}
q.fileQueue = &FileQueue{}
if options == nil {
options = DefaultOptions
}
err := q.fileQueue.Open(dir, queueName, options)
if err != nil {
return err
}
q.options = options
q.path = dir + "/" + queueName
q.frontIndexMap = make(map[int64]*QueueFront)
q.fileQueue.Subscribe(q.doSubscribe)
return nil
}
// Status get status info from current queue
func (q *FileFanoutQueue) Status(fanoutID int64) *QueueFilesStatus {
qf, err := q.getQueueFront(fanoutID)
if err != nil {
return nil
}
queueFilesStatus := q.fileQueue.status(qf.frontIndex, -1, -1)
if queueFilesStatus == nil {
return nil
}
return queueFilesStatus
}
// IsEmpty test if target fanoutID is empty
func (q *FileFanoutQueue) IsEmpty(fanoutID int64) bool {
qf, err := q.getQueueFront(fanoutID)
if err != nil {
return true
}
return q.fileQueue.isEmpty(qf.frontIndex)
}
// Size return item size with target fanoutID
func (q *FileFanoutQueue) Size(fanoutID int64) int64 {
qf, err := q.getQueueFront(fanoutID)
if err != nil {
return -1
}
return q.fileQueue.size(qf.frontIndex)
}
// Close free the resource
func (q *FileFanoutQueue) Close() {
// close file queue
q.fileQueue.Close()
for _, v := range q.frontIndexMap {
if v.fanoutDatafile != nil {
v.fanoutDatafile.Close()
}
v.fanoutDatafile = nil
}
q.opened = false
}
// Enqueue Append an item to the queue and return index no
func (q *FileFanoutQueue) Enqueue(data []byte) (int64, error) {
return q.fileQueue.Enqueue(data)
}
// Dequeue dequeue data from target fanoutID
func (q *FileFanoutQueue) Dequeue(fanoutID int64) (int64, []byte, error) {
qf, err := q.getQueueFront(fanoutID)
if err != nil {
return -1, nil, err
}
if q.IsEmpty(fanoutID) {
return -1, nil, nil
}
index, err := qf.updateQueueFrontIndex(1)
if err != nil {
return -1, nil, err
}
data, err := q.fileQueue.peek(index)
if err != nil {
return -1, nil, err
}
return index, data, nil
}
// Peek peek the head item from target fanoutID
func (q *FileFanoutQueue) Peek(fanoutID int64) (int64, []byte, error) {
qf, err := q.getQueueFront(fanoutID)
if err != nil {
return -1, nil, err
}
index := qf.frontIndex
data, err := q.fileQueue.peek(index)
if err != nil {
return -1, nil, err
}
return index, data, nil
}
// // PeekAll Retrieves all the items from the front of a queue
// return array of data and array of index
func (q *FileFanoutQueue) PeekAll(fanoutID int64) ([][]byte, []int64, error) {
qf, err := q.getQueueFront(fanoutID)
if err != nil {
return nil, nil, err
}
index := qf.frontIndex
return q.fileQueue.peekAll(index, q.Size(fanoutID))
}
// PeekPagination to peek data from queue by paing feature.
func (q *FileFanoutQueue) PeekPagination(fanoutID int64, page, pagesize uint64) ([][]byte, []int64, error) {
qf, err := q.getQueueFront(fanoutID)
if err != nil {
return nil, nil, err
}
index := qf.frontIndex
return q.fileQueue.peekPagination(index, q.fileQueue.size(index), page, pagesize)
}
// Skip To skip deqeue target number of items
func (q *FileFanoutQueue) Skip(fanoutID int64, count int64) error {
if count <= 0 {
// do nothing
return nil
}
qf, err := q.getQueueFront(fanoutID)
if err != nil {
return err
}
_, err = qf.updateQueueFrontIndex(count)
if err != nil {
return err
}
return nil
}
// Subscribe do async subscribe by target fanout id
func (q *FileFanoutQueue) Subscribe(fanoutID int64, fn func(int64, []byte, error)) error {
if fn == nil {
return errors.New("parameter 'fn' is nil")
}
qf, err := q.getQueueFront(fanoutID)
if err != nil {
return err
}
if qf.subscriber != nil {
return ErrSubscribeExistErr
}
// do subsrcibe by async way
go func() {
q.doLoopSubscribe(fanoutID, fn)
qf.subscriber = fn
}()
return nil
}
// FreeSubscribe to free subscriber by target fanout id
func (q *FileFanoutQueue) FreeSubscribe(fanoutID int64) {
qf, err := q.getQueueFront(fanoutID)
if err != nil {
return
}
qf.subscriber = nil
}
// FreeAllSubscribe to free all subscriber
func (q *FileFanoutQueue) FreeAllSubscribe() {
for _, qf := range q.frontIndexMap {
qf.subscriber = nil
}
}
func (q *FileFanoutQueue) getQueueFront(fanoutID int64) (*QueueFront, error) {
q.queueGetLock.Lock()
defer q.queueGetLock.Unlock()
qf := q.frontIndexMap[fanoutID]
if qf != nil {
return qf, nil
}
qf = &QueueFront{
fanoutID: fanoutID,
frontIndex: 0,
}
err := qf.open(q.path)
if err != nil {
return nil, err
}
q.frontIndexMap[fanoutID] = qf
return qf, nil
}
func (q *QueueFront) open(path string) error {
frontFilePath := path + "/" + FanoutFrontFileName + strconv.Itoa(int(q.fanoutID)) + "/"
err := os.MkdirAll(frontFilePath, os.ModeDir)
if err != nil {
return err
}
// create index file
q.fanoutDatafile = &DB{
path: frontFilePath + GetFileName(filePrefix, fileSuffix, q.fanoutID),
InitialMmapSize: defaultFrontPageSize,
opened: true,
}
err = q.fanoutDatafile.Open(defaultFileMode)
if err != nil {
return err
}
q.frontIndex = BytesToInt(q.fanoutDatafile.data[:defaultFrontPageSize])
Assert(q.frontIndex >= 0, "front index can not be a negetive number. value is %v", q.frontIndex)
return nil
}
func (q *QueueFront) updateQueueFrontIndex(count int64) (int64, error) {
q.queueFrontWriteLock.Lock()
defer q.queueFrontWriteLock.Unlock()
queueFrontIndex := q.frontIndex
nextQueueFrontIndex := queueFrontIndex
if nextQueueFrontIndex == MaxInt64 {
nextQueueFrontIndex = 0
} else {
nextQueueFrontIndex += count
}
if nextQueueFrontIndex < 0 {
// if overflow then reset to zero
nextQueueFrontIndex = 0
}
q.frontIndex = nextQueueFrontIndex
bb := IntToBytes(q.frontIndex)
for idx, b := range bb {
q.fanoutDatafile.data[idx] = b
}
return queueFrontIndex, nil
}
func (q *FileFanoutQueue) doSubscribe(index int64, data []byte, err error) {
for fanoutID, v := range q.frontIndexMap {
if v.subscriber != nil {
v.subscribLock.Lock()
defer v.subscribLock.Unlock()
// here should be care something about blocked callback subscriber
q.doLoopSubscribe(fanoutID, v.subscriber)
}
}
}
func (q *FileFanoutQueue) doLoopSubscribe(fanoutID int64, subscriber func(int64, []byte, error)) {
for {
if subscriber == nil {
return
}
index, bb, err := q.Dequeue(fanoutID)
if bb == nil || len(bb) == 0 {
break // queue is empty
}
subscriber(index, bb, err)
}
}