countminsketch

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: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

Functions

func IncrItemPipe

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

IncrItemPipe 为物品添加计数 @params key string 目标key @params item string 要增加的物品 @params opts ...optparams.Option[IncrOpts] 增加数据时的设置,可以使用`IncrWithIncrement`额外指定物品的增量 @returns *redisbloomhelper.OneInt64MapPlacehold 物品在新增后的存量

func IncrWithIncrItems

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

IncrWithIncrItems MincrItem专用,使用IncrItems的列表设置items,使用该参数标明将执行incrby,因此不能和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,因此不能和IncrWithItems混用 注意增量小于1会被调整为1

func IncrWithRefreshTTL

func IncrWithRefreshTTL() optparams.Option[IncrOpts]

IncrWithhRefreshTTL 设置总是刷新,pipeline中无效

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, opts ...optparams.Option[InitOpts]) (*redis.Cmd, error)

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

func InitWithDIM

func InitWithDIM(width, depth int64) optparams.Option[InitOpts]

InitWithProbability 使用概率方式初始化

func InitWithProbability

func InitWithProbability(error_rate, probability float64) optparams.Option[InitOpts]

InitWithProbability 使用概率方式初始化

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 MIncrItemPipe

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

MIncrItemPipe 为物品添加计数 @params key string 目标key @params opts ...optparams.Option[IncrOpts] 增加数据时的设置,请使用`IncrWithItemMap`或`IncrWithIncrItems`设置物品及其增量 @returns *redisbloomhelper.Int64MapPlacehold 物品在新增后的存量

func MQueryItemPipe

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

MQueryItemPipe 查看物品当前计数 @params key string 目标key @params items ...string 待查看物品 @returns *redisbloomhelper.Int64MapPlacehold 物品在新增后的存量

func MergePipe

func MergePipe(pipe redis.Pipeliner, ctx context.Context, key string, sources []*WeightedCountMinSketchKey, opts ...optparams.Option[InitOpts]) (*redis.Cmd, error)

MergePipe pipeline中将几个CountMinSketch合并到指定的key @params key string 合并到的目标key @params sources []*WeightedCountMinSketchKey 用于merge的其他CountMinSketch和其设置的权重序列 @params opts ...optparams.Option[InitOpts] 创建新CountMinSketch的设置,如果有设置则会先创建key再进行merge,否则认为key已经存在 @returns *CountMinSketch 融合了自己和othersources信息的新CountMinSketch对象

func QueryItemPipe

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

QeryItemPipe 查看物品当前计数 @params key string 目标key @params item string 代查看物品 @returns *redisbloomhelper.OneInt64MapPlacehold 物品在新增后的存量

func WithAutoRefreshInterval

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

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

func WithInitDIM

func WithInitDIM(width, depth int64) optparams.Option[Options]

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

func WithInitProbability

func WithInitProbability(error_rate, probability float64) 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 CountMinSketch

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

CountMinSketch count-min sketch对象 (Count–min sketch](https://en.wikipedia.org/wiki/Count%E2%80%93min_sketch)算法是一个在数据大小非常大时使用的一种通过牺牲准确性提高的效率的计数算法. 这个算法的原理是:不存储所有的不同的元素,只存储它们Sketch的计数. 基本的思路如下: 1. 创建n个长度为 X 的数组A用来计数(i属于n,第i个数组的长度即为x_i,第i个数组即为A_i),初始化每个元素的计数值为 0 2. 对于一个新来的元素y,用n个hash函数分别计算hash_i(y)到 0 到 x_i 之间的一个数作为数组的位置索引J(第i个数组的索引即为J_i) 3. 数组A_i对应的索引位置的计数值加一(A_i[J_i] += 1) 4. 这时要查询某个元素出现的频率,只要简单的返回这个元素哈希望后对应的数组的位置索引的计数值的最小值即可. 在实践中我们会控制数组的长度固定,数组的个数和数组的长度就是这个算法初始化的变量了,其中`width`是数组的长度,控制误差大小,`depth`是数组的宽度,控制膨胀计数的期望概率,也会影响内存占用 注意,和两个filter不同,cms必须先创建,incr不会创建cms结构,可以先创建再调用init方法初始化,也可以创建时使用对应的可选参数同步初始化

func New

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

New 创建一个新的令牌桶对象 @params cli redis.UniversalClient @params opts ...optparams.Option[Options] 设置对象的可选参数,注意 WithInitProbability 和 WithInitDIM如果有填只会取第一个

func (*CountMinSketch) Clean

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

Clean 清除CountMinSketch 的key

func (*CountMinSketch) IncrItem

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

IncrItem 为物品添加计数 @params item string 要增加的物品 @params opts ...optparams.Option[IncrOpts] 增加数据时的设置,可以使用`IncrWithIncrement`额外指定物品的增量 @returns int64 物品在新增后的存量

func (*CountMinSketch) Info

Info 查看指定CountMinSketch的状态

func (*CountMinSketch) Init

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

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

func (*CountMinSketch) InitFromSources

func (c *CountMinSketch) InitFromSources(ctx context.Context, sources ...*WeightedCountMinSketch) error

InitFromSources 将几个CountMinSketch的内容merge进自己这个key从而初始化 @params sources ...*WeightedCountMinSketch 用于初始化的其他CountMinSketch和其设置的权重序列

func (*CountMinSketch) MIncrItem

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

MIncrItem 为物品添加计数 @params opts ...optparams.Option[IncrOpts] 增加数据时的设置,请使用`IncrWithItemMap`或`IncrWithIncrItems`设置物品及其增量 @returns map[string]int64 物品在新增后的存量

func (*CountMinSketch) MQueryItem

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

MQueryItem 查看物品当前计数 @params items ...string 待查看物品 @returns map[string]int64 物品在新增后的存量

func (*CountMinSketch) Merge

func (c *CountMinSketch) Merge(ctx context.Context, weight int64, othersources []*WeightedCountMinSketch, opts ...optparams.Option[Options]) (*CountMinSketch, error)

Merge 自己和几个其他的CountMinSketch合并构造一个新的CountMinSketch @params weight int64 自身保存数据在新merge出来的CountMinSketch中的权重 @params othersources []*WeightedCountMinSketch 用于merge的其他CountMinSketch和其设置的权重序列 @params opts ...optparams.Option[Options] 创建新CountMinSketch的设置,我们可以使用这个设置新key的命名等 @returns *CountMinSketch 融合了自己和othersources信息的新CountMinSketch对象

func (*CountMinSketch) QueryItem

func (c *CountMinSketch) QueryItem(ctx context.Context, item string) (int64, error)

QeryItem 查看物品当前计数 @params item string 代查看物品 @returns int64 物品在新增后的存量

type CountMinSketchInfo

type CountMinSketchInfo struct {
	Width int64 `json:"Width"`
	Depth int64 `json:"Depth"`
	Count int64 `json:"Count"`
}

CountMinSketchInfo CountMinSketch数据结构状态信息

type IncrOpts

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

IncrOpts 是否刷新ttl的配置项

type InfoPlacehold

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

func InfoPipe

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

InfoPipe 查看指定CountMinSketch的状态 @params key string 目标key

func (*InfoPlacehold) Result

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

type InitOpts

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

type Options

type Options struct {
	Middlewaretype string
	MiddlewareOpts []optparams.Option[middlewarehelper.Options] //初始化Middleware的配置
	InitOpts       []optparams.Option[InitOpts]                 //countminsketch专用的初始化工具
}

Options broker的配置

type WeightedCountMinSketch

type WeightedCountMinSketch struct {
	Src    *CountMinSketch
	Weight int64
}

type WeightedCountMinSketchKey

type WeightedCountMinSketchKey struct {
	Key    string
	Weight int64
}

Jump to

Keyboard shortcuts

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