mirror of
https://github.com/predis/predis.git
synced 2026-09-13 20:07:28 +00:00
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.
This commit is contained in:
@@ -152,6 +152,7 @@ class PredisClusterHashStrategy implements CommandHashStrategyInterface
|
||||
'HSET' => $keyIsFirstArgument,
|
||||
'HSETNX' => $keyIsFirstArgument,
|
||||
'HVALS' => $keyIsFirstArgument,
|
||||
'HSCAN' => $keyIsFirstArgument,
|
||||
|
||||
/* scripting */
|
||||
'EVAL' => array($this, 'getKeyFromScriptingCommands'),
|
||||
|
||||
@@ -138,6 +138,7 @@ class RedisClusterHashStrategy implements CommandHashStrategyInterface
|
||||
'HSET' => $keyIsFirstArgument,
|
||||
'HSETNX' => $keyIsFirstArgument,
|
||||
'HVALS' => $keyIsFirstArgument,
|
||||
'HSCAN' => $keyIsFirstArgument,
|
||||
|
||||
/* scripting */
|
||||
'EVAL' => array($this, 'getKeyFromScriptingCommands'),
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
<?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/hscan
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,6 +207,7 @@ class ReplicationStrategy
|
||||
'HKEYS' => true,
|
||||
'HVALS' => true,
|
||||
'HGETALL' => true,
|
||||
'HSCAN' => true,
|
||||
'PING' => true,
|
||||
'AUTH' => true,
|
||||
'SELECT' => true,
|
||||
|
||||
@@ -357,6 +357,7 @@ class PredisClusterHashStrategyTest extends StandardTestCase
|
||||
'HSET' => 'keys-first',
|
||||
'HSETNX' => 'keys-first',
|
||||
'HVALS' => 'keys-first',
|
||||
'HSCAN' => 'keys-first',
|
||||
|
||||
/* scripting */
|
||||
'EVAL' => 'keys-script',
|
||||
|
||||
@@ -350,6 +350,7 @@ class RedisClusterHashStrategyTest extends StandardTestCase
|
||||
'HSET' => 'keys-first',
|
||||
'HSETNX' => 'keys-first',
|
||||
'HVALS' => 'keys-first',
|
||||
'HSCAN' => 'keys-first',
|
||||
|
||||
/* scripting */
|
||||
'EVAL' => 'keys-script',
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
<?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;
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
@@ -176,6 +176,7 @@ class ServerVersion28Test extends ServerVersionTestCase
|
||||
135 => 'scan',
|
||||
136 => 'sscan',
|
||||
137 => 'zscan',
|
||||
138 => 'hscan',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,6 +176,7 @@ class ServerVersionNextTest extends ServerVersionTestCase
|
||||
135 => 'scan',
|
||||
136 => 'sscan',
|
||||
137 => 'zscan',
|
||||
138 => 'hscan',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -360,6 +360,7 @@ class ReplicationStrategyTest extends StandardTestCase
|
||||
'HSET' => 'write',
|
||||
'HSETNX' => 'write',
|
||||
'HVALS' => 'read',
|
||||
'HSCAN' => 'read',
|
||||
|
||||
/* scripting */
|
||||
'EVAL' => 'write',
|
||||
|
||||
Reference in New Issue
Block a user