[Zope3-checkins] CVS: Zope3/src/zope/index/keyword - index.py:1.2

Andreas Jung andreas@andreas-jung.com
Tue, 15 Jul 2003 13:52:06 -0400


Update of /cvs-repository/Zope3/src/zope/index/keyword
In directory cvs.zope.org:/tmp/cvs-serv30451/keyword

Modified Files:
	index.py 
Log Message:
new attr _num_docs for efficient document counting


=== Zope3/src/zope/index/keyword/index.py 1.1 => 1.2 ===
--- Zope3/src/zope/index/keyword/index.py:1.1	Tue Jul 15 12:07:43 2003
+++ Zope3/src/zope/index/keyword/index.py	Tue Jul 15 13:52:01 2003
@@ -19,6 +19,7 @@
 from zodb.btrees.IOBTree import IOBTree
 from zodb.btrees.OOBTree import OOBTree, OOSet
 from zodb.btrees.IIBTree import IISet, union, intersection
+from zodb.btrees.Length import Length
 
 from types import ListType, TupleType, StringTypes
 from zope.index.interfaces import IInjection, IKeywordQuerying, IStatistics
@@ -42,10 +43,11 @@
         # keywords since it would allow use to use integers instead of
         # strings
         self._rev_index = IOBTree()
+        self._num_docs = Length(0)
 
     def documentCount(self):
         """Return the number of documents in the index."""
-        return len(self._rev_index)
+        return self._num_docs()
 
     def wordCount(self):
         """Return the number of indexed words"""
@@ -55,7 +57,8 @@
         return bool(self._rev_index.has_key(docid))
 
     def index_doc(self, docid, seq):
-
+        
+        if not seq: return
         seq = [w.lower() for w in seq]
             
         if self.has_doc(docid):       # unindex doc if present
@@ -66,7 +69,9 @@
 
         self._insert_forward(docid, seq)
         self._insert_reverse(docid, seq)
+        self._num_docs.change(1)
 
+        
     def unindex_doc(self, docid):
 
         idx  = self._fwd_index
@@ -75,12 +80,13 @@
             for word in self._rev_index[docid]:
                 idx[word].remove(docid)
                 if not idx[word]: del idx[word] 
-        except KeyError: pass
+        except KeyError: return
         
         try:
             del self._rev_index[docid]
         except KeyError: pass
 
+        self._num_docs.change(-1)
 
     def _insert_forward(self, docid, words):
         """insert a sequence of words into the forward index """