[Checkins] SVN: zc.catalog/trunk/ Add sorting support from zope.index's FieldIndex.

Dan Korostelev nadako at gmail.com
Fri Feb 27 05:35:04 EST 2009


Log message for revision 97338:
  Add sorting support from zope.index's FieldIndex.

Changed:
  U   zc.catalog/trunk/CHANGES.txt
  U   zc.catalog/trunk/buildout.cfg
  U   zc.catalog/trunk/src/zc/catalog/index.py
  U   zc.catalog/trunk/src/zc/catalog/valueindex.txt

-=-
Modified: zc.catalog/trunk/CHANGES.txt
===================================================================
--- zc.catalog/trunk/CHANGES.txt	2009-02-27 10:33:42 UTC (rev 97337)
+++ zc.catalog/trunk/CHANGES.txt	2009-02-27 10:35:04 UTC (rev 97338)
@@ -8,7 +8,7 @@
 1.4.1 (unreleased)
 ------------------
 
-*
+* Add FieldIndex-like sorting support for the ValueIndex.
 
 
 1.4.0 (2009-02-07)

Modified: zc.catalog/trunk/buildout.cfg
===================================================================
--- zc.catalog/trunk/buildout.cfg	2009-02-27 10:33:42 UTC (rev 97337)
+++ zc.catalog/trunk/buildout.cfg	2009-02-27 10:35:04 UTC (rev 97338)
@@ -1,5 +1,5 @@
 [buildout]
-develop = .
+develop = . ../zope.index
 parts = test
 
 [test]

Modified: zc.catalog/trunk/src/zc/catalog/index.py
===================================================================
--- zc.catalog/trunk/src/zc/catalog/index.py	2009-02-27 10:33:42 UTC (rev 97337)
+++ zc.catalog/trunk/src/zc/catalog/index.py	2009-02-27 10:35:04 UTC (rev 97338)
@@ -27,6 +27,7 @@
 import zope.component.interfaces
 import zope.interface.common.idatetime
 import zope.index.interfaces
+from zope.index.field.sorting import SortingIndexMixin
 import zope.security.management
 from zope.publisher.interfaces import IRequest
 import zc.catalog.interfaces
@@ -150,10 +151,15 @@
         raise ValueError('may only pass a dict to apply')
     return query_type, query
 
-class ValueIndex(AbstractIndex):
+class ValueIndex(SortingIndexMixin, AbstractIndex):
 
     interface.implements(zc.catalog.interfaces.IValueIndex)
 
+    # attributes used by sorting mixin
+    _sorting_num_docs_attr = 'documentCount'        # Length object
+    _sorting_fwd_index_attr = 'values_to_documents' # forward BTree index
+    _sorting_rev_index_attr = 'documents_to_values' # reverse BTree index
+
     def _add_value(self, doc_id, added):
         values_to_documents = self.values_to_documents
         docs = values_to_documents.get(added)

Modified: zc.catalog/trunk/src/zc/catalog/valueindex.txt
===================================================================
--- zc.catalog/trunk/src/zc/catalog/valueindex.txt	2009-02-27 10:33:42 UTC (rev 97337)
+++ zc.catalog/trunk/src/zc/catalog/valueindex.txt	2009-02-27 10:35:04 UTC (rev 97338)
@@ -220,3 +220,40 @@
     True
     >>> index.containsValue('q')
     False
+
+Sorting
+-------
+
+Value indexes supports sorting, just like zope.index.field.FieldIndex.
+
+    >>> index.clear()
+
+    >>> index.index_doc(1, 9)
+    >>> index.index_doc(2, 8)
+    >>> index.index_doc(3, 7)
+    >>> index.index_doc(4, 6)
+    >>> index.index_doc(5, 5)
+    >>> index.index_doc(6, 4)
+    >>> index.index_doc(7, 3)
+    >>> index.index_doc(8, 2)
+    >>> index.index_doc(9, 1)
+
+    >>> list(index.sort([4, 2, 9, 7, 3, 1, 5]))
+    [9, 7, 5, 4, 3, 2, 1]
+
+We can also specify the ``reverse`` argument to reverse results:
+
+    >>> list(index.sort([4, 2, 9, 7, 3, 1, 5], reverse=True))
+    [1, 2, 3, 4, 5, 7, 9]
+
+And as per IIndexSort, we can limit results by specifying the ``limit``
+argument:
+
+    >>> list(index.sort([4, 2, 9, 7, 3, 1, 5], limit=3)) 
+    [9, 7, 5]
+
+If we pass an id that is not indexed by this index, it won't be included
+in the result.
+
+    >>> list(index.sort([2, 10]))
+    [2]



More information about the Checkins mailing list