[Zodb-checkins] SVN: ZODB/branches/blob-merge-branch/src/ZODB/ Add (badly implemented) methods to TmpStore to allow blob import to work again. This will need to be redone, but it gets us to the point where all the tests pass.

Chris McDonough chrism at plope.com
Sat Sep 24 11:34:08 EDT 2005


Log message for revision 38592:
  Add (badly implemented) methods to TmpStore to allow blob import to work again.  This will need to be redone, but it gets us to the point where all the tests pass.
  
  Also provide a __repr__ to the BlobStorage proxy which lets us know that we're using a proxied storage.
  

Changed:
  U   ZODB/branches/blob-merge-branch/src/ZODB/Blobs/BlobStorage.py
  U   ZODB/branches/blob-merge-branch/src/ZODB/Blobs/tests/transaction.txt
  U   ZODB/branches/blob-merge-branch/src/ZODB/Connection.py

-=-
Modified: ZODB/branches/blob-merge-branch/src/ZODB/Blobs/BlobStorage.py
===================================================================
--- ZODB/branches/blob-merge-branch/src/ZODB/Blobs/BlobStorage.py	2005-09-24 15:26:14 UTC (rev 38591)
+++ ZODB/branches/blob-merge-branch/src/ZODB/Blobs/BlobStorage.py	2005-09-24 15:34:07 UTC (rev 38592)
@@ -42,6 +42,11 @@
         ProxyBase.__init__(self, storage)
         self.base_directory = base_directory
         self.dirty_oids = []
+
+    def __repr__(self):
+        normal_storage = getProxiedObject(self)
+        return '<BlobStorage proxy for %r at %s>' % (normal_storage,
+                                                     hex(id(self)))
      
     def storeBlob(self, oid, oldserial, data, blobfilename, version,
                   transaction):

Modified: ZODB/branches/blob-merge-branch/src/ZODB/Blobs/tests/transaction.txt
===================================================================
--- ZODB/branches/blob-merge-branch/src/ZODB/Blobs/tests/transaction.txt	2005-09-24 15:26:14 UTC (rev 38591)
+++ ZODB/branches/blob-merge-branch/src/ZODB/Blobs/tests/transaction.txt	2005-09-24 15:34:07 UTC (rev 38592)
@@ -143,8 +143,8 @@
 
     >>> tm1 = transaction.TransactionManager()
     >>> tm2 = transaction.TransactionManager()
-    >>> root3 = database.open(txn_mgr=tm1).root()
-    >>> root4 = database.open(txn_mgr=tm2).root()
+    >>> root3 = database.open(transaction_manager=tm1).root()
+    >>> root4 = database.open(transaction_manager=tm2).root()
     >>> blob1c3 = root3['blob1']
     >>> blob1c4 = root4['blob1']
     >>> blob1c3fh1 = blob1c3.open('a')

Modified: ZODB/branches/blob-merge-branch/src/ZODB/Connection.py
===================================================================
--- ZODB/branches/blob-merge-branch/src/ZODB/Connection.py	2005-09-24 15:26:14 UTC (rev 38591)
+++ ZODB/branches/blob-merge-branch/src/ZODB/Connection.py	2005-09-24 15:34:07 UTC (rev 38592)
@@ -20,6 +20,7 @@
 import tempfile
 import threading
 import warnings
+import os
 from time import time
 
 from persistent import PickleCache
@@ -42,9 +43,11 @@
 from ZODB.POSException import InvalidObjectReference, ConnectionStateError
 from ZODB.POSException import ConflictError, ReadConflictError
 from ZODB.POSException import Unsupported
+from ZODB.POSException import POSKeyError
 from ZODB.serialize import ObjectWriter, ObjectReader, myhasattr
 from ZODB.utils import DEPRECATED_ARGUMENT, deprecated36
 from ZODB.utils import p64, u64, z64, oid_repr, positive_id
+from ZODB import utils
 
 global_reset_counter = 0
 
@@ -1120,10 +1123,15 @@
 
         for oid in oids:
             data, serial = src.load(oid, src)
-            s = self._storage.store(oid, serial, data,
-                                    self._version, transaction)
+            try:
+                blobfilename = src.loadBlob(oid, serial, self._version)
+            except POSKeyError:
+                s = self._storage.store(oid, serial, data,
+                                        self._version, transaction)
+            else:
+                s = self._storage.storeBlob(oid, serial, data, blobfilename,
+                                            self._version, transaction)
             self._handle_serial(s, oid, change=False)
-
         src.close()
 
     def _abort_savepoint(self):
@@ -1166,6 +1174,9 @@
     def rollback(self):
         self.datamanager._rollback(self.state)
 
+BLOB_SUFFIX = ".blob"
+BLOB_DIRTY = "store"
+
 class TmpStore:
     """A storage-like thing to support savepoints."""
 
@@ -1179,6 +1190,7 @@
 
         self._base_version = base_version
         self._file = tempfile.TemporaryFile()
+        self._blobdir = tempfile.mkdtemp()
         # position: current file position
         # _tpos: file position at last commit point
         self.position = 0L
@@ -1222,6 +1234,38 @@
         self.position += l + len(header)
         return serial
 
+    def storeBlob(self, oid, serial, data, blobfilename, version,
+                  transaction):
+        # XXX we need to clean up after ourselves!
+        serial = self.store(oid, serial, data, version, transaction)
+        assert isinstance(serial, str) # XXX in theory serials could be 
+                                       # something else
+
+        targetpath = self._getBlobPath(oid)
+        if not os.path.exists(targetpath):
+            os.makedirs(targetpath, 0700)
+
+        targetname = self._getCleanFilename(oid, serial)
+        utils.best_rename(blobfilename, targetname)
+
+    def loadBlob(self, oid, serial, version):
+        """Return the filename where the blob file can be found.
+        """
+        filename = self._getCleanFilename(oid, serial)
+        if not os.path.exists(filename):
+            raise POSKeyError, "Not an existing blob."
+        return filename
+
+    def _getBlobPath(self, oid):
+        return os.path.join(self._blobdir,
+                            utils.oid_repr(oid)
+                            )
+
+    def _getCleanFilename(self, oid, tid):
+        return os.path.join(self._getBlobPath(oid),
+                            "%s%s" % (utils.tid_repr(tid), 
+                                      BLOB_SUFFIX,)
+                            )
     def reset(self, position, index):
         self._file.truncate(position)
         self.position = position



More information about the Zodb-checkins mailing list