Debian Codegraph @

uniquequeue.go

raw symbols SCIP
1 // Copyright 2020 Andrew Thornton. All rights reserved.
2 // Use of this source code is governed by a MIT-style
3 // license that can be found in the LICENSE file.
4
5 package levelqueue
6
7 import (
8 "fmt"
9
10 "github.com/syndtr/goleveldb/leveldb"
11 "github.com/syndtr/goleveldb/leveldb/errors"
12 )
13
14 const (
15 uniqueQueuePrefixStr = "unique"
16 )
17
18 // UniqueQueue defines an unique queue struct
19 type UniqueQueue struct {
20 q *Queue
21 set *Set
22 db *leveldb.DB
23 closeUnderlyingDB bool
24 }
25
26 // OpenUnique opens an unique queue from the db path or creates a set if it doesn't exist.
27 // The keys in the queue portion will not be prefixed, and the set keys will be prefixed with "set-"
28 func OpenUnique(dataDir string) (*UniqueQueue, error) {
29 db, err := leveldb.OpenFile(dataDir, nil)
30 if err != nil {
31 if !errors.IsCorrupted(err) {
32 return nil, err
33 }
34 db, err = leveldb.RecoverFile(dataDir, nil)
35 if err != nil {
36 return nil, err
37 }
38 }
39 return NewUniqueQueue(db, []byte{}, []byte(uniqueQueuePrefixStr), true)
40 }
41
42 // NewUniqueQueue creates a new unique queue from a db.
43 // The queue keys will be prefixed with queuePrefix and the set keys with setPrefix
44 // and at close the db will be closed as per closeUnderlyingDB
45 func NewUniqueQueue(db *leveldb.DB, queuePrefix []byte, setPrefix []byte, closeUnderlyingDB bool) (*UniqueQueue, error) {
46 internal, err := NewQueue(db, queuePrefix, false)
47 if err != nil {
48 return nil, err
49 }
50 set, err := NewSet(db, setPrefix, false)
51 if err != nil {
52 return nil, err
53 }
54 queue := &UniqueQueue{
55 q: internal,
56 set: set,
57 db: db,
58 closeUnderlyingDB: closeUnderlyingDB,
59 }
60
61 return queue, err
62 }
63
64 // LPush pushes data to the left of the queue
65 func (queue *UniqueQueue) LPush(data []byte) error {
66 return queue.LPushFunc(data, nil)
67 }
68
69 // LPushFunc pushes data to the left of the queue and calls the callback if it is added
70 func (queue *UniqueQueue) LPushFunc(data []byte, fn func() error) error {
71 added, err := queue.set.Add(data)
72 if err != nil {
73 return err
74 }
75 if !added {
76 return ErrAlreadyInQueue
77 }
78
79 if fn != nil {
80 err = fn()
81 if err != nil {
82 _, remErr := queue.set.Remove(data)
83 if remErr != nil {
84 return fmt.Errorf("%v & %v", err, remErr)
85 }
86 return err
87 }
88 }
89
90 return queue.q.LPush(data)
91 }
92
93 // RPush pushes data to the right of the queue
94 func (queue *UniqueQueue) RPush(data []byte) error {
95 return queue.RPushFunc(data, nil)
96 }
97
98 // RPushFunc pushes data to the right of the queue and calls the callback if is added
99 func (queue *UniqueQueue) RPushFunc(data []byte, fn func() error) error {
100 added, err := queue.set.Add(data)
101 if err != nil {
102 return err
103 }
104 if !added {
105 return ErrAlreadyInQueue
106 }
107
108 if fn != nil {
109 err = fn()
110 if err != nil {
111 _, remErr := queue.set.Remove(data)
112 if remErr != nil {
113 return fmt.Errorf("%v & %v", err, remErr)
114 }
115 return err
116 }
117 }
118
119 return queue.q.RPush(data)
120 }
121
122 // RPop pop data from the right of the queue
123 func (queue *UniqueQueue) RPop() ([]byte, error) {
124 popped, err := queue.q.RPop()
125 if err != nil {
126 return popped, err
127 }
128 _, err = queue.set.Remove(popped)
129
130 return popped, err
131 }
132
133 // 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.
134 func (queue *UniqueQueue) RHandle(h func([]byte) error) error {
135 return queue.q.RHandle(func(data []byte) error {
136 err := h(data)
137 if err != nil {
138 return err
139 }
140 _, err = queue.set.Remove(data)
141 return err
142 })
143 }
144
145 // LPop pops data from left of the queue
146 func (queue *UniqueQueue) LPop() ([]byte, error) {
147 popped, err := queue.q.LPop()
148 if err != nil {
149 return popped, err
150 }
151 _, err = queue.set.Remove(popped)
152
153 return popped, err
154 }
155
156 // 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.
157 func (queue *UniqueQueue) LHandle(h func([]byte) error) error {
158 return queue.q.LHandle(func(data []byte) error {
159 err := h(data)
160 if err != nil {
161 return err
162 }
163 _, err = queue.set.Remove(data)
164 return err
165 })
166 }
167
168 // Has checks whether the data is already in the queue
169 func (queue *UniqueQueue) Has(data []byte) (bool, error) {
170 return queue.set.Has(data)
171 }
172
173 // Len returns the length of the queue
174 func (queue *UniqueQueue) Len() int64 {
175 queue.set.lock.Lock()
176 defer queue.set.lock.Unlock()
177 return queue.q.Len()
178 }
179
180 // Close closes the queue (and the underlying DB if set to closeUnderlyingDB)
181 func (queue *UniqueQueue) Close() error {
182 _ = queue.q.Close()
183 _ = queue.set.Close()
184 queue.set.lock.Lock()
185 defer queue.set.lock.Unlock()
186 if !queue.closeUnderlyingDB || queue.db == nil {
187 queue.db = nil
188 return nil
189 }
190 err := queue.db.Close()
191 queue.db = nil
192 return err
193 }