ソースを参照

Allow passing an array for the list of source keys to be used in ZUNIONSTORE/ZINTERSTORE.

Daniele Alessandri 14 年 前
コミット
06d974c0bc
3 ファイル変更46 行追加4 行削除
  1. 3 0
      CHANGELOG
  2. 9 4
      lib/Predis.php
  3. 34 0
      test/RedisCommandsTest.php

+ 3 - 0
CHANGELOG

@@ -1,6 +1,9 @@
 v0.6.6 (2011-xx-xx)
   * Add support for connecting to Redis using UNIX domain sockets (ISSUE #25).
 
+  * ZUNIONSTORE and ZINTERSTORE can accept an array to specify a list of the 
+    source keys to be used to populate the destination key.
+
 v0.6.5 (2011-02-12)
   * FIX: due to an untested internal change introduced in v0.6.4, a wrong 
     handling of bulk reads of zero-length values was producing protocol 

+ 9 - 4
lib/Predis.php

@@ -2695,12 +2695,17 @@ class ZSetUnionStore extends \Predis\MultiBulkCommand {
     public function getCommandId() { return 'ZUNIONSTORE'; }
     public function filterArguments(Array $arguments) {
         $options = array();
-        $argc    = count($arguments);
-        if ($argc > 1 && is_array($arguments[$argc - 1])) {
+        $argc = count($arguments);
+        if ($argc > 2 && is_array($arguments[$argc - 1])) {
             $options = $this->prepareOptions(array_pop($arguments));
         }
-        $args = is_array($arguments[0]) ? $arguments[0] : $arguments;
-        return  array_merge($args, $options);
+        if (is_array($arguments[1])) {
+            $arguments = array_merge(
+                array($arguments[0], count($arguments[1])),
+                $arguments[1]
+            );
+        }
+        return array_merge($arguments, $options);
     }
     private function prepareOptions($options) {
         $opts = array_change_key_case($options, CASE_UPPER);

+ 34 - 0
test/RedisCommandsTest.php

@@ -1507,6 +1507,23 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
             $this->redis->zrange('zsetc', 0, -1, 'withscores')
         );
 
+        // using an array to pass the list of source keys
+        $sourceKeys = array('zseta', 'zsetb');
+
+        $this->assertEquals(4, $this->redis->zunionstore('zsetc', $sourceKeys));
+        $this->assertEquals(
+            array(array('a', 1), array('b', 3), array('d', 3), array('c', 5)),
+            $this->redis->zrange('zsetc', 0, -1, 'withscores')
+        );
+
+        // using an array to pass the list of source keys + options array
+        $options = array('weights' => array(2, 3));
+        $this->assertEquals(4, $this->redis->zunionstore('zsetc', $sourceKeys, $options));
+        $this->assertEquals(
+            array(array('a', 2), array('b', 7), array('d', 9), array('c', 12)),
+            $this->redis->zrange('zsetc', 0, -1, 'withscores')
+        );
+
         RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
             $test->redis->set('zsetFake', 'fake');
             $test->redis->zunionstore('zsetc', 2, 'zseta', 'zsetFake');
@@ -1552,6 +1569,23 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
             $this->redis->zrange('zsetc', 0, -1, 'withscores')
         );
 
+        // using an array to pass the list of source keys
+        $sourceKeys = array('zseta', 'zsetb');
+
+        $this->assertEquals(2, $this->redis->zinterstore('zsetc', $sourceKeys));
+        $this->assertEquals(
+            array(array('b', 3), array('c', 5)),
+            $this->redis->zrange('zsetc', 0, -1, 'withscores')
+        );
+
+        // using an array to pass the list of source keys + options array
+        $options = array('weights' => array(2, 3));
+        $this->assertEquals(2, $this->redis->zinterstore('zsetc', $sourceKeys, $options));
+        $this->assertEquals(
+            array(array('b', 7), array('c', 12)),
+            $this->redis->zrange('zsetc', 0, -1, 'withscores')
+        );
+
         RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
             $test->redis->set('zsetFake', 'fake');
             $test->redis->zinterstore('zsetc', 2, 'zseta', 'zsetFake');