-
Notifications
You must be signed in to change notification settings - Fork 1
/
topk.go
446 lines (426 loc) · 12.7 KB
/
topk.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package topk
import (
"context"
"errors"
"fmt"
"strconv"
"time"
// log "github.com/Golang-Tools/loggerhelper/v2"
"github.com/Golang-Tools/optparams"
"github.com/Golang-Tools/redishelper/v2/exthelper"
"github.com/Golang-Tools/redishelper/v2/exthelper/redisbloomhelper"
"github.com/Golang-Tools/redishelper/v2/middlewarehelper"
"github.com/go-redis/redis/v8"
)
//TopK TopK对象,用于统计大量数据下的前几位有哪些
//比较常见的场景比如大量数据情况下的排行榜应用
//使用了算法[HeavyKeeper](https://www.usenix.org/system/files/conference/atc18/atc18-gong.pdf).
//其基本原理是使用一个shape为(width,depth)的表保存物品的签名和计数,通过hash函数将签名和计数更新进表,并同时使用指数衰减的方法淘汰小流量数据
//取的时候取其中的最大值作为物品的计数
//注意,和两个filter不同,cms必须先创建,incr不会创建cms结构,可以先创建再调用init方法初始化,也可以创建时使用对应的可选参数同步初始化
type TopK struct {
*middlewarehelper.MiddleWareAbc
opt Options
}
//New 创建一个topk对象
//@params cli redis.UniversalClient
//@params opts ...optparams.Option[Options]
func New(cli redis.UniversalClient, opts ...optparams.Option[Options]) (*TopK, error) {
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond)
defer cancel()
m, err := exthelper.CheckModule(cli, ctx, "bf")
if err != nil {
return nil, err
}
if m == nil {
return nil, errors.New("redis server not load module bf yet")
}
bm := new(TopK)
bm.opt = Defaultopt
optparams.GetOption(&bm.opt, opts...)
mi, err := middlewarehelper.New(cli, bm.opt.Middlewaretype, bm.opt.MiddlewareOpts...)
if err != nil {
return nil, err
}
bm.MiddleWareAbc = mi
if bm.opt.InitTopK > 0 {
if bm.MaxTTL() > 0 {
bm.opt.InitOpts = append(bm.opt.InitOpts, InitWithRefreshTTL())
}
err := bm.Init(ctx, bm.opt.InitTopK, bm.opt.InitOpts...)
if err != nil {
return nil, err
}
}
return bm, nil
}
func initCmds(key string, topk int64, opt *InitOpts) (cmd string, params []interface{}, cmdswithoutttl []interface{}) {
cmd = "TOPK.RESERVE"
params = append(params, topk)
cmdswithoutttl = append(cmdswithoutttl, cmd, key, topk)
if opt.Width > 0 {
params = append(params, opt.Width)
cmdswithoutttl = append(cmdswithoutttl, opt.Width)
}
if opt.Depth > 0 {
params = append(params, opt.Depth)
cmdswithoutttl = append(cmdswithoutttl, opt.Depth)
}
if opt.Decay > 0 {
params = append(params, opt.Decay)
cmdswithoutttl = append(cmdswithoutttl, opt.Decay)
}
// log.Debug("initCmds", log.Dict{"cmd": cmd, "params": params, "cmdswithoutttl": cmdswithoutttl})
return
}
//Init 初始化TopK
//@params opts ...optparams.Option[InitOpts] 注意InitWithProbability和InitWithDIM必选一个,如果选了超过一个则会默认取第一个
func (c *TopK) Init(ctx context.Context, topk int64, opts ...optparams.Option[InitOpts]) error {
defaultopt := InitOpts{}
optparams.GetOption(&defaultopt, opts...)
refreshopt := middlewarehelper.RefreshOpt{}
optparams.GetOption(&refreshopt, defaultopt.RefreshOpts...)
var ttl time.Duration = 0
if refreshopt.RefreshTTL == middlewarehelper.RefreshTTLType_Refresh {
if refreshopt.TTL > 0 {
ttl = refreshopt.TTL
} else {
if c.MaxTTL() > 0 {
ttl = c.MaxTTL()
}
}
}
cmd, params, cmdswithoutttl := initCmds(c.Key(), topk, &defaultopt)
if ttl > 0 {
_, err := c.DoCmdWithTTL(ctx, cmd, c.Key(), c.MaxTTL(), params...)
if err != nil {
return err
}
} else {
_, err := c.Client().Do(ctx, cmdswithoutttl...).Result()
if err != nil {
return err
}
}
return nil
}
//Clean 清除TopK 的key
func (c *TopK) Clean(ctx context.Context) error {
_, err := c.Client().Del(ctx, c.Key()).Result()
if err != nil {
return err
}
return nil
}
func mIncrItemCmds(key string, opt *IncrOpts) (cmd string, params []interface{}, cmdswithoutttl []interface{}, err error) {
itemlen := len(opt.MincrItems)
if itemlen == 0 {
err = redisbloomhelper.ErrItemListEmpty
return
}
incrlen := len(opt.MincrIncrement)
cmd = "TOPK.INCRBY"
if incrlen == 0 {
cmd = "TOPK.ADD"
cmdswithoutttl = append(cmdswithoutttl, cmd, key)
for _, item := range opt.MincrItems {
params = append(params, item)
cmdswithoutttl = append(cmdswithoutttl, item)
}
} else {
if incrlen != itemlen {
err = redisbloomhelper.ErrItemLenNotMatch
return
}
cmdswithoutttl = append(cmdswithoutttl, cmd, key)
for index, item := range opt.MincrItems {
incr := opt.MincrIncrement[index]
params = append(params, item, incr)
cmdswithoutttl = append(cmdswithoutttl, item, incr)
}
}
return
}
//MIncrItem 为物品添加计数
//@params opts ...optparams.Option[IncrOpts] 请求设置项,请使用`IncrWithItems`或`IncrWithItemMap`或`IncrWithIncrItems`设置物品及其增量
//@returns []string 新增后被挤出队列的物品
func (c *TopK) MIncrItem(ctx context.Context, opts ...optparams.Option[IncrOpts]) ([]string, error) {
defaultopt := IncrOpts{}
optparams.GetOption(&defaultopt, opts...)
refreshopt := middlewarehelper.RefreshOpt{}
optparams.GetOption(&refreshopt, defaultopt.RefreshOpts...)
cmd, params, cmdswithoutttl, err := mIncrItemCmds(c.Key(), &defaultopt)
if err != nil {
return nil, err
}
var ttl time.Duration = 0
if refreshopt.RefreshTTL == middlewarehelper.RefreshTTLType_Refresh {
if refreshopt.TTL > 0 {
ttl = refreshopt.TTL
} else {
if c.MaxTTL() > 0 {
ttl = c.MaxTTL()
}
}
}
var infos []interface{}
var ok bool
if ttl > 0 {
res, err := c.DoCmdWithTTL(ctx, cmd, c.Key(), ttl, params...)
if err != nil {
return nil, err
}
infos, ok = res.([]interface{})
} else {
res, err := c.Client().Do(ctx, cmdswithoutttl...).Result()
if err != nil {
return nil, err
}
infos, ok = res.([]interface{})
}
if !ok {
return nil, fmt.Errorf("cannot parser %s results to []interface{}", cmd)
}
result := []string{}
for _, expelleditem := range infos {
if expelleditem != nil {
item, ok := expelleditem.(string)
if !ok {
return nil, fmt.Errorf("cannot parser %s result to int64", cmd)
}
result = append(result, item)
}
}
return result, nil
}
//IncrItem 为物品添加计数
//@params item string 物品及其对应的增量
//@returns []string 新增后被挤出队列的物品
func (c *TopK) IncrItem(ctx context.Context, item string, opts ...optparams.Option[IncrOpts]) ([]string, error) {
defaultopt := IncrOpts{}
optparams.GetOption(&defaultopt, opts...)
newopt := []optparams.Option[IncrOpts]{}
if defaultopt.Increment > 0 {
newopt = append(newopt, IncrWithIncrItems(&redisbloomhelper.IncrItem{Item: item, Increment: defaultopt.Increment}))
} else {
newopt = append(newopt, IncrWithItems(item))
}
if len(defaultopt.RefreshOpts) > 0 {
newopt = append(newopt, mIncr(defaultopt.RefreshOpts...))
}
ress, err := c.MIncrItem(ctx, newopt...)
if err != nil {
return nil, err
}
return ress, nil
}
func mQueryItemCmds(key string, items ...string) (cmd string, cmdswithoutttl []interface{}) {
cmd = "TOPK.QUERY"
cmdswithoutttl = append(cmdswithoutttl, cmd, key)
for _, item := range items {
cmdswithoutttl = append(cmdswithoutttl, item)
}
return
}
//MQueryItem 查看物品当前是否在topk序列
//@params items ...string 待查看物品
//@returns map[string]bool 物品:是否在topk序列
func (c *TopK) MQueryItem(ctx context.Context, items ...string) (map[string]bool, error) {
itemlen := len(items)
if itemlen == 0 {
return nil, redisbloomhelper.ErrItemListEmpty
}
cmd, cmdswithoutttl := mQueryItemCmds(c.Key(), items...)
var infos []interface{}
var ok bool
res, err := c.Client().Do(ctx, cmdswithoutttl...).Result()
if err != nil {
return nil, err
}
infos, ok = res.([]interface{})
if !ok {
return nil, fmt.Errorf("cannot parser %s results to []interface{}", cmd)
}
if len(infos) != itemlen {
return nil, redisbloomhelper.ErrResultLenNotMatch
}
result := map[string]bool{}
for index, item := range items {
existsint, ok := infos[index].(int64)
if !ok {
return nil, fmt.Errorf("cannot parser %s result to int64", cmd)
}
if existsint == 1 {
result[item] = true
} else {
result[item] = false
}
}
return result, nil
}
//QueryItem 查看物品当前是否在topk序列
//@params item ...string 待查看物品
//@returns bool 是否在topk序列,true为在
func (c *TopK) QueryItem(ctx context.Context, item string) (bool, error) {
ress, err := c.MQueryItem(ctx, item)
if err != nil {
return false, err
}
return ress[item], nil
}
func mCountItemCmds(key string, items ...string) (cmd string, cmdswithoutttl []interface{}) {
cmd = "TOPK.COUNT"
cmdswithoutttl = append(cmdswithoutttl, cmd, key)
for _, item := range items {
cmdswithoutttl = append(cmdswithoutttl, item)
}
return
}
//MCountItem topk中检查item的计数,注意这个计数永远不会高于实际数量并且可能会更低.
//@params items ...string 要检查的物品
//@return map[string]int64 item对应的计数
func (c *TopK) MCountItem(ctx context.Context, items ...string) (map[string]int64, error) {
itemlen := len(items)
if itemlen == 0 {
return nil, redisbloomhelper.ErrItemListEmpty
}
cmd, cmdswithoutttl := mCountItemCmds(c.Key(), items...)
var infos []interface{}
var ok bool
res, err := c.Client().Do(ctx, cmdswithoutttl...).Result()
if err != nil {
return nil, err
}
infos, ok = res.([]interface{})
if !ok {
return nil, fmt.Errorf("cannot parser %s results to []interface{}", cmd)
}
if len(infos) != itemlen {
return nil, redisbloomhelper.ErrResultLenNotMatch
}
result := map[string]int64{}
for index, item := range items {
existsint, ok := infos[index].(int64)
if !ok {
return nil, fmt.Errorf("cannot parser %s result to int64", cmd)
}
result[item] = existsint
}
return result, nil
}
//CountItem topk中检查item的计数,注意这个计数永远不会高于实际数量并且可能会更低.
//@params item string 要检查的物品
//@return map[string]int64 item对应的计数
func (c *TopK) CountItem(ctx context.Context, item string) (int64, error) {
ress, err := c.MCountItem(ctx, item)
if err != nil {
return 0, err
}
return ress[item], nil
}
//TopKInfo TopK状态信息
type TopKInfo struct {
K int64 `json:"K"`
Width int64 `json:"Width"`
Depth int64 `json:"Depth"`
Decay float64 `json:"Decay"`
}
//Info 查看指定TopK的状态
func (c *TopK) Info(ctx context.Context) (*TopKInfo, error) {
cmd := []interface{}{"TOPK.INFO", c.Key()}
res, err := c.Client().Do(ctx, cmd...).Result()
if err != nil {
return nil, err
}
infos, ok := res.([]interface{})
if len(infos) != 8 {
return nil, redisbloomhelper.ErrResultLenNotMatch
}
if !ok {
return nil, errors.New("cannot parser TOPK.INFO result to []interface{}")
}
// log.Info("get info", log.Dict{"res": res})
k, ok := infos[1].(int64)
if !ok {
return nil, errors.New("cannot parser K result to int64")
}
width, ok := infos[3].(int64)
if !ok {
return nil, errors.New("cannot parser Width result to int64")
}
depth, ok := infos[5].(int64)
if !ok {
return nil, errors.New("cannot parser Depth result to int64")
}
decay_s, ok := infos[7].(string)
if !ok {
return nil, errors.New("cannot parser Decay result to int64")
}
decay, err := strconv.ParseFloat(decay_s, 64)
if err != nil {
return nil, err
}
info := TopKInfo{
K: k,
Width: width,
Depth: depth,
Decay: decay,
}
return &info, nil
}
//List 列出全部topk物品
func (c *TopK) List(ctx context.Context) ([]string, error) {
cmd := []interface{}{"TOPK.LIST", c.Key()}
res, err := c.Client().Do(ctx, cmd...).Result()
if err != nil {
return nil, err
}
infos, ok := res.([]interface{})
if !ok {
return nil, errors.New("cannot parser TOPK.LIST result to []interface{}")
}
result := make([]string, len(infos))
for index, itemi := range infos {
item, ok := itemi.(string)
if !ok {
return nil, errors.New("cannot parser TOPK.LIST result to string")
}
result[index] = item
}
return result, nil
}
type CountItem struct {
Item string `json:"Item"`
Count int64 `json:"Count"`
}
//ListWithCount 列出全部topk物品及其对应的权重
func (c *TopK) ListWithCount(ctx context.Context) ([]*CountItem, error) {
cmd := []interface{}{"TOPK.LIST", c.Key(), "WITHCOUNT"}
res, err := c.Client().Do(ctx, cmd...).Result()
if err != nil {
return nil, err
}
infos, ok := res.([]interface{})
if !ok {
return nil, errors.New("cannot parser TOPK.LIST result to []interface{}")
}
result := []*CountItem{}
i := 0
for i < len(infos) {
item, ok1 := infos[i].(string)
if !ok1 {
return nil, errors.New("cannot parser TOPK.LIST result to string")
}
count, ok2 := infos[i+1].(int64)
if !ok2 {
return nil, errors.New("cannot parser TOPK.LIST result to int64")
}
ci := CountItem{
Item: item,
Count: count,
}
result = append(result, &ci)
i += 2
}
return result, nil
}