Browse Source

New command: ZRANGEBYLEX (Redis 2.8.9).

Closes #175.
Daniele Alessandri 10 năm trước cách đây
mục cha
commit
0a580c9d1b

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

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

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

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

+ 54 - 0
lib/Predis/Command/ZSetRangeByLex.php

@@ -0,0 +1,54 @@
+<?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/zrangebylex
+ * @author Daniele Alessandri <suppakilla@gmail.com>
+ */
+class ZSetRangeByLex extends ZSetRange
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function getId()
+    {
+        return 'ZRANGEBYLEX';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function prepareOptions($options)
+    {
+        $opts = array_change_key_case($options, CASE_UPPER);
+        $finalizedOpts = array();
+
+        if (isset($opts['LIMIT']) && is_array($opts['LIMIT'])) {
+            $limit = array_change_key_case($opts['LIMIT'], CASE_UPPER);
+
+            $finalizedOpts[] = 'LIMIT';
+            $finalizedOpts[] = isset($limit['OFFSET']) ? $limit['OFFSET'] : $limit[0];
+            $finalizedOpts[] = isset($limit['COUNT']) ? $limit['COUNT'] : $limit[1];
+        }
+
+        return $finalizedOpts;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function withScores()
+    {
+        return false;
+    }
+}

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

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

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

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

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

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

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

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

+ 230 - 0
tests/Predis/Command/ZSetRangeByLexTest.php

@@ -0,0 +1,230 @@
+<?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 ZSetRangeByLexTest extends PredisCommandTestCase
+{
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedCommand()
+    {
+        return 'Predis\Command\ZSetRangeByLex';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedId()
+    {
+        return 'ZRANGEBYLEX';
+    }
+
+    /**
+     * @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 disconnected
+     */
+    public function testPrefixKeys()
+    {
+        $modifiers = array(
+            'limit' => array(0, 100),
+        );
+
+        $arguments = array('zset', '[a', '[z', $modifiers);
+        $expected = array('prefix:zset', '[a', '[z', 'LIMIT', 0, 100);
+
+        $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 testReturnsElementsInWholeRange()
+    {
+        $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(array('a', 'b', 'c', 'd', 'e', 'f', 'g'), $redis->zrangebylex('letters', '-', '+'));
+        $this->assertSame(array(), $redis->zrangebylex('letters', '+', '-'));
+        $this->assertSame(array(), $redis->zrangebylex('unknown', '-', '+'));
+        $this->assertSame(array(), $redis->zrangebylex('unknown', '+', '-'));
+    }
+
+    /**
+     * @group connected
+     */
+    public function testReturnsElementsInInclusiveRange()
+    {
+        $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(array('a'), $redis->zrangebylex('letters', '[a', '[a'));
+        $this->assertSame(array('c', 'd', 'e', 'f'), $redis->zrangebylex('letters', '[c', '[f'));
+        $this->assertSame(array('a', 'b', 'c'), $redis->zrangebylex('letters', '-', '[c'));
+        $this->assertSame(array(), $redis->zrangebylex('letters', '+', '[c'));
+        $this->assertSame(array(), $redis->zrangebylex('letters', '[x', '[z'));
+        $this->assertSame(array(), $redis->zrangebylex('unknown', '[0', '[1'));
+    }
+
+    /**
+     * @group connected
+     */
+    public function testReturnsElementsInExclusiveRange()
+    {
+        $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(array(), $redis->zrangebylex('letters', '(a', '(a'));
+        $this->assertSame(array('d', 'e'), $redis->zrangebylex('letters', '(c', '(f'));
+        $this->assertSame(array('a', 'b'), $redis->zrangebylex('letters', '-', '(c'));
+        $this->assertSame(array(), $redis->zrangebylex('letters', '+', '(c'));
+        $this->assertSame(array(), $redis->zrangebylex('letters', '(x', '(z'));
+        $this->assertSame(array(), $redis->zrangebylex('unknown', '(0', '(1'));
+    }
+
+    /**
+     * @group connected
+     */
+    public function testReturnsElementsInMixedRange()
+    {
+        $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(array(), $redis->zrangebylex('letters', '[a', '(a'));
+        $this->assertSame(array(), $redis->zrangebylex('letters', '(a', '[a'));
+        $this->assertSame(array('c', 'd', 'e'), $redis->zrangebylex('letters', '[c', '(f'));
+        $this->assertSame(array('d', 'e', 'f'), $redis->zrangebylex('letters', '(c', '[f'));
+        $this->assertSame(array(), $redis->zrangebylex('unknown', '[0', '(5'));
+    }
+
+    /**
+     * @group connected
+     */
+    public function testRangeWithLimitModifier()
+    {
+        $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(array('c', 'd', 'e'), $redis->zrangebylex('letters', '-', '+', 'LIMIT', '2', '3'));
+        $this->assertSame(array('c', 'd', 'e'), $redis->zrangebylex('letters', '-', '+', array('limit' => array(2, 3))));
+        $this->assertSame(array('c', 'd', 'e'), $redis->zrangebylex('letters', '-', '+', array('limit' => array('offset' => 2, 'count' => 3))));
+        $this->assertSame(array(), $redis->zrangebylex('letters', '[a', '[f', 'LIMIT', '2', '0'));
+        $this->assertSame(array(), $redis->zrangebylex('letters', '[a', '[f', 'LIMIT', '-4', '2'));
+    }
+
+    /**
+     * @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->zrangebylex('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->zrangebylex('foo', '-', '+');
+    }
+}

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

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

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

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

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

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