Extended TimeSeries support by implementing TS.ALTER command (#1220)

* Added support for new arguments for BITPOS, BITCOUNT commands (#1045)

* Added support for new arguments for EXPIRE, EXPIREAT commands (#1046)

* Extended core support by implementing SORT_RO command (#1044)

* Added support for SORT_RO command

* Codestyle fixes

* Added command description

---------

Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>

* fix deprecated call

* Added support for container commands (#1049)

* Added support for container commands FUNCTION LOAD, FUNCTION DELETE and FCALL

* Changed ContainerInterface and AbstractContainer

* Re-implement logic of abstract methods

---------

Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>

* Added stream commands to KeyPrefixProcessor (#1051)

Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>

* Fix return type of ReplicationInterface::getSlaves (#1111)

* Codestyle fixes

* Changed return annotation

* Added support for TS.ALTER command

---------

Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>
Co-authored-by: Till Krüss <till@kruss.io>
Co-authored-by: Stephan <glaubinix@users.noreply.github.com>
This commit is contained in:
Vladyslav Vildanov
2023-03-24 12:27:35 +02:00
committed by GitHub
parent 7758bd8ca8
commit a151571cd5
6 changed files with 228 additions and 0 deletions
+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.
*/
use Predis\Client;
use Predis\Command\Argument\TimeSeries\AlterArguments;
use Predis\Command\Argument\TimeSeries\CommonArguments;
use Predis\Command\Argument\TimeSeries\CreateArguments;
require __DIR__ . '/../../shared.php';
// Example of TS.ALTER command usage:
// 1. Create time series
$client = new Client();
$arguments = (new CreateArguments())
->retention(60000)
->duplicatePolicy(CommonArguments::POLICY_MAX)
->labels('sensor_id', 2, 'area_id', 32);
$response = $client->tscreate('temperature:2:32', $arguments);
echo "Time series creation status: {$response}\n";
// 2. Update Duplicate policy for time series above
$arguments = (new AlterArguments())
->duplicatePolicy(CommonArguments::POLICY_FIRST);
$response = $client->tsalter('temperature:2:32', $arguments);
$output = ($response == 'OK') ? 'Duplicate policy was successfully updated' : $response;
echo $output;
+2
View File
@@ -28,6 +28,7 @@ use Predis\Command\Argument\Search\SynUpdateArguments;
use Predis\Command\Argument\Server\LimitOffsetCount;
use Predis\Command\Argument\Server\To;
use Predis\Command\Argument\TimeSeries\AddArguments;
use Predis\Command\Argument\TimeSeries\AlterArguments as TSAlterArguments;
use Predis\Command\Argument\TimeSeries\CreateArguments as TSCreateArguments;
use Predis\Command\Argument\TimeSeries\GetArguments;
use Predis\Command\CommandInterface;
@@ -236,6 +237,7 @@ use Predis\Command\Container\Search\FTCONFIG;
* @method $this topkquery(string $key, ...$items)
* @method $this topkreserve(string $key, int $topK, int $width = 8, int $depth = 7, float $decay = 0.9)
* @method $this tsadd(string $key, int $timestamp, float $value, ?AddArguments $arguments = null)
* @method $this tsalter(string $key, ?TSAlterArguments $arguments = null)
* @method $this tscreate(string $key, ?TSCreateArguments $arguments = null)
* @method $this tsdel(string $key, int $fromTimestamp, int $toTimestamp)
* @method $this tsget(string $key, GetArguments $arguments = null)
+2
View File
@@ -28,6 +28,7 @@ use Predis\Command\Argument\Search\SynUpdateArguments;
use Predis\Command\Argument\Server\LimitOffsetCount;
use Predis\Command\Argument\Server\To;
use Predis\Command\Argument\TimeSeries\AddArguments;
use Predis\Command\Argument\TimeSeries\AlterArguments as TSAlterArguments;
use Predis\Command\Argument\TimeSeries\CreateArguments as TSCreateArguments;
use Predis\Command\Argument\TimeSeries\GetArguments;
use Predis\Command\CommandInterface;
@@ -246,6 +247,7 @@ use Predis\Response\Status;
* @method array topkquery(string $key, ...$items)
* @method Status topkreserve(string $key, int $topK, int $width = 8, int $depth = 7, float $decay = 0.9)
* @method int tsadd(string $key, int $timestamp, float $value, ?AddArguments $arguments = null)
* @method Status tsalter(string $key, ?TSAlterArguments $arguments = null)
* @method Status tscreate(string $key, ?TSCreateArguments $arguments = null)
* @method int tsdel(string $key, int $fromTimestamp, int $toTimestamp)
* @method array tsget(string $key, GetArguments $arguments = null)
@@ -0,0 +1,17 @@
<?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\Argument\TimeSeries;
class AlterArguments extends CommonArguments
{
}
+39
View File
@@ -0,0 +1,39 @@
<?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\TimeSeries;
use Predis\Command\Command as RedisCommand;
/**
* @see https://redis.io/commands/ts.alter/
*
* Update the retention, chunk size, duplicate policy, and labels of an existing time series.
*/
class TSALTER extends RedisCommand
{
public function getId()
{
return 'TS.ALTER';
}
public function setArguments(array $arguments)
{
[$key] = $arguments;
$commandArguments = (!empty($arguments[1])) ? $arguments[1]->toArray() : [];
parent::setArguments(array_merge(
[$key],
$commandArguments
));
}
}
@@ -0,0 +1,127 @@
<?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\TimeSeries;
use Predis\Command\Argument\TimeSeries\AlterArguments;
use Predis\Command\Argument\TimeSeries\CommonArguments;
use Predis\Command\Argument\TimeSeries\CreateArguments;
use Predis\Command\Redis\PredisCommandTestCase;
use Predis\Response\ServerException;
class TSALTER_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return TSALTER::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'TSALTER';
}
/**
* @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
* @return void
* @requiresRedisTimeSeriesVersion >= 1.0.0
*/
public function testAlterUpdatesExistingTimeSeriesConfiguration(): void
{
$redis = $this->getClient();
$arguments = (new CreateArguments())
->retention(60000)
->duplicatePolicy(CommonArguments::POLICY_MAX)
->labels('sensor_id', 2, 'area_id', 32);
$this->assertEquals(
'OK',
$redis->tscreate('temperature:2:32', $arguments)
);
$alterArguments = (new AlterArguments())
->retention(10000000);
$this->assertEquals(
'OK',
$redis->tsalter('temperature:2:32', $alterArguments)
);
}
/**
* @group connected
* @return void
* @requiresRedisTimeSeriesVersion >= 1.0.0
*/
public function testThrowsExceptionOnNonExistingKey(): void
{
$redis = $this->getClient();
$this->expectException(ServerException::class);
$this->expectExceptionMessage('ERR TSDB: the key does not exist');
$redis->tsalter('non_existing_key');
}
public function argumentsProvider(): array
{
return [
'with default arguments' => [
['key'],
['key'],
],
'with RETENTION modifier' => [
['key', (new AlterArguments())->retention(100)],
['key', 'RETENTION', 100],
],
'with CHUNK_SIZE modifier' => [
['key', (new AlterArguments())->chunkSize(100)],
['key', 'CHUNK_SIZE', 100],
],
'with DUPLICATE_POLICY modifier' => [
['key', (new AlterArguments())->duplicatePolicy(CommonArguments::POLICY_FIRST)],
['key', 'DUPLICATE_POLICY', CommonArguments::POLICY_FIRST],
],
'with all modifiers' => [
['key', (new AlterArguments())->retention(100)->chunkSize(100)->duplicatePolicy(CommonArguments::POLICY_FIRST)],
['key', 'RETENTION', 100, 'CHUNK_SIZE', 100, 'DUPLICATE_POLICY', CommonArguments::POLICY_FIRST],
],
];
}
}