-
Notifications
You must be signed in to change notification settings - Fork 0
/
oldvodqueue.go
62 lines (52 loc) · 1.38 KB
/
oldvodqueue.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
package scraper
import (
"errors"
"github.com/monitor1379/yagods/maps/treemap"
"github.com/monitor1379/yagods/utils"
)
type oldVodsPriorityQueue struct {
tree *treemap.Map[*oldVodKey, *LiveVod]
}
type oldVodKey struct {
maxViews int
streamId string
startTimeUnix int64
}
func (vod *LiveVod) getOldVodKey() *oldVodKey {
return &oldVodKey{maxViews: vod.MaxViews, streamId: vod.StreamId, startTimeUnix: vod.StartTimeUnix}
}
func CreateNewOldVodQueue() *oldVodsPriorityQueue {
return &oldVodsPriorityQueue{
tree: treemap.NewWith[*oldVodKey, *LiveVod](func(a, b *oldVodKey) int {
dif := a.maxViews - b.maxViews
if dif != 0 {
return dif
}
dif = utils.StringComparator(a.streamId, b.streamId)
if dif != 0 {
return dif
}
return utils.NumberComparator(a.startTimeUnix, b.startTimeUnix)
})}
}
func (vods *oldVodsPriorityQueue) Put(vod *LiveVod) {
vods.tree.Put(vod.getOldVodKey(), vod)
}
func (vods *oldVodsPriorityQueue) Size() int {
return vods.tree.Size()
}
func (vods *oldVodsPriorityQueue) PopLowViewCount() (*LiveVod, error) {
_, vod := vods.tree.Min()
if vod == nil {
return nil, errors.New("tree is empty")
}
vods.tree.Remove(vod.getOldVodKey())
return vod, nil
}
func (vods *oldVodsPriorityQueue) GetLowViewCount() (*LiveVod, error) {
_, vod := vods.tree.Min()
if vod == nil {
return nil, errors.New("tree is empty")
}
return vod, nil
}