|
@@ -11,9 +11,7 @@
|
|
|
|
|
|
namespace Predis\Iterator\Scan;
|
|
namespace Predis\Iterator\Scan;
|
|
|
|
|
|
-use Iterator;
|
|
|
|
use Predis\ClientInterface;
|
|
use Predis\ClientInterface;
|
|
-use Predis\NotSupportedException;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* Abstracts the iteration of the keyspace on a Redis instance
|
|
* Abstracts the iteration of the keyspace on a Redis instance
|
|
@@ -23,142 +21,23 @@ use Predis\NotSupportedException;
|
|
* @author Daniele Alessandri <suppakilla@gmail.com>
|
|
* @author Daniele Alessandri <suppakilla@gmail.com>
|
|
* @link http://redis.io/commands/scan
|
|
* @link http://redis.io/commands/scan
|
|
*/
|
|
*/
|
|
-class KeyspaceIterator implements Iterator
|
|
|
|
|
|
+class KeyspaceIterator extends AbstractScanIterator
|
|
{
|
|
{
|
|
- protected $client;
|
|
|
|
- protected $match;
|
|
|
|
- protected $count;
|
|
|
|
-
|
|
|
|
- protected $valid;
|
|
|
|
- protected $scanmore;
|
|
|
|
- protected $elements;
|
|
|
|
- protected $position;
|
|
|
|
- protected $cursor;
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
- * @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.
|
|
|
|
|
|
+ * {@inheritdoc}
|
|
*/
|
|
*/
|
|
public function __construct(ClientInterface $client, $match = null, $count = null)
|
|
public function __construct(ClientInterface $client, $match = null, $count = null)
|
|
{
|
|
{
|
|
- if (!$client->getProfile()->supportsCommand('SCAN')) {
|
|
|
|
- throw new NotSupportedException('The specified server profile does not support the SCAN command.');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- $this->client = $client;
|
|
|
|
- $this->match = $match;
|
|
|
|
- $this->count = $count;
|
|
|
|
-
|
|
|
|
- $this->reset();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * Resets the inner state of the iterator.
|
|
|
|
- */
|
|
|
|
- protected function reset()
|
|
|
|
- {
|
|
|
|
- $this->valid = true;
|
|
|
|
- $this->scanmore = true;
|
|
|
|
- $this->elements = array();
|
|
|
|
- $this->position = -1;
|
|
|
|
- $this->cursor = 0;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 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;
|
|
|
|
- }
|
|
|
|
|
|
+ $this->requiredCommand($client, 'SCAN');
|
|
|
|
|
|
- if ($this->count > 0) {
|
|
|
|
- $options['COUNT'] = $this->count;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return $options;
|
|
|
|
|
|
+ parent::__construct($client, $match, $count);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Performs a new SCAN to fetch new elements in the collection from
|
|
|
|
- * Redis, effectively advancing the iteration process.
|
|
|
|
- *
|
|
|
|
- * @return array
|
|
|
|
|
|
+ * {@inheritdoc}
|
|
*/
|
|
*/
|
|
protected function executeScanCommand()
|
|
protected function executeScanCommand()
|
|
{
|
|
{
|
|
return $this->client->scan($this->cursor, $this->getScanOptions());
|
|
return $this->client->scan($this->cursor, $this->getScanOptions());
|
|
}
|
|
}
|
|
-
|
|
|
|
- /**
|
|
|
|
- * Populates the local buffer of elements fetched from the server
|
|
|
|
- * during the iteration.
|
|
|
|
- */
|
|
|
|
- protected function feed()
|
|
|
|
- {
|
|
|
|
- list($cursor, $elements) = $this->executeScanCommand();
|
|
|
|
-
|
|
|
|
- if (!$cursor) {
|
|
|
|
- $this->scanmore = false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- $this->cursor = $cursor;
|
|
|
|
- $this->elements = $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->feed();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if ($this->elements) {
|
|
|
|
- $this->position++;
|
|
|
|
- $this->current = array_shift($this->elements);
|
|
|
|
- } else {
|
|
|
|
- $this->valid = false;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * {@inheritdoc}
|
|
|
|
- */
|
|
|
|
- public function valid()
|
|
|
|
- {
|
|
|
|
- return $this->valid;
|
|
|
|
- }
|
|
|
|
}
|
|
}
|