Skip to content

Commit 1f4b831

Browse files
committed
refactoring, added BloomFilter datatype and finish bloom filter commands
1 parent 0cdb8b3 commit 1f4b831

18 files changed

+555
-203
lines changed

examples/scan.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* @project phpredis-bloom
4+
* @author Rafael Campoy <[email protected]>
5+
* @copyright 2019 Rafael Campoy <[email protected]>
6+
* @license MIT
7+
* @link https://github.com/averias/php-rejson
8+
*
9+
* Copyright and license information, is included in
10+
* the LICENSE file that is distributed with this source code.
11+
*/
12+
13+
namespace Example;
14+
15+
require(dirname(__DIR__).'/vendor/autoload.php');
16+
17+
use Averias\RedisBloom\Enum\OptionalParams;
18+
use Averias\RedisBloom\Factory\RedisBloomClientFactory;
19+
20+
const SCAN_KEY = 'scan-key';
21+
const TARGET_SCAN_KEY = 'target-scan-key';
22+
23+
$factory = new RedisBloomClientFactory();
24+
$client = $factory->createClient();
25+
26+
$capacity = 10000;
27+
$insertData = [];
28+
29+
30+
for ($i = 0; $i < $capacity; $i++) {
31+
$insertData[] = "abc{$i}";
32+
}
33+
$client->bloomFilterInsert(
34+
SCAN_KEY,
35+
$insertData,
36+
[OptionalParams::CAPACITY => $capacity, OptionalParams::ERROR => 0.01]
37+
);
38+
39+
$bloomFilter = $factory->createBloomFilter(SCAN_KEY);
40+
$bloomFilter->copy(TARGET_SCAN_KEY);
41+
42+
$multiExists = $client->bloomFilterMultiExists(TARGET_SCAN_KEY, ...$insertData);
43+
44+
$nonExistentKey = [];
45+
foreach ($multiExists as $key => $exist) {
46+
if ($exist === false) {
47+
$nonExistentKey[] = $insertData[$key];
48+
}
49+
}
50+
51+
if (empty($nonExistentKey)) {
52+
echo "all items were copied" . PHP_EOL;
53+
} else {
54+
echo count($nonExistentKey) . " were not copied:" . PHP_EOL;
55+
var_dump($nonExistentKey);
56+
}
57+
58+
$client->del(SCAN_KEY);
59+
$client->del(TARGET_SCAN_KEY);

src/RedisBloomClient/Adapter/AdapterProvider.php

+21-13
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Averias\RedisBloom\Exception\RedisBloomModuleNotInstalledException;
1818
use Averias\RedisBloom\Exception\ResponseException;
1919
use Averias\RedisBloom\Validator\RedisClientValidatorInterface;
20-
use Redis;
2120

2221
class AdapterProvider
2322
{
@@ -36,26 +35,35 @@ public function __construct(RedisClientValidatorInterface $validator)
3635
* @throws RedisBloomModuleNotInstalledException
3736
* @throws ResponseException
3837
*/
39-
public function get(array $config = []): RedisClientAdapterInterface
38+
public function getRedisClientAdapter(array $config = []): RedisClientAdapterInterface
4039
{
41-
$redisClient = $this->getRedisClient($config);
40+
$redisClientAdapter = new RedisClientAdapter($this->getRedisAdapter($config));
41+
$this->validateRedisBloomModuleInstalled($redisClientAdapter);
4242

43-
$modules = $redisClient->executeRawCommand('MODULE', 'list');
44-
if (!$this->validator->isRedisBloomModuleInstalled($modules)) {
45-
throw new RedisBloomModuleNotInstalledException('RedisBloom module not installed in Redis server.');
46-
}
47-
48-
return $redisClient;
43+
return $redisClientAdapter;
4944
}
5045

5146
/**
5247
* @param array $config
53-
* @return RedisClientAdapter
48+
* @return RedisAdapterInterface
5449
* @throws ConnectionException
5550
*/
56-
protected function getRedisClient(array $config = []): RedisClientAdapter
51+
protected function getRedisAdapter(array $config = []): RedisAdapterInterface
5752
{
5853
$connectionOptions = new ConnectionOptions($config);
59-
return new RedisClientAdapter(new Redis(), $connectionOptions);
54+
return new RedisAdapter($connectionOptions);
55+
}
56+
57+
/**
58+
* @param RedisClientAdapterInterface $redisClientAdapter
59+
* @throws RedisBloomModuleNotInstalledException
60+
* @throws ResponseException
61+
*/
62+
protected function validateRedisBloomModuleInstalled(RedisClientAdapterInterface $redisClientAdapter): void
63+
{
64+
$modules = $redisClientAdapter->executeRawCommand('MODULE', 'list');
65+
if (!$this->validator->isRedisBloomModuleInstalled($modules)) {
66+
throw new RedisBloomModuleNotInstalledException('RedisBloom module not installed in Redis server.');
67+
}
6068
}
61-
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* @project phpredis-bloom
4+
* @author Rafael Campoy <[email protected]>
5+
* @copyright 2019 Rafael Campoy <[email protected]>
6+
* @license MIT
7+
* @link https://github.com/averias/php-rejson
8+
*
9+
* Copyright and license information, is included in
10+
* the LICENSE file that is distributed with this source code.
11+
*/
12+
13+
namespace Averias\RedisBloom\Adapter;
14+
15+
use Averias\RedisBloom\Connection\ConnectionOptions;
16+
use Averias\RedisBloom\Exception\ConnectionException;
17+
use Redis;
18+
19+
class RedisAdapter implements RedisAdapterInterface
20+
{
21+
/** @var ConnectionOptions */
22+
protected $connectionOptions;
23+
24+
/** @var Redis */
25+
protected $redis;
26+
27+
/**
28+
* @param ConnectionOptions $connectionOptions
29+
* @throws ConnectionException
30+
*/
31+
public function __construct(ConnectionOptions $connectionOptions)
32+
{
33+
$this->connectionOptions = $connectionOptions;
34+
$this->redis = new Redis();
35+
$this->setConnection();
36+
}
37+
38+
public function getLastError(): ?string
39+
{
40+
return $this->redis->getLastError();
41+
}
42+
43+
/**
44+
* @param string $commandName
45+
* @param mixed ...$arguments
46+
* @return mixed
47+
* @throws ConnectionException
48+
*/
49+
public function rawCommand(string $commandName, ...$arguments)
50+
{
51+
$this->checkConnection();
52+
return $this->redis->rawCommand($commandName, ...$arguments);
53+
}
54+
55+
/**
56+
* @param string $methodName
57+
* @param array $arguments
58+
* @return mixed
59+
* @throws ConnectionException
60+
*/
61+
public function executeCommandByName(string $methodName, array $arguments = [])
62+
{
63+
$this->checkConnection();
64+
return call_user_func_array([$this->redis, $methodName], $arguments);
65+
}
66+
67+
/**
68+
* @throws ConnectionException
69+
*/
70+
public function checkConnection(): void
71+
{
72+
if (!$this->redis->isConnected()) {
73+
$this->setConnection();
74+
}
75+
}
76+
77+
/**
78+
* @throws ConnectionException
79+
*/
80+
public function setConnection(): void
81+
{
82+
if (!$this->connect()) {
83+
throw new ConnectionException(
84+
sprintf("connection to Redis server failed, reason: %s", $this->redis->getLastError())
85+
);
86+
}
87+
88+
if ($this->connectionOptions->getDatabase() != 0) {
89+
$this->redis->select($this->connectionOptions->getDatabase());
90+
}
91+
92+
$this->redis->setOption(Redis::OPT_REPLY_LITERAL, 1);
93+
}
94+
95+
/**
96+
* @return bool
97+
*/
98+
public function connect(): bool
99+
{
100+
$connectionValues = $this->connectionOptions->getConnectionValues();
101+
if ($this->connectionOptions->isPersistent()) {
102+
return $this->redis->pconnect(...$connectionValues);
103+
}
104+
105+
return $this->redis->connect(...$connectionValues);
106+
}
107+
108+
/**
109+
* @return bool
110+
*/
111+
public function closeConnection(): bool
112+
{
113+
return $this->redis->close();
114+
}
115+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* @project phpredis-bloom
4+
* @author Rafael Campoy <[email protected]>
5+
* @copyright 2019 Rafael Campoy <[email protected]>
6+
* @license MIT
7+
* @link https://github.com/averias/php-rejson
8+
*
9+
* Copyright and license information, is included in
10+
* the LICENSE file that is distributed with this source code.
11+
*/
12+
13+
namespace Averias\RedisBloom\Adapter;
14+
15+
use Averias\RedisBloom\Connection\ConnectionOptions;
16+
use Averias\RedisBloom\Exception\ConnectionException;
17+
18+
interface RedisAdapterInterface
19+
{
20+
/**
21+
* @param ConnectionOptions $connectionOptions
22+
* @throws ConnectionException
23+
*/
24+
public function __construct(ConnectionOptions $connectionOptions);
25+
26+
/**
27+
* @return string|null
28+
*/
29+
public function getLastError(): ?string;
30+
31+
/**
32+
* @param string $commandName
33+
* @param mixed ...$arguments
34+
* @return mixed
35+
* @throws ConnectionException
36+
*/
37+
public function rawCommand(string $commandName, ...$arguments);
38+
39+
/**
40+
* @param string $methodName
41+
* @param array $arguments
42+
* @return mixed
43+
* @throws ConnectionException
44+
*/
45+
public function executeCommandByName(string $methodName, array $arguments = []);
46+
47+
/**
48+
* @throws ConnectionException
49+
*/
50+
public function checkConnection(): void;
51+
52+
/**
53+
* @throws ConnectionException
54+
*/
55+
public function setConnection(): void;
56+
57+
/**
58+
* @return bool
59+
*/
60+
public function connect(): bool;
61+
62+
/**
63+
* @return bool
64+
*/
65+
public function closeConnection(): bool;
66+
}

0 commit comments

Comments
 (0)