Browse Source

Add new command: GEODIST (Redis 3.2.0).

Daniele Alessandri 9 năm trước cách đây
mục cha
commit
b2284d015f

+ 1 - 0
src/ClientContextInterface.php

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

+ 1 - 0
src/ClientInterface.php

@@ -167,6 +167,7 @@ use Predis\Profile\ProfileInterface;
  * @method int    geoadd($key, $longitude, $latitude, $member)
  * @method int    geoadd($key, $longitude, $latitude, $member)
  * @method array  geohash($key, array $members)
  * @method array  geohash($key, array $members)
  * @method array  geopos($key, array $members)
  * @method array  geopos($key, array $members)
+ * @method string geodist($key, $member1, $member2, $unit = null)
  *
  *
  * @author Daniele Alessandri <suppakilla@gmail.com>
  * @author Daniele Alessandri <suppakilla@gmail.com>
  */
  */

+ 1 - 0
src/Cluster/ClusterStrategy.php

@@ -170,6 +170,7 @@ abstract class ClusterStrategy implements StrategyInterface
             'GEOADD' => $getKeyFromFirstArgument,
             'GEOADD' => $getKeyFromFirstArgument,
             'GEOHASH' => $getKeyFromFirstArgument,
             'GEOHASH' => $getKeyFromFirstArgument,
             'GEOPOS' => $getKeyFromFirstArgument,
             'GEOPOS' => $getKeyFromFirstArgument,
+            'GEODIST' => $getKeyFromFirstArgument,
         );
         );
     }
     }
 
 

+ 28 - 0
src/Command/GeospatialGeoDist.php

@@ -0,0 +1,28 @@
+<?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/geodist
+ *
+ * @author Daniele Alessandri <suppakilla@gmail.com>
+ */
+class GeospatialGeoDist extends Command
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function getId()
+    {
+        return 'GEODIST';
+    }
+}

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

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

+ 1 - 0
src/Profile/RedisVersion320.php

@@ -273,6 +273,7 @@ class RedisVersion320 extends RedisProfile
             'GEOADD' => 'Predis\Command\GeospatialGeoAdd',
             'GEOADD' => 'Predis\Command\GeospatialGeoAdd',
             'GEOHASH' => 'Predis\Command\GeospatialGeoHash',
             'GEOHASH' => 'Predis\Command\GeospatialGeoHash',
             'GEOPOS' => 'Predis\Command\GeospatialGeoPos',
             'GEOPOS' => 'Predis\Command\GeospatialGeoPos',
+            'GEODIST' => 'Predis\Command\GeospatialGeoDist',
         );
         );
     }
     }
 }
 }

+ 1 - 0
src/Replication/ReplicationStrategy.php

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

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

@@ -382,6 +382,7 @@ class PredisStrategyTest extends PredisTestCase
             'GEOADD' => 'keys-first',
             'GEOADD' => 'keys-first',
             'GEOHASH' => 'keys-first',
             'GEOHASH' => 'keys-first',
             'GEOPOS' => 'keys-first',
             'GEOPOS' => 'keys-first',
+            'GEODIST' => 'keys-first',
         );
         );
 
 
         if (isset($type)) {
         if (isset($type)) {

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

@@ -392,6 +392,7 @@ class RedisStrategyTest extends PredisTestCase
             'GEOADD' => 'keys-first',
             'GEOADD' => 'keys-first',
             'GEOHASH' => 'keys-first',
             'GEOHASH' => 'keys-first',
             'GEOPOS' => 'keys-first',
             'GEOPOS' => 'keys-first',
+            'GEODIST' => 'keys-first',
         );
         );
 
 
         if (isset($type)) {
         if (isset($type)) {

+ 88 - 0
tests/Predis/Command/GeospatialGeoDistTest.php

@@ -0,0 +1,88 @@
+<?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 GeospatialGeoDistTest extends PredisCommandTestCase
+{
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedCommand()
+    {
+        return 'Predis\Command\GeospatialGeoDist';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedId()
+    {
+        return 'GEODIST';
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testFilterArguments()
+    {
+        $arguments = array('key', 'member:1', 'member:2', 'km');
+        $expected = array('key', 'member:1', 'member:2', 'km');
+
+        $command = $this->getCommand();
+        $command->setArguments($arguments);
+
+        $this->assertSame($expected, $command->getArguments());
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testParseResponse()
+    {
+        $raw = array('103.31822459492736');
+        $expected = array('103.31822459492736');
+
+        $command = $this->getCommand();
+
+        $this->assertSame($expected, $command->parseResponse($raw));
+    }
+
+    /**
+     * @group connected
+     * @requiresRedisVersion >= 3.2.0
+     */
+    public function testCommandReturnsGeoDistance()
+    {
+        $redis = $this->getClient();
+
+        $redis->geoadd('Sicily', '13.361389', '38.115556', 'Palermo', '15.087269', '37.502669', 'Catania');
+        $this->assertSame('166.2742', $redis->geodist('Sicily', 'Palermo', 'Catania', 'km'));
+    }
+
+    /**
+     * @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->geodist('Sicily', 'Palermo', 'Catania');
+    }
+}

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

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

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

@@ -194,6 +194,7 @@ class RedisUnstableTest extends PredisProfileTestCase
             153 => 'GEOADD',
             153 => 'GEOADD',
             154 => 'GEOHASH',
             154 => 'GEOHASH',
             155 => 'GEOPOS',
             155 => 'GEOPOS',
+            156 => 'GEODIST',
         );
         );
     }
     }
 }
 }

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

@@ -194,6 +194,7 @@ class RedisVersion320Test extends PredisProfileTestCase
             153 => 'GEOADD',
             153 => 'GEOADD',
             154 => 'GEOHASH',
             154 => 'GEOHASH',
             155 => 'GEOPOS',
             155 => 'GEOPOS',
+            156 => 'GEODIST',
         );
         );
     }
     }
 }
 }

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

@@ -443,6 +443,7 @@ class ReplicationStrategyTest extends PredisTestCase
             'GEOADD' => 'write',
             'GEOADD' => 'write',
             'GEOHASH' => 'read',
             'GEOHASH' => 'read',
             'GEOPOS' => 'read',
             'GEOPOS' => 'read',
+            'GEODIST' => 'read',
         );
         );
 
 
         if (isset($type)) {
         if (isset($type)) {