Extended Bloom Filters by implementing BF.RESERVE command (#888)

* Added CommandResolver, moved resolve command logic there, added ClientConfiguration object

* Fixed test, added dependecies

* Added resolved command to commands array

* Used aggregation approach for modules

* Added decorator to check Redis JSON module version

* Added support for JSON.SET and JSON.GET commands

* Added separate workflow for redis-stack tests

* Changed docker imange name to correct one

* Fixed indentation

* Added test coverage for JSON.GET command

* Changed module version resolving using annotations mapping

* Re-written CommandResolver test

* Update ClientInterface.php

* Changes to CI, readme, removed unused modules from configuration

* Fixed build badge URL

* Refactored annotation check to be generic for each module

* Added support for BF.ADD and BF.EXISTS commands

* Added support for BF.INFO command

* Fixed arguments data provider

* Fixed bug with incorrect tests skip

* Added support for BF.RESERVE command

* Added command description

* Codestyle fixes

* Added missing test

Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>
Co-authored-by: Till Krüss <tillkruss@users.noreply.github.com>
This commit is contained in:
Vladyslav Vildanov
2023-01-26 14:25:10 +02:00
committed by GitHub
parent f82bee9a81
commit 549b4414c4
6 changed files with 331 additions and 0 deletions
+1
View File
@@ -47,6 +47,7 @@ use Predis\Command\CommandInterface;
* @method $this bfinfo(string $key, string $modifier = '')
* @method $this bfmadd(string $key, ...$item)
* @method $this bfmexists(string $key, ...$item)
* @method $this bfreserve(string $key, float $errorRate, int $capacity, int $expansion = -1, bool $nonScaling = false)
* @method $this bitcount($key, $start = null, $end = null)
* @method $this bitop($operation, $destkey, $key)
* @method $this bitfield($key, $subcommand, ...$subcommandArg)
+1
View File
@@ -56,6 +56,7 @@ use Predis\Response\Status;
* @method array bfinfo(string $key, string $modifier = '')
* @method array bfmadd(string $key, ...$item)
* @method array bfmexists(string $key, ...$item)
* @method Status bfreserve(string $key, float $errorRate, int $capacity, int $expansion = -1, bool $nonScaling = false)
* @method int bitcount(string $key, $start = null, $end = null)
* @method int bitop($operation, $destkey, $key)
* @method array|null bitfield(string $key, $subcommand, ...$subcommandArg)
@@ -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\Redis\BloomFilter;
use Predis\Command\Command as RedisCommand;
use Predis\Command\Traits\BloomFilters\Expansion;
/**
* @see https://redis.io/commands/bf.reserve/
*
* Creates an empty Bloom Filter with a single sub-filter for the initial capacity
* requested and with an upper bound error_rate.
*
* By default, the filter auto-scales by creating additional sub-filters when capacity is reached.
* The new sub-filter is created with size of the previous sub-filter multiplied by expansion.
*/
class BFRESERVE extends RedisCommand
{
use Expansion {
Expansion::setArguments as setExpansion;
}
protected static $expansionArgumentPositionOffset = 3;
public function getId()
{
return 'BF.RESERVE';
}
public function setArguments(array $arguments)
{
if (array_key_exists(4, $arguments) && $arguments[4]) {
$arguments[4] = 'NONSCALING';
}
$this->setExpansion($arguments);
$this->filterArguments();
}
}
@@ -0,0 +1,53 @@
<?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 UnexpectedValueException;
trait Expansion
{
private static $expansionModifier = 'EXPANSION';
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$expansionArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
if ($arguments[static::$expansionArgumentPositionOffset] === -1) {
array_splice($arguments, static::$expansionArgumentPositionOffset, 1, [false]);
parent::setArguments($arguments);
return;
}
if ($arguments[static::$expansionArgumentPositionOffset] < 1) {
throw new UnexpectedValueException('Wrong expansion argument value or position offset');
}
$argument = $arguments[static::$expansionArgumentPositionOffset];
$argumentsBefore = array_slice($arguments, 0, static::$expansionArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$expansionArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
[self::$expansionModifier],
[$argument],
$argumentsAfter
));
}
}
@@ -0,0 +1,155 @@
<?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\BloomFilter;
use Predis\Command\Redis\PredisCommandTestCase;
use Predis\Response\ServerException;
use UnexpectedValueException;
class BFRESERVE_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return BFRESERVE::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'BFRESERVE';
}
/**
* @group disconnected
* @dataProvider argumentsProvider
*/
public function testFilterArguments(array $actualArguments, array $expectedArguments): void
{
$command = $this->getCommand();
$command->setArguments($actualArguments);
$this->assertSameValues($expectedArguments, $command->getArguments());
}
/**
* @group disconnected
*/
public function testParseResponse(): void
{
$this->assertSame(1, $this->getCommand()->parseResponse(1));
}
/**
* @group connected
* @dataProvider filtersProvider
* @param array $filter
* @param string $key
* @param string $modifier
* @param array $expectedModification
* @return void
* @requiresRedisBfVersion >= 1.0.0
*/
public function testReserveCreatesBloomFilterWithCorrectConfiguration(
array $filter,
string $key,
string $modifier,
array $expectedModification
): void {
$redis = $this->getClient();
$actualResponse = $redis->bfreserve(...$filter);
$this->assertEquals('OK', $actualResponse);
$this->assertSame($expectedModification, $redis->bfinfo($key, $modifier));
}
/**
* @group connected
* @return void
* @requiresRedisBfVersion 1.0.0
*/
public function testThrowsExceptionOnUnexpectedValueGiven(): void
{
$redis = $this->getClient();
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Wrong expansion argument value or position offset');
$redis->bfreserve('key', 0.01, 2, 0);
}
/**
* @group connected
* @requiresRedisBfVersion >= 1.0
*/
public function testThrowsExceptionOnWrongType(): void
{
$this->expectException(ServerException::class);
$this->expectExceptionMessage('Operation against a key holding the wrong kind of value');
$redis = $this->getClient();
$redis->set('bfreserve_foo', 'bar');
$redis->bfreserve('bfreserve_foo', 0.01, 2);
}
public function argumentsProvider(): array
{
return [
'with default arguments' => [
['key', 0.01, 2],
['key', 0.01, 2],
],
'with EXPANSION argument' => [
['key', 0.01, 2, 2],
['key', 0.01, 2, 'EXPANSION', 2],
],
'with NONSCALING modifier' => [
['key', 0.01, 2, -1, true],
['key', 0.01, 2, 'NONSCALING'],
],
'with all arguments' => [
['key', 0.01, 2, 2, true],
['key', 0.01, 2, 'EXPANSION', 2, 'NONSCALING'],
],
];
}
public function filtersProvider(): array
{
return [
'with default arguments' => [
['key', 0.01, 100],
'key',
'capacity',
[100],
],
'with modified expansion' => [
['key', 0.01, 100, 2],
'key',
'expansion',
[2],
],
'with NONSCALING modifier' => [
['key', 0.01, 100, -1, true],
'key',
'expansion',
[null],
],
];
}
}
@@ -0,0 +1,72 @@
<?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 ExpansionTest extends PredisTestCase
{
private $testClass;
protected function setUp(): void
{
$this->testClass = new class() extends RedisCommand {
use Expansion;
public static $expansionArgumentPositionOffset = 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::$expansionArgumentPositionOffset = $offset;
$this->testClass->setArguments($actualArguments);
$this->assertSame($expectedArguments, $this->testClass->getArguments());
}
/**
* @return void
*/
public function testThrowsExceptionOnUnexpectedValueGiven(): void
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Wrong expansion argument value or position offset');
$this->testClass->setArguments([-2]);
}
public function argumentsProvider(): array
{
return [
'with wrong offset' => [2, ['argument1'], ['argument1']],
'with value equals -1' => [0, [-1], [false]],
'with correct value' => [0, [1], ['EXPANSION', 1]],
];
}
}