2020-02-28 02:59:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
|
2020-04-23 22:53:24 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2020-02-28 02:59:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type headBuffer struct {
|
|
|
|
buffer *list.List
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHeadBuffer(size int) *headBuffer {
|
|
|
|
buffer := list.New()
|
|
|
|
buffer.Init()
|
|
|
|
|
|
|
|
return &headBuffer{
|
|
|
|
buffer: buffer,
|
|
|
|
size: size,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 22:53:24 +00:00
|
|
|
func (h *headBuffer) Push(hc *api.HeadChange) (rethc *api.HeadChange) {
|
2020-02-28 02:59:43 +00:00
|
|
|
if h.buffer.Len() == h.size {
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
el := h.buffer.Front()
|
2020-04-23 22:53:24 +00:00
|
|
|
rethc, ok = el.Value.(*api.HeadChange)
|
2020-02-28 02:59:43 +00:00
|
|
|
if !ok {
|
|
|
|
panic("Value from list is not the correct type")
|
|
|
|
}
|
|
|
|
|
|
|
|
h.buffer.Remove(el)
|
|
|
|
}
|
|
|
|
|
|
|
|
h.buffer.PushBack(hc)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *headBuffer) Pop() {
|
|
|
|
el := h.buffer.Back()
|
|
|
|
if el != nil {
|
|
|
|
h.buffer.Remove(el)
|
|
|
|
}
|
|
|
|
}
|