diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index dac8b5c2..13d02328 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -71,9 +71,11 @@ use Predis\Command\CommandInterface; * @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 cmsincrby(string $key, string|int...$itemIncrementDictionary) * @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 cmsquery(string $key, string ...$item) * @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 98febcee..5cc16e26 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -80,9 +80,11 @@ 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 cmsincrby(string $key, string|int...$itemIncrementDictionary) * @method array cmsinfo(string $key) * @method Status cmsinitbydim(string $key, int $width, int $depth) * @method Status cmsinitbyprob(string $key, float $errorRate, float $probability) + * @method array cmsquery(string $key, string ...$item) * @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/CMSINCRBY.php b/src/Command/Redis/CountMinSketch/CMSINCRBY.php new file mode 100644 index 00000000..6ce43385 --- /dev/null +++ b/src/Command/Redis/CountMinSketch/CMSINCRBY.php @@ -0,0 +1,29 @@ +getCommand(); + $command->setArguments($actualArguments); + + $this->assertSameValues($expectedArguments, $command->getArguments()); + } + + /** + * @group connected + * @dataProvider sketchesProvider + * @param array $incrementArguments + * @param array $queryArguments + * @param array $expectedResponse + * @return void + * @requiresRedisBfVersion >= 2.0.0 + */ + public function testIncrementGivenItemsWithinCountMinSketch( + array $incrementArguments, + array $queryArguments, + array $expectedResponse + ): void { + $redis = $this->getClient(); + + $redis->cmsinitbydim('key', 2000, 7); + + $actualResponse = $redis->cmsincrby(...$incrementArguments); + $queryResponse = $redis->cmsquery(...$queryArguments); + + $this->assertSame($expectedResponse, $actualResponse); + $this->assertSame($expectedResponse, $queryResponse); + } + + /** + * @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->cmsincrby('cmsincrby_foo', 'item1', 1); + } + + public function sketchesProvider(): array + { + return [ + 'with single item' => [ + ['key', 'item1', 1], + ['key', 'item1'], + [1], + ], + 'with multiple items' => [ + ['key', 'item1', 1, 'item2', 1], + ['key', 'item1', 'item2'], + [1, 1], + ], + 'with incrementing on X' => [ + ['key', 'item1', 2, 'item2', 5], + ['key', 'item1', 'item2'], + [2, 5], + ], + ]; + } +} diff --git a/tests/Predis/Command/Redis/CountMinSketch/CMSQUERY_Test.php b/tests/Predis/Command/Redis/CountMinSketch/CMSQUERY_Test.php new file mode 100644 index 00000000..05004e0f --- /dev/null +++ b/tests/Predis/Command/Redis/CountMinSketch/CMSQUERY_Test.php @@ -0,0 +1,97 @@ +getCommand(); + $command->setArguments($actualArguments); + + $this->assertSameValues($expectedArguments, $command->getArguments()); + } + + /** + * @group connected + * @dataProvider sketchesProvider + * @param array $queryArguments + * @param array $expectedResponse + * @return void + * @requiresRedisBfVersion >= 2.0.0 + */ + public function testReturnCountValuesForGivenItemsWithinCountMinSketch( + array $queryArguments, + array $expectedResponse + ): void { + $redis = $this->getClient(); + + $redis->cmsinitbydim('key', 2000, 7); + + $actualResponse = $redis->cmsquery(...$queryArguments); + $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->cmsquery('cmsquery_foo', 'item1'); + } + + public function sketchesProvider(): array + { + return [ + 'with single item' => [ + ['key', 'item1'], + [0], + ], + 'with multiple items' => [ + ['key', 'item1', 'item2'], + [0, 0], + ], + ]; + } +}