[7588] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * Copyright 2011 Bas de Nooijer. All rights reserved. |
---|
| 4 | * |
---|
| 5 | * Redistribution and use in source and binary forms, with or without |
---|
| 6 | * modification, are permitted provided that the following conditions are met: |
---|
| 7 | * |
---|
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
| 9 | * this list of conditions and the following disclaimer. |
---|
| 10 | * |
---|
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
| 12 | * this listof conditions and the following disclaimer in the documentation |
---|
| 13 | * and/or other materials provided with the distribution. |
---|
| 14 | * |
---|
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" |
---|
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
| 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
| 25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
| 26 | * |
---|
| 27 | * The views and conclusions contained in the software and documentation are |
---|
| 28 | * those of the authors and should not be interpreted as representing official |
---|
| 29 | * policies, either expressed or implied, of the copyright holder. |
---|
| 30 | * |
---|
| 31 | * @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl> |
---|
| 32 | * @license http://github.com/basdenooijer/solarium/raw/master/COPYING |
---|
| 33 | * @link http://www.solarium-project.org/ |
---|
| 34 | * |
---|
| 35 | * @package Solarium |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * Prefetch plugin |
---|
| 40 | * |
---|
| 41 | * This plugin can be used to create an 'endless' iterator over a complete resultset. The iterator will take care of |
---|
| 42 | * fetching the data in sets (sequential prefetching). |
---|
| 43 | * |
---|
| 44 | * @package Solarium |
---|
| 45 | * @subpackage Plugin |
---|
| 46 | */ |
---|
| 47 | class Solarium_Plugin_PrefetchIterator extends Solarium_Plugin_Abstract implements Iterator, Countable |
---|
| 48 | { |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * Default options |
---|
| 52 | * |
---|
| 53 | * @var array |
---|
| 54 | */ |
---|
| 55 | protected $_options = array( |
---|
| 56 | 'prefetch' => 100, |
---|
| 57 | ); |
---|
| 58 | |
---|
| 59 | /** |
---|
| 60 | * Query instance to execute |
---|
| 61 | * |
---|
| 62 | * @var Solarium_Query_Select |
---|
| 63 | */ |
---|
| 64 | protected $_query; |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * Start position (offset) |
---|
| 68 | * |
---|
| 69 | * @var int |
---|
| 70 | */ |
---|
| 71 | protected $_start = 0; |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * Last resultset from the query instance |
---|
| 75 | * |
---|
| 76 | * @var Solarium_Result_Select |
---|
| 77 | */ |
---|
| 78 | protected $_result; |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | * Iterator position |
---|
| 82 | * |
---|
| 83 | * @var int |
---|
| 84 | */ |
---|
| 85 | protected $_position; |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * Documents from the last resultset |
---|
| 89 | * |
---|
| 90 | * @var array |
---|
| 91 | */ |
---|
| 92 | protected $_documents; |
---|
| 93 | |
---|
| 94 | /** |
---|
| 95 | * Set prefetch option |
---|
| 96 | * |
---|
| 97 | * @param integer $value |
---|
| 98 | * @return self Provides fluent interface |
---|
| 99 | */ |
---|
| 100 | public function setPrefetch($value) |
---|
| 101 | { |
---|
| 102 | return $this->_setOption('prefetch', $value); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | /** |
---|
| 106 | * Get prefetch option |
---|
| 107 | * |
---|
| 108 | * @return integer |
---|
| 109 | */ |
---|
| 110 | public function getPrefetch() |
---|
| 111 | { |
---|
| 112 | return $this->getOption('prefetch'); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | /** |
---|
| 116 | * Set query to use for prefetching |
---|
| 117 | * |
---|
| 118 | * @param Solarium_Query_Select $query |
---|
| 119 | * @return self Provides fluent interface |
---|
| 120 | */ |
---|
| 121 | public function setQuery($query) |
---|
| 122 | { |
---|
| 123 | $this->_query = $query; |
---|
| 124 | return $this; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | * Get the query object used |
---|
| 129 | * |
---|
| 130 | * @return Solarium_Query_Select |
---|
| 131 | */ |
---|
| 132 | public function getQuery() |
---|
| 133 | { |
---|
| 134 | return $this->_query; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | /** |
---|
| 138 | * Countable implementation |
---|
| 139 | * |
---|
| 140 | * @return int |
---|
| 141 | */ |
---|
| 142 | public function count() |
---|
| 143 | { |
---|
| 144 | // if no results are available yet, get them now |
---|
| 145 | if (null == $this->_result) $this->_fetchNext(); |
---|
| 146 | |
---|
| 147 | return $this->_result->getNumFound(); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /** |
---|
| 151 | * Iterator implementation |
---|
| 152 | */ |
---|
| 153 | function rewind() |
---|
| 154 | { |
---|
| 155 | $this->_position = 0; |
---|
| 156 | |
---|
| 157 | // this condition prevent useless re-fetching of data if a count is done before the iterator is used |
---|
| 158 | if ($this->_start !== $this->_options['prefetch']) { |
---|
| 159 | $this->_start = 0; |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | /** |
---|
| 164 | * Iterator implementation |
---|
| 165 | */ |
---|
| 166 | function current() |
---|
| 167 | { |
---|
| 168 | $adjustedIndex = $this->_position % $this->_options['prefetch']; |
---|
| 169 | return $this->_documents[$adjustedIndex]; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | /** |
---|
| 173 | * Iterator implementation |
---|
| 174 | * |
---|
| 175 | * @return int |
---|
| 176 | */ |
---|
| 177 | function key() |
---|
| 178 | { |
---|
| 179 | return $this->_position; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | /** |
---|
| 183 | * Iterator implementation |
---|
| 184 | */ |
---|
| 185 | function next() |
---|
| 186 | { |
---|
| 187 | ++$this->_position; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /** |
---|
| 191 | * Iterator implementation |
---|
| 192 | * |
---|
| 193 | * @return boolean |
---|
| 194 | */ |
---|
| 195 | function valid() |
---|
| 196 | { |
---|
| 197 | $adjustedIndex = $this->_position % $this->_options['prefetch']; |
---|
| 198 | |
---|
| 199 | // this condition prevent useless re-fetching of data if a count is done before the iterator is used |
---|
| 200 | if ($adjustedIndex == 0 && ($this->_position !== 0 || null == $this->_result)) { |
---|
| 201 | $this->_fetchNext(); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | return isset($this->_documents[$adjustedIndex]); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | /** |
---|
| 208 | * Fetch the next set of results |
---|
| 209 | * |
---|
| 210 | * @return void |
---|
| 211 | */ |
---|
| 212 | protected function _fetchNext() |
---|
| 213 | { |
---|
| 214 | $this->_query->setStart($this->_start)->setRows($this->getPrefetch()); |
---|
| 215 | $this->_result = $this->_client->execute($this->_query); |
---|
| 216 | $this->_documents = $this->_result->getDocuments(); |
---|
| 217 | $this->_start += $this->getPrefetch(); |
---|
| 218 | } |
---|
| 219 | } |
---|