CursorBasedIterator.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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
  16. * that can incrementally iterate over cursor-based collections stored
  17. * on Redis using commands in the `SCAN` family.
  18. *
  19. * Given their incremental nature with multiple fetches, these kind of
  20. * iterators offer limited guarantees about the returned elements because
  21. * the collection 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 Hints 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 instance supports the specified Redis
  52. * command required to fetch elements from the server to perform
  53. * the iteration.
  54. *
  55. * @param ClientInterface $client Client connected to Redis.
  56. * @param string $commandID Command ID.
  57. */
  58. protected function requiredCommand(ClientInterface $client, $commandID)
  59. {
  60. if (!$client->getProfile()->supportsCommand($commandID)) {
  61. throw new NotSupportedException("The specified server profile does not support the `$commandID` command.");
  62. }
  63. }
  64. /**
  65. * Resets the inner state of the iterator.
  66. */
  67. protected function reset()
  68. {
  69. $this->valid = true;
  70. $this->fetchmore = true;
  71. $this->elements = array();
  72. $this->cursor = 0;
  73. $this->position = -1;
  74. $this->current = null;
  75. }
  76. /**
  77. * Returns an array of options for the `SCAN` command.
  78. *
  79. * @return array
  80. */
  81. protected function getScanOptions()
  82. {
  83. $options = array();
  84. if (strlen($this->match) > 0) {
  85. $options['MATCH'] = $this->match;
  86. }
  87. if ($this->count > 0) {
  88. $options['COUNT'] = $this->count;
  89. }
  90. return $options;
  91. }
  92. /**
  93. * Fetches a new set of elements from the remote collection,
  94. * effectively advancing the iteration process.
  95. *
  96. * @return array
  97. */
  98. abstract protected function executeCommand();
  99. /**
  100. * Populates the local buffer of elements fetched from the
  101. * server during the iteration.
  102. */
  103. protected function fetch()
  104. {
  105. list($cursor, $elements) = $this->executeCommand();
  106. if (!$cursor) {
  107. $this->fetchmore = false;
  108. }
  109. $this->cursor = $cursor;
  110. $this->elements = $elements;
  111. }
  112. /**
  113. * Extracts next values for key() and current().
  114. */
  115. protected function extractNext()
  116. {
  117. $this->position++;
  118. $this->current = array_shift($this->elements);
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function rewind()
  124. {
  125. $this->reset();
  126. $this->next();
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function current()
  132. {
  133. return $this->current;
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function key()
  139. {
  140. return $this->position;
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function next()
  146. {
  147. tryFetch: {
  148. if (!$this->elements && $this->fetchmore) {
  149. $this->fetch();
  150. }
  151. if ($this->elements) {
  152. $this->extractNext();
  153. } elseif ($this->cursor) {
  154. goto tryFetch;
  155. } else {
  156. $this->valid = false;
  157. }
  158. }
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. public function valid()
  164. {
  165. return $this->valid;
  166. }
  167. }