ExamplePriorityQueue demonstrates the implementation of a Goque queue.
()
| 8 | |
| 9 | // ExamplePriorityQueue demonstrates the implementation of a Goque queue. |
| 10 | func Example_priorityQueue() { |
| 11 | // Open/create a priority queue. |
| 12 | pq, err := goque.OpenPriorityQueue("data_dir", goque.ASC) |
| 13 | if err != nil { |
| 14 | fmt.Println(err) |
| 15 | return |
| 16 | } |
| 17 | defer pq.Close() |
| 18 | |
| 19 | // Enqueue the item. |
| 20 | item, err := pq.Enqueue(0, []byte("item value")) |
| 21 | if err != nil { |
| 22 | fmt.Println(err) |
| 23 | return |
| 24 | } |
| 25 | |
| 26 | fmt.Println(item.ID) // 1 |
| 27 | fmt.Println(item.Priority) // 0 |
| 28 | fmt.Println(item.Key) // [0 58 0 0 0 0 0 0 0 1] |
| 29 | fmt.Println(item.Value) // [105 116 101 109 32 118 97 108 117 101] |
| 30 | fmt.Println(item.ToString()) // item value |
| 31 | |
| 32 | // Change the item value in the queue. |
| 33 | item, err = pq.Update(item.Priority, item.ID, []byte("new item value")) |
| 34 | if err != nil { |
| 35 | fmt.Println(err) |
| 36 | return |
| 37 | } |
| 38 | |
| 39 | fmt.Println(item.ToString()) // new item value |
| 40 | |
| 41 | // Dequeue the next item. |
| 42 | deqItem, err := pq.Dequeue() |
| 43 | if err != nil { |
| 44 | fmt.Println(err) |
| 45 | return |
| 46 | } |
| 47 | |
| 48 | fmt.Println(deqItem.ToString()) // new item value |
| 49 | |
| 50 | // Delete the queue and its database. |
| 51 | pq.Drop() |
| 52 | } |