implement XSETID command (#1559)

* implement XSETID command

* modify CHANGELOG.md

* split test to basic and extended version

---------

Co-authored-by: aleksanders <alexander.s@seranking.com>
This commit is contained in:
alschastny
2025-06-16 10:11:51 +03:00
committed by GitHub
parent 1e390b1814
commit 33e1174a4f
5 changed files with 196 additions and 0 deletions
+1
View File
@@ -6,6 +6,7 @@
- Added support for `XACK` command (#1555)
- Added support for `XCLAIM` command (#1557)
- Added support for `XPENDING` command (#1558)
- Added support for `XSETID` command (#1559)
### Changed
- Handle and retry `LOADING` errors from Sentinel replicas (#1536)
+1
View File
@@ -295,6 +295,7 @@ use Predis\Command\Redis\VADD;
* @method $this xrange(string $key, string $start, string $end, ?int $count = null)
* @method $this xread(int $count = null, int $block = null, array $streams = null, string ...$id)
* @method $this xreadgroup(string $group, string $consumer, ?int $count = null, ?int $blockMs = null, bool $noAck = false, string ...$keyOrId)
* @method $this xsetid(string $key, string $lastId, ?int $entriesAdded = null, ?string $maxDeleteId = null)
* @method $this xtrim(string $key, array|string $strategy, string $threshold, array $options = null)
* @method $this zadd($key, array $membersAndScoresDictionary)
* @method $this zcard($key)
+1
View File
@@ -306,6 +306,7 @@ use Predis\Response\Status;
* @method array xrange(string $key, string $start, string $end, ?int $count = null)
* @method array|null xread(int $count = null, int $block = null, array $streams = null, string ...$id)
* @method array xreadgroup(string $group, string $consumer, ?int $count = null, ?int $blockMs = null, bool $noAck = false, string ...$keyOrId)
* @method Status xsetid(string $key, string $lastId, ?int $entriesAdded = null, ?string $maxDeleteId = null)
* @method string xtrim(string $key, array|string $strategy, string $threshold, array $options = null)
* @method int zadd(string $key, array $membersAndScoresDictionary)
* @method int zcard(string $key)
+49
View File
@@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2025 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;
use Predis\Command\PrefixableCommand as RedisCommand;
/**
* @see http://redis.io/commands/xsetid
*/
class XSETID extends RedisCommand
{
/**
* {@inheritdoc}
*/
public function getId()
{
return 'XSETID';
}
public function setArguments(array $arguments): void
{
$preparedArguments = array_slice($arguments, 0, 2);
if (isset($arguments[2])) {
array_push($preparedArguments, 'ENTRIESADDED', $arguments[2]);
}
if (isset($arguments[3])) {
array_push($preparedArguments, 'MAXDELETEDID', $arguments[3]);
}
parent::setArguments($preparedArguments);
}
public function prefixKeys($prefix)
{
$this->applyPrefixForFirstArgument($prefix);
}
}
+144
View File
@@ -0,0 +1,144 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2025 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;
use Predis\Response\ServerException;
/**
* @group commands
* @group realm-stream
*/
class XSETID_Test extends PredisCommandTestCase
{
/**
* {@inheritdoc}
*/
protected function getExpectedCommand(): string
{
return 'Predis\Command\Redis\XSETID';
}
/**
* {@inheritdoc}
*/
protected function getExpectedId(): string
{
return 'XSETID';
}
/**
* @dataProvider argumentsProvider
* @group disconnected
*/
public function testFilterArguments(array $actualArguments, array $expectedArguments): void
{
$command = $this->getCommand();
$command->setArguments($actualArguments);
$this->assertSame($expectedArguments, $command->getArguments());
}
/**
* @group disconnected
*/
public function testParseResponse(): void
{
$this->assertSame('OK', $this->getCommand()->parseResponse('OK'));
}
/**
* @group disconnected
*/
public function testPrefixKeys(): void
{
$arguments = ['stream', '100-1'];
$expected = ['prefix:stream', '100-1'];
$command = $this->getCommandWithArgumentsArray($arguments);
$command->prefixKeys('prefix:');
$this->assertSame($expected, $command->getArguments());
}
/**
* @group connected
* @requiresRedisVersion >= 5.0.0
*/
public function testSetId(): void
{
$redis = $this->getClient();
$redis->xadd('stream', ['key0' => 'val0'], '0-1');
$this->assertEquals('OK', $redis->xsetid('stream', '1-1', 500, '1-1'));
// Attempt to set id less than top id
$redis->xadd('stream', ['key0' => 'val0'], '2-1');
$this->expectException(ServerException::class);
$redis->xsetid('stream', '1-1');
}
/**
* @group connected
* @group relay-incompatible
* @requiresRedisVersion >= 5.0.0
*/
public function testSetIdExtended(): void
{
$redis = $this->getClient();
$redis->xadd('stream', ['key0' => 'val0'], '0-1');
// Basic
$this->assertEquals('OK', $redis->xsetid('stream', '1-1'));
$xinfo = $redis->xinfo->stream('stream');
$this->assertSame('1-1', $xinfo['last-generated-id']);
// Extended
$this->assertEquals('OK', $redis->xsetid('stream', '2-1', 500, '1-1'));
$xinfo = $redis->xinfo->stream('stream');
$this->assertSame('2-1', $xinfo['last-generated-id']);
$this->assertSame(500, $xinfo['entries-added']);
$this->assertSame('1-1', $xinfo['max-deleted-entry-id']);
// Rewind
$this->assertEquals('OK', $redis->xsetid('stream', '1-1', 500, '1-1'));
$xinfo = $redis->xinfo->stream('stream');
$this->assertSame('1-1', $xinfo['last-generated-id']);
// Attempt to set id less than top id
$redis->xadd('stream', ['key0' => 'val0'], '2-1');
$this->expectException(ServerException::class);
$redis->xsetid('stream', '1-1');
}
public function argumentsProvider(): array
{
return [
'with default arguments' => [
['stream', '100-1'],
['stream', '100-1'],
],
'with ENTRIESADDED modifier' => [
['stream', '100-1', 500],
['stream', '100-1', 'ENTRIESADDED', 500],
],
'with MAXDELETEDID modifier' => [
['stream', '100-1', null, '50-1'],
['stream', '100-1', 'MAXDELETEDID', '50-1'],
],
'with all arguments' => [
['stream', '100-1', 500, '50-1'],
['stream', '100-1', 'ENTRIESADDED', 500, 'MAXDELETEDID', '50-1'],
],
];
}
}