[Zope-CVS] CVS: Products/BTreeFolder2 - BTreeFolder2.py:1.5

Shane Hathaway shane@cvs.zope.org
Fri, 10 May 2002 10:52:15 -0400


Update of /cvs-repository/Products/BTreeFolder2
In directory cvs.zope.org:/tmp/cvs-serv19321

Modified Files:
	BTreeFolder2.py 
Log Message:
CMF needs the meta type index after all.  Added _mt_index and a test.


=== Products/BTreeFolder2/BTreeFolder2.py 1.4 => 1.5 ===
 from zLOG import LOG, ERROR, WARNING
 from Products.ZCatalog.Lazy import LazyMap
-#from ComputedAttribute import ComputedAttribute
 
 
 manage_addBTreeFolderForm = DTMLFile('folderAdd', globals())
@@ -81,12 +80,14 @@
     _tree = None      # OOBTree: { id -> object }
     _count = None     # A BTrees.Length
     _v_nextid = 0     # The next computed ID
+    _mt_index = None  # OOBTree: { meta_type -> OIBTree: { id -> 1 } }
     title = ''
 
 
     def __init__(self, id):
         self.id = id
         self._init()
+        self._mt_index = OOBTree()
 
 
     def _init(self):
@@ -98,6 +99,7 @@
         ids = source.objectIds()
         tree = self._tree
         count = self._count
+        mti = self._mt_index
         for name in ids:
             value = source._getOb(name, None)
             if value is not None:
@@ -107,6 +109,15 @@
                         % name)
                 tree[name] = aq_base(value)
                 count.change(1)
+                # Update the meta type index.
+                meta_type = getattr(value, 'meta_type', None)
+                if meta_type is None:
+                    continue
+                ids = mti.get(meta_type, None)
+                if ids is None:
+                    ids = OIBTree()
+                    mti[meta_type] = ids
+                ids[name] = 1
 
 
     def _getOb(self, id, default=_marker):
@@ -128,12 +139,28 @@
         tree = self._tree
         tree[id] = object
         self._count.change(1)
+        # Update the meta type index.
+        mti = self._mt_index
+        meta_type = getattr(object, 'meta_type', None)
+        if meta_type is not None:
+            ids = mti.get(meta_type, None)
+            if ids is None:
+                ids = OIBTree()
+                mti[meta_type] = ids
+            ids[id] = 1
 
 
     def _delOb(self, id):
         tree = self._tree
+        meta_type = getattr(tree[id], 'meta_type', None)
         del tree[id]
         self._count.change(-1)
+        # Update the meta type index.
+        if meta_type is not None:
+            mti = self._mt_index
+            ids = mti.get(meta_type, None)
+            if ids is not None and ids.has_key(id):
+                del ids[id]
 
 
     security.declareProtected(view_management_screens, 'getBatchObjectListing')
@@ -213,8 +240,18 @@
         # If 'spec' is specified, returns objects whose meta_type
         # matches 'spec'.
         if spec is not None:
-            raise RuntimeError, (
-                'BTreeFolder2 does not support lookups by meta_type.')
+            if isinstance(spec, StringType):
+                spec = [spec]
+            mti = self._mt_index
+            set = None
+            for meta_type in spec:
+                ids = mti.get(meta_type, None)
+                if ids is not None:
+                    set = union(set, ids)
+            if set is None:
+                return ()
+            else:
+                return set.keys()
         else:
             return self._tree.keys()