浏览代码

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

Backported from the master branch.

Many thanks to @rubensayshi for his initial commits on this in #163,
but I had to start from scratch for the master branch first.
Daniele Alessandri 10 年之前
父节点
当前提交
0e3b105959

+ 3 - 0
CHANGELOG.md

@@ -4,6 +4,9 @@ v0.8.6 (2014-xx-xx)
 - Switched to Redis 2.8 as the default server profile as there are no changes
   that would break compatibility with previous releases.
 
+- Added `PFADD`, `PFCOUNT`, `PFMERGE` to the server profile for Redis 2.8 for
+  handling the HyperLogLog data structure introduced in Redis 2.8.9.
+
 - Minor tweaks to make this version of Predis compatible with HHVM >= 2.4.0.
 
 - Add support for key hash tags when using redis-cluster (Redis 3.0.0b1).

+ 5 - 0
lib/Predis/Cluster/PredisClusterHashStrategy.php

@@ -155,6 +155,11 @@ class PredisClusterHashStrategy implements CommandHashStrategyInterface
             'HVALS'                 => $keyIsFirstArgument,
             'HSCAN'                 => $keyIsFirstArgument,
 
+            /* commands operating on HyperLogLog */
+            'PFADD'                 => $keyIsFirstArgument,
+            'PFCOUNT'               => $keysAreAllArguments,
+            'PFMERGE'               => $keysAreAllArguments,
+
             /* scripting */
             'EVAL'                  => array($this, 'getKeyFromScriptingCommands'),
             'EVALSHA'               => array($this, 'getKeyFromScriptingCommands'),

+ 5 - 0
lib/Predis/Cluster/RedisClusterHashStrategy.php

@@ -141,6 +141,11 @@ class RedisClusterHashStrategy implements CommandHashStrategyInterface
             '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 - 0
lib/Predis/Command/HyperLogLogAdd.php

@@ -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 PrefixableCommand
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function getId()
+    {
+        return 'PFADD';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function filterArguments(array $arguments)
+    {
+        return self::normalizeVariadic($arguments);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function parseResponse($data)
+    {
+        return (bool) $data;
+    }
+}

+ 43 - 0
lib/Predis/Command/HyperLogLogCount.php

@@ -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/pfcount
+ * @author Daniele Alessandri <suppakilla@gmail.com>
+ */
+class HyperLogLogCount extends PrefixableCommand
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function getId()
+    {
+        return 'PFCOUNT';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function filterArguments(array $arguments)
+    {
+        return self::normalizeArguments($arguments);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function prefixKeys($prefix)
+    {
+        PrefixHelpers::all($this, $prefix);
+    }
+}

+ 43 - 0
lib/Predis/Command/HyperLogLogMerge.php

@@ -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/pfmerge
+ * @author Daniele Alessandri <suppakilla@gmail.com>
+ */
+class HyperLogLogMerge extends PrefixableCommand
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function getId()
+    {
+        return 'PFMERGE';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function filterArguments(array $arguments)
+    {
+        return self::normalizeArguments($arguments);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function prefixKeys($prefix)
+    {
+        PrefixHelpers::all($this, $prefix);
+    }
+}

+ 5 - 0
lib/Predis/Profile/ServerVersion28.php

@@ -242,6 +242,11 @@ class ServerVersion28 extends ServerProfile
 
             /* commands operating on hashes */
             'hscan'                     => 'Predis\Command\HashScan',
+
+            /* commands operating on HyperLogLog */
+            'pfadd'                     => 'Predis\Command\HyperLogLogAdd',
+            'pfcount'                   => 'Predis\Command\HyperLogLogCount',
+            'pfmerge'                   => 'Predis\Command\HyperLogLogMerge',
         );
     }
 }

+ 1 - 0
lib/Predis/Replication/ReplicationStrategy.php

@@ -217,6 +217,7 @@ class ReplicationStrategy
             'OBJECT'            => true,
             'BITCOUNT'          => true,
             'TIME'              => true,
+            'PFCOUNT'           => true,
             'SORT'              => array($this, 'isSortReadOnly'),
         );
     }

+ 5 - 0
tests/Predis/Cluster/PredisClusterHashStrategyTest.php

@@ -359,6 +359,11 @@ class PredisClusterHashStrategyTest 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',

+ 5 - 0
tests/Predis/Cluster/RedisClusterHashStrategyTest.php

@@ -359,6 +359,11 @@ class RedisClusterHashStrategyTest 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',

+ 88 - 0
tests/Predis/Command/HyperLogLogAddTest.php

@@ -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\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');
+    }
+}

+ 103 - 0
tests/Predis/Command/HyperLogLogCountTest.php

@@ -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\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\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');
+    }
+}

+ 86 - 0
tests/Predis/Command/HyperLogLogMergeTest.php

@@ -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\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');
+    }
+}

+ 3 - 0
tests/Predis/Profile/ServerVersion28Test.php

@@ -177,6 +177,9 @@ class ServerVersion28Test extends PredisProfileTestCase
             136 => 'sscan',
             137 => 'zscan',
             138 => 'hscan',
+            139 => 'pfadd',
+            140 => 'pfcount',
+            141 => 'pfmerge',
         );
     }
 }

+ 3 - 0
tests/Predis/Profile/ServerVersionNextTest.php

@@ -177,6 +177,9 @@ class ServerVersionNextTest extends PredisProfileTestCase
             136 => 'sscan',
             137 => 'zscan',
             138 => 'hscan',
+            139 => 'pfadd',
+            140 => 'pfcount',
+            141 => 'pfmerge',
         );
     }
 }

+ 5 - 0
tests/Predis/Replication/ReplicationStrategyTest.php

@@ -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',