[Zope-Checkins] CVS: Zope/lib/python/Products/ZCTextIndex - OkapiIndex.py:1.29.74.2

Casey Duncan casey@zope.com
Thu, 5 Jun 2003 00:42:28 -0400


Update of /cvs-repository/Zope/lib/python/Products/ZCTextIndex
In directory cvs.zope.org:/tmp/cvs-serv14976

Modified Files:
      Tag: casey-zctextindex-fewer-conflicts-branch
	OkapiIndex.py 
Log Message:
Add opportunistic upgrade code for _totaldoclen attribute. Also made sure queries still function before it is upgraded. Added upgrade tests to testLexicon. Fixed broken test in testZCTextIndex.


=== Zope/lib/python/Products/ZCTextIndex/OkapiIndex.py 1.29.74.1 => 1.29.74.2 ===
--- Zope/lib/python/Products/ZCTextIndex/OkapiIndex.py:1.29.74.1	Wed Jun  4 00:26:38 2003
+++ Zope/lib/python/Products/ZCTextIndex/OkapiIndex.py	Thu Jun  5 00:41:58 2003
@@ -56,16 +56,24 @@
 
     def index_doc(self, docid, text):
         count = BaseIndex.index_doc(self, docid, text)
-        self._totaldoclen.change(count)
+        self._change_doc_len(count)
         return count
 
     def _reindex_doc(self, docid, text):
-        self._totaldoclen.change(-self._docweight[docid])
+        self._change_doc_len(-self._docweight[docid])
         return BaseIndex._reindex_doc(self, docid, text)
 
     def unindex_doc(self, docid):
-        self._totaldoclen.change(-self._docweight[docid])
+        self._change_doc_len(-self._docweight[docid])
         BaseIndex.unindex_doc(self, docid)
+    
+    def _change_doc_len(self, delta):
+        # Change total doc length used for scoring
+        try:
+            self._totaldoclen.change(delta)
+        except AttributeError:
+            # Opportunistically upgrade _totaldoclen attribute to Length object
+            self._totaldoclen = Length(long(self._totaldoclen + delta))
 
     # The workhorse.  Return a list of (IIBucket, weight) pairs, one pair
     # for each wid t in wids.  The IIBucket, times the weight, maps D to
@@ -79,7 +87,12 @@
         if not wids:
             return []
         N = float(len(self._docweight))  # total # of docs
-        meandoclen = self._totaldoclen() / N
+        try:
+            doclen = self._totaldoclen()
+        except TypeError:
+            # _totaldoclen has not yet been upgraded
+            doclen = self._totaldoclen
+        meandoclen = doclen / N
         K1 = self.K1
         B = self.B
         K1_plus1 = K1 + 1.0
@@ -123,7 +136,12 @@
         if not wids:
             return []
         N = float(len(self._docweight))  # total # of docs
-        meandoclen = self._totaldoclen() / N
+        try:
+            doclen = self._totaldoclen()
+        except TypeError:
+            # _totaldoclen has not yet been upgraded
+            doclen = self._totaldoclen
+        meandoclen = doclen / N
         #K1 = self.K1
         #B = self.B
         #K1_plus1 = K1 + 1.0