-
-
Notifications
You must be signed in to change notification settings - Fork 989
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extended core support by implementing SORT_RO command (#1044)
* Added support for SORT_RO command * Codestyle fixes * Added command description --------- Co-authored-by: Vladyslav Vildanov <[email protected]>
- Loading branch information
1 parent
50e005b
commit db65e5e
Showing
18 changed files
with
738 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Predis package. | ||
* | ||
* (c) 2009-2020 Daniele Alessandri | ||
* (c) 2021-2023 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\Argument\Server; | ||
|
||
use Predis\Command\Argument\ArrayableArgument; | ||
|
||
interface LimitInterface extends ArrayableArgument | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Predis package. | ||
* | ||
* (c) 2009-2020 Daniele Alessandri | ||
* (c) 2021-2023 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\Argument\Server; | ||
|
||
class LimitOffsetCount implements LimitInterface | ||
{ | ||
private const KEYWORD = 'LIMIT'; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $offset; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $count; | ||
|
||
public function __construct(int $offset, int $count) | ||
{ | ||
$this->offset = $offset; | ||
$this->count = $count; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return [self::KEYWORD, $this->offset, $this->count]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Predis package. | ||
* | ||
* (c) 2009-2020 Daniele Alessandri | ||
* (c) 2021-2023 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\Command as RedisCommand; | ||
use Predis\Command\Traits\By\ByArgument; | ||
use Predis\Command\Traits\Get\Get; | ||
use Predis\Command\Traits\Limit\LimitObject; | ||
use Predis\Command\Traits\Sorting; | ||
|
||
/** | ||
* @see https://redis.io/commands/sort_ro/ | ||
* | ||
* Read-only variant of the SORT command. | ||
* It is exactly like the original SORT but refuses the STORE option | ||
* and can safely be used in read-only replicas. | ||
*/ | ||
class SORT_RO extends RedisCommand | ||
{ | ||
use ByArgument { | ||
ByArgument::setArguments as setBy; | ||
} | ||
use LimitObject { | ||
LimitObject::setArguments as setLimit; | ||
} | ||
use Get { | ||
Get::setArguments as setGetArgument; | ||
} | ||
use Sorting { | ||
Sorting::setArguments as setSorting; | ||
} | ||
|
||
protected static $byArgumentPositionOffset = 1; | ||
protected static $getArgumentPositionOffset = 3; | ||
protected static $sortArgumentPositionOffset = 4; | ||
|
||
public function getId() | ||
{ | ||
return 'SORT_RO'; | ||
} | ||
|
||
public function setArguments(array $arguments) | ||
{ | ||
$alpha = array_pop($arguments); | ||
|
||
if (is_bool($alpha) && $alpha) { | ||
$arguments[] = 'ALPHA'; | ||
} elseif (!is_bool($alpha)) { | ||
$arguments[] = $alpha; | ||
} | ||
|
||
$this->setSorting($arguments); | ||
$arguments = $this->getArguments(); | ||
|
||
$this->setGetArgument($arguments); | ||
$arguments = $this->getArguments(); | ||
|
||
$this->setLimit($arguments); | ||
$arguments = $this->getArguments(); | ||
|
||
$this->setBy($arguments); | ||
$this->filterArguments(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Predis package. | ||
* | ||
* (c) 2009-2020 Daniele Alessandri | ||
* (c) 2021-2023 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\Traits\By; | ||
|
||
use Predis\Command\Command; | ||
|
||
/** | ||
* @mixin Command | ||
*/ | ||
trait ByArgument | ||
{ | ||
private $byModifier = 'BY'; | ||
|
||
public function setArguments(array $arguments) | ||
{ | ||
$argumentsLength = count($arguments); | ||
|
||
if (static::$byArgumentPositionOffset >= $argumentsLength || null === $arguments[static::$byArgumentPositionOffset]) { | ||
parent::setArguments($arguments); | ||
|
||
return; | ||
} | ||
|
||
$argument = $arguments[static::$byArgumentPositionOffset]; | ||
$argumentsBefore = array_slice($arguments, 0, static::$byArgumentPositionOffset); | ||
$argumentsAfter = array_slice($arguments, static::$byArgumentPositionOffset + 1); | ||
|
||
parent::setArguments(array_merge($argumentsBefore, [$this->byModifier, $argument], $argumentsAfter)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Predis package. | ||
* | ||
* (c) 2009-2020 Daniele Alessandri | ||
* (c) 2021-2023 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\Traits\Get; | ||
|
||
use UnexpectedValueException; | ||
|
||
trait Get | ||
{ | ||
private static $getModifier = 'GET'; | ||
|
||
public function setArguments(array $arguments) | ||
{ | ||
$argumentsLength = count($arguments); | ||
|
||
if (static::$getArgumentPositionOffset >= $argumentsLength) { | ||
parent::setArguments($arguments); | ||
|
||
return; | ||
} | ||
|
||
if (!is_array($arguments[static::$getArgumentPositionOffset])) { | ||
throw new UnexpectedValueException('Wrong get argument type'); | ||
} | ||
|
||
$patterns = []; | ||
|
||
foreach ($arguments[static::$getArgumentPositionOffset] as $pattern) { | ||
$patterns[] = self::$getModifier; | ||
$patterns[] = $pattern; | ||
} | ||
|
||
$argumentsBeforeKeys = array_slice($arguments, 0, static::$getArgumentPositionOffset); | ||
$argumentsAfterKeys = array_slice($arguments, static::$getArgumentPositionOffset + 1); | ||
|
||
parent::setArguments(array_merge($argumentsBeforeKeys, $patterns, $argumentsAfterKeys)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Predis package. | ||
* | ||
* (c) 2009-2020 Daniele Alessandri | ||
* (c) 2021-2023 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\Traits\Limit; | ||
|
||
use Predis\Command\Argument\Server\LimitInterface; | ||
|
||
trait LimitObject | ||
{ | ||
public function setArguments(array $arguments) | ||
{ | ||
$argumentPositionOffset = $this->getLimitArgumentPositionOffset($arguments); | ||
|
||
if (null === $argumentPositionOffset) { | ||
parent::setArguments($arguments); | ||
|
||
return; | ||
} | ||
|
||
$limitObject = $arguments[$argumentPositionOffset]; | ||
$argumentsBefore = array_slice($arguments, 0, $argumentPositionOffset); | ||
$argumentsAfter = array_slice($arguments, $argumentPositionOffset + 1); | ||
|
||
parent::setArguments(array_merge( | ||
$argumentsBefore, | ||
$limitObject->toArray(), | ||
$argumentsAfter | ||
)); | ||
} | ||
|
||
private function getLimitArgumentPositionOffset(array $arguments): ?int | ||
{ | ||
foreach ($arguments as $i => $value) { | ||
if ($value instanceof LimitInterface) { | ||
return $i; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
Oops, something went wrong.