Commit f1916535 authored by Jim Fulton's avatar Jim Fulton

Finished converting (almost all) lock calls to use the with statement

and colapsed some undo methods.
parent 39fb497b
...@@ -16,10 +16,9 @@ ...@@ -16,10 +16,9 @@
from __future__ import with_statement from __future__ import with_statement
from cPickle import Pickler, Unpickler, loads from cPickle import Pickler, loads
from persistent.TimeStamp import TimeStamp from persistent.TimeStamp import TimeStamp
from struct import pack, unpack from struct import pack, unpack
from types import StringType
from zc.lockfile import LockFile from zc.lockfile import LockFile
from ZODB.FileStorage.format import CorruptedError, CorruptedDataError from ZODB.FileStorage.format import CorruptedError, CorruptedDataError
from ZODB.FileStorage.format import FileStorageFormatter, DataHeader from ZODB.FileStorage.format import FileStorageFormatter, DataHeader
...@@ -28,7 +27,6 @@ from ZODB.FileStorage.format import TxnHeader, DATA_HDR, DATA_HDR_LEN ...@@ -28,7 +27,6 @@ from ZODB.FileStorage.format import TxnHeader, DATA_HDR, DATA_HDR_LEN
from ZODB.FileStorage.fspack import FileStoragePacker from ZODB.FileStorage.fspack import FileStoragePacker
from ZODB.fsIndex import fsIndex from ZODB.fsIndex import fsIndex
from ZODB import BaseStorage, ConflictResolution, POSException from ZODB import BaseStorage, ConflictResolution, POSException
from ZODB.loglevels import BLATHER
from ZODB.POSException import UndoError, POSKeyError, MultipleUndoErrors from ZODB.POSException import UndoError, POSKeyError, MultipleUndoErrors
from ZODB.utils import p64, u64, z64 from ZODB.utils import p64, u64, z64
...@@ -38,7 +36,6 @@ import contextlib ...@@ -38,7 +36,6 @@ import contextlib
import errno import errno
import logging import logging
import os import os
import sys
import threading import threading
import time import time
import ZODB.blob import ZODB.blob
...@@ -427,8 +424,7 @@ class FileStorage( ...@@ -427,8 +424,7 @@ class FileStorage(
raise POSKeyError(oid) raise POSKeyError(oid)
def loadSerial(self, oid, serial): def loadSerial(self, oid, serial):
self._lock_acquire() with self._lock:
try:
pos = self._lookup_pos(oid) pos = self._lookup_pos(oid)
while 1: while 1:
h = self._read_data_header(pos, oid) h = self._read_data_header(pos, oid)
...@@ -441,8 +437,6 @@ class FileStorage( ...@@ -441,8 +437,6 @@ class FileStorage(
return self._file.read(h.plen) return self._file.read(h.plen)
else: else:
return self._loadBack_impl(oid, h.back)[0] return self._loadBack_impl(oid, h.back)[0]
finally:
self._lock_release()
def loadBefore(self, oid, tid): def loadBefore(self, oid, tid):
with self._files.get() as _file: with self._files.get() as _file:
...@@ -471,8 +465,7 @@ class FileStorage( ...@@ -471,8 +465,7 @@ class FileStorage(
raise POSException.StorageTransactionError(self, transaction) raise POSException.StorageTransactionError(self, transaction)
assert not version assert not version
self._lock_acquire() with self._lock:
try:
if oid > self._oid: if oid > self._oid:
self.set_max_oid(oid) self.set_max_oid(oid)
old = self._index_get(oid, 0) old = self._index_get(oid, 0)
...@@ -510,17 +503,13 @@ class FileStorage( ...@@ -510,17 +503,13 @@ class FileStorage(
else: else:
return self._tid return self._tid
finally:
self._lock_release()
def deleteObject(self, oid, oldserial, transaction): def deleteObject(self, oid, oldserial, transaction):
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:
raise POSException.StorageTransactionError(self, transaction) raise POSException.StorageTransactionError(self, transaction)
self._lock_acquire() with self._lock:
try:
old = self._index_get(oid, 0) old = self._index_get(oid, 0)
if not old: if not old:
raise POSException.POSKeyError(oid) raise POSException.POSKeyError(oid)
...@@ -543,9 +532,6 @@ class FileStorage( ...@@ -543,9 +532,6 @@ class FileStorage(
raise FileStorageQuotaError( raise FileStorageQuotaError(
"The storage quota has been exceeded.") "The storage quota has been exceeded.")
finally:
self._lock_release()
def _data_find(self, tpos, oid, data): def _data_find(self, tpos, oid, data):
# Return backpointer for oid. Must call with the lock held. # Return backpointer for oid. Must call with the lock held.
# This is a file offset to oid's data record if found, else 0. # This is a file offset to oid's data record if found, else 0.
...@@ -617,8 +603,7 @@ class FileStorage( ...@@ -617,8 +603,7 @@ class FileStorage(
if version: if version:
raise TypeError("Versions are no-longer supported") raise TypeError("Versions are no-longer supported")
self._lock_acquire() with self._lock:
try:
if oid > self._oid: if oid > self._oid:
self.set_max_oid(oid) self.set_max_oid(oid)
prev_pos = 0 prev_pos = 0
...@@ -654,8 +639,6 @@ class FileStorage( ...@@ -654,8 +639,6 @@ class FileStorage(
self._tfile.write(z64) self._tfile.write(z64)
else: else:
self._tfile.write(data) self._tfile.write(data)
finally:
self._lock_release()
def supportsUndo(self): def supportsUndo(self):
return 1 return 1
...@@ -679,10 +662,8 @@ class FileStorage( ...@@ -679,10 +662,8 @@ class FileStorage(
if len(e) > 65535: if len(e) > 65535:
raise FileStorageError('too much extension data') raise FileStorageError('too much extension data')
def tpc_vote(self, transaction): def tpc_vote(self, transaction):
self._lock_acquire() with self._lock:
try:
if transaction is not self._transaction: if transaction is not self._transaction:
raise POSException.StorageTransactionError( raise POSException.StorageTransactionError(
"tpc_vote called with wrong transaction") "tpc_vote called with wrong transaction")
...@@ -711,8 +692,6 @@ class FileStorage( ...@@ -711,8 +692,6 @@ class FileStorage(
self._file.truncate(self._pos) self._file.truncate(self._pos)
raise raise
self._nextpos = self._pos + (tl + 8) self._nextpos = self._pos + (tl + 8)
finally:
self._lock_release()
def tpc_finish(self, transaction, f=None): def tpc_finish(self, transaction, f=None):
with self._files.write_lock(): with self._files.write_lock():
...@@ -794,16 +773,13 @@ class FileStorage( ...@@ -794,16 +773,13 @@ class FileStorage(
return h.tid, pos, data return h.tid, pos, data
def getTid(self, oid): def getTid(self, oid):
self._lock_acquire() with self._lock:
try:
pos = self._lookup_pos(oid) pos = self._lookup_pos(oid)
h = self._read_data_header(pos, oid) h = self._read_data_header(pos, oid)
if h.plen == 0 and h.back == 0: if h.plen == 0 and h.back == 0:
# Undone creation # Undone creation
raise POSKeyError(oid) raise POSKeyError(oid)
return h.tid return h.tid
finally:
self._lock_release()
def _transactionalUndoRecord(self, oid, pos, tid, pre): def _transactionalUndoRecord(self, oid, pos, tid, pre):
"""Get the undo information for a data record """Get the undo information for a data record
...@@ -887,8 +863,7 @@ class FileStorage( ...@@ -887,8 +863,7 @@ class FileStorage(
# the normalization code was incorrect for years (used +1 # the normalization code was incorrect for years (used +1
# instead -- off by 1), until ZODB 3.4. # instead -- off by 1), until ZODB 3.4.
last = first - last last = first - last
self._lock_acquire() with self._lock:
try:
if self._pack_is_in_progress: if self._pack_is_in_progress:
raise UndoError( raise UndoError(
'Undo is currently disabled for database maintenance.<p>') 'Undo is currently disabled for database maintenance.<p>')
...@@ -905,8 +880,6 @@ class FileStorage( ...@@ -905,8 +880,6 @@ class FileStorage(
self._lock_release() self._lock_release()
self._lock_acquire() self._lock_acquire()
return us.results return us.results
finally:
self._lock_release()
def undo(self, transaction_id, transaction): def undo(self, transaction_id, transaction):
"""Undo a transaction, given by transaction_id. """Undo a transaction, given by transaction_id.
...@@ -925,20 +898,14 @@ class FileStorage( ...@@ -925,20 +898,14 @@ class FileStorage(
if transaction is not self._transaction: if transaction is not self._transaction:
raise POSException.StorageTransactionError(self, transaction) raise POSException.StorageTransactionError(self, transaction)
self._lock_acquire() with self._lock:
try: # Find the right transaction to undo and call _txn_undo_write().
return self._txn_undo(transaction_id) tid = base64.decodestring(transaction_id + '\n')
finally: assert len(tid) == 8
self._lock_release() tpos = self._txn_find(tid, 1)
tindex = self._txn_undo_write(tpos)
def _txn_undo(self, transaction_id): self._tindex.update(tindex)
# Find the right transaction to undo and call _txn_undo_write(). return self._tid, tindex.keys()
tid = base64.decodestring(transaction_id + '\n')
assert len(tid) == 8
tpos = self._txn_find(tid, 1)
tindex = self._txn_undo_write(tpos)
self._tindex.update(tindex)
return self._tid, tindex.keys()
def _txn_find(self, tid, stop_at_pack): def _txn_find(self, tid, stop_at_pack):
pos = self._pos pos = self._pos
...@@ -1027,8 +994,7 @@ class FileStorage( ...@@ -1027,8 +994,7 @@ class FileStorage(
return tindex return tindex
def history(self, oid, size=1, filter=None): def history(self, oid, size=1, filter=None):
self._lock_acquire() with self._lock:
try:
r = [] r = []
pos = self._lookup_pos(oid) pos = self._lookup_pos(oid)
...@@ -1056,8 +1022,6 @@ class FileStorage( ...@@ -1056,8 +1022,6 @@ class FileStorage(
pos = h.prev pos = h.prev
else: else:
return r return r
finally:
self._lock_release()
def _redundant_pack(self, file, pos): def _redundant_pack(self, file, pos):
assert pos > 8, pos assert pos > 8, pos
...@@ -1097,13 +1061,10 @@ class FileStorage( ...@@ -1097,13 +1061,10 @@ class FileStorage(
if not self._index: if not self._index:
return return
self._lock_acquire() with self._lock:
try:
if self._pack_is_in_progress: if self._pack_is_in_progress:
raise FileStorageError('Already packing') raise FileStorageError('Already packing')
self._pack_is_in_progress = True self._pack_is_in_progress = True
finally:
self._lock_release()
if gc is None: if gc is None:
gc = self._pack_gc gc = self._pack_gc
...@@ -1154,18 +1115,14 @@ class FileStorage( ...@@ -1154,18 +1115,14 @@ class FileStorage(
finally: finally:
if have_commit_lock: if have_commit_lock:
self._commit_lock_release() self._commit_lock_release()
self._lock_acquire() with self._lock:
self._pack_is_in_progress = False self._pack_is_in_progress = False
self._lock_release()
if not self.pack_keep_old: if not self.pack_keep_old:
os.remove(oldpath) os.remove(oldpath)
self._lock_acquire() with self._lock:
try:
self._save_index() self._save_index()
finally:
self._lock_release()
def _remove_blob_files_tagged_for_removal_during_pack(self): def _remove_blob_files_tagged_for_removal_during_pack(self):
lblob_dir = len(self.blob_dir) lblob_dir = len(self.blob_dir)
...@@ -1277,8 +1234,7 @@ class FileStorage( ...@@ -1277,8 +1234,7 @@ class FileStorage(
file = self._file file = self._file
seek = file.seek seek = file.seek
read = file.read read = file.read
self._lock_acquire() with self._lock:
try:
pos = self._pos pos = self._pos
while count > 0 and pos > 4: while count > 0 and pos > 4:
count -= 1 count -= 1
...@@ -1288,9 +1244,6 @@ class FileStorage( ...@@ -1288,9 +1244,6 @@ class FileStorage(
seek(0) seek(0)
return [(trans.tid, [r.oid for r in trans]) return [(trans.tid, [r.oid for r in trans])
for trans in FileIterator(self._file_name, pos=pos)] for trans in FileIterator(self._file_name, pos=pos)]
finally:
self._lock_release()
def lastTid(self, oid): def lastTid(self, oid):
"""Return last serialno committed for object oid. """Return last serialno committed for object oid.
......
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