[Checkins] SVN: zc.catalog/trunk/ Add sorting indexes support for the NormalizationWrapper

Dan Korostelev nadako at gmail.com
Fri Feb 27 06:57:47 EST 2009


Log message for revision 97345:
  Add sorting indexes support for the NormalizationWrapper

Changed:
  U   zc.catalog/trunk/CHANGES.txt
  U   zc.catalog/trunk/src/zc/catalog/catalogindex.py
  U   zc.catalog/trunk/src/zc/catalog/index.py
  U   zc.catalog/trunk/src/zc/catalog/normalizedindex.txt

-=-
Modified: zc.catalog/trunk/CHANGES.txt
===================================================================
--- zc.catalog/trunk/CHANGES.txt	2009-02-27 11:00:20 UTC (rev 97344)
+++ zc.catalog/trunk/CHANGES.txt	2009-02-27 11:57:47 UTC (rev 97345)
@@ -10,7 +10,9 @@
 
 * Add FieldIndex-like sorting support for the ValueIndex.
 
+* Add sorting indexes support for the NormalizationWrapper.
 
+
 1.4.0 (2009-02-07)
 ------------------
 

Modified: zc.catalog/trunk/src/zc/catalog/catalogindex.py
===================================================================
--- zc.catalog/trunk/src/zc/catalog/catalogindex.py	2009-02-27 11:00:20 UTC (rev 97344)
+++ zc.catalog/trunk/src/zc/catalog/catalogindex.py	2009-02-27 11:57:47 UTC (rev 97345)
@@ -19,6 +19,7 @@
 
 import zope.catalog.attribute
 import zope.container.contained
+import zope.index.interfaces
 
 import zc.catalog.index
 import zc.catalog.interfaces
@@ -50,14 +51,15 @@
 
 @zope.interface.implementer(
     zope.interface.implementedBy(NormalizationWrapper),
-    zc.catalog.interfaces.IValueIndex)
+    zc.catalog.interfaces.IValueIndex,
+    zope.index.interfaces.IIndexSort)
 def DateTimeValueIndex(
     field_name=None, interface=None, field_callable=False,
     resolution=2): # hour; good for per-day searches
     ix = NormalizationWrapper(
         field_name, interface, field_callable, zc.catalog.index.ValueIndex(),
         zc.catalog.index.DateTimeNormalizer(resolution), False)
-    zope.interface.directlyProvides(ix, zc.catalog.interfaces.IValueIndex)
+    zope.interface.alsoProvides(ix, zc.catalog.interfaces.IValueIndex)
     return ix
 
 @zope.interface.implementer(
@@ -69,5 +71,5 @@
     ix = NormalizationWrapper(
         field_name, interface, field_callable, zc.catalog.index.SetIndex(),
         zc.catalog.index.DateTimeNormalizer(resolution), True)
-    zope.interface.directlyProvides(ix, zc.catalog.interfaces.ISetIndex)
+    zope.interface.alsoProvides(ix, zc.catalog.interfaces.ISetIndex)
     return ix

Modified: zc.catalog/trunk/src/zc/catalog/index.py
===================================================================
--- zc.catalog/trunk/src/zc/catalog/index.py	2009-02-27 11:00:20 UTC (rev 97344)
+++ zc.catalog/trunk/src/zc/catalog/index.py	2009-02-27 11:57:47 UTC (rev 97345)
@@ -360,6 +360,8 @@
 
     def __init__(self, index, normalizer, collection_index=False):
         self.index = index
+        if zope.index.interfaces.IIndexSort.providedBy(index):
+            zope.interface.alsoProvides(self, zope.index.interfaces.IIndexSort)
         self.normalizer = normalizer
         self.collection_index = collection_index
 
@@ -420,6 +422,10 @@
     def ids(self):
         return self.index.ids()
 
+    @property
+    def sort(self):
+        # delegate upstream or raise AttributeError
+        return self.index.sort
 
 class CallableWrapper(persistent.Persistent):
 
@@ -527,11 +533,12 @@
 
 @interface.implementer(
     zope.interface.implementedBy(NormalizationWrapper),
+    zope.index.interfaces.IIndexSort,
     zc.catalog.interfaces.IValueIndex)
 def DateTimeValueIndex(resolution=2): # 2 == minute; note that hour is good
     # for timezone-aware per-day searches
     ix = NormalizationWrapper(ValueIndex(), DateTimeNormalizer(resolution))
-    interface.directlyProvides(ix, zc.catalog.interfaces.IValueIndex)
+    interface.alsoProvides(ix, zc.catalog.interfaces.IValueIndex)
     return ix
 
 @interface.implementer(
@@ -540,5 +547,5 @@
 def DateTimeSetIndex(resolution=2): # 2 == minute; note that hour is good
     # for timezone-aware per-day searches
     ix = NormalizationWrapper(SetIndex(), DateTimeNormalizer(resolution), True)
-    interface.directlyProvides(ix, zc.catalog.interfaces.ISetIndex)    
+    interface.alsoProvides(ix, zc.catalog.interfaces.ISetIndex)    
     return ix

Modified: zc.catalog/trunk/src/zc/catalog/normalizedindex.txt
===================================================================
--- zc.catalog/trunk/src/zc/catalog/normalizedindex.txt	2009-02-27 11:00:20 UTC (rev 97344)
+++ zc.catalog/trunk/src/zc/catalog/normalizedindex.txt	2009-02-27 11:57:47 UTC (rev 97345)
@@ -322,3 +322,36 @@
      datetime.datetime(2005, 7, 17, 23, 21, ...<...Eastern...>)]
 
     >>> zope.security.management.endInteraction() # TODO put in tests tearDown
+
+Sorting
+-------
+
+The normalization wrapper provides the zope.index.interfaces.IIndexSort
+interface if its upstream index provides it. For example, the
+DateTimeValueIndex will provide IIndexSort, because ValueIndex provides
+sorting. It will also delegate the ``sort`` method to the value index.
+
+    >>> from zc.catalog.index import DateTimeValueIndex
+    >>> from zope.index.interfaces import IIndexSort
+
+    >>> ix = DateTimeValueIndex()
+    >>> IIndexSort.providedBy(ix.index)
+    True
+    >>> IIndexSort.providedBy(ix)
+    True
+    >>> ix.sort.im_self is ix.index
+    True
+
+But it won't work for indexes that doesn't do sorting, for example
+DateTimeSetIndex.
+
+    >>> ix = DateTimeSetIndex()
+    >>> IIndexSort.providedBy(ix.index)
+    False
+    >>> IIndexSort.providedBy(ix)
+    False
+    >>> ix.sort
+    Traceback (most recent call last):
+    ...
+    AttributeError: 'SetIndex' object has no attribute 'sort'
+   
\ No newline at end of file



More information about the Checkins mailing list