Commit 3f5dc8ad authored by Tim Peters's avatar Tim Peters

Mechanical readability and debuggability changes:

- Put horizontal whitespace around most operators.
- Split all compound statements to have one statement
  per physical line.
- Switched to augmented assignment where possible.
parent cdd118d9
...@@ -99,9 +99,9 @@ class DB(object): ...@@ -99,9 +99,9 @@ class DB(object):
- `version_cache_deactivate_after`: ignored - `version_cache_deactivate_after`: ignored
""" """
# Allocate locks: # Allocate locks:
l=allocate_lock() l = allocate_lock()
self._a=l.acquire self._a = l.acquire
self._r=l.release self._r = l.release
# Setup connection pools and cache info # Setup connection pools and cache info
self._pools = {},[] self._pools = {},[]
...@@ -122,7 +122,8 @@ class DB(object): ...@@ -122,7 +122,8 @@ class DB(object):
# Setup storage # Setup storage
self._storage=storage self._storage=storage
storage.registerDB(self, None) storage.registerDB(self, None)
if not hasattr(storage,'tpc_vote'): storage.tpc_vote=lambda *args: None if not hasattr(storage,'tpc_vote'):
storage.tpc_vote = lambda *args: None
try: try:
storage.load('\0\0\0\0\0\0\0\0','') storage.load('\0\0\0\0\0\0\0\0','')
except KeyError: except KeyError:
...@@ -153,15 +154,17 @@ class DB(object): ...@@ -153,15 +154,17 @@ class DB(object):
def _cacheMean(self, attr): def _cacheMean(self, attr):
# XXX this method doesn't work # XXX this method doesn't work
m=[0,0] m = [0, 0]
def f(con, m=m, attr=attr): def f(con, m=m, attr=attr):
t=getattr(con._cache, attr) t = getattr(con._cache, attr)
m[0]=m[0]+t m[0] += t
m[1]=m[1]+1 m[1] += 1
self._connectionMap(f) self._connectionMap(f)
if m[1]: m=m[0]/m[1] if m[1]:
else: m=None m = m[0] / m[1] # XXX should this be // ?
else:
m = None
return m return m
def _closeConnection(self, connection): def _closeConnection(self, connection):
...@@ -189,7 +192,6 @@ class DB(object): ...@@ -189,7 +192,6 @@ class DB(object):
# We need to break circular refs to make it really go. # We need to break circular refs to make it really go.
# XXX What objects are involved in the cycle? # XXX What objects are involved in the cycle?
connection.__dict__.clear() connection.__dict__.clear()
return return
pool.append(connection) pool.append(connection)
...@@ -202,18 +204,21 @@ class DB(object): ...@@ -202,18 +204,21 @@ class DB(object):
def _connectionMap(self, f): def _connectionMap(self, f):
self._a() self._a()
try: try:
pools,pooll=self._pools pools, pooll = self._pools
for pool, allocated in pooll: for pool, allocated in pooll:
for cc in allocated: f(cc) for cc in allocated:
f(cc)
temps=self._temps temps = self._temps
if temps: if temps:
t=[] t = []
rc=sys.getrefcount rc = sys.getrefcount
for cc in temps: for cc in temps:
if rc(cc) > 3: f(cc) if rc(cc) > 3:
self._temps=t f(cc)
finally: self._r() self._temps = t
finally:
self._r()
def abortVersion(self, version, txn=None): def abortVersion(self, version, txn=None):
if txn is None: if txn is None:
...@@ -233,7 +238,7 @@ class DB(object): ...@@ -233,7 +238,7 @@ class DB(object):
module = module and '%s.' % module or '' module = module and '%s.' % module or ''
c = "%s%s" % (module, ob.__class__.__name__) c = "%s%s" % (module, ob.__class__.__name__)
if have_detail(c): if have_detail(c):
detail[c] = detail[c] + 1 detail[c] += 1
else: else:
detail[c] = 1 detail[c] = 1
...@@ -289,8 +294,9 @@ class DB(object): ...@@ -289,8 +294,9 @@ class DB(object):
def cacheLastGCTime(self): def cacheLastGCTime(self):
m=[0] m=[0]
def f(con, m=m): def f(con, m=m):
t=con._cache.cache_last_gc_time t = con._cache.cache_last_gc_time
if t > m[0]: m[0]=t if t > m[0]:
m[0] = t
self._connectionMap(f) self._connectionMap(f)
return m[0] return m[0]
...@@ -298,20 +304,23 @@ class DB(object): ...@@ -298,20 +304,23 @@ class DB(object):
def cacheMinimize(self): def cacheMinimize(self):
self._connectionMap(lambda c: c._cache.minimize()) self._connectionMap(lambda c: c._cache.minimize())
def cacheMeanAge(self): return self._cacheMean('cache_mean_age') def cacheMeanAge(self):
def cacheMeanDeac(self): return self._cacheMean('cache_mean_deac') return self._cacheMean('cache_mean_age')
def cacheMeanDeal(self): return self._cacheMean('cache_mean_deal') def cacheMeanDeac(self):
return self._cacheMean('cache_mean_deac')
def cacheMeanDeal(self):
return self._cacheMean('cache_mean_deal')
def cacheSize(self): def cacheSize(self):
m=[0] m=[0]
def f(con, m=m): def f(con, m=m):
m[0] = m[0] + con._cache.cache_non_ghost_count m[0] += con._cache.cache_non_ghost_count
self._connectionMap(f) self._connectionMap(f)
return m[0] return m[0]
def cacheDetailSize(self): def cacheDetailSize(self):
m=[] m = []
def f(con, m=m): def f(con, m=m):
m.append({'connection':repr(con), m.append({'connection':repr(con),
'ngsize':con._cache.cache_non_ghost_count, 'ngsize':con._cache.cache_non_ghost_count,
...@@ -346,11 +355,14 @@ class DB(object): ...@@ -346,11 +355,14 @@ class DB(object):
def lastTransaction(self): def lastTransaction(self):
return self._storage.lastTransaction() return self._storage.lastTransaction()
def getName(self): return self._storage.getName() def getName(self):
return self._storage.getName()
def getPoolSize(self): return self._pool_size def getPoolSize(self):
return self._pool_size
def getSize(self): return self._storage.getSize() def getSize(self):
return self._storage.getSize()
def getVersionCacheSize(self): def getVersionCacheSize(self):
return self._version_cache_size return self._version_cache_size
...@@ -367,13 +379,14 @@ class DB(object): ...@@ -367,13 +379,14 @@ class DB(object):
connection. connection.
""" """
if connection is not None: if connection is not None:
version=connection._version version = connection._version
# Update modified in version cache # Update modified in version cache
# XXX must make this work with list or dict to backport to 2.6 # XXX must make this work with list or dict to backport to 2.6
for oid in oids.keys(): for oid in oids.keys():
h=hash(oid)%131 h = hash(oid) % 131
o=self._miv_cache.get(h, None) o = self._miv_cache.get(h, None)
if o is not None and o[0]==oid: del self._miv_cache[h] if o is not None and o[0]==oid:
del self._miv_cache[h]
# Notify connections # Notify connections
for pool, allocated in self._pools[1]: for pool, allocated in self._pools[1]:
...@@ -385,7 +398,7 @@ class DB(object): ...@@ -385,7 +398,7 @@ class DB(object):
cc.invalidate(tid, oids) cc.invalidate(tid, oids)
if self._temps: if self._temps:
t=[] t = []
for cc in self._temps: for cc in self._temps:
if sys.getrefcount(cc) > 3: if sys.getrefcount(cc) > 3:
if (cc is not connection and if (cc is not connection and
...@@ -397,13 +410,13 @@ class DB(object): ...@@ -397,13 +410,13 @@ class DB(object):
self._temps = t self._temps = t
def modifiedInVersion(self, oid): def modifiedInVersion(self, oid):
h=hash(oid)%131 h = hash(oid) % 131
cache=self._miv_cache cache = self._miv_cache
o=cache.get(h, None) o=cache.get(h, None)
if o and o[0]==oid: if o and o[0]==oid:
return o[1] return o[1]
v=self._storage.modifiedInVersion(oid) v = self._storage.modifiedInVersion(oid)
cache[h]=oid, v cache[h] = oid, v
return v return v
def objectCount(self): def objectCount(self):
...@@ -464,7 +477,6 @@ class DB(object): ...@@ -464,7 +477,6 @@ class DB(object):
transaction[id(c)] = c transaction[id(c)] = c
return c return c
pools, pooll = self._pools pools, pooll = self._pools
# pools is a mapping object: # pools is a mapping object:
...@@ -526,7 +538,8 @@ class DB(object): ...@@ -526,7 +538,8 @@ class DB(object):
# Note that the pool size will normally be 1 here, # Note that the pool size will normally be 1 here,
# but it could be higher due to a race condition. # but it could be higher due to a race condition.
pool_lock.release() pool_lock.release()
else: return else:
return
elif len(pool)==1: elif len(pool)==1:
# Taking last one, lock the pool. # Taking last one, lock the pool.
...@@ -585,17 +598,19 @@ class DB(object): ...@@ -585,17 +598,19 @@ class DB(object):
del allocated[:] del allocated[:]
def connectionDebugInfo(self): def connectionDebugInfo(self):
r=[] r = []
pools,pooll=self._pools pools, pooll = self._pools
t=time() t = time()
for version, (pool, allocated, lock) in pools.items(): for version, (pool, allocated, lock) in pools.items():
for c in allocated: for c in allocated:
o=c._opened o = c._opened
d=c._debug_info d = c._debug_info
if d: if d:
if len(d)==1: d=d[0] if len(d)==1:
else: d='' d = d[0]
d="%s (%s)" % (d, len(c._cache)) else:
d=''
d = "%s (%s)" % (d, len(c._cache))
r.append({ r.append({
'opened': o and ("%s (%.2fs)" % (ctime(o), t-o)), 'opened': o and ("%s (%.2fs)" % (ctime(o), t-o)),
...@@ -644,21 +659,23 @@ class DB(object): ...@@ -644,21 +659,23 @@ class DB(object):
return find_global(modulename, globalname) return find_global(modulename, globalname)
def setPoolSize(self, v): def setPoolSize(self, v):
self._pool_size=v self._pool_size = v
def setActivityMonitor(self, am): def setActivityMonitor(self, am):
self._activity_monitor = am self._activity_monitor = am
def setVersionCacheSize(self, v): def setVersionCacheSize(self, v):
self._version_cache_size=v self._version_cache_size = v
for ver in self._pools[0].keys(): for ver in self._pools[0].keys():
if ver: if ver:
for c in self._pools[0][ver][1]: for c in self._pools[0][ver][1]:
c._cache.cache_size=v c._cache.cache_size = v
def setVersionPoolSize(self, v): self._version_pool_size=v def setVersionPoolSize(self, v):
self._version_pool_size=v
def cacheStatistics(self): return () # :( def cacheStatistics(self):
return () # :(
def undo(self, id, txn=None): def undo(self, id, txn=None):
"""Undo a transaction identified by id. """Undo a transaction identified by id.
...@@ -742,8 +759,9 @@ class CommitVersion(ResourceManager): ...@@ -742,8 +759,9 @@ class CommitVersion(ResourceManager):
self._dest = dest self._dest = dest
def commit(self, ob, t): def commit(self, ob, t):
dest=self._dest dest = self._dest
tid, oids = self._db._storage.commitVersion(self._version, self._dest, tid, oids = self._db._storage.commitVersion(self._version,
self._dest,
t) t)
oids = dict.fromkeys(oids, 1) oids = dict.fromkeys(oids, 1)
self._db.invalidate(tid, oids, version=self._dest) self._db.invalidate(tid, oids, version=self._dest)
...@@ -760,7 +778,9 @@ class AbortVersion(ResourceManager): ...@@ -760,7 +778,9 @@ class AbortVersion(ResourceManager):
def commit(self, ob, t): def commit(self, ob, t):
tid, oids = self._db._storage.abortVersion(self._version, t) tid, oids = self._db._storage.abortVersion(self._version, t)
self._db.invalidate(tid, dict.fromkeys(oids, 1), version=self._version) self._db.invalidate(tid,
dict.fromkeys(oids, 1),
version=self._version)
class TransactionalUndo(ResourceManager): class TransactionalUndo(ResourceManager):
......
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