Explorar o código

New command: ZREMRANGEBYLEX (Redis 2.8.9).

Daniele Alessandri %!s(int64=10) %!d(string=hai) anos
pai
achega
940326e5bd

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

@@ -141,6 +141,7 @@ class PredisStrategy implements StrategyInterface
             'ZSCAN'                 => $keyIsFirstArgument,
             'ZLEXCOUNT'             => $keyIsFirstArgument,
             'ZRANGEBYLEX'           => $keyIsFirstArgument,
+            'ZREMRANGEBYLEX'        => $keyIsFirstArgument,
 
             /* commands operating on hashes */
             'HDEL'                  => $keyIsFirstArgument,

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

@@ -126,6 +126,7 @@ class RedisStrategy implements StrategyInterface
             'ZSCAN'                 => $keyIsFirstArgument,
             'ZLEXCOUNT'             => $keyIsFirstArgument,
             'ZRANGEBYLEX'           => $keyIsFirstArgument,
+            'ZREMRANGEBYLEX'        => $keyIsFirstArgument,
 
             /* commands operating on hashes */
             'HDEL'                  => $keyIsFirstArgument,

+ 1 - 0
lib/Predis/Command/Processor/KeyPrefixProcessor.php

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

+ 27 - 0
lib/Predis/Command/ZSetRemoveRangeByLex.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/zremrangebylex
+ * @author Daniele Alessandri <suppakilla@gmail.com>
+ */
+class ZSetRemoveRangeByLex extends Command
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function getId()
+    {
+        return 'ZREMRANGEBYLEX';
+    }
+}

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

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

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

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

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

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

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

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

+ 149 - 0
tests/Predis/Command/ZSetRemoveRangeByLexTest.php

@@ -0,0 +1,149 @@
+<?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 ZSetRemoveRangeByLexTest extends PredisCommandTestCase
+{
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedCommand()
+    {
+        return 'Predis\Command\ZSetRemoveRangeByLex';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedId()
+    {
+        return 'ZREMRANGEBYLEX';
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testFilterArguments()
+    {
+        $arguments = array('key', '[a', '[b');
+        $expected = array('key', '[a', '[b');
+
+        $command = $this->getCommand();
+        $command->setArguments($arguments);
+
+        $this->assertSame($expected, $command->getArguments());
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testParseResponse()
+    {
+        $this->assertSame(1, $this->getCommand()->parseResponse(1));
+    }
+
+    /**
+     * @group connected
+     */
+    public function testRemovesRangeByLexWithWholeRange()
+    {
+        $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(0, $redis->zremrangebylex('letters', '+', '-'));
+        $this->assertSame(7, $redis->zremrangebylex('letters', '-', '+'));
+
+        $this->assertSame(array(), $redis->zrange('letters', 0, -1));
+    }
+
+    /**
+     * @group connected
+     */
+    public function testRemovesRangeByLexWithInclusiveRange()
+    {
+        $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->zremrangebylex('letters', '[b', '[d'));
+        $this->assertSame(array('a', 'e', 'f', 'g'), $redis->zrange('letters', 0, -1));
+    }
+
+    /**
+     * @group connected
+     */
+    public function testRemovesRangeByLexWithExclusiveRange()
+    {
+        $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->zremrangebylex('letters', '(a', '(e'));
+        $this->assertSame(array('a', 'e', 'f', 'g'), $redis->zrange('letters', 0, -1));
+    }
+
+    /**
+     * @group connected
+     */
+    public function testRemovesRangeByLexWithMixedRange()
+    {
+        $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->zremrangebylex('letters', '[b', '(e'));
+        $this->assertSame(array('a', 'e', 'f', 'g'), $redis->zrange('letters', 0, -1));
+    }
+
+    /**
+     * @group connected
+     * @expectedException Predis\Response\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->zremrangebylex('letters', 'b', 'f');
+    }
+
+    /**
+     * @group connected
+     * @expectedException Predis\Response\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->zremrangebylex('foo', '[a', '[b');
+    }
+}

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

@@ -179,11 +179,12 @@ class RedisUnstableTest extends PredisProfileTestCase
             138 => 'ZSCAN',
             139 => 'ZLEXCOUNT',
             140 => 'ZRANGEBYLEX',
-            141 => 'HSCAN',
-            142 => 'PUBSUB',
-            143 => 'PFADD',
-            144 => 'PFCOUNT',
-            145 => 'PFMERGE',
+            141 => 'ZREMRANGEBYLEX',
+            142 => 'HSCAN',
+            143 => 'PUBSUB',
+            144 => 'PFADD',
+            145 => 'PFCOUNT',
+            146 => 'PFMERGE',
         );
     }
 }

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

@@ -179,11 +179,12 @@ class RedisVersion280Test extends PredisProfileTestCase
             138 => 'ZSCAN',
             139 => 'ZLEXCOUNT',
             140 => 'ZRANGEBYLEX',
-            141 => 'HSCAN',
-            142 => 'PUBSUB',
-            143 => 'PFADD',
-            144 => 'PFCOUNT',
-            145 => 'PFMERGE',
+            141 => 'ZREMRANGEBYLEX',
+            142 => 'HSCAN',
+            143 => 'PUBSUB',
+            144 => 'PFADD',
+            145 => 'PFCOUNT',
+            146 => 'PFMERGE',
         );
     }
 }

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

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