Extended Sorted Set support by adding BZPOPMIN command (#862)

* Refactored zinterstore, zunionstore commands and command traits

* Merge conflicts resolve, update cluster strategy test with new arguments

* Updated assertion in case if command executed faster then duration minimal threshold

* Updated Keys trait to handle cases when no numkeys modifier needed

* Added support for BZPOPMIN command

* Added command link and description

Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>
This commit is contained in:
Vladyslav Vildanov
2022-12-12 18:38:26 +02:00
committed by GitHub
parent 7c0cc7bdbe
commit bc5892e260
6 changed files with 197 additions and 7 deletions
+1
View File
@@ -40,6 +40,7 @@ use Predis\Command\CommandInterface;
* @method $this bitop($operation, $destkey, $key)
* @method $this bitfield($key, $subcommand, ...$subcommandArg)
* @method $this bitpos($key, $bit, $start = null, $end = null)
* @method $this bzpopmin(array $keys, int $timeout)
* @method $this bzmpop(int $timeout, array $keys, string $modifier = 'min', int $count = 1)
* @method $this decr($key)
* @method $this decrby($key, $decrement)
+1
View File
@@ -49,6 +49,7 @@ use Predis\Response\Status;
* @method int bitop($operation, $destkey, $key)
* @method array|null bitfield(string $key, $subcommand, ...$subcommandArg)
* @method int bitpos(string $key, $bit, $start = null, $end = null)
* @method array bzpopmin(array $keys, int $timeout)
* @method array bzmpop(int $timeout, array $keys, string $modifier = 'min', int $count = 1)
* @method int decr(string $key)
* @method int decrby(string $key, int $decrement)
+46
View File
@@ -0,0 +1,46 @@
<?php
namespace Predis\Command\Redis;
use Predis\Command\Traits\Keys;
use Predis\Command\Command as RedisCommand;
/**
* @link https://redis.io/commands/bzpopmin/
*
* BZPOPMIN is the blocking variant of the sorted set ZPOPMIN primitive.
*
* It is the blocking version because it blocks the connection when there are
* no members to pop from any of the given sorted sets.
* A member with the lowest score is popped from first sorted set that is non-empty,
* with the given keys being checked in the order that they are given.
*/
class BZPOPMIN extends RedisCommand
{
use Keys {
Keys::setArguments as setKeys;
}
protected static $keysArgumentPositionOffset = 0;
public function getId()
{
return 'BZPOPMIN';
}
public function setArguments(array $arguments)
{
$this->setKeys($arguments, false);
}
public function parseResponse($data)
{
$key = array_shift($data);
if (null === $key) {
return [$key];
}
return array_combine([$key], [[$data[0] => $data[1]]]);
}
}
+8 -3
View File
@@ -10,7 +10,7 @@ use UnexpectedValueException;
*/
trait Keys
{
public function setArguments(array $arguments)
public function setArguments(array $arguments, bool $withNumkeys = true)
{
$argumentsLength = count($arguments);
@@ -22,10 +22,15 @@ trait Keys
}
$keysArgument = $arguments[static::$keysArgumentPositionOffset];
$numkeys = count($keysArgument);
$argumentsBeforeKeys = array_slice($arguments, 0, static::$keysArgumentPositionOffset);
$argumentsAfterKeys = array_slice($arguments, static::$keysArgumentPositionOffset + 1);
parent::setArguments(array_merge($argumentsBeforeKeys, [$numkeys], $keysArgument, $argumentsAfterKeys));
if ($withNumkeys) {
$numkeys = count($keysArgument);
parent::setArguments(array_merge($argumentsBeforeKeys, [$numkeys], $keysArgument, $argumentsAfterKeys));
return;
}
parent::setArguments(array_merge($argumentsBeforeKeys, $keysArgument, $argumentsAfterKeys));
}
}
@@ -0,0 +1,122 @@
<?php
namespace Predis\Command\Redis;
use Predis\Response\ServerException;
use UnexpectedValueException;
class BZPOPMIN_Test extends PredisCommandTestCase
{
/**
* @inheritDoc
*/
protected function getExpectedCommand(): string
{
return BZPOPMIN::class;
}
/**
* @inheritDoc
*/
protected function getExpectedId(): string
{
return 'BZPOPMIN';
}
/**
* @group disconnected
* @dataProvider argumentsProvider
*/
public function testFilterArguments(array $actualArguments, array $expectedArguments): void
{
$command = $this->getCommand();
$command->setArguments($actualArguments);
$this->assertSame($expectedArguments, $command->getArguments());
}
/**
* @group disconnected
* @dataProvider responsesProvider
*/
public function testParseResponse(array $actualResponse, array $expectedResponse): void
{
$this->assertSame($expectedResponse, $this->getCommand()->parseResponse($actualResponse));
}
/**
* @group connected
* @return void
* @requiresRedisVersion >= 5.0.0
*/
public function testReturnsPoppedMinElementFromGivenNonEmptySortedSet(): void
{
$redis = $this->getClient();
$sortedSetDictionary = [1, 'member1', 2, 'member2', 3, 'member3'];
$expectedResponse = ['test-bzpopmin' => ['member1' => '1']];
$expectedModifiedSortedSet = ['member2', 'member3'];
$redis->zadd('test-bzpopmin', ...$sortedSetDictionary);
$this->assertSame($expectedResponse, $redis->bzpopmin(['empty sorted set','test-bzpopmin'], 0));
$this->assertSame($expectedModifiedSortedSet, $redis->zrange('test-bzpopmin', 0, -1));
}
/**
* @group connected
* @return void
* @requiresRedisVersion >= 5.0.0
*/
public function testThrowsExceptionOnUnexpectedValueGiven(): void
{
$redis = $this->getClient();
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Wrong keys argument type or position offset');
$redis->bzpopmin(1, 0);
}
/**
* @group connected
* @requiresRedisVersion >= 5.0.0
*/
public function testThrowsExceptionOnWrongType(): void
{
$this->expectException(ServerException::class);
$this->expectExceptionMessage('Operation against a key holding the wrong kind of value');
$redis = $this->getClient();
$redis->set('bzpopmin_foo', 'bar');
$redis->bzpopmin(['bzpopmin_foo'], 0);
}
public function argumentsProvider(): array
{
return [
'with one key' => [
[['key1'], 1],
['key1', 1]
],
'with multiple keys' => [
[['key1', 'key2', 'key3'], 1],
['key1', 'key2', 'key3', 1]
],
];
}
public function responsesProvider(): array
{
return [
'null-element array' => [
[null],
[null]
],
'three-element array' => [
['key', 'member', 'score'],
['key' => ['member' => 'score']]
],
];
}
}
+19 -4
View File
@@ -29,15 +29,20 @@ class KeysTest extends PredisTestCase
/**
* @dataProvider argumentsProvider
* @param int $offset
* @param bool $withNumkeys
* @param array $actualArguments
* @param array $expectedArguments
* @return void
*/
public function testReturnsCorrectArguments(int $offset, array $actualArguments, array $expectedArguments): void
{
public function testReturnsCorrectArguments(
int $offset,
bool $withNumkeys,
array $actualArguments,
array $expectedArguments
): void {
$this->testClass::$keysArgumentPositionOffset = $offset;
$this->testClass->setArguments($actualArguments);
$this->testClass->setArguments($actualArguments, $withNumkeys);
$this->assertSame($expectedArguments, $this->testClass->getArguments());
}
@@ -63,24 +68,34 @@ class KeysTest extends PredisTestCase
return [
'keys argument first and there is arguments after' => [
0,
true,
[['key1', 'key2'], 'second argument', 'third argument'],
[2, 'key1', 'key2', 'second argument', 'third argument']
],
'keys argument last and there is arguments before' => [
2,
true,
['first argument', 'second argument', ['key1', 'key2']],
['first argument', 'second argument', 2, 'key1', 'key2']
],
'keys argument not the first and not the last' => [
1,
true,
['first argument', ['key1', 'key2'], 'third argument'],
['first argument', 2, 'key1', 'key2', 'third argument']
],
'keys argument the only argument' => [
0,
true,
[['key1', 'key2']],
[2, 'key1', 'key2']
]
],
'without numkeys modifier' => [
0,
false,
[['key1', 'key2']],
['key1', 'key2'],
],
];
}