From ca498b67a1f771e40408a6b6477811d1ecc191d2 Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Wed, 25 Jan 2023 18:48:14 +0200 Subject: [PATCH] Added support for Cuckoo Filter, added CF.ADD and CF.EXISTS commands (#907) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added CommandResolver, moved resolve command logic there, added ClientConfiguration object * Fixed test, added dependecies * Added resolved command to commands array * Used aggregation approach for modules * Added decorator to check Redis JSON module version * Added support for JSON.SET and JSON.GET commands * Added separate workflow for redis-stack tests * Changed docker imange name to correct one * Fixed indentation * Added test coverage for JSON.GET command * Changed module version resolving using annotations mapping * Re-written CommandResolver test * Update ClientInterface.php * Changes to CI, readme, removed unused modules from configuration * Fixed build badge URL * Refactored annotation check to be generic for each module * Added support for BF.ADD and BF.EXISTS commands * Fixed bug with incorrect tests skip * Changed module name according to original naming * Added support for CF.ADD and CF.EXISTS commands * Removed old files * Revert old changes Co-authored-by: Vladyslav Vildanov Co-authored-by: Till Krüss --- src/ClientConfiguration.php | 1 + src/ClientContextInterface.php | 2 + src/ClientInterface.php | 2 + src/Command/Redis/CuckooFilter/CFADD.php | 28 ++++++ src/Command/Redis/CuckooFilter/CFEXISTS.php | 28 ++++++ .../Command/Redis/CuckooFilter/CFADD_Test.php | 86 +++++++++++++++++++ .../Redis/CuckooFilter/CFEXISTS_Test.php | 71 +++++++++++++++ 7 files changed, 218 insertions(+) create mode 100644 src/Command/Redis/CuckooFilter/CFADD.php create mode 100644 src/Command/Redis/CuckooFilter/CFEXISTS.php create mode 100644 tests/Predis/Command/Redis/CuckooFilter/CFADD_Test.php create mode 100644 tests/Predis/Command/Redis/CuckooFilter/CFEXISTS_Test.php diff --git a/src/ClientConfiguration.php b/src/ClientConfiguration.php index 22dbf868..ef4dcb36 100644 --- a/src/ClientConfiguration.php +++ b/src/ClientConfiguration.php @@ -21,6 +21,7 @@ class ClientConfiguration 'modules' => [ ['name' => 'Json', 'commandPrefix' => 'JSON'], ['name' => 'BloomFilter', 'commandPrefix' => 'BF'], + ['name' => 'CuckooFilter', 'commandPrefix' => 'CF'], ], ]; diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index dd9bff9e..188814cb 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -52,6 +52,8 @@ use Predis\Command\CommandInterface; * @method $this bzpopmax(array $keys, int $timeout) * @method $this bzpopmin(array $keys, int $timeout) * @method $this bzmpop(int $timeout, array $keys, string $modifier = 'min', int $count = 1) + * @method $this cfadd(string $key, $item) + * @method $this cfexists(string $key, $item) * @method $this decr($key) * @method $this decrby($key, $decrement) * @method $this failover(?To $to = null, bool $abort = false, int $timeout = -1) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 000a027a..53e5425c 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -61,6 +61,8 @@ use Predis\Response\Status; * @method array bzpopmax(array $keys, int $timeout) * @method array bzpopmin(array $keys, int $timeout) * @method array bzmpop(int $timeout, array $keys, string $modifier = 'min', int $count = 1) + * @method int cfadd(string $key, $item) + * @method int cfexists(string $key, $item) * @method int decr(string $key) * @method int decrby(string $key, int $decrement) * @method Status failover(?To $to = null, bool $abort = false, int $timeout = -1) diff --git a/src/Command/Redis/CuckooFilter/CFADD.php b/src/Command/Redis/CuckooFilter/CFADD.php new file mode 100644 index 00000000..92d05889 --- /dev/null +++ b/src/Command/Redis/CuckooFilter/CFADD.php @@ -0,0 +1,28 @@ +getCommand(); + $command->setArguments($actualArguments); + + $this->assertSameValues($expectedArguments, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $this->assertSame(1, $this->getCommand()->parseResponse(1)); + } + + /** + * @group connected + * @return void + * @requiresRedisBfVersion >= 1.0.0 + */ + public function testAddItemToCuckooFilter(): void + { + $redis = $this->getClient(); + + $actualResponse = $redis->cfadd('key', 'item'); + $this->assertSame(1, $actualResponse); + $this->assertSame(1, $redis->cfexists('key', 'item')); + } + + /** + * @group connected + * @requiresRedisBfVersion >= 1.0.0 + */ + public function testThrowsExceptionOnWrongType(): void + { + $this->expectException(ServerException::class); + $this->expectExceptionMessage('Operation against a key holding the wrong kind of value'); + + $redis = $this->getClient(); + + $redis->set('cfadd_foo', 'bar'); + $redis->cfadd('cfadd_foo', 'foo'); + } +} diff --git a/tests/Predis/Command/Redis/CuckooFilter/CFEXISTS_Test.php b/tests/Predis/Command/Redis/CuckooFilter/CFEXISTS_Test.php new file mode 100644 index 00000000..492c20d9 --- /dev/null +++ b/tests/Predis/Command/Redis/CuckooFilter/CFEXISTS_Test.php @@ -0,0 +1,71 @@ +getCommand(); + $command->setArguments($actualArguments); + + $this->assertSameValues($expectedArguments, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $this->assertSame(1, $this->getCommand()->parseResponse(1)); + } + + /** + * @group connected + * @return void + * @requiresRedisBfVersion >= 1.0.0 + */ + public function testExistsReturnsExistingItemWithinCuckooFilter(): void + { + $redis = $this->getClient(); + + $redis->cfadd('key', 'item'); + + $this->assertSame(1, $redis->cfexists('key', 'item')); + $this->assertSame(0, $redis->cfexists('non-existing key', 'item')); + } +}