Added support for CF.INFO command (#1082)

Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>
This commit is contained in:
Vladyslav Vildanov
2023-01-31 16:12:18 +02:00
committed by GitHub
parent 00248ea3db
commit 92d4306dc4
5 changed files with 185 additions and 1 deletions
+1
View File
@@ -64,6 +64,7 @@ use Predis\Command\CommandInterface;
* @method $this cfcount(string $key, $item)
* @method $this cfexists(string $key, $item)
* @method $this cfmexists(string $key, ...$item)
* @method $this cfinfo(string $key)
* @method $this decr($key)
* @method $this decrby($key, $decrement)
* @method $this failover(?To $to = null, bool $abort = false, int $timeout = -1)
+1
View File
@@ -73,6 +73,7 @@ use Predis\Response\Status;
* @method int cfcount(string $key, $item)
* @method int cfexists(string $key, $item)
* @method int cfmexists(string $key, ...$item)
* @method array cfinfo(string $key)
* @method int decr(string $key)
* @method int decrby(string $key, int $decrement)
* @method Status failover(?To $to = null, bool $abort = false, int $timeout = -1)
+1 -1
View File
@@ -66,7 +66,7 @@ class BFINFO extends RedisCommand
$result = [];
for ($i = 0, $iMax = count($data); $i < $iMax; ++$i) {
if ($data[$i + 1] ?? false) {
if (array_key_exists($i + 1, $data)) {
$result[(string) $data[$i]] = $data[++$i];
}
}
+45
View File
@@ -0,0 +1,45 @@
<?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\CuckooFilter;
use Predis\Command\Command as RedisCommand;
/**
* @see https://redis.io/commands/cf.info/
*
* Return information about key
*/
class CFINFO extends RedisCommand
{
public function getId()
{
return 'CF.INFO';
}
public function parseResponse($data)
{
if (count($data) > 1) {
$result = [];
for ($i = 0, $iMax = count($data); $i < $iMax; ++$i) {
if (array_key_exists($i + 1, $data)) {
$result[(string) $data[$i]] = $data[++$i];
}
}
return $result;
}
return $data;
}
}
@@ -0,0 +1,137 @@
<?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\CuckooFilter;
use Predis\Command\Redis\PredisCommandTestCase;
use Predis\Response\ServerException;
class CFINFO_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return CFINFO::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'CFINFO';
}
/**
* @group disconnected
*/
public function testFilterArguments(): void
{
$actualArguments = ['key'];
$expectedArguments = ['key'];
$command = $this->getCommand();
$command->setArguments($actualArguments);
$this->assertSameValues($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
* @requiresRedisBfVersion >= 1.0.0
*/
public function testInfoReturnsInformationAboutGivenCuckooFilter(): void
{
$redis = $this->getClient();
$expectedResponse = [
'Size' => 1080,
'Number of buckets' => 512,
'Number of filters' => 1,
'Number of items inserted' => 1,
'Number of items deleted' => 0,
'Bucket size' => 2,
'Expansion rate' => 1,
'Max iterations' => 20,
];
$redis->cfadd('key', 'item');
$this->assertSame($expectedResponse, $redis->cfinfo('key'));
}
/**
* @group connected
* @return void
* @requiresRedisBfVersion >= 1.0.0
*/
public function testInfoThrowsExceptionOnNonExistingFilterKeyGiven(): void
{
$redis = $this->getClient();
$this->expectException(ServerException::class);
$this->expectExceptionMessage('ERR not found');
$redis->cfinfo('non_existing_key');
}
public function responsesProvider(): array
{
return [
'with one modifier' => [
[100],
[100],
],
'with all modifiers' => [
[
'Size',
100,
'Number of buckets',
296,
'Number of filter',
1,
'Number of items inserted',
1,
'Number of items deleted',
1,
'Bucket size',
0,
'Expansion rate',
1,
'Max iteration',
20,
],
[
'Size' => 100,
'Number of buckets' => 296,
'Number of filter' => 1,
'Number of items inserted' => 1,
'Number of items deleted' => 1,
'Bucket size' => 0,
'Expansion rate' => 1,
'Max iteration' => 20,
],
],
];
}
}