Skip to content

Commit 4cfcf97

Browse files
committed
Allow WriteOptions to be given if desired.
1 parent d618510 commit 4cfcf97

File tree

5 files changed

+34
-20
lines changed

5 files changed

+34
-20
lines changed

options.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package goque
2+
3+
import "github.com/syndtr/goleveldb/leveldb/opt"
4+
5+
func getOpts(o []*opt.WriteOptions) *opt.WriteOptions {
6+
if len(o) == 1 {
7+
return o[1]
8+
}
9+
return nil
10+
}

prefix_queue.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/syndtr/goleveldb/leveldb"
1212
"github.com/syndtr/goleveldb/leveldb/errors"
13+
"github.com/syndtr/goleveldb/leveldb/opt"
1314
)
1415

1516
// prefixDelimiter defines the delimiter used to separate a prefix from an
@@ -71,7 +72,7 @@ func OpenPrefixQueue(dataDir string) (*PrefixQueue, error) {
7172
}
7273

7374
// Enqueue adds an item to the queue.
74-
func (pq *PrefixQueue) Enqueue(prefix, value []byte) (*Item, error) {
75+
func (pq *PrefixQueue) Enqueue(prefix, value []byte, opts ...*opt.WriteOptions) (*Item, error) {
7576
pq.Lock()
7677
defer pq.Unlock()
7778

@@ -94,7 +95,7 @@ func (pq *PrefixQueue) Enqueue(prefix, value []byte) (*Item, error) {
9495
}
9596

9697
// Add it to the queue.
97-
if err := pq.db.Put(item.Key, item.Value, nil); err != nil {
98+
if err := pq.db.Put(item.Key, item.Value, getOpts(opts)); err != nil {
9899
return nil, err
99100
}
100101

@@ -248,7 +249,7 @@ func (pq *PrefixQueue) PeekByIDString(prefix string, id uint64) (*Item, error) {
248249
}
249250

250251
// Update updates an item in the given queue without changing its position.
251-
func (pq *PrefixQueue) Update(prefix []byte, id uint64, newValue []byte) (*Item, error) {
252+
func (pq *PrefixQueue) Update(prefix []byte, id uint64, newValue []byte, opts ...*opt.WriteOptions) (*Item, error) {
252253
pq.Lock()
253254
defer pq.Unlock()
254255

@@ -276,7 +277,7 @@ func (pq *PrefixQueue) Update(prefix []byte, id uint64, newValue []byte) (*Item,
276277
}
277278

278279
// Update this item in the queue.
279-
if err := pq.db.Put(item.Key, item.Value, nil); err != nil {
280+
if err := pq.db.Put(item.Key, item.Value, getOpts(opts)); err != nil {
280281
return nil, err
281282
}
282283

@@ -392,7 +393,7 @@ func (pq *PrefixQueue) getOrCreateQueue(prefix []byte) (*queue, error) {
392393
}
393394

394395
// savePrefixQueue saves the given queue for the given prefix.
395-
func (pq *PrefixQueue) saveQueue(prefix []byte, q *queue) error {
396+
func (pq *PrefixQueue) saveQueue(prefix []byte, q *queue, opts ...*opt.WriteOptions) error {
396397
// Encode the queue using gob.
397398
var buffer bytes.Buffer
398399
enc := gob.NewEncoder(&buffer)
@@ -401,14 +402,14 @@ func (pq *PrefixQueue) saveQueue(prefix []byte, q *queue) error {
401402
}
402403

403404
// Save it to the database.
404-
return pq.db.Put(generateKeyPrefixData(prefix), buffer.Bytes(), nil)
405+
return pq.db.Put(generateKeyPrefixData(prefix), buffer.Bytes(), getOpts(opts))
405406
}
406407

407408
// save saves the main prefix queue data.
408-
func (pq *PrefixQueue) save() error {
409+
func (pq *PrefixQueue) save(opts ...*opt.WriteOptions) error {
409410
val := make([]byte, 8)
410411
binary.BigEndian.PutUint64(val, pq.size)
411-
return pq.db.Put(pq.getDataKey(), val, nil)
412+
return pq.db.Put(pq.getDataKey(), val, getOpts(opts))
412413
}
413414

414415
// getDataKey generates the main prefix queue data key.

priority_queue.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sync"
99

1010
"github.com/syndtr/goleveldb/leveldb"
11+
"github.com/syndtr/goleveldb/leveldb/opt"
1112
"github.com/syndtr/goleveldb/leveldb/util"
1213
)
1314

@@ -82,7 +83,7 @@ func OpenPriorityQueue(dataDir string, order order) (*PriorityQueue, error) {
8283
}
8384

8485
// Enqueue adds an item to the priority queue.
85-
func (pq *PriorityQueue) Enqueue(priority uint8, value []byte) (*PriorityItem, error) {
86+
func (pq *PriorityQueue) Enqueue(priority uint8, value []byte, opts ...*opt.WriteOptions) (*PriorityItem, error) {
8687
pq.Lock()
8788
defer pq.Unlock()
8889

@@ -103,7 +104,7 @@ func (pq *PriorityQueue) Enqueue(priority uint8, value []byte) (*PriorityItem, e
103104
}
104105

105106
// Add it to the priority queue.
106-
if err := pq.db.Put(item.Key, item.Value, nil); err != nil {
107+
if err := pq.db.Put(item.Key, item.Value, getOpts(opts)); err != nil {
107108
return nil, err
108109
}
109110

@@ -264,7 +265,7 @@ func (pq *PriorityQueue) PeekByPriorityID(priority uint8, id uint64) (*PriorityI
264265

265266
// Update updates an item in the priority queue without changing its
266267
// position.
267-
func (pq *PriorityQueue) Update(priority uint8, id uint64, newValue []byte) (*PriorityItem, error) {
268+
func (pq *PriorityQueue) Update(priority uint8, id uint64, newValue []byte, opts ...*opt.WriteOptions) (*PriorityItem, error) {
268269
pq.Lock()
269270
defer pq.Unlock()
270271

@@ -287,7 +288,7 @@ func (pq *PriorityQueue) Update(priority uint8, id uint64, newValue []byte) (*Pr
287288
}
288289

289290
// Update this item in the queue.
290-
if err := pq.db.Put(item.Key, item.Value, nil); err != nil {
291+
if err := pq.db.Put(item.Key, item.Value, getOpts(opts)); err != nil {
291292
return nil, err
292293
}
293294

queue.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sync"
99

1010
"github.com/syndtr/goleveldb/leveldb"
11+
"github.com/syndtr/goleveldb/leveldb/opt"
1112
)
1213

1314
// Queue is a standard FIFO (first in, first out) queue.
@@ -55,7 +56,7 @@ func OpenQueue(dataDir string) (*Queue, error) {
5556
}
5657

5758
// Enqueue adds an item to the queue.
58-
func (q *Queue) Enqueue(value []byte) (*Item, error) {
59+
func (q *Queue) Enqueue(value []byte, opts ...*opt.WriteOptions) (*Item, error) {
5960
q.Lock()
6061
defer q.Unlock()
6162

@@ -72,7 +73,7 @@ func (q *Queue) Enqueue(value []byte) (*Item, error) {
7273
}
7374

7475
// Add it to the queue.
75-
if err := q.db.Put(item.Key, item.Value, nil); err != nil {
76+
if err := q.db.Put(item.Key, item.Value, getOpts(opts)); err != nil {
7677
return nil, err
7778
}
7879

@@ -188,7 +189,7 @@ func (q *Queue) PeekByID(id uint64) (*Item, error) {
188189
}
189190

190191
// Update updates an item in the queue without changing its position.
191-
func (q *Queue) Update(id uint64, newValue []byte) (*Item, error) {
192+
func (q *Queue) Update(id uint64, newValue []byte, opts ...*opt.WriteOptions) (*Item, error) {
192193
q.Lock()
193194
defer q.Unlock()
194195

@@ -210,7 +211,7 @@ func (q *Queue) Update(id uint64, newValue []byte) (*Item, error) {
210211
}
211212

212213
// Update this item in the queue.
213-
if err := q.db.Put(item.Key, item.Value, nil); err != nil {
214+
if err := q.db.Put(item.Key, item.Value, getOpts(opts)); err != nil {
214215
return nil, err
215216
}
216217

stack.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sync"
99

1010
"github.com/syndtr/goleveldb/leveldb"
11+
"github.com/syndtr/goleveldb/leveldb/opt"
1112
)
1213

1314
// Stack is a standard LIFO (last in, first out) stack.
@@ -55,7 +56,7 @@ func OpenStack(dataDir string) (*Stack, error) {
5556
}
5657

5758
// Push adds an item to the stack.
58-
func (s *Stack) Push(value []byte) (*Item, error) {
59+
func (s *Stack) Push(value []byte, opts ...*opt.WriteOptions) (*Item, error) {
5960
s.Lock()
6061
defer s.Unlock()
6162

@@ -72,7 +73,7 @@ func (s *Stack) Push(value []byte) (*Item, error) {
7273
}
7374

7475
// Add it to the stack.
75-
if err := s.db.Put(item.Key, item.Value, nil); err != nil {
76+
if err := s.db.Put(item.Key, item.Value, getOpts(opts)); err != nil {
7677
return nil, err
7778
}
7879

@@ -188,7 +189,7 @@ func (s *Stack) PeekByID(id uint64) (*Item, error) {
188189
}
189190

190191
// Update updates an item in the stack without changing its position.
191-
func (s *Stack) Update(id uint64, newValue []byte) (*Item, error) {
192+
func (s *Stack) Update(id uint64, newValue []byte, opts ...*opt.WriteOptions) (*Item, error) {
192193
s.Lock()
193194
defer s.Unlock()
194195

@@ -210,7 +211,7 @@ func (s *Stack) Update(id uint64, newValue []byte) (*Item, error) {
210211
}
211212

212213
// Update this item in the stack.
213-
if err := s.db.Put(item.Key, item.Value, nil); err != nil {
214+
if err := s.db.Put(item.Key, item.Value, getOpts(opts)); err != nil {
214215
return nil, err
215216
}
216217

0 commit comments

Comments
 (0)