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 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(
  61. "The specified server profile does not support the `$commandID` command."
  62. );
  63. }
  64. }
  65. /**
  66. * Resets the inner state of the iterator.
  67. */
  68. protected function reset()
  69. {
  70. $this->valid = true;
  71. $this->fetchmore = true;
  72. $this->elements = array();
  73. $this->cursor = 0;
  74. $this->position = -1;
  75. $this->current = null;
  76. }
  77. /**
  78. * Returns an array of options for the `SCAN` command.
  79. *
  80. * @return array
  81. */
  82. protected function getScanOptions()
  83. {
  84. $options = array();
  85. if (strlen($this->match) > 0) {
  86. $options['MATCH'] = $this->match;
  87. }
  88. if ($this->count > 0) {
  89. $options['COUNT'] = $this->count;
  90. }
  91. return $options;
  92. }
  93. /**
  94. * Fetches a new set of elements from the remote collection, effectively
  95. * advancing the iteration process.
  96. *
  97. * @return array
  98. */
  99. protected abstract function executeCommand();
  100. /**
  101. * Populates the local buffer of elements fetched from the server during
  102. * the iteration.
  103. */
  104. protected function fetch()
  105. {
  106. list($cursor, $elements) = $this->executeCommand();
  107. if (!$cursor) {
  108. $this->fetchmore = false;
  109. }
  110. $this->cursor = $cursor;
  111. $this->elements = $elements;
  112. }
  113. /**
  114. * Extracts next values for key() and current().
  115. */
  116. protected function extractNext()
  117. {
  118. $this->position++;
  119. $this->current = array_shift($this->elements);
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function rewind()
  125. {
  126. $this->reset();
  127. $this->next();
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function current()
  133. {
  134. return $this->current;
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function key()
  140. {
  141. return $this->position;
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function next()
  147. {
  148. if (!$this->elements && $this->fetchmore) {
  149. $this->fetch();
  150. }
  151. if ($this->elements) {
  152. $this->extractNext();
  153. } else if ($this->cursor) {
  154. $this->next();
  155. } else {
  156. $this->valid = false;
  157. }
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function valid()
  163. {
  164. return $this->valid;
  165. }
  166. }