Browse Source

New command: SSCAN (Redis 2.8).

See http://redis.io/commands/scan for reference.
Daniele Alessandri 11 years ago
parent
commit
0faae20737

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

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

+ 76 - 0
lib/Predis/Command/SetScan.php

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

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

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

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

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

+ 158 - 0
tests/Predis/Command/SetScanTest.php

@@ -0,0 +1,158 @@
+<?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-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]);
+    }
+}

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

@@ -174,6 +174,7 @@ class ServerVersion28Test extends ServerVersionTestCase
             133 => 'script',
             134 => 'time',
             135 => 'scan',
+            136 => 'sscan',
         );
     }
 }

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

@@ -174,6 +174,7 @@ class ServerVersionNextTest extends ServerVersionTestCase
             133 => 'script',
             134 => 'time',
             135 => 'scan',
+            136 => 'sscan',
         );
     }
 }

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