Extended Count-Min Sketch support by implementing CMS.INCRBY and CMS.QUERY commands (#1091)

* Added support for CMS.INCRBY and CMS.QUERY commands

* Codestyle fixes

---------

Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>
This commit is contained in:
Vladyslav Vildanov
2023-02-02 14:54:12 +02:00
committed by GitHub
parent e831c71cc6
commit acc1dd0cb8
6 changed files with 267 additions and 0 deletions
+2
View File
@@ -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)
+2
View File
@@ -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)
@@ -0,0 +1,29 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Redis\CountMinSketch;
use Predis\Command\Command as RedisCommand;
/**
* @see https://redis.io/commands/cms.incrby/
*
* Increases the count of item by increment.
* Multiple items can be increased with one call.
*/
class CMSINCRBY extends RedisCommand
{
public function getId()
{
return 'CMS.INCRBY';
}
}
@@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Redis\CountMinSketch;
use Predis\Command\Command as RedisCommand;
/**
* @see https://redis.io/commands/cms.query/
*
* Returns the count for one or more items in a sketch.
*/
class CMSQUERY extends RedisCommand
{
public function getId()
{
return 'CMS.QUERY';
}
}
@@ -0,0 +1,109 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Redis\CountMinSketch;
use Predis\Command\Redis\PredisCommandTestCase;
use Predis\Response\ServerException;
class CMSINCRBY_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return CMSINCRBY::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'CMSINCRBY';
}
/**
* @group disconnected
*/
public function testFilterArguments(): void
{
$actualArguments = ['key', 'item1', 1, 'item2', 1];
$expectedArguments = ['key', 'item1', 1, 'item2', 1];
$command = $this->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],
],
];
}
}
@@ -0,0 +1,97 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Redis\CountMinSketch;
use Predis\Command\Redis\PredisCommandTestCase;
use Predis\Response\ServerException;
class CMSQUERY_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return CMSQUERY::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'CMSQUERY';
}
/**
* @group disconnected
*/
public function testFilterArguments(): void
{
$actualArguments = ['key', 'item1', 'item2'];
$expectedArguments = ['key', 'item1', 'item2'];
$command = $this->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],
],
];
}
}