[Zope-CVS] CVS: Products/RecentItemsIndex - index.py:1.3 test.py:1.3

Casey Duncan casey at zope.com
Wed Aug 4 16:41:17 EDT 2004


Update of /cvs-repository/Products/RecentItemsIndex
In directory cvs.zope.org:/tmp/cvs-serv17239

Modified Files:
	index.py test.py 
Log Message:
Add support for merging recent items queries across catalogs


=== Products/RecentItemsIndex/index.py 1.2 => 1.3 ===
--- Products/RecentItemsIndex/index.py:1.2	Mon Jul 19 17:16:56 2004
+++ Products/RecentItemsIndex/index.py	Wed Aug  4 16:41:17 2004
@@ -111,7 +111,7 @@
             counts[value] = len(items)
         return counts
         
-    def query(self, value, limit=None):
+    def query(self, value, limit=None, merge=1):
         """Return a lazy sequence of catalog brains like a catalog search
         that coorespond to the most recent items for the value(s) given. 
         The result are returned in order, newest first. An integer value
@@ -122,26 +122,41 @@
         catalog = aq_parent(aq_inner(self))
         if isinstance(value, (types.TupleType, types.ListType)):
             # Query for multiple values
-            itemrids = []
+            results = []
             for fieldval in value:
                 try:
                     itempairs = self._value2items[fieldval].keys()
                 except KeyError:
                     pass
                 else:
-                    itemrids.extend(itempairs)
-            itemrids.sort()
-            itemrids = [rid for date, rid in itemrids]
+                    results.extend(itempairs)
+            results.sort()
+            if merge:
+                results = [rid for date, rid in results]
+            else:
+                # Create triples expected by mergeResults()
+                results = [(date, rid, catalog.__getitem__)
+                           for date, rid in results]
         else:
             # Query for single value
             try:
-                itemrids = self._value2items[value].values()
+                items = self._value2items[value]                    
             except KeyError:
-                itemrids = []
-        itemrids.reverse()
+                results = []
+            else:
+                if merge:
+                    results = items.values()
+                else:
+                    # Create triples expected by mergeResults()
+                    results = [(date, rid, catalog.__getitem__)
+                               for date, rid in items.keys()]
+        results.reverse()
         if limit is not None:
-            itemrids = itemrids[:limit]
-        return LazyMap(catalog.__getitem__, itemrids, len(itemrids))
+            results = results[:limit]
+        if merge:
+            return LazyMap(catalog.__getitem__, results, len(results))
+        else:
+            return results
     
     ## Pluggable Index API ##
     


=== Products/RecentItemsIndex/test.py 1.2 => 1.3 ===
--- Products/RecentItemsIndex/test.py:1.2	Mon Jul 19 17:16:56 2004
+++ Products/RecentItemsIndex/test.py	Wed Aug  4 16:41:17 2004
@@ -256,6 +256,16 @@
         expected = expected[:3]
         self.assertEqual([doc.docid for doc in result], expected)
     
+    def test_query_no_merge(self):
+        docs = self.test_index_many()
+        top = self._get_top_docs(docs)
+        result = self.index.query('dooey', merge=0)
+        expected = [(date, docid, self.test.__getitem__) 
+                    for date, docid in top['dooey']]
+        expected.reverse()
+        for rrow, erow in zip(result, expected):
+           self.assertEqual(rrow[:2], erow[:2])        
+    
     def test_query_multiple_values(self):
         docs = self.test_index_many()
         top = self._get_top_docs(docs)
@@ -282,6 +292,17 @@
         expected = self.test_query_multiple_values()[:4]
         result = self.index.query(['huey', 'dooey'], limit=4)
         self.assertEqual([doc.docid for doc in result], expected)
+    
+    def test_query_multiple_no_merge(self):
+        docs = self.test_index_many()
+        top = self._get_top_docs(docs)
+        result = self.index.query(['dooey', 'huey'], merge=0)
+        expected = [(date, docid, self.test.__getitem__) 
+                    for date, docid in top['huey'] + top['dooey']]
+        expected.sort()
+        expected.reverse()
+        for rrow, erow in zip(result, expected):
+           self.assertEqual(rrow[:2], erow[:2]) 
         
     def test_apply_index(self):
         # _apply_index always returns none since recent items index



More information about the Zope-CVS mailing list