Skip to content

Commit 0fc2724

Browse files
Merge pull request #21 from dengliming/f_bf_insert
Add Support for BF.INSERT
2 parents 5e2f277 + 4177d5c commit 0fc2724

File tree

5 files changed

+72
-79
lines changed

5 files changed

+72
-79
lines changed

Gopkg.lock

Lines changed: 0 additions & 39 deletions
This file was deleted.

Gopkg.toml

Lines changed: 0 additions & 39 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func main() {
6464
| [BF.RESERVE](https://oss.redislabs.com/redisbloom/Bloom_Commands/#bfreserve) | [Reserve](https://godoc.org/github.com/RedisBloom/redisbloom-go#Client.Reserve) |
6565
| [BF.ADD](https://oss.redislabs.com/redisbloom/Bloom_Commands/#bfadd) | [Add](https://godoc.org/github.com/RedisBloom/redisbloom-go#Client.Add) |
6666
| [BF.MADD](https://oss.redislabs.com/redisbloom/Bloom_Commands/#bfmadd) | [BfAddMulti](https://godoc.org/github.com/RedisBloom/redisbloom-go#Client.BfAddMulti) |
67-
| [BF.INSERT](https://oss.redislabs.com/redisbloom/Bloom_Commands/#bfinsert) | N/A |
67+
| [BF.INSERT](https://oss.redislabs.com/redisbloom/Bloom_Commands/#bfinsert) | [BfInsert](https://godoc.org/github.com/RedisBloom/redisbloom-go#Client.BfInsert) |
6868
| [BF.EXISTS](https://oss.redislabs.com/redisbloom/Bloom_Commands/#bfexists) | [Exists](https://godoc.org/github.com/RedisBloom/redisbloom-go#Client.Exists) |
6969
| [BF.MEXISTS](https://oss.redislabs.com/redisbloom/Bloom_Commands/#bfmexists) | [BfExistsMulti](https://godoc.org/github.com/RedisBloom/redisbloom-go#Client.BfExistsMulti) |
7070
| [BF.SCANDUMP](https://oss.redislabs.com/redisbloom/Bloom_Commands/#bfscandump) | [BfScanDump](https://godoc.org/github.com/RedisBloom/redisbloom-go#Client.BfScanDump) |

client.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,44 @@ 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) (res []int64, err 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+
var resp []interface{}
180+
var innerRes int64
181+
resp, err = redis.Values(conn.Do("BF.INSERT", args...))
182+
if err != nil {
183+
return
184+
}
185+
for _, arrayPos := range resp {
186+
innerRes, err = redis.Int64(arrayPos, err)
187+
if err == nil {
188+
res = append(res, innerRes)
189+
} else {
190+
break
191+
}
192+
}
193+
return
194+
}
195+
158196
// Initializes a TopK with specified parameters.
159197
func (client *Client) TopkReserve(key string, topk int64, width int64, depth int64, decay float64) (string, error) {
160198
conn := client.Pool.Get()

client_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,39 @@ 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+
key_nocreate := "test_bf_insert_nocreate"
137+
key_noscaling := "test_bf_insert_noscaling"
138+
139+
ret, err := client.BfInsert(key, 1000, 0.1, -1, false, false, []string{"a"})
140+
assert.Nil(t, err)
141+
assert.Equal(t, 1, len(ret))
142+
assert.True(t, ret[0] > 0)
143+
existsResult, err := client.BfExistsMulti(key, []string{"a"})
144+
assert.Nil(t, err)
145+
assert.Equal(t, 1, len(existsResult))
146+
assert.Equal(t, int64(1), existsResult[0])
147+
148+
ret, err = client.BfInsert(key, 1000, 0.1, -1, false, false, []string{"a", "b"})
149+
assert.Nil(t, err)
150+
assert.Equal(t, 2, len(ret))
151+
152+
// Test for NOCREATE : If specified, indicates that the filter should not be created if it does not already exist
153+
_, err = client.BfInsert(key_nocreate, 1000, 0.1, -1, true, false, []string{"a"})
154+
assert.NotNil(t, err)
155+
156+
// Test NONSCALING : Prevents the filter from creating additional sub-filters if initial capacity is reached.
157+
ret, err = client.BfInsert(key_noscaling, 2, 0.1, -1, false, true, []string{"a", "b"})
158+
assert.Nil(t, err)
159+
assert.Equal(t, 2, len(ret))
160+
ret, err = client.BfInsert(key_noscaling, 2, 0.1, -1, false, true, []string{"c"})
161+
assert.NotNil(t, err)
162+
assert.Equal(t, 0, len(ret))
163+
assert.Equal(t, err.Error(), "ERR non scaling filter is full")
164+
}
165+
133166
func TestClient_TopkReserve(t *testing.T) {
134167
client.FlushAll()
135168
ret, err := client.TopkReserve("test_topk_reserve", 10, 2000, 7, 0.925)

0 commit comments

Comments
 (0)