From 33e1174a4f79e738eb8d1c7c63d9c9e8921dc10a Mon Sep 17 00:00:00 2001 From: alschastny <91664363+alschastny@users.noreply.github.com> Date: Mon, 16 Jun 2025 10:11:51 +0300 Subject: [PATCH] implement XSETID command (#1559) * implement XSETID command * modify CHANGELOG.md * split test to basic and extended version --------- Co-authored-by: aleksanders --- CHANGELOG.md | 1 + src/ClientContextInterface.php | 1 + src/ClientInterface.php | 1 + src/Command/Redis/XSETID.php | 49 +++++++ tests/Predis/Command/Redis/XSETID_Test.php | 144 +++++++++++++++++++++ 5 files changed, 196 insertions(+) create mode 100644 src/Command/Redis/XSETID.php create mode 100644 tests/Predis/Command/Redis/XSETID_Test.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 10a71811..a9e36531 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 3a0f4303..72d43b4f 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -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) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index a97ff781..1c52d1d2 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -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) diff --git a/src/Command/Redis/XSETID.php b/src/Command/Redis/XSETID.php new file mode 100644 index 00000000..18185829 --- /dev/null +++ b/src/Command/Redis/XSETID.php @@ -0,0 +1,49 @@ +applyPrefixForFirstArgument($prefix); + } +} diff --git a/tests/Predis/Command/Redis/XSETID_Test.php b/tests/Predis/Command/Redis/XSETID_Test.php new file mode 100644 index 00000000..ff887a15 --- /dev/null +++ b/tests/Predis/Command/Redis/XSETID_Test.php @@ -0,0 +1,144 @@ +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'], + ], + ]; + } +}