From d78e1b6ab7378b65d500a14d2d9f3d0f7091c168 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Sat, 2 Nov 2013 17:26:46 +0100 Subject: [PATCH] New command: HSCAN (Redis 2.8). One test is currently marked as skipped because it makes Redis crash when the specified MATCH pattern returns one or more elements. See http://redis.io/commands/scan for reference. --- .../Cluster/PredisClusterHashStrategy.php | 1 + .../Cluster/RedisClusterHashStrategy.php | 1 + lib/Predis/Command/HashScan.php | 84 +++++++++ lib/Predis/Profile/ServerVersion28.php | 3 + .../Replication/ReplicationStrategy.php | 1 + .../Cluster/PredisClusterHashStrategyTest.php | 1 + .../Cluster/RedisClusterHashStrategyTest.php | 1 + tests/Predis/Command/HashScanTest.php | 165 ++++++++++++++++++ tests/Predis/Profile/ServerVersion28Test.php | 1 + .../Predis/Profile/ServerVersionNextTest.php | 1 + .../Replication/ReplicationStrategyTest.php | 1 + 11 files changed, 260 insertions(+) create mode 100644 lib/Predis/Command/HashScan.php create mode 100644 tests/Predis/Command/HashScanTest.php diff --git a/lib/Predis/Cluster/PredisClusterHashStrategy.php b/lib/Predis/Cluster/PredisClusterHashStrategy.php index fca0b855..d2e72c35 100644 --- a/lib/Predis/Cluster/PredisClusterHashStrategy.php +++ b/lib/Predis/Cluster/PredisClusterHashStrategy.php @@ -152,6 +152,7 @@ class PredisClusterHashStrategy implements CommandHashStrategyInterface 'HSET' => $keyIsFirstArgument, 'HSETNX' => $keyIsFirstArgument, 'HVALS' => $keyIsFirstArgument, + 'HSCAN' => $keyIsFirstArgument, /* scripting */ 'EVAL' => array($this, 'getKeyFromScriptingCommands'), diff --git a/lib/Predis/Cluster/RedisClusterHashStrategy.php b/lib/Predis/Cluster/RedisClusterHashStrategy.php index aad1ecc5..dfee5cca 100644 --- a/lib/Predis/Cluster/RedisClusterHashStrategy.php +++ b/lib/Predis/Cluster/RedisClusterHashStrategy.php @@ -138,6 +138,7 @@ class RedisClusterHashStrategy implements CommandHashStrategyInterface 'HSET' => $keyIsFirstArgument, 'HSETNX' => $keyIsFirstArgument, 'HVALS' => $keyIsFirstArgument, + 'HSCAN' => $keyIsFirstArgument, /* scripting */ 'EVAL' => array($this, 'getKeyFromScriptingCommands'), diff --git a/lib/Predis/Command/HashScan.php b/lib/Predis/Command/HashScan.php new file mode 100644 index 00000000..0ca20886 --- /dev/null +++ b/lib/Predis/Command/HashScan.php @@ -0,0 +1,84 @@ + + * + * 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/hscan + * @author Daniele Alessandri + */ +class HashScan extends PrefixableCommand +{ + /** + * {@inheritdoc} + */ + public function getId() + { + return 'HSCAN'; + } + + /** + * {@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]; + $fields = $data[1]; + $result = array(); + + for ($i = 0; $i < count($fields); $i++) { + $result[$fields[$i]] = $fields[++$i]; + } + + $data[1] = $result; + } + + return $data; + } +} diff --git a/lib/Predis/Profile/ServerVersion28.php b/lib/Predis/Profile/ServerVersion28.php index d0f13666..f2474ee7 100644 --- a/lib/Predis/Profile/ServerVersion28.php +++ b/lib/Predis/Profile/ServerVersion28.php @@ -245,6 +245,9 @@ class ServerVersion28 extends ServerProfile /* commands operating on sorted sets */ 'zscan' => 'Predis\Command\ZSetScan', + + /* commands operating on hashes */ + 'hscan' => 'Predis\Command\HashScan', ); } } diff --git a/lib/Predis/Replication/ReplicationStrategy.php b/lib/Predis/Replication/ReplicationStrategy.php index e1a37ea7..c64f3e99 100644 --- a/lib/Predis/Replication/ReplicationStrategy.php +++ b/lib/Predis/Replication/ReplicationStrategy.php @@ -207,6 +207,7 @@ class ReplicationStrategy 'HKEYS' => true, 'HVALS' => true, 'HGETALL' => true, + 'HSCAN' => true, 'PING' => true, 'AUTH' => true, 'SELECT' => true, diff --git a/tests/Predis/Cluster/PredisClusterHashStrategyTest.php b/tests/Predis/Cluster/PredisClusterHashStrategyTest.php index 1ea3e1be..1d907459 100644 --- a/tests/Predis/Cluster/PredisClusterHashStrategyTest.php +++ b/tests/Predis/Cluster/PredisClusterHashStrategyTest.php @@ -357,6 +357,7 @@ class PredisClusterHashStrategyTest extends StandardTestCase 'HSET' => 'keys-first', 'HSETNX' => 'keys-first', 'HVALS' => 'keys-first', + 'HSCAN' => 'keys-first', /* scripting */ 'EVAL' => 'keys-script', diff --git a/tests/Predis/Cluster/RedisClusterHashStrategyTest.php b/tests/Predis/Cluster/RedisClusterHashStrategyTest.php index a0131799..dd89ac09 100644 --- a/tests/Predis/Cluster/RedisClusterHashStrategyTest.php +++ b/tests/Predis/Cluster/RedisClusterHashStrategyTest.php @@ -350,6 +350,7 @@ class RedisClusterHashStrategyTest extends StandardTestCase 'HSET' => 'keys-first', 'HSETNX' => 'keys-first', 'HVALS' => 'keys-first', + 'HSCAN' => 'keys-first', /* scripting */ 'EVAL' => 'keys-script', diff --git a/tests/Predis/Command/HashScanTest.php b/tests/Predis/Command/HashScanTest.php new file mode 100644 index 00000000..22b9d761 --- /dev/null +++ b/tests/Predis/Command/HashScanTest.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-hash + */ +class HashScanTest extends CommandTestCase +{ + /** + * {@inheritdoc} + */ + protected function getExpectedCommand() + { + return 'Predis\Command\HashScan'; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedId() + { + return 'HSCAN'; + } + + /** + * @group disconnected + */ + public function testFilterArguments() + { + $arguments = array('key', 0, 'MATCH', 'field:*', 'COUNT', 10); + $expected = array('key', 0, 'MATCH', 'field:*', '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' => 'field:*', 'count' => 10)); + $expected = array('key', 0, 'MATCH', 'field:*', 'COUNT', 10); + + $command = $this->getCommand(); + $command->setArguments($arguments); + + $this->assertSame($expected, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse() + { + $raw = array('3', array('field:1', '1', 'field:2', '2', 'field:3', '3')); + $expected = array(3, array('field:1' => '1', 'field:2' => '2', 'field:3' => '3')); + + $command = $this->getCommand(); + + $this->assertSame($expected, $command->parseResponse($raw)); + } + + /** + * @group disconnected + */ + public function testPrefixKeys() + { + $arguments = array('key', '0', 'MATCH', 'field:*', 'COUNT', 10); + $expected = array('prefix:key', '0', 'MATCH', 'field:*', '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() + { + $expectedFields = array('field:one', 'field:two', 'field:three', 'field:four'); + $expectedValues = array('one', 'two', 'three', 'four'); + + $redis = $this->getClient(); + $redis->hmset('key', array_combine($expectedFields, $expectedValues)); + + $response = $redis->hscan('key', 0); + + $this->assertSame(0, $response[0]); + $this->assertSame($expectedFields, array_keys($response[1])); + $this->assertSame($expectedValues, array_values($response[1])); + } + + /** + * @group connected + */ + public function testScanWithMatchingMembers() + { + $this->markTestSkipped('This test currently makes Redis crash.'); + + $redis = $this->getClient(); + $redis->hmset('key', array('field:one' => 'one', 'field:two' => 'two', 'field:three' => 'three', 'field:four' => 'four')); + + $response = $redis->hscan('key', 0, 'MATCH', 'field:t*'); + + $this->assertSame(array('field:two', 'field:three'), array_keys($response[1])); + $this->assertSame(array('two', 'three'), array_values($response[1])); + } + + /** + * @group connected + */ + public function testScanWithNoMatchingMembers() + { + $redis = $this->getClient(); + $redis->hmset('key', array('field:one' => 'one', 'field:two' => 'two', 'field:three' => 'three', 'field:four' => 'four')); + + $response = $redis->hscan('key', 0, 'MATCH', 'nofield:*'); + + $this->assertSame(0, $response[0]); + $this->assertEmpty($response[1]); + } +} diff --git a/tests/Predis/Profile/ServerVersion28Test.php b/tests/Predis/Profile/ServerVersion28Test.php index b13bf57a..42a012d9 100644 --- a/tests/Predis/Profile/ServerVersion28Test.php +++ b/tests/Predis/Profile/ServerVersion28Test.php @@ -176,6 +176,7 @@ class ServerVersion28Test extends ServerVersionTestCase 135 => 'scan', 136 => 'sscan', 137 => 'zscan', + 138 => 'hscan', ); } } diff --git a/tests/Predis/Profile/ServerVersionNextTest.php b/tests/Predis/Profile/ServerVersionNextTest.php index 12864bd1..77e1e2a1 100644 --- a/tests/Predis/Profile/ServerVersionNextTest.php +++ b/tests/Predis/Profile/ServerVersionNextTest.php @@ -176,6 +176,7 @@ class ServerVersionNextTest extends ServerVersionTestCase 135 => 'scan', 136 => 'sscan', 137 => 'zscan', + 138 => 'hscan', ); } } diff --git a/tests/Predis/Replication/ReplicationStrategyTest.php b/tests/Predis/Replication/ReplicationStrategyTest.php index 6b9be633..d34e29b6 100644 --- a/tests/Predis/Replication/ReplicationStrategyTest.php +++ b/tests/Predis/Replication/ReplicationStrategyTest.php @@ -360,6 +360,7 @@ class ReplicationStrategyTest extends StandardTestCase 'HSET' => 'write', 'HSETNX' => 'write', 'HVALS' => 'read', + 'HSCAN' => 'read', /* scripting */ 'EVAL' => 'write',