Quellcode durchsuchen

Add new command: GEOADD (Redis 3.2.0).

Daniele Alessandri vor 8 Jahren
Ursprung
Commit
dd0edf761a

+ 1 - 0
src/ClientContextInterface.php

@@ -157,6 +157,7 @@ use Predis\Command\CommandInterface;
  * @method $this slowlog($subcommand, $argument = null)
  * @method $this time()
  * @method $this command()
+ * @method $this geoadd($key, $longitude, $latitude, $member)
  *
  * @author Daniele Alessandri <suppakilla@gmail.com>
  */

+ 1 - 0
src/ClientInterface.php

@@ -165,6 +165,7 @@ use Predis\Profile\ProfileInterface;
  * @method mixed  slowlog($subcommand, $argument = null)
  * @method array  time()
  * @method array  command()
+ * @method int    geoadd($key, $longitude, $latitude, $member)
  *
  * @author Daniele Alessandri <suppakilla@gmail.com>
  */

+ 3 - 0
src/Cluster/ClusterStrategy.php

@@ -165,6 +165,9 @@ abstract class ClusterStrategy implements StrategyInterface
             /* scripting */
             'EVAL' => array($this, 'getKeyFromScriptingCommands'),
             'EVALSHA' => array($this, 'getKeyFromScriptingCommands'),
+
+            /* commands performing geospatial operations */
+            'GEOADD' => $getKeyFromFirstArgument,
         );
     }
 

+ 42 - 0
src/Command/GeospatialGeoAdd.php

@@ -0,0 +1,42 @@
+<?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/geoadd
+ *
+ * @author Daniele Alessandri <suppakilla@gmail.com>
+ */
+class GeospatialGeoAdd extends Command
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function getId()
+    {
+        return 'GEOADD';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function filterArguments(array $arguments)
+    {
+        if (count($arguments) === 2 && is_array($arguments[1])) {
+            foreach (array_pop($arguments) as $item) {
+                $arguments = array_merge($arguments, $item);
+            }
+        }
+
+        return $arguments;
+    }
+}

+ 1 - 0
src/Command/Processor/KeyPrefixProcessor.php

@@ -160,6 +160,7 @@ class KeyPrefixProcessor implements ProcessorInterface
             /* ---------------- Redis 3.2 ---------------- */
             'HSTRLEN' => 'static::first',
             'BITFIELD' => 'static::first',
+            'GEOADD' => 'static::first',
         );
     }
 

+ 3 - 0
src/Profile/RedisVersion320.php

@@ -268,6 +268,9 @@ class RedisVersion320 extends RedisProfile
             /* commands operating on hashes */
             'HSTRLEN' => 'Predis\Command\HashStringLength',
             'BITFIELD' => 'Predis\Command\StringBitField',
+
+            /* commands performing geospatial operations */
+            'GEOADD' => 'Predis\Command\GeospatialGeoAdd',
         );
     }
 }

+ 3 - 0
tests/Predis/Cluster/PredisStrategyTest.php

@@ -395,6 +395,9 @@ class PredisStrategyTest extends PredisTestCase
             /* scripting */
             'EVAL' => 'keys-script',
             'EVALSHA' => 'keys-script',
+
+            /* commands performing geospatial operations */
+            'GEOADD' => 'keys-first',
         );
 
         if (isset($type)) {

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

@@ -405,6 +405,9 @@ class RedisStrategyTest extends PredisTestCase
             /* scripting */
             'EVAL' => 'keys-script',
             'EVALSHA' => 'keys-script',
+
+            /* commands performing geospatial operations */
+            'GEOADD' => 'keys-first',
         );
 
         if (isset($type)) {

+ 106 - 0
tests/Predis/Command/GeospatialGeoAddTest.php

@@ -0,0 +1,106 @@
+<?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;
+
+/**
+ * @group commands
+ * @group realm-geospatial
+ */
+class GeospatialGeoAddTest extends PredisCommandTestCase
+{
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedCommand()
+    {
+        return 'Predis\Command\GeospatialGeoAdd';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedId()
+    {
+        return 'GEOADD';
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testFilterArguments()
+    {
+        $arguments = array('Sicily', '13.361389', '38.115556', 'Palermo', '15.087269', '37.502669', 'Catania');
+        $expected = array('Sicily', '13.361389', '38.115556', 'Palermo', '15.087269', '37.502669', 'Catania');
+
+        $command = $this->getCommand();
+        $command->setArguments($arguments);
+
+        $this->assertSame($expected, $command->getArguments());
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testFilterArgumentsWithMembersAsSingleArray()
+    {
+        $arguments = array('Sicily', array(
+            array('13.361389', '38.115556', 'Palermo'),
+            array('15.087269', '37.502669', 'Catania'),
+        ));
+
+        $expected = array('Sicily', '13.361389', '38.115556', 'Palermo', '15.087269', '37.502669', 'Catania');
+
+        $command = $this->getCommand();
+        $command->setArguments($arguments);
+
+        $this->assertSame($expected, $command->getArguments());
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testParseResponse()
+    {
+        $raw = 1;
+        $expected = 1;
+
+        $command = $this->getCommand();
+
+        $this->assertSame($expected, $command->parseResponse($raw));
+    }
+
+    /**
+     * @group connected
+     * @requiresRedisVersion >= 3.2.0
+     */
+    public function testCommandFillsSortedSet()
+    {
+        $redis = $this->getClient();
+
+        $redis->geoadd('Sicily', '13.361389', '38.115556', 'Palermo');
+        $this->assertSame(array('Palermo' => '3479099956230698'), $redis->zrange('Sicily', 0, -1, 'WITHSCORES'));
+    }
+
+    /**
+     * @group connected
+     * @requiresRedisVersion >= 3.2.0
+     * @expectedException \Predis\Response\ServerException
+     * @expectedExceptionMessage Operation against a key holding the wrong kind of value
+     */
+    public function testThrowsExceptionOnWrongType()
+    {
+        $redis = $this->getClient();
+
+        $redis->lpush('Sicily', 'Palermo');
+        $redis->geoadd('Sicily', '13.361389', '38.115556', 'Palermo');
+    }
+}

+ 4 - 0
tests/Predis/Command/Processor/KeyPrefixProcessorTest.php

@@ -877,6 +877,10 @@ class KeyPrefixProcessorTest extends PredisTestCase
                 array('key', 'GET', 'u8', '0', 'SET', 'u8', '0', '1'),
                 array('prefix:key', 'GET', 'u8', '0', 'SET', 'u8', '0', '1'),
             ),
+            array('GEOADD',
+                array('key', '13.361389', '38.115556', 'member:1', '15.087269', '37.502669', 'member:2'),
+                array('prefix:key', '13.361389', '38.115556', 'member:1', '15.087269', '37.502669', 'member:2'),
+            ),
         );
     }
 }

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

@@ -191,6 +191,7 @@ class RedisUnstableTest extends PredisProfileTestCase
             150 => 'COMMAND',
             151 => 'HSTRLEN',
             152 => 'BITFIELD',
+            153 => 'GEOADD',
         );
     }
 }

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

@@ -191,6 +191,7 @@ class RedisVersion320Test extends PredisProfileTestCase
             150 => 'COMMAND',
             151 => 'HSTRLEN',
             152 => 'BITFIELD',
+            153 => 'GEOADD',
         );
     }
 }

+ 3 - 0
tests/Predis/Replication/ReplicationStrategyTest.php

@@ -438,6 +438,9 @@ class ReplicationStrategyTest extends PredisTestCase
             /* scripting */
             'EVAL' => 'write',
             'EVALSHA' => 'write',
+
+            /* commands performing geospatial operations */
+            'GEOADD' => 'write',
         );
 
         if (isset($type)) {