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
|
// Copyright 2020 Andrew Thornton. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package levelqueue
import (
"fmt"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/errors"
)
const (
uniqueQueuePrefixStr = "unique"
)
// UniqueQueue defines an unique queue struct
type UniqueQueue struct {
q *Queue
set *Set
db *leveldb.DB
closeUnderlyingDB bool
}
// OpenUnique opens an unique queue from the db path or creates a set if it doesn't exist.
// The keys in the queue portion will not be prefixed, and the set keys will be prefixed with "set-"
func OpenUnique(dataDir string) (*UniqueQueue, error) {
db, err := leveldb.OpenFile(dataDir, nil)
if err != nil {
if !errors.IsCorrupted(err) {
return nil, err
}
db, err = leveldb.RecoverFile(dataDir, nil)
if err != nil {
return nil, err
}
}
return NewUniqueQueue(db, []byte{}, []byte(uniqueQueuePrefixStr), true)
}
// NewUniqueQueue creates a new unique queue from a db.
// The queue keys will be prefixed with queuePrefix and the set keys with setPrefix
// and at close the db will be closed as per closeUnderlyingDB
func NewUniqueQueue(db *leveldb.DB, queuePrefix []byte, setPrefix []byte, closeUnderlyingDB bool) (*UniqueQueue, error) {
internal, err := NewQueue(db, queuePrefix, false)
if err != nil {
return nil, err
}
set, err := NewSet(db, setPrefix, false)
if err != nil {
return nil, err
}
queue := &UniqueQueue{
q: internal,
set: set,
db: db,
closeUnderlyingDB: closeUnderlyingDB,
}
return queue, err
}
// LPush pushes data to the left of the queue
func (queue *UniqueQueue) LPush(data []byte) error {
return queue.LPushFunc(data, nil)
}
// LPushFunc pushes data to the left of the queue and calls the callback if it is added
func (queue *UniqueQueue) LPushFunc(data []byte, fn func() error) error {
added, err := queue.set.Add(data)
if err != nil {
return err
}
if !added {
return ErrAlreadyInQueue
}
if fn != nil {
err = fn()
if err != nil {
_, remErr := queue.set.Remove(data)
if remErr != nil {
return fmt.Errorf("%v & %v", err, remErr)
}
return err
}
}
return queue.q.LPush(data)
}
// RPush pushes data to the right of the queue
func (queue *UniqueQueue) RPush(data []byte) error {
return queue.RPushFunc(data, nil)
}
// RPushFunc pushes data to the right of the queue and calls the callback if is added
func (queue *UniqueQueue) RPushFunc(data []byte, fn func() error) error {
added, err := queue.set.Add(data)
if err != nil {
return err
}
if !added {
return ErrAlreadyInQueue
}
if fn != nil {
err = fn()
if err != nil {
_, remErr := queue.set.Remove(data)
if remErr != nil {
return fmt.Errorf("%v & %v", err, remErr)
}
return err
}
}
return queue.q.RPush(data)
}
// RPop pop data from the right of the queue
func (queue *UniqueQueue) RPop() ([]byte, error) {
popped, err := queue.q.RPop()
if err != nil {
return popped, err
}
_, err = queue.set.Remove(popped)
return popped, err
}
// RHandle receives a user callback function to handle the right element of the queue, if the function returns nil, then delete the element, otherwise keep the element.
func (queue *UniqueQueue) RHandle(h func([]byte) error) error {
return queue.q.RHandle(func(data []byte) error {
err := h(data)
if err != nil {
return err
}
_, err = queue.set.Remove(data)
return err
})
}
// LPop pops data from left of the queue
func (queue *UniqueQueue) LPop() ([]byte, error) {
popped, err := queue.q.LPop()
if err != nil {
return popped, err
}
_, err = queue.set.Remove(popped)
return popped, err
}
// LHandle receives a user callback function to handle the left element of the queue, if the function returns nil, then delete the element, otherwise keep the element.
func (queue *UniqueQueue) LHandle(h func([]byte) error) error {
return queue.q.LHandle(func(data []byte) error {
err := h(data)
if err != nil {
return err
}
_, err = queue.set.Remove(data)
return err
})
}
// Has checks whether the data is already in the queue
func (queue *UniqueQueue) Has(data []byte) (bool, error) {
return queue.set.Has(data)
}
// Len returns the length of the queue
func (queue *UniqueQueue) Len() int64 {
queue.set.lock.Lock()
defer queue.set.lock.Unlock()
return queue.q.Len()
}
// Close closes the queue (and the underlying DB if set to closeUnderlyingDB)
func (queue *UniqueQueue) Close() error {
_ = queue.q.Close()
_ = queue.set.Close()
queue.set.lock.Lock()
defer queue.set.lock.Unlock()
if !queue.closeUnderlyingDB {
queue.db = nil
return nil
}
err := queue.db.Close()
queue.db = nil
return err
}
|