[Checkins] SVN: zc.catalog/trunk/src/zc/catalog/ Encapsulated the BTree components and merge functions of a particular

Albertas Agejevas alga at pov.lt
Fri Feb 2 17:40:27 EST 2007


Log message for revision 72339:
  Encapsulated the BTree components and merge functions of a particular
  components into a class.
  

Changed:
  U   zc.catalog/trunk/src/zc/catalog/__init__.py
  U   zc.catalog/trunk/src/zc/catalog/extentcatalog.py
  U   zc.catalog/trunk/src/zc/catalog/extentcatalog.txt
  U   zc.catalog/trunk/src/zc/catalog/index.py
  U   zc.catalog/trunk/src/zc/catalog/interfaces.py
  U   zc.catalog/trunk/src/zc/catalog/tests.py

-=-
Modified: zc.catalog/trunk/src/zc/catalog/__init__.py
===================================================================
--- zc.catalog/trunk/src/zc/catalog/__init__.py	2007-02-02 21:53:52 UTC (rev 72338)
+++ zc.catalog/trunk/src/zc/catalog/__init__.py	2007-02-02 22:40:27 UTC (rev 72339)
@@ -1 +1,71 @@
+#############################################################################
 #
+# Copyright (c) 2007 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""zc.component package
+
+"""
+import BTrees.Interfaces
+import BTrees.IFBTree
+
+
+class BTreeAPI32(object):
+    """IFTree components and merge functions.
+
+    This class can be used as a pickleable reference to a particular
+    flavour of BTrees used in an index or catalog (IFBTrees in this case).
+    """
+
+    TreeSet = BTrees.IFBTree.IFTreeSet
+    Set = BTrees.IFBTree.IFSet
+    Bucket = BTrees.IFBTree.IFBucket
+
+    # IMerge
+    difference = BTrees.IFBTree.difference
+    union = BTrees.IFBTree.union
+    intersection = BTrees.IFBTree.intersection
+
+    # IIMerge
+    weightedUnion = BTrees.IFBTree.weightedUnion
+    weightedIntersection = BTrees.IFBTree.weightedIntersection
+
+    # IMergeIntegerKey
+    multiunion = BTrees.IFBTree.multiunion
+
+
+try:
+    import BTrees.LFBTree
+except:
+    pass
+else:
+    class BTreeAPI64(object):
+        """IFTree components and merge functions.
+
+        This class can be used as a pickleable reference to a particular
+        flavour of BTrees used in an index or catalog (LFBTrees in this case).
+        """
+
+        TreeSet = BTrees.LFBTree.LFTreeSet
+        Set = BTrees.LFBTree.LFSet
+        Bucket = BTrees.LFBTree.LFBucket
+
+        # IMerge
+        difference = BTrees.LFBTree.difference
+        union = BTrees.LFBTree.union
+        intersection = BTrees.LFBTree.intersection
+
+        # IIMerge
+        weightedUnion = BTrees.LFBTree.weightedUnion
+        weightedIntersection = BTrees.LFBTree.weightedIntersection
+
+        # IMergeIntegerKey
+        multiunion = BTrees.LFBTree.multiunion

Modified: zc.catalog/trunk/src/zc/catalog/extentcatalog.py
===================================================================
--- zc.catalog/trunk/src/zc/catalog/extentcatalog.py	2007-02-02 21:53:52 UTC (rev 72338)
+++ zc.catalog/trunk/src/zc/catalog/extentcatalog.py	2007-02-02 22:40:27 UTC (rev 72339)
@@ -26,26 +26,21 @@
 from zope.component.interfaces import IFactory
 from BTrees.Interfaces import IMerge
 
+import zc.catalog
 from zc.catalog import interfaces
 
+
 class FilterExtent(persistent.Persistent):
     interface.implements(interfaces.IFilterExtent)
     __parent__ = None
 
-    # The [IL]FBTree utilities of the flavor we need
-    _weightedUnion = IFBTree.weightedUnion
-    _weightedIntersection = IFBTree.weightedIntersection
-    _difference = IFBTree.difference
+    BTreeAPI = zc.catalog.BTreeAPI32
 
     def __init__(self, filter):
-        treeset = zope.component.queryUtility(IFactory, name='IFTreeSet',
-                                              default=IFBTree.IFTreeSet)
-        self.set = treeset()
-        myIFBTree = zope.component.queryUtility(IMerge, name='IFBTree',
-                                                default=IFBTree)
-        self._weightedUnion = myIFBTree.weightedUnion
-        self._weightedIntersection = myIFBTree.weightedIntersection
-        self._difference = myIFBTree.difference
+        self.BTreeAPI = zope.component.queryUtility(
+            interfaces.IBTreeAPI,
+            default=zc.catalog.BTreeAPI32)
+        self.set = self.BTreeAPI.TreeSet()
         self.filter = filter
 
     def addable(self, uid, obj):
@@ -61,7 +56,7 @@
     __ror__ = __or__
 
     def union(self, other, self_weight=1, other_weight=1):
-        return self._weightedUnion(
+        return self.BTreeAPI.weightedUnion(
             self.set, other, self_weight, other_weight)[1]
 
     def __and__(self, other):
@@ -71,7 +66,7 @@
     __rand__ = __and__
 
     def intersection(self, other, self_weight=1, other_weight=1):
-        return self._weightedIntersection(
+        return self.BTreeAPI.weightedIntersection(
             self.set, other, self_weight, other_weight)[1]
 
     def __sub__(self, other):
@@ -79,14 +74,14 @@
         return self.difference(other)
 
     def difference(self, other):
-        return self._difference(self.set, other)
+        return self.BTreeAPI.difference(self.set, other)
 
     def __rsub__(self, other):
         "set - extent"
         return self.rdifference(other)
 
     def rdifference(self, other):
-        return self._difference(other, self.set)
+        return self.BTreeAPI.difference(other, self.set)
 
     def __iter__(self):
         return iter(self.set)

Modified: zc.catalog/trunk/src/zc/catalog/extentcatalog.txt
===================================================================
--- zc.catalog/trunk/src/zc/catalog/extentcatalog.txt	2007-02-02 21:53:52 UTC (rev 72338)
+++ zc.catalog/trunk/src/zc/catalog/extentcatalog.txt	2007-02-02 22:40:27 UTC (rev 72339)
@@ -6,6 +6,8 @@
 some items to index, and a filter that determines what the extent accepts.
 We'll do this within a real ZODB and a real intid utility [#setup]_.
 
+    >>> import zc.catalog
+    >>> import zc.catalog.interfaces
     >>> from zc.catalog import interfaces, extentcatalog
     >>> from zope import interface, component
     >>> from zope.interface import verify
@@ -15,16 +17,15 @@
     >>> root = makeRoot()
     >>> intid = zope.component.getUtility(
     ...     zope.app.intid.interfaces.IIntIds, context=root)
-    >>> IFTreeSet = component.queryUtility(component.interfaces.IFactory,
-    ...                                    name="IFTreeSet",
-    ...                                    default=BTrees.IFBTree.IFTreeSet)
+    >>> BTreeAPI = component.queryUtility(zc.catalog.interfaces.IBTreeAPI,
+    ...                                   default=zc.catalog.BTreeAPI32)
 
     >>> from zope.app.container.interfaces import IContained
     >>> class DummyIndex(persistent.Persistent):
     ...     interface.implements(IContained)
     ...     __parent__ = __name__ = None
     ...     def __init__(self):
-    ...         self.uids = IFTreeSet()
+    ...         self.uids = BTreeAPI.TreeSet()
     ...     def unindex_doc(self, uid):
     ...         if uid in self.uids:
     ...             self.uids.remove(uid)
@@ -184,7 +185,7 @@
     >>> for i in range(1, 100, 2):
     ...     extent.add(i, None)
     ...
-    >>> alt_set = IFTreeSet()
+    >>> alt_set = BTreeAPI.TreeSet()
     >>> alt_set.update(range(0, 166, 33)) # return value is unimportant here
     6
     >>> sorted(alt_set)

Modified: zc.catalog/trunk/src/zc/catalog/index.py
===================================================================
--- zc.catalog/trunk/src/zc/catalog/index.py	2007-02-02 21:53:52 UTC (rev 72338)
+++ zc.catalog/trunk/src/zc/catalog/index.py	2007-02-02 22:40:27 UTC (rev 72339)
@@ -40,40 +40,14 @@
                          zc.catalog.interfaces.IIndexValues,
                          )
 
-    # The [IL]FBTree utilities of the flavor we need
-    _multiunion = IFBTree.multiunion
-    _weightedUnion = IFBTree.weightedUnion
-    _intersection = IFBTree.intersection
+    BTreeAPI = zc.catalog.BTreeAPI32
 
-    # BTree components of the flavor we need
-    IFTreeSet = IFBTree.IFTreeSet
-    IFSet = IFBTree.IFSet
-    IFBucket = IFBTree.IFBucket
-
     def __init__(self):
         self.clear()
-        self.IFTreeSet = component.queryUtility(
-            zope.component.interfaces.IFactory,
-            name='IFTreeSet',
-            default=IFBTree.IFTreeSet)
-        self.IFSet = component.queryUtility(
-            zope.component.interfaces.IFactory,
-            name='IFSet',
-            default=IFBTree.IFSet)
-        self.IFBucket = component.queryUtility(
-            zope.component.interfaces.IFactory,
-            name='IFBucket',
-            default=IFBTree.IFBucket)
+        self.BTreeAPI = component.queryUtility(
+            zc.catalog.interfaces.IBTreeAPI,
+            default=zc.catalog.BTreeAPI32)
 
-        myIFBTree = component.queryUtility(
-            IMerge,
-            name='IFBTree',
-            default=IFBTree)
-
-        self._multiunion = myIFBTree.multiunion
-        self._weightedUnion = myIFBTree.weightedUnion
-        self._intersection = myIFBTree.intersection
-
     def clear(self):
         self.values_to_documents = OOBTree.OOBTree()
         self.documents_to_values = component.queryUtility(
@@ -133,7 +107,7 @@
         values_to_documents = self.values_to_documents
         docs = values_to_documents.get(added)
         if docs is None:
-            values_to_documents[added] = self.IFTreeSet((doc_id,))
+            values_to_documents[added] = self.BTreeAPI.TreeSet((doc_id,))
             self.wordCount.change(1)
         else:
             docs.insert(doc_id)
@@ -175,23 +149,23 @@
         if query_type is None:
             res = None
         elif query_type == 'any_of':
-            res = self._multiunion(
+            res = self.BTreeAPI.multiunion(
                 [s for s in (values_to_documents.get(v) for v in query)
                  if s is not None])
         elif query_type == 'any':
             if query is None:
-                res = self.IFSet(self.ids())
+                res = self.BTreeAPI.Set(self.ids())
             else:
                 assert zc.catalog.interfaces.IExtent.providedBy(query)
-                res = query & self.IFSet(self.ids())
+                res = query & self.BTreeAPI.Set(self.ids())
         elif query_type == 'between':
-            res = self._multiunion(
+            res = self.BTreeAPI.multiunion(
                 [s for s in (values_to_documents.get(v) for v in
                              values_to_documents.keys(*query))
                  if s is not None])
         elif query_type == 'none':
             assert zc.catalog.interfaces.IExtent.providedBy(query)
-            res = query - self.IFSet(self.ids())
+            res = query - self.BTreeAPI.Set(self.ids())
         else:
             raise ValueError(
                 "unknown query type", query_type)
@@ -222,7 +196,7 @@
         for v in added:
             docs = values_to_documents.get(v)
             if docs is None:
-                values_to_documents[v] = self.IFTreeSet((doc_id,))
+                values_to_documents[v] = self.BTreeAPI.TreeSet((doc_id,))
                 self.wordCount.change(1)
             else:
                 docs.insert(doc_id)
@@ -272,37 +246,37 @@
         if query_type is None:
             res = None
         elif query_type == 'any_of':
-            res = self.IFBucket()
+            res = self.BTreeAPI.Bucket()
             for v in query:
-                _, res = self._weightedUnion(
+                _, res = self.BTreeAPI.weightedUnion(
                     res, values_to_documents.get(v))
         elif query_type == 'any':
             if query is None:
-                res = self.IFSet(self.ids())
+                res = self.BTreeAPI.Set(self.ids())
             else:
                 assert zc.catalog.interfaces.IExtent.providedBy(query)
-                res = query & self.IFSet(self.ids())
+                res = query & self.BTreeAPI.Set(self.ids())
         elif query_type == 'all_of':
             res = None
             values = iter(query)
             try:
                 res = values_to_documents.get(values.next())
             except StopIteration:
-                res = self.IFTreeSet()
+                res = self.BTreeAPI.TreeSet()
             while res:
                 try:
                     v = values.next()
                 except StopIteration:
                     break
-                res = self._intersection(res, values_to_documents.get(v))
+                res = self.BTreeAPI.intersection(res, values_to_documents.get(v))
         elif query_type == 'between':
-            res = self.IFBucket()
+            res = self.BTreeAPI.Bucket()
             for v in values_to_documents.keys(*query):
-                _, res = self._weightedUnion(res,
-                                             values_to_documents.get(v))
+                _, res = self.BTreeAPI.weightedUnion(res,
+                                                     values_to_documents.get(v))
         elif query_type == 'none':
             assert zc.catalog.interfaces.IExtent.providedBy(query)
-            res = query - self.IFSet(self.ids())
+            res = query - self.BTreeAPI.Set(self.ids())
         else:
             raise ValueError(
                 "unknown query type", query_type)

Modified: zc.catalog/trunk/src/zc/catalog/interfaces.py
===================================================================
--- zc.catalog/trunk/src/zc/catalog/interfaces.py	2007-02-02 21:53:52 UTC (rev 72338)
+++ zc.catalog/trunk/src/zc/catalog/interfaces.py	2007-02-02 22:40:27 UTC (rev 72339)
@@ -21,6 +21,7 @@
 import zope.index.interfaces
 import zope.app.catalog.interfaces
 from zc.catalog.i18n import _
+import BTrees.Interfaces
 
 class IExtent(interface.Interface):
 
@@ -293,3 +294,12 @@
         title=_('Resolution'),
         default=2,
         required=True)
+
+
+class IBTreeAPI(BTrees.Interfaces.IMergeIntegerKey, BTrees.Interfaces.IIMerge):
+    """A class encapsulating a flavour (32/64 bit) of the XFBTrees"""
+
+    TreeSet = interface.Attribute("The class of a tree set flavour")
+    Set = interface.Attribute("The class of a set flavour")
+    Bucket = interface.Attribute("The class of a set flavour")
+

Modified: zc.catalog/trunk/src/zc/catalog/tests.py
===================================================================
--- zc.catalog/trunk/src/zc/catalog/tests.py	2007-02-02 21:53:52 UTC (rev 72338)
+++ zc.catalog/trunk/src/zc/catalog/tests.py	2007-02-02 22:40:27 UTC (rev 72339)
@@ -22,6 +22,9 @@
 import zope.component.factory
 import zope.component.interfaces
 
+import zc.catalog
+import zc.catalog.interfaces
+
 import BTrees.Interfaces
 import BTrees.LOBTree
 import BTrees.OLBTree
@@ -40,29 +43,17 @@
 def setUp64bit(test):
     zope.component.testing.setUp(test)
     zope.component.provideUtility(
-        zope.component.factory.Factory(BTrees.LFBTree.LFBTree),
-        name='IFBTree')
-    zope.component.provideUtility(
-        zope.component.factory.Factory(BTrees.LFBTree.LFTreeSet),
-        name='IFTreeSet')
-    zope.component.provideUtility(
-        zope.component.factory.Factory(BTrees.LFBTree.LFSet),
-        name='IFSet')
-    zope.component.provideUtility(
-        zope.component.factory.Factory(BTrees.LFBTree.LFBucket),
-        name='IFBucket')
-    zope.component.provideUtility(
         zope.component.factory.Factory(BTrees.LOBTree.LOBTree),
         name='IOBTree')
     zope.component.provideUtility(
         zope.component.factory.Factory(BTrees.OLBTree.OLBTree),
         name='OIBTree')
     zope.component.provideUtility(
-        BTrees.LFBTree,
-        provides=BTrees.Interfaces.IMerge,
-        name='IFBTree')
+        zc.catalog.BTreeAPI64,
+        provides=zc.catalog.interfaces.IBTreeAPI)
 
 
+
 def tearDown64bit(test):
     zope.component.testing.tearDown(test)
 



More information about the Checkins mailing list