diff --git a/lib/Predis/Cluster/PredisClusterHashStrategy.php b/lib/Predis/Cluster/PredisClusterHashStrategy.php index 9b9b3030..90b0d738 100644 --- a/lib/Predis/Cluster/PredisClusterHashStrategy.php +++ b/lib/Predis/Cluster/PredisClusterHashStrategy.php @@ -114,6 +114,7 @@ class PredisClusterHashStrategy implements CommandHashStrategyInterface 'SUNIONSTORE' => $keysAreAllArguments, 'SISMEMBER' => $keyIsFirstArgument, 'SMEMBERS' => $keyIsFirstArgument, + 'SSCAN' => $keyIsFirstArgument, 'SPOP' => $keyIsFirstArgument, 'SRANDMEMBER' => $keyIsFirstArgument, 'SREM' => $keyIsFirstArgument, diff --git a/lib/Predis/Cluster/RedisClusterHashStrategy.php b/lib/Predis/Cluster/RedisClusterHashStrategy.php index bec2bf59..5a680b0b 100644 --- a/lib/Predis/Cluster/RedisClusterHashStrategy.php +++ b/lib/Predis/Cluster/RedisClusterHashStrategy.php @@ -102,6 +102,7 @@ class RedisClusterHashStrategy implements CommandHashStrategyInterface 'SCARD' => $keyIsFirstArgument, 'SISMEMBER' => $keyIsFirstArgument, 'SMEMBERS' => $keyIsFirstArgument, + 'SSCAN' => $keyIsFirstArgument, 'SPOP' => $keyIsFirstArgument, 'SRANDMEMBER' => $keyIsFirstArgument, 'SREM' => $keyIsFirstArgument, diff --git a/lib/Predis/Command/SetScan.php b/lib/Predis/Command/SetScan.php new file mode 100644 index 00000000..9efa80b8 --- /dev/null +++ b/lib/Predis/Command/SetScan.php @@ -0,0 +1,76 @@ + + * + * 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/sscan + * @author Daniele Alessandri + */ +class SetScan extends PrefixableCommand +{ + /** + * {@inheritdoc} + */ + public function getId() + { + return 'SSCAN'; + } + + /** + * {@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]; + } + + return $data; + } +} diff --git a/lib/Predis/Profile/ServerVersion28.php b/lib/Predis/Profile/ServerVersion28.php index 2a1b2565..d0efa191 100644 --- a/lib/Predis/Profile/ServerVersion28.php +++ b/lib/Predis/Profile/ServerVersion28.php @@ -239,6 +239,9 @@ class ServerVersion28 extends ServerProfile /* commands operating on the key space */ 'scan' => 'Predis\Command\KeyScan', + + /* commands operating on sets */ + 'sscan' => 'Predis\Command\SetScan', ); } } diff --git a/lib/Predis/Replication/ReplicationStrategy.php b/lib/Predis/Replication/ReplicationStrategy.php index 4ea88f77..b4bd0f50 100644 --- a/lib/Predis/Replication/ReplicationStrategy.php +++ b/lib/Predis/Replication/ReplicationStrategy.php @@ -188,6 +188,7 @@ class ReplicationStrategy 'SUNION' => true, 'SDIFF' => true, 'SMEMBERS' => true, + 'SSCAN' => true, 'SRANDMEMBER' => true, 'ZRANGE' => true, 'ZREVRANGE' => true, diff --git a/tests/Predis/Cluster/PredisClusterHashStrategyTest.php b/tests/Predis/Cluster/PredisClusterHashStrategyTest.php index 2cf14db7..a6a86e03 100644 --- a/tests/Predis/Cluster/PredisClusterHashStrategyTest.php +++ b/tests/Predis/Cluster/PredisClusterHashStrategyTest.php @@ -319,6 +319,7 @@ class PredisClusterHashStrategyTest extends StandardTestCase 'SUNIONSTORE' => 'keys-all', 'SISMEMBER' => 'keys-first', 'SMEMBERS' => 'keys-first', + 'SSCAN' => 'keys-first', 'SPOP' => 'keys-first', 'SRANDMEMBER' => 'keys-first', 'SREM' => 'keys-first', diff --git a/tests/Predis/Cluster/RedisClusterHashStrategyTest.php b/tests/Predis/Cluster/RedisClusterHashStrategyTest.php index cf45cd97..aa45cda4 100644 --- a/tests/Predis/Cluster/RedisClusterHashStrategyTest.php +++ b/tests/Predis/Cluster/RedisClusterHashStrategyTest.php @@ -314,6 +314,7 @@ class RedisClusterHashStrategyTest extends StandardTestCase 'SCARD' => 'keys-first', 'SISMEMBER' => 'keys-first', 'SMEMBERS' => 'keys-first', + 'SSCAN' => 'keys-first', 'SPOP' => 'keys-first', 'SRANDMEMBER' => 'keys-first', 'SREM' => 'keys-first', diff --git a/tests/Predis/Command/SetScanTest.php b/tests/Predis/Command/SetScanTest.php new file mode 100644 index 00000000..6ef6eda7 --- /dev/null +++ b/tests/Predis/Command/SetScanTest.php @@ -0,0 +1,158 @@ + + * + * 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-set + */ +class SetScanTest extends CommandTestCase +{ + /** + * {@inheritdoc} + */ + protected function getExpectedCommand() + { + return 'Predis\Command\SetScan'; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedId() + { + return 'SSCAN'; + } + + /** + * @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', 'member:2', 'member:3')); + $expected = array(3, array('member:1', 'member:2', 'member:3')); + + $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() + { + $redis = $this->getClient(); + $redis->sadd('key', $members = array('member:one', 'member:two', 'member:three', 'member:four')); + + $response = $redis->sscan('key', 0); + + $this->assertSame(0, $response[0]); + $this->assertSameValues($members, $response[1]); + } + + /** + * @group connected + */ + public function testScanWithMatchingMembers() + { + $redis = $this->getClient(); + $redis->sadd('key', $members = array('member:one', 'member:two', 'member:three', 'member:four')); + + $response = $redis->sscan('key', 0, 'MATCH', 'member:t*'); + + $this->assertSameValues(array('member:two', 'member:three'), $response[1]); + } + + /** + * @group connected + */ + public function testScanWithNoMatchingMembers() + { + $redis = $this->getClient(); + $redis->sadd('key', $members = array('member:one', 'member:two', 'member:three', 'member:four')); + + $response = $redis->sscan('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 05641001..32a38d48 100644 --- a/tests/Predis/Profile/ServerVersion28Test.php +++ b/tests/Predis/Profile/ServerVersion28Test.php @@ -174,6 +174,7 @@ class ServerVersion28Test extends ServerVersionTestCase 133 => 'script', 134 => 'time', 135 => 'scan', + 136 => 'sscan', ); } } diff --git a/tests/Predis/Profile/ServerVersionNextTest.php b/tests/Predis/Profile/ServerVersionNextTest.php index a19835a6..3954ca85 100644 --- a/tests/Predis/Profile/ServerVersionNextTest.php +++ b/tests/Predis/Profile/ServerVersionNextTest.php @@ -174,6 +174,7 @@ class ServerVersionNextTest extends ServerVersionTestCase 133 => 'script', 134 => 'time', 135 => 'scan', + 136 => 'sscan', ); } } diff --git a/tests/Predis/Replication/ReplicationStrategyTest.php b/tests/Predis/Replication/ReplicationStrategyTest.php index 5d8c8727..46fdb716 100644 --- a/tests/Predis/Replication/ReplicationStrategyTest.php +++ b/tests/Predis/Replication/ReplicationStrategyTest.php @@ -322,8 +322,9 @@ class ReplicationStrategyTest extends StandardTestCase 'SCARD' => 'read', 'SISMEMBER' => 'read', 'SMEMBERS' => 'read', - 'SPOP' => 'write', + 'SSCAN' => 'read', 'SRANDMEMBER' => 'read', + 'SPOP' => 'write', 'SREM' => 'write', 'SINTER' => 'read', 'SUNION' => 'read',