Sfoglia il codice sorgente

New command: ZSCAN (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.
Daniele Alessandri 11 anni fa
parent
commit
d5aec78086

+ 1 - 0
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,

+ 1 - 0
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,

+ 85 - 0
lib/Predis/Command/ZSetScan.php

@@ -0,0 +1,85 @@
+<?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/zscan
+ * @author Daniele Alessandri <suppakilla@gmail.com>
+ */
+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;
+    }
+}

+ 3 - 0
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',
         );
     }
 }

+ 1 - 0
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,

+ 1 - 0
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',

+ 1 - 0
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',

+ 165 - 0
tests/Predis/Command/ZSetScanTest.php

@@ -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-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]);
+    }
+}

+ 1 - 0
tests/Predis/Profile/ServerVersion28Test.php

@@ -175,6 +175,7 @@ class ServerVersion28Test extends ServerVersionTestCase
             134 => 'time',
             135 => 'scan',
             136 => 'sscan',
+            137 => 'zscan',
         );
     }
 }

+ 1 - 0
tests/Predis/Profile/ServerVersionNextTest.php

@@ -175,6 +175,7 @@ class ServerVersionNextTest extends ServerVersionTestCase
             134 => 'time',
             135 => 'scan',
             136 => 'sscan',
+            137 => 'zscan',
         );
     }
 }

+ 1 - 0
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',