From f82bee9a81aa7f9917466cb045cbe6a6e64e2e9f Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Thu, 26 Jan 2023 11:19:31 +0200 Subject: [PATCH] Extend Bloom filters by imlplementing BFMADD and BFMEXISTS commands (#887) 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 * Added support for BF.MADD and BF.MEXISTS commands * Removed old file * Revert changes Co-authored-by: Vladyslav Vildanov Co-authored-by: Till Krüss --- src/ClientContextInterface.php | 2 + src/ClientInterface.php | 2 + src/Command/Redis/BloomFilter/BFMADD.php | 29 ++++++ src/Command/Redis/BloomFilter/BFMEXISTS.php | 28 ++++++ .../Command/Redis/BloomFilter/BFMADD_Test.php | 90 +++++++++++++++++++ .../Redis/BloomFilter/BFMEXISTS_Test.php | 73 +++++++++++++++ 6 files changed, 224 insertions(+) create mode 100644 src/Command/Redis/BloomFilter/BFMADD.php create mode 100644 src/Command/Redis/BloomFilter/BFMEXISTS.php create mode 100644 tests/Predis/Command/Redis/BloomFilter/BFMADD_Test.php create mode 100644 tests/Predis/Command/Redis/BloomFilter/BFMEXISTS_Test.php diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index bc588913..f59d7329 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -45,6 +45,8 @@ use Predis\Command\CommandInterface; * @method $this bfadd(string $key, $item) * @method $this bfexists(string $key, $item) * @method $this bfinfo(string $key, string $modifier = '') + * @method $this bfmadd(string $key, ...$item) + * @method $this bfmexists(string $key, ...$item) * @method $this bitcount($key, $start = null, $end = null) * @method $this bitop($operation, $destkey, $key) * @method $this bitfield($key, $subcommand, ...$subcommandArg) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 9d013c6b..cf493a88 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -54,6 +54,8 @@ use Predis\Response\Status; * @method int bfadd(string $key, $item) * @method int bfexists(string $key, $item) * @method array bfinfo(string $key, string $modifier = '') + * @method array bfmadd(string $key, ...$item) + * @method array bfmexists(string $key, ...$item) * @method int bitcount(string $key, $start = null, $end = null) * @method int bitop($operation, $destkey, $key) * @method array|null bitfield(string $key, $subcommand, ...$subcommandArg) diff --git a/src/Command/Redis/BloomFilter/BFMADD.php b/src/Command/Redis/BloomFilter/BFMADD.php new file mode 100644 index 00000000..16466465 --- /dev/null +++ b/src/Command/Redis/BloomFilter/BFMADD.php @@ -0,0 +1,29 @@ +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 + */ + public function testAddGivenItemsIntoBloomFilter(): void + { + $redis = $this->getClient(); + + $actualResponse = $redis->bfmadd('key', 'item1', 'item2'); + $this->assertSame([1, 1], $actualResponse); + $this->assertSame([1, 1], $redis->bfmexists('key', 'item1', 'item2')); + } + + /** + * @group connected + * @requiresRedisBfVersion >= 1.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('bfmadd_foo', 'bar'); + $redis->bfmadd('bfmadd_foo', 'foo'); + } +} diff --git a/tests/Predis/Command/Redis/BloomFilter/BFMEXISTS_Test.php b/tests/Predis/Command/Redis/BloomFilter/BFMEXISTS_Test.php new file mode 100644 index 00000000..1aa02162 --- /dev/null +++ b/tests/Predis/Command/Redis/BloomFilter/BFMEXISTS_Test.php @@ -0,0 +1,73 @@ +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 + */ + public function testExistsReturnsExistingItemsWithinBloomFilter(): void + { + $redis = $this->getClient(); + + $redis->bfmadd('key', 'item1', 'item2'); + $this->assertSame([1, 1], $redis->bfmexists('key', 'item1', 'item2')); + } +}