Added support for Bloom Filters module, added support for BF.ADD, BF.EXISTS commands (#874)

* 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

* Fixed bug with incorrect tests skip

* Changed module name according to original naming

* Revert some old changes

* Fixed misspelling

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-25 17:37:21 +02:00
committed by GitHub
parent f8797aaa29
commit 7b7e93a53f
8 changed files with 229 additions and 0 deletions
+1
View File
@@ -20,6 +20,7 @@ class ClientConfiguration
private static $config = [
'modules' => [
['name' => 'Json', 'commandPrefix' => 'JSON'],
['name' => 'BloomFilter', 'commandPrefix' => 'BF'],
],
];
+2
View File
@@ -42,6 +42,8 @@ use Predis\Command\CommandInterface;
* @method $this ttl($key)
* @method $this type($key)
* @method $this append($key, $value)
* @method $this bfadd(string $key, $item)
* @method $this bfexists(string $key, $item)
* @method $this bitcount($key, $start = null, $end = null)
* @method $this bitop($operation, $destkey, $key)
* @method $this bitfield($key, $subcommand, ...$subcommandArg)
+2
View File
@@ -51,6 +51,8 @@ use Predis\Response\Status;
* @method int ttl(string $key)
* @method mixed type(string $key)
* @method int append(string $key, $value)
* @method int bfadd(string $key, $item)
* @method int bfexists(string $key, $item)
* @method int bitcount(string $key, $start = null, $end = null)
* @method int bitop($operation, $destkey, $key)
* @method array|null bitfield(string $key, $subcommand, ...$subcommandArg)
+29
View File
@@ -0,0 +1,29 @@
<?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;
/**
* @see https://redis.io/commands/bf.add/
*
* Creates an empty Bloom Filter with a single sub-filter for the
* initial capacity requested and with an upper bound error_rate.
*/
class BFADD extends RedisCommand
{
public function getId()
{
return 'BF.ADD';
}
}
@@ -0,0 +1,28 @@
<?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;
/**
* @see https://redis.io/commands/bf.exists/
*
* Determines whether an item may exist in the Bloom Filter or not.
*/
class BFEXISTS extends RedisCommand
{
public function getId()
{
return 'BF.EXISTS';
}
}
+2
View File
@@ -30,6 +30,7 @@ abstract class PredisTestCase extends \PHPUnit\Framework\TestCase
*/
private $modulesMapping = [
'json' => ['annotation' => 'requiresRedisJsonVersion', 'name' => 'ReJSON'],
'bloomFilter' => ['annotation' => 'requiresRedisBfVersion', 'name' => 'bf'],
];
/**
@@ -46,6 +47,7 @@ abstract class PredisTestCase extends \PHPUnit\Framework\TestCase
{
$this->checkRequiredRedisServerVersion();
$this->checkRequiredRedisModuleVersion('json');
$this->checkRequiredRedisModuleVersion('bloomFilter');
}
/**
@@ -0,0 +1,90 @@
<?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;
/**
* @group commands
* @group realm-bloom
*/
class BFADD_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return BFADD::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'BFADD';
}
/**
* @group disconnected
*/
public function testFilterArguments(): void
{
$actualArguments = ['key', 'item'];
$expectedArguments = ['key', 'item'];
$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
* @return void
* @requiresRedisBfVersion >= 1.0
*/
public function testAddGivenItemIntoBloomFilter(): void
{
$redis = $this->getClient();
$actualResponse = $redis->bfadd('key', 'item');
$this->assertSame(1, $actualResponse);
$this->assertSame(1, $redis->bfexists('key', 'item'));
}
/**
* @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('bfadd_foo', 'bar');
$redis->bfadd('bfadd_foo', 'foo');
}
}
@@ -0,0 +1,75 @@
<?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;
/**
* @group commands
* @group realm-bloom
*/
class BFEXISTS_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return BFEXISTS::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'BFEXISTS';
}
/**
* @group disconnected
*/
public function testFilterArguments(): void
{
$actualArguments = ['key', 'item'];
$expectedArguments = ['key', 'item'];
$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
* @return void
* @requiresRedisBfVersion >= 1.0
*/
public function testExistsReturnsExistingItemWithinBloomFilter(): void
{
$redis = $this->getClient();
$redis->bfadd('key', 'item');
$this->assertSame(1, $redis->bfexists('key', 'item'));
$this->assertSame(0, $redis->bfexists('key', 'non-existing'));
}
}