فهرست منبع

Check that STORE key in SORT ends up in same slot.

Daniele Alessandri 8 سال پیش
والد
کامیت
aeb9b7ccae
3فایلهای تغییر یافته به همراه67 افزوده شده و 2 حذف شده
  1. 30 1
      src/Cluster/ClusterStrategy.php
  2. 19 1
      tests/Predis/Cluster/PredisStrategyTest.php
  3. 18 0
      tests/Predis/Cluster/RedisStrategyTest.php

+ 30 - 1
src/Cluster/ClusterStrategy.php

@@ -53,7 +53,7 @@ abstract class ClusterStrategy implements StrategyInterface
             'PEXPIREAT' => $getKeyFromFirstArgument,
             'TTL' => $getKeyFromFirstArgument,
             'PTTL' => $getKeyFromFirstArgument,
-            'SORT' => $getKeyFromFirstArgument, // TODO
+            'SORT' => array($this, 'getKeyFromSortCommand'),
             'DUMP' => $getKeyFromFirstArgument,
             'RESTORE' => $getKeyFromFirstArgument,
 
@@ -261,6 +261,35 @@ abstract class ClusterStrategy implements StrategyInterface
         }
     }
 
+    /**
+     * Extracts the key from SORT command.
+     *
+     * @param CommandInterface $command Command instance.
+     *
+     * @return string|null
+     */
+    protected function getKeyFromSortCommand(CommandInterface $command)
+    {
+        $arguments = $command->getArguments();
+        $firstKey = $arguments[0];
+
+        if (1 === $argc = count($arguments)) {
+            return $firstKey;
+        }
+
+        $keys = array($firstKey);
+
+        for ($i = 1; $i < $argc; $i++) {
+            if (strtoupper($arguments[$i]) === 'STORE') {
+                $keys[] = $arguments[++$i];
+            }
+        }
+
+        if ($this->checkSameSlotForKeys($keys)) {
+            return $firstKey;
+        }
+    }
+
     /**
      * Extracts the key from BLPOP and BRPOP commands.
      *

+ 19 - 1
tests/Predis/Cluster/PredisStrategyTest.php

@@ -107,6 +107,24 @@ class PredisStrategyTest extends PredisTestCase
         }
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testKeysForSortCommand()
+    {
+        $strategy = $this->getClusterStrategy();
+        $profile = Profile\Factory::getDevelopment();
+        $arguments = array('{key}:1', 'value1', '{key}:2', 'value2');
+
+        $commandID = 'SORT';
+
+        $command = $profile->createCommand($commandID, array('{key}:1'));
+        $this->assertNotNull($strategy->getSlot($command), $commandID);
+
+        $command = $profile->createCommand($commandID, array('{key}:1', array('STORE' => '{key}:2')));
+        $this->assertNotNull($strategy->getSlot($command), $commandID);
+    }
+
     /**
      * @group disconnected
      */
@@ -265,7 +283,7 @@ class PredisStrategyTest extends PredisTestCase
             'PEXPIREAT' => 'keys-first',
             'TTL' => 'keys-first',
             'PTTL' => 'keys-first',
-            'SORT' => 'keys-first', // TODO
+            'SORT' => 'variable',
             'DUMP' => 'keys-first',
             'RESTORE' => 'keys-first',
 

+ 18 - 0
tests/Predis/Cluster/RedisStrategyTest.php

@@ -135,6 +135,24 @@ class RedisStrategyTest extends PredisTestCase
         }
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testKeysForSortCommand()
+    {
+        $strategy = $this->getClusterStrategy();
+        $profile = Profile\Factory::getDevelopment();
+        $arguments = array('{key}:1', 'value1', '{key}:2', 'value2');
+
+        $commandID = 'SORT';
+
+        $command = $profile->createCommand($commandID, array('{key}:1'));
+        $this->assertNotNull($strategy->getSlot($command), $commandID);
+
+        $command = $profile->createCommand($commandID, array('{key}:1', array('STORE' => '{key}:2')));
+        $this->assertNotNull($strategy->getSlot($command), $commandID);
+    }
+
     /**
      * @group disconnected
      */