mirror of
https://github.com/predis/predis.git
synced 2026-08-31 12:43:31 +00:00
Extended Count-Min Sketch support by implementing CMS.MERGE command (#1092)
* Added support for CMS.MERGE command * Added command description --------- Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>
This commit is contained in:
committed by
GitHub
parent
acc1dd0cb8
commit
1661903cba
@@ -75,6 +75,7 @@ use Predis\Command\CommandInterface;
|
||||
* @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 cmsmerge(string $destination, array $sources, array $weights = [])
|
||||
* @method $this cmsquery(string $key, string ...$item)
|
||||
* @method $this decr($key)
|
||||
* @method $this decrby($key, $decrement)
|
||||
|
||||
@@ -84,6 +84,7 @@ use Predis\Response\Status;
|
||||
* @method array cmsinfo(string $key)
|
||||
* @method Status cmsinitbydim(string $key, int $width, int $depth)
|
||||
* @method Status cmsinitbyprob(string $key, float $errorRate, float $probability)
|
||||
* @method Status cmsmerge(string $destination, array $sources, array $weights = [])
|
||||
* @method array cmsquery(string $key, string ...$item)
|
||||
* @method int decr(string $key)
|
||||
* @method int decrby(string $key, int $decrement)
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
<?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.merge/
|
||||
*
|
||||
* Merges several sketches into one sketch.
|
||||
* All sketches must have identical width and depth.
|
||||
* Weights can be used to multiply certain sketches. Default weight is 1.
|
||||
*/
|
||||
class CMSMERGE extends RedisCommand
|
||||
{
|
||||
public function getId()
|
||||
{
|
||||
return 'CMS.MERGE';
|
||||
}
|
||||
|
||||
public function setArguments(array $arguments)
|
||||
{
|
||||
$processedArguments = array_merge([$arguments[0], count($arguments[1])], $arguments[1]);
|
||||
|
||||
if (!empty($arguments[2])) {
|
||||
$processedArguments[] = 'WEIGHTS';
|
||||
$processedArguments = array_merge($processedArguments, $arguments[2]);
|
||||
}
|
||||
|
||||
parent::setArguments($processedArguments);
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,14 @@ class CMSINCRBY_Test extends PredisCommandTestCase
|
||||
$this->assertSameValues($expectedArguments, $command->getArguments());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testParseResponse(): void
|
||||
{
|
||||
$this->assertSame(1, $this->getCommand()->parseResponse(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @dataProvider sketchesProvider
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
<?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 CMSMERGE_Test extends PredisCommandTestCase
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedCommand(): string
|
||||
{
|
||||
return CMSMERGE::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedId(): string
|
||||
{
|
||||
return 'CMSMERGE';
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
* @dataProvider argumentsProvider
|
||||
*/
|
||||
public function testFilterArguments(array $actualArguments, array $expectedArguments): void
|
||||
{
|
||||
$command = $this->getCommand();
|
||||
$command->setArguments($actualArguments);
|
||||
|
||||
$this->assertSame($expectedArguments, $command->getArguments());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testParseResponse(): void
|
||||
{
|
||||
$this->assertSame(1, $this->getCommand()->parseResponse(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @dataProvider sketchesProvider
|
||||
* @param array $mergeArguments
|
||||
* @param string $destinationKey
|
||||
* @param array $items
|
||||
* @param array $expectedCounts
|
||||
* @return void
|
||||
* @requiresRedisBfVersion >= 2.0.0
|
||||
*/
|
||||
public function testMergeSketchesAndSaveWithinDestinationCountMinSketch(
|
||||
array $mergeArguments,
|
||||
string $destinationKey,
|
||||
array $items,
|
||||
array $expectedCounts
|
||||
): void {
|
||||
$redis = $this->getClient();
|
||||
|
||||
$redis->cmsinitbyprob('source1', 0.001, 0.01);
|
||||
$redis->cmsinitbyprob('source2', 0.001, 0.01);
|
||||
$redis->cmsinitbyprob('destination', 0.001, 0.01);
|
||||
$redis->cmsincrby('source1', 'item1', 1, 'item2', 1);
|
||||
$redis->cmsincrby('source2', 'item1', 1, 'item2', 1);
|
||||
|
||||
$actualResponse = $redis->cmsmerge(...$mergeArguments);
|
||||
|
||||
$this->assertEquals('OK', $actualResponse);
|
||||
$this->assertSame($expectedCounts, $redis->cmsquery('destination', ...$items));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @return void
|
||||
* @requiresRedisBfVersion >= 2.0.0
|
||||
*/
|
||||
public function testThrowsExceptionOnNonExistingDestinationCountMinSketch(): void
|
||||
{
|
||||
$redis = $this->getClient();
|
||||
|
||||
$redis->cmsinitbyprob('source1', 0.001, 0.01);
|
||||
$redis->cmsinitbyprob('source2', 0.001, 0.01);
|
||||
|
||||
$this->expectException(ServerException::class);
|
||||
$this->expectExceptionMessage('CMS: key does not exist');
|
||||
|
||||
$redis->cmsmerge('destination', ['source1', 'source2']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @return void
|
||||
* @requiresRedisBfVersion >= 2.0.0
|
||||
*/
|
||||
public function testThrowsExceptionOnNonExistingSourceCountMinSketch(): void
|
||||
{
|
||||
$redis = $this->getClient();
|
||||
|
||||
$redis->cmsinitbyprob('source1', 0.001, 0.01);
|
||||
$redis->cmsinitbyprob('destination', 0.001, 0.01);
|
||||
|
||||
$this->expectException(ServerException::class);
|
||||
$this->expectExceptionMessage('CMS: key does not exist');
|
||||
|
||||
$redis->cmsmerge('destination', ['source1', 'source2']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @return void
|
||||
* @requiresRedisBfVersion >= 2.0.0
|
||||
*/
|
||||
public function testThrowsExceptionOnDifferentCountMinSketchesConfigurations(): void
|
||||
{
|
||||
$redis = $this->getClient();
|
||||
|
||||
$redis->cmsinitbyprob('source1', 0.001, 0.01);
|
||||
$redis->cmsinitbyprob('source2', 0.01, 0.01);
|
||||
$redis->cmsinitbyprob('destination', 0.001, 0.01);
|
||||
|
||||
$this->expectException(ServerException::class);
|
||||
$this->expectExceptionMessage('CMS: width/depth is not equal');
|
||||
|
||||
$redis->cmsmerge('destination', ['source1', 'source2']);
|
||||
}
|
||||
|
||||
public function argumentsProvider(): array
|
||||
{
|
||||
return [
|
||||
'with default arguments' => [
|
||||
['destination', ['source']],
|
||||
['destination', 1, 'source'],
|
||||
],
|
||||
'with multiple sources' => [
|
||||
['destination', ['source1', 'source2']],
|
||||
['destination', 2, 'source1', 'source2'],
|
||||
],
|
||||
'with WEIGHTS' => [
|
||||
['destination', ['source1', 'source2'], [1, 3]],
|
||||
['destination', 2, 'source1', 'source2', 'WEIGHTS', 1, 3],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function sketchesProvider(): array
|
||||
{
|
||||
return [
|
||||
'with default arguments' => [
|
||||
['destination', ['source1', 'source2']],
|
||||
'destination',
|
||||
['item1', 'item2'],
|
||||
[2, 2],
|
||||
],
|
||||
'with modified WEIGHTS' => [
|
||||
['destination', ['source1', 'source2'], [2, 4]],
|
||||
'destination',
|
||||
['item1', 'item2'],
|
||||
[6, 6],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,14 @@ class CMSQUERY_Test extends PredisCommandTestCase
|
||||
$this->assertSameValues($expectedArguments, $command->getArguments());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testParseResponse(): void
|
||||
{
|
||||
$this->assertSame(1, $this->getCommand()->parseResponse(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @dataProvider sketchesProvider
|
||||
|
||||
Reference in New Issue
Block a user