Skip to content

Commit 4f23c66

Browse files
committed
Add Support for BF.MEXISTS BF.MADD
1 parent ce792d3 commit 4f23c66

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

client.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ func NewClientFromPool(pool *redis.Pool, name string) *Client {
4444
return ret
4545
}
4646

47-
// Reserve - Creates an empty Bloom Filter with a given desired error ratio and initial capacity.
47+
// Reserve - Creates an empty Bloom Filter with a given desired error ratio and initial capacity.
4848
// args:
4949
// key - the name of the filter
5050
// error_rate - the desired probability for false positives
51-
// capacity - the number of entries you intend to add to the filter
51+
// capacity - the number of entries you intend to add to the filter
5252
func (client *Client) Reserve(key string, error_rate float64, capacity uint64) (err error) {
5353
conn := client.Pool.Get()
5454
defer conn.Close()
@@ -58,27 +58,27 @@ func (client *Client) Reserve(key string, error_rate float64, capacity uint64) (
5858

5959
// Add - Add (or create and add) a new value to the filter
6060
// args:
61-
// key - the name of the filter
62-
// item - the item to add
61+
// key - the name of the filter
62+
// item - the item to add
6363
func (client *Client) Add(key string, item string) (exists bool, err error) {
6464
conn := client.Pool.Get()
6565
defer conn.Close()
6666
return redis.Bool(conn.Do("BF.ADD", key, item))
6767
}
6868

69-
// Exists - Determines whether an item may exist in the Bloom Filter or not.
69+
// Exists - Determines whether an item may exist in the Bloom Filter or not.
7070
// args:
71-
// key - the name of the filter
72-
// item - the item to check for
71+
// key - the name of the filter
72+
// item - the item to check for
7373
func (client *Client) Exists(key string, item string) (exists bool, err error) {
7474
conn := client.Pool.Get()
7575
defer conn.Close()
7676
return redis.Bool(conn.Do("BF.EXISTS", key, item))
7777
}
7878

79-
// Info - Return information about key
79+
// Info - Return information about key
8080
// args:
81-
// key - the name of the filter
81+
// key - the name of the filter
8282
func (client *Client) Info(key string) (info map[string]int64, err error) {
8383
conn := client.Pool.Get()
8484
defer conn.Close()
@@ -107,3 +107,27 @@ func (client *Client) Info(key string) (info map[string]int64, err error) {
107107
}
108108
return info, nil
109109
}
110+
111+
// BfAddMulti - Adds one or more items to the Bloom Filter, creating the filter if it does not yet exist.
112+
// args:
113+
// key - the name of the filter
114+
// item - One or more items to add
115+
func (client *Client) BfAddMulti(key string, items []string) ([]int64, error) {
116+
conn := client.Pool.Get()
117+
defer conn.Close()
118+
args := redis.Args{key}.AddFlat(items)
119+
result, err := conn.Do("BF.MADD", args...)
120+
return redis.Int64s(result, err)
121+
}
122+
123+
// BfExistsMulti - Determines if one or more items may exist in the filter or not.
124+
// args:
125+
// key - the name of the filter
126+
// item - one or more items to check
127+
func (client *Client) BfExistsMulti(key string, items []string) ([]int64, error) {
128+
conn := client.Pool.Get()
129+
defer conn.Close()
130+
args := redis.Args{key}.AddFlat(items)
131+
result, err := conn.Do("BF.MEXISTS", args...)
132+
return redis.Int64s(result, err)
133+
}

client_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,25 @@ func TestExists(t *testing.T) {
107107
assert.Nil(t, err)
108108
assert.False(t, exists)
109109
}
110+
111+
func TestClient_BfAddMulti(t *testing.T) {
112+
client.FlushAll()
113+
ret, err := client.BfAddMulti("test_add_multi", []string{"a", "b", "c"})
114+
assert.Nil(t, err)
115+
assert.NotNil(t, ret)
116+
}
117+
118+
func TestClient_BfExistsMulti(t *testing.T) {
119+
client.FlushAll()
120+
key := "test_exists_multi"
121+
ret, err := client.BfAddMulti(key, []string{"a", "b", "c"})
122+
assert.Nil(t, err)
123+
assert.NotNil(t, ret)
124+
125+
existsResult, err := client.BfExistsMulti(key, []string{"a", "b", "notexists"})
126+
assert.Nil(t, err)
127+
assert.Equal(t, 3, len(existsResult))
128+
assert.Equal(t, int64(1), existsResult[0])
129+
assert.Equal(t, int64(1), existsResult[1])
130+
assert.Equal(t, int64(0), existsResult[2])
131+
}

0 commit comments

Comments
 (0)