Commit 3b2df02b authored by Yoshinori Okuji's avatar Yoshinori Okuji

Go back to seb's fix.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@4459 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent be9305bb
...@@ -84,7 +84,7 @@ def manage_addSQLCatalog(self, id, title, ...@@ -84,7 +84,7 @@ def manage_addSQLCatalog(self, id, title,
class UidBuffer(TM): class UidBuffer(TM):
"""Uid Buffer class caches a list of reserved uids in a transaction-safe way.""" """Uid Buffer class caches a list of reserved uids in a transaction-safe way."""
def __init__(self, catalog): def __init__(self):
"""Initialize some variables. """Initialize some variables.
temporary_buffer is used to hold reserved uids created by non-committed transactions. temporary_buffer is used to hold reserved uids created by non-committed transactions.
...@@ -92,37 +92,9 @@ class UidBuffer(TM): ...@@ -92,37 +92,9 @@ class UidBuffer(TM):
finished_buffer is used to hold reserved uids created by committed-transactions. finished_buffer is used to hold reserved uids created by committed-transactions.
This distinction is important, because uids by non-committed transactions might become This distinction is important, because uids by non-committed transactions might become
invalid afterwards, so they may not be used by other transactions. invalid afterwards, so they may not be used by other transactions."""
allocated_buffer is used to hold reserved uids consumed by non-committed transactions.
This buffer is used to mark uids as already consumed to prevent the same uids from being
selected by produceUid again."""
self.temporary_buffer = {} self.temporary_buffer = {}
self.finished_buffer = [] self.finished_buffer = []
self.allocated_buffer = {}
self.catalog = catalog
def _begin(self, *ignored):
# In Zope 2.8 (ZODB 3.4), use beforeCommitHook instead of
# patching Trasaction.
transaction = get_transaction()
try:
transaction.beforeCommitHook(self.tpc_prepare, transaction)
except AttributeError:
pass
def tpc_prepare(self, transaction, sub=None):
"""Mark used uids."""
tid = get_ident()
try:
uid_list = self.allocated_buffer[tid]
method_id = self.catalog.sql_catalog_reserve_uid
method = getattr(self.catalog, method_id)
method(uid = uid_list)
del self.allocated_buffer[tid]
except KeyError:
pass
def _finish(self): def _finish(self):
"""Move the uids in the temporary buffer to the finished buffer.""" """Move the uids in the temporary buffer to the finished buffer."""
...@@ -132,10 +104,6 @@ class UidBuffer(TM): ...@@ -132,10 +104,6 @@ class UidBuffer(TM):
del self.temporary_buffer[tid] del self.temporary_buffer[tid]
except KeyError: except KeyError:
pass pass
try:
del self.allocated_buffer[tid]
except KeyError:
pass
def _abort(self): def _abort(self):
"""Erase the uids in the temporary buffer.""" """Erase the uids in the temporary buffer."""
...@@ -144,10 +112,6 @@ class UidBuffer(TM): ...@@ -144,10 +112,6 @@ class UidBuffer(TM):
del self.temporary_buffer[tid] del self.temporary_buffer[tid]
except KeyError: except KeyError:
pass pass
try:
del self.allocated_buffer[tid]
except KeyError:
pass
def __len__(self): def __len__(self):
tid = get_ident() tid = get_ident()
...@@ -165,11 +129,6 @@ class UidBuffer(TM): ...@@ -165,11 +129,6 @@ class UidBuffer(TM):
uid_list.remove(value) uid_list.remove(value)
except ValueError: except ValueError:
pass pass
for uid_list in self.allocated_buffer.values():
try:
uid_list.remove(value)
except ValueError:
pass
try: try:
self.finished_buffer.remove(value) self.finished_buffer.remove(value)
except ValueError: except ValueError:
...@@ -182,21 +141,12 @@ class UidBuffer(TM): ...@@ -182,21 +141,12 @@ class UidBuffer(TM):
uid = self.temporary_buffer[tid].pop() uid = self.temporary_buffer[tid].pop()
except (KeyError, IndexError): except (KeyError, IndexError):
uid = self.finished_buffer.pop() uid = self.finished_buffer.pop()
self.allocated_buffer.setdefault(tid, []).append(uid)
return uid return uid
def extend(self, iterable): def extend(self, iterable):
self._register() self._register()
tid = get_ident() tid = get_ident()
try: self.temporary_buffer.setdefault(tid, []).extend(iterable)
allocated_buffer = self.allocated_buffer[tid]
uid_list = []
for uid in iterable:
if uid not in allocated_buffer:
uid_list.append(uid)
except KeyError:
uid_list = iterable
self.temporary_buffer.setdefault(tid, []).extend(uid_list)
class Catalog(Folder, Persistent, Acquisition.Implicit, ExtensionClass.Base): class Catalog(Folder, Persistent, Acquisition.Implicit, ExtensionClass.Base):
""" An Object Catalog """ An Object Catalog
...@@ -827,15 +777,13 @@ class Catalog(Folder, Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -827,15 +777,13 @@ class Catalog(Folder, Persistent, Acquisition.Implicit, ExtensionClass.Base):
# This checks if the list of local reserved uids was cleared after clearReserved # This checks if the list of local reserved uids was cleared after clearReserved
# had been called. # had been called.
if klass._local_clear_reserved_time != self._last_clear_reserved_time: if klass._local_clear_reserved_time != self._last_clear_reserved_time:
self._v_uid_buffer = UidBuffer(self) self._v_uid_buffer = UidBuffer()
klass._local_clear_reserved_time = self._last_clear_reserved_time klass._local_clear_reserved_time = self._last_clear_reserved_time
elif not hasattr(self, '_v_uid_buffer'): elif not hasattr(self, '_v_uid_buffer'):
self._v_uid_buffer = UidBuffer(self) self._v_uid_buffer = UidBuffer()
if len(self._v_uid_buffer) == 0: if len(self._v_uid_buffer) == 0:
method_id = self.sql_catalog_produce_reserved method_id = self.sql_catalog_produce_reserved
method = getattr(self, method_id) method = getattr(self, method_id)
instance_id = klass._instance_id
if instance_id is None:
# Generate an instance id randomly. Note that there is a small possibility that this # Generate an instance id randomly. Note that there is a small possibility that this
# would conflict with others. # would conflict with others.
random_factor_list = [time.time(), os.getpid(), os.times()] random_factor_list = [time.time(), os.getpid(), os.times()]
...@@ -843,8 +791,7 @@ class Catalog(Folder, Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -843,8 +791,7 @@ class Catalog(Folder, Persistent, Acquisition.Implicit, ExtensionClass.Base):
random_factor_list.append(os.getloadavg()) random_factor_list.append(os.getloadavg())
except (OSError, AttributeError): # AttributeError is required under cygwin except (OSError, AttributeError): # AttributeError is required under cygwin
pass pass
instance_id = md5.new(str(random_factor_list)).hexdigest()[:30] instance_id = md5.new(str(random_factor_list)).hexdigest()
klass._instance_id = instance_id
uid_list = [x.uid for x in method(count = UID_BUFFER_SIZE, instance_id = instance_id) if x.uid != 0] uid_list = [x.uid for x in method(count = UID_BUFFER_SIZE, instance_id = instance_id) if x.uid != 0]
self._v_uid_buffer.extend(uid_list) self._v_uid_buffer.extend(uid_list)
......
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