Back to index

plone3  3.1.7
eval.py
Go to the documentation of this file.
00001 # Copyright (C) 2003-2006 by Dr. Dieter Maurer, Eichendorffstr. 23, D-66386 St. Ingbert, Germany
00002 #       $Id: eval.py,v 1.2 2006/06/25 19:11:24 dieter Exp $
00003 '''Query evaluation.
00004 
00005 Put into its own module to avoid cyclic module imports.
00006 '''
00007 from BTrees.IIBTree import IISet
00008 
00009 from Products.ZCatalog.Lazy import LazyCat, LazyMap
00010 
00011 from AdvancedQuery import _QueryContext, ISearch, IBTree, _notPassed
00012 from sorting import sort as _sort, normSortSpecs as _normSortSpecs
00013 
00014 def _eval(query, cat):
00015   '''evaluate *query* in the context of *cat* (a 'Products.ZCatalog.Catalog.Catalog').'''
00016   rs = query._eval(_QueryContext(cat))
00017   if isinstance(rs, ISearch):
00018     if hasattr(rs, 'asSet'): rs = rs.asSet()
00019     elif isinstance(rs, IBTree): rs = rs.getTree()
00020     else: hits = tuple(rs); rs = IISet(); rs.__setstate__((hits,))
00021   return rs
00022 
00023 def eval(catalog, query, sortSpecs=(), withSortValues=_notPassed):
00024   '''evaluate *query* for *catalog*; sort according to *sortSpecs*.
00025 
00026   *sortSpecs* is a sequence of sort specifications.
00027   
00028   A sort spec is either a ranking spec, an index name or a pair
00029   index name + sort direction ('asc/desc').
00030 
00031   If *withSortValues* is not passed, it is set to 'True' when *sortSpecs*
00032   contains a ranking spec; otherwise, it is set to 'False'.
00033 
00034   If *withSortValues*, the catalog brains 'data_record_score_' is
00035   abused to communicate the sort value (a tuple with one
00036   component per sort spec). 'data_record_normalized_score_' is
00037   set to 'None' in this case.
00038   '''
00039   cat = catalog._catalog
00040   rs = _eval(query, cat)
00041   if not rs: return LazyCat(())
00042   sortSpecs, withSortValues = _normSortSpecs(sortSpecs, withSortValues, cat)
00043   if sortSpecs or withSortValues: rs = _sort(rs, sortSpecs, withSortValues)
00044   if hasattr(rs, 'keys'): rs= rs.keys() # a TreeSet does not have '__getitem__'
00045   return LazyMap(cat.__getitem__, rs)