11package redis_bloom_go
22
33import (
4+ "errors"
45 "strings"
6+ "strconv"
57 "github.com/gomodule/redigo/redis"
68)
79
@@ -33,6 +35,19 @@ func NewClient(addr, name string, authPass *string) *Client {
3335 return ret
3436}
3537
38+ // Reserve - Creates an empty Bloom Filter with a given desired error ratio and initial capacity.
39+ // args:
40+ // key - the name of the filter
41+ // error_rate - the desired probability for false positives
42+ // capacity - the number of entries you intend to add to the filter
43+ func (client * Client ) Reserve (key string , error_rate float64 , capacity uint64 ) (err error ) {
44+ conn := client .Pool .Get ()
45+ defer conn .Close ()
46+ _ , err = conn .Do ("BF.RESERVE" , key , strconv .FormatFloat (error_rate , 'g' , 16 , 64 ), capacity )
47+ return err
48+ }
49+
50+
3651// Add - Add (or create and add) a new value to the filter
3752// args:
3853// key - the name of the filter
@@ -52,3 +67,35 @@ func (client *Client) Exists(key string, item string) (exists bool, err error) {
5267 defer conn .Close ()
5368 return redis .Bool (conn .Do ("BF.EXISTS" , key , item ))
5469}
70+
71+ // Info - Return information about key
72+ // args:
73+ // key - the name of the filter
74+ func (client * Client ) Info (key string ) (info map [string ]int64 , err error ) {
75+ conn := client .Pool .Get ()
76+ defer conn .Close ()
77+ result , err := conn .Do ("BF.INFO" , key )
78+ if err != nil {
79+ return nil , err
80+ }
81+
82+ values , err := redis .Values (result , nil )
83+ if err != nil {
84+ return nil , err
85+ }
86+ if len (values )% 2 != 0 {
87+ return nil , errors .New ("Info expects even number of values result" )
88+ }
89+ info = map [string ]int64 {}
90+ for i := 0 ; i < len (values ); i += 2 {
91+ key , err = redis .String (values [i ], nil )
92+ if err != nil {
93+ return nil , err
94+ }
95+ info [key ], err = redis .Int64 (values [i + 1 ], nil )
96+ if err != nil {
97+ return nil , err
98+ }
99+ }
100+ return info , nil
101+ }
0 commit comments