[Checkins] SVN: Products.ZCatalog/trunk/ Added a floor and ceiling value to the date range index. Values outside the specified range will be interpreted the same way as passing `None`, i.e. `since the beginning of time` and `until the end of it`.

Hanno Schlichting hannosch at hannosch.eu
Fri Apr 8 08:19:26 EDT 2011


Log message for revision 121349:
  Added a floor and ceiling value to the date range index. Values outside the specified range will be interpreted the same way as passing `None`, i.e. `since the beginning of time` and `until the end of it`.
  

Changed:
  U   Products.ZCatalog/trunk/CHANGES.txt
  U   Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/DateRangeIndex.py
  U   Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/dtml/addDateRangeIndex.dtml
  U   Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/dtml/manageDateRangeIndex.dtml
  U   Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/tests/test_DateRangeIndex.py

-=-
Modified: Products.ZCatalog/trunk/CHANGES.txt
===================================================================
--- Products.ZCatalog/trunk/CHANGES.txt	2011-04-08 10:39:42 UTC (rev 121348)
+++ Products.ZCatalog/trunk/CHANGES.txt	2011-04-08 12:19:26 UTC (rev 121349)
@@ -4,6 +4,12 @@
 2.13.9 (unreleased)
 -------------------
 
+- Added a floor and ceiling value to the date range index. Values outside the
+  specified range will be interpreted the same way as passing `None`, i.e.
+  `since the beginning of time` and `until the end of it`. This allows the
+  index to apply its optimizations, while objects with values outside this
+  range can still be stored in a normal date index, which omits explicitly
+  passed in `None` values.
 
 2.13.8 (2011-04-01)
 -------------------

Modified: Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/DateRangeIndex.py
===================================================================
--- Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/DateRangeIndex.py	2011-04-08 10:39:42 UTC (rev 121348)
+++ Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/DateRangeIndex.py	2011-04-08 12:19:26 UTC (rev 121349)
@@ -84,15 +84,22 @@
 
     since_field = until_field = None
 
+    # int(DateTime('1000/1/1 0:00 GMT-12').millis() / 1000 / 60)
+    floor_value = -510162480
+    # int(DateTime('2499/12/31 0:00 GMT+12').millis() / 1000 / 60)
+    ceiling_value = 278751600
+
     def __init__(self, id, since_field=None, until_field=None,
-            caller=None, extra=None):
+            caller=None, extra=None, floor_value=None, ceiling_value=None):
 
         if extra:
             since_field = extra.since_field
             until_field = extra.until_field
+            floor_value = getattr(extra, 'floor_value', None)
+            ceiling_value = getattr(extra, 'ceiling_value', None)
 
         self._setId(id)
-        self._edit(since_field, until_field)
+        self._edit(since_field, until_field, floor_value, ceiling_value)
         self.clear()
 
     security.declareProtected(view, 'getSinceField')
@@ -107,24 +114,39 @@
         """
         return self._until_field
 
+    security.declareProtected(view, 'getFloorValue')
+    def getFloorValue(self):
+        """"""
+        return self.floor_value
+
+    security.declareProtected(view, 'getCeilingValue')
+    def getCeilingValue(self):
+        """"""
+        return self.ceiling_value
+
     manage_indexProperties = DTMLFile('manageDateRangeIndex', _dtmldir)
 
     security.declareProtected(manage_zcatalog_indexes, 'manage_edit')
-    def manage_edit(self, since_field, until_field, REQUEST):
+    def manage_edit(self, since_field, until_field, floor_value,
+                    ceiling_value, REQUEST):
         """
         """
-        self._edit(since_field, until_field)
+        self._edit(since_field, until_field, floor_value, ceiling_value)
         REQUEST['RESPONSE'].redirect('%s/manage_main'
                                      '?manage_tabs_message=Updated'
                                      % REQUEST.get('URL2'))
 
     security.declarePrivate('_edit')
-    def _edit(self, since_field, until_field):
+    def _edit(self, since_field, until_field, floor_value=None,
+              ceiling_value=None):
+        """Update the fields used to compute the range.
         """
-            Update the fields used to compute the range.
-        """
         self._since_field = since_field
         self._until_field = until_field
+        if floor_value is not None:
+            self.floor_value = int(floor_value)
+        if ceiling_value is not None:
+            self.ceiling_value = int(ceiling_value)
 
     security.declareProtected(manage_zcatalog_indexes, 'clear')
     def clear(self):
@@ -269,7 +291,7 @@
             if catalog is not None:
                 key = self._cache_key(catalog)
                 cache = REQUEST.get(key, None)
-                tid = term / 10
+                tid = isinstance(term, int) and term / 10 or 'None'
                 if resultset is None:
                     cachekey = '_daterangeindex_%s_%s' % (iid, tid)
                 else:
@@ -378,12 +400,18 @@
             value = dt_obj.millis() / 1000 / 60 # flatten to minutes
         elif isinstance(value, DateTime):
             value = value.millis() / 1000 / 60 # flatten to minutes
-        result = int(value)
-        if result > MAX32:
+        if value > MAX32 or value < -MAX32:
             # t_val must be integer fitting in the 32bit range
             raise OverflowError('%s is not within the range of dates allowed'
                                 'by a DateRangeIndex' % value)
-        return result
+        value = int(value)
+        # handle values outside our specified range
+        import pdb; pdb.set_trace( )
+        if value > self.ceiling_value:
+            return None
+        elif value < self.floor_value:
+            return None
+        return value
 
 InitializeClass(DateRangeIndex)
 

Modified: Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/dtml/addDateRangeIndex.dtml
===================================================================
--- Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/dtml/addDateRangeIndex.dtml	2011-04-08 10:39:42 UTC (rev 121348)
+++ Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/dtml/addDateRangeIndex.dtml	2011-04-08 12:19:26 UTC (rev 121349)
@@ -42,6 +42,26 @@
    <input type="text" name="extra.until_field:record" size="40" />
   </td>
  </tr>
+ <tr>
+  <td align="left" valign="top">
+  <div class="form-label"> 
+  Floor value
+  </div>
+  </td>
+  <td align="left" valign="top">
+   <input type="text" name="extra.floor_value:record" size="15" />
+  </td>
+ </tr>
+ <tr>
+  <td align="left" valign="top">
+  <div class="form-label"> 
+  Ceiling value
+  </div>
+  </td>
+  <td align="left" valign="top">
+   <input type="text" name="extra.ceiling_value:record" size="15" />
+  </td>
+ </tr>
   <tr>
     <td align="left" valign="top">
     </td>

Modified: Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/dtml/manageDateRangeIndex.dtml
===================================================================
--- Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/dtml/manageDateRangeIndex.dtml	2011-04-08 10:39:42 UTC (rev 121348)
+++ Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/dtml/manageDateRangeIndex.dtml	2011-04-08 12:19:26 UTC (rev 121349)
@@ -22,8 +22,8 @@
   <td align="left" valign="top">
    <input name="since_field" value="&dtml-getSinceField;" size="40" />
   </td>
- </tr>
-
+</tr>
+<tr>
   <td align="left" valign="top">
   <div class="form-label">
   Until field
@@ -31,9 +31,26 @@
   <td align="left" valign="top">
    <input name="until_field" value="&dtml-getUntilField;" />
   </td>
- </tr>
-
+</tr>
 <tr>
+  <td align="left" valign="top">
+  <div class="form-label">
+  Floor value
+  </td>
+  <td align="left" valign="top">
+   <input name="floor_value" value="&dtml-getFloorValue;" />
+  </td>
+</tr>
+<tr>
+  <td align="left" valign="top">
+  <div class="form-label">
+  Ceiling value
+  </td>
+  <td align="left" valign="top">
+   <input name="ceiling_value" value="&dtml-getCeilingValue;" />
+  </td>
+</tr>
+<tr>
   <td></td>
   <td align="left" valign="top">
   <div class="form-element">

Modified: Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/tests/test_DateRangeIndex.py
===================================================================
--- Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/tests/test_DateRangeIndex.py	2011-04-08 10:39:42 UTC (rev 121348)
+++ Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/tests/test_DateRangeIndex.py	2011-04-08 12:19:26 UTC (rev 121349)
@@ -113,14 +113,28 @@
                 self.assertEqual(index.getEntryForObject(result), match.datum())
 
     def test_longdates(self):
-        self.assertRaises(OverflowError, self._badlong)
+        too_large = long(2**31)
+        too_small = - long(2**31)
+        index = self._makeOne('work', 'start', 'stop')
+        bad = Dummy('bad', too_large, too_large)
+        self.assertRaises(OverflowError, index.index_object, 0, bad)
+        bad = Dummy('bad', too_small, too_small)
+        self.assertRaises(OverflowError, index.index_object, 0, bad)
 
-    def _badlong(self):
-        import sys
+    def test_floor_date(self):
         index = self._makeOne('work', 'start', 'stop')
-        bad = Dummy('bad', long(sys.maxint) + 1, long(sys.maxint) + 1)
+        floor = index.floor_value - 1
+        bad = Dummy('bad', floor, None)
         index.index_object(0, bad)
+        self.assertTrue(0 in index._always.keys())
 
+    def test_ceiling_date(self):
+        index = self._makeOne('work', 'start', 'stop')
+        ceiling = index.ceiling_value + 1
+        bad = Dummy('bad', None, ceiling)
+        index.index_object(1, bad)
+        self.assertTrue(1 in index._always.keys())
+
     def test_datetime(self):
         from datetime import datetime
         from DateTime.DateTime import DateTime



More information about the checkins mailing list