CursorBasedIterator.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Collection\Iterator;
  11. use Iterator;
  12. use Predis\ClientInterface;
  13. use Predis\NotSupportedException;
  14. /**
  15. * Provides the base implementation for a fully-rewindable PHP iterator that can
  16. * incrementally iterate over cursor-based collections stored on Redis using the
  17. * commands in the `SCAN` family.
  18. *
  19. * Given their incremental nature with multiple fetches, these kind of iterators
  20. * offer limited guarantees about the returned elements because the collection
  21. * can change several times during the iteration process.
  22. *
  23. * @see http://redis.io/commands/scan
  24. *
  25. * @author Daniele Alessandri <suppakilla@gmail.com>
  26. */
  27. abstract class CursorBasedIterator implements Iterator
  28. {
  29. protected $client;
  30. protected $match;
  31. protected $count;
  32. protected $valid;
  33. protected $fetchmore;
  34. protected $elements;
  35. protected $cursor;
  36. protected $position;
  37. protected $current;
  38. /**
  39. * @param ClientInterface $client Client connected to Redis.
  40. * @param string $match Pattern to match during the server-side iteration.
  41. * @param int $count Hint used by Redis to compute the number of results per iteration.
  42. */
  43. public function __construct(ClientInterface $client, $match = null, $count = null)
  44. {
  45. $this->client = $client;
  46. $this->match = $match;
  47. $this->count = $count;
  48. $this->reset();
  49. }
  50. /**
  51. * Ensures that the client supports the specified Redis command required to
  52. * fetch elements from the server to perform the iteration.
  53. *
  54. * @param ClientInterface $client Client connected to Redis.
  55. * @param string $commandID Command ID.
  56. */
  57. protected function requiredCommand(ClientInterface $client, $commandID)
  58. {
  59. if (!$client->getProfile()->supportsCommand($commandID)) {
  60. throw new NotSupportedException("The current profile does not support '$commandID'.");
  61. }
  62. }
  63. /**
  64. * Resets the inner state of the iterator.
  65. */
  66. protected function reset()
  67. {
  68. $this->valid = true;
  69. $this->fetchmore = true;
  70. $this->elements = array();
  71. $this->cursor = 0;
  72. $this->position = -1;
  73. $this->current = null;
  74. }
  75. /**
  76. * Returns an array of options for the `SCAN` command.
  77. *
  78. * @return array
  79. */
  80. protected function getScanOptions()
  81. {
  82. $options = array();
  83. if (strlen($this->match) > 0) {
  84. $options['MATCH'] = $this->match;
  85. }
  86. if ($this->count > 0) {
  87. $options['COUNT'] = $this->count;
  88. }
  89. return $options;
  90. }
  91. /**
  92. * Fetches a new set of elements from the remote collection, effectively
  93. * advancing the iteration process.
  94. *
  95. * @return array
  96. */
  97. abstract protected function executeCommand();
  98. /**
  99. * Populates the local buffer of elements fetched from the server during
  100. * the iteration.
  101. */
  102. protected function fetch()
  103. {
  104. list($cursor, $elements) = $this->executeCommand();
  105. if (!$cursor) {
  106. $this->fetchmore = false;
  107. }
  108. $this->cursor = $cursor;
  109. $this->elements = $elements;
  110. }
  111. /**
  112. * Extracts next values for key() and current().
  113. */
  114. protected function extractNext()
  115. {
  116. $this->position++;
  117. $this->current = array_shift($this->elements);
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function rewind()
  123. {
  124. $this->reset();
  125. $this->next();
  126. }
  127. /**
  128. * {@inheritdoc}
  129. */
  130. public function current()
  131. {
  132. return $this->current;
  133. }
  134. /**
  135. * {@inheritdoc}
  136. */
  137. public function key()
  138. {
  139. return $this->position;
  140. }
  141. /**
  142. * {@inheritdoc}
  143. */
  144. public function next()
  145. {
  146. tryFetch: {
  147. if (!$this->elements && $this->fetchmore) {
  148. $this->fetch();
  149. }
  150. if ($this->elements) {
  151. $this->extractNext();
  152. } elseif ($this->cursor) {
  153. goto tryFetch;
  154. } else {
  155. $this->valid = false;
  156. }
  157. }
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function valid()
  163. {
  164. return $this->valid;
  165. }
  166. }