2020-05-07 23:38:05 +00:00
|
|
|
package sectorstorage
|
|
|
|
|
2020-07-27 11:20:18 +00:00
|
|
|
import "sort"
|
2020-05-07 23:38:05 +00:00
|
|
|
|
2022-05-18 13:47:08 +00:00
|
|
|
type RequestQueue []*WorkerRequest
|
2020-05-07 23:38:05 +00:00
|
|
|
|
2022-05-18 13:47:08 +00:00
|
|
|
func (q RequestQueue) Len() int { return len(q) }
|
2020-05-07 23:38:05 +00:00
|
|
|
|
2022-05-18 13:47:08 +00:00
|
|
|
func (q RequestQueue) Less(i, j int) bool {
|
|
|
|
oneMuchLess, muchLess := q[i].TaskType.MuchLess(q[j].TaskType)
|
2020-08-27 21:28:52 +00:00
|
|
|
if oneMuchLess {
|
|
|
|
return muchLess
|
|
|
|
}
|
|
|
|
|
2022-05-18 13:47:08 +00:00
|
|
|
if q[i].Priority != q[j].Priority {
|
|
|
|
return q[i].Priority > q[j].Priority
|
2020-06-24 21:06:56 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 13:47:08 +00:00
|
|
|
if q[i].TaskType != q[j].TaskType {
|
|
|
|
return q[i].TaskType.Less(q[j].TaskType)
|
2020-05-13 23:56:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 13:47:08 +00:00
|
|
|
return q[i].Sector.ID.Number < q[j].Sector.ID.Number // optimize minerActor.NewSectors bitfield
|
2020-05-07 23:38:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 13:47:08 +00:00
|
|
|
func (q RequestQueue) Swap(i, j int) {
|
2020-05-07 23:38:05 +00:00
|
|
|
q[i], q[j] = q[j], q[i]
|
|
|
|
q[i].index = i
|
|
|
|
q[j].index = j
|
|
|
|
}
|
|
|
|
|
2022-05-18 13:47:08 +00:00
|
|
|
func (q *RequestQueue) Push(x *WorkerRequest) {
|
2020-05-07 23:38:05 +00:00
|
|
|
n := len(*q)
|
2020-07-27 11:20:18 +00:00
|
|
|
item := x
|
2020-05-07 23:38:05 +00:00
|
|
|
item.index = n
|
|
|
|
*q = append(*q, item)
|
2020-07-27 11:20:18 +00:00
|
|
|
sort.Sort(q)
|
2020-05-07 23:38:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 13:47:08 +00:00
|
|
|
func (q *RequestQueue) Remove(i int) *WorkerRequest {
|
2020-05-07 23:38:05 +00:00
|
|
|
old := *q
|
|
|
|
n := len(old)
|
2020-07-27 11:20:18 +00:00
|
|
|
item := old[i]
|
2020-07-27 11:21:36 +00:00
|
|
|
old[i] = old[n-1]
|
|
|
|
old[n-1] = nil
|
2020-07-27 11:20:18 +00:00
|
|
|
item.index = -1
|
2020-05-07 23:38:05 +00:00
|
|
|
*q = old[0 : n-1]
|
2020-07-27 11:20:18 +00:00
|
|
|
sort.Sort(q)
|
2020-05-07 23:38:05 +00:00
|
|
|
return item
|
|
|
|
}
|