[Zope-Checkins] SVN: Zope/trunk/src/Products/ZCatalog/ Sort indexes according to ILimitedResultIndex support. Indexes without the support are queried first, so the indexes supporting the feature already get a limited result set.

Hanno Schlichting hannosch at hannosch.eu
Sun Aug 1 13:02:11 EDT 2010


Log message for revision 115341:
  Sort indexes according to ILimitedResultIndex support. Indexes without the support are queried first, so the indexes supporting the feature already get a limited result set.
  

Changed:
  U   Zope/trunk/src/Products/ZCatalog/Catalog.py
  U   Zope/trunk/src/Products/ZCatalog/tests/test_catalog.py

-=-
Modified: Zope/trunk/src/Products/ZCatalog/Catalog.py
===================================================================
--- Zope/trunk/src/Products/ZCatalog/Catalog.py	2010-08-01 16:51:14 UTC (rev 115340)
+++ Zope/trunk/src/Products/ZCatalog/Catalog.py	2010-08-01 17:02:11 UTC (rev 115341)
@@ -471,7 +471,13 @@
     def _sorted_search_indexes(self, query):
         # Simple implementation doing no ordering.
         query_keys = query.keys()
-        return [i for i in self.indexes.keys() if i in query_keys]
+        order = []
+        for name, index in self.indexes.items():
+            if name not in query_keys:
+                continue
+            order.append((ILimitedResultIndex.providedBy(index), name))
+        order.sort()
+        return order
 
     def search(self, query, sort_index=None, reverse=0, limit=None, merge=1):
         """Iterate through the indexes, applying the query to each one. If
@@ -495,21 +501,15 @@
 
         # Canonicalize the request into a sensible query before passing it on
         query = self.make_query(query)
-        query_keys = query.keys()
-
         cr = self.getCatalogReport(query)
         cr.start()
 
-        for i in self._sorted_search_indexes(query):
+        for limit_result, i in self._sorted_search_indexes(query):
             index = self.getIndex(i)
             _apply_index = getattr(index, "_apply_index", None)
             if _apply_index is None:
                 continue
 
-            limit_result = False
-            if ILimitedResultIndex.providedBy(index):
-                limit_result = True
-
             cr.split(i)
             if limit_result:
                 r = _apply_index(query, rs)

Modified: Zope/trunk/src/Products/ZCatalog/tests/test_catalog.py
===================================================================
--- Zope/trunk/src/Products/ZCatalog/tests/test_catalog.py	2010-08-01 16:51:14 UTC (rev 115340)
+++ Zope/trunk/src/Products/ZCatalog/tests/test_catalog.py	2010-08-01 17:02:11 UTC (rev 115341)
@@ -291,13 +291,20 @@
 
     def test_sorted_search_indexes_one(self):
         result = self._catalog._sorted_search_indexes({'att1': 'a'})
-        self.assertEquals(result, ['att1'])
+        self.assertEquals(result, [(True, 'att1')])
 
     def test_sorted_search_indexes_many(self):
         query = {'att1': 'a', 'att2': 'b', 'num': 1}
         result = self._catalog._sorted_search_indexes(query)
-        self.assertEquals(set(result), set(['att1', 'att2', 'num']))
+        indexes = [r[1] for r in result]
+        self.assertEquals(set(indexes), set(['att1', 'att2', 'num']))
 
+    def test_sorted_search_indexes_priority(self):
+        # att2 and col2 don't support ILimitedResultIndex, att1 does
+        query = {'att1': 'a', 'att2': 'b', 'col2': 'c'}
+        result = self._catalog._sorted_search_indexes(query)
+        self.assertEquals(result.index((True, 'att1')), 2)
+
     # search
     # sortResults
     # _get_sort_attr



More information about the Zope-Checkins mailing list