[Zope-CVS] CVS: Packages/PartitionedFileStorage - FileStorage_py_1_98_2_7.patch:1.1
Shane Hathaway
shane@zope.com
Wed, 23 Apr 2003 14:40:26 -0400
Update of /cvs-repository/Packages/PartitionedFileStorage
In directory cvs.zope.org:/tmp/cvs-serv10478
Added Files:
FileStorage_py_1_98_2_7.patch
Log Message:
This version of the patch applies cleanly to Zope 2.6-branch
=== Added File Packages/PartitionedFileStorage/FileStorage_py_1_98_2_7.patch ===
Index: FileStorage.py
===================================================================
RCS file: /cvs-repository/Zope/lib/python/ZODB/FileStorage.py,v
retrieving revision 1.98.2.7
diff -u -u -r1.98.2.7 FileStorage.py
--- FileStorage.py 17 Mar 2003 19:00:20 -0000 1.98.2.7
+++ FileStorage.py 23 Apr 2003 18:36:29 -0000
@@ -197,6 +197,43 @@
packed_version='FS21'
+
+class FileOperations:
+ '''This class implements some basic operations on files.
+ PartitionedFileOperations provides an alternate implementation.
+ '''
+ def open(self, name, mode='r', bufsize=0):
+ global open
+ return open(name, mode, bufsize)
+
+ def exists(self, name):
+ return os.path.exists(name)
+
+ def rename(self, oldname, newname):
+ os.rename(oldname, newname)
+
+ def remove(self, name):
+ os.remove(name)
+
+ def fsync(self, file):
+ global fsync
+ if fsync is not None:
+ fsync(file.fileno())
+
+defaultFops = FileOperations()
+
+
+# Set the PARTITIONED_FILE_SIZE environment variable to use
+# PartitionedFile support. Note: To convert a partitioned file back
+# to a normal file, just 'cat' (Unix) or 'copy' (DOS/Windows) the
+# partitions together in order.
+PARTITIONED_FILE_SIZE = long(os.environ.get('PARTITIONED_FILE_SIZE', 0))
+if PARTITIONED_FILE_SIZE > 0:
+ from PartitionedFile import PartitionedFileOperations
+ defaultFops = PartitionedFileOperations(partlen=PARTITIONED_FILE_SIZE)
+
+
+
class FileStorage(BaseStorage.BaseStorage,
ConflictResolution.ConflictResolvingStorage):
# default pack time is 0
@@ -205,7 +242,10 @@
_records_before_save = 10000
def __init__(self, file_name, create=0, read_only=0, stop=None,
- quota=None):
+ quota=None, fops=None):
+ if fops is None:
+ fops = defaultFops
+ self.fops = fops
if read_only:
self._is_read_only = 1
@@ -247,7 +287,8 @@
self._file = None
if not create:
try:
- self._file = open(file_name, read_only and 'rb' or 'r+b')
+ self._file = self.fops.open(
+ file_name, read_only and 'rb' or 'r+b')
except IOError, exc:
if exc.errno == errno.EFBIG:
# The file is too big to open. Fail visibly.
@@ -258,15 +299,15 @@
# If something else went wrong, it's hard to guess
# what the problem was. If the file does not exist,
# create it. Otherwise, fail.
- if os.path.exists(file_name):
+ if self.fops.exists(file_name):
raise
else:
create = 1
if self._file is None and create:
- if os.path.exists(file_name):
- os.remove(file_name)
- self._file = open(file_name, 'w+b')
+ if self.fops.exists(file_name):
+ self.fops.remove(file_name)
+ self._file = self.fops.open(file_name, 'w+b')
self._file.write(packed_version)
r = self._restore_index()
@@ -1004,7 +1045,7 @@
file.write(self._tstatus)
file.flush()
- if fsync is not None: fsync(file.fileno())
+ self.fops.fsync(file)
self._pos=nextpos
@@ -1489,7 +1530,7 @@
_commit_lock_release=self._commit_lock_release
index, vindex, tindex, tvindex = self._newIndexes()
name=self.__name__
- file=open(name, 'rb')
+ file=self.fops.open(name, 'r+b')
stop=`apply(TimeStamp, time.gmtime(t)[:5]+(t%60,))`
if stop==z64: raise FileStorageError, 'Invalid pack time'
@@ -1548,7 +1589,7 @@
# Step 2, copy data and compute new index based on new positions.
index, vindex, tindex, tvindex = self._newIndexes()
- ofile=open(name+'.pack', 'w+b')
+ ofile=self.fops.open(name+'.pack', 'w+b')
# Index for non-version data. This is a temporary structure
# to reduce I/O during packing
@@ -1592,7 +1633,7 @@
# continuing
ofile.close()
file.close()
- os.remove(name+'.pack')
+ self.fops.remove(name+'.pack')
return
packing=0
@@ -1830,17 +1871,17 @@
file.close()
self._file.close()
try:
- if os.path.exists(name+'.old'):
- os.remove(name+'.old')
- os.rename(name, name+'.old')
+ if self.fops.exists(name+'.old'):
+ self.fops.remove(name+'.old')
+ self.fops.rename(name, name+'.old')
except:
# Waaa
- self._file=open(name,'r+b')
+ self._file=self.fops.open(name, 'r+b')
raise
# OK, we're beyond the point of no return
- os.rename(name+'.pack', name)
- self._file=open(name,'r+b')
+ self.fops.rename(name+'.pack', name)
+ self._file=self.fops.open(name, 'r+b')
self._initIndex(index, vindex, tindex, tvindex)
self._pos=opos
self._save_index()
@@ -2016,8 +2057,10 @@
return p, s
-def recover(file_name):
- file=open(file_name, 'r+b')
+def recover(file_name, fops=None):
+ if fops is None:
+ fops = defaultFops
+ file=fops.open(file_name, 'r+b')
index={}
vindex={}
tindex={}
@@ -2267,7 +2310,9 @@
# seek to transaction header, where tid is first 8 bytes
return file.read(8)
-def _truncate(file, name, pos):
+def _truncate(file, name, pos, fops=None):
+ if fops is None:
+ fops = defaultFops
seek=file.seek
seek(0,2)
file_size=file.tell()
@@ -2275,11 +2320,11 @@
i=0
while 1:
oname='%s.tr%s' % (name, i)
- if os.path.exists(oname):
+ if fops.exists(oname):
i=i+1
else:
warn("Writing truncated data from %s to %s", name, oname)
- o=open(oname,'wb')
+ o = fops.open(oname, 'wb')
seek(pos)
cp(file, o, file_size-pos)
o.close()
@@ -2314,9 +2359,11 @@
_ltid = z64
_file = None
- def __init__(self, file, start=None, stop=None):
+ def __init__(self, file, start=None, stop=None, fops=None):
+ if fops is None:
+ fops = defaultFops
if isinstance(file, StringType):
- file = open(file, 'rb')
+ file = fops.open(file, 'rb')
self._file = file
if file.read(4) != packed_version:
raise FileStorageFormatError, file.name