topk

package
v2.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 27, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Defaultopt = Options{
	Middlewaretype: "redis-ext-topk",
	MiddlewareOpts: []optparams.Option[middlewarehelper.Options]{},
	InitOpts:       []optparams.Option[InitOpts]{},
}

Functions

func CountItemPipe

func CountItemPipe(pipe redis.Pipeliner, ctx context.Context, key string, item string) (*redisbloomhelper.OneInt64MapPlacehold, error)

CountItemPipe topk中检查item的计数,注意这个计数永远不会高于实际数量并且可能会更低. @params key string 使用的key @params item string 要检查的物品 @return *redisbloomhelper.OneInt64MapPlacehold item对应的计数的占位符,使用`.Result()`获取值

func IncrItemPipe

func IncrItemPipe(pipe redis.Pipeliner, ctx context.Context, key string, item string, opts ...optparams.Option[IncrOpts]) (*redisbloomhelper.StringListValPlacehold, error)

IncrItemPipe 为物品添加计数 @params key string 使用的key @params item string 物品及其对应的增量 @returns *redisbloomhelper.StringListValPlacehold 新增后被挤出队列的物品的占位符,使用`.Result()`获取值

func IncrWithIncrItems

func IncrWithIncrItems(incritems ...*redisbloomhelper.IncrItem) optparams.Option[IncrOpts]

IncrWithIncrItems MincrItem专用,使用IncrItems的列表设置items,使用该参数标明将执行incrby而非add,因此不能和IncrWithItems混用 注意增量小于1会被调整为1

func IncrWithIncrement

func IncrWithIncrement(n int64) optparams.Option[IncrOpts]

IncrWithIncrement IncrItem专用,为物品设置增量

func IncrWithItemMap

func IncrWithItemMap(itemmap map[string]int64) optparams.Option[IncrOpts]

IncrWithItemMap MincrItem专用,使用字段形式设置items,使用该参数标明将执行incrby而非add,因此不能和IncrWithItems混用 注意增量小于1会被调整为1

func IncrWithItems

func IncrWithItems(items ...string) optparams.Option[IncrOpts]

IncrWithItems MincrItem专用,设置items并且不添加增量,使用该参数标明将执行add而非incrby,因此不能和IncrWithItemMap以及IncrWithIncrItems混用

func IncrWithRefreshTTL

func IncrWithRefreshTTL() optparams.Option[IncrOpts]

IncrWithhRefreshTTL 设置总是刷新

func IncrWithTTL

func IncrWithTTL(t time.Duration) optparams.Option[IncrOpts]

IncrWithTTL 设置总是使用指定的ttl刷新key

func InitPipe

func InitPipe(pipe redis.Pipeliner, ctx context.Context, key string, topk int64, opts ...optparams.Option[InitOpts]) *redis.Cmd

InitPipe 初始化TopK @params key string 使用的key @params topk int64 结构保存的topk @params opts ...optparams.Option[InitOpts] 可选参数

func InitWithDecay

func InitWithDecay(decay float64) optparams.Option[InitOpts]

InitWithDecay 使用概率方式初始化

func InitWithDepth

func InitWithDepth(depth int64) optparams.Option[InitOpts]

InitWithDepth 使用概率方式初始化

func InitWithRefreshTTL

func InitWithRefreshTTL() optparams.Option[InitOpts]

InitWithhRefreshTTL 设置使用maxttl设置key的过期,pipeline无效

func InitWithTTL

func InitWithTTL(t time.Duration) optparams.Option[InitOpts]

InitWithTTL 设置使用指定的ttl设置key的过期

func InitWithWidth

func InitWithWidth(width int64) optparams.Option[InitOpts]

InitWithWidth 使用概率方式初始化

func ListPipe

func ListPipe(pipe redis.Pipeliner, ctx context.Context, key string) *redisbloomhelper.StringListValPlacehold

ListPipe 列出全部topk物品 @params key string 使用的key

func MCountItemPipe

func MCountItemPipe(pipe redis.Pipeliner, ctx context.Context, key string, items ...string) (*redisbloomhelper.Int64MapPlacehold, error)

MCountItemPipe topk中检查item的计数,注意这个计数永远不会高于实际数量并且可能会更低. @params key string 使用的key @params items ...string 要检查的物品 @return *redisbloomhelper.Int64MapPlacehold item对应的计数的占位符,使用`.Result()`获取值

func MIncrItemPipe

func MIncrItemPipe(pipe redis.Pipeliner, ctx context.Context, key string, opts ...optparams.Option[IncrOpts]) (*redisbloomhelper.StringListValPlacehold, error)

MIncrItemPipe 为物品添加计数 @params key string 使用的key @params opts ...optparams.Option[IncrOpts] 请求设置项,请使用`IncrWithItems`或`IncrWithItemMap`或`IncrWithIncrItems`设置物品及其增量 @returns *redisbloomhelper.StringListValPlacehold 新增后被挤出队列的物品的占位符,使用`.Result()`获取值

func MQueryItemPipe

func MQueryItemPipe(pipe redis.Pipeliner, ctx context.Context, key string, items ...string) (*redisbloomhelper.BoolMapPlacehold, error)

MQueryItemPipe 查看物品当前是否在topk序列 @params key string 使用的key @params items ...string 待查看物品 @returns *redisbloomhelper.BoolMapPlacehold 物品:是否在topk序列的占位符,使用`.Result()`获取值

func QueryItemPipe

func QueryItemPipe(pipe redis.Pipeliner, ctx context.Context, key string, item string) (*redisbloomhelper.OneBoolMapPlacehold, error)

QueryItemPipe 查看物品当前是否在topk序列 @params key string 使用的key @params item ...string 待查看物品 @returns *redisbloomhelper.OneBoolMapPlacehold 是否在topk序列(true为在)的占位符,使用`.Result()`获取值

func WithAutoRefreshInterval

func WithAutoRefreshInterval(autoRefreshInterval string) optparams.Option[Options]

WithAutoRefreshInterval 设置自动刷新过期时间的设置

func WithInit

func WithInit(topk int64, opts ...optparams.Option[InitOpts]) optparams.Option[Options]

WithInitProbability cms专用,创建cms对象时使用prob方式初始化对应的key

func WithKey

func WithKey(key string) optparams.Option[Options]

WithKey 中间件通用设置,指定使用的键,注意设置后namespace依然有效

func WithMaxTTL

func WithMaxTTL(maxTTL time.Duration) optparams.Option[Options]

WithMaxTTL 设置token消减间隔时长,单位s

func WithMiddlewaretype

func WithMiddlewaretype(typename string) optparams.Option[Options]

WithMiddlewaretype 设置中间件类型

func WithNamespace

func WithNamespace(ns ...string) optparams.Option[Options]

WithNamespace 中间件通用设置,指定锁的命名空间

func WithSpecifiedKey

func WithSpecifiedKey(key string) optparams.Option[Options]

WithSpecifiedKey 中间件通用设置,指定使用的键,注意设置key后namespace将失效

func WithTaskCron

func WithTaskCron(taskCron *cron.Cron) optparams.Option[Options]

WithTaskCron 设置定时器

Types

type CountItem

type CountItem struct {
	Item  string `json:"Item"`
	Count int64  `json:"Count"`
}

type CountItemPlacehold

type CountItemPlacehold struct {
	Ck  string
	Cmd *redis.Cmd
}

func ListWithCountPipe

func ListWithCountPipe(pipe redis.Pipeliner, ctx context.Context, key string) *CountItemPlacehold

ListWithCountPipe 列出全部topk物品及其对应的权重 @params key string 使用的key

func (*CountItemPlacehold) Result

func (r *CountItemPlacehold) Result() ([]*CountItem, error)

type IncrOpts

type IncrOpts struct {
	RefreshOpts    []optparams.Option[middlewarehelper.RefreshOpt]
	Increment      int64    //incr专用
	MincrItems     []string //mincr专用
	MincrIncrement []int64  //mincr专用
}

type InfoPlacehold

type InfoPlacehold struct {
	Ck  string
	Cmd *redis.Cmd
}

func InfoPipe

func InfoPipe(pipe redis.Pipeliner, ctx context.Context, key string) *InfoPlacehold

InfoPipe 查看指定TopK的状态 @params key string 使用的key

func (*InfoPlacehold) Result

func (r *InfoPlacehold) Result() (*TopKInfo, error)

type InitOpts

type InitOpts struct {
	Width       int64
	Depth       int64
	Decay       float64
	RefreshOpts []optparams.Option[middlewarehelper.RefreshOpt]
}

type Options

type Options struct {
	Middlewaretype string
	MiddlewareOpts []optparams.Option[middlewarehelper.Options] //初始化Middleware的配置
	InitTopK       int64
	InitOpts       []optparams.Option[InitOpts] //初始化参数
}

Options broker的配置

type TopK

type TopK struct {
	*middlewarehelper.MiddleWareAbc
	// contains filtered or unexported fields
}

TopK TopK对象,用于统计大量数据下的前几位有哪些 比较常见的场景比如大量数据情况下的排行榜应用 使用了算法[HeavyKeeper](https://www.usenix.org/system/files/conference/atc18/atc18-gong.pdf). 其基本原理是使用一个shape为(width,depth)的表保存物品的签名和计数,通过hash函数将签名和计数更新进表,并同时使用指数衰减的方法淘汰小流量数据 取的时候取其中的最大值作为物品的计数 注意,和两个filter不同,cms必须先创建,incr不会创建cms结构,可以先创建再调用init方法初始化,也可以创建时使用对应的可选参数同步初始化

func New

func New(cli redis.UniversalClient, opts ...optparams.Option[Options]) (*TopK, error)

New 创建一个topk对象 @params cli redis.UniversalClient @params opts ...optparams.Option[Options]

func (*TopK) Clean

func (c *TopK) Clean(ctx context.Context) error

Clean 清除TopK 的key

func (*TopK) CountItem

func (c *TopK) CountItem(ctx context.Context, item string) (int64, error)

CountItem topk中检查item的计数,注意这个计数永远不会高于实际数量并且可能会更低. @params item string 要检查的物品 @return map[string]int64 item对应的计数

func (*TopK) IncrItem

func (c *TopK) IncrItem(ctx context.Context, item string, opts ...optparams.Option[IncrOpts]) ([]string, error)

IncrItem 为物品添加计数 @params item string 物品及其对应的增量 @returns []string 新增后被挤出队列的物品

func (*TopK) Info

func (c *TopK) Info(ctx context.Context) (*TopKInfo, error)

Info 查看指定TopK的状态

func (*TopK) Init

func (c *TopK) Init(ctx context.Context, topk int64, opts ...optparams.Option[InitOpts]) error

Init 初始化TopK @params opts ...optparams.Option[InitOpts] 注意InitWithProbability和InitWithDIM必选一个,如果选了超过一个则会默认取第一个

func (*TopK) List

func (c *TopK) List(ctx context.Context) ([]string, error)

List 列出全部topk物品

func (*TopK) ListWithCount

func (c *TopK) ListWithCount(ctx context.Context) ([]*CountItem, error)

ListWithCount 列出全部topk物品及其对应的权重

func (*TopK) MCountItem

func (c *TopK) MCountItem(ctx context.Context, items ...string) (map[string]int64, error)

MCountItem topk中检查item的计数,注意这个计数永远不会高于实际数量并且可能会更低. @params items ...string 要检查的物品 @return map[string]int64 item对应的计数

func (*TopK) MIncrItem

func (c *TopK) MIncrItem(ctx context.Context, opts ...optparams.Option[IncrOpts]) ([]string, error)

MIncrItem 为物品添加计数 @params opts ...optparams.Option[IncrOpts] 请求设置项,请使用`IncrWithItems`或`IncrWithItemMap`或`IncrWithIncrItems`设置物品及其增量 @returns []string 新增后被挤出队列的物品

func (*TopK) MQueryItem

func (c *TopK) MQueryItem(ctx context.Context, items ...string) (map[string]bool, error)

MQueryItem 查看物品当前是否在topk序列 @params items ...string 待查看物品 @returns map[string]bool 物品:是否在topk序列

func (*TopK) QueryItem

func (c *TopK) QueryItem(ctx context.Context, item string) (bool, error)

QueryItem 查看物品当前是否在topk序列 @params item ...string 待查看物品 @returns bool 是否在topk序列,true为在

type TopKInfo

type TopKInfo struct {
	K     int64   `json:"K"`
	Width int64   `json:"Width"`
	Depth int64   `json:"Depth"`
	Decay float64 `json:"Decay"`
}

TopKInfo TopK状态信息

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL