Skip to content

Commit 31fa4cd

Browse files
committed
[add] added BF.INSERT tests for NONSCALING and and NOCREATE
1 parent ab021b8 commit 31fa4cd

File tree

4 files changed

+36
-80
lines changed

4 files changed

+36
-80
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.

client.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func (client *Client) BfLoadChunk(key string, iter int64, data []byte) (string,
156156
}
157157

158158
// 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) {
159+
func (client *Client) BfInsert(key string, cap int64, errorRatio float64, expansion int64, noCreate bool, nonScaling bool, items []string) (res []int64, err error) {
160160
conn := client.Pool.Get()
161161
defer conn.Close()
162162
args := redis.Args{key}
@@ -176,7 +176,21 @@ func (client *Client) BfInsert(key string, cap int64, errorRatio float64, expans
176176
args = args.Add("NONSCALING")
177177
}
178178
args = args.Add("ITEMS").AddFlat(items)
179-
return redis.Int64s(conn.Do("BF.INSERT", args...))
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
180194
}
181195

182196
// Initializes a TopK with specified parameters.

client_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ func TestClient_BfExistsMulti(t *testing.T) {
133133
func TestClient_BfInsert(t *testing.T) {
134134
client.FlushAll()
135135
key := "test_bf_insert"
136+
key_nocreate := "test_bf_insert_nocreate"
137+
key_noscaling := "test_bf_insert_noscaling"
138+
136139
ret, err := client.BfInsert(key, 1000, 0.1, -1, false, false, []string{"a"})
137140
assert.Nil(t, err)
138141
assert.Equal(t, 1, len(ret))
@@ -141,6 +144,23 @@ func TestClient_BfInsert(t *testing.T) {
141144
assert.Nil(t, err)
142145
assert.Equal(t, 1, len(existsResult))
143146
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")
144164
}
145165

146166
func TestClient_TopkReserve(t *testing.T) {

0 commit comments

Comments
 (0)