Skip to content

Commit 54ad9a7

Browse files
authored
Merge branch 'main' into xpending-command
2 parents 5155d97 + 0a8fe54 commit 54ad9a7

File tree

5 files changed

+271
-0
lines changed

5 files changed

+271
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
### Added
55
- Add experimental support for vector sets commands (#1550)
66
- Added support for `XACK` command (#1555)
7+
- Added support for `XCLAIM` command (#1557)
78
- Added support for `XPENDING` command (#1558)
89

910
### Changed

src/ClientContextInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@
287287
* @method $this xack(string $key, string $group, string ...$id)
288288
* @method $this xadd(string $key, array $dictionary, string $id = '*', array $options = null)
289289
* @method $this xautoclaim(string $key, string $group, string $consumer, int $minIdleTime, string $start, ?int $count = null, bool $justId = false)
290+
* @method $this xclaim(string $key, string $group, string $consumer, int $minIdleTime, string|array $ids, ?int $idle = null, ?int $time = null, ?int $retryCount = null, bool $force = false, bool $justId = false, ?string $lastId = null)
290291
* @method $this xdel(string $key, string ...$id)
291292
* @method $this xlen(string $key)
292293
* @method $this xpending(string $key, string $group, ?int $minIdleTime = null, ?string $start = null, ?string $end = null, ?int $count = null, ?string $consumer = null)

src/ClientInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@
298298
* @method int xack(string $key, string $group, string ...$id)
299299
* @method string xadd(string $key, array $dictionary, string $id = '*', array $options = null)
300300
* @method array xautoclaim(string $key, string $group, string $consumer, int $minIdleTime, string $start, ?int $count = null, bool $justId = false)
301+
* @method array xclaim(string $key, string $group, string $consumer, int $minIdleTime, string|array $ids, ?int $idle = null, ?int $time = null, ?int $retryCount = null, bool $force = false, bool $justId = false, ?string $lastId = null)
301302
* @method int xdel(string $key, string ...$id)
302303
* @method int xlen(string $key)
303304
* @method array xpending(string $key, string $group, ?int $minIdleTime = null, ?string $start = null, ?string $end = null, ?int $count = null, ?string $consumer = null)

src/Command/Redis/XCLAIM.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Predis package.
5+
*
6+
* (c) 2009-2020 Daniele Alessandri
7+
* (c) 2021-2025 Till Krüss
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace Predis\Command\Redis;
14+
15+
use Predis\Command\PrefixableCommand as RedisCommand;
16+
use Predis\Command\Redis\Utils\CommandUtility;
17+
18+
/**
19+
* @see http://redis.io/commands/xclaim
20+
*/
21+
class XCLAIM extends RedisCommand
22+
{
23+
public function getId(): string
24+
{
25+
return 'XCLAIM';
26+
}
27+
28+
public function setArguments(array $arguments): void
29+
{
30+
if (count($arguments) < 5) {
31+
return;
32+
}
33+
34+
$processedArguments = array_slice($arguments, 0, 4);
35+
$ids = $arguments[4];
36+
$processedArguments = array_merge($processedArguments, is_array($ids) ? $ids : [$ids]);
37+
38+
if (array_key_exists(5, $arguments) && null !== $arguments[5]) {
39+
array_push($processedArguments, 'IDLE', $arguments[5]);
40+
}
41+
42+
if (array_key_exists(6, $arguments) && null !== $arguments[6]) {
43+
array_push($processedArguments, 'TIME', $arguments[6]);
44+
}
45+
46+
if (array_key_exists(7, $arguments) && null !== $arguments[7]) {
47+
array_push($processedArguments, 'RETRYCOUNT', $arguments[7]);
48+
}
49+
50+
if (array_key_exists(8, $arguments) && false !== $arguments[8]) {
51+
$processedArguments[] = 'FORCE';
52+
}
53+
54+
if (array_key_exists(9, $arguments) && false !== $arguments[9]) {
55+
$processedArguments[] = 'JUSTID';
56+
}
57+
58+
if (array_key_exists(10, $arguments) && false !== $arguments[10]) {
59+
array_push($processedArguments, 'LASTID', $arguments[10]);
60+
}
61+
62+
parent::setArguments($processedArguments);
63+
}
64+
65+
public function parseResponse($data): array
66+
{
67+
// JUSTID format
68+
if (isset($data[0]) && !is_array($data[0])) {
69+
return $data;
70+
}
71+
72+
$result = [];
73+
foreach ($data as [$id, $kvDict]) {
74+
$result[$id] = CommandUtility::arrayToDictionary($kvDict);
75+
}
76+
77+
return $result;
78+
}
79+
80+
public function parseResp3Response($data): array
81+
{
82+
return $this->parseResponse($data);
83+
}
84+
85+
public function prefixKeys($prefix)
86+
{
87+
$this->applyPrefixForFirstArgument($prefix);
88+
}
89+
}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Predis package.
5+
*
6+
* (c) 2009-2020 Daniele Alessandri
7+
* (c) 2021-2025 Till Krüss
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace Predis\Command\Redis;
14+
15+
use Predis\ClientInterface;
16+
17+
/**
18+
* @group commands
19+
* @group realm-stream
20+
*/
21+
class XCLAIM_Test extends PredisCommandTestCase
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
protected function getExpectedCommand(): string
27+
{
28+
return 'Predis\Command\Redis\XCLAIM';
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
protected function getExpectedId(): string
35+
{
36+
return 'XCLAIM';
37+
}
38+
39+
/**
40+
* @dataProvider argumentsProvider
41+
* @group disconnected
42+
*/
43+
public function testFilterArguments(array $actualArguments, array $expectedArguments): void
44+
{
45+
$command = $this->getCommand();
46+
$command->setArguments($actualArguments);
47+
48+
$this->assertSame($expectedArguments, $command->getArguments());
49+
}
50+
51+
/**
52+
* @group disconnected
53+
*/
54+
public function testParseResponse(): void
55+
{
56+
$command = $this->getCommand();
57+
58+
$raw = [['1-1', ['key1', 'val1']], ['2-1', ['key2', 'val2']]];
59+
$expected = ['1-1' => ['key1' => 'val1'], '2-1' => ['key2' => 'val2']];
60+
$this->assertSame($expected, $command->parseResponse($raw));
61+
$this->assertSame($expected, $command->parseResp3Response($raw));
62+
63+
// JUSTID format
64+
$raw = ['1-1', '2-1'];
65+
$expected = ['1-1', '2-1'];
66+
$this->assertSame($expected, $command->parseResponse($raw));
67+
$this->assertSame($expected, $command->parseResp3Response($raw));
68+
}
69+
70+
/**
71+
* @group disconnected
72+
*/
73+
public function testPrefixKeys(): void
74+
{
75+
$arguments = ['stream', 'group', 'consumer', 0, 'id1'];
76+
$expected = ['prefix:stream', 'group', 'consumer', 0, 'id1'];
77+
78+
$command = $this->getCommandWithArgumentsArray($arguments);
79+
$command->prefixKeys('prefix:');
80+
81+
$this->assertSame($expected, $command->getArguments());
82+
}
83+
84+
/**
85+
* @group connected
86+
* @requiresRedisVersion >= 5.0.0
87+
*/
88+
public function testClaim(): void
89+
{
90+
$redis = $this->getClient();
91+
$this->testClaimWithClient($redis);
92+
}
93+
94+
/**
95+
* @group connected
96+
* @requiresRedisVersion >= 5.0.0
97+
*/
98+
public function testClaimResp3(): void
99+
{
100+
$redis = $this->getResp3Client();
101+
$this->testClaimWithClient($redis);
102+
}
103+
104+
private function testClaimWithClient(ClientInterface $redis): void
105+
{
106+
$redis->xadd('stream', ['key0' => 'val0'], '0-1');
107+
$redis->xadd('stream', ['key1' => 'val1'], '1-1');
108+
$redis->xadd('stream', ['key2' => 'val2'], '2-1');
109+
$redis->xadd('stream', ['key3' => 'val3'], '3-1');
110+
111+
$redis->xgroup->create('stream', 'group', '0');
112+
113+
$redis->xreadgroup('group', 'consumer1', 4, null, false, 'stream', '>');
114+
115+
// Claim one
116+
$claimed = $redis->xclaim('stream', 'group', 'consumer2', 0, '0-1');
117+
$this->assertSame(['0-1' => ['key0' => 'val0']], $claimed);
118+
119+
// Claim many
120+
$claimed = $redis->xclaim('stream', 'group', 'consumer2', 0, ['1-1', '2-1']);
121+
$this->assertSame(['1-1' => ['key1' => 'val1'], '2-1' => ['key2' => 'val2']], $claimed);
122+
123+
// Claim deleted
124+
$redis->xdel('stream', '1-1');
125+
$claimed = $redis->xclaim('stream', 'group', 'consumer3', 0, ['0-1', '1-1', '2-1'], null, null, null, false, true);
126+
$this->assertSame(['0-1', '2-1'], $claimed);
127+
128+
// Claim with all options
129+
$redis->xdel('stream', '1-1');
130+
$claimed = $redis->xclaim('stream', 'group', 'consumer3', 0, '3-1', 10, 100, 5, true, true, '3-1');
131+
$this->assertSame(['3-1'], $claimed);
132+
133+
// Claim unknown
134+
$claimed = $redis->xclaim('stream', 'group', 'consumer3', 0, ['4-1']);
135+
$this->assertSame([], $claimed);
136+
}
137+
138+
public function argumentsProvider(): array
139+
{
140+
return [
141+
'with default arguments' => [
142+
['stream', 'group', 'consumer', 0, 'id1'],
143+
['stream', 'group', 'consumer', 0, 'id1'],
144+
],
145+
'with array ids' => [
146+
['stream', 'group', 'consumer', 0, ['id1', 'id2']],
147+
['stream', 'group', 'consumer', 0, 'id1', 'id2'],
148+
],
149+
'with IDLE modifier' => [
150+
['stream', 'group', 'consumer', 0, ['id1', 'id2'], 10],
151+
['stream', 'group', 'consumer', 0, 'id1', 'id2', 'IDLE', 10],
152+
],
153+
'with TIME modifier' => [
154+
['stream', 'group', 'consumer', 0, ['id1', 'id2'], null, 12345],
155+
['stream', 'group', 'consumer', 0, 'id1', 'id2', 'TIME', 12345],
156+
],
157+
'with RETRYCOUNT modifier' => [
158+
['stream', 'group', 'consumer', 0, ['id1', 'id2'], null, null, 5],
159+
['stream', 'group', 'consumer', 0, 'id1', 'id2', 'RETRYCOUNT', 5],
160+
],
161+
'with FORCE modifier' => [
162+
['stream', 'group', 'consumer', 0, ['id1', 'id2'], null, null, null, true],
163+
['stream', 'group', 'consumer', 0, 'id1', 'id2', 'FORCE'],
164+
],
165+
'with JUSTID modifier' => [
166+
['stream', 'group', 'consumer', 0, ['id1', 'id2'], null, null, null, false, true],
167+
['stream', 'group', 'consumer', 0, 'id1', 'id2', 'JUSTID'],
168+
],
169+
'with LASTID modifier' => [
170+
['stream', 'group', 'consumer', 0, ['id1', 'id2'], null, null, null, false, false, '1-1'],
171+
['stream', 'group', 'consumer', 0, 'id1', 'id2', 'LASTID', '1-1'],
172+
],
173+
'with all arguments' => [
174+
['stream', 'group', 'consumer', 100, ['id1', 'id2'], 10, 12345, 5, true, true, '1-1'],
175+
['stream', 'group', 'consumer', 100, 'id1', 'id2', 'IDLE', 10, 'TIME', 12345, 'RETRYCOUNT', 5, 'FORCE', 'JUSTID', 'LASTID', '1-1'],
176+
],
177+
];
178+
}
179+
}

0 commit comments

Comments
 (0)