mirror of
https://github.com/predis/predis.git
synced 2026-09-14 20:37:29 +00:00
Added support for Count-Min Sketch with base commands CMS.INITBYDIM, CMS.INFO (#1088)
* Added support for Count-Min Sketches submodule, added support for CMS.INITBYDIM adn CMS.INFO commands * Codestyle fixes --------- Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>
This commit is contained in:
committed by
GitHub
parent
d04f06a2a3
commit
5304ad95e8
@@ -22,6 +22,7 @@ class ClientConfiguration
|
||||
['name' => 'Json', 'commandPrefix' => 'JSON'],
|
||||
['name' => 'BloomFilter', 'commandPrefix' => 'BF'],
|
||||
['name' => 'CuckooFilter', 'commandPrefix' => 'CF'],
|
||||
['name' => 'CountMinSketch', 'commandPrefix' => 'CMS'],
|
||||
],
|
||||
];
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
<?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.info/
|
||||
*
|
||||
* Returns width, depth and total count of the sketch.
|
||||
*/
|
||||
class CMSINFO extends RedisCommand
|
||||
{
|
||||
public function getId()
|
||||
{
|
||||
return 'CMS.INFO';
|
||||
}
|
||||
|
||||
public function parseResponse($data)
|
||||
{
|
||||
if (count($data) > 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;
|
||||
}
|
||||
}
|
||||
@@ -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.initbydim/
|
||||
*
|
||||
* Initializes a Count-Min Sketch to dimensions specified by user.
|
||||
*/
|
||||
class CMSINITBYDIM extends RedisCommand
|
||||
{
|
||||
public function getId()
|
||||
{
|
||||
return 'CMS.INITBYDIM';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?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 CMSINFO_Test extends PredisCommandTestCase
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedCommand(): string
|
||||
{
|
||||
return CMSINFO::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedId(): string
|
||||
{
|
||||
return 'CMSINFO';
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testFilterArguments(): void
|
||||
{
|
||||
$actualArguments = ['key'];
|
||||
$expectedArguments = ['key'];
|
||||
|
||||
$command = $this->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');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?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 CMSINITBYDIM_Test extends PredisCommandTestCase
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedCommand(): string
|
||||
{
|
||||
return CMSINITBYDIM::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedId(): string
|
||||
{
|
||||
return 'CMSINITBYDIM';
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testFilterArguments(): void
|
||||
{
|
||||
$actualArguments = ['key', 2000, 10];
|
||||
$expectedArguments = ['key', 2000, 10];
|
||||
|
||||
$command = $this->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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user