/usr/share/pyshared/hurry/query/query.py is in python-hurry.query 1.1.0-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ##############################################################################
#
# 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: query.py 114320 2010-07-08 08:26:20Z janwijbrand $
"""
from BTrees.IFBTree import weightedIntersection, union, difference, IFBTree
from zope.catalog.catalog import ResultSet
from zope.catalog.field import IFieldIndex
from zope.catalog.interfaces import ICatalog
from zope.catalog.text import ITextIndex
from zope.component import getUtility
from zope.interface import implements
from zope.intid.interfaces import IIntIds
from zope.index.interfaces import IIndexSort
from hurry.query import interfaces
# XXX look into using multiunion for performance?
class Query(object):
implements(interfaces.IQuery)
def searchResults(
self, query, context=None, sort_field=None, limit=None, reverse=False):
results = query.apply(context)
if results is None:
return
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.
catalog_name, index_name = sort_field
catalog = getUtility(ICatalog, catalog_name, context)
index = catalog[index_name]
if not IIndexSort.providedBy(index):
raise ValueError(
'Index %s in catalog %s does not support '
'sorting.' % (index_name, catalog_name))
results = list(index.sort(results, limit=limit, reverse=reverse))
else:
# There's no sort_field given. We still allow to reverse
# and/or limit the resultset. This mimics zope.catalog's
# searchResults semantics.
if reverse or limit:
results = list(results)
if reverse:
results.reverse()
if limit:
del results[limit:]
uidutil = getUtility(IIntIds, '', context)
return ResultSet(results, uidutil)
class Term(object):
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):
self.terms = terms
def apply(self, context=None):
results = []
for term in self.terms:
r = term.apply(context)
if not r:
# empty results
return r
results.append((len(r), r))
if not results:
# no applicable terms at all
# XXX should this be possible?
return IFBTree()
results.sort()
_, result = results.pop(0)
for _, r in results:
_, result = weightedIntersection(result, r)
return result
class Or(Term):
def __init__(self, *terms):
self.terms = terms
def apply(self, context=None):
results = []
for term in self.terms:
r = term.apply(context)
# empty results
if not r:
continue
results.append(r)
if not results:
# no applicable terms at all
# XXX should this be possible?
return IFBTree()
result = results.pop(0)
for r in results:
result = union(result, r)
return result
class Not(Term):
def __init__(self, term):
self.term = term
def apply(self, context=None):
return difference(self._all(), self.term.apply(context))
def _all(self):
# XXX may not work well/be efficient with extentcatalog
# XXX not very efficient in general, better to use internal
# IntIds datastructure but that would break abstraction..
intids = getUtility(IIntIds)
result = IFBTree()
for uid in intids:
result.insert(uid, 0)
return result
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, context=None):
index = self.getIndex(context)
return index.apply(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, context=None):
return self.getIndex(context).apply((self.value, self.value))
class NotEq(FieldTerm):
def __init__(self, index_id, not_value):
super(NotEq, self).__init__(index_id)
self.not_value = not_value
def apply(self, context=None):
index = self.getIndex(context)
all = index.apply((None, None))
r = index.apply((self.not_value, self.not_value))
return difference(all, r)
class Between(FieldTerm):
def __init__(self, index_id, min_value, max_value):
super(Between, self).__init__(index_id)
self.min_value = min_value
self.max_value = max_value
def apply(self, context=None):
return self.getIndex(context).apply((self.min_value, self.max_value))
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 = values
def apply(self, 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 not results:
# no applicable terms at all
return IFBTree()
result = results.pop(0)
for r in results:
result = union(result, r)
return result
|