[Zope3-checkins] SVN: Zope3/trunk/src/zope/index/text/ Added clear method

Jim Fulton jim at zope.com
Sun Aug 29 16:55:31 EDT 2004


Log message for revision 27341:
  Added clear method
  
  Changed unindex_doc to ignore docids not in the index
  


Changed:
  U   Zope3/trunk/src/zope/index/text/baseindex.py
  U   Zope3/trunk/src/zope/index/text/tests/test_index.py


-=-
Modified: Zope3/trunk/src/zope/index/text/baseindex.py
===================================================================
--- Zope3/trunk/src/zope/index/text/baseindex.py	2004-08-29 20:55:24 UTC (rev 27340)
+++ Zope3/trunk/src/zope/index/text/baseindex.py	2004-08-29 20:55:30 UTC (rev 27341)
@@ -85,6 +85,9 @@
         # Use a BTree length for efficient length computation w/o conflicts
         self.wordCount = Length.Length()
 
+    def clear(self):
+        self.__init__(self._lexicon)
+
     def wordCount(self):
         """Return the number of words in the index."""
         # This is overridden per instance
@@ -101,7 +104,7 @@
 
     # A subclass may wish to extend or override this.
     def index_doc(self, docid, text):
-        if self._docwords.has_key(docid):
+        if docid in self._docwords:
             return self._reindex_doc(docid, text)
         wids = self._lexicon.sourceToWordIds(text)
         wid2weight, docweight = self._get_frequencies(wids)
@@ -161,10 +164,12 @@
         raise NotImplementedError
 
     def has_doc(self, docid):
-        return self._docwords.has_key(docid)
+        return docid in self._docwords
 
     # A subclass may wish to extend or override this.
     def unindex_doc(self, docid):
+        if docid not in self._docwords:
+            return
         for wid in unique(self.get_words(docid)):
             self._del_wordinfo(wid, docid)
         del self._docwords[docid]

Modified: Zope3/trunk/src/zope/index/text/tests/test_index.py
===================================================================
--- Zope3/trunk/src/zope/index/text/tests/test_index.py	2004-08-29 20:55:24 UTC (rev 27340)
+++ Zope3/trunk/src/zope/index/text/tests/test_index.py	2004-08-29 20:55:30 UTC (rev 27341)
@@ -30,10 +30,8 @@
         self.lexicon = Lexicon(Splitter())
         self.index = self.IndexFactory(self.lexicon)
 
-    def test_index_document(self, DOCID=1):
-        doc = "simple document contains five words"
-        self.assert_(not self.index.has_doc(DOCID))
-        self.index.index_doc(DOCID, doc)
+
+    def _test_index_document_assertions(self, DOCID=1):
         self.assertEqual(self.index.documentCount(), 1)
         self.assertEqual(self.index.wordCount(), 5)
         self.assertEqual(self.lexicon.wordCount(), 5)
@@ -49,16 +47,36 @@
             self.assertEqual(len(map), 1)
             self.assert_(map.has_key(DOCID))
 
-    def test_unindex_document(self):
-        DOCID = 1
-        self.test_index_document(DOCID)
-        self.index.unindex_doc(DOCID)
+    def test_index_document(self, DOCID=1):
+        doc = "simple document contains five words"
+        self.assert_(not self.index.has_doc(DOCID))
+        self.index.index_doc(DOCID, doc)
+        self._test_index_document_assertions(DOCID)
+
+    def test_unindex_document_absent_docid(self):
+        self.test_index_document(1)
+        self.index.unindex_doc(2)
+        self._test_index_document_assertions(1)
+
+    def test_clear(self):
+        self.test_index_document(1)
+        self.index.clear()
+        self._test_unindex_document_assertions()
+
+    def _test_unindex_document_assertions(self):
         self.assertEqual(len(self.index._docweight), 0)
         self.assertEqual(len(self.index._wordinfo), 0)
         self.assertEqual(len(self.index._docwords), 0)
         self.assertEqual(len(self.index._wordinfo),
                          self.index.wordCount())
 
+    def test_unindex_document(self):
+        DOCID = 1
+        self.test_index_document(DOCID)
+        self.index.unindex_doc(DOCID)
+        self._test_unindex_document_assertions()
+        
+
     def test_index_two_documents(self):
         self.test_index_document()
         doc = "another document just four"



More information about the Zope3-Checkins mailing list