/
query.py
635 lines (481 loc) · 17.3 KB
/
query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
##############################################################################
#
# Copyright (c) 2005-2009 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Basic query implementation
This module contains an IQuery utility implementation, basic query term
implementations and concrete term implementations for zope.catalog indexes.
$Id$
"""
import itertools
import logging
import os
import time
from BTrees.IFBTree import IFSet
from BTrees.IFBTree import weightedIntersection
from BTrees.IFBTree import multiunion, difference, intersection
from zope.cachedescriptors.property import Lazy
from zope.catalog.field import IFieldIndex
from zope.catalog.interfaces import ICatalog
from zope.catalog.text import ITextIndex
from zope.component import getUtility, getSiteManager, IComponentLookup
from zope.interface import implements
from zope.intid.interfaces import IIntIds
from zope.index.interfaces import IIndexSort
from zope.index.text.parsetree import ParseError
from zope.location.location import located, LocationProxy
from hurry.query import interfaces
import transaction
import threading
logger = logging.getLogger('hurry.query')
HURRY_QUERY_TIMING = False
if 'HURRY_QUERY_TIMING' in os.environ:
try:
HURRY_QUERY_TIMING = float(os.environ['HURRY_QUERY_TIMING'])
except (ValueError, TypeError):
pass
class Cache(threading.local):
implements(transaction.interfaces.IDataManager)
def __init__(self, manager):
self._manager = manager
self.reset()
def sortKey(self):
return 'A' * 26
def use(self, context):
if not self._joined:
self._joined = True
transaction = self._manager.get()
transaction.join(self)
if context is not self._context:
# The context changed, reset the cache as we might access
# different indexes.
self.cache = {}
self._context = context
return self.cache
def tpc_begin(self, transaction):
pass
def tpc_vote(self, transaction):
pass
def tpc_finish(self, transaction):
self.reset()
def tpc_abort(self, transaction):
self.reset()
def abort(self, transaction):
self.reset()
def commit(self, transaction):
pass
def reset(self):
self._joined = False
self._context = None
self.cache = {}
transaction_cache = Cache(transaction.manager)
class Locator(object):
def __init__(self, container, get):
self.container = container
self.get = get
def __call__(self, oid):
contained = self.get(oid)
return located(
LocationProxy(contained), self.container, contained.__name__)
class Results(object):
implements(interfaces.IResults)
def __init__(self, context, all_results, selected_results,
wrapper=None, locate_to=None):
self.context = context
self.locate_to = locate_to
self.wrapper = wrapper
self.__all = all_results
self.__selected = selected_results
@Lazy
def get(self):
get = getUtility(IIntIds, '', self.context).getObject
if self.wrapper is not None:
get = (lambda get: lambda id: self.wrapper(get(id)))(get)
if self.locate_to is not None:
return Locator(self.locate_to, get)
return get
@property
def total(self):
return len(self.__all)
@property
def count(self):
return len(self.__selected)
def first(self):
for uid in self.__selected:
return self.get(uid)
def __len__(self):
return len(self.__selected)
def __iter__(self):
for uid in self.__selected:
yield self.get(uid)
class NoResults(object):
implements(interfaces.IResults)
count = 0
total = 0
def first(self):
return None
def __len__(self):
return 0
def __iter__(self):
return iter([])
no_results = NoResults()
class Timing(object):
def __init__(self, key=None, order=0):
self.key = key
self.start = time.clock()
self.start_order = order
self.end = None
self.end_order = None
def done(self, order=0):
self.end = time.clock()
self.end_order = order
@property
def total(self):
if self.end is not None:
return self.end - self.start
return None
class TimingAwareCache(object):
def __init__(self, cache):
self.cache = cache
self.timing = {}
self.count = 0
self.post = None
def start_post(self):
self.post = Timing()
def end_post(self):
self.post.done()
def __setitem__(self, key, value):
self.cache[key] = value
timing = self.timing.get(key)
if timing is not None:
timing.done(self.count)
self.count += 1
def get(self, key):
value = self.cache.get(key)
if value is None:
self.timing[key] = Timing(key, self.count)
self.count += 1
return value
def report(self, over=0):
all_timing = sorted(self.timing.values(), key=lambda t: t.start_order)
if not len(all_timing):
return
total_post = 0 if self.post is None else self.post.total
total_terms = all_timing[0].total
if (total_terms + total_post) < over:
return
indent = 0
order = [all_timing[0].end_order]
logger.info(
'Catalog query toke {:.4f}s for terms, {:.4f}s to finish.'.format(
total_terms, total_post))
for timing in all_timing:
if timing.start_order < order[-1]:
indent += 4
order.append(timing.end_order)
logger.info(
'{} {:.4f}s: {}.'.format(
' ' * indent, timing.total, str(timing.key)))
if timing.end_order > order[-1]:
indent -= 4
order.pop()
class Query(object):
implements(interfaces.IQuery)
def searchResults(
self, query, context=None, sort_field=None, limit=None,
reverse=False, start=0, caching=False, timing=HURRY_QUERY_TIMING,
wrapper=None, locate_to=None):
if context is None:
context = getSiteManager()
else:
context = IComponentLookup(context)
if caching is True:
cache = transaction_cache.use(context)
elif caching is False:
cache = {}
else:
# A custom cache object was injected, use it.
cache = caching
timer = None
if timing is not False:
timer = cache = TimingAwareCache(cache)
all_results = query.cached_apply(cache, context)
if not all_results:
if timer is not None:
timer.report(over=timing)
return no_results
if timer is not None:
timer.start_post()
is_iterator = False
if sort_field is not None:
# Like in zope.catalog's searchResults we require the given
# index to sort on to provide IIndexSort. We bail out if
# the index does not.
if not IIndexSort.providedBy(sort_field):
assert isinstance(sort_field, tuple) and len(sort_field) == 2
catalog_name, index_name = sort_field
catalog = getUtility(ICatalog, catalog_name, context)
sort_field = catalog[index_name]
if not IIndexSort.providedBy(sort_field):
raise ValueError(
'Index {} in catalog {} does not support '
'sorting.'.format(index_name, catalog_name))
sort_limit = None
if limit:
sort_limit = limit
if start:
sort_limit += start
selected_results = sort_field.sort(
all_results,
limit=sort_limit,
reverse=reverse)
if start:
selected_results = itertools.islice(
selected_results, start, None)
is_iterator = True
else:
# There's no sort_field given. We still allow to reverse
# and/or limit the resultset. This mimics zope.catalog's
# searchResults semantics.
selected_results = all_results
if reverse:
selected_results = reversed(selected_results)
is_iterator = True
if limit or start:
slice_end = limit and start + limit or None
selected_results = itertools.islice(
selected_results, start, slice_end)
is_iterator = True
if is_iterator:
selected_results = list(selected_results)
if timer is not None:
timer.end_post()
timer.report(over=timing)
return Results(
context, all_results, selected_results, wrapper, locate_to)
class Term(object):
implements(interfaces.ITerm)
def key(self, context=None):
raise NotImplementedError()
def apply(self, cache, context=None):
raise NotImplementedError()
def cached_apply(self, cache, context=None):
try:
key = self.key(context)
except NotImplementedError:
return self.apply(cache, context)
cached = cache.get(key)
if cached is not None:
return cached
result = self.apply(cache, context)
cache[key] = result
return result
def __and__(self, other):
return And(self, other)
def __rand__(self, other):
return And(other, self)
def __or__(self, other):
return Or(self, other)
def __ror__(self, other):
return Or(other, self)
def __invert__(self):
return Not(self)
class And(Term):
def __init__(self, *terms, **kwargs):
self.terms = terms
self.weighted = kwargs.get('weigthed', False)
def apply(self, cache, context=None):
results = []
for term in self.terms:
result = term.cached_apply(cache, context)
if not result:
# Empty results
return result
results.append(result)
if len(results) == 0:
return IFSet()
if len(results) == 1:
return results[0]
# Sort results to have the smallest set first to optimize the
# set operation.
results.sort(key=lambda r: len(r))
result = results.pop(0)
for r in results:
if self.weighted:
_, result = weightedIntersection(result, r)
else:
result = intersection(result, r)
if not result:
# Empty results
return result
return result
def key(self, context=None):
return ('and',) + tuple(term.key(context) for term in self.terms)
class Or(Term):
def __init__(self, *terms):
self.terms = terms
def apply(self, cache, context=None):
results = []
for term in self.terms:
result = term.cached_apply(cache, context)
# empty results
if not result:
continue
results.append(result)
if len(results) == 0:
return IFSet()
if len(results) == 1:
return results[0]
return multiunion(results)
def key(self, context=None):
return ('or',) + tuple(term.key(context) for term in self.terms)
class Difference(Term):
def __init__(self, *terms):
self.terms = terms
def apply(self, cache, context=None):
results = []
for index, term in enumerate(self.terms):
result = term.cached_apply(cache, context)
# If we do not have any results for the first index, just
# return an empty set and stop here.
if not result:
if not index:
return IFSet()
continue
results.append(result)
result = results.pop(0)
for other in results:
result = difference(result, other)
if not result:
# Empty results
return result
return result
def key(self, context=None):
return ('difference',) + tuple(
term.key(context) for term in self.terms)
class Not(Term):
# XXX This term will load all the intids of your application
# resulting in major and heavy performance issues. It is advised
# not to use it.
def __init__(self, term):
self.term = term
def apply(self, cache, context=None):
return difference(self._all(), self.term.cached_apply(cache, context))
def _all(self):
return IFSet(uid for uid in getUtility(IIntIds))
def key(self, context=None):
return ('not', self.term.key(context))
class Objects(Term):
def __init__(self, objects):
self.objects = objects
self._ids = None
def ids(self, context=None):
if self._ids is None:
get_uid = getUtility(IIntIds, '', context).getId
self._ids = tuple(get_uid(o) for o in self.objects)
return self._ids
def apply(self, cache, context=None):
return IFSet(self.ids(context))
def key(self, context=None):
return ('objects', self.ids(context))
class IndexTerm(Term):
def __init__(self, (catalog_name, index_name)):
self.catalog_name = catalog_name
self.index_name = index_name
def getIndex(self, context):
catalog = getUtility(ICatalog, self.catalog_name, context)
index = catalog[self.index_name]
return index
class Text(IndexTerm):
def __init__(self, index_id, text):
super(Text, self).__init__(index_id)
self.text = text
def getIndex(self, context):
index = super(Text, self).getIndex(context)
assert ITextIndex.providedBy(index)
return index
def apply(self, cache, context=None):
index = self.getIndex(context)
try:
return index.apply(self.text)
except ParseError:
logger.error(
'search text "{}" yielded a ParseError'.format(self.text))
return IFSet()
def key(self, context=None):
return ('text', self.catalog_name, self.index_name, self.text)
class FieldTerm(IndexTerm):
def getIndex(self, context):
index = super(FieldTerm, self).getIndex(context)
assert IFieldIndex.providedBy(index)
return index
class Eq(FieldTerm):
def __init__(self, index_id, value):
assert value is not None
super(Eq, self).__init__(index_id)
self.value = value
def apply(self, cache, context=None):
return self.getIndex(context).apply((self.value, self.value))
def key(self, context=None):
return ('equal', self.catalog_name, self.index_name, self.value)
class NotEq(FieldTerm):
def __init__(self, index_id, value):
super(NotEq, self).__init__(index_id)
self.value = value
def apply(self, cache, context=None):
index = self.getIndex(context)
values = index.apply((None, None))
matches = index.apply((self.value, self.value))
return difference(values, matches)
def key(self, context=None):
return ('not equal', self.catalog_name, self.index_name, self.value)
class All(FieldTerm):
def apply(self, cache, context=None):
return self.getIndex(context).apply((None, None))
def key(self, context=None):
return ('all', self.catalog_name, self.index_name)
class Between(FieldTerm):
def __init__(self, index_id,
minimum=None, maximum=None):
super(Between, self).__init__(index_id)
self.options = (minimum, maximum)
def apply(self, cache, context=None):
return self.getIndex(context).apply(self.options)
def key(self, context=None):
return ('between', self.catalog_name, self.index_name, self.options)
class Ge(Between):
def __init__(self, index_id, min_value):
super(Ge, self).__init__(index_id, min_value, None)
class Le(Between):
def __init__(self, index_id, max_value):
super(Le, self).__init__(index_id, None, max_value)
class In(FieldTerm):
def __init__(self, index_id, values):
assert None not in values
super(In, self).__init__(index_id)
self.values = tuple(values)
def apply(self, cache, context=None):
results = []
index = self.getIndex(context)
for value in self.values:
r = index.apply((value, value))
# empty results
if not r:
continue
results.append(r)
if len(results) == 0:
return IFSet()
if len(results) == 1:
return results[0]
return multiunion(results)
def key(self, context=None):
return ('in', self.catalog_name, self.index_name, self.values)