From e20ebaf8fbfe34227d524bedaa8abfb57787e328 Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Thu, 26 Jan 2023 14:52:49 +0200 Subject: [PATCH] Extended Bloom Filters by implementing BF.LOADCHUNK command (#890) 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.SCANDUMP command * Added support for BF.LOADCHUNK command * Codestyle fixes * Sort alphabetical Co-authored-by: Vladyslav Vildanov Co-authored-by: Till Krüss --- src/ClientContextInterface.php | 1 + src/ClientInterface.php | 1 + src/Command/Redis/BloomFilter/BFLOADCHUNK.php | 28 +++++ .../Redis/BloomFilter/BFLOADCHUNK_Test.php | 108 ++++++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 src/Command/Redis/BloomFilter/BFLOADCHUNK.php create mode 100644 tests/Predis/Command/Redis/BloomFilter/BFLOADCHUNK_Test.php diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 868501f9..16314bef 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -45,6 +45,7 @@ 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 bfloadchunk(string $key, int $iterator, $data) * @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) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 10b56037..20a1e334 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -54,6 +54,7 @@ use Predis\Response\Status; * @method int bfadd(string $key, $item) * @method int bfexists(string $key, $item) * @method array bfinfo(string $key, string $modifier = '') + * @method Status bfloadchunk(string $key, int $iterator, $data) * @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) diff --git a/src/Command/Redis/BloomFilter/BFLOADCHUNK.php b/src/Command/Redis/BloomFilter/BFLOADCHUNK.php new file mode 100644 index 00000000..76a78e99 --- /dev/null +++ b/src/Command/Redis/BloomFilter/BFLOADCHUNK.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 testLoadChunkSuccessfullyRestoresBloomFilter(): void + { + $redis = $this->getClient(); + + $redis->bfadd('key', 'item1'); + + $chunks = []; + $iter = 0; + + while (true) { + [$iter, $data] = $redis->bfscandump('key', $iter); + + if ($iter === 0) { + break; + } + + $chunks[] = [$iter, $data]; + } + + $redis->flushall(); + + foreach ($chunks as $chunk) { + [$iter, $data] = $chunk; + $actualResponse = $redis->bfloadchunk('key', $iter, $data); + + $this->assertEquals('OK', $actualResponse); + } + + $this->assertSame(1, $redis->bfexists('key', 'item1')); + } + + /** + * @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('bfloadchunk_foo', 'bar'); + $redis->bfloadchunk('bfloadchunk_foo', 0, 'data'); + } +}