-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeque.go
70 lines (61 loc) · 2.12 KB
/
deque.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
package boltx
import (
"encoding"
"github.com/boltdb/bolt"
)
// Deque defines a double-ended queue on a bucket. It's persistent and safe to use with
// multiple goroutines.
//
// deque := boltx.NewDeque(db, []byte("deque-test"))
//
// go func () {
// deque.EnqueueModelBack(&model{"item"})
// }()
//
// model := &model{}
// deque.DequeueModelFront(model)
//
// log.Println(model)
type Deque struct {
db *bolt.DB
name []byte
session *Session
}
// NewDeque initializes a deque in the bucket with the provided name.
func NewDeque(db *bolt.DB, name []byte) *Deque {
return &Deque{
db: db,
name: name,
session: NewSession(db),
}
}
// EnqueueModelFront puts the provided model to the front of the deque.
func (d *Deque) EnqueueModelFront(model encoding.BinaryMarshaler) error {
return d.session.Update(func(tx *bolt.Tx) error {
return PushModelAndSignal(tx, d.name, PositionFront, model, DefaultUint64DequeKey, d.session)
})
}
// EnqueueModelBack puts the provided model to the back of the deque.
func (d *Deque) EnqueueModelBack(model encoding.BinaryMarshaler) error {
return d.session.Update(func(tx *bolt.Tx) error {
return PushModelAndSignal(tx, d.name, PositionBack, model, DefaultUint64DequeKey, d.session)
})
}
// DequeueModelFront gets the value from the front of the deque, unmarshals it into the provided
// model and removes it. If the deque is empty the call blocks until an element is enqueued.
func (d *Deque) DequeueModelFront(model encoding.BinaryUnmarshaler) error {
return d.session.Update(func(tx *bolt.Tx) error {
return PopModelOrWait(tx, d.name, PositionFront, model, d.session)
})
}
// DequeueModelBack gets the value from the back of the deque, unmarshals it into the provided
// model and removes it. If the deque is empty the call blocks until an element is enqueued.
func (d *Deque) DequeueModelBack(model encoding.BinaryUnmarshaler) error {
return d.session.Update(func(tx *bolt.Tx) error {
return PopModelOrWait(tx, d.name, PositionBack, model, d.session)
})
}
// Size returns the number of elements in the deque.
func (d *Deque) Size() int {
return BucketSize(d.db, d.name)
}