[Checkins] SVN: hurry.query/trunk/ add optional argument to searchResults() so that a context can be specified in which the catalog will be looked up

Kevin Teague kevin at bud.ca
Thu Dec 3 19:50:00 EST 2009


Log message for revision 106202:
  add optional argument to searchResults() so that a context can be specified in which the catalog will be looked up

Changed:
  U   hurry.query/trunk/CHANGES.txt
  U   hurry.query/trunk/src/hurry/query/query.py
  U   hurry.query/trunk/src/hurry/query/query.txt
  U   hurry.query/trunk/src/hurry/query/set.py
  U   hurry.query/trunk/src/hurry/query/value.py

-=-
Modified: hurry.query/trunk/CHANGES.txt
===================================================================
--- hurry.query/trunk/CHANGES.txt	2009-12-03 21:53:00 UTC (rev 106201)
+++ hurry.query/trunk/CHANGES.txt	2009-12-04 00:50:00 UTC (rev 106202)
@@ -4,9 +4,10 @@
 1.0.1 (unreleased)
 ------------------
 
-- Nothing changed yet.
+* Allow the searchResults method of a Query object to take an additional
+  optional context argument. This context will determine which catalog
+  the search is performed on.
 
-
 1.0.0 (2009-11-30)
 ------------------
 

Modified: hurry.query/trunk/src/hurry/query/query.py
===================================================================
--- hurry.query/trunk/src/hurry/query/query.py	2009-12-03 21:53:00 UTC (rev 106201)
+++ hurry.query/trunk/src/hurry/query/query.py	2009-12-04 00:50:00 UTC (rev 106202)
@@ -34,10 +34,10 @@
 class Query(object):
     implements(interfaces.IQuery)
 
-    def searchResults(self, query):
-        results = query.apply()
+    def searchResults(self, query, context=None):
+        results = query.apply(context)
         if results is not None:
-            uidutil = getUtility(IIntIds)
+            uidutil = getUtility(IIntIds, '', context)
             results = ResultSet(results, uidutil)
         return results
 
@@ -65,10 +65,10 @@
     def __init__(self, *terms):
         self.terms = terms
 
-    def apply(self):
+    def apply(self, context=None):
         results = []
         for term in self.terms:
-            r = term.apply()
+            r = term.apply(context)
             if not r:
                 # empty results
                 return r
@@ -92,10 +92,10 @@
     def __init__(self, *terms):
         self.terms = terms
 
-    def apply(self):
+    def apply(self, context=None):
         results = []
         for term in self.terms:
-            r = term.apply()
+            r = term.apply(context)
             # empty results
             if not r:
                 continue
@@ -117,8 +117,8 @@
     def __init__(self, term):
         self.term = term
 
-    def apply(self):
-        return difference(self._all(), self.term.apply())
+    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
@@ -137,8 +137,8 @@
         self.catalog_name = catalog_name
         self.index_name = index_name
 
-    def getIndex(self):
-        catalog = getUtility(ICatalog, self.catalog_name)
+    def getIndex(self, context):
+        catalog = getUtility(ICatalog, self.catalog_name, context)
         index = catalog[self.index_name]
         return index
 
@@ -149,20 +149,20 @@
         super(Text, self).__init__(index_id)
         self.text = text
 
-    def getIndex(self):
-        index = super(Text, self).getIndex()
+    def getIndex(self, context):
+        index = super(Text, self).getIndex(context)
         assert ITextIndex.providedBy(index)
         return index
 
-    def apply(self):
-        index = self.getIndex()
+    def apply(self, context=None):
+        index = self.getIndex(context)
         return index.apply(self.text)
 
 
 class FieldTerm(IndexTerm):
 
-    def getIndex(self):
-        index = super(FieldTerm, self).getIndex()
+    def getIndex(self, context):
+        index = super(FieldTerm, self).getIndex(context)
         assert IFieldIndex.providedBy(index)
         return index
 
@@ -174,8 +174,8 @@
         super(Eq, self).__init__(index_id)
         self.value = value
 
-    def apply(self):
-        return self.getIndex().apply((self.value, self.value))
+    def apply(self, context=None):
+        return self.getIndex(context).apply((self.value, self.value))
 
 
 class NotEq(FieldTerm):
@@ -184,8 +184,8 @@
         super(NotEq, self).__init__(index_id)
         self.not_value = not_value
 
-    def apply(self):
-        index = self.getIndex()
+    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)
@@ -198,8 +198,8 @@
         self.min_value = min_value
         self.max_value = max_value
 
-    def apply(self):
-        return self.getIndex().apply((self.min_value, self.max_value))
+    def apply(self, context=None):
+        return self.getIndex(context).apply((self.min_value, self.max_value))
 
 
 class Ge(Between):
@@ -221,9 +221,9 @@
         super(In, self).__init__(index_id)
         self.values = values
 
-    def apply(self):
+    def apply(self, context=None):
         results = []
-        index = self.getIndex()
+        index = self.getIndex(context)
         for value in self.values:
             r = index.apply((value, value))
             # empty results

Modified: hurry.query/trunk/src/hurry/query/query.txt
===================================================================
--- hurry.query/trunk/src/hurry/query/query.txt	2009-12-03 21:53:00 UTC (rev 106201)
+++ hurry.query/trunk/src/hurry/query/query.txt	2009-12-04 00:50:00 UTC (rev 106202)
@@ -113,9 +113,9 @@
 
   >>> from zope.component import getUtility
   >>> from hurry.query.interfaces import IQuery
-  >>> def displayQuery(q):
+  >>> def displayQuery(q, context=None):
   ...     query = getUtility(IQuery)
-  ...     r = query.searchResults(q)
+  ...     r = query.searchResults(q, context)
   ...     return [e.id for e in sorted(list(r))]
 
 FieldIndex Queries
@@ -397,3 +397,45 @@
 
   >>> displayQuery(value.ExtentNone(f1, extent))
   [7, 8, 9]
+
+
+Querying different indexes
+--------------------------
+
+It's possible to specify the context when creating a query. This context
+determines which index will be searched.
+
+First setup a second registry and second catalog and populate it.
+
+  >>> catalog2 = Catalog()
+  >>> from zope.component.registry import Components
+  >>> import zope.component.interfaces
+  >>> import zope.interface
+  >>> intid1 = DummyIntId()
+  >>> class MockSite(object):
+  ...     zope.interface.implements(zope.component.interfaces.IComponentLookup)
+  ...     def __init__(self):
+  ...         self.registry = Components('components')
+  ...     def queryUtility(self, interface, name='', default=None):
+  ...         if name == '': return intid1
+  ...         else: return catalog2
+  ...     def getSiteManager(self):
+  ...         return self.registry
+  >>> from zope.component.hooks import setSite
+  >>> site1 = MockSite()
+  >>> setSite(site1)
+  >>> catalog2['f1'] = FieldIndex('f1', IContent)
+  >>> content = [
+  ... Content(1,'A'),
+  ... Content(2,'B'),]
+  >>> for entry in content:
+  ...     catalog2.index_doc(intid1.register(entry), entry)
+
+Now we can query this catalog by specifying the context:
+
+  >>> query = getUtility(IQuery)
+  >>> displayQuery(Eq(f1, 'A'), context=site1)
+  [1]
+
+  >>> displayQuery(In(f1, ['A', 'B']), context=site1)
+  [1, 2]

Modified: hurry.query/trunk/src/hurry/query/set.py
===================================================================
--- hurry.query/trunk/src/hurry/query/set.py	2009-12-03 21:53:00 UTC (rev 106201)
+++ hurry.query/trunk/src/hurry/query/set.py	2009-12-04 00:50:00 UTC (rev 106202)
@@ -22,8 +22,8 @@
 
 class SetTerm(query.IndexTerm):
 
-    def getIndex(self):
-        index = super(SetTerm, self).getIndex()
+    def getIndex(self, context):
+        index = super(SetTerm, self).getIndex(context)
         assert ISetIndex.providedBy(index)
         return index
 
@@ -34,8 +34,8 @@
         super(AnyOf, self).__init__(index_id)
         self.values = values
 
-    def apply(self):
-        return self.getIndex().apply({'any_of': self.values})
+    def apply(self, context=None):
+        return self.getIndex(context).apply({'any_of': self.values})
 
 
 class AllOf(SetTerm):
@@ -44,8 +44,8 @@
         super(AllOf, self).__init__(index_id)
         self.values = values
 
-    def apply(self):
-        return self.getIndex().apply({'all_of': self.values})
+    def apply(self, context=None):
+        return self.getIndex(context).apply({'all_of': self.values})
 
 
 class SetBetween(SetTerm):
@@ -56,8 +56,8 @@
         super(SetBetween, self).__init__(index_id)
         self.tuple = (minimum, maximum, include_minimum, include_maximum)
 
-    def apply(self):
-        return self.getIndex().apply({'between': self.tuple})
+    def apply(self, context=None):
+        return self.getIndex(context).apply({'between': self.tuple})
 
 
 class ExtentAny(SetTerm):
@@ -67,8 +67,8 @@
         super(Any, self).__init__(index_id)
         self.extent = extent
 
-    def apply(self):
-        return self.getIndex().apply({'any': self.extent})
+    def apply(self, context=None):
+        return self.getIndex(context).apply({'any': self.extent})
 
 
 class ExtentNone(SetTerm):
@@ -78,5 +78,5 @@
         super(None, self).__init__(index_id)
         self.extent = extent
 
-    def apply(self):
-        return self.getIndex().apply({'none': self.extent})
+    def apply(self, context=None):
+        return self.getIndex(context).apply({'none': self.extent})

Modified: hurry.query/trunk/src/hurry/query/value.py
===================================================================
--- hurry.query/trunk/src/hurry/query/value.py	2009-12-03 21:53:00 UTC (rev 106201)
+++ hurry.query/trunk/src/hurry/query/value.py	2009-12-04 00:50:00 UTC (rev 106202)
@@ -22,8 +22,8 @@
 
 class ValueTerm(query.IndexTerm):
 
-    def getIndex(self):
-        index = super(ValueTerm, self).getIndex()
+    def getIndex(self, context):
+        index = super(ValueTerm, self).getIndex(context)
         assert IValueIndex.providedBy(index)
         return index
 
@@ -35,8 +35,8 @@
         super(Eq, self).__init__(index_id)
         self.value = value
 
-    def apply(self):
-        return self.getIndex().apply({'any_of': (self.value,)})
+    def apply(self, context=None):
+        return self.getIndex(context).apply({'any_of': (self.value,)})
 
 
 class NotEq(ValueTerm):
@@ -45,8 +45,8 @@
         super(NotEq, self).__init__(index_id)
         self.not_value = not_value
 
-    def apply(self):
-        index = self.getIndex()
+    def apply(self, context=None):
+        index = self.getIndex(context)
         values = list(index.values())
         # the remove method produces a value error when the value to
         # be removed is not in the list in the first place.  Having a
@@ -69,8 +69,8 @@
         self.exclude_min = exclude_min
         self.exclude_max = exclude_max
 
-    def apply(self):
-        return self.getIndex().apply(
+    def apply(self, context=None):
+        return self.getIndex(context).apply(
             {'between': (self.min_value, self.max_value,
                          self.exclude_min, self.exclude_max)})
 
@@ -94,8 +94,8 @@
         super(In, self).__init__(index_id)
         self.values = values
 
-    def apply(self):
-        return self.getIndex().apply({'any_of': self.values})
+    def apply(self, context=None):
+        return self.getIndex(context).apply({'any_of': self.values})
 
 
 class ExtentAny(ValueTerm):
@@ -105,8 +105,8 @@
         super(ExtentAny, self).__init__(index_id)
         self.extent = extent
 
-    def apply(self):
-        return self.getIndex().apply({'any': self.extent})
+    def apply(self, context=None):
+        return self.getIndex(context).apply({'any': self.extent})
 
 
 class ExtentNone(ValueTerm):
@@ -116,5 +116,5 @@
         super(ExtentNone, self).__init__(index_id)
         self.extent = extent
 
-    def apply(self):
-        return self.getIndex().apply({'none': self.extent})
+    def apply(self, context=None):
+        return self.getIndex(context).apply({'none': self.extent})



More information about the checkins mailing list