[Zope3-checkins] CVS: ZODB4/ZODB - BaseStorage.py:1.24.6.1 FileStorage.py:1.110.6.5

Jeremy Hylton jeremy@zope.com
Wed, 11 Dec 2002 14:55:11 -0500


Update of /cvs-repository/ZODB4/ZODB
In directory cvs.zope.org:/tmp/cvs-serv18482/ZODB

Modified Files:
      Tag: ZODB4-Q-branch
	BaseStorage.py FileStorage.py 
Log Message:
Lots more FileStorage refactoring.

All the tests pass again.  Yay!  But I suspect the storage tests don't
aren't very thorough w.r.t. FileStorage's complex implementation.
Need better tests.

Move most of the pack logic into FileStoragePacker helper class.  Use
FileStorageFormatter helper routines inside the packer.  This
simplifies the formatter, because no code ever needs to explicitly
pass a file object.  Move several helper methods from FileStorage to
the packer.

(Nearly) finish removal of undo.  There were a bunch of cases that
checked whether status == 'u'.  Since status can never be 'u', they've
all been removed.  One exception is versionEmpty(), because I can't
tell what the code is trying to do.

Deal better with an empty storage in _restore_index().
Move lock_file() to helper method.



=== ZODB4/ZODB/BaseStorage.py 1.24 => 1.24.6.1 ===
--- ZODB4/ZODB/BaseStorage.py:1.24	Wed Dec  4 16:36:02 2002
+++ ZODB4/ZODB/BaseStorage.py	Wed Dec 11 14:55:11 2002
@@ -16,6 +16,8 @@
 $Id$
 """
 
+__metaclass__ = type
+
 import threading
 from ZODB import POSException
 from ZODB.TimeStamp import newTimeStamp, TimeStamp


=== ZODB4/ZODB/FileStorage.py 1.110.6.4 => 1.110.6.5 === (982/1082 lines abridged)
--- ZODB4/ZODB/FileStorage.py:1.110.6.4	Tue Dec 10 18:58:18 2002
+++ ZODB4/ZODB/FileStorage.py	Wed Dec 11 14:55:11 2002
@@ -250,22 +250,19 @@
     def _read_txn_header(self, pos):
         pass
     
-    def _loadBack_impl(self, oid, back, afile=None):
+    def _loadBack_impl(self, oid, back):
         # shared implementation used by various _loadBack methods
-        # XXX Shouldn't need to pass afile, but it's more work
-        # to make pack use a separate formatter object.
-        afile = afile or self._file
         while 1:
             if not back:
-                # If the backpointer is 0, the object does not currently exist.
+                # If backpointer is 0, object does not currently exist.
                 raise POSKeyError(oid)
             h = self._read_data_header(back)
             if h.plen:
-                return afile.read(h.plen), h.serial, back, h.tloc
+                return self._file.read(h.plen), h.serial, back, h.tloc
             back = h.back
 
-    def _loadBack(self, oid, back, afile=None):
-        data, serial, old, tloc = self._loadBack_impl(oid, back, afile)
+    def _loadBack(self, oid, back):
+        data, serial, old, tloc = self._loadBack_impl(oid, back)
         return data, serial
 
     def _loadBackPOS(self, oid, back):
@@ -307,34 +304,21 @@
             if create:
                 raise ValueError, "can't create a read-only file"
         elif stop is not None:
-            raise ValueError, "time-travel is only supported in read-only mode"
+            raise ValueError("time-travel is only supported "
+                             "in read-only mode")
 
         if stop is None:
             stop='\377'*8
 
-        # Lock the database and set up the temp file.
+        self._file_name = file_name
+        super(FileStorage, self).__init__(file_name)
+        self._initIndex()
         if not read_only:
-            try:
-                f = open(file_name + '.lock', 'r+')
-            except:
-                f = open(file_name+'.lock', 'w+')

[-=- -=- -=- 982 lines omitted -=- -=- -=-]

+                else:
+                    # Not empty, but we need to adjust transaction length
+                    # and update the status
+                    ofile.seek(otpos + 8)
+                    otl = p64(otl)
+                    ofile.write(otl + status)
+                    ofile.seek(opos)
+                    ofile.write(otl)
+                    opos += 8
+
+            else:
+                ofile.write(p64(otl))
+                opos += 8
+
+            if not packing:
+                # We are in the copying phase. We need to get the lock
+                # again to avoid someone writing data while we read it.
+                self._commit_lock_acquire()
+                self._lock_acquire()
+                self.locked = True
+        
+        # OK, we've copied everything. Now we need to wrap things up.
+        ofile.flush()
+        ofile.close()
+        self._file.close()
+
+        return opos, index, vindex, tindex, tvindex
+
 def shift_transactions_forward(index, vindex, tindex, file, pos, opos):
     """Copy transactions forward in the data file
 
@@ -1847,7 +1755,7 @@
                 seek(8,1)
                 version=read(vlen)
                 pv=p64(vindex_get(version, 0))
-                if status != 'u': vindex[version]=opos
+                vindex[version]=opos
 
             tindex[oid]=opos
 
@@ -1886,8 +1794,7 @@
         # skip the (intentionally redundant) transaction length
         pos=pos+8
 
-        if status != 'u':
-            index.update(tindex) # Record the position
+        index.update(tindex) # Record the position
 
         tindex.clear()