From 8d353cc22377cee31695d5c9a13f16e6d680545a Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Mon, 9 Jun 2014 14:17:40 +0200 Subject: [PATCH] New command: ZREMRANGEBYLEX (Redis 2.8.9). Closes #177. --- .../Cluster/PredisClusterHashStrategy.php | 1 + .../Cluster/RedisClusterHashStrategy.php | 1 + lib/Predis/Command/ZSetRemoveRangeByLex.php | 27 +++ lib/Predis/Profile/ServerVersion28.php | 1 + .../Cluster/PredisClusterHashStrategyTest.php | 1 + .../Cluster/RedisClusterHashStrategyTest.php | 1 + .../Command/ZSetRemoveRangeByLexTest.php | 174 ++++++++++++++++++ tests/Predis/Profile/ServerVersion28Test.php | 9 +- .../Predis/Profile/ServerVersionNextTest.php | 9 +- .../Replication/ReplicationStrategyTest.php | 1 + 10 files changed, 217 insertions(+), 8 deletions(-) create mode 100644 lib/Predis/Command/ZSetRemoveRangeByLex.php create mode 100644 tests/Predis/Command/ZSetRemoveRangeByLexTest.php diff --git a/lib/Predis/Cluster/PredisClusterHashStrategy.php b/lib/Predis/Cluster/PredisClusterHashStrategy.php index 5e0330df..04976f13 100644 --- a/lib/Predis/Cluster/PredisClusterHashStrategy.php +++ b/lib/Predis/Cluster/PredisClusterHashStrategy.php @@ -140,6 +140,7 @@ class PredisClusterHashStrategy implements CommandHashStrategyInterface 'ZSCAN' => $keyIsFirstArgument, 'ZLEXCOUNT' => $keyIsFirstArgument, 'ZRANGEBYLEX' => $keyIsFirstArgument, + 'ZREMRANGEBYLEX' => $keyIsFirstArgument, /* commands operating on hashes */ 'HDEL' => $keyIsFirstArgument, diff --git a/lib/Predis/Cluster/RedisClusterHashStrategy.php b/lib/Predis/Cluster/RedisClusterHashStrategy.php index acdd235e..bb7ae82d 100644 --- a/lib/Predis/Cluster/RedisClusterHashStrategy.php +++ b/lib/Predis/Cluster/RedisClusterHashStrategy.php @@ -126,6 +126,7 @@ class RedisClusterHashStrategy implements CommandHashStrategyInterface 'ZSCAN' => $keyIsFirstArgument, 'ZLEXCOUNT' => $keyIsFirstArgument, 'ZRANGEBYLEX' => $keyIsFirstArgument, + 'ZREMRANGEBYLEX' => $keyIsFirstArgument, /* commands operating on hashes */ 'HDEL' => $keyIsFirstArgument, diff --git a/lib/Predis/Command/ZSetRemoveRangeByLex.php b/lib/Predis/Command/ZSetRemoveRangeByLex.php new file mode 100644 index 00000000..6999d5fa --- /dev/null +++ b/lib/Predis/Command/ZSetRemoveRangeByLex.php @@ -0,0 +1,27 @@ + + * + * 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 + */ +class ZSetRemoveRangeByLex extends PrefixableCommand +{ + /** + * {@inheritdoc} + */ + public function getId() + { + return 'ZREMRANGEBYLEX'; + } +} diff --git a/lib/Predis/Profile/ServerVersion28.php b/lib/Predis/Profile/ServerVersion28.php index bc523a22..2f36b3ae 100644 --- a/lib/Predis/Profile/ServerVersion28.php +++ b/lib/Predis/Profile/ServerVersion28.php @@ -241,6 +241,7 @@ class ServerVersion28 extends ServerProfile 'zscan' => 'Predis\Command\ZSetScan', 'zlexcount' => 'Predis\Command\ZSetLexCount', 'zrangebylex' => 'Predis\Command\ZSetRangeByLex', + 'zremrangebylex' => 'Predis\Command\ZSetRemoveRangeByLex', /* commands operating on hashes */ 'hscan' => 'Predis\Command\HashScan', diff --git a/tests/Predis/Cluster/PredisClusterHashStrategyTest.php b/tests/Predis/Cluster/PredisClusterHashStrategyTest.php index 38531926..57bc5571 100644 --- a/tests/Predis/Cluster/PredisClusterHashStrategyTest.php +++ b/tests/Predis/Cluster/PredisClusterHashStrategyTest.php @@ -344,6 +344,7 @@ class PredisClusterHashStrategyTest extends PredisTestCase 'ZSCAN' => 'keys-first', 'ZLEXCOUNT' => 'keys-first', 'ZRANGEBYLEX' => 'keys-first', + 'ZREMRANGEBYLEX' => 'keys-first', /* commands operating on hashes */ 'HDEL' => 'keys-first', diff --git a/tests/Predis/Cluster/RedisClusterHashStrategyTest.php b/tests/Predis/Cluster/RedisClusterHashStrategyTest.php index 2d5228e6..261d703e 100644 --- a/tests/Predis/Cluster/RedisClusterHashStrategyTest.php +++ b/tests/Predis/Cluster/RedisClusterHashStrategyTest.php @@ -344,6 +344,7 @@ class RedisClusterHashStrategyTest extends PredisTestCase 'ZSCAN' => 'keys-first', 'ZLEXCOUNT' => 'keys-first', 'ZRANGEBYLEX' => 'keys-first', + 'ZREMRANGEBYLEX' => 'keys-first', /* commands operating on hashes */ 'HDEL' => 'keys-first', diff --git a/tests/Predis/Command/ZSetRemoveRangeByLexTest.php b/tests/Predis/Command/ZSetRemoveRangeByLexTest.php new file mode 100644 index 00000000..34db06c1 --- /dev/null +++ b/tests/Predis/Command/ZSetRemoveRangeByLexTest.php @@ -0,0 +1,174 @@ + + * + * 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 disconnected + */ + public function testPrefixKeys() + { + $arguments = array('key', '[a', '[b'); + $expected = array('prefix:key', '[a', '[b'); + + $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 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\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\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'); + } +} diff --git a/tests/Predis/Profile/ServerVersion28Test.php b/tests/Predis/Profile/ServerVersion28Test.php index bae884f4..10d47f0e 100644 --- a/tests/Predis/Profile/ServerVersion28Test.php +++ b/tests/Predis/Profile/ServerVersion28Test.php @@ -178,10 +178,11 @@ class ServerVersion28Test extends PredisProfileTestCase 137 => 'zscan', 138 => 'zlexcount', 139 => 'zrangebylex', - 140 => 'hscan', - 141 => 'pfadd', - 142 => 'pfcount', - 143 => 'pfmerge', + 140 => 'zremrangebylex', + 141 => 'hscan', + 142 => 'pfadd', + 143 => 'pfcount', + 144 => 'pfmerge', ); } } diff --git a/tests/Predis/Profile/ServerVersionNextTest.php b/tests/Predis/Profile/ServerVersionNextTest.php index 076112f4..116ce29c 100644 --- a/tests/Predis/Profile/ServerVersionNextTest.php +++ b/tests/Predis/Profile/ServerVersionNextTest.php @@ -178,10 +178,11 @@ class ServerVersionNextTest extends PredisProfileTestCase 137 => 'zscan', 138 => 'zlexcount', 139 => 'zrangebylex', - 140 => 'hscan', - 141 => 'pfadd', - 142 => 'pfcount', - 143 => 'pfmerge', + 140 => 'zremrangebylex', + 141 => 'hscan', + 142 => 'pfadd', + 143 => 'pfcount', + 144 => 'pfmerge', ); } } diff --git a/tests/Predis/Replication/ReplicationStrategyTest.php b/tests/Predis/Replication/ReplicationStrategyTest.php index 270d180e..eb57bf00 100644 --- a/tests/Predis/Replication/ReplicationStrategyTest.php +++ b/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',