diff --git a/lib/Predis/Cluster/PredisClusterHashStrategy.php b/lib/Predis/Cluster/PredisClusterHashStrategy.php index 90b0d738..fca0b855 100644 --- a/lib/Predis/Cluster/PredisClusterHashStrategy.php +++ b/lib/Predis/Cluster/PredisClusterHashStrategy.php @@ -136,6 +136,7 @@ class PredisClusterHashStrategy implements CommandHashStrategyInterface 'ZREVRANK' => $keyIsFirstArgument, 'ZSCORE' => $keyIsFirstArgument, 'ZUNIONSTORE' => array($this, 'getKeyFromZsetAggregationCommands'), + 'ZSCAN' => $keyIsFirstArgument, /* commands operating on hashes */ 'HDEL' => $keyIsFirstArgument, diff --git a/lib/Predis/Cluster/RedisClusterHashStrategy.php b/lib/Predis/Cluster/RedisClusterHashStrategy.php index 5a680b0b..aad1ecc5 100644 --- a/lib/Predis/Cluster/RedisClusterHashStrategy.php +++ b/lib/Predis/Cluster/RedisClusterHashStrategy.php @@ -122,6 +122,7 @@ class RedisClusterHashStrategy implements CommandHashStrategyInterface 'ZREVRANGEBYSCORE' => $keyIsFirstArgument, 'ZREVRANK' => $keyIsFirstArgument, 'ZSCORE' => $keyIsFirstArgument, + 'ZSCAN' => $keyIsFirstArgument, /* commands operating on hashes */ 'HDEL' => $keyIsFirstArgument, diff --git a/lib/Predis/Command/ZSetScan.php b/lib/Predis/Command/ZSetScan.php new file mode 100644 index 00000000..fad09c15 --- /dev/null +++ b/lib/Predis/Command/ZSetScan.php @@ -0,0 +1,85 @@ + + * + * 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/zscan + * @author Daniele Alessandri + */ +class ZSetScan extends PrefixableCommand +{ + /** + * {@inheritdoc} + */ + public function getId() + { + return 'ZSCAN'; + } + + /** + * {@inheritdoc} + */ + protected function filterArguments(Array $arguments) + { + if (count($arguments) === 3 && is_array($arguments[2])) { + $options = $this->prepareOptions(array_pop($arguments)); + $arguments = array_merge($arguments, $options); + } + + return $arguments; + } + + /** + * Returns a list of options and modifiers compatible with Redis. + * + * @param array $options List of options. + * @return array + */ + protected function prepareOptions($options) + { + $options = array_change_key_case($options, CASE_UPPER); + $normalized = array(); + + if (!empty($options['MATCH'])) { + $normalized[] = 'MATCH'; + $normalized[] = $options['MATCH']; + } + + if (!empty($options['COUNT'])) { + $normalized[] = 'COUNT'; + $normalized[] = $options['COUNT']; + } + + return $normalized; + } + + /** + * {@inheritdoc} + */ + public function parseResponse($data) + { + if (is_array($data)) { + $data[0] = (int) $data[0]; + + $members = $data[1]; + $result = array(); + + for ($i = 0; $i < count($members); $i++) { + $result[] = array($members[$i], (float) $members[++$i]); + } + + $data[1] = $result; + } + + return $data; + } +} diff --git a/lib/Predis/Profile/ServerVersion28.php b/lib/Predis/Profile/ServerVersion28.php index d0efa191..d0f13666 100644 --- a/lib/Predis/Profile/ServerVersion28.php +++ b/lib/Predis/Profile/ServerVersion28.php @@ -242,6 +242,9 @@ class ServerVersion28 extends ServerProfile /* commands operating on sets */ 'sscan' => 'Predis\Command\SetScan', + + /* commands operating on sorted sets */ + 'zscan' => 'Predis\Command\ZSetScan', ); } } diff --git a/lib/Predis/Replication/ReplicationStrategy.php b/lib/Predis/Replication/ReplicationStrategy.php index b4bd0f50..e1a37ea7 100644 --- a/lib/Predis/Replication/ReplicationStrategy.php +++ b/lib/Predis/Replication/ReplicationStrategy.php @@ -199,6 +199,7 @@ class ReplicationStrategy 'ZCOUNT' => true, 'ZRANK' => true, 'ZREVRANK' => true, + 'ZSCAN' => true, 'HGET' => true, 'HMGET' => true, 'HEXISTS' => true, diff --git a/tests/Predis/Cluster/PredisClusterHashStrategyTest.php b/tests/Predis/Cluster/PredisClusterHashStrategyTest.php index a6a86e03..1ea3e1be 100644 --- a/tests/Predis/Cluster/PredisClusterHashStrategyTest.php +++ b/tests/Predis/Cluster/PredisClusterHashStrategyTest.php @@ -341,6 +341,7 @@ class PredisClusterHashStrategyTest extends StandardTestCase 'ZREVRANK' => 'keys-first', 'ZSCORE' => 'keys-first', 'ZUNIONSTORE' => 'keys-zaggregated', + 'ZSCAN' => 'keys-first', /* commands operating on hashes */ 'HDEL' => 'keys-first', diff --git a/tests/Predis/Cluster/RedisClusterHashStrategyTest.php b/tests/Predis/Cluster/RedisClusterHashStrategyTest.php index aa45cda4..a0131799 100644 --- a/tests/Predis/Cluster/RedisClusterHashStrategyTest.php +++ b/tests/Predis/Cluster/RedisClusterHashStrategyTest.php @@ -334,6 +334,7 @@ class RedisClusterHashStrategyTest extends StandardTestCase 'ZREVRANGEBYSCORE' => 'keys-first', 'ZREVRANK' => 'keys-first', 'ZSCORE' => 'keys-first', + 'ZSCAN' => 'keys-first', /* commands operating on hashes */ 'HDEL' => 'keys-first', diff --git a/tests/Predis/Command/ZSetScanTest.php b/tests/Predis/Command/ZSetScanTest.php new file mode 100644 index 00000000..a227c162 --- /dev/null +++ b/tests/Predis/Command/ZSetScanTest.php @@ -0,0 +1,165 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Predis\Command; + +use \PHPUnit_Framework_TestCase as StandardTestCase; + +/** + * @group commands + * @group realm-zset + */ +class ZSetScanTest extends CommandTestCase +{ + /** + * {@inheritdoc} + */ + protected function getExpectedCommand() + { + return 'Predis\Command\ZSetScan'; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedId() + { + return 'ZSCAN'; + } + + /** + * @group disconnected + */ + public function testFilterArguments() + { + $arguments = array('key', 0, 'MATCH', 'member:*', 'COUNT', 10); + $expected = array('key', 0, 'MATCH', 'member:*', 'COUNT', 10); + + $command = $this->getCommand(); + $command->setArguments($arguments); + + $this->assertSame($expected, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testFilterArgumentsBasicUsage() + { + $arguments = array('key', 0); + $expected = array('key', 0); + + $command = $this->getCommand(); + $command->setArguments($arguments); + + $this->assertSame($expected, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testFilterArgumentsWithOptionsArray() + { + $arguments = array('key', 0, array('match' => 'member:*', 'count' => 10)); + $expected = array('key', 0, 'MATCH', 'member:*', 'COUNT', 10); + + $command = $this->getCommand(); + $command->setArguments($arguments); + + $this->assertSame($expected, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse() + { + $raw = array('3', array('member:1', '1', 'member:2', '2', 'member:3', '3')); + $expected = array(3, array(array('member:1' , 1.0), array('member:2', 2.0), array('member:3' , 3.0))); + + $command = $this->getCommand(); + + $this->assertSame($expected, $command->parseResponse($raw)); + } + + /** + * @group disconnected + */ + public function testPrefixKeys() + { + $arguments = array('key', '0', 'MATCH', 'member:*', 'COUNT', 10); + $expected = array('prefix:key', '0', 'MATCH', 'member:*', 'COUNT', 10); + + $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 testScanWithoutMatch() + { + $expectedMembers = array('member:one', 'member:two', 'member:three', 'member:four'); + $expectedScores = array(1.0, 2.0, 3.0, 4.0); + + $redis = $this->getClient(); + $redis->zadd('key', array_combine($expectedMembers, $expectedScores)); + + $response = $redis->zscan('key', 0); + + $this->assertSame(0, $response[0]); + $this->assertSame($expectedMembers, array_map(function($e) { return $e[0]; }, $response[1])); + $this->assertSame($expectedScores, array_map(function($e) { return $e[1]; }, $response[1])); + } + + /** + * @group connected + */ + public function testScanWithMatchingMembers() + { + $this->markTestSkipped('This test currently makes Redis crash.'); + + $redis = $this->getClient(); + $redis->hmset('key', array('member:one' => 1, 'member:two' => 2, 'member:three' => 3, 'member:four' => 4)); + + $response = $redis->hscan('key', 0, 'MATCH', 'member:t*'); + + $this->assertSame(array('member:two', 'member:three'), array_keys($response[1])); + $this->assertSame(array(2.0, 3.0), array_values($response[1])); + } + + /** + * @group connected + */ + public function testScanWithNoMatchingMembers() + { + $redis = $this->getClient(); + $redis->zadd('key', $members = array('member:one' => 1, 'member:two' => 2, 'member:three' => 3, 'member:four' => 4)); + + $response = $redis->zscan('key', 0, 'MATCH', 'nomember:*'); + + $this->assertSame(0, $response[0]); + $this->assertEmpty($response[1]); + } +} diff --git a/tests/Predis/Profile/ServerVersion28Test.php b/tests/Predis/Profile/ServerVersion28Test.php index 32a38d48..b13bf57a 100644 --- a/tests/Predis/Profile/ServerVersion28Test.php +++ b/tests/Predis/Profile/ServerVersion28Test.php @@ -175,6 +175,7 @@ class ServerVersion28Test extends ServerVersionTestCase 134 => 'time', 135 => 'scan', 136 => 'sscan', + 137 => 'zscan', ); } } diff --git a/tests/Predis/Profile/ServerVersionNextTest.php b/tests/Predis/Profile/ServerVersionNextTest.php index 3954ca85..12864bd1 100644 --- a/tests/Predis/Profile/ServerVersionNextTest.php +++ b/tests/Predis/Profile/ServerVersionNextTest.php @@ -175,6 +175,7 @@ class ServerVersionNextTest extends ServerVersionTestCase 134 => 'time', 135 => 'scan', 136 => 'sscan', + 137 => 'zscan', ); } } diff --git a/tests/Predis/Replication/ReplicationStrategyTest.php b/tests/Predis/Replication/ReplicationStrategyTest.php index 46fdb716..6b9be633 100644 --- a/tests/Predis/Replication/ReplicationStrategyTest.php +++ b/tests/Predis/Replication/ReplicationStrategyTest.php @@ -345,6 +345,7 @@ class ReplicationStrategyTest extends StandardTestCase 'ZREVRANGEBYSCORE' => 'read', 'ZREVRANK' => 'read', 'ZSCORE' => 'read', + 'ZSCAN' => 'read', /* commands operating on hashes */ 'HDEL' => 'write',