Skip to content

Commit 9a1cfef

Browse files
committed
Add Support for BF.INSERT
1 parent e00e2ac commit 9a1cfef

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

client.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,30 @@ func (client *Client) BfLoadChunk(key string, iter int64, data []byte) (string,
155155
return redis.String(conn.Do("BF.LOADCHUNK", key, iter, data))
156156
}
157157

158+
// This command will add one or more items to the bloom filter, by default creating it if it does not yet exist.
159+
func (client *Client) BfInsert(key string, cap int64, errorRatio float64, expansion int64, noCreate bool, nonScaling bool, items []string) ([]int64, error) {
160+
conn := client.Pool.Get()
161+
defer conn.Close()
162+
args := redis.Args{key}
163+
if cap > 0 {
164+
args = args.Add("CAPACITY", cap)
165+
}
166+
if errorRatio > 0 {
167+
args = args.Add("ERROR", errorRatio)
168+
}
169+
if expansion > 0 {
170+
args = args.Add("EXPANSION", expansion)
171+
}
172+
if noCreate {
173+
args = args.Add("NOCREATE")
174+
}
175+
if nonScaling {
176+
args = args.Add("NONSCALING")
177+
}
178+
args = args.Add("ITEMS").AddFlat(items)
179+
return redis.Int64s(conn.Do("BF.INSERT", args...))
180+
}
181+
158182
// Initializes a TopK with specified parameters.
159183
func (client *Client) TopkReserve(key string, topk int64, width int64, depth int64, decay float64) (string, error) {
160184
conn := client.Pool.Get()

client_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,19 @@ func TestClient_BfExistsMulti(t *testing.T) {
130130
assert.Equal(t, int64(0), existsResult[2])
131131
}
132132

133+
func TestClient_BfInsert(t *testing.T) {
134+
client.FlushAll()
135+
key := "test_bf_insert"
136+
ret, err := client.BfInsert(key, 1000, 0.1, -1, false, false, []string{"a"})
137+
assert.Nil(t, err)
138+
assert.Equal(t, 1, len(ret))
139+
assert.True(t, ret[0] > 0)
140+
existsResult, err := client.BfExistsMulti(key, []string{"a"})
141+
assert.Nil(t, err)
142+
assert.Equal(t, 1, len(existsResult))
143+
assert.Equal(t, int64(1), existsResult[0])
144+
}
145+
133146
func TestClient_TopkReserve(t *testing.T) {
134147
client.FlushAll()
135148
ret, err := client.TopkReserve("test_topk_reserve", 10, 2000, 7, 0.925)

0 commit comments

Comments
 (0)