[Zope-Checkins] CVS: Zope2 - PathIndex.py:1.1.2.5

andreas@serenade.digicool.com andreas@serenade.digicool.com
Tue, 8 May 2001 08:34:18 -0400


Update of /cvs-repository/Zope2/lib/python/SearchIndex
In directory serenade.digicool.com:/tmp/cvs-serv7615/lib/python/SearchIndex

Modified Files:
      Tag: ajung-pathindex
	PathIndex.py 
Log Message:
update



--- Updated File PathIndex.py in package Zope2 --
--- PathIndex.py	2001/05/07 15:20:30	1.1.2.4
+++ PathIndex.py	2001/05/08 12:34:17	1.1.2.5
@@ -83,8 +83,8 @@
 # 
 ##############################################################################
 
-from BTrees.OOBTree import OOBTree
 from BTrees.IOBTree import IOBTree
+from BTrees.OOBTree import OOBTree
 from BTrees.IIBTree import IISet
 from Persistence import Persistent
 from Acquisition import Implicit
@@ -93,28 +93,43 @@
 import re,string
 
 class PathIndex(Persistent,Implicit):
+    """ A path index stores all path components of the physical 
+    path of an object: 
+
+    - we split the physical path at "/"
 
+    - all slices [0:n] of the result list will be stored in the index
+      as key and the documentId as value
+    """
+
     meta_type = 'Path Index'
 
     def __init__(self,id):
         self.id = id
         self.clear()
 
+
     def clear(self):
+        """ clear everything """
         self._index = OOBTree()
         self._unindex = IOBTree()
 
 
     def insertEntry(self,k,v):
+        """ 
+        k is list of path components (generated by splitPath() )
+        v is the documentId
+        """
 
-        if self._index.has_key(k):  self._index[k].append(v)
-        else:                       self._index[k] = [v]
+        if self._index.has_key(k):    self._index[k].append(v)
+        else:                         self._index[k] = [v]
 
         if self._unindex.has_key(v):  self._unindex[v].append(k)
         else:                         self._unindex[v] = [k]
 
 
     def index_object(self, documentId, obj ,threshold):
+        """ hook for (Z)Catalog """
 
         try:
             path = obj.getPhysicalPath()
@@ -125,6 +140,9 @@
 
         comps = self.splitPath(path,obj)
 
+        if obj.meta_type != 'Folder':
+            comps = comps[:-1]
+
         for i in range(1,len(comps)+1):
             comp = comps[:i] 
             self.insertEntry( comp,documentId)
@@ -133,6 +151,7 @@
     
 
     def unindex_object(self,id):
+        """ hook for (Z)Catalog """
 
         for k in self._unindex[id]:
             self._index[k].remove(id)
@@ -143,6 +162,10 @@
 
 
     def splitPath(self,path,obj=None):
+        """ split physical path of object. If the object has
+        as function splitPath() we use this user-defined function
+        to split the path
+        """
 
         if hasattr(obj,"splitPath"):
             comps = obj.splitPath(path)
@@ -153,10 +176,16 @@
 
 
     def search(self,path,level=0):
+        """
+        path is a list of path components to be searched
+        level>=0  starts searching at the given level
+        level==-1 searches over all levels
+        """       
 
         dict = {}
         comps = self.splitPath(path)
 
+
         if level >=0:
             for k in self._index.keys():
                 s =  k[level:len(comps)+1]
@@ -173,41 +202,61 @@
     
         return dict.keys()
 
+
     def __len__(self):
         return len(self._index)
 
 
     def keys(self):   
+        """ return list of all path components """
         keys = []
         for k in self._index.keys(): keys.append(k)
         return keys
 
+
     def values(self):   
         values = []
         for k in self._index.values(): values.append(k)
         return values
 
+
     def items(self):   
+        """ mapping path components : documentIds """
+
         items = []
         for k in self._index.items(): items.append(k)
         return items
 
-    def _apply_index(self, request, cid=''): 
 
+    def _apply_index(self, request, cid=''): 
+        """ hook for (Z)Catalog
+        request   mapping type (usually {"path": "..." }
+                  additionaly a parameter "path_level" might be passed
+                  to specify the level (see search())
+
+        cid      ???
+        """
+           
         if request.has_key(self.id):
             keys = request[self.id]
         else:
             return None
 
+        level = request.get("path_level",-1)
+
         if type(keys) is StringType:
             if not keys or not string.strip(keys):
                 return None
             keys = [keys]
-            
 
         res = IISet()
+
         for k in keys:
-            rows = self.search(k,-1)
+
+            if k[0]=="/": level = 0
+
+            rows = self.search(k,level)
+
             for r in rows:
                 res.insert(r)