forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.go
112 lines (96 loc) · 2.79 KB
/
schedule.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package schedule
import (
"container/heap"
"time"
"github.com/juju/errors"
"github.com/juju/utils/clock"
)
// Schedule provides a schedule for storage operations, with the following
// properties:
// - fast to add and remove items by key: O(log(n)); n is the total number of items
// - fast to identify/remove the next scheduled item: O(log(n))
type Schedule struct {
time clock.Clock
items scheduleItems
m map[interface{}]*scheduleItem
}
// NewSchedule constructs a new schedule, using the given Clock for the Next
// method.
func NewSchedule(clock clock.Clock) *Schedule {
return &Schedule{
time: clock,
m: make(map[interface{}]*scheduleItem),
}
}
// Next returns a channel which will send after the next scheduled item's time
// has been reached. If there are no scheduled items, nil is returned.
func (s *Schedule) Next() <-chan time.Time {
if len(s.items) > 0 {
// TODO(fwereade): 2016-03-17 lp:1558657
return s.time.After(s.items[0].t.Sub(s.time.Now()))
}
return nil
}
// Ready returns the parameters for items that are scheduled at or before
// "now", and removes them from the schedule. The resulting slices are in
// order of time; items scheduled for the same time have no defined relative
// order.
func (s *Schedule) Ready(now time.Time) []interface{} {
var ready []interface{}
for len(s.items) > 0 && !s.items[0].t.After(now) {
item := heap.Pop(&s.items).(*scheduleItem)
delete(s.m, item.key)
ready = append(ready, item.value)
}
return ready
}
// Add adds an item with the specified value, with the corresponding key
// and time to the schedule. Add will panic if there already exists an item
// with the same key.
func (s *Schedule) Add(key, value interface{}, t time.Time) {
if _, ok := s.m[key]; ok {
panic(errors.Errorf("duplicate key %v", key))
}
item := &scheduleItem{key: key, value: value, t: t}
s.m[key] = item
heap.Push(&s.items, item)
}
// Remove removes the item corresponding to the specified key from the
// schedule. If no item with the specified key exists, this is a no-op.
func (s *Schedule) Remove(key interface{}) {
if item, ok := s.m[key]; ok {
heap.Remove(&s.items, item.i)
delete(s.m, key)
}
}
type scheduleItems []*scheduleItem
type scheduleItem struct {
i int
key interface{}
value interface{}
t time.Time
}
func (s scheduleItems) Len() int {
return len(s)
}
func (s scheduleItems) Less(i, j int) bool {
return s[i].t.Before(s[j].t)
}
func (s scheduleItems) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
s[i].i = i
s[j].i = j
}
func (s *scheduleItems) Push(x interface{}) {
item := x.(*scheduleItem)
item.i = len(*s)
*s = append(*s, item)
}
func (s *scheduleItems) Pop() interface{} {
n := len(*s) - 1
x := (*s)[n]
*s = (*s)[:n]
return x
}