[Checkins] SVN: hurry.query/trunk/src/hurry/query/query. allow to limit and/or reverse the search result set even if no sort_field is given. this mimics zope.catalog searchResults semantics.

Jan-Wijbrand Kolman janwijbrand at gmail.com
Thu Jul 8 04:26:20 EDT 2010


Log message for revision 114320:
  allow to limit and/or reverse the search result set even if no sort_field is given. this mimics zope.catalog searchResults semantics.

Changed:
  U   hurry.query/trunk/src/hurry/query/query.py
  U   hurry.query/trunk/src/hurry/query/query.txt

-=-
Modified: hurry.query/trunk/src/hurry/query/query.py
===================================================================
--- hurry.query/trunk/src/hurry/query/query.py	2010-07-08 07:46:28 UTC (rev 114319)
+++ hurry.query/trunk/src/hurry/query/query.py	2010-07-08 08:26:20 UTC (rev 114320)
@@ -42,6 +42,9 @@
             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]
@@ -50,9 +53,17 @@
                     'Index %s in catalog %s does not support '
                     'sorting.' % (index_name, catalog_name))
             results = list(index.sort(results, limit=limit, reverse=reverse))
-        # Note: in case no sort_field is provided, the resultset order
-        # is undefined. As such limiting and reverse does not have any
-        # practical meaning.
+        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)
 

Modified: hurry.query/trunk/src/hurry/query/query.txt
===================================================================
--- hurry.query/trunk/src/hurry/query/query.txt	2010-07-08 07:46:28 UTC (rev 114319)
+++ hurry.query/trunk/src/hurry/query/query.txt	2010-07-08 08:26:20 UTC (rev 114320)
@@ -474,7 +474,6 @@
   ...     r = query.searchResults(q, context, **kw)
   ...     return [e.id for e in r]
 
-
 Without using sorting in the query itself, the resultset has an undefined
 order. We "manually" sort the results here to have something testable.
 
@@ -515,3 +514,16 @@
   Traceback (most recent call last):
   ...
   ValueError: Index t in catalog catalog1 does not support sorting.
+
+The resultset can still be reversed and limited even if there's no sort_field
+given (Note that the actual order of the result set when not using explicit
+sorting is not defined. In this test it is assumed that the natural order of
+the tested index is deterministic enough to be used as a proper test).
+
+  >>> f1 = ('catalog1', 'f1')
+  >>> displayResult(Eq(f1, 'a'), limit=2)
+  [1, 2]
+
+  >>> f1 = ('catalog1', 'f1')
+  >>> displayResult(Eq(f1, 'a'), limit=2, reverse=True)
+  [6, 2]



More information about the checkins mailing list