[Zodb-checkins] CVS: ZODB3/ZODB/zodb4 - conversion.py:1.1.2.2
main.py:1.1.2.2
Fred L. Drake, Jr.
fred at zope.com
Thu Jan 29 16:31:43 EST 2004
Update of /cvs-repository/ZODB3/ZODB/zodb4
In directory cvs.zope.org:/tmp/cvs-serv25329
Modified Files:
Tag: zope3-zodb3-devel-branch
conversion.py main.py
Log Message:
lots of simplification to take better advantage of existing ZODB3 code
=== ZODB3/ZODB/zodb4/conversion.py 1.1.2.1 => 1.1.2.2 ===
--- ZODB3/ZODB/zodb4/conversion.py:1.1.2.1 Wed Jan 28 08:06:22 2004
+++ ZODB3/ZODB/zodb4/conversion.py Thu Jan 29 16:31:42 2004
@@ -15,76 +15,25 @@
from cPickle import dumps
-from ZODB.FileStorage import FileStorage, format
-from ZODB.zodb4 import z4format
+from ZODB.FileStorage import FileStorage
from ZODB.zodb4 import z4iterator
-from ZODB.zodb4 import z4utils
class Conversion:
- def __init__(self, input, output):
- """Initialize a ZODB4->ZODB3 FileStorage converter.
-
- input is an open file containing a ZODB 4 filestorage; it should
- be read-only.
-
- output is an open file to which a new ZODB 3 filestorage will be
- written.
-
- Both files must be opened in binary mode.
- """
- self.input = input
- self.instore = z4iterator.FileIterator()
- self.instore._file = input
- self.output = output
- self.outstore = format.FileStorageFormatter()
- self.outstore._file = output
- # ZODB 4 data position --> ZODB 3 data position
- self._data_pos_map = {}
+ def __init__(self, input_path, output_path):
+ """Initialize a ZODB4->ZODB3 FileStorage converter."""
+ self.instore = IterableFileIterator(input_path)
+ self.outstore = FileStorage(output_path)
def run(self):
- self.convert_header()
- for txn in self.instore:
- # txn is a RecordIterator
- self.convert_transaction(txn)
-
- def convert_header(self):
self.instore._read_metadata()
- self.output.write(FileStorage.packed_version)
+ self.outstore.copyTransactionsFrom(self.instore)
+ self.outstore.close()
+ self.instore.close()
+
+
+class IterableFileIterator(z4iterator.FileIterator):
- def convert_transaction(self, txn):
- """Convert one transaction."""
- data_records = [] # [(z4rec, z3rec), ...]
- extension_data = dumps(txn._extension)
- header = format.TxnHeader(txn.tid,
- None, # tlen
- txn.status,
- len(txn.user or ""),
- len(txn.description or ""),
- len(extension_data))
- header.user = txn.user
- header.descr = txn.description
- header.ext = extension_data
- for datarec in txn:
- assert datarec.serial == txn.tid
- assert not datarec.version
- z3rec = format.DataHeader(datarec.oid,
- datarec.serial, # == tid
- None, # prev
- None, # tloc
- 0, # vlen
- 0, # plen
- )
- # setVersion() not needed since Z3 doesn't use them
- data_records.append((datarec, z3rec))
-
- data = [] #
- if datarec.data is None:
- data = z3rec.asString() + z4utils.p64(???)
- else:
- data = z3rec.asString() + datarec.data
-
- pos = self.output.tell()
- self._data_pos_map[datarec.] = pos
- self.output.write(data)
+ def iterator(self):
+ return self
=== ZODB3/ZODB/zodb4/main.py 1.1.2.1 => 1.1.2.2 ===
--- ZODB3/ZODB/zodb4/main.py:1.1.2.1 Wed Jan 28 08:06:22 2004
+++ ZODB3/ZODB/zodb4/main.py Thu Jan 29 16:31:42 2004
@@ -56,10 +56,11 @@
def run(self):
if not os.path.exists(self.dbfile):
- self.error("input database does not exist: %s" % self.infile)
+ self.error("input database does not exist: %s" % self.dbfile)
base, ext = os.path.splitext(self.dbfile)
if ext != ".fs":
base = self.dbfile
+ self.dbindex = self.dbfile + ".index"
self.bakfile = base + ".fs4"
self.bakindex = self.bakfile + ".index"
if os.path.exists(self.bakfile):
@@ -71,33 +72,23 @@
self.convert()
def convert(self):
- infile_lock = self.bakfile + ".lock"
- outfile_index = self.dbfile + ".index"
- outfile_lock = self.dbfile + ".lock"
- open_files = [LockFile(outfile_lock)]
+ lock = LockFile(self.bakfile + ".lock")
try:
- open_files.append(LockFile(infile_lock))
# move the ZODB 4 database to be the backup
os.rename(self.dbfile, self.bakfile)
- try:
- os.rename(self.dbfile + ".index", self.bakindex)
- except:
- # we couldn't rename *both*, so try to make sure we
- # don't rename either
- os.rename(self.bakfile, self.dbfile)
- raise
- # open the data files:
- input = open(self.infile, "rb")
- open_files.append(input)
- output = open(self.outfile, "wb+")
- open_files.append(output)
+ if os.path.exists(self.dbindex):
+ try:
+ os.rename(self.dbindex, self.bakindex)
+ except:
+ # we couldn't rename *both*, so try to make sure we
+ # don't rename either
+ os.rename(self.bakfile, self.dbfile)
+ raise
# go:
- converter = conversion.Conversion(input, output)
+ converter = conversion.Conversion(self.bakfile, self.dbfile)
converter.run()
finally:
- open_files.reverse()
- for f in open_files:
- f.close()
+ lock.close()
def parse_args(self, args):
opts, args = getopt.getopt(args, "v", ["verbose"])
More information about the Zodb-checkins
mailing list