[Zope-Checkins] CVS: Zope/lib/python/Products/ZCatalog - Catalog.py:1.96 IZCatalog.py:1.2 ZCatalog.py:1.115

Casey Duncan casey@zope.com
Wed, 14 Aug 2002 15:10:15 -0400


Update of /cvs-repository/Zope/lib/python/Products/ZCatalog
In directory cvs.zope.org:/tmp/cvs-serv15813

Modified Files:
	Catalog.py IZCatalog.py ZCatalog.py 
Log Message:
Added new APIS:
  - Catalog.getIndex returns an acquisition wrapped index, use instead of 
    Catalog.indexes[...]
  - ZCatalog.getIndexObjects returns the list of index obs also acquisition
    wrapped. Its use is preferred over the previous index_objects method.

Changed Catalog code to utilize getIndex. This is mostly a neutral change except for in clear() which did not used to wrap before calling the index.
Help docs and interfaces updated to reflect the change.


=== Zope/lib/python/Products/ZCatalog/Catalog.py 1.95 => 1.96 ===
--- Zope/lib/python/Products/ZCatalog/Catalog.py:1.95	Wed Jul 31 13:05:58 2002
+++ Zope/lib/python/Products/ZCatalog/Catalog.py	Wed Aug 14 15:10:14 2002
@@ -81,8 +81,9 @@
         try: self.__len__.set(0)
         except AttributeError: self.__len__=BTrees.Length.Length()
 
-        for x in self.indexes.values():
-            x.clear()
+        for index in self.indexes.values():
+            if hasattr(index, '__of__'): index=index.__of__(self)
+            index.clear()
 
     def _convertBTrees(self, threshold=200):
 
@@ -252,9 +253,6 @@
         if not name:
             raise 'Invalid Index Name', 'Name of index is empty'
 
-        # this is currently a succesion of hacks.  Indexes should be
-        # pluggable and managable
-
         indexes = self.indexes
 
         if isinstance(index_type, types.StringType):
@@ -274,8 +272,11 @@
         indexes = self.indexes
         del indexes[name]
         self.indexes = indexes
-
-
+        
+    def getIndex(self, name):
+        """ get an index wrapped in the catalog """
+        return self.indexes[name].__of__(self)
+        
     # the cataloging API
 
     def catalogObject(self, object, uid, threshold=None,idxs=[]):
@@ -341,13 +342,8 @@
         if idxs==[]: use_indexes = self.indexes.keys()
         else:        use_indexes = idxs
 
-        for item in use_indexes:
-            x = self.indexes[item]
-            
-            ## tricky!  indexes need to acquire now, and because they
-            ## are in a standard dict __getattr__ isn't used, so
-            ## acquisition doesn't kick in, we must explicitly wrap!
-            x = x.__of__(self)
+        for name in use_indexes:
+            x = self.getIndex(name)
             if hasattr(x, 'index_object'):
                 blah = x.index_object(index, object, threshold)
                 total = total + blah
@@ -372,12 +368,12 @@
         data = self.data
         uids = self.uids
         paths = self.paths
-        indexes = self.indexes
+        indexes = self.indexes.keys()
         rid = uids.get(uid, None)
 
         if rid is not None:
-            for x in indexes.values():
-                x = x.__of__(self)
+            for name in indexes:
+                x = self.getIndex(name)
                 if hasattr(x, 'unindex_object'):
                     x.unindex_object(rid)
             del data[rid]
@@ -393,7 +389,7 @@
 
     def uniqueValuesFor(self, name):
         """ return unique values for FieldIndex name """
-        return self.indexes[name].uniqueValues()
+        return self.getIndex(name).uniqueValues()
 
     def hasuid(self, uid):
         """ return the rid if catalog contains an object with uid """
@@ -424,8 +420,8 @@
 
     def getIndexDataForRID(self, rid):
         result = {}
-        for (id, index) in self.indexes.items():
-            result[id] = index.__of__(self).getEntryForObject(rid, "")
+        for name in self.indexes.keys():
+            result[name] = self.getIndex(name).getEntryForObject(rid, "")
         return result
     
 ## This is the Catalog search engine. Most of the heavy lifting happens below
@@ -465,7 +461,7 @@
         if used is None:
             used = {}
         for i in self.indexes.keys():
-            index = self.indexes[i].__of__(self)
+            index = self.getIndex(i)
             _apply_index = getattr(index, "_apply_index", None)
             if _apply_index is None:
                 continue
@@ -633,6 +629,9 @@
 
 
     def searchResults(self, REQUEST=None, used=None, _merge=1, **kw):
+        if REQUEST is None and not kw:
+            # Try to acquire request if we get no args for bw compat
+            REQUEST = getattr(self, 'REQUEST', None)
         args = CatalogSearchArgumentsMap(REQUEST, kw)
         sort_index = self._getSortIndex(args)
         # Perform searches with indexes and sort_index


=== Zope/lib/python/Products/ZCatalog/IZCatalog.py 1.1 => 1.2 ===
--- Zope/lib/python/Products/ZCatalog/IZCatalog.py:1.1	Mon Jul 29 10:10:48 2002
+++ Zope/lib/python/Products/ZCatalog/IZCatalog.py	Wed Aug 14 15:10:14 2002
@@ -108,6 +108,13 @@
 
     def index_objects():
         """Returns a sequence of actual index objects.
+        
+        NOTE: This returns unwrapped indexes! You should probably use
+        getIndexObjects instead. Some indexes expect to be wrapped.
+        """
+        
+    def getIndexObjects():
+        """Returns a list of acquisition wrapped index objects
         """
 
     def searchResults(REQUEST=None, **kw):
@@ -154,8 +161,7 @@
         There are some rules to consider when querying this method:
 
             - an empty query mapping (or a bogus REQUEST) returns all
-              items in the
-            catalog.
+              items in the catalog.
 
             - results from a query involving only field/keyword
               indexes, e.g.  {'id':'foo'} and no 'sort_on' will be


=== Zope/lib/python/Products/ZCatalog/ZCatalog.py 1.114 => 1.115 ===
--- Zope/lib/python/Products/ZCatalog/ZCatalog.py:1.114	Wed Jul 31 13:04:18 2002
+++ Zope/lib/python/Products/ZCatalog/ZCatalog.py	Wed Aug 14 15:10:14 2002
@@ -133,7 +133,7 @@
 
         ('Search ZCatalog',
          ['searchResults', '__call__', 'uniqueValuesFor',
-          'getpath', 'schema', 'indexes', 'index_objects',
+          'getpath', 'schema', 'indexes', 'index_objects', 'getIndexObjects'
           'all_meta_types', 'valid_roles', 'resolve_url',
           'getobject'],
          ['Anonymous', 'Manager']), 
@@ -561,7 +561,14 @@
         return self._catalog.indexes.keys()
 
     def index_objects(self):
+        # This method returns unwrapped indexes!
+        # You should probably use getIndexObjects instead
         return self._catalog.indexes.values()
+        
+    def getIndexObjects(self):
+        # Return a list of wrapped(!) indexes
+        catalog = self._catalog
+        return [index.__of__(catalog) for index in catalog.indexes.values()]
 
     def _searchable_arguments(self):
         r = {}