Commit 8ff1520f authored by Dmitry Vasiliev's avatar Dmitry Vasiliev

Merge rev 28208 from ZODB 3.3 branch.

Code cleanups.
parent fbb50b05
...@@ -22,15 +22,17 @@ The Mapping storage uses a single data structure to map object ids to data. ...@@ -22,15 +22,17 @@ The Mapping storage uses a single data structure to map object ids to data.
""" """
from ZODB.utils import u64, z64 from ZODB.utils import u64, z64
from ZODB import BaseStorage from ZODB.BaseStorage import BaseStorage
from ZODB import POSException from ZODB import POSException
from persistent.TimeStamp import TimeStamp from persistent.TimeStamp import TimeStamp
class MappingStorage(BaseStorage.BaseStorage): class MappingStorage(BaseStorage):
def __init__(self, name='Mapping Storage'): def __init__(self, name='Mapping Storage'):
BaseStorage.BaseStorage.__init__(self, name) BaseStorage.__init__(self, name)
self._index = {} self._index = {}
# FIXME: Why we don't use dict for _tindex?
self._tindex = [] self._tindex = []
self._ltid = None self._ltid = None
# Note: If you subclass this and use a persistent mapping facility # Note: If you subclass this and use a persistent mapping facility
...@@ -41,12 +43,15 @@ class MappingStorage(BaseStorage.BaseStorage): ...@@ -41,12 +43,15 @@ class MappingStorage(BaseStorage.BaseStorage):
return len(self._index) return len(self._index)
def getSize(self): def getSize(self):
# These constants are for Python object memory overheads self._lock_acquire()
s = 32 try:
for oid in self._index.keys(): # These constants are for Python object memory overheads
p = self._index[oid] s = 32
s += 56 + len(p) for p in self._index.itervalues():
return s s += 56 + len(p)
return s
finally:
self._lock_release()
def load(self, oid, version): def load(self, oid, version):
self._lock_acquire() self._lock_acquire()
...@@ -70,8 +75,7 @@ class MappingStorage(BaseStorage.BaseStorage): ...@@ -70,8 +75,7 @@ class MappingStorage(BaseStorage.BaseStorage):
self._lock_acquire() self._lock_acquire()
try: try:
# The tid is the first 8 bytes of the buffer. # The tid is the first 8 bytes of the buffer.
s = self._index[oid] return self._index[oid][:8]
return s[:8]
finally: finally:
self._lock_release() self._lock_release()
...@@ -81,13 +85,12 @@ class MappingStorage(BaseStorage.BaseStorage): ...@@ -81,13 +85,12 @@ class MappingStorage(BaseStorage.BaseStorage):
raise POSException.StorageTransactionError(self, transaction) raise POSException.StorageTransactionError(self, transaction)
if version: if version:
raise POSException.Unsupported, "Versions aren't supported" raise POSException.Unsupported("Versions aren't supported")
self._lock_acquire() self._lock_acquire()
try: try:
if self._index.has_key(oid): if oid in self._index:
old = self._index[oid] oserial = self._index[oid][:8]
oserial = old[:8]
if serial != oserial: if serial != oserial:
raise POSException.ConflictError(oid=oid, raise POSException.ConflictError(oid=oid,
serials=(oserial, serial), serials=(oserial, serial),
...@@ -102,8 +105,7 @@ class MappingStorage(BaseStorage.BaseStorage): ...@@ -102,8 +105,7 @@ class MappingStorage(BaseStorage.BaseStorage):
self._tindex = [] self._tindex = []
def _finish(self, tid, user, desc, ext): def _finish(self, tid, user, desc, ext):
for oid, p in self._tindex: self._index.update(dict(self._tindex))
self._index[oid] = p
self._ltid = self._tid self._ltid = self._tid
def lastTransaction(self): def lastTransaction(self):
...@@ -119,17 +121,16 @@ class MappingStorage(BaseStorage.BaseStorage): ...@@ -119,17 +121,16 @@ class MappingStorage(BaseStorage.BaseStorage):
pindex = {} pindex = {}
while rootl: while rootl:
oid = rootl.pop() oid = rootl.pop()
if pindex.has_key(oid): if oid in pindex:
continue continue
# Scan non-version pickle for references # Scan non-version pickle for references
r = self._index[oid] r = self._index[oid]
pindex[oid] = r pindex[oid] = r
p = r[8:] referencesf(r[8:], rootl)
referencesf(p, rootl)
# Now delete any unreferenced entries: # Now delete any unreferenced entries:
for oid in self._index.keys(): for oid in self._index.keys():
if not pindex.has_key(oid): if oid not in pindex:
del self._index[oid] del self._index[oid]
finally: finally:
...@@ -137,13 +138,12 @@ class MappingStorage(BaseStorage.BaseStorage): ...@@ -137,13 +138,12 @@ class MappingStorage(BaseStorage.BaseStorage):
def _splat(self): def _splat(self):
"""Spit out a string showing state.""" """Spit out a string showing state."""
o = [] o = ['Index:']
o.append('Index:')
keys = self._index.keys() keys = self._index.keys()
keys.sort() keys.sort()
for oid in keys: for oid in keys:
r = self._index[oid] r = self._index[oid]
o.append(' %s: %s, %s' % o.append(' %s: %s, %s' %
(u64(oid),TimeStamp(r[:8]),`r[8:]`)) (u64(oid), TimeStamp(r[:8]), repr(r[8:])))
return '\n'.join(o) return '\n'.join(o)
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