MCPcopy Index your code
hub / github.com/django-haystack/django-haystack / _fill_cache

Method _fill_cache

haystack/query.py:216–274  ·  view source on GitHub ↗
(self, start, end, **kwargs)

Source from the content-addressed store, hash-verified

214 return model._default_manager.in_bulk(pks)
215
216 def _fill_cache(self, start, end, **kwargs):
217 # Tell the query where to start from and how many we'd like.
218 self.query._reset()
219
220 if start is None:
221 start = 0
222
223 query_start = start
224 query_start += self._ignored_result_count
225 query_end = end
226 if query_end is not None:
227 query_end += self._ignored_result_count
228
229 self.query.set_limits(query_start, query_end)
230 results = self.query.get_results(**kwargs)
231
232 if results is None or len(results) == 0:
233 # trim missing stuff from the result cache
234 self._result_cache = self._result_cache[:start]
235 return False
236
237 # Setup the full cache now that we know how many results there are.
238 # We need the ``None``s as placeholders to know what parts of the
239 # cache we have/haven't filled.
240 # Using ``None`` like this takes up very little memory. In testing,
241 # an array of 100,000 ``None``s consumed less than .5 Mb, which ought
242 # to be an acceptable loss for consistent and more efficient caching.
243 if len(self._result_cache) == 0:
244 self._result_cache = [None] * self.query.get_count()
245
246 fill_start, fill_end = start, end
247 if fill_end is None:
248 fill_end = self.query.get_count()
249 cache_start = fill_start
250
251 while True:
252 to_cache = self.post_process_results(results)
253
254 # Assign by slice.
255 self._result_cache[cache_start : cache_start + len(to_cache)] = to_cache
256
257 if None in self._result_cache[start:end]:
258 fill_start = fill_end
259 fill_end += ITERATOR_LOAD_PER_QUERY
260 cache_start += len(to_cache)
261
262 # Tell the query where to start from and how many we'd like.
263 self.query._reset()
264 self.query.set_limits(fill_start, fill_end)
265 results = self.query.get_results()
266
267 if results is None or len(results) == 0:
268 # No more results. Trim missing stuff from the result cache
269 self._result_cache = self._result_cache[:cache_start]
270 break
271 else:
272 break
273

Callers 15

_manual_iterMethod · 0.95
__getitem__Method · 0.95
_fill_cacheMethod · 0.45
_fill_cacheMethod · 0.45
test_load_allMethod · 0.45
test_fill_cacheMethod · 0.45
test_fill_cacheMethod · 0.45
test_fill_cacheMethod · 0.45

Calls 5

post_process_resultsMethod · 0.95
_resetMethod · 0.80
set_limitsMethod · 0.80
get_countMethod · 0.80
get_resultsMethod · 0.45

Tested by 11

test_load_allMethod · 0.36
test_fill_cacheMethod · 0.36
test_fill_cacheMethod · 0.36
test_fill_cacheMethod · 0.36
test_fill_cacheMethod · 0.36
test_fill_cacheMethod · 0.36