mirror of
https://github.com/predis/predis.git
synced 2026-09-12 03:17:22 +00:00
Extend Sorted Set support by implementing BZPOPMAX command (#864)
* 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 * Added support for BZPOPMAX command * Added absract command to fix inheritance chain Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>
This commit is contained in:
committed by
GitHub
parent
65447807ed
commit
f26fd899f9
@@ -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 bzpopmax(array $keys, int $timeout)
|
||||
* @method $this bzpopmin(array $keys, int $timeout)
|
||||
* @method $this bzmpop(int $timeout, array $keys, string $modifier = 'min', int $count = 1)
|
||||
* @method $this decr($key)
|
||||
|
||||
@@ -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 bzpopmax(array $keys, int $timeout)
|
||||
* @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)
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Predis\Command\Redis\AbstractCommand;
|
||||
use Predis\Command\Command as RedisCommand;
|
||||
use Predis\Command\Traits\Keys;
|
||||
|
||||
abstract class BZPOPBase extends RedisCommand
|
||||
{
|
||||
use Keys {
|
||||
Keys::setArguments as setKeys;
|
||||
}
|
||||
|
||||
protected static $keysArgumentPositionOffset = 0;
|
||||
|
||||
abstract public function getId(): string;
|
||||
|
||||
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]]]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Predis\Command\Redis;
|
||||
|
||||
use Predis\Command\Redis\AbstractCommand\BZPOPBase;
|
||||
|
||||
/**
|
||||
* @link https://redis.io/commands/bzpopmax/
|
||||
*
|
||||
* BZPOPMAX is the blocking variant of the sorted set ZPOPMAX 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 highest 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 BZPOPMAX extends BZPOPBase
|
||||
{
|
||||
public function getId(): string
|
||||
{
|
||||
return 'BZPOPMAX';
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,7 @@
|
||||
|
||||
namespace Predis\Command\Redis;
|
||||
|
||||
use Predis\Command\Traits\Keys;
|
||||
use Predis\Command\Command as RedisCommand;
|
||||
use Predis\Command\Redis\AbstractCommand\BZPOPBase;
|
||||
|
||||
/**
|
||||
* @link https://redis.io/commands/bzpopmin/
|
||||
@@ -15,32 +14,10 @@ use Predis\Command\Command as RedisCommand;
|
||||
* 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
|
||||
class BZPOPMIN extends BZPOPBase
|
||||
{
|
||||
use Keys {
|
||||
Keys::setArguments as setKeys;
|
||||
}
|
||||
|
||||
protected static $keysArgumentPositionOffset = 0;
|
||||
|
||||
public function getId()
|
||||
public function getId(): string
|
||||
{
|
||||
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]]]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace Predis\Command\Redis\AbstractCommand;
|
||||
|
||||
use Predis\Command\CommandInterface;
|
||||
use PredisTestCase;
|
||||
|
||||
class BZPOPBaseTest extends PredisTestCase
|
||||
{
|
||||
/**
|
||||
* @var CommandInterface
|
||||
*/
|
||||
private $testCommand;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->testCommand = new class extends BZPOPBase {
|
||||
|
||||
public function getId(): string
|
||||
{
|
||||
return 'test';
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
* @dataProvider argumentsProvider
|
||||
*/
|
||||
public function testFilterArguments(array $actualArguments, array $expectedArguments): void
|
||||
{
|
||||
$this->testCommand->setArguments($actualArguments);
|
||||
|
||||
$this->assertSame($expectedArguments, $this->testCommand->getArguments());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
* @dataProvider responsesProvider
|
||||
*/
|
||||
public function testParseResponse(array $actualResponse, array $expectedResponse): void
|
||||
{
|
||||
$this->assertSame($expectedResponse, $this->testCommand->parseResponse($actualResponse));
|
||||
}
|
||||
|
||||
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']]
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Predis\Command\Redis;
|
||||
|
||||
use Predis\Response\ServerException;
|
||||
use UnexpectedValueException;
|
||||
|
||||
class BZPOPMAX_Test extends PredisCommandTestCase
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function getExpectedCommand(): string
|
||||
{
|
||||
return BZPOPMAX::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function getExpectedId(): string
|
||||
{
|
||||
return 'BZPOPMAX';
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @return void
|
||||
* @requiresRedisVersion >= 5.0.0
|
||||
*/
|
||||
public function testReturnsPoppedMaxElementFromGivenNonEmptySortedSet(): void
|
||||
{
|
||||
$redis = $this->getClient();
|
||||
$sortedSetDictionary = [1, 'member1', 2, 'member2', 3, 'member3'];
|
||||
$expectedResponse = ['test-bzpopmax' => ['member3' => '3']];
|
||||
$expectedModifiedSortedSet = ['member1', 'member2'];
|
||||
|
||||
$redis->zadd('test-bzpopmax', ...$sortedSetDictionary);
|
||||
|
||||
$this->assertSame($expectedResponse, $redis->bzpopmax(['empty sorted set','test-bzpopmax'], 0));
|
||||
$this->assertSame($expectedModifiedSortedSet, $redis->zrange('test-bzpopmax', 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->bzpopmax(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('bzpopmax_foo', 'bar');
|
||||
$redis->bzpopmax(['bzpopmax_foo'], 0);
|
||||
}
|
||||
}
|
||||
@@ -23,27 +23,6 @@ class BZPOPMIN_Test extends PredisCommandTestCase
|
||||
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
|
||||
@@ -91,32 +70,4 @@ class BZPOPMIN_Test extends PredisCommandTestCase
|
||||
$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']]
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user