New commands: PFADD, PFCOUNT, PFMERGE (Redis 2.8.9).

Many thanks to @rubensayshi for his initial commits on this in #163,
but I had to start from scratch for the master branch first.
This commit is contained in:
Daniele Alessandri
2014-05-30 11:32:39 +02:00
parent f784dfad84
commit 2aa0b071fc
17 changed files with 442 additions and 0 deletions
+5
View File
@@ -156,6 +156,11 @@ class PredisStrategy implements StrategyInterface
'HVALS' => $keyIsFirstArgument,
'HSCAN' => $keyIsFirstArgument,
/* commands operating on HyperLogLog */
'PFADD' => $keyIsFirstArgument,
'PFCOUNT' => $keysAreAllArguments,
'PFMERGE' => $keysAreAllArguments,
/* scripting */
'EVAL' => array($this, 'getKeyFromScriptingCommands'),
'EVALSHA' => array($this, 'getKeyFromScriptingCommands'),
+5
View File
@@ -141,6 +141,11 @@ class RedisStrategy implements StrategyInterface
'HVALS' => $keyIsFirstArgument,
'HSCAN' => $keyIsFirstArgument,
/* commands operating on HyperLogLog */
'PFADD' => $keyIsFirstArgument,
'PFCOUNT' => array($this, 'getKeyFromAllArguments'),
'PFMERGE' => array($this, 'getKeyFromAllArguments'),
/* scripting */
'EVAL' => array($this, 'getKeyFromScriptingCommands'),
'EVALSHA' => array($this, 'getKeyFromScriptingCommands'),
+43
View File
@@ -0,0 +1,43 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command;
/**
* @link http://redis.io/commands/pfadd
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class HyperLogLogAdd extends Command
{
/**
* {@inheritdoc}
*/
public function getId()
{
return 'PFADD';
}
/**
* {@inheritdoc}
*/
protected function filterArguments(array $arguments)
{
return self::normalizeVariadic($arguments);
}
/**
* {@inheritdoc}
*/
public function parseResponse($data)
{
return (bool) $data;
}
}
+35
View File
@@ -0,0 +1,35 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command;
/**
* @link http://redis.io/commands/pfcount
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class HyperLogLogCount extends Command
{
/**
* {@inheritdoc}
*/
public function getId()
{
return 'PFCOUNT';
}
/**
* {@inheritdoc}
*/
protected function filterArguments(array $arguments)
{
return self::normalizeArguments($arguments);
}
}
+35
View File
@@ -0,0 +1,35 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command;
/**
* @link http://redis.io/commands/pfmerge
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class HyperLogLogMerge extends Command
{
/**
* {@inheritdoc}
*/
public function getId()
{
return 'PFMERGE';
}
/**
* {@inheritdoc}
*/
protected function filterArguments(array $arguments)
{
return self::normalizeArguments($arguments);
}
}
@@ -149,6 +149,9 @@ class KeyPrefixProcessor implements ProcessorInterface
'SSCAN' => 'self::first',
'ZSCAN' => 'self::first',
'HSCAN' => 'self::first',
'PFADD' => 'self::first',
'PFCOUNT' => 'self::all',
'PFMERGE' => 'self::all',
);
}
+5
View File
@@ -246,6 +246,11 @@ class RedisVersion280 extends RedisProfile
/* publish - subscribe */
'PUBSUB' => 'Predis\Command\PubSubPubsub',
/* commands operating on HyperLogLog */
'PFADD' => 'Predis\Command\HyperLogLogAdd',
'PFCOUNT' => 'Predis\Command\HyperLogLogCount',
'PFMERGE' => 'Predis\Command\HyperLogLogMerge',
);
}
}
@@ -220,6 +220,7 @@ class ReplicationStrategy
'OBJECT' => true,
'BITCOUNT' => true,
'TIME' => true,
'PFCOUNT' => true,
'SORT' => array($this, 'isSortReadOnly'),
);
}
@@ -362,6 +362,11 @@ class PredisStrategyTest extends PredisTestCase
'HVALS' => 'keys-first',
'HSCAN' => 'keys-first',
/* commands operating on HyperLogLog */
'PFADD' => 'keys-first',
'PFCOUNT' => 'keys-all',
'PFMERGE' => 'keys-all',
/* scripting */
'EVAL' => 'keys-script',
'EVALSHA' => 'keys-script',
@@ -359,6 +359,11 @@ class RedisStrategyTest extends PredisTestCase
'HVALS' => 'keys-first',
'HSCAN' => 'keys-first',
/* commands operating on HyperLogLog */
'PFADD' => 'keys-first',
'PFCOUNT' => 'keys-all',
'PFMERGE' => 'keys-all',
/* scripting */
'EVAL' => 'keys-script',
'EVALSHA' => 'keys-script',
@@ -0,0 +1,88 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command;
/**
* @group commands
* @group realm-hyperloglog
* @todo Add integration tests depending on the minor redis version
*/
class HyperLogLogAddTest extends PredisCommandTestCase
{
/**
* {@inheritdoc}
*/
protected function getExpectedCommand()
{
return 'Predis\Command\HyperLogLogAdd';
}
/**
* {@inheritdoc}
*/
protected function getExpectedId()
{
return 'PFADD';
}
/**
* @group disconnected
*/
public function testFilterArguments()
{
$arguments = array('key', 'a', 'b', 'c');
$expected = array('key', 'a', 'b', 'c');
$command = $this->getCommand();
$command->setArguments($arguments);
$this->assertSame($expected, $command->getArguments());
}
/**
* @group disconnected
*/
public function testFilterArgumentsFieldsAsSingleArray()
{
$arguments = array('key', array('a', 'b', 'c'));
$expected = array('key', 'a', 'b', 'c');
$command = $this->getCommand();
$command->setArguments($arguments);
$this->assertSame($expected, $command->getArguments());
}
/**
* @group disconnected
*/
public function testParseResponse()
{
$command = $this->getCommand();
$this->assertSame(false, $command->parseResponse(0));
$this->assertSame(true, $command->parseResponse(1));
}
/**
* @group connected
* @expectedException Predis\Response\ServerException
* @expectedExceptionMessage Operation against a key holding the wrong kind of value
*/
public function testThrowsExceptionOnWrongType()
{
$redis = $this->getClient();
$redis->lpush('metavars', 'foo', 'hoge');
$redis->pfadd('metavars', 'foofoo');
}
}
@@ -0,0 +1,103 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command;
/**
* @group commands
* @group realm-hyperloglog
* @todo Add integration tests depending on the minor redis version
*/
class HyperLogLogCountTest extends PredisCommandTestCase
{
/**
* {@inheritdoc}
*/
protected function getExpectedCommand()
{
return 'Predis\Command\HyperLogLogCount';
}
/**
* {@inheritdoc}
*/
protected function getExpectedId()
{
return 'PFCOUNT';
}
/**
* @group disconnected
*/
public function testFilterArguments()
{
$arguments = array('key:1', 'key:2');
$expected = array('key:1', 'key:2');
$command = $this->getCommand();
$command->setArguments($arguments);
$this->assertSame($expected, $command->getArguments());
}
/**
* @group disconnected
*/
public function testFilterArgumentsFieldsAsSingleArray()
{
$arguments = array(array('key:1', 'key:2'));
$expected = array('key:1', 'key:2');
$command = $this->getCommand();
$command->setArguments($arguments);
$this->assertSame($expected, $command->getArguments());
}
/**
* @group disconnected
*/
public function testParseResponse()
{
$command = $this->getCommand();
$this->assertSame(0, $command->parseResponse(0));
$this->assertSame(1, $command->parseResponse(1));
$this->assertSame(10, $command->parseResponse(10));
}
/**
* @group connected
* @expectedException Predis\Response\ServerException
* @expectedExceptionMessage Operation against a key holding the wrong kind of value
*/
public function testThrowsExceptionOnWrongType()
{
$redis = $this->getClient();
$redis->lpush('metavars', 'foo', 'hoge');
$redis->pfcount('metavars');
}
/**
* @group connected
* @expectedException Predis\Response\ServerException
* @expectedExceptionMessage Operation against a key holding the wrong kind of value
*/
public function testThrowsExceptionOnWrongTypeOfAtLeastOneKey()
{
$redis = $this->getClient();
$redis->pfadd('metavars:1', 'foo', 'hoge');
$redis->lpush('metavars:2', 'foofoo');
$redis->pfcount('metavars:1', 'metavars:2');
}
}
@@ -0,0 +1,86 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command;
/**
* @group commands
* @group realm-hyperloglog
* @todo Add integration tests depending on the minor redis version
*/
class HyperLogLogMergeTest extends PredisCommandTestCase
{
/**
* {@inheritdoc}
*/
protected function getExpectedCommand()
{
return 'Predis\Command\HyperLogLogMerge';
}
/**
* {@inheritdoc}
*/
protected function getExpectedId()
{
return 'PFMERGE';
}
/**
* @group disconnected
*/
public function testFilterArguments()
{
$arguments = array('key:1', 'key:2', 'key:3');
$expected = array('key:1', 'key:2', 'key:3');
$command = $this->getCommand();
$command->setArguments($arguments);
$this->assertSame($expected, $command->getArguments());
}
/**
* @group disconnected
*/
public function testFilterArgumentsFieldsAsSingleArray()
{
$arguments = array(array('key:1', 'key:2', 'key:3'));
$expected = array('key:1', 'key:2', 'key:3');
$command = $this->getCommand();
$command->setArguments($arguments);
$this->assertSame($expected, $command->getArguments());
}
/**
* @group disconnected
*/
public function testParseResponse()
{
$this->assertSame('OK', $this->getCommand()->parseResponse('OK'));
}
/**
* @group connected
* @expectedException Predis\Response\ServerException
* @expectedExceptionMessage Operation against a key holding the wrong kind of value
*/
public function testThrowsExceptionOnWrongType()
{
$redis = $this->getClient();
$redis->pfadd('metavars:1', 'foo', 'hoge');
$redis->lpush('metavars:2', 'foofoo');
$redis->pfmerge('metavars:1', 'metavars:2');
}
}
@@ -799,6 +799,18 @@ class KeyPrefixProcessorTest extends PredisTestCase
array('key', '0', 'MATCH', 'field:*', 'COUNT', 10),
array('prefix:key', '0', 'MATCH', 'field:*', 'COUNT', 10),
),
array('PFADD',
array('key', 'a', 'b', 'c'),
array('prefix:key', 'a', 'b', 'c'),
),
array('PFCOUNT',
array('key:1', 'key:2', 'key:3'),
array('prefix:key:1', 'prefix:key:2', 'prefix:key:3'),
),
array('PFMERGE',
array('key:1', 'key:2', 'key:3'),
array('prefix:key:1', 'prefix:key:2', 'prefix:key:3'),
),
);
}
}
@@ -179,6 +179,9 @@ class RedisUnstableTest extends PredisProfileTestCase
138 => 'ZSCAN',
139 => 'HSCAN',
140 => 'PUBSUB',
141 => 'PFADD',
142 => 'PFCOUNT',
143 => 'PFMERGE',
);
}
}
@@ -179,6 +179,9 @@ class RedisVersion280Test extends PredisProfileTestCase
138 => 'ZSCAN',
139 => 'HSCAN',
140 => 'PUBSUB',
141 => 'PFADD',
142 => 'PFCOUNT',
143 => 'PFMERGE',
);
}
}
@@ -358,6 +358,11 @@ class ReplicationStrategyTest extends PredisTestCase
'HVALS' => 'read',
'HSCAN' => 'read',
/* commands operating on HyperLogLog */
'PFADD' => 'write',
'PFMERGE' => 'write',
'PFCOUNT' => 'read',
/* scripting */
'EVAL' => 'write',
'EVALSHA' => 'write',