Commit 96def86d authored by Hanno Schlichting's avatar Hanno Schlichting

Use `storage._lock` as a context manager, also PEP8/cleanup.

parent 125359ad
...@@ -4,6 +4,8 @@ Changelog ...@@ -4,6 +4,8 @@ Changelog
3.1 - unreleased 3.1 - unreleased
---------------- ----------------
- Use `storage._lock` as a context manager.
- Declare PyPy compatibility. - Declare PyPy compatibility.
3.0 - 2016-04-03 3.0 - 2016-04-03
......
...@@ -129,8 +129,7 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage): ...@@ -129,8 +129,7 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage):
""" """
def load(self, oid, version=''): def load(self, oid, version=''):
self._lock_acquire() with self._lock:
try:
try: try:
s = self._index[oid] s = self._index[oid]
p = self._opickle[oid] p = self._opickle[oid]
...@@ -148,8 +147,6 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage): ...@@ -148,8 +147,6 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage):
raise POSException.ConflictError(oid=oid) raise POSException.ConflictError(oid=oid)
else: else:
raise raise
finally:
self._lock_release()
# Apparently loadEx is required to use this as a ZEO storage for # Apparently loadEx is required to use this as a ZEO storage for
# ZODB 3.3. The tests don't make it totally clear what it's meant # ZODB 3.3. The tests don't make it totally clear what it's meant
...@@ -170,8 +167,7 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage): ...@@ -170,8 +167,7 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage):
It does not actually implement all the semantics that a revisioning It does not actually implement all the semantics that a revisioning
storage needs! storage needs!
""" """
self._lock_acquire() with self._lock:
try:
data = self._conflict_cache.get((oid, serial), marker) data = self._conflict_cache.get((oid, serial), marker)
if data is marker: if data is marker:
# XXX Need 2 serialnos to pass them to ConflictError-- # XXX Need 2 serialnos to pass them to ConflictError--
...@@ -179,8 +175,6 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage): ...@@ -179,8 +175,6 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage):
raise POSException.ConflictError(oid=oid) raise POSException.ConflictError(oid=oid)
else: else:
return data[0] # data here is actually (data, t) return data[0] # data here is actually (data, t)
finally:
self._lock_release()
def loadBefore(self, oid, tid): def loadBefore(self, oid, tid):
""" Return most recent revision of oid before tid committed. """ Return most recent revision of oid before tid committed.
...@@ -188,13 +182,12 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage): ...@@ -188,13 +182,12 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage):
Needed for MVCC. Needed for MVCC.
""" """
# implementation stolen from ZODB.test_storage.MinimalMemoryStorage # implementation stolen from ZODB.test_storage.MinimalMemoryStorage
self._lock_acquire() with self._lock:
try:
tids = [stid for soid, stid in self._conflict_cache if soid == oid] tids = [stid for soid, stid in self._conflict_cache if soid == oid]
if not tids: if not tids:
raise KeyError(oid) raise KeyError(oid)
tids.sort() tids.sort()
i = bisect.bisect_left(tids, tid) -1 i = bisect.bisect_left(tids, tid) - 1
if i == -1: if i == -1:
return None return None
start_tid = tids[i] start_tid = tids[i]
...@@ -205,16 +198,13 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage): ...@@ -205,16 +198,13 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage):
end_tid = tids[j] end_tid = tids[j]
data = self.loadSerial(oid, start_tid) data = self.loadSerial(oid, start_tid)
return data, start_tid, end_tid return data, start_tid, end_tid
finally:
self._lock_release()
def store(self, oid, serial, data, version, transaction): def store(self, oid, serial, data, version, transaction):
if transaction is not self._transaction: if transaction is not self._transaction:
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 in self._index: if oid in self._index:
oserial = self._index[oid] oserial = self._index[oid]
if serial != oserial: if serial != oserial:
...@@ -232,8 +222,6 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage): ...@@ -232,8 +222,6 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage):
newserial = self._tid newserial = self._tid
self._tmp.append((oid, data)) self._tmp.append((oid, data))
return serial == oserial and newserial or ResolvedSerial return serial == oserial and newserial or ResolvedSerial
finally:
self._lock_release()
def _finish(self, tid, u, d, e): def _finish(self, tid, u, d, e):
zeros = {} zeros = {}
...@@ -259,7 +247,6 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage): ...@@ -259,7 +247,6 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage):
# doesn't already exist # doesn't already exist
if referenceCount_get(oid) is None: if referenceCount_get(oid) is None:
referenceCount[oid] = 0 referenceCount[oid] = 0
#zeros[oid]=1
# update references that are already associated with this # update references that are already associated with this
# object # object
...@@ -276,7 +263,7 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage): ...@@ -276,7 +263,7 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage):
oreferences[oid].remove(roid) oreferences[oid].remove(roid)
# decrement refcnt: # decrement refcnt:
rc = referenceCount_get(roid, 1) rc = referenceCount_get(roid, 1)
rc = rc-1 rc = rc - 1
if rc < 0: if rc < 0:
# This should never happen # This should never happen
raise ReferenceCountError( raise ReferenceCountError(
...@@ -357,7 +344,6 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage): ...@@ -357,7 +344,6 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage):
(ReferenceCountError.__doc__, roid, rc)) (ReferenceCountError.__doc__, roid, rc))
else: else:
# DM 2005-01-07: decremented *before* the test! see above # DM 2005-01-07: decremented *before* the test! see above
#referenceCount[roid] = rc - 1
referenceCount[roid] = rc referenceCount[roid] = rc
try: try:
del self._oreferences[oid] del self._oreferences[oid]
...@@ -365,8 +351,7 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage): ...@@ -365,8 +351,7 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage):
pass pass
def pack(self, t, referencesf): def pack(self, t, referencesf):
self._lock_acquire() with self._lock:
try:
rindex = {} rindex = {}
rootl = ['\0\0\0\0\0\0\0\0'] rootl = ['\0\0\0\0\0\0\0\0']
...@@ -381,7 +366,5 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage): ...@@ -381,7 +366,5 @@ class TemporaryStorage(BaseStorage, ConflictResolvingStorage):
# sweep unreferenced objects # sweep unreferenced objects
for oid in self._index.keys(): for oid in self._index.keys():
if not oid in rindex: if oid not in rindex:
self._takeOutGarbage(oid) self._takeOutGarbage(oid)
finally:
self._lock_release()
...@@ -25,8 +25,7 @@ class ZODBProtocolTests(StorageTestBase.StorageTestBase, ...@@ -25,8 +25,7 @@ class ZODBProtocolTests(StorageTestBase.StorageTestBase,
BasicStorage.BasicStorage, BasicStorage.BasicStorage,
Synchronization.SynchronizedStorage, Synchronization.SynchronizedStorage,
ConflictResolution.ConflictResolvingStorage, ConflictResolution.ConflictResolvingStorage,
MTStorage.MTStorage, MTStorage.MTStorage):
):
def setUp(self): def setUp(self):
StorageTestBase.StorageTestBase.setUp(self) StorageTestBase.StorageTestBase.setUp(self)
......
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