From 699d023280645ff9e1a147a442ea1e7c117110c1 Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Tue, 7 Feb 2023 10:50:31 +0200 Subject: [PATCH] Extended TDigest support by implementing TDIGEST.RESET command (#1104) * Added support for TDIGEST.RESET command * Codestyle fixes --------- Co-authored-by: Vladyslav Vildanov --- src/ClientContextInterface.php | 1 + src/ClientInterface.php | 1 + src/Command/Redis/TDigest/TDIGESTRESET.php | 28 +++++ .../Redis/TDigest/TDIGESTRESET_Test.php | 100 ++++++++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 src/Command/Redis/TDigest/TDIGESTRESET.php create mode 100644 tests/Predis/Command/Redis/TDigest/TDIGESTRESET_Test.php diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 4fe56a4c..496acdb0 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -182,6 +182,7 @@ use Predis\Command\CommandInterface; * @method $this tdigestmax(string $key) * @method $this tdigestmin(string $key) * @method $this tdigestrank(string $key, ...$value) + * @method $this tdigestreset(string $key) * @method $this zadd($key, array $membersAndScoresDictionary) * @method $this zcard($key) * @method $this zcount($key, $min, $max) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index afffa298..d1bd3c56 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -192,6 +192,7 @@ use Predis\Response\Status; * @method string tdigestmax(string $key) * @method string tdigestmin(string $key) * @method array tdigestrank(string $key, float ...$value) + * @method Status tdigestreset(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) diff --git a/src/Command/Redis/TDigest/TDIGESTRESET.php b/src/Command/Redis/TDigest/TDIGESTRESET.php new file mode 100644 index 00000000..0456f4f9 --- /dev/null +++ b/src/Command/Redis/TDigest/TDIGESTRESET.php @@ -0,0 +1,28 @@ +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.4.0 + */ + public function testResetExistingSketch(): void + { + $redis = $this->getClient(); + + $redis->tdigestcreate('key', 500); + $redis->tdigestadd('key', 1, 2, 2, 3, 3, 3); + + $this->assertSame( + ['1', '2', '2', '3', '3', '3'], + $redis->tdigestbyrank('key', 0, 1, 2, 3, 4, 5) + ); + + $actualResponse = $redis->tdigestreset('key'); + $info = $redis->tdigestinfo('key'); + + $this->assertEquals('OK', $actualResponse); + $this->assertSame(500, $info['Compression']); + $this->assertSame( + ['nan', 'nan', 'nan', 'nan', 'nan', 'nan'], + $redis->tdigestbyrank('key', 0, 1, 2, 3, 4, 5) + ); + } + + /** + * @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->tdigestreset('key'); + } +}