mirror of
https://github.com/predis/predis.git
synced 2026-09-02 13:49:32 +00:00
Added support for TDigest sketch, added TDIGEST.CREATE, TDIGEST.INFO commands (#1093)
Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>
This commit is contained in:
committed by
GitHub
parent
1661903cba
commit
43ecf3f4fb
@@ -23,6 +23,7 @@ class ClientConfiguration
|
||||
['name' => 'BloomFilter', 'commandPrefix' => 'BF'],
|
||||
['name' => 'CuckooFilter', 'commandPrefix' => 'CF'],
|
||||
['name' => 'CountMinSketch', 'commandPrefix' => 'CMS'],
|
||||
['name' => 'TDigest', 'commandPrefix' => 'TDIGEST'],
|
||||
],
|
||||
];
|
||||
|
||||
|
||||
@@ -173,6 +173,8 @@ use Predis\Command\CommandInterface;
|
||||
* @method $this sscan($key, $cursor, array $options = null)
|
||||
* @method $this sunion(array|string $keys)
|
||||
* @method $this sunionstore($destination, array|string $keys)
|
||||
* @method $this tdigestcreate(string $key, int $compression = 0)
|
||||
* @method $this tdigestinfo(string $key)
|
||||
* @method $this zadd($key, array $membersAndScoresDictionary)
|
||||
* @method $this zcard($key)
|
||||
* @method $this zcount($key, $min, $max)
|
||||
|
||||
@@ -183,6 +183,8 @@ use Predis\Response\Status;
|
||||
* @method string[] sunion(array|string $keys)
|
||||
* @method int sunionstore(string $destination, array|string $keys)
|
||||
* @method int touch(string[]|string $keyOrKeys, string ...$keys = null)
|
||||
* @method Status tdigestcreate(string $key, int $compression = 0)
|
||||
* @method array tdigestinfo(string $key)
|
||||
* @method string xadd(string $key, array $dictionary, string $id = '*', array $options = null)
|
||||
* @method int xdel(string $key, string ...$id)
|
||||
* @method int xlen(string $key)
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
<?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\TDigest;
|
||||
|
||||
use Predis\Command\Command as RedisCommand;
|
||||
|
||||
/**
|
||||
* @see https://redis.io/commands/tdigest.create/
|
||||
*
|
||||
* Allocates memory and initializes a new t-digest sketch.
|
||||
*/
|
||||
class TDIGESTCREATE extends RedisCommand
|
||||
{
|
||||
public function getId()
|
||||
{
|
||||
return 'TDIGEST.CREATE';
|
||||
}
|
||||
|
||||
public function setArguments(array $arguments)
|
||||
{
|
||||
if (!empty($arguments[1])) {
|
||||
$arguments[2] = $arguments[1];
|
||||
$arguments[1] = 'COMPRESSION';
|
||||
} elseif (array_key_exists(1, $arguments) && $arguments[1] < 1) {
|
||||
array_pop($arguments);
|
||||
}
|
||||
|
||||
parent::setArguments($arguments);
|
||||
}
|
||||
}
|
||||
@@ -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\TDigest;
|
||||
|
||||
use Predis\Command\Command as RedisCommand;
|
||||
|
||||
/**
|
||||
* @see https://redis.io/commands/tdigest.info/
|
||||
*
|
||||
* Returns information and statistics about a t-digest sketch.
|
||||
*/
|
||||
class TDIGESTINFO extends RedisCommand
|
||||
{
|
||||
public function getId()
|
||||
{
|
||||
return 'TDIGEST.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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
<?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\TDigest;
|
||||
|
||||
use Predis\Command\Redis\PredisCommandTestCase;
|
||||
use Predis\Response\ServerException;
|
||||
|
||||
class TDIGESTCREATE_Test extends PredisCommandTestCase
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedCommand(): string
|
||||
{
|
||||
return TDIGESTCREATE::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedId(): string
|
||||
{
|
||||
return 'TDIGESTCREATE';
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 sketchesProvider
|
||||
* @param array $createArguments
|
||||
* @param string $key
|
||||
* @param int $expectedCompression
|
||||
* @return void
|
||||
* @requiresRedisBfVersion >= 2.4.0
|
||||
*/
|
||||
public function testCreateTDigestSketchWithGivenConfiguration(
|
||||
array $createArguments,
|
||||
string $key,
|
||||
int $expectedCompression
|
||||
): void {
|
||||
$redis = $this->getClient();
|
||||
|
||||
$actualResponse = $redis->tdigestcreate(...$createArguments);
|
||||
$info = $redis->tdigestinfo($key);
|
||||
|
||||
$this->assertEquals('OK', $actualResponse);
|
||||
$this->assertSame($expectedCompression, $info['Compression']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @return void
|
||||
* @requiresRedisBfVersion >= 2.4.0
|
||||
*/
|
||||
public function testThrowsExceptionOnAlreadyCreatedKey(): void
|
||||
{
|
||||
$redis = $this->getClient();
|
||||
|
||||
$actualResponse = $redis->tdigestcreate('key');
|
||||
$this->assertEquals('OK', $actualResponse);
|
||||
|
||||
$this->expectException(ServerException::class);
|
||||
$this->expectExceptionMessage('ERR T-Digest: key already exists');
|
||||
|
||||
$redis->tdigestcreate('key');
|
||||
}
|
||||
|
||||
public function argumentsProvider(): array
|
||||
{
|
||||
return [
|
||||
'with default arguments' => [
|
||||
['key'],
|
||||
['key'],
|
||||
],
|
||||
'with 0 compression' => [
|
||||
['key', 0],
|
||||
['key'],
|
||||
],
|
||||
'with COMPRESSION modifier' => [
|
||||
['key', 100],
|
||||
['key', 'COMPRESSION', 100],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function sketchesProvider(): array
|
||||
{
|
||||
return [
|
||||
'with default arguments' => [
|
||||
['key'],
|
||||
'key',
|
||||
100,
|
||||
],
|
||||
'with modified COMPRESSION' => [
|
||||
['key', 120],
|
||||
'key',
|
||||
120,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
<?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\TDigest;
|
||||
|
||||
use Predis\Command\Redis\PredisCommandTestCase;
|
||||
use Predis\Response\ServerException;
|
||||
|
||||
class TDIGESTINFO_Test extends PredisCommandTestCase
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedCommand(): string
|
||||
{
|
||||
return TDIGESTINFO::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedId(): string
|
||||
{
|
||||
return 'TDIGESTINFO';
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 = [
|
||||
'Compression', 100, 'Capacity', 610, 'Merged nodes', 0, 'Unmerged nodes', 5, 'Merged weight', 0,
|
||||
'Unmerged weight', 5, 'Observations', 5, 'Total compressions', 0, 'Memory usage', 9768,
|
||||
];
|
||||
$expectedResponse = [
|
||||
'Compression' => 100,
|
||||
'Capacity' => 610,
|
||||
'Merged nodes' => 0,
|
||||
'Unmerged nodes' => 5,
|
||||
'Merged weight' => 0,
|
||||
'Unmerged weight' => 5,
|
||||
'Observations' => 5,
|
||||
'Total compressions' => 0,
|
||||
'Memory usage' => 9768,
|
||||
];
|
||||
|
||||
$this->assertSame($expectedResponse, $this->getCommand()->parseResponse($actualResponse));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @return void
|
||||
* @requiresRedisBfVersion >= 2.4.0
|
||||
*/
|
||||
public function testInfoReturnsInformationAboutGivenTDigestSketch(): void
|
||||
{
|
||||
$redis = $this->getClient();
|
||||
$expectedResponse = [
|
||||
'Compression' => 100,
|
||||
'Capacity' => 610,
|
||||
'Merged nodes' => 0,
|
||||
'Unmerged nodes' => 0,
|
||||
'Merged weight' => 0,
|
||||
'Unmerged weight' => 0,
|
||||
'Observations' => 0,
|
||||
'Total compressions' => 0,
|
||||
'Memory usage' => 9768,
|
||||
];
|
||||
|
||||
$redis->tdigestcreate('key');
|
||||
|
||||
$this->assertSame($expectedResponse, $redis->tdigestinfo('key'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @return void
|
||||
* @requiresRedisBfVersion >= 2.4.0
|
||||
*/
|
||||
public function testThrowsExceptionOnNonExistingTDigestSketch(): void
|
||||
{
|
||||
$redis = $this->getClient();
|
||||
|
||||
$this->expectException(ServerException::class);
|
||||
$this->expectExceptionMessage('ERR T-Digest: key does not exist');
|
||||
|
||||
$redis->tdigestinfo('key');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user