/
rank.go
234 lines (200 loc) · 4.12 KB
/
rank.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package teastats
import (
"encoding/json"
"fmt"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/types"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/util"
"sync"
)
// 排行计算
type Rank struct {
size int
top map[string]int
buffer map[string]int
bufferSize int
min int
minKey string
locker sync.RWMutex
isLoading bool
dirtyKeys map[string]int
}
// 排行值
type RankValue struct {
Key string `json:"key"`
Value int `json:"value"`
}
// 获取新对象
func NewRank(topSize int, bufferSize int) *Rank {
return &Rank{
size: topSize,
bufferSize: bufferSize,
top: map[string]int{},
buffer: map[string]int{},
dirtyKeys: map[string]int{},
}
}
// 添加键值
func (this *Rank) Add(key string) {
this.locker.Lock()
defer this.locker.Unlock()
v, ok := this.top[key]
if ok {
newV := v + 1
this.top[key] = newV
if !this.isLoading {
this.dirtyKeys[key] = newV
}
if this.min == 0 {
this.min = newV
this.minKey = key
} else if this.minKey == key {
this.min = 0
for k, v := range this.top {
if v < this.min {
this.min = v
this.minKey = k
}
}
}
return
}
v, ok = this.buffer[key]
newV := v + 1
if !this.isLoading {
this.dirtyKeys[key] = newV
}
if newV >= this.min { // 移到top中
if ok {
delete(this.buffer, key)
}
this.top[key] = newV
if len(this.top) > this.size {
this.min = 0
for k, v := range this.top {
if v < this.min || this.min == 0 {
this.min = v
this.minKey = k
}
}
// 将min key转到buffer中
delete(this.top, this.minKey)
if len(this.buffer) < this.bufferSize {
this.buffer[this.minKey] = this.min
}
this.min = 0
this.minKey = ""
this.min = 0
for k, v := range this.top {
if v < this.min || this.min == 0 {
this.min = v
this.minKey = k
}
}
} else if this.min == 0 {
this.min = newV
this.minKey = key
}
} else if len(this.top) < this.size {
this.top[key] = newV
} else {
if ok || len(this.buffer) < this.bufferSize {
this.buffer[key] = newV
}
}
}
// 获取排行结果
func (this *Rank) Top() []*RankValue {
this.locker.RLock()
defer this.locker.RUnlock()
values := []*RankValue{}
for k, v := range this.top {
values = append(values, &RankValue{
Key: k,
Value: v,
})
}
lists.Sort(values, func(i int, j int) bool {
return values[i].Value > values[j].Value
})
return values
}
// 保存到LevelDB
func (this *Rank) Save(db *leveldb.DB, prefix string) {
tx, err := db.OpenTransaction()
if err != nil {
return
}
this.locker.Lock()
keysCopy := this.dirtyKeys
this.dirtyKeys = map[string]int{}
topData, err := json.Marshal(this.top)
this.locker.Unlock()
for k, v := range keysCopy {
err1 := tx.Put([]byte(prefix+"@item@"+k), []byte(fmt.Sprintf("%d", v)), nil)
if err1 != nil {
logs.Error(err1)
}
}
if err != nil {
logs.Error(err)
} else {
err1 := tx.Put([]byte(prefix+"@top"), topData, nil)
if err1 != nil {
logs.Error(err1)
}
}
err = tx.Commit()
if err != nil {
logs.Error(err)
}
}
// 从LevelDB中加载
func (this *Rank) Load(db *leveldb.DB, prefix string) {
this.locker.Lock()
defer this.locker.Unlock()
this.isLoading = true
top, err := db.Get([]byte(prefix+"@top"), nil)
if err == nil && top != nil {
m := map[string]int{}
err = json.Unmarshal(top, &m)
if err == nil {
this.top = m
for k, v := range m {
if v < this.min || this.min == 0 {
this.min = v
this.minKey = k
}
}
}
}
it := db.NewIterator(util.BytesPrefix([]byte(prefix+"@item@")), nil)
prefixLen := len(prefix + "@item@")
i := 0
for it.Next() {
k := string(it.Key()[prefixLen:])
if _, ok := this.top[k]; ok {
continue
}
if i >= this.bufferSize {
break
}
v := types.Int(string(it.Value()))
this.buffer[k] = v
i++
}
it.Release()
this.isLoading = false
}
// 重置
func (this *Rank) Reset() {
this.locker.Lock()
defer this.locker.Unlock()
this.top = map[string]int{}
this.buffer = map[string]int{}
this.min = 0
this.minKey = ""
this.dirtyKeys = map[string]int{}
}