Files
predis/lib/Predis/Collection/Iterator/CursorBasedIterator.php
T
Daniele Alessandri 5ad00774e1 Rename for the last time all the iterator classes.
We are experimenting with a new approach at naming classes using less
redundant names by leveraging the containing namespace. The PHP "use"
directive is not limited to class names but can be used to import the
whole namespace, which means you can do something like this:

  use Predis\Collection\Iterator;
  // ...
  foreach (new Iterator\Keyspace($client) as $key) {
  	// ...
  }

Alternatively you can always rely on "use ... as ..." to import one of
the classes by giving it a more meaningful name in the context of the
root namespace:

  use Predis\Collection\Iterator\Keyspace as KeyspaceIterator;
  // ...
  foreach (new KeyspaceIterator($client) as $key) {
  	// ...
  }

In this specific case we chose to apply the -Key postfix to classes
iterating Redis keys to be more explicit about the fact that those
iterators does not work on local in-memory collections, but fetch
items from a key stored on a remote Redis server.
2013-11-07 12:09:59 +01:00

187 lines
4.3 KiB
PHP

<?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\Collection\Iterator;
use Iterator;
use Predis\ClientInterface;
use Predis\NotSupportedException;
/**
* 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>
*/
abstract class CursorBasedIterator implements Iterator
{
protected $client;
protected $match;
protected $count;
protected $valid;
protected $scanmore;
protected $elements;
protected $cursor;
protected $position;
protected $current;
/**
* @param ClientInterface $client Client connected to Redis.
* @param string $match Pattern to match during the server-side iteration.
* @param int $count Hints used by Redis to compute the number of results per iteration.
*/
public function __construct(ClientInterface $client, $match = null, $count = null)
{
$this->client = $client;
$this->match = $match;
$this->count = $count;
$this->reset();
}
/**
* 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`).
*/
protected function requiredCommand(ClientInterface $client, $commandID)
{
if (!$client->getProfile()->supportsCommand($commandID)) {
throw new NotSupportedException("The specified server profile does not support the `$commandID` command.");
}
}
/**
* Resets the inner state of the iterator.
*/
protected function reset()
{
$this->valid = true;
$this->scanmore = true;
$this->elements = array();
$this->cursor = 0;
$this->position = -1;
$this->current = null;
}
/**
* Returns an array of options for the `SCAN` command.
*
* @return array
*/
protected function getScanOptions()
{
$options = array();
if (strlen($this->match) > 0) {
$options['MATCH'] = $this->match;
}
if ($this->count > 0) {
$options['COUNT'] = $this->count;
}
return $options;
}
/**
* Fetches a new set of elements from the remote collection,
* effectively advancing the iteration process.
*
* @return array
*/
protected abstract function executeCommand();
/**
* Populates the local buffer of elements fetched from the
* server during the iteration.
*/
protected function fetch()
{
list($cursor, $elements) = $this->executeCommand();
if (!$cursor) {
$this->scanmore = false;
}
$this->cursor = $cursor;
$this->elements = $elements;
}
/**
* Extracts next values for key() and current().
*/
protected function extractNext()
{
$this->position++;
$this->current = array_shift($this->elements);
}
/**
* {@inheritdoc}
*/
public function rewind()
{
$this->reset();
$this->next();
}
/**
* {@inheritdoc}
*/
public function current()
{
return $this->current;
}
/**
* {@inheritdoc}
*/
public function key()
{
return $this->position;
}
/**
* {@inheritdoc}
*/
public function next()
{
if (!$this->elements && $this->scanmore) {
$this->fetch();
}
if ($this->elements) {
$this->extractNext();
} else if ($this->cursor) {
$this->next();
} else {
$this->valid = false;
}
}
/**
* {@inheritdoc}
*/
public function valid()
{
return $this->valid;
}
}