From e831c71cc64023887cdded39ee4e1adde52f0c4d Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Thu, 2 Feb 2023 14:50:39 +0200 Subject: [PATCH] Extended Count-Min Sketch support by implementing CMS.INITBYPROB command (#1090) * Added support for CMS.INITBYPROB command * Codestyle fixes --------- Co-authored-by: Vladyslav Vildanov --- src/ClientContextInterface.php | 1 + src/ClientInterface.php | 1 + .../Redis/CountMinSketch/CMSINITBYPROB.php | 28 ++++++ .../CountMinSketch/CMSINITBYPROB_Test.php | 89 +++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 src/Command/Redis/CountMinSketch/CMSINITBYPROB.php create mode 100644 tests/Predis/Command/Redis/CountMinSketch/CMSINITBYPROB_Test.php diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 225657dd..dac8b5c2 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -73,6 +73,7 @@ use Predis\Command\CommandInterface; * @method $this cfscandump(string $key, int $iterator) * @method $this cmsinfo(string $key) * @method $this cmsinitbydim(string $key, int $width, int $depth) + * @method $this cmsinitbyprob(string $key, float $errorRate, float $probability) * @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 a593e8ab..98febcee 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -82,6 +82,7 @@ use Predis\Response\Status; * @method array cfscandump(string $key, int $iterator) * @method array cmsinfo(string $key) * @method Status cmsinitbydim(string $key, int $width, int $depth) + * @method Status cmsinitbyprob(string $key, float $errorRate, float $probability) * @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/CountMinSketch/CMSINITBYPROB.php b/src/Command/Redis/CountMinSketch/CMSINITBYPROB.php new file mode 100644 index 00000000..76e0a788 --- /dev/null +++ b/src/Command/Redis/CountMinSketch/CMSINITBYPROB.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 >= 2.0.0 + */ + public function testInitializeCountMinSketchWithDesiredErrorRateAndProbability(): void + { + $redis = $this->getClient(); + + $actualResponse = $redis->cmsinitbyprob('key', 0.001, 0.01); + $info = $redis->cmsinfo('key'); + + $this->assertEquals('OK', $actualResponse); + $this->assertSame(2000, $info['width']); + $this->assertSame(7, $info['depth']); + } + + /** + * @group connected + * @requiresRedisBfVersion >= 2.0.0 + */ + public function testThrowsExceptionOnAlreadyExistingKey(): void + { + $this->expectException(ServerException::class); + $this->expectExceptionMessage('CMS: key already exists'); + + $redis = $this->getClient(); + + $redis->set('cmsinitbydim_foo', 'bar'); + $redis->cmsinitbyprob('cmsinitbydim_foo', 0.001, 0.01); + } +}