Added support for CF.INSERTNX command (#1087)

Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>
This commit is contained in:
Vladyslav Vildanov
2023-02-01 14:55:23 +02:00
committed by GitHub
parent ac8e021c57
commit d04f06a2a3
4 changed files with 194 additions and 0 deletions
+2
View File
@@ -68,8 +68,10 @@ use Predis\Command\CommandInterface;
* @method $this cfmexists(string $key, ...$item)
* @method $this cfinfo(string $key)
* @method $this cfinsert(string $key, int $capacity = -1, bool $noCreate = false, string ...$item)
* @method $this cfinsertnx(string $key, int $capacity = -1, bool $noCreate = false, string ...$item)
* @method $this cfreserve(string $key, int $capacity, int $bucketSize = -1, int $maxIterations = -1, int $expansion = -1)
* @method $this cfscandump(string $key, int $iterator) * @method $this decr($key)
* @method $this decr($key)
* @method $this decrby($key, $decrement)
* @method $this failover(?To $to = null, bool $abort = false, int $timeout = -1)
* @method $this get($key)
+1
View File
@@ -77,6 +77,7 @@ use Predis\Response\Status;
* @method int cfmexists(string $key, ...$item)
* @method array cfinfo(string $key)
* @method array cfinsert(string $key, int $capacity = -1, bool $noCreate = false, string ...$item)
* @method array cfinsertnx(string $key, int $capacity = -1, bool $noCreate = false, string ...$item)
* @method Status cfreserve(string $key, int $capacity, int $bucketSize = -1, int $maxIterations = -1, int $expansion = -1)
* @method array cfscandump(string $key, int $iterator)
* @method int decr(string $key)
@@ -0,0 +1,27 @@
<?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;
/**
* @see https://redis.io/commands/cf.insertnx/
*
* Adds one or more items to a cuckoo filter, allowing the filter
* to be created with a custom capacity if it does not exist yet.
*/
class CFINSERTNX extends CFINSERT
{
public function getId()
{
return 'CF.INSERTNX';
}
}
@@ -0,0 +1,164 @@
<?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 CFINSERTNX_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return CFINSERTNX::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'CFINSERTNX';
}
/**
* @group disconnected
* @dataProvider argumentsProvider
*/
public function testFilterArguments(array $actualArguments, array $expectedArguments): void
{
$command = $this->getCommand();
$command->setArguments($actualArguments);
$this->assertSameValues($expectedArguments, $command->getArguments());
}
/**
* @group connected
* @dataProvider filtersProvider
* @param array $filterArguments
* @param string $key
* @param int $expectedCapacity
* @param array $expectedResponse
* @return void
* @requiresRedisBfVersion >= 1.0.0
*/
public function testInsertItemsIntoNonExistingCuckooFilter(
array $filterArguments,
string $key,
int $expectedCapacity,
array $expectedResponse
): void {
$redis = $this->getClient();
$actualResponse = $redis->cfinsertnx(...$filterArguments);
$info = $redis->cfinfo($key);
$this->assertSame($expectedResponse, $actualResponse);
$this->assertSame($expectedCapacity, $info['Size']);
}
/**
* @group connected
* @return void
* @requiresRedisBfVersion >= 1.0.0
*/
public function testDoNotInsertAlreadyExistingItems(): void
{
$redis = $this->getClient();
$redis->cfadd('filter', 'item1');
$redis->cfadd('filter', 'item2');
$actualResponse = $redis->cfinsertnx('filter', -1, false, 'item1', 'item2');
$this->assertSame([0, 0], $actualResponse);
}
/**
* @group connected
* @return void
* @requiresRedisBfVersion >= 1.0.0
*/
public function testInsertThrowsErrorOnInsertingIntoNonExistingFilterWithNoCreateModifier(): void
{
$redis = $this->getClient();
$this->expectException(ServerException::class);
$this->expectExceptionMessage('ERR not found');
$redis->cfinsertnx('key', -1, true, 'item');
}
/**
* @group connected
* @return void
* @requiresRedisBfVersion >= 1.0.0
*/
public function testInsertIntoAlreadyExistingFilterWithNoCreateModifier(): void
{
$redis = $this->getClient();
$redis->cfadd('filter', 'item');
$actualResponse = $redis->cfinsertnx('filter', -1, true, 'item1');
$this->assertSame([1], $actualResponse);
}
public function argumentsProvider(): array
{
return [
'with default arguments' => [
['key', -1, false, 'item1'],
['key', 'ITEMS', 'item1'],
],
'with CAPACITY modifier' => [
['key', 500, false, 'item1'],
['key', 'CAPACITY', 500, 'ITEMS', 'item1'],
],
'with NOCREATE modifier' => [
['key', -1, true, 'item1'],
['key', 'NOCREATE', 'ITEMS', 'item1'],
],
'with all arguments' => [
['key', 500, true, 'item1', 'item2'],
['key', 'CAPACITY', 500, 'NOCREATE', 'ITEMS', 'item1', 'item2'],
],
];
}
public function filtersProvider(): array
{
return [
'with default arguments' => [
['key', -1, false, 'item1'],
'key',
1080,
[1],
],
'with modified CAPACITY' => [
['key', 500, false, 'item1'],
'key',
568,
[1],
],
'with multiple items' => [
['key', -1, false, 'item1', 'item2'],
'key',
1080,
[1, 1],
],
];
}
}