diff options
author | shacharPash <93581407+shacharPash@users.noreply.github.com> | 2023-01-11 12:57:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-11 12:57:56 +0200 |
commit | be84b930266932bc81fd5390e6963621ef0b97da (patch) | |
tree | 8b2d20e02eceb0d8834ef8c737b6b8efc2d0a499 | |
parent | 7dd73a306add608807c372a98d833b7cb3394681 (diff) | |
download | redis-py-be84b930266932bc81fd5390e6963621ef0b97da.tar.gz |
Add support to BF.CARD (#2545)
* Add support to BF.CARD
* Add Async test
* change to with pytest.raises
-rw-r--r-- | redis/commands/bf/__init__.py | 1 | ||||
-rw-r--r-- | redis/commands/bf/commands.py | 9 | ||||
-rw-r--r-- | tests/test_asyncio/test_bloom.py | 15 | ||||
-rw-r--r-- | tests/test_bloom.py | 15 |
4 files changed, 40 insertions, 0 deletions
diff --git a/redis/commands/bf/__init__.py b/redis/commands/bf/__init__.py index a448def..4da060e 100644 --- a/redis/commands/bf/__init__.py +++ b/redis/commands/bf/__init__.py @@ -198,6 +198,7 @@ class BFBloom(BFCommands, AbstractBloom): # 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 f6bf281..c45523c 100644 --- a/redis/commands/bf/commands.py +++ b/redis/commands/bf/commands.py @@ -11,6 +11,7 @@ BF_MEXISTS = "BF.MEXISTS" 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 @@ class BFCommands: """ # 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.""" diff --git a/tests/test_asyncio/test_bloom.py b/tests/test_asyncio/test_bloom.py index cac989f..9f4a805 100644 --- a/tests/test_asyncio/test_bloom.py +++ b/tests/test_asyncio/test_bloom.py @@ -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): diff --git a/tests/test_bloom.py b/tests/test_bloom.py index 13921bf..30d3219 100644 --- a/tests/test_bloom.py +++ b/tests/test_bloom.py @@ -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): |