diff --git a/src/ClientConfiguration.php b/src/ClientConfiguration.php index ef4dcb36..7f0087f4 100644 --- a/src/ClientConfiguration.php +++ b/src/ClientConfiguration.php @@ -22,6 +22,7 @@ class ClientConfiguration ['name' => 'Json', 'commandPrefix' => 'JSON'], ['name' => 'BloomFilter', 'commandPrefix' => 'BF'], ['name' => 'CuckooFilter', 'commandPrefix' => 'CF'], + ['name' => 'CountMinSketch', 'commandPrefix' => 'CMS'], ], ]; diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index c6708fd2..225657dd 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -70,7 +70,9 @@ use Predis\Command\CommandInterface; * @method $this cfinsert(string $key, int $capacity = -1, bool $noCreate = false, string ...$item) * @method $this cfinsertnx(string $key, int $capacity = -1, bool $noCreate = false, string ...$item) * @method $this cfreserve(string $key, int $capacity, int $bucketSize = -1, int $maxIterations = -1, int $expansion = -1) - * @method $this cfscandump(string $key, int $iterator) * @method $this decr($key) + * @method $this cfscandump(string $key, int $iterator) + * @method $this cmsinfo(string $key) + * @method $this cmsinitbydim(string $key, int $width, int $depth) * @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 6d000d1d..a593e8ab 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -80,6 +80,8 @@ use Predis\Response\Status; * @method array cfinsertnx(string $key, int $capacity = -1, bool $noCreate = false, string ...$item) * @method Status cfreserve(string $key, int $capacity, int $bucketSize = -1, int $maxIterations = -1, int $expansion = -1) * @method array cfscandump(string $key, int $iterator) + * @method array cmsinfo(string $key) + * @method Status cmsinitbydim(string $key, int $width, int $depth) * @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/CMSINFO.php b/src/Command/Redis/CountMinSketch/CMSINFO.php new file mode 100644 index 00000000..ed6d2249 --- /dev/null +++ b/src/Command/Redis/CountMinSketch/CMSINFO.php @@ -0,0 +1,45 @@ + 1) { + $result = []; + + for ($i = 0, $iMax = count($data); $i < $iMax; ++$i) { + if (array_key_exists($i + 1, $data)) { + $result[(string) $data[$i]] = $data[++$i]; + } + } + + return $result; + } + + return $data; + } +} diff --git a/src/Command/Redis/CountMinSketch/CMSINITBYDIM.php b/src/Command/Redis/CountMinSketch/CMSINITBYDIM.php new file mode 100644 index 00000000..8f1f23ff --- /dev/null +++ b/src/Command/Redis/CountMinSketch/CMSINITBYDIM.php @@ -0,0 +1,28 @@ +getCommand(); + $command->setArguments($actualArguments); + + $this->assertSameValues($expectedArguments, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $actualResponse = ['width', 2000, 'depth', 10, 'count', 0]; + $expectedResponse = ['width' => 2000, 'depth' => 10, 'count' => 0]; + + $this->assertSame($expectedResponse, $this->getCommand()->parseResponse($actualResponse)); + } + + /** + * @group connected + * @return void + * @requiresRedisBfVersion >= 2.0.0 + */ + public function testReturnsInfoAboutGivenCountMinSketch(): void + { + $redis = $this->getClient(); + $expectedResponse = ['width' => 2000, 'depth' => 10, 'count' => 0]; + + $redis->cmsinitbydim('key', 2000, 10); + + $actualResponse = $redis->cmsinfo('key'); + + $this->assertSame($expectedResponse, $actualResponse); + } + + /** + * @group connected + * @requiresRedisBfVersion >= 2.0.0 + */ + public function testThrowsExceptionOnNonExistingKey(): void + { + $this->expectException(ServerException::class); + $this->expectExceptionMessage('CMS: key does not exist'); + + $redis = $this->getClient(); + + $redis->cmsinfo('cmsinfo_foo'); + } +} diff --git a/tests/Predis/Command/Redis/CountMinSketch/CMSINITBYDIM_Test.php b/tests/Predis/Command/Redis/CountMinSketch/CMSINITBYDIM_Test.php new file mode 100644 index 00000000..b13d1261 --- /dev/null +++ b/tests/Predis/Command/Redis/CountMinSketch/CMSINITBYDIM_Test.php @@ -0,0 +1,89 @@ +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 testInitializeCountMinSketchWithGivenConfiguration(): void + { + $redis = $this->getClient(); + + $actualResponse = $redis->cmsinitbydim('key', 2000, 10); + $info = $redis->cmsinfo('key'); + + $this->assertEquals('OK', $actualResponse); + $this->assertSame(2000, $info['width']); + $this->assertSame(10, $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->cmsinitbydim('cmsinitbydim_foo', 2000, 10); + } +}