forked from beeker1121/goque
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_prefix_queue_test.go
51 lines (41 loc) · 1.1 KB
/
example_prefix_queue_test.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
package goque_test
import (
"fmt"
"github.com/beeker1121/goque"
)
// ExamplePrefixQueue demonstrates the implementation of a Goque queue.
func Example_prefixQueue() {
// Open/create a prefix queue.
pq, err := goque.OpenPrefixQueue("data_dir")
if err != nil {
fmt.Println(err)
return
}
defer pq.Close()
// Enqueue an item.
item, err := pq.Enqueue([]byte("prefix"), []byte("item value"))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(item.ID) // 1
fmt.Println(item.Key) // [112 114 101 102 105 120 0 0 0 0 0 0 0 0 1]
fmt.Println(item.Value) // [105 116 101 109 32 118 97 108 117 101]
fmt.Println(item.ToString()) // item value
// Change the item value in the queue.
item, err = pq.Update([]byte("prefix"), item.ID, []byte("new item value"))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(item.ToString()) // new item value
// Dequeue the next item.
deqItem, err := pq.Dequeue([]byte("prefix"))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(deqItem.ToString()) // new item value
// Delete the queue and its database.
pq.Drop()
}