-
Notifications
You must be signed in to change notification settings - Fork 33
/
queue.go
163 lines (137 loc) · 3.9 KB
/
queue.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package queue
import (
"container/heap"
"time"
)
// queueable is the interface for items that can be added to the queue.
type queueable[T comparable] interface {
comparable
Key() T
ScheduledTime() time.Time
}
// queue implements a queue for items that are scheduled to be executed at a later time.
// It acts as a "priority queue", in which items are added in order of when they're scheduled.
// Internally, it uses a heap (from container/heap) that allows Insert and Pop operations to be completed in O(log N) time (where N is the queue's length).
// Note: methods in this struct are not safe for concurrent use. Callers should use locks to ensure consistency.
type queue[K comparable, T queueable[K]] struct {
heap *queueHeap[K, T]
items map[K]*queueItem[K, T]
}
// newQueue creates a new queue.
func newQueue[K comparable, T queueable[K]]() queue[K, T] {
return queue[K, T]{
heap: new(queueHeap[K, T]),
items: make(map[K]*queueItem[K, T]),
}
}
// Len returns the number of items in the queue.
func (p *queue[K, T]) Len() int {
return p.heap.Len()
}
// Insert inserts a new item into the queue.
// If replace is true, existing items are replaced
func (p *queue[K, T]) Insert(r T, replace bool) {
key := r.Key()
// Check if the item already exists
item, ok := p.items[key]
if ok {
if replace {
item.value = r
heap.Fix(p.heap, item.index)
}
return
}
item = &queueItem[K, T]{
value: r,
}
heap.Push(p.heap, item)
p.items[key] = item
}
// Pop removes the next item in the queue and returns it.
// The returned boolean value will be "true" if an item was found.
func (p *queue[K, T]) Pop() (T, bool) {
if p.Len() == 0 {
var zero T
return zero, false
}
item, ok := heap.Pop(p.heap).(*queueItem[K, T])
if !ok || item == nil {
var zero T
return zero, false
}
delete(p.items, item.value.Key())
return item.value, true
}
// Peek returns the next item in the queue, without removing it.
// The returned boolean value will be "true" if an item was found.
func (p *queue[K, T]) Peek() (T, bool) {
if p.Len() == 0 {
var zero T
return zero, false
}
return (*p.heap)[0].value, true
}
// Remove an item from the queue.
func (p *queue[K, T]) Remove(key K) {
// If the item is not in the queue, this is a nop
item, ok := p.items[key]
if !ok {
return
}
heap.Remove(p.heap, item.index)
delete(p.items, key)
}
// Update an item in the queue.
func (p *queue[K, T]) Update(r T) {
// If the item is not in the queue, this is a nop
item, ok := p.items[r.Key()]
if !ok {
return
}
item.value = r
heap.Fix(p.heap, item.index)
}
type queueItem[K comparable, T queueable[K]] struct {
value T
// The index of the item in the heap. This is maintained by the heap.Interface methods.
index int
}
type queueHeap[K comparable, T queueable[K]] []*queueItem[K, T]
func (pq queueHeap[K, T]) Len() int {
return len(pq)
}
func (pq queueHeap[K, T]) Less(i, j int) bool {
return pq[i].value.ScheduledTime().Before(pq[j].value.ScheduledTime())
}
func (pq queueHeap[K, T]) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = i
pq[j].index = j
}
func (pq *queueHeap[K, T]) Push(x any) {
n := len(*pq)
item := x.(*queueItem[K, T])
item.index = n
*pq = append(*pq, item)
}
func (pq *queueHeap[K, T]) Pop() any {
old := *pq
n := len(old)
item := old[n-1]
old[n-1] = nil // Avoid memory leak
item.index = -1 // For safety
*pq = old[0 : n-1]
return item
}