Skip to content

Commit be84b93

Browse files
authored
Add support to BF.CARD (#2545)
* Add support to BF.CARD * Add Async test * change to with pytest.raises
1 parent 7dd73a3 commit be84b93

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

redis/commands/bf/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ def __init__(self, client, **kwargs):
198198
# BF_MEXISTS: spaceHolder,
199199
# BF_SCANDUMP: spaceHolder,
200200
# BF_LOADCHUNK: spaceHolder,
201+
# BF_CARD: spaceHolder,
201202
BF_INFO: BFInfo,
202203
}
203204

redis/commands/bf/commands.py

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
BF_SCANDUMP = "BF.SCANDUMP"
1212
BF_LOADCHUNK = "BF.LOADCHUNK"
1313
BF_INFO = "BF.INFO"
14+
BF_CARD = "BF.CARD"
1415

1516
CF_RESERVE = "CF.RESERVE"
1617
CF_ADD = "CF.ADD"
@@ -165,6 +166,14 @@ def info(self, key):
165166
""" # noqa
166167
return self.execute_command(BF_INFO, key)
167168

169+
def card(self, key):
170+
"""
171+
Returns the cardinality of a Bloom filter - number of items that were added to a Bloom filter and detected as unique
172+
(items that caused at least one bit to be set in at least one sub-filter).
173+
For more information see `BF.CARD <https://redis.io/commands/bf.card>`_.
174+
""" # noqa
175+
return self.execute_command(BF_CARD, key)
176+
168177

169178
class CFCommands:
170179
"""Cuckoo Filter commands."""

tests/test_asyncio/test_bloom.py

+15
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,21 @@ async def test_bf_info(modclient: redis.Redis):
149149
assert True
150150

151151

152+
@pytest.mark.redismod
153+
async def test_bf_card(modclient: redis.Redis):
154+
# return 0 if the key does not exist
155+
assert await modclient.bf().card("not_exist") == 0
156+
157+
# Store a filter
158+
assert await modclient.bf().add("bf1", "item_foo") == 1
159+
assert await modclient.bf().card("bf1") == 1
160+
161+
# Error when key is of a type other than Bloom filter.
162+
with pytest.raises(redis.ResponseError):
163+
await modclient.set("setKey", "value")
164+
await modclient.bf().card("setKey")
165+
166+
152167
# region Test Cuckoo Filter
153168
@pytest.mark.redismod
154169
async def test_cf_add_and_insert(modclient: redis.Redis):

tests/test_bloom.py

+15
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,21 @@ def test_bf_info(client):
165165
assert True
166166

167167

168+
@pytest.mark.redismod
169+
def test_bf_card(client):
170+
# return 0 if the key does not exist
171+
assert client.bf().card("not_exist") == 0
172+
173+
# Store a filter
174+
assert client.bf().add("bf1", "item_foo") == 1
175+
assert client.bf().card("bf1") == 1
176+
177+
# Error when key is of a type other than Bloom filter.
178+
with pytest.raises(redis.ResponseError):
179+
client.set("setKey", "value")
180+
client.bf().card("setKey")
181+
182+
168183
# region Test Cuckoo Filter
169184
@pytest.mark.redismod
170185
def test_cf_add_and_insert(client):

0 commit comments

Comments
 (0)