[Checkins] SVN: zc.relationship/trunk/src/zc/relationship/ Unindexing a relationship that does not exist is supposed to be a no-op. Fixed, with tests. Also added __contains__, largely as a test convenience.

Gary Poster gary at zope.com
Mon Jul 17 10:22:33 EDT 2006


Log message for revision 69152:
  Unindexing a relationship that does not exist is supposed to be a no-op.  Fixed, with tests.  Also added __contains__, largely as a test convenience.
  

Changed:
  U   zc.relationship/trunk/src/zc/relationship/README.txt
  U   zc.relationship/trunk/src/zc/relationship/index.py
  U   zc.relationship/trunk/src/zc/relationship/interfaces.py

-=-
Modified: zc.relationship/trunk/src/zc/relationship/README.txt
===================================================================
--- zc.relationship/trunk/src/zc/relationship/README.txt	2006-07-17 11:18:56 UTC (rev 69151)
+++ zc.relationship/trunk/src/zc/relationship/README.txt	2006-07-17 14:22:33 UTC (rev 69152)
@@ -1388,3 +1388,66 @@
 
 Remember that BTrees (not just BTreeSets) can be used for these values: the keys
 are used as the values in that case.
+
+__contains__ and Unindexing
+=============================
+
+You can test whether a relationship is in an index with __contains__.  Note
+that this uses the actual relationship, not the relationship token.
+
+    >>> ix = index.Index(
+    ...     ({'element': IRelationship['subjects'], 'multiple': True,
+    ...       'dump': dump, 'load': load},
+    ...      {'element': IRelationship['relationshiptype'],
+    ...       'dump': relTypeDump, 'load': relTypeLoad, 'btree': OIBTree,
+    ...       'name': 'reltype'},
+    ...      {'element': IRelationship['objects'], 'multiple': True,
+    ...       'dump': dump, 'load': load},
+    ...      {'element': IContextAwareRelationship['getContext'],
+    ...       'name': 'context'}),
+    ...     index.TransposingTransitiveQueriesFactory('subjects', 'objects'))
+    >>> ix.documentCount()
+    0
+    >>> app['fredisprojectmanager'].subjects = (people['Fred'],)
+    >>> ix.index(app['fredisprojectmanager'])
+    >>> ix.index(app['another_rel'])
+    >>> ix.documentCount()
+    2
+    >>> app['fredisprojectmanager'] in ix
+    True
+    >>> list(ix.findValues(
+    ...     'subjects',
+    ...     q({'reltype': 'has the role of',
+    ...       'objects': roles['Project Manager'],
+    ...       'context': projects['zope.org redesign']})))
+    [<Person 'Fred'>]
+
+    >>> app['another_rel'] in ix
+    True
+
+    >>> app['abeAndBran'] in ix
+    False
+
+As noted, you can unindex using unindex(relationship) or
+unindex_doc(relationship token).
+
+    >>> ix.unindex_doc(ix.tokenizeRelationship(app['fredisprojectmanager']))
+    >>> app['fredisprojectmanager'] in ix
+    False
+    >>> list(ix.findValues(
+    ...     'subjects',
+    ...     q({'reltype': 'has the role of',
+    ...       'objects': roles['Project Manager'],
+    ...       'context': projects['zope.org redesign']})))
+    []
+
+    >>> ix.unindex(app['another_rel'])
+    >>> app['another_rel'] in ix
+    False
+
+As defined by zope.index.interfaces.IInjection, if the relationship is
+not in the index then calling unindex_doc is a no-op; the same holds
+true for unindex.
+
+    >>> ix.unindex(app['abeAndBran'])
+    >>> ix.unindex_doc(ix.tokenizeRelationship(app['abeAndBran']))

Modified: zc.relationship/trunk/src/zc/relationship/index.py
===================================================================
--- zc.relationship/trunk/src/zc/relationship/index.py	2006-07-17 11:18:56 UTC (rev 69151)
+++ zc.relationship/trunk/src/zc/relationship/index.py	2006-07-17 14:22:33 UTC (rev 69152)
@@ -268,13 +268,17 @@
     def unindex(self, rel):
         self.unindex_doc(self._relTools['dump'](rel, self, {}))
 
+    def __contains__(self, rel):
+        return self.tokenizeRelationship(rel) in self._relTokens   
+
     def unindex_doc(self, relToken):
-        for data in self._attrs.values():
-            tokens = self._reltoken_name_TO_objtokenset.pop(
-                (relToken, data['name']))
-            self._remove(relToken, tokens, data['name'])
-        self._relTokens.remove(relToken)
-        self._relLength.change(-1)
+        if relToken in self._relTokens:
+            for data in self._attrs.values():
+                tokens = self._reltoken_name_TO_objtokenset.pop(
+                    (relToken, data['name']))
+                self._remove(relToken, tokens, data['name'])
+            self._relTokens.remove(relToken)
+            self._relLength.change(-1)
 
     def documentCount(self):
         return self._relLength.value

Modified: zc.relationship/trunk/src/zc/relationship/interfaces.py
===================================================================
--- zc.relationship/trunk/src/zc/relationship/interfaces.py	2006-07-17 11:18:56 UTC (rev 69151)
+++ zc.relationship/trunk/src/zc/relationship/interfaces.py	2006-07-17 14:22:33 UTC (rev 69152)
@@ -57,6 +57,9 @@
         """obtains the token for the relationship and unindexes (calls
         IInjection.unindex_doc)"""
 
+    def __contains__(relationship):
+        """returns whether the relationship is in the index"""
+
     def findValueTokens(resultName, query, maxDepth=None, filter=None,
                         targetQuery=None, targetFilter=None,
                         transitiveQueriesFactory=None):



More information about the Checkins mailing list