forked from open-falcon/falcon-plus
/
history.go
100 lines (84 loc) · 1.99 KB
/
history.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
package store
import (
"container/list"
"github.com/open-falcon/falcon-plus/common/model"
"sync"
)
type JudgeItemMap struct {
sync.RWMutex
M map[string]*SafeLinkedList
}
func NewJudgeItemMap() *JudgeItemMap {
return &JudgeItemMap{M: make(map[string]*SafeLinkedList)}
}
func (this *JudgeItemMap) Get(key string) (*SafeLinkedList, bool) {
this.RLock()
defer this.RUnlock()
val, ok := this.M[key]
return val, ok
}
func (this *JudgeItemMap) Set(key string, val *SafeLinkedList) {
this.Lock()
defer this.Unlock()
this.M[key] = val
}
func (this *JudgeItemMap) Len() int {
this.RLock()
defer this.RUnlock()
return len(this.M)
}
func (this *JudgeItemMap) Delete(key string) {
this.Lock()
defer this.Unlock()
delete(this.M, key)
}
func (this *JudgeItemMap) BatchDelete(keys []string) {
count := len(keys)
if count == 0 {
return
}
this.Lock()
defer this.Unlock()
for i := 0; i < count; i++ {
delete(this.M, keys[i])
}
}
func (this *JudgeItemMap) CleanStale(before int64) {
keys := []string{}
this.RLock()
for key, L := range this.M {
front := L.Front()
if front == nil {
continue
}
if front.Value.(*model.JudgeItem).Timestamp < before {
keys = append(keys, key)
}
}
this.RUnlock()
this.BatchDelete(keys)
}
func (this *JudgeItemMap) PushFrontAndMaintain(key string, val *model.JudgeItem, maxCount int, now int64) {
if linkedList, exists := this.Get(key); exists {
needJudge := linkedList.PushFrontAndMaintain(val, maxCount)
if needJudge {
Judge(linkedList, val, now)
}
} else {
NL := list.New()
NL.PushFront(val)
safeList := &SafeLinkedList{L: NL}
this.Set(key, safeList)
Judge(safeList, val, now)
}
}
// 这是个线程不安全的大Map,需要提前初始化好
var HistoryBigMap = make(map[string]*JudgeItemMap)
func InitHistoryBigMap() {
arr := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}
for i := 0; i < 16; i++ {
for j := 0; j < 16; j++ {
HistoryBigMap[arr[i]+arr[j]] = NewJudgeItemMap()
}
}
}