Эх сурвалжийг харах

Add new command: GEOPOS (Redis 3.2.0).

Daniele Alessandri 9 жил өмнө
parent
commit
c4e0044269

+ 1 - 0
src/ClientContextInterface.php

@@ -158,6 +158,7 @@ use Predis\Command\CommandInterface;
  * @method $this command()
  * @method $this geoadd($key, $longitude, $latitude, $member)
  * @method $this geohash($key, array $members)
+ * @method $this geopos($key, array $members)
  *
  * @author Daniele Alessandri <suppakilla@gmail.com>
  */

+ 1 - 0
src/ClientInterface.php

@@ -166,6 +166,7 @@ use Predis\Profile\ProfileInterface;
  * @method array  command()
  * @method int    geoadd($key, $longitude, $latitude, $member)
  * @method array  geohash($key, array $members)
+ * @method array  geopos($key, array $members)
  *
  * @author Daniele Alessandri <suppakilla@gmail.com>
  */

+ 1 - 0
src/Cluster/ClusterStrategy.php

@@ -169,6 +169,7 @@ abstract class ClusterStrategy implements StrategyInterface
             /* commands performing geospatial operations */
             'GEOADD' => $getKeyFromFirstArgument,
             'GEOHASH' => $getKeyFromFirstArgument,
+            'GEOPOS' => $getKeyFromFirstArgument,
         );
     }
 

+ 41 - 0
src/Command/GeospatialGeoPos.php

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

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

@@ -162,6 +162,7 @@ class KeyPrefixProcessor implements ProcessorInterface
             'BITFIELD' => 'static::first',
             'GEOADD' => 'static::first',
             'GEOHASH' => 'static::first',
+            'GEOPOS' => 'static::first',
         );
     }
 

+ 1 - 0
src/Profile/RedisVersion320.php

@@ -272,6 +272,7 @@ class RedisVersion320 extends RedisProfile
             /* commands performing geospatial operations */
             'GEOADD' => 'Predis\Command\GeospatialGeoAdd',
             'GEOHASH' => 'Predis\Command\GeospatialGeoHash',
+            'GEOPOS' => 'Predis\Command\GeospatialGeoPos',
         );
     }
 }

+ 1 - 0
src/Replication/ReplicationStrategy.php

@@ -259,6 +259,7 @@ class ReplicationStrategy
             'SORT' => array($this, 'isSortReadOnly'),
             'BITFIELD' => array($this, 'isBitfieldReadOnly'),
             'GEOHASH' => true,
+            'GEOPOS' => true,
         );
     }
 }

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

@@ -381,6 +381,7 @@ class PredisStrategyTest extends PredisTestCase
             /* commands performing geospatial operations */
             'GEOADD' => 'keys-first',
             'GEOHASH' => 'keys-first',
+            'GEOPOS' => 'keys-first',
         );
 
         if (isset($type)) {

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

@@ -391,6 +391,7 @@ class RedisStrategyTest extends PredisTestCase
             /* commands performing geospatial operations */
             'GEOADD' => 'keys-first',
             'GEOHASH' => 'keys-first',
+            'GEOPOS' => 'keys-first',
         );
 
         if (isset($type)) {

+ 112 - 0
tests/Predis/Command/GeospatialGeoPosTest.php

@@ -0,0 +1,112 @@
+<?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 GeospatialGeoPosTest extends PredisCommandTestCase
+{
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedCommand()
+    {
+        return 'Predis\Command\GeospatialGeoPos';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedId()
+    {
+        return 'GEOPOS';
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testFilterArguments()
+    {
+        $arguments = array('key', 'member:1', 'member:2');
+        $expected = array('key', 'member:1', 'member:2');
+
+        $command = $this->getCommand();
+        $command->setArguments($arguments);
+
+        $this->assertSame($expected, $command->getArguments());
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testFilterArgumentsWithMembersAsSingleArray()
+    {
+        $arguments = array('key', array('member:1', 'member:2'));
+        $expected = array('key', 'member:1', 'member:2');
+
+        $command = $this->getCommand();
+        $command->setArguments($arguments);
+
+        $this->assertSame($expected, $command->getArguments());
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testParseResponse()
+    {
+        $raw = array(
+            array("13.361389338970184", "38.115556395496299"),
+            array("15.087267458438873", "37.50266842333162"),
+        );
+
+        $expected = array(
+            array("13.361389338970184", "38.115556395496299"),
+            array("15.087267458438873", "37.50266842333162"),
+        );
+
+        $command = $this->getCommand();
+
+        $this->assertSame($expected, $command->parseResponse($raw));
+    }
+
+    /**
+     * @group connected
+     * @requiresRedisVersion >= 3.2.0
+     */
+    public function testCommandReturnsGeoPositions()
+    {
+        $redis = $this->getClient();
+
+        $redis->geoadd('Sicily', '13.361389', '38.115556', 'Palermo', '15.087269', '37.502669', 'Catania');
+        $this->assertEquals(array(
+            array("13.361389338970184", "38.115556395496299"),
+            array("15.087267458438873", "37.50266842333162"),
+        ), $redis->geopos('Sicily', 'Palermo', 'Catania'));
+    }
+
+    /**
+     * @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->geopos('Sicily', 'Palermo');
+    }
+}

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

@@ -881,6 +881,10 @@ class KeyPrefixProcessorTest extends PredisTestCase
                 array('key', 'member:1', 'member:2'),
                 array('prefix:key', 'member:1', 'member:2'),
             ),
+            array('GEOPOS',
+                array('key', 'member:1', 'member:2'),
+                array('prefix:key', 'member:1', 'member:2'),
+            ),
         );
     }
 }

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

@@ -193,6 +193,7 @@ class RedisUnstableTest extends PredisProfileTestCase
             152 => 'BITFIELD',
             153 => 'GEOADD',
             154 => 'GEOHASH',
+            155 => 'GEOPOS',
         );
     }
 }

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

@@ -193,6 +193,7 @@ class RedisVersion320Test extends PredisProfileTestCase
             152 => 'BITFIELD',
             153 => 'GEOADD',
             154 => 'GEOHASH',
+            155 => 'GEOPOS',
         );
     }
 }

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

@@ -442,6 +442,7 @@ class ReplicationStrategyTest extends PredisTestCase
             /* commands performing geospatial operations */
             'GEOADD' => 'write',
             'GEOHASH' => 'read',
+            'GEOPOS' => 'read',
         );
 
         if (isset($type)) {