From df2c9eb7ff435dbdfd8e2e976714d5b37a628dca Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Wed, 6 Nov 2013 16:13:48 +0100 Subject: [PATCH] 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. --- ...tors.php => RedisCollectionsIterators.php} | 8 ++-- .../Iterator}/HashIterator.php | 6 +-- .../Iterator}/KeyspaceIterator.php | 6 +-- .../Iterator/RedisCollectionIterator.php} | 38 ++++++++++--------- .../Iterator}/SetIterator.php | 6 +-- .../Iterator}/SortedSetIterator.php | 6 +-- .../Iterator}/HashIteratorTest.php | 2 +- .../Iterator}/KeyspaceIteratorTest.php | 2 +- .../Iterator}/SetIteratorTest.php | 2 +- .../Iterator}/SortedSetIteratorTest.php | 2 +- 10 files changed, 40 insertions(+), 38 deletions(-) rename examples/{ScanBasedIterators.php => RedisCollectionsIterators.php} (93%) rename lib/Predis/{Iterator/Scan => Collection/Iterator}/HashIterator.php (89%) rename lib/Predis/{Iterator/Scan => Collection/Iterator}/KeyspaceIterator.php (87%) rename lib/Predis/{Iterator/Scan/AbstractScanIterator.php => Collection/Iterator/RedisCollectionIterator.php} (75%) rename lib/Predis/{Iterator/Scan => Collection/Iterator}/SetIterator.php (88%) rename lib/Predis/{Iterator/Scan => Collection/Iterator}/SortedSetIterator.php (89%) rename tests/Predis/{Iterator/Scan => Collection/Iterator}/HashIteratorTest.php (99%) rename tests/Predis/{Iterator/Scan => Collection/Iterator}/KeyspaceIteratorTest.php (99%) rename tests/Predis/{Iterator/Scan => Collection/Iterator}/SetIteratorTest.php (99%) rename tests/Predis/{Iterator/Scan => Collection/Iterator}/SortedSetIteratorTest.php (99%) diff --git a/examples/ScanBasedIterators.php b/examples/RedisCollectionsIterators.php similarity index 93% rename from examples/ScanBasedIterators.php rename to examples/RedisCollectionsIterators.php index 5fa57d39..f7b00c29 100644 --- a/examples/ScanBasedIterators.php +++ b/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 diff --git a/lib/Predis/Iterator/Scan/HashIterator.php b/lib/Predis/Collection/Iterator/HashIterator.php similarity index 89% rename from lib/Predis/Iterator/Scan/HashIterator.php rename to lib/Predis/Collection/Iterator/HashIterator.php index 078338f3..80c8ae50 100644 --- a/lib/Predis/Iterator/Scan/HashIterator.php +++ b/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 * @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()); } diff --git a/lib/Predis/Iterator/Scan/KeyspaceIterator.php b/lib/Predis/Collection/Iterator/KeyspaceIterator.php similarity index 87% rename from lib/Predis/Iterator/Scan/KeyspaceIterator.php rename to lib/Predis/Collection/Iterator/KeyspaceIterator.php index 4cd3d629..dd5c9934 100644 --- a/lib/Predis/Iterator/Scan/KeyspaceIterator.php +++ b/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 * @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()); } diff --git a/lib/Predis/Iterator/Scan/AbstractScanIterator.php b/lib/Predis/Collection/Iterator/RedisCollectionIterator.php similarity index 75% rename from lib/Predis/Iterator/Scan/AbstractScanIterator.php rename to lib/Predis/Collection/Iterator/RedisCollectionIterator.php index d7efe7b9..83075e40 100644 --- a/lib/Predis/Iterator/Scan/AbstractScanIterator.php +++ b/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 - * @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) { diff --git a/lib/Predis/Iterator/Scan/SetIterator.php b/lib/Predis/Collection/Iterator/SetIterator.php similarity index 88% rename from lib/Predis/Iterator/Scan/SetIterator.php rename to lib/Predis/Collection/Iterator/SetIterator.php index ae7c4c99..5b33eb3b 100644 --- a/lib/Predis/Iterator/Scan/SetIterator.php +++ b/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 * @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()); } diff --git a/lib/Predis/Iterator/Scan/SortedSetIterator.php b/lib/Predis/Collection/Iterator/SortedSetIterator.php similarity index 89% rename from lib/Predis/Iterator/Scan/SortedSetIterator.php rename to lib/Predis/Collection/Iterator/SortedSetIterator.php index 7b8ed033..fc31d7a7 100644 --- a/lib/Predis/Iterator/Scan/SortedSetIterator.php +++ b/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 * @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()); } diff --git a/tests/Predis/Iterator/Scan/HashIteratorTest.php b/tests/Predis/Collection/Iterator/HashIteratorTest.php similarity index 99% rename from tests/Predis/Iterator/Scan/HashIteratorTest.php rename to tests/Predis/Collection/Iterator/HashIteratorTest.php index 1a9aeaed..52f8850a 100644 --- a/tests/Predis/Iterator/Scan/HashIteratorTest.php +++ b/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; diff --git a/tests/Predis/Iterator/Scan/KeyspaceIteratorTest.php b/tests/Predis/Collection/Iterator/KeyspaceIteratorTest.php similarity index 99% rename from tests/Predis/Iterator/Scan/KeyspaceIteratorTest.php rename to tests/Predis/Collection/Iterator/KeyspaceIteratorTest.php index 60524fed..fdf86fc5 100644 --- a/tests/Predis/Iterator/Scan/KeyspaceIteratorTest.php +++ b/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; diff --git a/tests/Predis/Iterator/Scan/SetIteratorTest.php b/tests/Predis/Collection/Iterator/SetIteratorTest.php similarity index 99% rename from tests/Predis/Iterator/Scan/SetIteratorTest.php rename to tests/Predis/Collection/Iterator/SetIteratorTest.php index 17bba9df..a2441f26 100644 --- a/tests/Predis/Iterator/Scan/SetIteratorTest.php +++ b/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; diff --git a/tests/Predis/Iterator/Scan/SortedSetIteratorTest.php b/tests/Predis/Collection/Iterator/SortedSetIteratorTest.php similarity index 99% rename from tests/Predis/Iterator/Scan/SortedSetIteratorTest.php rename to tests/Predis/Collection/Iterator/SortedSetIteratorTest.php index 8b0832ca..9bdfeff7 100644 --- a/tests/Predis/Iterator/Scan/SortedSetIteratorTest.php +++ b/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;