[Zope-Checkins] CVS: Zope/lib/python/Products/PluginIndexes/common - PluggableIndex.py:1.9 UnIndex.py:1.17

Andreas Jung andreas@andreas-jung.com
Thu, 23 Jan 2003 12:46:36 -0500


Update of /cvs-repository/Zope/lib/python/Products/PluginIndexes/common
In directory cvs.zope.org:/tmp/cvs-serv19547/lib/python/Products/PluginIndexes/common

Modified Files:
	PluggableIndex.py UnIndex.py 
Log Message:
Merging ajung-oneindex-multipleattributes-branch

This implements: http://lists.zope.org/pipermail/zope-coders/20002-November/002680.html



=== Zope/lib/python/Products/PluginIndexes/common/PluggableIndex.py 1.8 => 1.9 ===
--- Zope/lib/python/Products/PluginIndexes/common/PluggableIndex.py:1.8	Thu Dec  5 16:35:54 2002
+++ Zope/lib/python/Products/PluginIndexes/common/PluggableIndex.py	Thu Jan 23 12:46:33 2003
@@ -24,6 +24,11 @@
     def getEntryForObject(documentId, default=None):
         """Get all information contained for 'documentId'."""
 
+    def getIndexSourceNames():
+        """ return a sequence of attribute names that are indexed 
+            by the index. 
+        """
+
     def index_object(documentId, obj, threshold=None):
         """Index an object.
 


=== Zope/lib/python/Products/PluginIndexes/common/UnIndex.py 1.16 => 1.17 ===
--- Zope/lib/python/Products/PluginIndexes/common/UnIndex.py:1.16	Thu Dec  5 16:35:54 2002
+++ Zope/lib/python/Products/PluginIndexes/common/UnIndex.py	Thu Jan 23 12:46:33 2003
@@ -38,7 +38,7 @@
     """UnIndex object interface"""
 
 
-    def __init__(self, id, ignore_ex=None, call_methods=None):
+    def __init__(self, id, ignore_ex=None, call_methods=None, extra=None, caller=None):
         """Create an unindex
 
         UnIndexes are indexes that contain two index components, the
@@ -69,16 +69,27 @@
           You will also need to pass in an object in the index and
           uninded methods for this to work.
 
+          'extra' -- a record-style object that keeps additional 
+          index-related parameters
+
+          'caller' -- reference to the calling object (usually 
+          a (Z)Catalog instance
+
         """
 
         self.id = id
         self.ignore_ex=ignore_ex        # currently unimplimented
         self.call_methods=call_methods
 
-        # experimental code for specifing the operator
-        self.operators = ['or','and']
+        self.operators = ('or', 'and')
         self.useOperator = 'or'
 
+        # allow index to index multiple attributes
+        try: 
+            self.indexed_attrs = extra.indexed_attrs.split(',')
+            self.indexed_attrs = [ attr.strip() for attr in  self.indexed_attrs if attr ]
+        except: self.indexed_attrs = [ self.id ]
+        
         self.__len__=BTrees.Length.Length() # see __len__ method docstring
         self.clear()
 
@@ -214,13 +225,28 @@
                 indexRow=IITreeSet((indexRow, documentId))
                 self._index[entry] = indexRow
 
+
     def index_object(self, documentId, obj, threshold=None):
+        """ wrapper to handle indexing of multiple attributes """
+
+        # needed for backward compatibility
+        try: fields = self.indexed_attrs
+        except: fields  = [ self.id ]
+
+        res = 0
+        for attr in fields:
+            res += self._index_object(documentId, obj, threshold, attr) 
+
+        return res > 0 
+        
+
+    def _index_object(self, documentId, obj, threshold=None, attr=''):
         """ index and object 'obj' with integer id 'documentId'"""
         global _marker
         returnStatus = 0
 
         # First we need to see if there's anything interesting to look at
-        datum = self._get_object_datum(obj)
+        datum = self._get_object_datum(obj, attr)
 
         # We don't want to do anything that we don't have to here, so we'll
         # check to see if the new and existing information is the same.
@@ -244,12 +270,12 @@
 
         return returnStatus
 
-    def _get_object_datum(self,obj):
+    def _get_object_datum(self,obj, attr):
         # self.id is the name of the index, which is also the name of the
         # attribute we're interested in.  If the attribute is callable,
         # we'll do so.
         try:
-            datum = getattr(obj, self.id)
+            datum = getattr(obj, attr)
             if callable(datum):
                 datum = datum()
         except AttributeError:
@@ -389,6 +415,17 @@
         else:
             return 0
 
+    def getIndexSourceNames(self):
+        """ return name of indexed attributes """
+        return (self.id, )
+
+    def getIndexSourceNames(self):
+        """ return sequence of indexed attributes """
+        
+        try:
+            return self.indexed_attrs 
+        except:
+            return [ self.id ]
 
     def uniqueValues(self, name=None, withLengths=0):
         """\