Переглянути джерело

add zrevrangebylex command

Michal Humpula 10 роки тому
батько
коміт
af48b30398

+ 1 - 0
src/Cluster/ClusterStrategy.php

@@ -138,6 +138,7 @@ abstract class ClusterStrategy implements StrategyInterface
             'ZLEXCOUNT'             => $getKeyFromFirstArgument,
             'ZRANGEBYLEX'           => $getKeyFromFirstArgument,
             'ZREMRANGEBYLEX'        => $getKeyFromFirstArgument,
+            'ZREVRANGEBYLEX'        => $getKeyFromFirstArgument,
 
             /* commands operating on hashes */
             'HDEL'                  => $getKeyFromFirstArgument,

+ 1 - 0
src/Command/Processor/KeyPrefixProcessor.php

@@ -155,6 +155,7 @@ class KeyPrefixProcessor implements ProcessorInterface
             'ZLEXCOUNT'                 => 'self::first',
             'ZRANGEBYLEX'               => 'self::first',
             'ZREMRANGEBYLEX'            => 'self::first',
+            'ZREVRANGEBYLEX'            => 'self::first',
         );
     }
 

+ 21 - 0
src/Command/ZSetReverseRangeByLex.php

@@ -0,0 +1,21 @@
+<?php
+
+/*
+ * This file is part of the Predis package.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Predis\Command;
+
+class ZSetReverseRangeByLex extends ZSetRangeByLex
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function getId()
+    {
+        return 'ZREVRANGEBYLEX';
+    }
+}

+ 1 - 0
src/Profile/RedisVersion280.php

@@ -246,6 +246,7 @@ class RedisVersion280 extends RedisProfile
             'ZLEXCOUNT'                 => 'Predis\Command\ZSetLexCount',
             'ZRANGEBYLEX'               => 'Predis\Command\ZSetRangeByLex',
             'ZREMRANGEBYLEX'            => 'Predis\Command\ZSetRemoveRangeByLex',
+            'ZREVRANGEBYLEX'            => 'Predis\Command\ZSetReverseRangeByLex',
 
             /* commands operating on hashes */
             'HSCAN'                     => 'Predis\Command\HashScan',

+ 1 - 0
src/Profile/RedisVersion300.php

@@ -246,6 +246,7 @@ class RedisVersion300 extends RedisProfile
             'ZLEXCOUNT'                 => 'Predis\Command\ZSetLexCount',
             'ZRANGEBYLEX'               => 'Predis\Command\ZSetRangeByLex',
             'ZREMRANGEBYLEX'            => 'Predis\Command\ZSetRemoveRangeByLex',
+            'ZREVRANGEBYLEX'            => 'Predis\Command\ZSetReverseRangeByLex',
 
             /* commands operating on hashes */
             'HSCAN'                     => 'Predis\Command\HashScan',

+ 1 - 0
src/Replication/ReplicationStrategy.php

@@ -211,6 +211,7 @@ class ReplicationStrategy
             'ZSCAN'             => true,
             'ZLEXCOUNT'         => true,
             'ZRANGEBYLEX'       => true,
+            'ZREVRANGEBYLEX'    => true,
             'HGET'              => true,
             'HMGET'             => true,
             'HEXISTS'           => true,

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

@@ -349,6 +349,7 @@ class PredisStrategyTest extends PredisTestCase
             'ZLEXCOUNT'             => 'keys-first',
             'ZRANGEBYLEX'           => 'keys-first',
             'ZREMRANGEBYLEX'        => 'keys-first',
+            'ZREVRANGEBYLEX'        => 'keys-first',
 
             /* commands operating on hashes */
             'HDEL'                  => 'keys-first',

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

@@ -359,6 +359,7 @@ class RedisStrategyTest extends PredisTestCase
             'ZLEXCOUNT'             => 'keys-first',
             'ZRANGEBYLEX'           => 'keys-first',
             'ZREMRANGEBYLEX'        => 'keys-first',
+            'ZREVRANGEBYLEX'        => 'keys-first',
 
             /* commands operating on hashes */
             'HDEL'                  => 'keys-first',

+ 4 - 0
tests/Predis/Command/Processor/KeyPrefixProcessorTest.php

@@ -825,6 +825,10 @@ class KeyPrefixProcessorTest extends PredisTestCase
                 array('key', '-', '+'),
                 array('prefix:key', '-', '+'),
             ),
+            array('ZREVRANGEBYLEX',
+                array('key', '+', '-', 'LIMIT', '0', '10'),
+                array('prefix:key', '+', '-', 'LIMIT', '0', '10'),
+            ),
         );
     }
 }

+ 192 - 0
tests/Predis/Command/ZSetReverseRangeByLexTest.php

@@ -0,0 +1,192 @@
+<?php
+
+/*
+ * This file is part of the Predis package.
+ *
+ * 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 ZSetRevRangeByLexTest extends PredisCommandTestCase
+{
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedCommand()
+    {
+        return 'Predis\Command\ZSetReverseRangeByLex';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedId()
+    {
+        return 'ZREVRANGEBYLEX';
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testFilterArguments()
+    {
+        $modifiers = array(
+            'limit' => array(0, 100),
+        );
+
+        $arguments = array('zset', '[a', '[z', $modifiers);
+        $expected = array('zset', '[a', '[z', 'LIMIT', 0, 100);
+
+        $command = $this->getCommand();
+        $command->setArguments($arguments);
+
+        $this->assertSame($expected, $command->getArguments());
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testFilterArgumentsWithNamedLimit()
+    {
+        $arguments = array('zset', '[a', '[z', array('limit' => array('offset' => 1, 'count' => 2)));
+        $expected = array('zset', '[a', '[z', 'LIMIT', 1, 2);
+
+        $command = $this->getCommand();
+        $command->setArguments($arguments);
+
+        $this->assertSame($expected, $command->getArguments());
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testParseResponse()
+    {
+        $raw = array('a', 'b', 'c');
+        $expected = array('a', 'b', 'c');
+
+        $command = $this->getCommand();
+
+        $this->assertSame($expected, $command->parseResponse($raw));
+    }
+
+    /**
+     * @group connected
+     * @requiresRedisVersion >= 2.8.9
+     */
+    public function testReturnsElementsInWholeRange()
+    {
+        $redis = $this->getClient();
+
+        $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
+
+        $this->assertSame(array('g', 'f', 'e', 'd', 'c', 'b', 'a'), $redis->zrevrangebylex('letters', '+', '-'));
+        $this->assertSame(array(), $redis->zrevrangebylex('letters', '-', '+'));
+        $this->assertSame(array(), $redis->zrevrangebylex('unknown', '-', '+'));
+        $this->assertSame(array(), $redis->zrevrangebylex('unknown', '+', '-'));
+    }
+
+    /**
+     * @group connected
+     * @requiresRedisVersion >= 2.8.9
+     */
+    public function testReturnsElementsInInclusiveRange()
+    {
+        $redis = $this->getClient();
+
+        $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
+
+        $this->assertSame(array('a'), $redis->zrevrangebylex('letters', '[a', '[a'));
+        $this->assertSame(array('f', 'e', 'd', 'c'), $redis->zrevrangebylex('letters', '[f', '[c'));
+        $this->assertSame(array('g', 'f', 'e'), $redis->zrevrangebylex('letters', '+', '[e'));
+        $this->assertSame(array(), $redis->zrevrangebylex('letters', '-', '[c'));
+        $this->assertSame(array(), $redis->zrevrangebylex('letters', '[z', '[x'));
+        $this->assertSame(array(), $redis->zrevrangebylex('unknown', '[1', '[0'));
+    }
+
+    /**
+     * @group connected
+     * @requiresRedisVersion >= 2.8.9
+     */
+    public function testReturnsElementsInExclusiveRange()
+    {
+        $redis = $this->getClient();
+
+        $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
+
+        $this->assertSame(array(), $redis->zrevrangebylex('letters', '(a', '(a'));
+        $this->assertSame(array('e', 'd'), $redis->zrevrangebylex('letters', '(f', '(c'));
+        $this->assertSame(array('g', 'f'), $redis->zrevrangebylex('letters', '+', '(e'));
+        $this->assertSame(array(), $redis->zrevrangebylex('letters', '-', '(c'));
+        $this->assertSame(array(), $redis->zrevrangebylex('letters', '(z', '(x'));
+        $this->assertSame(array(), $redis->zrevrangebylex('unknown', '(1', '(0'));
+    }
+
+    /**
+     * @group connected
+     * @requiresRedisVersion >= 2.8.9
+     */
+    public function testReturnsElementsInMixedRange()
+    {
+        $redis = $this->getClient();
+
+        $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
+
+        $this->assertSame(array(), $redis->zrevrangebylex('letters', '[a', '(a'));
+        $this->assertSame(array(), $redis->zrevrangebylex('letters', '(a', '[a'));
+        $this->assertSame(array('f', 'e', 'd'), $redis->zrevrangebylex('letters', '[f', '(c'));
+        $this->assertSame(array('e', 'd', 'c'), $redis->zrevrangebylex('letters', '(f', '[c'));
+        $this->assertSame(array(), $redis->zrevrangebylex('unknown', '[5', '(0'));
+    }
+
+    /**
+     * @group connected
+     * @requiresRedisVersion >= 2.8.9
+     */
+    public function testRangeWithLimitModifier()
+    {
+        $redis = $this->getClient();
+
+        $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
+
+        $this->assertSame(array('e', 'd', 'c'), $redis->zrevrangebylex('letters', '+', '-', 'LIMIT', '2', '3'));
+        $this->assertSame(array('e', 'd', 'c'), $redis->zrevrangebylex('letters', '+', '-', array('limit' => array(2, 3))));
+        $this->assertSame(array('e', 'd', 'c'), $redis->zrevrangebylex('letters', '+', '-', array('limit' => array('offset' => 2, 'count' => 3))));
+        $this->assertSame(array(), $redis->zrevrangebylex('letters', '[f', '[a', 'LIMIT', '2', '0'));
+        $this->assertSame(array(), $redis->zrevrangebylex('letters', '[f', '[a', 'LIMIT', '-4', '2'));
+    }
+
+    /**
+     * @group connected
+     * @requiresRedisVersion >= 2.8.9
+     * @expectedException \Predis\Response\ServerException
+     * @expectedExceptionMessage min or max not valid string range item
+     */
+    public function testThrowsExceptionOnInvalidRangeFormat()
+    {
+        $redis = $this->getClient();
+
+        $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
+        $redis->zrevrangebylex('letters', 'f', 'b');
+    }
+
+    /**
+     * @group connected
+     * @requiresRedisVersion >= 2.8.9
+     * @expectedException \Predis\Response\ServerException
+     * @expectedExceptionMessage Operation against a key holding the wrong kind of value
+     */
+    public function testThrowsExceptionOnWrongType()
+    {
+        $redis = $this->getClient();
+
+        $redis->set('foo', 'bar');
+        $redis->zrevrangebylex('foo', '+', '-');
+    }
+}

+ 7 - 6
tests/Predis/Profile/RedisUnstableTest.php

@@ -181,12 +181,13 @@ class RedisUnstableTest extends PredisProfileTestCase
             140 => 'ZLEXCOUNT',
             141 => 'ZRANGEBYLEX',
             142 => 'ZREMRANGEBYLEX',
-            143 => 'HSCAN',
-            144 => 'PUBSUB',
-            145 => 'PFADD',
-            146 => 'PFCOUNT',
-            147 => 'PFMERGE',
-            148 => 'COMMAND',
+            143 => 'ZREVRANGEBYLEX',
+            144 => 'HSCAN',
+            145 => 'PUBSUB',
+            146 => 'PFADD',
+            147 => 'PFCOUNT',
+            148 => 'PFMERGE',
+            149 => 'COMMAND',
         );
     }
 }

+ 7 - 6
tests/Predis/Profile/RedisVersion280Test.php

@@ -181,12 +181,13 @@ class RedisVersion280Test extends PredisProfileTestCase
             140 => 'ZLEXCOUNT',
             141 => 'ZRANGEBYLEX',
             142 => 'ZREMRANGEBYLEX',
-            143 => 'HSCAN',
-            144 => 'PUBSUB',
-            145 => 'PFADD',
-            146 => 'PFCOUNT',
-            147 => 'PFMERGE',
-            148 => 'COMMAND',
+            143 => 'ZREVRANGEBYLEX',
+            144 => 'HSCAN',
+            145 => 'PUBSUB',
+            146 => 'PFADD',
+            147 => 'PFCOUNT',
+            148 => 'PFMERGE',
+            149 => 'COMMAND',
         );
     }
 }

+ 7 - 6
tests/Predis/Profile/RedisVersion300Test.php

@@ -181,12 +181,13 @@ class RedisVersion300Test extends PredisProfileTestCase
             140 => 'ZLEXCOUNT',
             141 => 'ZRANGEBYLEX',
             142 => 'ZREMRANGEBYLEX',
-            143 => 'HSCAN',
-            144 => 'PUBSUB',
-            145 => 'PFADD',
-            146 => 'PFCOUNT',
-            147 => 'PFMERGE',
-            148 => 'COMMAND',
+            143 => 'ZREVRANGEBYLEX',
+            144 => 'HSCAN',
+            145 => 'PUBSUB',
+            146 => 'PFADD',
+            147 => 'PFCOUNT',
+            148 => 'PFMERGE',
+            149 => 'COMMAND',
         );
     }
 }

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

@@ -346,6 +346,7 @@ class ReplicationStrategyTest extends PredisTestCase
             'ZLEXCOUNT'             => 'read',
             'ZRANGEBYLEX'           => 'read',
             'ZREMRANGEBYLEX'        => 'write',
+            'ZREVRANGEBYLEX'        => 'read',
 
             /* commands operating on hashes */
             'HDEL'                  => 'write',