Skip to content

Add support to BF.CARD #2545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions redis/commands/bf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def __init__(self, client, **kwargs):
# BF_MEXISTS: spaceHolder,
# BF_SCANDUMP: spaceHolder,
# BF_LOADCHUNK: spaceHolder,
# BF_CARD: spaceHolder,
BF_INFO: BFInfo,
}

Expand Down
9 changes: 9 additions & 0 deletions redis/commands/bf/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
BF_SCANDUMP = "BF.SCANDUMP"
BF_LOADCHUNK = "BF.LOADCHUNK"
BF_INFO = "BF.INFO"
BF_CARD = "BF.CARD"

CF_RESERVE = "CF.RESERVE"
CF_ADD = "CF.ADD"
Expand Down Expand Up @@ -165,6 +166,14 @@ def info(self, key):
""" # noqa
return self.execute_command(BF_INFO, key)

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


class CFCommands:
"""Cuckoo Filter commands."""
Expand Down
15 changes: 15 additions & 0 deletions tests/test_asyncio/test_bloom.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ async def test_bf_info(modclient: redis.Redis):
assert True


@pytest.mark.redismod
async def test_bf_card(modclient: redis.Redis):
# return 0 if the key does not exist
assert await modclient.bf().card("not_exist") == 0

# Store a filter
assert await modclient.bf().add("bf1", "item_foo") == 1
assert await modclient.bf().card("bf1") == 1

# Error when key is of a type other than Bloom filter.
with pytest.raises(redis.ResponseError):
await modclient.set("setKey", "value")
await modclient.bf().card("setKey")


# region Test Cuckoo Filter
@pytest.mark.redismod
async def test_cf_add_and_insert(modclient: redis.Redis):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_bloom.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ def test_bf_info(client):
assert True


@pytest.mark.redismod
def test_bf_card(client):
# return 0 if the key does not exist
assert client.bf().card("not_exist") == 0

# Store a filter
assert client.bf().add("bf1", "item_foo") == 1
assert client.bf().card("bf1") == 1

# Error when key is of a type other than Bloom filter.
with pytest.raises(redis.ResponseError):
client.set("setKey", "value")
client.bf().card("setKey")


# region Test Cuckoo Filter
@pytest.mark.redismod
def test_cf_add_and_insert(client):
Expand Down