From a942905994abaeae95485a7a8bd288d9271897cd Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Mon, 9 Jan 2023 11:46:16 +0200 Subject: [PATCH 1/3] Add support to BF.CARD --- redis/commands/bf/__init__.py | 1 + redis/commands/bf/commands.py | 9 +++++++++ tests/test_bloom.py | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/redis/commands/bf/__init__.py b/redis/commands/bf/__init__.py index a448def46d..4da060e995 100644 --- a/redis/commands/bf/__init__.py +++ b/redis/commands/bf/__init__.py @@ -198,6 +198,7 @@ def __init__(self, client, **kwargs): # BF_MEXISTS: spaceHolder, # BF_SCANDUMP: spaceHolder, # BF_LOADCHUNK: spaceHolder, + # BF_CARD: spaceHolder, BF_INFO: BFInfo, } diff --git a/redis/commands/bf/commands.py b/redis/commands/bf/commands.py index f6bf281639..c45523c99b 100644 --- a/redis/commands/bf/commands.py +++ b/redis/commands/bf/commands.py @@ -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" @@ -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 `_. + """ # noqa + return self.execute_command(BF_CARD, key) + class CFCommands: """Cuckoo Filter commands.""" diff --git a/tests/test_bloom.py b/tests/test_bloom.py index 13921bfbd1..07cb5f0f4c 100644 --- a/tests/test_bloom.py +++ b/tests/test_bloom.py @@ -165,6 +165,25 @@ def test_bf_info(client): assert True +@pytest.mark.redismod +def test_bf_card(client): + expansion = 4 + # 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 + + try: + # Error when key is of a type other than Bloom filter. + client.set("setKey", "value") + client.bf().card("setKey") + assert False + except RedisError: + assert True + + # region Test Cuckoo Filter @pytest.mark.redismod def test_cf_add_and_insert(client): From 711d5058957fdb63105918c2233e5d69598385e2 Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Mon, 9 Jan 2023 11:58:00 +0200 Subject: [PATCH 2/3] Add Async test --- tests/test_asyncio/test_bloom.py | 18 ++++++++++++++++++ tests/test_bloom.py | 1 - 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/test_asyncio/test_bloom.py b/tests/test_asyncio/test_bloom.py index cac989f913..4a1b2b9cbf 100644 --- a/tests/test_asyncio/test_bloom.py +++ b/tests/test_asyncio/test_bloom.py @@ -149,6 +149,24 @@ 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 + + try: + # Error when key is of a type other than Bloom filter. + await modclient.set("setKey", "value") + await modclient.bf().card("setKey") + assert False + except RedisError: + assert True + + # region Test Cuckoo Filter @pytest.mark.redismod async def test_cf_add_and_insert(modclient: redis.Redis): diff --git a/tests/test_bloom.py b/tests/test_bloom.py index 07cb5f0f4c..f8feada146 100644 --- a/tests/test_bloom.py +++ b/tests/test_bloom.py @@ -167,7 +167,6 @@ def test_bf_info(client): @pytest.mark.redismod def test_bf_card(client): - expansion = 4 # return 0 if the key does not exist assert client.bf().card("not_exist") == 0 From 23b9af7f6f336e6b5915a015e63f9f0b7d766006 Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Wed, 11 Jan 2023 12:22:33 +0200 Subject: [PATCH 3/3] change to with pytest.raises --- tests/test_asyncio/test_bloom.py | 7 ++----- tests/test_bloom.py | 7 ++----- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/tests/test_asyncio/test_bloom.py b/tests/test_asyncio/test_bloom.py index 4a1b2b9cbf..9f4a805c4c 100644 --- a/tests/test_asyncio/test_bloom.py +++ b/tests/test_asyncio/test_bloom.py @@ -158,13 +158,10 @@ async def test_bf_card(modclient: redis.Redis): assert await modclient.bf().add("bf1", "item_foo") == 1 assert await modclient.bf().card("bf1") == 1 - try: - # Error when key is of a type other than Bloom filter. + # 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") - assert False - except RedisError: - assert True # region Test Cuckoo Filter diff --git a/tests/test_bloom.py b/tests/test_bloom.py index f8feada146..30d3219404 100644 --- a/tests/test_bloom.py +++ b/tests/test_bloom.py @@ -174,13 +174,10 @@ def test_bf_card(client): assert client.bf().add("bf1", "item_foo") == 1 assert client.bf().card("bf1") == 1 - try: - # Error when key is of a type other than Bloom filter. + # Error when key is of a type other than Bloom filter. + with pytest.raises(redis.ResponseError): client.set("setKey", "value") client.bf().card("setKey") - assert False - except RedisError: - assert True # region Test Cuckoo Filter