浏览代码

Add new command: GEOHASH (Redis 3.2.0).

Daniele Alessandri 9 年之前
父节点
当前提交
e96f7f748a

+ 1 - 0
src/ClientContextInterface.php

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

+ 1 - 0
src/ClientInterface.php

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

+ 1 - 0
src/Cluster/ClusterStrategy.php

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

+ 41 - 0
src/Command/GeospatialGeoHash.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/geohash
+ *
+ * @author Daniele Alessandri <suppakilla@gmail.com>
+ */
+class GeospatialGeoHash extends Command
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function getId()
+    {
+        return 'GEOHASH';
+    }
+
+    /**
+     * {@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

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

+ 1 - 0
src/Profile/RedisVersion320.php

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

+ 1 - 0
src/Replication/ReplicationStrategy.php

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

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

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

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

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

+ 102 - 0
tests/Predis/Command/GeospatialGeoHashTest.php

@@ -0,0 +1,102 @@
+<?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 GeospatialGeoHashTest extends PredisCommandTestCase
+{
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedCommand()
+    {
+        return 'Predis\Command\GeospatialGeoHash';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedId()
+    {
+        return 'GEOHASH';
+    }
+
+    /**
+     * @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('sqc8b49rny0', 'sqdtr74hyu0');
+        $expected = array('sqc8b49rny0', 'sqdtr74hyu0');
+
+        $command = $this->getCommand();
+
+        $this->assertSame($expected, $command->parseResponse($raw));
+    }
+
+    /**
+     * @group connected
+     * @requiresRedisVersion >= 3.2.0
+     */
+    public function testCommandReturnsGeoHashes()
+    {
+        $redis = $this->getClient();
+
+        $redis->geoadd('Sicily', '13.361389', '38.115556', 'Palermo', '15.087269', '37.502669', 'Catania');
+        $this->assertSame(array('sqc8b49rny0', 'sqdtr74hyu0'), $redis->geohash('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->geohash('Sicily', 'Palermo');
+    }
+}

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

@@ -881,6 +881,10 @@ class KeyPrefixProcessorTest extends PredisTestCase
                 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'),
             ),
+            array('GEOHASH',
+                array('key', 'member:1', 'member:2'),
+                array('prefix:key', 'member:1', 'member:2'),
+            ),
         );
     }
 }

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

@@ -192,6 +192,7 @@ class RedisUnstableTest extends PredisProfileTestCase
             151 => 'HSTRLEN',
             152 => 'BITFIELD',
             153 => 'GEOADD',
+            154 => 'GEOHASH',
         );
     }
 }

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

@@ -192,6 +192,7 @@ class RedisVersion320Test extends PredisProfileTestCase
             151 => 'HSTRLEN',
             152 => 'BITFIELD',
             153 => 'GEOADD',
+            154 => 'GEOHASH',
         );
     }
 }

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

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