Selaa lähdekoodia

Move Redis collections iterators in a different namespace.

The base iterator class is now known as RedisCollectionIterator and
we've also renamed a few methods to make their naming more generic.
Daniele Alessandri 11 vuotta sitten
vanhempi
commit
df2c9eb7ff

+ 4 - 4
examples/ScanBasedIterators.php → examples/RedisCollectionsIterators.php

@@ -11,10 +11,10 @@
 
 require 'SharedConfigurations.php';
 
-use Predis\Iterator\Scan\KeyspaceIterator;
-use Predis\Iterator\Scan\SetIterator;
-use Predis\Iterator\Scan\SortedSetIterator;
-use Predis\Iterator\Scan\HashIterator;
+use Predis\Collection\Iterator\KeyspaceIterator;
+use Predis\Collection\Iterator\SetIterator;
+use Predis\Collection\Iterator\SortedSetIterator;
+use Predis\Collection\Iterator\HashIterator;
 
 // Redis 2.8 features new commands allowing clients to incrementally
 // iterate over collections without blocking the server like it happens

+ 3 - 3
lib/Predis/Iterator/Scan/HashIterator.php → lib/Predis/Collection/Iterator/HashIterator.php

@@ -9,7 +9,7 @@
  * file that was distributed with this source code.
  */
 
-namespace Predis\Iterator\Scan;
+namespace Predis\Collection\Iterator;
 
 use Predis\ClientInterface;
 
@@ -21,7 +21,7 @@ use Predis\ClientInterface;
  * @author Daniele Alessandri <suppakilla@gmail.com>
  * @link http://redis.io/commands/scan
  */
-class HashIterator extends AbstractScanIterator
+class HashIterator extends RedisCollectionIterator
 {
     protected $key;
 
@@ -40,7 +40,7 @@ class HashIterator extends AbstractScanIterator
     /**
      * {@inheritdoc}
      */
-    protected function executeScanCommand()
+    protected function executeCommand()
     {
         return $this->client->hscan($this->key, $this->cursor, $this->getScanOptions());
     }

+ 3 - 3
lib/Predis/Iterator/Scan/KeyspaceIterator.php → lib/Predis/Collection/Iterator/KeyspaceIterator.php

@@ -9,7 +9,7 @@
  * file that was distributed with this source code.
  */
 
-namespace Predis\Iterator\Scan;
+namespace Predis\Collection\Iterator;
 
 use Predis\ClientInterface;
 
@@ -21,7 +21,7 @@ use Predis\ClientInterface;
  * @author Daniele Alessandri <suppakilla@gmail.com>
  * @link http://redis.io/commands/scan
  */
-class KeyspaceIterator extends AbstractScanIterator
+class KeyspaceIterator extends RedisCollectionIterator
 {
     /**
      * {@inheritdoc}
@@ -36,7 +36,7 @@ class KeyspaceIterator extends AbstractScanIterator
     /**
      * {@inheritdoc}
      */
-    protected function executeScanCommand()
+    protected function executeCommand()
     {
         return $this->client->scan($this->cursor, $this->getScanOptions());
     }

+ 20 - 18
lib/Predis/Iterator/Scan/AbstractScanIterator.php → lib/Predis/Collection/Iterator/RedisCollectionIterator.php

@@ -9,22 +9,23 @@
  * file that was distributed with this source code.
  */
 
-namespace Predis\Iterator\Scan;
+namespace Predis\Collection\Iterator;
 
 use Iterator;
-use Countable;
 use Predis\ClientInterface;
 use Predis\NotSupportedException;
 
 /**
- * This class provides the base implementation for a fully-rewindable
- * PHP iterator that can incrementally iterate over collections stored
- * on Redis by leveraging the SCAN family of commands (Redis >= 2.8).
+ * Provides the base implementation for a fully-rewindable PHP iterator
+ * that can incrementally iterate over collections stored on Redis.
+ *
+ * Given their incremental nature with multiple fetches, these kind of
+ * iterators offer limited guarantees about the returned elements because
+ * the collection can change several times during the iteration process.
  *
  * @author Daniele Alessandri <suppakilla@gmail.com>
- * @link http://redis.io/commands/scan
  */
-abstract class AbstractScanIterator implements Iterator
+abstract class RedisCollectionIterator implements Iterator
 {
     protected $client;
     protected $match;
@@ -52,8 +53,9 @@ abstract class AbstractScanIterator implements Iterator
     }
 
     /**
-     * Ensures that the client instance supports the specified
-     * Redis command required to perform the server-side iteration.
+     * Ensures that the client instance supports the specified Redis
+     * command required to fetch elements from the server to perform
+     * the iteration.
      *
      * @param ClientInterface Client connected to Redis.
      * @param string $commandID Command ID (e.g. `SCAN`).
@@ -79,7 +81,7 @@ abstract class AbstractScanIterator implements Iterator
     }
 
     /**
-     * Returns an array of options for the SCAN command.
+     * Returns an array of options for the `SCAN` command.
      *
      * @return array
      */
@@ -99,20 +101,20 @@ abstract class AbstractScanIterator implements Iterator
     }
 
     /**
-     * Performs a new SCAN to fetch new elements in the collection from
-     * Redis, effectively advancing the iteration process.
+     * Fetches a new set of elements from the remote collection,
+     * effectively advancing the iteration process.
      *
      * @return array
      */
-    protected abstract function executeScanCommand();
+    protected abstract function executeCommand();
 
     /**
-     * Populates the local buffer of elements fetched from the server
-     * during the iteration.
+     * Populates the local buffer of elements fetched from the
+     * server during the iteration.
      */
-    protected function feed()
+    protected function fetch()
     {
-        list($cursor, $elements) = $this->executeScanCommand();
+        list($cursor, $elements) = $this->executeCommand();
 
         if (!$cursor) {
             $this->scanmore = false;
@@ -162,7 +164,7 @@ abstract class AbstractScanIterator implements Iterator
     public function next()
     {
         if (!$this->elements && $this->scanmore) {
-            $this->feed();
+            $this->fetch();
         }
 
         if ($this->elements) {

+ 3 - 3
lib/Predis/Iterator/Scan/SetIterator.php → lib/Predis/Collection/Iterator/SetIterator.php

@@ -9,7 +9,7 @@
  * file that was distributed with this source code.
  */
 
-namespace Predis\Iterator\Scan;
+namespace Predis\Collection\Iterator;
 
 use Predis\ClientInterface;
 
@@ -21,7 +21,7 @@ use Predis\ClientInterface;
  * @author Daniele Alessandri <suppakilla@gmail.com>
  * @link http://redis.io/commands/scan
  */
-class SetIterator extends AbstractScanIterator
+class SetIterator extends RedisCollectionIterator
 {
     protected $key;
 
@@ -40,7 +40,7 @@ class SetIterator extends AbstractScanIterator
     /**
      * {@inheritdoc}
      */
-    protected function executeScanCommand()
+    protected function executeCommand()
     {
         return $this->client->sscan($this->key, $this->cursor, $this->getScanOptions());
     }

+ 3 - 3
lib/Predis/Iterator/Scan/SortedSetIterator.php → lib/Predis/Collection/Iterator/SortedSetIterator.php

@@ -9,7 +9,7 @@
  * file that was distributed with this source code.
  */
 
-namespace Predis\Iterator\Scan;
+namespace Predis\Collection\Iterator;
 
 use Predis\ClientInterface;
 
@@ -21,7 +21,7 @@ use Predis\ClientInterface;
  * @author Daniele Alessandri <suppakilla@gmail.com>
  * @link http://redis.io/commands/scan
  */
-class SortedSetIterator extends AbstractScanIterator
+class SortedSetIterator extends RedisCollectionIterator
 {
     protected $key;
 
@@ -40,7 +40,7 @@ class SortedSetIterator extends AbstractScanIterator
     /**
      * {@inheritdoc}
      */
-    protected function executeScanCommand()
+    protected function executeCommand()
     {
         return $this->client->zscan($this->key, $this->cursor, $this->getScanOptions());
     }

+ 1 - 1
tests/Predis/Iterator/Scan/HashIteratorTest.php → tests/Predis/Collection/Iterator/HashIteratorTest.php

@@ -9,7 +9,7 @@
  * file that was distributed with this source code.
  */
 
-namespace Predis\Iterator\Scan;
+namespace Predis\Collection\Iterator;
 
 use \PHPUnit_Framework_TestCase as StandardTestCase;
 

+ 1 - 1
tests/Predis/Iterator/Scan/KeyspaceIteratorTest.php → tests/Predis/Collection/Iterator/KeyspaceIteratorTest.php

@@ -9,7 +9,7 @@
  * file that was distributed with this source code.
  */
 
-namespace Predis\Iterator\Scan;
+namespace Predis\Collection\Iterator;
 
 use \PHPUnit_Framework_TestCase as StandardTestCase;
 

+ 1 - 1
tests/Predis/Iterator/Scan/SetIteratorTest.php → tests/Predis/Collection/Iterator/SetIteratorTest.php

@@ -9,7 +9,7 @@
  * file that was distributed with this source code.
  */
 
-namespace Predis\Iterator\Scan;
+namespace Predis\Collection\Iterator;
 
 use \PHPUnit_Framework_TestCase as StandardTestCase;
 

+ 1 - 1
tests/Predis/Iterator/Scan/SortedSetIteratorTest.php → tests/Predis/Collection/Iterator/SortedSetIteratorTest.php

@@ -9,7 +9,7 @@
  * file that was distributed with this source code.
  */
 
-namespace Predis\Iterator\Scan;
+namespace Predis\Collection\Iterator;
 
 use \PHPUnit_Framework_TestCase as StandardTestCase;