Kaynağa Gözat

Rename command hash strategy cass used by Predis cluster.

Daniele Alessandri 12 yıl önce
ebeveyn
işleme
d0572b0efc

+ 11 - 10
lib/Predis/Command/Hash/CommandHashStrategy.php → lib/Predis/Command/Hash/PredisClusterHashStrategy.php

@@ -20,7 +20,7 @@ use Predis\Distribution\HashGeneratorInterface;
  *
  * @author Daniele Alessandri <suppakilla@gmail.com>
  */
-class CommandHashStrategy implements CommandHashStrategyInterface
+class PredisClusterHashStrategy implements CommandHashStrategyInterface
 {
     private $commands;
 
@@ -40,11 +40,12 @@ class CommandHashStrategy implements CommandHashStrategyInterface
     protected function getDefaultCommands()
     {
         $keyIsFirstArgument = array($this, 'getKeyFromFirstArgument');
+        $keysAreAllArguments = array($this, 'getKeyFromAllArguments');
 
         return array(
             /* commands operating on the key space */
             'EXISTS'                => $keyIsFirstArgument,
-            'DEL'                   => array($this, 'getKeyFromAllArguments'),
+            'DEL'                   => $keysAreAllArguments,
             'TYPE'                  => $keyIsFirstArgument,
             'EXPIRE'                => $keyIsFirstArgument,
             'EXPIREAT'              => $keyIsFirstArgument,
@@ -61,7 +62,7 @@ class CommandHashStrategy implements CommandHashStrategyInterface
             'DECRBY'                => $keyIsFirstArgument,
             'GET'                   => $keyIsFirstArgument,
             'GETBIT'                => $keyIsFirstArgument,
-            'MGET'                  => array($this, 'getKeyFromAllArguments'),
+            'MGET'                  => $keysAreAllArguments,
             'SET'                   => $keyIsFirstArgument,
             'GETRANGE'              => $keyIsFirstArgument,
             'GETSET'                => $keyIsFirstArgument,
@@ -84,7 +85,7 @@ class CommandHashStrategy implements CommandHashStrategyInterface
             'LLEN'                  => $keyIsFirstArgument,
             'LPOP'                  => $keyIsFirstArgument,
             'RPOP'                  => $keyIsFirstArgument,
-            'RPOPLPUSH'             => array($this, 'getKeyFromAllArguments'),
+            'RPOPLPUSH'             => $keysAreAllArguments,
             'BLPOP'                 => array($this, 'getKeyFromBlockingListCommands'),
             'BRPOP'                 => array($this, 'getKeyFromBlockingListCommands'),
             'BRPOPLPUSH'            => array($this, 'getKeyFromBlockingListCommands'),
@@ -100,12 +101,12 @@ class CommandHashStrategy implements CommandHashStrategyInterface
             /* commands operating on sets */
             'SADD'                  => $keyIsFirstArgument,
             'SCARD'                 => $keyIsFirstArgument,
-            'SDIFF'                 => array($this, 'getKeyFromAllArguments'),
-            'SDIFFSTORE'            => array($this, 'getKeyFromAllArguments'),
-            'SINTER'                => array($this, 'getKeyFromAllArguments'),
-            'SINTERSTORE'           => array($this, 'getKeyFromAllArguments'),
-            'SUNION'                => array($this, 'getKeyFromAllArguments'),
-            'SUNIONSTORE'           => array($this, 'getKeyFromAllArguments'),
+            'SDIFF'                 => $keysAreAllArguments,
+            'SDIFFSTORE'            => $keysAreAllArguments,
+            'SINTER'                => $keysAreAllArguments,
+            'SINTERSTORE'           => $keysAreAllArguments,
+            'SUNION'                => $keysAreAllArguments,
+            'SUNIONSTORE'           => $keysAreAllArguments,
             'SISMEMBER'             => $keyIsFirstArgument,
             'SMEMBERS'              => $keyIsFirstArgument,
             'SPOP'                  => $keyIsFirstArgument,

+ 2 - 3
lib/Predis/Connection/PredisCluster.php

@@ -14,7 +14,7 @@ namespace Predis\Connection;
 use Predis\ClientException;
 use Predis\NotSupportedException;
 use Predis\Command\CommandInterface;
-use Predis\Command\Hash\CommandHashStrategy;
+use Predis\Command\Hash\PredisClusterHashStrategy;
 use Predis\Distribution\HashRing;
 use Predis\Distribution\DistributionStrategyInterface;
 
@@ -37,7 +37,7 @@ class PredisCluster implements ClusterConnectionInterface, \IteratorAggregate, \
     public function __construct(DistributionStrategyInterface $distributor = null)
     {
         $this->pool = array();
-        $this->cmdHasher = new CommandHashStrategy();
+        $this->cmdHasher = new PredisClusterHashStrategy();
         $this->distributor = $distributor ?: new HashRing();
     }
 
@@ -151,7 +151,6 @@ class PredisCluster implements ClusterConnectionInterface, \IteratorAggregate, \
         return isset($this->pool[$alias]) ? $this->pool[$alias] : null;
     }
 
-
     /**
      * Retrieves a connection instance from the cluster using a key.
      *

+ 12 - 12
tests/Predis/Command/Hash/CommandHashStrategyTest.php → tests/Predis/Command/Hash/PredisClusterHashStrategyTest.php

@@ -19,7 +19,7 @@ use Predis\Profile\ServerProfile;
 /**
  *
  */
-class CommandHashStrategyTest extends StandardTestCase
+class PredisClusterHashStrategyTest extends StandardTestCase
 {
     /**
      * @group disconnected
@@ -28,7 +28,7 @@ class CommandHashStrategyTest extends StandardTestCase
     {
         $expected = -1938594527;
         $distribution = new HashRing();
-        $hashstrategy = new CommandHashStrategy();
+        $hashstrategy = new PredisClusterHashStrategy();
 
         $this->assertSame($expected, $hashstrategy->getKeyHash($distribution, '{foo}'));
         $this->assertSame($expected, $hashstrategy->getKeyHash($distribution, '{foo}:bar'));
@@ -44,7 +44,7 @@ class CommandHashStrategyTest extends StandardTestCase
      */
     public function testSupportedCommands()
     {
-        $hashstrategy = new CommandHashStrategy();
+        $hashstrategy = new PredisClusterHashStrategy();
 
         $this->assertSame($this->getExpectedCommands(), $hashstrategy->getSupportedCommands());
     }
@@ -55,7 +55,7 @@ class CommandHashStrategyTest extends StandardTestCase
     public function testReturnsNullOnUnsupportedCommand()
     {
         $distribution = new HashRing();
-        $hashstrategy = new CommandHashStrategy();
+        $hashstrategy = new PredisClusterHashStrategy();
         $command = ServerProfile::getDevelopment()->createCommand('ping');
 
         $this->assertNull($hashstrategy->getHash($distribution, $command));
@@ -67,7 +67,7 @@ class CommandHashStrategyTest extends StandardTestCase
     public function testFirstKeyCommands()
     {
         $distribution = new HashRing();
-        $hashstrategy = new CommandHashStrategy();
+        $hashstrategy = new PredisClusterHashStrategy();
         $profile = ServerProfile::getDevelopment();
         $arguments = array('key');
 
@@ -83,7 +83,7 @@ class CommandHashStrategyTest extends StandardTestCase
     public function testAllKeysCommands()
     {
         $distribution = new HashRing();
-        $hashstrategy = new CommandHashStrategy();
+        $hashstrategy = new PredisClusterHashStrategy();
         $profile = ServerProfile::getDevelopment();
         $arguments = array('{key}:1', '{key}:2', '{key}:3', '{key}:4');
 
@@ -99,7 +99,7 @@ class CommandHashStrategyTest extends StandardTestCase
     public function testInterleavedKeysCommands()
     {
         $distribution = new HashRing();
-        $hashstrategy = new CommandHashStrategy();
+        $hashstrategy = new PredisClusterHashStrategy();
         $profile = ServerProfile::getDevelopment();
         $arguments = array('{key}:1', 'value1', '{key}:2', 'value2');
 
@@ -115,7 +115,7 @@ class CommandHashStrategyTest extends StandardTestCase
     public function testKeysForBlockingListCommands()
     {
         $distribution = new HashRing();
-        $hashstrategy = new CommandHashStrategy();
+        $hashstrategy = new PredisClusterHashStrategy();
         $profile = ServerProfile::getDevelopment();
         $arguments = array('{key}:1', '{key}:2', 10);
 
@@ -131,7 +131,7 @@ class CommandHashStrategyTest extends StandardTestCase
     public function testKeysForZsetAggregationCommands()
     {
         $distribution = new HashRing();
-        $hashstrategy = new CommandHashStrategy();
+        $hashstrategy = new PredisClusterHashStrategy();
         $profile = ServerProfile::getDevelopment();
         $arguments = array('{key}:destination', 2, '{key}:1', '{key}:1', array('aggregate' => 'SUM'));
 
@@ -147,7 +147,7 @@ class CommandHashStrategyTest extends StandardTestCase
     public function testKeysForBitOpCommand()
     {
         $distribution = new HashRing();
-        $hashstrategy = new CommandHashStrategy();
+        $hashstrategy = new PredisClusterHashStrategy();
         $profile = ServerProfile::getDevelopment();
         $arguments = array('AND', '{key}:destination', '{key}:src:1', '{key}:src:2');
 
@@ -163,7 +163,7 @@ class CommandHashStrategyTest extends StandardTestCase
     public function testUnsettingCommandHandler()
     {
         $distribution = new HashRing();
-        $hashstrategy = new CommandHashStrategy();
+        $hashstrategy = new PredisClusterHashStrategy();
         $profile = ServerProfile::getDevelopment();
 
         $hashstrategy->setCommandHandler('set');
@@ -182,7 +182,7 @@ class CommandHashStrategyTest extends StandardTestCase
     public function testSettingCustomCommandHandler()
     {
         $distribution = new HashRing();
-        $hashstrategy = new CommandHashStrategy();
+        $hashstrategy = new PredisClusterHashStrategy();
         $profile = ServerProfile::getDevelopment();
 
         $callable = $this->getMock('stdClass', array('__invoke'));

+ 1 - 1
tests/Predis/Connection/PredisClusterTest.php

@@ -26,7 +26,7 @@ class PredisClusterTest extends StandardTestCase
     public function testExposesCommandHashStrategy()
     {
         $cluster = new PredisCluster();
-        $this->assertInstanceOf('Predis\Command\Hash\CommandHashStrategy', $cluster->getCommandHashStrategy());
+        $this->assertInstanceOf('Predis\Command\Hash\PredisClusterHashStrategy', $cluster->getCommandHashStrategy());
     }
 
     /**