From 067b0519006c4fce2204ffe33d1cde25cb3dcc51 Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Wed, 19 Mar 2025 09:57:52 +0200 Subject: [PATCH] Added missing FT._LIST and BITFIELD_RO commands (#1521) * Added missing FT._LIST and BITFIELD_RO commands * Codestyle fixes --- src/ClientContextInterface.php | 2 + src/ClientInterface.php | 2 + src/Command/Redis/BITFIELD_RO.php | 44 +++++++++++ src/Command/Redis/Search/FT_LIST.php | 26 +++++++ .../Predis/Command/Redis/BITFIELD_RO_Test.php | 76 +++++++++++++++++++ .../Command/Redis/Search/FT_LIST_Test.php | 60 +++++++++++++++ 6 files changed, 210 insertions(+) create mode 100644 src/Command/Redis/BITFIELD_RO.php create mode 100644 src/Command/Redis/Search/FT_LIST.php create mode 100644 tests/Predis/Command/Redis/BITFIELD_RO_Test.php create mode 100644 tests/Predis/Command/Redis/Search/FT_LIST_Test.php diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 982d93a4..3609baa6 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -83,6 +83,7 @@ use Predis\Command\Redis\Container\Search\FTCURSOR; * @method $this bitcount(string $key, $start = null, $end = null, string $index = 'byte') * @method $this bitop($operation, $destkey, $key) * @method $this bitfield($key, $subcommand, ...$subcommandArg) + * @method $this bitfield_ro(string $key, ?array $encodingOffsetMap = null) * @method $this bitpos($key, $bit, $start = null, $end = null, string $index = 'byte') * @method $this blmpop(int $timeout, array $keys, string $modifier = 'left', int $count = 1) * @method $this bzpopmax(array $keys, int $timeout) @@ -111,6 +112,7 @@ use Predis\Command\Redis\Container\Search\FTCURSOR; * @method $this failover(?To $to = null, bool $abort = false, int $timeout = -1) * @method $this fcall(string $function, array $keys, ...$args) * @method $this fcall_ro(string $function, array $keys, ...$args) + * @method $this ft_list() * @method $this ftaggregate(string $index, string $query, ?AggregateArguments $arguments = null) * @method $this ftaliasadd(string $alias, string $index) * @method $this ftaliasdel(string $alias) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 963a0d79..d4d8aa69 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -92,6 +92,7 @@ use Predis\Response\Status; * @method int bitcount(string $key, $start = null, $end = null, string $index = 'byte') * @method int bitop($operation, $destkey, $key) * @method array|null bitfield(string $key, $subcommand, ...$subcommandArg) + * @method array|null bitfield_ro(string $key, ?array $encodingOffsetMap = null) * @method int bitpos(string $key, $bit, $start = null, $end = null, string $index = 'byte') * @method array blmpop(int $timeout, array $keys, string $modifier = 'left', int $count = 1) * @method array bzpopmax(array $keys, int $timeout) @@ -120,6 +121,7 @@ use Predis\Response\Status; * @method Status failover(?To $to = null, bool $abort = false, int $timeout = -1) * @method mixed fcall(string $function, array $keys, ...$args) * @method mixed fcall_ro(string $function, array $keys, ...$args) + * @method array ft_list() * @method array ftaggregate(string $index, string $query, ?AggregateArguments $arguments = null) * @method Status ftaliasadd(string $alias, string $index) * @method Status ftaliasdel(string $alias) diff --git a/src/Command/Redis/BITFIELD_RO.php b/src/Command/Redis/BITFIELD_RO.php new file mode 100644 index 00000000..ad74560a --- /dev/null +++ b/src/Command/Redis/BITFIELD_RO.php @@ -0,0 +1,44 @@ + offset, into GET, encoding, offset + array_walk($arguments[1], function ($value, $key) use (&$processedArguments) { + array_push($processedArguments, 'GET', $key, $value); + }); + } + + parent::setArguments($processedArguments); + } +} diff --git a/src/Command/Redis/Search/FT_LIST.php b/src/Command/Redis/Search/FT_LIST.php new file mode 100644 index 00000000..45e09e1a --- /dev/null +++ b/src/Command/Redis/Search/FT_LIST.php @@ -0,0 +1,26 @@ +getCommand(); + $command->setArguments($actualArguments); + + $this->assertSame($expectedArguments, $command->getArguments()); + } + + /** + * @group connected + * @return void + * @requiresRedisVersion >= 6.2.0 + */ + public function testReturnBitsOfSpecificString() + { + $redis = $this->getClient(); + + $redis->set('foo', 'bar'); + + $this->assertSame([6, 98], $redis->bitfield_ro('foo', ['u4' => 0, 'i8' => 0])); + } + + public function argumentsProvider(): array + { + return [ + 'with default arguments' => [ + ['key'], + ['key'], + ], + 'with single encoding-offset entry' => [ + ['key', ['encoding' => 'offset']], + ['key', 'GET', 'encoding', 'offset'], + ], + 'with multiple encoding-offset entry' => [ + ['key', ['encoding' => 'offset', 'encoding1' => 'offset1', 'encoding2' => 'offset2']], + ['key', 'GET', 'encoding', 'offset', 'GET', 'encoding1', 'offset1', 'GET', 'encoding2', 'offset2'], + ], + ]; + } +} diff --git a/tests/Predis/Command/Redis/Search/FT_LIST_Test.php b/tests/Predis/Command/Redis/Search/FT_LIST_Test.php new file mode 100644 index 00000000..ed784c92 --- /dev/null +++ b/tests/Predis/Command/Redis/Search/FT_LIST_Test.php @@ -0,0 +1,60 @@ +assertEmpty($this->getCommand()->getArguments()); + } + + /** + * @group connected + * @return void + * @requiresRediSearchVersion >= 2.0.0 + */ + public function testReturnListOfExistingIndices(): void + { + $redis = $this->getClient(); + + $this->assertEquals('OK', $redis->ftcreate('idx1', [new TextField('text')])); + $this->assertEquals('OK', $redis->ftcreate('idx2', [new TextField('text')])); + + $this->sleep(0.1); + + $this->assertSameValues(['idx1', 'idx2'], $redis->ft_list()); + } +}