Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added Redis HyperLogLog #226

Merged
merged 6 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/redis/.travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ services:
install:
- wget https://github.com/redis/hiredis/archive/v0.13.3.tar.gz -O hiredis.tar.gz && mkdir -p hiredis && tar -xf hiredis.tar.gz -C hiredis --strip-components=1 && cd hiredis && sudo make -j$(nproc) && sudo make install && sudo ldconfig && cd ..
- echo 'no' | pecl install -f redis
- wget https://github.com/swoole/swoole-src/archive/v4.0.2.tar.gz -O swoole.tar.gz && mkdir -p swoole && tar -xf swoole.tar.gz -C swoole --strip-components=1 && rm swoole.tar.gz && cd swoole && phpize && ./configure --enable-async-redis && make -j$(nproc) && make install && cd -
- wget https://github.com/swoole/swoole-src/archive/v4.2.9.tar.gz -O swoole.tar.gz && mkdir -p swoole && tar -xf swoole.tar.gz -C swoole --strip-components=1 && rm swoole.tar.gz && cd swoole && phpize && ./configure --enable-async-redis && make -j$(nproc) && make install && cd -
- echo "extension = swoole.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini

before_script:
Expand Down
43 changes: 43 additions & 0 deletions src/redis/src/Operator/Processor/PrefixProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Swoft\Redis\Operator\Processor;

use Swoft\Bean\Annotation\Inject;
use Swoft\Helper\StringHelper;
use Swoft\Redis\Operator\CommandInterface;
use Swoft\Bean\Annotation\Bean;
use Swoft\Redis\Pool\Config\RedisPoolConfig;
Expand Down Expand Up @@ -150,6 +151,10 @@ public function init()
'EVALSHA' => 'static::evalKeys',
'ZRANGEBYLEX' => 'static::first',
'ZREVRANGEBYLEX' => 'static::first',
'PFADD' => 'static::first',
'PFCOUNT' => 'static::allarray',
'PFMERGE' => 'static::allarray',
'ZINTER' => 'static::allarray2',
];
}

Expand Down Expand Up @@ -258,6 +263,33 @@ public static function all(CommandInterface $command, $prefix)
}
}

/**
* Applies the specified prefix to all the arguments.
*
* @param CommandInterface $command Command instance.
* @param string $prefix Prefix string.
*/
public static function allarray(CommandInterface $command, $prefix, $len = 0)
{
if ($arguments = $command->getArguments()) {
$result = [];
foreach ($arguments as $index => $key) {
if ($len > 0 && $index >= $len) {
$result[] = $key;
} elseif (is_array($key)) {
foreach ($key as &$i) {
$i = "$prefix$i";
}
$result[] = $key;
} else {
$result[] = "$prefix$key";
}
}

$command->setRawArguments($result);
}
}

/**
* Applies the specified prefix only to even arguments in the list.
*
Expand Down Expand Up @@ -385,4 +417,15 @@ public static function zsetStore(CommandInterface $command, $prefix)
$command->setRawArguments($arguments);
}
}

public static function __callStatic($name, $arguments)
{
if (StringHelper::startsWith($name, 'allarray')) {
$len = (int)StringHelper::replaceFirst('allarray', '', $name);
$arguments[] = $len;
return static::allarray(...$arguments);
}

throw new \Exception("class 'Swoft\Redis\Operator\Processor\PrefixProcessor' does not have a method '{$name}'");
}
}
19 changes: 19 additions & 0 deletions src/redis/src/Operator/Strings/StringPfAdd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php


namespace Swoft\Redis\Operator\Strings;

use Swoft\Redis\Operator\Command;

class StringPfAdd extends Command
{
/**
* [String] pfAdd
*
* @return string
*/
public function getId()
{
return 'pfAdd';
}
}
19 changes: 19 additions & 0 deletions src/redis/src/Operator/Strings/StringPfCount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php


namespace Swoft\Redis\Operator\Strings;

use Swoft\Redis\Operator\Command;

class StringPfCount extends Command
{
/**
* [String] pfCount
*
* @return string
*/
public function getId()
{
return 'pfCount';
}
}
19 changes: 19 additions & 0 deletions src/redis/src/Operator/Strings/StringPfMerge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php


namespace Swoft\Redis\Operator\Strings;

use Swoft\Redis\Operator\Command;

class StringPfMerge extends Command
{
/**
* [String] pfMerge
*
* @return string
*/
public function getId()
{
return 'pfMerge';
}
}
13 changes: 13 additions & 0 deletions src/redis/src/Operator/ZSets/ZSetInterStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Swoft\Redis\Operator\ZSets;

use Swoft\Redis\Operator\Command;

class ZSetInterStore extends Command
{
public function getId()
{
return 'ZInter';
}
}
5 changes: 5 additions & 0 deletions src/redis/src/Profile/RedisCommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public function getSupportedCommands(): array
'INCRBYFLOAT' => '\Swoft\Redis\Operator\Strings\StringIncrementByFloat',
'BITOP' => '\Swoft\Redis\Operator\Strings\StringBitOp',
'BITCOUNT' => '\Swoft\Redis\Operator\Strings\StringBitCount',
/* ---------------- Redis 2.8 ---------------- */
'PFADD' => '\Swoft\Redis\Operator\Strings\StringPfAdd',
'PFCOUNT' => '\Swoft\Redis\Operator\Strings\StringPfCount',
'PFMERGE' => '\Swoft\Redis\Operator\Strings\StringPfMerge',

/* commands operating on lists */
/* ---------------- Redis 1.2 ---------------- */
Expand Down Expand Up @@ -146,6 +150,7 @@ public function getSupportedCommands(): array
'ZREVRANK' => '\Swoft\Redis\Operator\ZSets\ZSetReverseRank',
'ZREMRANGEBYRANK' => '\Swoft\Redis\Operator\ZSets\ZSetRemoveRangeByRank',
'ZDELETERANGEBYRANK' => '\Swoft\Redis\Operator\ZSets\ZSetRemoveRangeByRank',
'ZINTER' => '\Swoft\Redis\Operator\ZSets\ZSetInterStore',
/* ---------------- Redis 2.2 ---------------- */
'ZREVRANGEBYSCORE' => '\Swoft\Redis\Operator\ZSets\ZSetReverseRangeByScore',
/* ---------------- Redis 2.8 ---------------- */
Expand Down
2 changes: 1 addition & 1 deletion src/redis/test/Cases/ListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public function testblPop()
});

go(function () use ($key, $expected) {
\co::sleep(3.0);
\co::sleep(2.0);
$this->redis->lPush($key, $expected);
});
});
Expand Down
46 changes: 40 additions & 6 deletions src/redis/test/Cases/StringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SwoftTest\Redis;

use Swoft\App;
use Swoole\Coroutine\Redis;

/**
* StringTest
Expand All @@ -12,7 +13,7 @@ class StringTest extends AbstractTestCase
public function testSet()
{
$value = uniqid();
$key = 'stringKey';
$key = 'stringKey';
if (App::isCoContext()) {
$key .= 'co';
}
Expand Down Expand Up @@ -40,7 +41,7 @@ public function testSetByCo()
public function testGet()
{
$default = 'defualtValue';
$result = $this->redis->get("notKey" . uniqid(), $default);
$result = $this->redis->get("notKey" . uniqid(), $default);
$this->assertSame($result, $default);
}

Expand All @@ -53,13 +54,13 @@ public function testGetByCo()

public function testMsetAndMget()
{
$key = uniqid();
$key2 = uniqid();
$value = 'value1';
$key = uniqid();
$key2 = uniqid();
$value = 'value1';
$value2 = 'val2';

$result = $this->redis->mset([
$key => $value,
$key => $value,
$key2 => $value2,
]);

Expand All @@ -76,4 +77,37 @@ public function testMsetAndMgetByCo()
$this->testMsetAndMget();
});
}

public function testHyperLoglog()
{
$this->redis->delete('pf:test');
$this->redis->delete('pf:test2');
$this->redis->delete('pf:test3');

$result = $this->redis->pfAdd('pf:test', [1, 2, 3]);

$this->assertEquals(1, $result);

$result = $this->redis->pfCount('pf:test');
$this->assertEquals(3, $result);

$result = $this->redis->pfAdd('pf:test2', [3, 4, 5]);
$this->assertEquals(1, $result);

$result = $this->redis->pfMerge('pf:test3', ['pf:test', 'pf:test2']);
$this->assertTrue($result);

$result = $this->redis->pfCount('pf:test3');
$this->assertEquals(5, $result);

$result = $this->redis->pfCount(['pf:test', 'pf:test2']);
$this->assertEquals(5, $result);
}

public function testHyperLoglogByCo()
{
go(function () {
$this->testHyperLoglog();
});
}
}
27 changes: 27 additions & 0 deletions src/redis/test/Cases/ZsetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,33 @@ public function testZadd()
$this->assertEquals(['key3' => 3.2, 'key2' => 1.3, 'key4' => 1.2], $rangeKeys);
}

public function testZInter()
{
$key1 = 'test:zinter:1';
$key2 = 'test:zinter:2';
$output = 'test:zinter:output';

$this->redis->zAdd($key1, 0, 'i0');
$this->redis->zAdd($key1, 1, 'i1');
$this->redis->zAdd($key1, 1, 'i2');

$this->redis->zAdd($key2, 2, 'i2');
$this->redis->zAdd($key2, 2, 'i3');

$res = $this->redis->zInter($output, [$key1, $key2], [1, 2]);
$this->assertEquals(1, $res);

$res = $this->redis->zRange($output, 0, -1, true);
$this->assertEquals(['i2' => 5], $res);
}

public function testZInterByCo()
{
go(function () {
$this->testZInter();
});
}

public function testZaddByCo()
{
go(function () {
Expand Down