mirror of
https://github.com/predis/predis.git
synced 2026-08-30 20:21:31 +00:00
Extended BloomFilters support by implementing CF.INSERT command (#1083)
* Added support for CF.INSERT command * Override argument position offset value to default one * Codestyle fixes --------- Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>
This commit is contained in:
committed by
GitHub
parent
4284409c7b
commit
1b8a3cd28c
@@ -66,6 +66,7 @@ use Predis\Command\CommandInterface;
|
||||
* @method $this cfexists(string $key, $item)
|
||||
* @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 decr($key)
|
||||
* @method $this decrby($key, $decrement)
|
||||
* @method $this failover(?To $to = null, bool $abort = false, int $timeout = -1)
|
||||
|
||||
@@ -75,6 +75,7 @@ use Predis\Response\Status;
|
||||
* @method int cfexists(string $key, $item)
|
||||
* @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 int decr(string $key)
|
||||
* @method int decrby(string $key, int $decrement)
|
||||
* @method Status failover(?To $to = null, bool $abort = false, int $timeout = -1)
|
||||
|
||||
@@ -17,6 +17,7 @@ use Predis\Command\Traits\BloomFilters\Capacity;
|
||||
use Predis\Command\Traits\BloomFilters\Error;
|
||||
use Predis\Command\Traits\BloomFilters\Expansion;
|
||||
use Predis\Command\Traits\BloomFilters\Items;
|
||||
use Predis\Command\Traits\BloomFilters\NoCreate;
|
||||
|
||||
class BFINSERT extends RedisCommand
|
||||
{
|
||||
@@ -32,10 +33,14 @@ class BFINSERT extends RedisCommand
|
||||
use Items {
|
||||
Items::setArguments as setItems;
|
||||
}
|
||||
use NoCreate {
|
||||
NoCreate::setArguments as setNoCreate;
|
||||
}
|
||||
|
||||
protected static $capacityArgumentPositionOffset = 1;
|
||||
protected static $errorArgumentPositionOffset = 2;
|
||||
protected static $expansionArgumentPositionOffset = 3;
|
||||
protected static $noCreateArgumentPositionOffset = 4;
|
||||
protected static $itemsArgumentPositionOffset = 6;
|
||||
|
||||
public function getId()
|
||||
@@ -45,9 +50,8 @@ class BFINSERT extends RedisCommand
|
||||
|
||||
public function setArguments(array $arguments)
|
||||
{
|
||||
if (array_key_exists(4, $arguments) && $arguments[4]) {
|
||||
$arguments[4] = 'NOCREATE';
|
||||
}
|
||||
$this->setNoCreate($arguments);
|
||||
$arguments = $this->getArguments();
|
||||
|
||||
if (array_key_exists(5, $arguments) && $arguments[5]) {
|
||||
$arguments[5] = 'NONSCALING';
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
<?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;
|
||||
use Predis\Command\Traits\BloomFilters\Capacity;
|
||||
use Predis\Command\Traits\BloomFilters\Items;
|
||||
use Predis\Command\Traits\BloomFilters\NoCreate;
|
||||
|
||||
class CFINSERT extends RedisCommand
|
||||
{
|
||||
use Capacity {
|
||||
Capacity::setArguments as setCapacity;
|
||||
}
|
||||
use NoCreate {
|
||||
NoCreate::setArguments as setNoCreate;
|
||||
}
|
||||
use Items {
|
||||
Items::setArguments as setItems;
|
||||
}
|
||||
|
||||
protected static $capacityArgumentPositionOffset = 1;
|
||||
protected static $noCreateArgumentPositionOffset = 2;
|
||||
protected static $itemsArgumentPositionOffset = 3;
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return 'CF.INSERT';
|
||||
}
|
||||
|
||||
public function setArguments(array $arguments)
|
||||
{
|
||||
$this->setNoCreate($arguments);
|
||||
$arguments = $this->getArguments();
|
||||
|
||||
$this->setItems($arguments);
|
||||
$arguments = $this->getArguments();
|
||||
|
||||
$this->setCapacity($arguments);
|
||||
$this->filterArguments();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?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\BloomFilters;
|
||||
|
||||
use Predis\Command\Command;
|
||||
use UnexpectedValueException;
|
||||
|
||||
/**
|
||||
* @mixin Command
|
||||
*/
|
||||
trait NoCreate
|
||||
{
|
||||
public function setArguments(array $arguments)
|
||||
{
|
||||
$argumentsLength = count($arguments);
|
||||
|
||||
if (
|
||||
static::$noCreateArgumentPositionOffset >= $argumentsLength
|
||||
|| false === $arguments[static::$noCreateArgumentPositionOffset]
|
||||
) {
|
||||
parent::setArguments($arguments);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$argument = $arguments[static::$noCreateArgumentPositionOffset];
|
||||
|
||||
if (true === $argument) {
|
||||
$argument = 'NOCREATE';
|
||||
} else {
|
||||
throw new UnexpectedValueException('Wrong NOCREATE argument type');
|
||||
}
|
||||
|
||||
$argumentsBefore = array_slice($arguments, 0, static::$noCreateArgumentPositionOffset);
|
||||
$argumentsAfter = array_slice($arguments, static::$noCreateArgumentPositionOffset + 1);
|
||||
|
||||
parent::setArguments(array_merge($argumentsBefore, [$argument], $argumentsAfter));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
<?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;
|
||||
use UnexpectedValueException;
|
||||
|
||||
class CFINSERT_Test extends PredisCommandTestCase
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedCommand(): string
|
||||
{
|
||||
return CFINSERT::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedId(): string
|
||||
{
|
||||
return 'CFINSERT';
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 testInsertItemsIntoGivenCuckooFilter(
|
||||
array $filterArguments,
|
||||
string $key,
|
||||
int $expectedCapacity,
|
||||
array $expectedResponse
|
||||
): void {
|
||||
$redis = $this->getClient();
|
||||
|
||||
$actualResponse = $redis->cfinsert(...$filterArguments);
|
||||
$info = $redis->cfinfo($key);
|
||||
|
||||
$this->assertSame($expectedResponse, $actualResponse);
|
||||
$this->assertSame($expectedCapacity, $info['Size']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @return void
|
||||
* @requiresRedisBfVersion >= 1.0.0
|
||||
*/
|
||||
public function testInsertIgnoresCapacityModifierOnAlreadyExistingFilter(): void
|
||||
{
|
||||
$redis = $this->getClient();
|
||||
|
||||
$redis->cfadd('filter', 'item');
|
||||
|
||||
$actualResponse = $redis->cfinsert('filter', 500, false, 'item1');
|
||||
$info = $redis->cfinfo('filter');
|
||||
|
||||
$this->assertSame([1], $actualResponse);
|
||||
$this->assertSame(1080, $info['Size']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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->cfinsert('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->cfinsert('filter', -1, true, 'item1');
|
||||
$this->assertSame([1], $actualResponse);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @return void
|
||||
* @requiresRedisBfVersion >= 1.0.0
|
||||
*/
|
||||
public function testThrowsExceptionOnUnexpectedValueGiven(): void
|
||||
{
|
||||
$redis = $this->getClient();
|
||||
|
||||
$this->expectException(UnexpectedValueException::class);
|
||||
$this->expectExceptionMessage('Wrong NOCREATE argument type');
|
||||
|
||||
$redis->cfinsert('key', -1, 'wrong', 'item');
|
||||
}
|
||||
|
||||
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],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?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\BloomFilters;
|
||||
|
||||
use Predis\Command\Command as RedisCommand;
|
||||
use PredisTestCase;
|
||||
use UnexpectedValueException;
|
||||
|
||||
class NoCreateTest extends PredisTestCase
|
||||
{
|
||||
private $testClass;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->testClass = new class() extends RedisCommand {
|
||||
use NoCreate;
|
||||
|
||||
public static $noCreateArgumentPositionOffset = 0;
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return 'test';
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider argumentsProvider
|
||||
* @param int $offset
|
||||
* @param array $actualArguments
|
||||
* @param array $expectedArguments
|
||||
* @return void
|
||||
*/
|
||||
public function testReturnsCorrectArguments(int $offset, array $actualArguments, array $expectedArguments): void
|
||||
{
|
||||
$this->testClass::$noCreateArgumentPositionOffset = $offset;
|
||||
|
||||
$this->testClass->setArguments($actualArguments);
|
||||
|
||||
$this->assertSame($expectedArguments, $this->testClass->getArguments());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testThrowsExceptionOnUnexpectedValue(): void
|
||||
{
|
||||
$this->testClass::$noCreateArgumentPositionOffset = 0;
|
||||
|
||||
$this->expectException(UnexpectedValueException::class);
|
||||
$this->expectExceptionMessage('Wrong NOCREATE argument type');
|
||||
|
||||
$this->testClass->setArguments(['test']);
|
||||
}
|
||||
|
||||
public function argumentsProvider(): array
|
||||
{
|
||||
return [
|
||||
'NOCREATE false argument' => [
|
||||
0,
|
||||
[false, 'second argument', 'third argument'],
|
||||
[false, 'second argument', 'third argument'],
|
||||
],
|
||||
'NOCREATE argument first and there is arguments after' => [
|
||||
0,
|
||||
[true, 'second argument', 'third argument'],
|
||||
['NOCREATE', 'second argument', 'third argument'],
|
||||
],
|
||||
'NOCREATE argument last and there is arguments before' => [
|
||||
2,
|
||||
['first argument', 'second argument', true],
|
||||
['first argument', 'second argument', 'NOCREATE'],
|
||||
],
|
||||
'NOCREATE argument not the first and not the last' => [
|
||||
1,
|
||||
['first argument', true, 'third argument'],
|
||||
['first argument', 'NOCREATE', 'third argument'],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user