[Checkins] SVN: zc.sharing/trunk/src/zc/sharing/ make index work with BTrees family

Gary Poster gary at zope.com
Tue May 1 00:07:19 EDT 2007


Log message for revision 74953:
  make index work with BTrees family

Changed:
  U   zc.sharing/trunk/src/zc/sharing/CHANGES.txt
  U   zc.sharing/trunk/src/zc/sharing/index.py
  U   zc.sharing/trunk/src/zc/sharing/index.txt
  U   zc.sharing/trunk/src/zc/sharing/tests.py

-=-
Modified: zc.sharing/trunk/src/zc/sharing/CHANGES.txt
===================================================================
--- zc.sharing/trunk/src/zc/sharing/CHANGES.txt	2007-05-01 03:43:10 UTC (rev 74952)
+++ zc.sharing/trunk/src/zc/sharing/CHANGES.txt	2007-05-01 04:07:18 UTC (rev 74953)
@@ -10,6 +10,8 @@
   zope.app.publication.zopepublication.ZopePublication.beforeTraverse
   (now it looks up the global auth, and before it used an import).
 
+- Fix index to use BTree families
+
 1.1
 ===
 

Modified: zc.sharing/trunk/src/zc/sharing/index.py
===================================================================
--- zc.sharing/trunk/src/zc/sharing/index.py	2007-05-01 03:43:10 UTC (rev 74952)
+++ zc.sharing/trunk/src/zc/sharing/index.py	2007-05-01 04:07:18 UTC (rev 74953)
@@ -17,9 +17,7 @@
 $Id$
 """
 
-import BTrees.IFBTree
-import BTrees.OOBTree
-import BTrees.IOBTree
+import BTrees
 import persistent
 
 from zope import component, interface
@@ -37,10 +35,14 @@
     interface.implements(zope.app.catalog.interfaces.ICatalogIndex,
                          zope.index.interfaces.IStatistics,
                          )
-        
-    def __init__(self, privilege_id):
+
+    family = BTrees.family32
+
+    def __init__(self, privilege_id, family=None):
         self.privilege_id = privilege_id
         self.privileges = 1 << privilege_id
+        if family is not None:
+            self.family = family
         self.clear()
     
     def index_doc(self, doc_id, value):
@@ -58,13 +60,13 @@
             if self.privileges & sharing.getBinaryPrivileges(principal_id):
                 docs = principal_documents.get(principal_id)
                 if docs is None:
-                    docs = BTrees.IFBTree.IFTreeSet()
+                    docs = self.family.IF.TreeSet()
                     principal_documents[principal_id] = docs
                 docs.insert(doc_id)
 
                 principals = document_principals.get(doc_id)
                 if principals is None:
-                    principals = BTrees.OOBTree.OOTreeSet()
+                    principals = self.family.OO.TreeSet()
                     document_principals[doc_id] = principals
                 principals.insert(principal_id)
 
@@ -82,8 +84,8 @@
         del self.document_principals[doc_id]
 
     def clear(self):
-        self.principal_documents = BTrees.OOBTree.OOBTree()
-        self.document_principals = BTrees.IOBTree.IOBTree()
+        self.principal_documents = self.family.OO.BTree()
+        self.document_principals = self.family.IO.BTree()
 
     def apply(self, principal):
         principal_documents = self.principal_documents
@@ -94,10 +96,10 @@
         policy._findGroupsFor(principal, getPrincipal, groups)
 
         for gid in groups:
-            result = BTrees.IFBTree.union(result, principal_documents.get(gid))
+            result = self.family.IF.union(result, principal_documents.get(gid))
 
         if result is None:
-            result = BTrees.IFBTree.IFSet()
+            result = self.family.IF.Set()
 
         return result
 

Modified: zc.sharing/trunk/src/zc/sharing/index.txt
===================================================================
--- zc.sharing/trunk/src/zc/sharing/index.txt	2007-05-01 03:43:10 UTC (rev 74952)
+++ zc.sharing/trunk/src/zc/sharing/index.txt	2007-05-01 04:07:18 UTC (rev 74953)
@@ -30,11 +30,28 @@
     ...          return 0
 
 
-Now we can try indexing some documents:
+Now we can try indexing some documents.  First we instantiate an index.
+The one required argument is the privilege id of the privilege to be
+used for the index.
 
     >>> import zc.sharing.index
     >>> index = zc.sharing.index.Index(1)
 
+By default, zc.sharing indices use the 32-bit BTree family.  
+
+    >>> import BTrees
+    >>> index.family is BTrees.family32
+    True
+
+If your intid source is 32-bit, use the default; if it is 64-bit,
+specify BTrees.family64.
+
+When this document is run as a test, it is run twice, once with the 32 bit
+family and once with the 64 bit family.  This is represented with the `family`
+argument below, as we reinstantiate a new index and populate it.
+
+    >>> index = zc.sharing.index.Index(1, family)
+
     >>> index.index_doc(1, SampleDoc('bob', 'Everyone'))
     >>> index.index_doc(2, SampleDoc('bob'))
     >>> index.index_doc(3, SampleDoc('sally', 'Editors'))
@@ -42,10 +59,9 @@
     >>> index.index_doc(5, SampleDoc('sally'))
     >>> index.index_doc(6, SampleDoc('sally', 'Everyone'))
     >>> index.index_doc(7, SampleDoc('Workers'))
-    
-Note that, when we created the index, we passed the privilege id of
-the privilege to be used for the index.
 
+[#whitebox_family_test]_
+
 Now, we can search it to find the documents accessable to a principal.
 First we'll define a principal class.  Principals have a groups
 attribute that has the groups that the principal is contained in
@@ -72,8 +88,8 @@
     >>> component.provideUtility(Authentication())
 
     >>> r = index.apply(Principal('sally', 'Editors', 'Reviewers'))
-    >>> r.__class__.__name__
-    'IFSet'
+    >>> r.__class__ is family.IF.Set
+    True
 
     >>> list(r)
     [1, 3, 4, 5, 6, 7]
@@ -112,3 +128,11 @@
 
     >>> list(index.apply(Principal('fred')))
     []
+
+.. [#whitebox_family_test] Let's actually look at the internal data
+    structures: the correct family members are created.
+    
+    >>> isinstance(index.principal_documents, family.OO.BTree)
+    True
+    >>> isinstance(index.document_principals, family.IO.BTree)
+    True

Modified: zc.sharing/trunk/src/zc/sharing/tests.py
===================================================================
--- zc.sharing/trunk/src/zc/sharing/tests.py	2007-05-01 03:43:10 UTC (rev 74952)
+++ zc.sharing/trunk/src/zc/sharing/tests.py	2007-05-01 04:07:18 UTC (rev 74953)
@@ -17,6 +17,7 @@
 $Id$
 """
 import unittest
+import BTrees
 import zope.event
 import zope.component
 from zope.app.testing import placelesssetup
@@ -123,7 +124,15 @@
         doctest.DocFileSuite(
             'index.txt',
             setUp=placelesssetup.setUp, tearDown=tearDownIndex,
+            globs={'family': BTrees.family32},
+            optionflags=doctest.INTERPRET_FOOTNOTES,
             ),
+        doctest.DocFileSuite(
+            'index.txt',
+            setUp=placelesssetup.setUp, tearDown=tearDownIndex,
+            globs={'family': BTrees.family64},
+            optionflags=doctest.INTERPRET_FOOTNOTES,
+            ),
         ))
 
 if __name__ == '__main__':



More information about the Checkins mailing list