redis_bloom_go

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2021 License: BSD-3-Clause Imports: 8 Imported by: 15

README

license CircleCI GitHub issues Codecov GoDoc Go Report Card

redisbloom-go

Forum Discord

Go client for RedisBloom (https://github.com/RedisBloom/redisbloom), based on redigo.

Installing

$ go get github.com/RedisBloom/redisbloom-go

Running tests

A simple test suite is provided, and can be run with:

$ go test

The tests expect a Redis server with the RedisBloom module loaded to be available at localhost:6379. You can easily launch RedisBloom with Docker in the following manner:

docker run -d -p 6379:6379 --name redis-redisbloom redislabs/rebloom:latest 

Example Code

Make sure to check the full list of examples at Pkg.go.dev.

package main 

import (
        "fmt"
        redisbloom "github.com/RedisBloom/redisbloom-go"
)

func main() {
		// Connect to localhost with no password
    var client = redisbloom.NewClient("localhost:6379", "nohelp", nil)
       
    // BF.ADD mytest item 
    _, err := client.Add("mytest", "myItem")
    if err != nil {
        fmt.Println("Error:", err)
    }
    
    exists, err := client.Exists("mytest", "myItem")
    if err != nil {
        fmt.Println("Error:", err)
    }
    fmt.Println("myItem exists in mytest: ", exists)
}

Supported RedisBloom Commands

Make sure to check the full command reference at redisbloom.io.

Bloom Filter
Command Recommended API and godoc
BF.RESERVE Reserve
BF.ADD Add
BF.MADD BfAddMulti
BF.INSERT BfInsert
BF.EXISTS Exists
BF.MEXISTS BfExistsMulti
BF.SCANDUMP BfScanDump
BF.LOADCHUNK BfLoadChunk
BF.INFO Info
Cuckoo Filter
Command Recommended API and godoc
CF.RESERVE CfReserve
CF.ADD CfAdd
CF.ADDNX CfAddNx
CF.INSERT CfInsert
CF.INSERTNX CfInsertNx
CF.EXISTS CfExists
CF.DEL CfDel
CF.COUNT CfCount
CF.SCANDUMP CfScanDump
CF.LOADCHUNK CfLoadChunk
CF.INFO CfInfo
Count-Min Sketch
Command Recommended API and godoc
CMS.INITBYDIM CmsInitByDim
CMS.INITBYPROB CmsInitByProb
CMS.INCRBY CmsIncrBy
CMS.QUERY CmsQuery
CMS.MERGE CmsMerge
CMS.INFO CmsInfo
TopK Filter
Command Recommended API and godoc
TOPK.RESERVE TopkReserve
TOPK.ADD TopkAdd
TOPK.INCRBY TopkIncrby
TOPK.QUERY TopkQuery
TOPK.COUNT TopkCount
TOPK.LIST TopkList
TOPK.INFO TopkInfo

License

redisbloom-go is distributed under the BSD 3-Clause license - see LICENSE

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetInsertArgs added in v0.5.0

func GetInsertArgs(key string, cap int64, noCreate bool, items []string) redis.Args

func ParseInfoReply added in v0.5.0

func ParseInfoReply(values []interface{}, err error) (map[string]int64, error)

Types

type Client

type Client struct {
	Pool ConnPool
	Name string
}

Client is an interface to RedisBloom redis commands

func NewClient

func NewClient(addr, name string, authPass *string) *Client

NewClient creates a new client connecting to the redis host, and using the given name as key prefix. Addr can be a single host:port pair, or a comma separated list of host:port,host:port... In the case of multiple hosts we create a multi-pool and select connections at random Deprecated: Please use NewClientFromPool() instead

Example

exemplifies the NewClient function

package main

import (
	"fmt"
	redisbloom "github.com/RedisBloom/redisbloom-go"
)

func main() {
	host := "localhost:6379"
	var client = redisbloom.NewClient(host, "nohelp", nil)

	// BF.ADD mytest item
	_, err := client.Add("mytest", "myItem")
	if err != nil {
		fmt.Println("Error:", err)
	}

	exists, err := client.Exists("mytest", "myItem")
	if err != nil {
		fmt.Println("Error:", err)
	}
	fmt.Println("myItem exists in mytest: ", exists)
}
Output:

myItem exists in mytest:  true

func NewClientFromPool added in v0.3.0

func NewClientFromPool(pool *redis.Pool, name string) *Client

NewClientFromPool creates a new Client with the given pool and client name

Example

exemplifies the NewClientFromPool function

package main

import (
	"fmt"
	redisbloom "github.com/RedisBloom/redisbloom-go"
	"github.com/gomodule/redigo/redis"
	"log"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redisbloom.NewClientFromPool(pool, "bloom-client-1")

	// BF.ADD mytest item
	_, err := client.Add("mytest", "myItem")
	if err != nil {
		log.Fatalf("Error: %v", err)
	}

	exists, err := client.Exists("mytest", "myItem")
	if err != nil {
		log.Fatalf("Error: %v", err)
	}
	fmt.Println("myItem exists in mytest: ", exists)
}
Output:

myItem exists in mytest:  true

func (*Client) Add

func (client *Client) Add(key string, item string) (exists bool, err error)

Add - Add (or create and add) a new value to the filter args: key - the name of the filter item - the item to add

func (*Client) BfAddMulti added in v0.3.0

func (client *Client) BfAddMulti(key string, items []string) ([]int64, error)

BfAddMulti - Adds one or more items to the Bloom Filter, creating the filter if it does not yet exist. args: key - the name of the filter item - One or more items to add

func (*Client) BfExistsMulti added in v0.3.0

func (client *Client) BfExistsMulti(key string, items []string) ([]int64, error)

BfExistsMulti - Determines if one or more items may exist in the filter or not. args: key - the name of the filter item - one or more items to check

func (*Client) BfInsert added in v0.9.0

func (client *Client) BfInsert(key string, cap int64, errorRatio float64, expansion int64, noCreate bool, nonScaling bool, items []string) (res []int64, err error)

This command will add one or more items to the bloom filter, by default creating it if it does not yet exist.

func (*Client) BfLoadChunk added in v0.9.0

func (client *Client) BfLoadChunk(key string, iter int64, data []byte) (string, error)

Restores a filter previously saved using SCANDUMP .

func (*Client) BfScanDump added in v0.9.0

func (client *Client) BfScanDump(key string, iter int64) (int64, []byte, error)

Begins an incremental save of the bloom filter.

func (*Client) CfAdd added in v0.5.0

func (client *Client) CfAdd(key string, item string) (bool, error)

Adds an item to the cuckoo filter, creating the filter if it does not exist.

func (*Client) CfAddNx added in v0.5.0

func (client *Client) CfAddNx(key string, item string) (bool, error)

Adds an item to a cuckoo filter if the item did not exist previously.

func (*Client) CfCount added in v0.5.0

func (client *Client) CfCount(key string, item string) (int64, error)

Returns the number of times an item may be in the filter.

func (*Client) CfDel added in v0.5.0

func (client *Client) CfDel(key string, item string) (bool, error)

Deletes an item once from the filter.

func (*Client) CfExists added in v0.5.0

func (client *Client) CfExists(key string, item string) (bool, error)

Check if an item exists in a Cuckoo Filter

func (*Client) CfInfo added in v0.5.0

func (client *Client) CfInfo(key string) (map[string]int64, error)

Return information about key

func (*Client) CfInsert added in v0.5.0

func (client *Client) CfInsert(key string, cap int64, noCreate bool, items []string) ([]int64, error)

Adds one or more items to a cuckoo filter, allowing the filter to be created with a custom capacity if it does not yet exist.

func (*Client) CfInsertNx added in v0.5.0

func (client *Client) CfInsertNx(key string, cap int64, noCreate bool, items []string) ([]int64, error)

Adds one or more items to a cuckoo filter, allowing the filter to be created with a custom capacity if it does not yet exist.

func (*Client) CfLoadChunk added in v0.5.0

func (client *Client) CfLoadChunk(key string, iter int64, data []byte) (string, error)

Restores a filter previously saved using SCANDUMP

func (*Client) CfReserve added in v0.5.0

func (client *Client) CfReserve(key string, capacity int64, bucketSize int64, maxIterations int64, expansion int64) (string, error)

Create an empty cuckoo filter with an initial capacity of {capacity} items.

func (*Client) CfScanDump added in v0.5.0

func (client *Client) CfScanDump(key string, iter int64) (int64, []byte, error)

Begins an incremental save of the cuckoo filter.

func (*Client) CmsIncrBy added in v0.5.0

func (client *Client) CmsIncrBy(key string, itemIncrements map[string]int64) ([]int64, error)

Increases the count of item by increment. Multiple items can be increased with one call.

func (*Client) CmsInfo added in v0.5.0

func (client *Client) CmsInfo(key string) (map[string]int64, error)

Returns width, depth and total count of the sketch.

func (*Client) CmsInitByDim added in v0.5.0

func (client *Client) CmsInitByDim(key string, width int64, depth int64) (string, error)

Initializes a Count-Min Sketch to dimensions specified by user.

func (*Client) CmsInitByProb added in v0.5.0

func (client *Client) CmsInitByProb(key string, error float64, probability float64) (string, error)

Initializes a Count-Min Sketch to accommodate requested capacity.

func (*Client) CmsMerge added in v0.5.0

func (client *Client) CmsMerge(dest string, srcs []string, weights []int64) (string, error)

Merges several sketches into one sketch, stored at dest key All sketches must have identical width and depth.

func (*Client) CmsQuery added in v0.5.0

func (client *Client) CmsQuery(key string, items []string) ([]int64, error)

Returns count for item.

func (*Client) Exists

func (client *Client) Exists(key string, item string) (exists bool, err error)

Exists - Determines whether an item may exist in the Bloom Filter or not. args: key - the name of the filter item - the item to check for

func (*Client) Info added in v0.2.0

func (client *Client) Info(key string) (info map[string]int64, err error)

Info - Return information about key args: key - the name of the filter

func (*Client) Reserve added in v0.2.0

func (client *Client) Reserve(key string, error_rate float64, capacity uint64) (err error)

Reserve - Creates an empty Bloom Filter with a given desired error ratio and initial capacity. args: key - the name of the filter error_rate - the desired probability for false positives capacity - the number of entries you intend to add to the filter

func (*Client) TdAdd added in v1.0.0

func (client *Client) TdAdd(key string, samples map[float64]float64) (string, error)

TdAdd - Adds one or more samples to a sketch

Example

exemplifies the TdAdd function

package main

import (
	"fmt"
	redisbloom "github.com/RedisBloom/redisbloom-go"
)

func main() {
	host := "localhost:6379"
	var client = redisbloom.NewClient(host, "nohelp", nil)

	key := "example"
	ret, err := client.TdCreate(key, 100)
	if err != nil {
		fmt.Println("Error:", err)
	}

	samples := map[float64]float64{1.0: 1.0, 2.0: 2.0}
	ret, err = client.TdAdd(key, samples)
	if err != nil {
		fmt.Println("Error:", err)
	}

	fmt.Println(ret)
}
Output:

OK

func (*Client) TdCdf added in v1.0.0

func (client *Client) TdCdf(key string, value float64) (float64, error)

TdCdf - Returns the fraction of all points added which are <= value

Example

exemplifies the TdCdf function

package main

import (
	"fmt"
	redisbloom "github.com/RedisBloom/redisbloom-go"
)

func main() {
	host := "localhost:6379"
	var client = redisbloom.NewClient(host, "nohelp", nil)

	key := "example"
	_, err := client.TdCreate(key, 10)
	if err != nil {
		fmt.Println("Error:", err)
	}

	samples := map[float64]float64{1.0: 1.0, 2.0: 1.0, 3.0: 1.0, 4.0: 1.0, 5.0: 1.0}
	_, err = client.TdAdd(key, samples)
	if err != nil {
		fmt.Println("Error:", err)
	}

	cdf, err := client.TdCdf(key, 1.0)
	if err != nil {
		fmt.Println("Error:", err)
	}

	fmt.Println(cdf)
}
Output:

0.1

func (*Client) TdCreate added in v1.0.0

func (client *Client) TdCreate(key string, compression int64) (string, error)

TdCreate - Allocate the memory and initialize the t-digest

Example

exemplifies the TdCreate function

package main

import (
	"fmt"
	redisbloom "github.com/RedisBloom/redisbloom-go"
)

func main() {
	host := "localhost:6379"
	var client = redisbloom.NewClient(host, "nohelp", nil)

	ret, err := client.TdCreate("key", 100)
	if err != nil {
		fmt.Println("Error:", err)
	}

	fmt.Println(ret)
}
Output:

OK

func (*Client) TdInfo added in v1.0.0

func (client *Client) TdInfo(key string) (TDigestInfo, error)

TdInfo - Returns compression, capacity, total merged and unmerged nodes, the total compressions made up to date on that key, and merged and unmerged weight.

func (*Client) TdMax added in v1.0.0

func (client *Client) TdMax(key string) (float64, error)

TdMax - Get maximum value from the sketch. Will return DBL_MIN if the sketch is empty

Example

exemplifies the TdMax function

package main

import (
	"fmt"
	redisbloom "github.com/RedisBloom/redisbloom-go"
)

func main() {
	host := "localhost:6379"
	var client = redisbloom.NewClient(host, "nohelp", nil)

	key := "example"
	_, err := client.TdCreate(key, 10)
	if err != nil {
		fmt.Println("Error:", err)
	}

	samples := map[float64]float64{1.0: 1.0, 2.0: 2.0, 3.0: 3.0}
	_, err = client.TdAdd(key, samples)
	if err != nil {
		fmt.Println("Error:", err)
	}

	max, err := client.TdMax(key)
	if err != nil {
		fmt.Println("Error:", err)
	}

	fmt.Println(max)
}
Output:

3

func (*Client) TdMerge added in v1.0.0

func (client *Client) TdMerge(toKey string, fromKey string) (string, error)

TdMerge - Merges all of the values from 'from' to 'this' sketch

func (*Client) TdMin added in v1.0.0

func (client *Client) TdMin(key string) (float64, error)

TdMin - Get minimum value from the sketch. Will return DBL_MAX if the sketch is empty

Example

exemplifies the TdMin function

package main

import (
	"fmt"
	redisbloom "github.com/RedisBloom/redisbloom-go"
)

func main() {
	host := "localhost:6379"
	var client = redisbloom.NewClient(host, "nohelp", nil)

	key := "example"
	_, err := client.TdCreate(key, 10)
	if err != nil {
		fmt.Println("Error:", err)
	}

	samples := map[float64]float64{1.0: 1.0, 2.0: 2.0, 3.0: 3.0}
	_, err = client.TdAdd(key, samples)
	if err != nil {
		fmt.Println("Error:", err)
	}

	min, err := client.TdMin(key)
	if err != nil {
		fmt.Println("Error:", err)
	}

	fmt.Println(min)
}
Output:

1

func (*Client) TdQuantile added in v1.0.0

func (client *Client) TdQuantile(key string, quantile float64) (float64, error)

TdQuantile - Returns an estimate of the cutoff such that a specified fraction of the data added to this TDigest would be less than or equal to the cutoff

Example

exemplifies the TdQuantile function

package main

import (
	"fmt"
	redisbloom "github.com/RedisBloom/redisbloom-go"
)

func main() {
	host := "localhost:6379"
	var client = redisbloom.NewClient(host, "nohelp", nil)

	key := "example"
	_, err := client.TdCreate(key, 10)
	if err != nil {
		fmt.Println("Error:", err)
	}

	samples := map[float64]float64{1.0: 1.0, 2.0: 1.0, 3.0: 1.0, 4.0: 1.0, 5.0: 1.0}
	_, err = client.TdAdd(key, samples)
	if err != nil {
		fmt.Println("Error:", err)
	}

	ans, err := client.TdQuantile(key, 1.0)
	if err != nil {
		fmt.Println("Error:", err)
	}

	fmt.Println(ans)
}
Output:

5

func (*Client) TdReset added in v1.0.0

func (client *Client) TdReset(key string) (string, error)

TdReset - Reset the sketch to zero - empty out the sketch and re-initialize it

func (*Client) TopkAdd added in v0.3.0

func (client *Client) TopkAdd(key string, items []string) ([]string, error)

Adds an item to the data structure.

func (*Client) TopkCount added in v0.3.0

func (client *Client) TopkCount(key string, items []string) (result []int64, err error)

Returns count for an item.

func (*Client) TopkIncrBy added in v0.3.0

func (client *Client) TopkIncrBy(key string, itemIncrements map[string]int64) ([]string, error)

Increase the score of an item in the data structure by increment.

func (*Client) TopkInfo added in v0.3.0

func (client *Client) TopkInfo(key string) (map[string]string, error)

Returns number of required items (k), width, depth and decay values.

func (*Client) TopkList added in v0.3.0

func (client *Client) TopkList(key string) ([]string, error)

Return full list of items in Top K list.

func (*Client) TopkQuery added in v0.3.0

func (client *Client) TopkQuery(key string, items []string) ([]int64, error)

Checks whether an item is one of Top-K items.

func (*Client) TopkReserve added in v0.3.0

func (client *Client) TopkReserve(key string, topk int64, width int64, depth int64, decay float64) (string, error)

Initializes a TopK with specified parameters.

type ConnPool

type ConnPool interface {
	Get() redis.Conn
	Close() error
}

type MultiHostPool

type MultiHostPool struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewMultiHostPool

func NewMultiHostPool(hosts []string, authPass *string) *MultiHostPool

func (*MultiHostPool) Close added in v0.3.0

func (p *MultiHostPool) Close() (err error)

func (*MultiHostPool) Get

func (p *MultiHostPool) Get() redis.Conn

type SingleHostPool

type SingleHostPool struct {
	*redis.Pool
}

func NewSingleHostPool

func NewSingleHostPool(host string, authPass *string) *SingleHostPool

type TDigestInfo added in v1.0.0

type TDigestInfo struct {
	// contains filtered or unexported fields
}

TDigestInfo is a struct that represents T-Digest properties

func ParseTDigestInfo added in v1.0.0

func ParseTDigestInfo(result interface{}, err error) (info TDigestInfo, outErr error)

func (*TDigestInfo) Capacity added in v1.0.0

func (info *TDigestInfo) Capacity() int64

Capacity - returns the capacity of TDigestInfo instance

func (*TDigestInfo) Compression added in v1.0.0

func (info *TDigestInfo) Compression() int64

Compression - returns the compression of TDigestInfo instance

func (*TDigestInfo) MergedNodes added in v1.0.0

func (info *TDigestInfo) MergedNodes() int64

MergedNodes - returns the merged nodes of TDigestInfo instance

func (*TDigestInfo) MergedWeight added in v1.0.0

func (info *TDigestInfo) MergedWeight() float64

MergedWeight - returns the merged weight of TDigestInfo instance

func (*TDigestInfo) TotalCompressions added in v1.0.0

func (info *TDigestInfo) TotalCompressions() int64

TotalCompressions - returns the total compressions of TDigestInfo instance

func (*TDigestInfo) UnmergedNodes added in v1.0.0

func (info *TDigestInfo) UnmergedNodes() int64

UnmergedNodes - returns the unmerged nodes of TDigestInfo instance

func (*TDigestInfo) UnmergedWeight added in v1.0.0

func (info *TDigestInfo) UnmergedWeight() float64

UnmergedWeight - returns the unmerged weight of TDigestInfo instance

Jump to

Keyboard shortcuts

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