Added support for TOPK.RESERVE, TOPK.INFO commands (#1107)

Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>
This commit is contained in:
Vladyslav Vildanov
2023-02-07 17:45:34 +02:00
committed by GitHub
parent 89dd528235
commit 12673ef50a
7 changed files with 333 additions and 0 deletions
+1
View File
@@ -24,6 +24,7 @@ class ClientConfiguration
['name' => 'CuckooFilter', 'commandPrefix' => 'CF'],
['name' => 'CountMinSketch', 'commandPrefix' => 'CMS'],
['name' => 'TDigest', 'commandPrefix' => 'TDIGEST'],
['name' => 'TopK', 'commandPrefix' => 'TOPK'],
],
];
+2
View File
@@ -186,6 +186,8 @@ use Predis\Command\CommandInterface;
* @method $this tdigestreset(string $key)
* @method $this tdigestrevrank(string $key, float ...$value)
* @method $this tdigesttrimmed_mean(string $key, float $lowCutQuantile, float $highCutQuantile)
* @method $this topkinfo(string $key)
* @method $this topkreserve(string $key, int $topK, int $width = 8, int $depth = 7, float $decay = 0.9)
* @method $this zadd($key, array $membersAndScoresDictionary)
* @method $this zcard($key)
* @method $this zcount($key, $min, $max)
+2
View File
@@ -196,6 +196,8 @@ use Predis\Response\Status;
* @method Status tdigestreset(string $key)
* @method array tdigestrevrank(string $key, float ...$value)
* @method string tdigesttrimmed_mean(string $key, float $lowCutQuantile, float $highCutQuantile)
* @method array topkinfo(string $key)
* @method Status topkreserve(string $key, int $topK, int $width = 8, int $depth = 7, float $decay = 0.9)
* @method string xadd(string $key, array $dictionary, string $id = '*', array $options = null)
* @method int xdel(string $key, string ...$id)
* @method int xlen(string $key)
+41
View File
@@ -0,0 +1,41 @@
<?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\TopK;
use Predis\Command\Command as RedisCommand;
/**
* @see https://redis.io/commands/topk.info/
*
* Returns number of required items (k), width, depth and decay values.
*/
class TOPKINFO extends RedisCommand
{
public function getId()
{
return 'TOPK.INFO';
}
public function parseResponse($data)
{
$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;
}
}
+47
View File
@@ -0,0 +1,47 @@
<?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\TopK;
use Predis\Command\Command as RedisCommand;
/**
* @see https://redis.io/commands/topk.reserve/
*
* Initializes a TopK with specified parameters.
*/
class TOPKRESERVE extends RedisCommand
{
public function getId()
{
return 'TOPK.RESERVE';
}
public function setArguments(array $arguments)
{
switch (count($arguments)) {
case 3:
$arguments[] = 7; // default depth
$arguments[] = 0.9; // default decay
break;
case 4:
$arguments[] = 0.9; // default decay
break;
default:
parent::setArguments($arguments);
return;
}
parent::setArguments($arguments);
}
}
@@ -0,0 +1,92 @@
<?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\TopK;
use Predis\Command\Redis\PredisCommandTestCase;
use Predis\Response\ServerException;
class TOPKINFO_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return TOPKINFO::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'TOPKINFO';
}
/**
* @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 = ['k', 50, 'width', 8, 'depth', 7, 'decay', '0.90000000000000002'];
$expectedResponse = ['k' => 50, 'width' => 8, 'depth' => 7, 'decay' => '0.90000000000000002'];
$this->assertSame($expectedResponse, $this->getCommand()->parseResponse($actualResponse));
}
/**
* @group connected
* @return void
* @requiresRedisBfVersion >= 2.0.0
*/
public function testReturnsInfoAboutGivenTopKStructure(): void
{
$redis = $this->getClient();
$redis->topkreserve('key', 50);
$this->assertSame(
['k' => 50, 'width' => 8, 'depth' => 7, 'decay' => '0.90000000000000002'],
$redis->topkinfo('key')
);
}
/**
* @group connected
* @return void
* @requiresRedisBfVersion >= 2.0.0
*/
public function testThrowsExceptionOnNonExistingKey(): void
{
$redis = $this->getClient();
$this->expectException(ServerException::class);
$this->expectExceptionMessage('TopK: key does not exist');
$redis->topkinfo('key');
}
}
@@ -0,0 +1,148 @@
<?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\TopK;
use Predis\Command\Redis\PredisCommandTestCase;
use Predis\Response\ServerException;
class TOPKRESERVE_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return TOPKRESERVE::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'TOPKRESERVE';
}
/**
* @group disconnected
* @dataProvider argumentsProvider
*/
public function testFilterArguments(array $actualArguments, array $expectedArguments): void
{
$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
* @dataProvider structureProvider
* @param array $topKArguments
* @param string $key
* @param array $expectedInfoResponse
* @return void
* @requiresRedisBfVersion >= 2.0.0
*/
public function testReserveInitializeTopKStructureWithGivenConfiguration(
array $topKArguments,
string $key,
array $expectedInfoResponse
): void {
$redis = $this->getClient();
$actualResponse = $redis->topkreserve(...$topKArguments);
$actualInfoResponse = $redis->topkinfo($key);
$this->assertEquals('OK', $actualResponse);
$this->assertSame($expectedInfoResponse, $actualInfoResponse);
}
/**
* @group connected
* @return void
* @requiresRedisBfVersion >= 2.0.0
*/
public function testThrowsExceptionOnAlreadyExistingKey(): void
{
$redis = $this->getClient();
$redis->topkreserve('key', 50);
$this->expectException(ServerException::class);
$this->expectExceptionMessage('TopK: key already exists');
$redis->topkreserve('key', 50);
}
public function argumentsProvider(): array
{
return [
'with default arguments' => [
['key', 10],
['key', 10],
],
'with non-default width' => [
['key', 10, 5],
['key', 10, 5, 7, 0.9],
],
'with non-default depth' => [
['key', 10, 8, 9],
['key', 10, 8, 9, 0.9],
],
'with non-default decay' => [
['key', 10, 8, 7, 0.8],
['key', 10, 8, 7, 0.8],
],
];
}
public function structureProvider(): array
{
return [
'with default configuration' => [
['key', 50],
'key',
['k' => 50, 'width' => 8, 'depth' => 7, 'decay' => '0.90000000000000002'],
],
'with non-default width' => [
['key', 50, 9],
'key',
['k' => 50, 'width' => 9, 'depth' => 7, 'decay' => '0.90000000000000002'],
],
'with non-default depth' => [
['key', 50, 8, 9],
'key',
['k' => 50, 'width' => 8, 'depth' => 9, 'decay' => '0.90000000000000002'],
],
'with non-default decay' => [
['key', 50, 8, 7, 0.8],
'key',
['k' => 50, 'width' => 8, 'depth' => 7, 'decay' => '0.80000000000000004'],
],
'with all arguments non-default' => [
['key', 50, 9, 6, 0.8],
'key',
['k' => 50, 'width' => 9, 'depth' => 6, 'decay' => '0.80000000000000004'],
],
];
}
}