Skip to content

Commit

Permalink
Use storage._lock as a context manager, also PEP8/cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
hannosch committed Sep 9, 2016
1 parent 125359a commit 96def86
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 36 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changelog
3.1 - unreleased
----------------

- Use `storage._lock` as a context manager.

- Declare PyPy compatibility.

3.0 - 2016-04-03
Expand Down
47 changes: 15 additions & 32 deletions src/tempstorage/TemporaryStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,11 @@ def close(self):
"""

def load(self, oid, version=''):
self._lock_acquire()
try:
with self._lock:
try:
s = self._index[oid]
p = self._opickle[oid]
return p, s # pickle, serial
return p, s # pickle, serial
except KeyError:
# this oid was probably garbage collected while a thread held
# on to an object that had a reference to it; we can probably
Expand All @@ -148,8 +147,6 @@ def load(self, oid, version=''):
raise POSException.ConflictError(oid=oid)
else:
raise
finally:
self._lock_release()

# 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
Expand All @@ -170,70 +167,61 @@ def loadSerial(self, oid, serial, marker=[]):
It does not actually implement all the semantics that a revisioning
storage needs!
"""
self._lock_acquire()
try:
with self._lock:
data = self._conflict_cache.get((oid, serial), marker)
if data is marker:
# XXX Need 2 serialnos to pass them to ConflictError--
# the old and the new
raise POSException.ConflictError(oid=oid)
else:
return data[0] # data here is actually (data, t)
finally:
self._lock_release()
return data[0] # data here is actually (data, t)

def loadBefore(self, oid, tid):
""" Return most recent revision of oid before tid committed.
Needed for MVCC.
"""
# implementation stolen from ZODB.test_storage.MinimalMemoryStorage
self._lock_acquire()
try:
with self._lock:
tids = [stid for soid, stid in self._conflict_cache if soid == oid]
if not tids:
raise KeyError(oid)
tids.sort()
i = bisect.bisect_left(tids, tid) -1
i = bisect.bisect_left(tids, tid) - 1
if i == -1:
return None
start_tid = tids[i]
j = i + 1
if j == len(tids):
return None # the caller can't deal with current data
return None # the caller can't deal with current data
else:
end_tid = tids[j]
data = self.loadSerial(oid, start_tid)
return data, start_tid, end_tid
finally:
self._lock_release()

def store(self, oid, serial, data, version, transaction):
if transaction is not self._transaction:
raise POSException.StorageTransactionError(self, transaction)
assert not version

self._lock_acquire()
try:
with self._lock:
if oid in self._index:
oserial = self._index[oid]
if serial != oserial:
newdata = self.tryToResolveConflict(
oid, oserial, serial, data)
oid, oserial, serial, data)
if not newdata:
raise POSException.ConflictError(
oid=oid,
serials=(oserial, serial),
data=data)
oid=oid,
serials=(oserial, serial),
data=data)
else:
data = newdata
else:
oserial = serial
newserial = self._tid
self._tmp.append((oid, data))
return serial == oserial and newserial or ResolvedSerial
finally:
self._lock_release()

def _finish(self, tid, u, d, e):
zeros = {}
Expand All @@ -259,7 +247,6 @@ def _finish(self, tid, u, d, e):
# doesn't already exist
if referenceCount_get(oid) is None:
referenceCount[oid] = 0
#zeros[oid]=1

# update references that are already associated with this
# object
Expand All @@ -276,7 +263,7 @@ def _finish(self, tid, u, d, e):
oreferences[oid].remove(roid)
# decrement refcnt:
rc = referenceCount_get(roid, 1)
rc = rc-1
rc = rc - 1
if rc < 0:
# This should never happen
raise ReferenceCountError(
Expand Down Expand Up @@ -357,16 +344,14 @@ def _takeOutGarbage(self, oid):
(ReferenceCountError.__doc__, roid, rc))
else:
# DM 2005-01-07: decremented *before* the test! see above
#referenceCount[roid] = rc - 1
referenceCount[roid] = rc
try:
del self._oreferences[oid]
except Exception:
pass

def pack(self, t, referencesf):
self._lock_acquire()
try:
with self._lock:
rindex = {}
rootl = ['\0\0\0\0\0\0\0\0']

Expand All @@ -381,7 +366,5 @@ def pack(self, t, referencesf):

# sweep unreferenced objects
for oid in self._index.keys():
if not oid in rindex:
if oid not in rindex:
self._takeOutGarbage(oid)
finally:
self._lock_release()
7 changes: 3 additions & 4 deletions src/tempstorage/tests/testTemporaryStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class ZODBProtocolTests(StorageTestBase.StorageTestBase,
BasicStorage.BasicStorage,
Synchronization.SynchronizedStorage,
ConflictResolution.ConflictResolvingStorage,
MTStorage.MTStorage,
):
MTStorage.MTStorage):

def setUp(self):
StorageTestBase.StorageTestBase.setUp(self)
Expand Down Expand Up @@ -133,7 +132,7 @@ def test_conflict_cache_clears_over_time(self):
import time
from ZODB.tests.MinPO import MinPO
storage = self._makeOne()
storage._conflict_cache_gcevery = 1 # second
storage._conflict_cache_gcevery = 1 # second
storage._conflict_cache_maxage = 1 # second

oid = storage.new_oid()
Expand Down Expand Up @@ -181,7 +180,7 @@ def test_load_ex_matches_load(self):
def test_suite():
return unittest.TestSuite((
unittest.makeSuite(TemporaryStorageTests),
# Note: we follow the ZODB 'check' pattern here so that the base
# Note: we follow the ZODB 'check' pattern here so that the base
# class tests are picked up.
unittest.makeSuite(ZODBProtocolTests, 'check'),
))

0 comments on commit 96def86

Please sign in to comment.