From 549b4414c4cfef89c351ba36379bdcb71bc6d7f2 Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Thu, 26 Jan 2023 14:25:10 +0200 Subject: [PATCH] Extended Bloom Filters by implementing BF.RESERVE command (#888) 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 * Added support for BF.INFO command * Fixed arguments data provider * Fixed bug with incorrect tests skip * Added support for BF.RESERVE command * Added command description * Codestyle fixes * Added missing test Co-authored-by: Vladyslav Vildanov Co-authored-by: Till Krüss --- src/ClientContextInterface.php | 1 + src/ClientInterface.php | 1 + src/Command/Redis/BloomFilter/BFRESERVE.php | 49 ++++++ src/Command/Traits/BloomFilters/Expansion.php | 53 ++++++ .../Redis/BloomFilter/BFRESERVE_Test.php | 155 ++++++++++++++++++ .../Traits/BloomFilters/ExpansionTest.php | 72 ++++++++ 6 files changed, 331 insertions(+) create mode 100644 src/Command/Redis/BloomFilter/BFRESERVE.php create mode 100644 src/Command/Traits/BloomFilters/Expansion.php create mode 100644 tests/Predis/Command/Redis/BloomFilter/BFRESERVE_Test.php create mode 100644 tests/Predis/Command/Traits/BloomFilters/ExpansionTest.php diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index f59d7329..602a9087 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -47,6 +47,7 @@ use Predis\Command\CommandInterface; * @method $this bfinfo(string $key, string $modifier = '') * @method $this bfmadd(string $key, ...$item) * @method $this bfmexists(string $key, ...$item) + * @method $this bfreserve(string $key, float $errorRate, int $capacity, int $expansion = -1, bool $nonScaling = false) * @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 cf493a88..af00d98d 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -56,6 +56,7 @@ use Predis\Response\Status; * @method array bfinfo(string $key, string $modifier = '') * @method array bfmadd(string $key, ...$item) * @method array bfmexists(string $key, ...$item) + * @method Status bfreserve(string $key, float $errorRate, int $capacity, int $expansion = -1, bool $nonScaling = false) * @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/BFRESERVE.php b/src/Command/Redis/BloomFilter/BFRESERVE.php new file mode 100644 index 00000000..2364db48 --- /dev/null +++ b/src/Command/Redis/BloomFilter/BFRESERVE.php @@ -0,0 +1,49 @@ +setExpansion($arguments); + $this->filterArguments(); + } +} diff --git a/src/Command/Traits/BloomFilters/Expansion.php b/src/Command/Traits/BloomFilters/Expansion.php new file mode 100644 index 00000000..74d916f4 --- /dev/null +++ b/src/Command/Traits/BloomFilters/Expansion.php @@ -0,0 +1,53 @@ += $argumentsLength) { + parent::setArguments($arguments); + + return; + } + + if ($arguments[static::$expansionArgumentPositionOffset] === -1) { + array_splice($arguments, static::$expansionArgumentPositionOffset, 1, [false]); + parent::setArguments($arguments); + + return; + } + + if ($arguments[static::$expansionArgumentPositionOffset] < 1) { + throw new UnexpectedValueException('Wrong expansion argument value or position offset'); + } + + $argument = $arguments[static::$expansionArgumentPositionOffset]; + $argumentsBefore = array_slice($arguments, 0, static::$expansionArgumentPositionOffset); + $argumentsAfter = array_slice($arguments, static::$expansionArgumentPositionOffset + 1); + + parent::setArguments(array_merge( + $argumentsBefore, + [self::$expansionModifier], + [$argument], + $argumentsAfter + )); + } +} diff --git a/tests/Predis/Command/Redis/BloomFilter/BFRESERVE_Test.php b/tests/Predis/Command/Redis/BloomFilter/BFRESERVE_Test.php new file mode 100644 index 00000000..c4e90ffb --- /dev/null +++ b/tests/Predis/Command/Redis/BloomFilter/BFRESERVE_Test.php @@ -0,0 +1,155 @@ +getCommand(); + $command->setArguments($actualArguments); + + $this->assertSameValues($expectedArguments, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $this->assertSame(1, $this->getCommand()->parseResponse(1)); + } + + /** + * @group connected + * @dataProvider filtersProvider + * @param array $filter + * @param string $key + * @param string $modifier + * @param array $expectedModification + * @return void + * @requiresRedisBfVersion >= 1.0.0 + */ + public function testReserveCreatesBloomFilterWithCorrectConfiguration( + array $filter, + string $key, + string $modifier, + array $expectedModification + ): void { + $redis = $this->getClient(); + + $actualResponse = $redis->bfreserve(...$filter); + $this->assertEquals('OK', $actualResponse); + $this->assertSame($expectedModification, $redis->bfinfo($key, $modifier)); + } + + /** + * @group connected + * @return void + * @requiresRedisBfVersion 1.0.0 + */ + public function testThrowsExceptionOnUnexpectedValueGiven(): void + { + $redis = $this->getClient(); + + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage('Wrong expansion argument value or position offset'); + + $redis->bfreserve('key', 0.01, 2, 0); + } + + /** + * @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('bfreserve_foo', 'bar'); + $redis->bfreserve('bfreserve_foo', 0.01, 2); + } + + public function argumentsProvider(): array + { + return [ + 'with default arguments' => [ + ['key', 0.01, 2], + ['key', 0.01, 2], + ], + 'with EXPANSION argument' => [ + ['key', 0.01, 2, 2], + ['key', 0.01, 2, 'EXPANSION', 2], + ], + 'with NONSCALING modifier' => [ + ['key', 0.01, 2, -1, true], + ['key', 0.01, 2, 'NONSCALING'], + ], + 'with all arguments' => [ + ['key', 0.01, 2, 2, true], + ['key', 0.01, 2, 'EXPANSION', 2, 'NONSCALING'], + ], + ]; + } + + public function filtersProvider(): array + { + return [ + 'with default arguments' => [ + ['key', 0.01, 100], + 'key', + 'capacity', + [100], + ], + 'with modified expansion' => [ + ['key', 0.01, 100, 2], + 'key', + 'expansion', + [2], + ], + 'with NONSCALING modifier' => [ + ['key', 0.01, 100, -1, true], + 'key', + 'expansion', + [null], + ], + ]; + } +} diff --git a/tests/Predis/Command/Traits/BloomFilters/ExpansionTest.php b/tests/Predis/Command/Traits/BloomFilters/ExpansionTest.php new file mode 100644 index 00000000..0e61641e --- /dev/null +++ b/tests/Predis/Command/Traits/BloomFilters/ExpansionTest.php @@ -0,0 +1,72 @@ +testClass = new class() extends RedisCommand { + use Expansion; + + public static $expansionArgumentPositionOffset = 0; + + public function getId() + { + return 'test'; + } + }; + } + + /** + * @dataProvider argumentsProvider + * @param int $offset + * @param array $actualArguments + * @param array $expectedArguments + * @return void + */ + public function testReturnsCorrectArguments(int $offset, array $actualArguments, array $expectedArguments): void + { + $this->testClass::$expansionArgumentPositionOffset = $offset; + + $this->testClass->setArguments($actualArguments); + + $this->assertSame($expectedArguments, $this->testClass->getArguments()); + } + + /** + * @return void + */ + public function testThrowsExceptionOnUnexpectedValueGiven(): void + { + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage('Wrong expansion argument value or position offset'); + + $this->testClass->setArguments([-2]); + } + + public function argumentsProvider(): array + { + return [ + 'with wrong offset' => [2, ['argument1'], ['argument1']], + 'with value equals -1' => [0, [-1], [false]], + 'with correct value' => [0, [1], ['EXPANSION', 1]], + ]; + } +}