Ver código fonte

New command: ZLEXCOUNT (Redis 2.8.9).

Closes #176.
Daniele Alessandri 10 anos atrás
pai
commit
58d1cc12dc

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

@@ -138,6 +138,7 @@ class PredisClusterHashStrategy implements CommandHashStrategyInterface
             'ZSCORE'                => $keyIsFirstArgument,
             'ZUNIONSTORE'           => array($this, 'getKeyFromZsetAggregationCommands'),
             'ZSCAN'                 => $keyIsFirstArgument,
+            'ZLEXCOUNT'             => $keyIsFirstArgument,
 
             /* commands operating on hashes */
             'HDEL'                  => $keyIsFirstArgument,

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

@@ -124,6 +124,7 @@ class RedisClusterHashStrategy implements CommandHashStrategyInterface
             'ZREVRANK'              => $keyIsFirstArgument,
             'ZSCORE'                => $keyIsFirstArgument,
             'ZSCAN'                 => $keyIsFirstArgument,
+            'ZLEXCOUNT'             => $keyIsFirstArgument,
 
             /* commands operating on hashes */
             'HDEL'                  => $keyIsFirstArgument,

+ 27 - 0
lib/Predis/Command/ZSetLexCount.php

@@ -0,0 +1,27 @@
+<?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/zlexcount
+ * @author Daniele Alessandri <suppakilla@gmail.com>
+ */
+class ZSetLexCount extends PrefixableCommand
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function getId()
+    {
+        return 'ZLEXCOUNT';
+    }
+}

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

@@ -239,6 +239,7 @@ class ServerVersion28 extends ServerProfile
 
             /* commands operating on sorted sets */
             'zscan'                     => 'Predis\Command\ZSetScan',
+            'zlexcount'                 => 'Predis\Command\ZSetLexCount',
 
             /* commands operating on hashes */
             'hscan'                     => 'Predis\Command\HashScan',

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

@@ -201,6 +201,7 @@ class ReplicationStrategy
             'ZRANK'             => true,
             'ZREVRANK'          => true,
             'ZSCAN'             => true,
+            'ZLEXCOUNT'         => true,
             'HGET'              => true,
             'HMGET'             => true,
             'HEXISTS'           => true,

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

@@ -342,6 +342,7 @@ class PredisClusterHashStrategyTest extends PredisTestCase
             'ZSCORE'                => 'keys-first',
             'ZUNIONSTORE'           => 'keys-zaggregated',
             'ZSCAN'                 => 'keys-first',
+            'ZLEXCOUNT'             => 'keys-first',
 
             /* commands operating on hashes */
             'HDEL'                  => 'keys-first',

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

@@ -342,6 +342,7 @@ class RedisClusterHashStrategyTest extends PredisTestCase
             'ZREVRANK'              => 'keys-first',
             'ZSCORE'                => 'keys-first',
             'ZSCAN'                 => 'keys-first',
+            'ZLEXCOUNT'             => 'keys-first',
 
             /* commands operating on hashes */
             'HDEL'                  => 'keys-first',

+ 161 - 0
tests/Predis/Command/ZSetLexCountTest.php

@@ -0,0 +1,161 @@
+<?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-zset
+ */
+class ZSetLexCountTest extends PredisCommandTestCase
+{
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedCommand()
+    {
+        return 'Predis\Command\ZSetLexCount';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedId()
+    {
+        return 'ZLEXCOUNT';
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testFilterArguments()
+    {
+        $arguments = array('key', '+', '-');
+        $expected = array('key', '+', '-');
+
+        $command = $this->getCommand();
+        $command->setArguments($arguments);
+
+        $this->assertSame($expected, $command->getArguments());
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testParseResponse()
+    {
+        $this->assertSame(1, $this->getCommand()->parseResponse(1));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testPrefixKeys()
+    {
+        $arguments = array('key', '+', '-');
+        $expected = array('prefix:key', '+', '-');
+
+        $command = $this->getCommandWithArgumentsArray($arguments);
+        $command->prefixKeys('prefix:');
+
+        $this->assertSame($expected, $command->getArguments());
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testPrefixKeysIgnoredOnEmptyArguments()
+    {
+        $command = $this->getCommand();
+        $command->prefixKeys('prefix:');
+
+        $this->assertSame(array(), $command->getArguments());
+    }
+
+    /**
+     * @group connected
+     */
+    public function testExclusiveIntervalRange()
+    {
+        $this->markTestSkippedOnRedisVersionBelow('2.8.9', 'Lexicographical operations on sorted sets require Redis >= 2.8.9.', true);
+
+        $redis = $this->getClient();
+
+        $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
+
+        $this->assertSame(3, $redis->zlexcount('letters', '(b', '(f'));
+        $this->assertSame(5, $redis->zlexcount('letters', '(b', '(z'));
+        $this->assertSame(4, $redis->zlexcount('letters', '(0', '(e'));
+        $this->assertSame(0, $redis->zlexcount('letters', '(f', '(b'));
+    }
+
+    /**
+     * @group connected
+     */
+    public function testInclusiveIntervalRange()
+    {
+        $this->markTestSkippedOnRedisVersionBelow('2.8.9', 'Lexicographical operations on sorted sets require Redis >= 2.8.9.', true);
+
+        $redis = $this->getClient();
+
+        $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
+
+        $this->assertSame(5, $redis->zlexcount('letters', '[b', '[f'));
+        $this->assertSame(6, $redis->zlexcount('letters', '[b', '[z'));
+        $this->assertSame(5, $redis->zlexcount('letters', '[0', '[e'));
+        $this->assertSame(0, $redis->zlexcount('letters', '[f', '[b'));
+    }
+
+    /**
+     * @group connected
+     */
+    public function testWholeRangeInterval()
+    {
+        $this->markTestSkippedOnRedisVersionBelow('2.8.9', 'Lexicographical operations on sorted sets require Redis >= 2.8.9.', true);
+
+        $redis = $this->getClient();
+
+        $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
+
+        $this->assertSame(7, $redis->zlexcount('letters', '-', '+'));
+        $this->assertSame(0, $redis->zlexcount('letters', '+', '-'));
+    }
+
+    /**
+     * @group connected
+     * @expectedException Predis\ServerException
+     * @expectedExceptionMessage min or max not valid string range item
+     */
+    public function testThrowsExceptionOnInvalidRangeFormat()
+    {
+        $this->markTestSkippedOnRedisVersionBelow('2.8.9', 'Lexicographical operations on sorted sets require Redis >= 2.8.9.', true);
+
+        $redis = $this->getClient();
+
+        $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
+        $redis->zlexcount('letters', 'b', 'f');
+    }
+
+    /**
+     * @group connected
+     * @expectedException Predis\ServerException
+     * @expectedExceptionMessage Operation against a key holding the wrong kind of value
+     */
+    public function testThrowsExceptionOnWrongType()
+    {
+        $this->markTestSkippedOnRedisVersionBelow('2.8.9', 'Lexicographical operations on sorted sets require Redis >= 2.8.9.', true);
+
+        $redis = $this->getClient();
+
+        $redis->set('foo', 'bar');
+        $redis->zlexcount('foo', '+', '-');
+    }
+}

+ 5 - 4
tests/Predis/Profile/ServerVersion28Test.php

@@ -176,10 +176,11 @@ class ServerVersion28Test extends PredisProfileTestCase
             135 => 'scan',
             136 => 'sscan',
             137 => 'zscan',
-            138 => 'hscan',
-            139 => 'pfadd',
-            140 => 'pfcount',
-            141 => 'pfmerge',
+            138 => 'zlexcount',
+            139 => 'hscan',
+            140 => 'pfadd',
+            141 => 'pfcount',
+            142 => 'pfmerge',
         );
     }
 }

+ 5 - 4
tests/Predis/Profile/ServerVersionNextTest.php

@@ -176,10 +176,11 @@ class ServerVersionNextTest extends PredisProfileTestCase
             135 => 'scan',
             136 => 'sscan',
             137 => 'zscan',
-            138 => 'hscan',
-            139 => 'pfadd',
-            140 => 'pfcount',
-            141 => 'pfmerge',
+            138 => 'zlexcount',
+            139 => 'hscan',
+            140 => 'pfadd',
+            141 => 'pfcount',
+            142 => 'pfmerge',
         );
     }
 }

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

@@ -342,6 +342,7 @@ class ReplicationStrategyTest extends PredisTestCase
             'ZREVRANK'              => 'read',
             'ZSCORE'                => 'read',
             'ZSCAN'                 => 'read',
+            'ZLEXCOUNT'             => 'read',
 
             /* commands operating on hashes */
             'HDEL'                  => 'write',