Fetch the repository succeeded.
package core
import (
"container/heap"
"fmt"
)
type Timeline interface {
IsEmpty() bool
Insert(e IEvent) error
OutFirst() (IEvent, error)
}
//Timeline是目前集群当前反映未来的事件时间线
//该时间线为因为新任务的提交,和机器更新,以及调度算法的改变而变化
type timeLine struct {
eventQueue eventHeap
}
//初始化一个Timeline,根据三种类型的事件
func InitTimeline(i []IEvent) Timeline {
var t timeLine
t.eventQueue = make(eventHeap, 0)
t.eventQueue = append(t.eventQueue, i...)
heap.Init(&t.eventQueue)
return &t
}
//从几条sortedList当中有着最先节点的list,然后将最先节点出列
//该最先节点就是下一个该执行的事件
func (t *timeLine) OutFirst() (IEvent, error) {
if t.IsEmpty() {
return nil, fmt.Errorf("there is no event in the queue")
}
return heap.Pop(&(t.eventQueue)).(IEvent), nil
}
func (t *timeLine) IsEmpty() bool {
return len(t.eventQueue) == 0
}
// 向时间线加入新的事件
func (t *timeLine) Insert(e IEvent) error {
heap.Push(&(t.eventQueue), e)
return nil
}
//----------------private----------------------------------------------
// use the container/heap to realize the event_queue,the top of the heap is the first event will happen next
type eventHeap []IEvent
func (h eventHeap) Len() int { return len(h) }
func (h eventHeap) Less(i, j int) bool { return h[i].HappenTime() < h[j].HappenTime() }
func (h eventHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *eventHeap) Push(x interface{}) {
// Push and Pop use pointer receivers because they modify the slice's length,
// not just its contents.
*h = append(*h, x.(IEvent))
}
func (h *eventHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。