Extended TDigest Sketch support by implementing TDIGEST.ADD command (#1094)

* Added support for TDIGEST.ADD command

* Codestyle fixes

---------

Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>
This commit is contained in:
Vladyslav Vildanov
2023-02-03 16:52:46 +02:00
committed by GitHub
parent 43ecf3f4fb
commit cf539f9ea7
4 changed files with 112 additions and 0 deletions
+1
View File
@@ -173,6 +173,7 @@ 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 tdigestadd(string $key, float ...$value)
* @method $this tdigestcreate(string $key, int $compression = 0)
* @method $this tdigestinfo(string $key)
* @method $this zadd($key, array $membersAndScoresDictionary)
+1
View File
@@ -183,6 +183,7 @@ 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 tdigestadd(string $key, float ...$value)
* @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)
+28
View File
@@ -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\TDigest;
use Predis\Command\Command as RedisCommand;
/**
* @see https://redis.io/commands/tdigest.add/
*
* Adds one or more observations to a t-digest sketch.
*/
class TDIGESTADD extends RedisCommand
{
public function getId()
{
return 'TDIGEST.ADD';
}
}
@@ -0,0 +1,82 @@
<?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 TDIGESTADD_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return TDIGESTADD::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'TDIGESTADD';
}
/**
* @group disconnected
*/
public function testFilterArguments(): void
{
$actualArguments = ['key', 1, 2, 3];
$expectedArguments = ['key', 1, 2, 3];
$command = $this->getCommand();
$command->setArguments($actualArguments);
$this->assertSameValues($expectedArguments, $command->getArguments());
}
/**
* @group connected
* @return void
* @requiresRedisBfVersion >= 2.4.0
*/
public function testAddObservationsIntoGivenTDigestSketch(): void
{
$redis = $this->getClient();
$redis->tdigestcreate('key');
$actualResponse = $redis->tdigestadd('key', 1, 2, 3);
$info = $redis->tdigestinfo('key');
$this->assertEquals('OK', $actualResponse);
$this->assertSame(3, $info['Observations']);
}
/**
* @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->tdigestadd('key', 1, 2, 3);
}
}