-
Notifications
You must be signed in to change notification settings - Fork 81
/
example_priority_queue_test.go
52 lines (42 loc) · 1.1 KB
/
example_priority_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
52
package goque_test
import (
"fmt"
"github.com/beeker1121/goque"
)
// ExamplePriorityQueue demonstrates the implementation of a Goque queue.
func Example_priorityQueue() {
// Open/create a priority queue.
pq, err := goque.OpenPriorityQueue("data_dir", goque.ASC)
if err != nil {
fmt.Println(err)
return
}
defer pq.Close()
// Enqueue the item.
item, err := pq.Enqueue(0, []byte("item value"))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(item.ID) // 1
fmt.Println(item.Priority) // 0
fmt.Println(item.Key) // [0 58 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(item.Priority, 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()
if err != nil {
fmt.Println(err)
return
}
fmt.Println(deqItem.ToString()) // new item value
// Delete the queue and its database.
pq.Drop()
}