Commit 6726e914 authored by Jeremy Hylton's avatar Jeremy Hylton

Simplify tpc_vote() and reflow some long lines.

The simplifications focus on avoiding tests that can be done
elsewhere and on avoiding excessive use of local variable aliases for
attributes.

Move tests for too-long transaction metadata to _begin(), where the
variables are set instead of here where they are read.
parent be5a10d0
...@@ -115,7 +115,7 @@ ...@@ -115,7 +115,7 @@
# may have a back pointer to a version record or to a non-version # may have a back pointer to a version record or to a non-version
# record. # record.
# #
__version__='$Revision: 1.89 $'[11:-2] __version__='$Revision: 1.90 $'[11:-2]
import struct, time, os, string, base64, sys import struct, time, os, string, base64, sys
from struct import pack, unpack from struct import pack, unpack
...@@ -716,8 +716,8 @@ class FileStorage(BaseStorage.BaseStorage, ...@@ -716,8 +716,8 @@ class FileStorage(BaseStorage.BaseStorage,
# #
# - Nothing is returned # - Nothing is returned
# #
# - data can be None, which indicates a George Bailey object (i.e. one # - data can be None, which indicates a George Bailey object
# who's creation has been transactionally undone). # (i.e. one who's creation has been transactionally undone).
if self._is_read_only: if self._is_read_only:
raise POSException.ReadOnlyError() raise POSException.ReadOnlyError()
if transaction is not self._transaction: if transaction is not self._transaction:
...@@ -727,16 +727,18 @@ class FileStorage(BaseStorage.BaseStorage, ...@@ -727,16 +727,18 @@ class FileStorage(BaseStorage.BaseStorage,
try: try:
# Position of the non-version data # Position of the non-version data
pnv = None pnv = None
# We need to get some information about previous revisions of the # We need to get some information about previous revisions
# object. Specifically, we need the position of the non-version # of the object. Specifically, we need the position of
# data if this update is in a version. We also need the position # the non-version data if this update is in a version. We
# of the previous record in this version. # also need the position of the previous record in this
# version.
old = self._index_get(oid, 0) old = self._index_get(oid, 0)
if old: if old:
self._file.seek(old) self._file.seek(old)
# Read the previous revision record # Read the previous revision record
h = self._file.read(42) h = self._file.read(42)
doid,oserial,sprev,stloc,vlen,splen = unpack(">8s8s8s8sH8s", h) doid,oserial,sprev,stloc,vlen,splen = unpack(">8s8s8s8sH8s",
h)
if doid != oid: if doid != oid:
raise CorruptedDataError, h raise CorruptedDataError, h
# Calculate the file position in the temporary file # Calculate the file position in the temporary file
...@@ -791,62 +793,65 @@ class FileStorage(BaseStorage.BaseStorage, ...@@ -791,62 +793,65 @@ class FileStorage(BaseStorage.BaseStorage,
self._tfile.seek(0) self._tfile.seek(0)
def _begin(self, tid, u, d, e): def _begin(self, tid, u, d, e):
self._thl=TRANS_HDR_LEN+len(u)+len(d)+len(e) self._nextpos = 0
self._nextpos=0 self._thl = TRANS_HDR_LEN + len(u) + len(d) + len(e)
if self._thl > 65535:
# one of u, d, or e may be > 65535
# We have to check lengths here because struct.pack
# doesn't raise an exception on overflow!
if len(u) > 65535:
raise FileStorageError('user name too long')
if len(d) > 65535:
raise FileStorageError('description too long')
if len(e) > 65535:
raise FileStorageError('too much extension data')
def tpc_vote(self, transaction): def tpc_vote(self, transaction):
self._lock_acquire() self._lock_acquire()
try: try:
if transaction is not self._transaction: return if transaction is not self._transaction:
tfile=self._tfile return
dlen=tfile.tell() dlen = self._tfile.tell()
if not dlen: return # No data in this trans if not dlen:
file=self._file return # No data in this trans
write=file.write self._tfile.seek(0)
tfile.seek(0)
tid=self._serial
user, desc, ext = self._ude user, desc, ext = self._ude
luser=len(user) luser = len(user)
ldesc=len(desc) ldesc = len(desc)
lext=len(ext) lext = len(ext)
# We have to check lengths here because struct.pack self._file.seek(self._pos)
# doesn't raise an exception on overflow! tl = self._thl + dlen
if luser > 65535: raise FileStorageError('user name too long') stl = p64(tl)
if ldesc > 65535: raise FileStorageError('description too long')
if lext > 65535: raise FileStorageError('too much extension data')
tlen=self._thl
pos=self._pos
file.seek(pos)
tl=tlen+dlen
stl=p64(tl)
try: try:
# Note that we use a status of 'c', for checkpoint. # Note that we use a status of 'c', for checkpoint.
# If this flag isn't cleared, anything after this is # If this flag isn't cleared, anything after this is
# suspect. # suspect.
write(pack( self._file.write(pack(
">8s" "8s" "c" "H" "H" "H" ">8s" "8s" "c" "H" "H" "H"
,tid, stl,'c', luser, ldesc, lext, ,self._serial, stl,'c', luser, ldesc, lext,
)) ))
if user: write(user) if user:
if desc: write(desc) self._file.write(user)
if ext: write(ext) if desc:
self._file.write(desc)
if ext:
self._file.write(ext)
cp(tfile, file, dlen) cp(self._tfile, self._file, dlen)
write(stl) self._file.write(stl)
file.flush() self._file.flush()
except: except:
# Hm, an error occured writing out the data. Maybe the # Hm, an error occured writing out the data. Maybe the
# disk is full. We don't want any turd at the end. # disk is full. We don't want any turd at the end.
file.truncate(pos) self._file.truncate(self._pos)
raise raise
self._nextpos = self._pos + (tl + 8)
self._nextpos=pos+(tl+8) finally:
self._lock_release()
finally: self._lock_release()
def _finish(self, tid, u, d, e): def _finish(self, tid, u, d, e):
nextpos=self._nextpos nextpos=self._nextpos
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment