From 7b7e93a53fabf6b2b31ba1ffb333c7fded4d12f7 Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Wed, 25 Jan 2023 17:37:21 +0200 Subject: [PATCH] Added support for Bloom Filters module, added support for BF.ADD, BF.EXISTS commands (#874) 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 * Revert some old changes * Fixed misspelling 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/BloomFilter/BFADD.php | 29 ++++++ src/Command/Redis/BloomFilter/BFEXISTS.php | 28 ++++++ tests/PHPUnit/PredisTestCase.php | 2 + .../Command/Redis/BloomFilter/BFADD_Test.php | 90 +++++++++++++++++++ .../Redis/BloomFilter/BFEXISTS_Test.php | 75 ++++++++++++++++ 8 files changed, 229 insertions(+) create mode 100644 src/Command/Redis/BloomFilter/BFADD.php create mode 100644 src/Command/Redis/BloomFilter/BFEXISTS.php create mode 100644 tests/Predis/Command/Redis/BloomFilter/BFADD_Test.php create mode 100644 tests/Predis/Command/Redis/BloomFilter/BFEXISTS_Test.php diff --git a/src/ClientConfiguration.php b/src/ClientConfiguration.php index 20653d11..22dbf868 100644 --- a/src/ClientConfiguration.php +++ b/src/ClientConfiguration.php @@ -20,6 +20,7 @@ class ClientConfiguration private static $config = [ 'modules' => [ ['name' => 'Json', 'commandPrefix' => 'JSON'], + ['name' => 'BloomFilter', 'commandPrefix' => 'BF'], ], ]; diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 779b88c6..dd9bff9e 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -42,6 +42,8 @@ use Predis\Command\CommandInterface; * @method $this ttl($key) * @method $this type($key) * @method $this append($key, $value) + * @method $this bfadd(string $key, $item) + * @method $this bfexists(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 52cccf95..000a027a 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -51,6 +51,8 @@ use Predis\Response\Status; * @method int ttl(string $key) * @method mixed type(string $key) * @method int append(string $key, $value) + * @method int bfadd(string $key, $item) + * @method int bfexists(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/BFADD.php b/src/Command/Redis/BloomFilter/BFADD.php new file mode 100644 index 00000000..af30de08 --- /dev/null +++ b/src/Command/Redis/BloomFilter/BFADD.php @@ -0,0 +1,29 @@ + ['annotation' => 'requiresRedisJsonVersion', 'name' => 'ReJSON'], + 'bloomFilter' => ['annotation' => 'requiresRedisBfVersion', 'name' => 'bf'], ]; /** @@ -46,6 +47,7 @@ abstract class PredisTestCase extends \PHPUnit\Framework\TestCase { $this->checkRequiredRedisServerVersion(); $this->checkRequiredRedisModuleVersion('json'); + $this->checkRequiredRedisModuleVersion('bloomFilter'); } /** diff --git a/tests/Predis/Command/Redis/BloomFilter/BFADD_Test.php b/tests/Predis/Command/Redis/BloomFilter/BFADD_Test.php new file mode 100644 index 00000000..f1af9f90 --- /dev/null +++ b/tests/Predis/Command/Redis/BloomFilter/BFADD_Test.php @@ -0,0 +1,90 @@ +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 testAddGivenItemIntoBloomFilter(): void + { + $redis = $this->getClient(); + + $actualResponse = $redis->bfadd('key', 'item'); + $this->assertSame(1, $actualResponse); + $this->assertSame(1, $redis->bfexists('key', 'item')); + } + + /** + * @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('bfadd_foo', 'bar'); + $redis->bfadd('bfadd_foo', 'foo'); + } +} diff --git a/tests/Predis/Command/Redis/BloomFilter/BFEXISTS_Test.php b/tests/Predis/Command/Redis/BloomFilter/BFEXISTS_Test.php new file mode 100644 index 00000000..6bc0ca39 --- /dev/null +++ b/tests/Predis/Command/Redis/BloomFilter/BFEXISTS_Test.php @@ -0,0 +1,75 @@ +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 testExistsReturnsExistingItemWithinBloomFilter(): void + { + $redis = $this->getClient(); + + $redis->bfadd('key', 'item'); + + $this->assertSame(1, $redis->bfexists('key', 'item')); + $this->assertSame(0, $redis->bfexists('key', 'non-existing')); + } +}