Commit 7e798ef3 authored by iv's avatar iv

ERP5Workflow: PERF: improve ERP5PersistentMapping by using try/except

So that it will avoid checking a condition ('if self._object_dict is None')
which is very often false.
parent 13257f24
...@@ -6,13 +6,10 @@ from AccessControl import ClassSecurityInfo ...@@ -6,13 +6,10 @@ from AccessControl import ClassSecurityInfo
from AccessControl import getSecurityManager from AccessControl import getSecurityManager
from App.special_dtml import DTMLFile from App.special_dtml import DTMLFile
from zope.event import notify
from OFS.event import ObjectWillBeAddedEvent from OFS.event import ObjectWillBeAddedEvent
from OFS.subscribers import compatibilityCall
from OFS.ObjectManager import BadRequestException from OFS.ObjectManager import BadRequestException
from zope.lifecycleevent import ObjectAddedEvent from zope.lifecycleevent import ObjectAddedEvent
from zope.lifecycleevent import ObjectRemovedEvent from zope.lifecycleevent import ObjectRemovedEvent
from zope.container.contained import notifyContainerModified
from OFS.Folder import Folder from OFS.Folder import Folder
from Products.CMFCore.PortalFolder import PortalFolderBase from Products.CMFCore.PortalFolder import PortalFolderBase
from Products.ERP5Type.Base import Base from Products.ERP5Type.Base import Base
...@@ -36,12 +33,14 @@ class ERP5PersistentMappingFolder(PortalFolderBase): ...@@ -36,12 +33,14 @@ class ERP5PersistentMappingFolder(PortalFolderBase):
self._object_dict = PersistentMapping() self._object_dict = PersistentMapping()
def __getattr__(self, name): def __getattr__(self, name):
if self._object_dict is None:
raise AttributeError(name)
try: try:
return self._object_dict[name].__of__(self) return self._object_dict[name].__of__(self)
except KeyError: except KeyError:
raise AttributeError(name) raise AttributeError(name)
except TypeError:
if self._object_dict is None:
raise AttributeError(name)
raise
def _initPersistentObjects(self): def _initPersistentObjects(self):
self._cleanup() self._cleanup()
...@@ -55,14 +54,22 @@ class ERP5PersistentMappingFolder(PortalFolderBase): ...@@ -55,14 +54,22 @@ class ERP5PersistentMappingFolder(PortalFolderBase):
return self._getOb(name) return self._getOb(name)
def _setOb(self, object_id, object_value): def _setOb(self, object_id, object_value):
if self._object_dict is None: try:
self._object_dict = PersistentMapping() self._object_dict[object_id] = object_value
self._object_dict[object_id] = object_value except TypeError:
if self._object_dict is None:
self._object_dict = PersistentMapping()
self._object_dict[object_id] = object_value
else:
raise
def _delOb(self, object_id): def _delOb(self, object_id):
if self._object_dict is None: try:
raise KeyError, object_id del self._object_dict[object_id]
del self._object_dict[object_id] except TypeError:
if self._object_dict is None:
raise KeyError, object_id
raise
def _getOb(self, object_id, default=_marker): def _getOb(self, object_id, default=_marker):
""" """
...@@ -70,19 +77,20 @@ class ERP5PersistentMappingFolder(PortalFolderBase): ...@@ -70,19 +77,20 @@ class ERP5PersistentMappingFolder(PortalFolderBase):
""" """
try: try:
return self._object_dict[object_id].__of__(self) return self._object_dict[object_id].__of__(self)
except KeyError: except KeyError, TypeError: # TypeError if _object_dict is None
if default is _marker: if default is _marker:
raise raise AttributeError(object_id)
else: else:
return default return default
def objectIds(self, spec=None, **kw): def objectIds(self, spec=None, **kw):
# we don't plan to use meta_type now, so spec is just here for compatibility # we don't plan to use meta_type now, so spec is just here for compatibility
assert(spec is None) try:
if self._object_dict is None: return self._object_dict.keys()
return [] except AttributeError:
return self._object_dict.keys() if self._object_dict is None:
return []
raise
def objectCount(self): def objectCount(self):
return len(self._object_dict) return len(self._object_dict)
...@@ -108,10 +116,14 @@ class ERP5PersistentMappingFolder(PortalFolderBase): ...@@ -108,10 +116,14 @@ class ERP5PersistentMappingFolder(PortalFolderBase):
# hack to get the portal type for a specified meta_type # hack to get the portal type for a specified meta_type
portal_type = [meta_type[5:] for meta_type in spec portal_type = [meta_type[5:] for meta_type in spec
if meta_type.startswith('ERP5 ')] if meta_type.startswith('ERP5 ')]
if self._object_dict is None:
return []
object_list = self._object_dict.values() try:
object_list = self._object_dict.values()
except AttributeError:
if self._object_dict is None:
return []
raise
if portal_type is not None: if portal_type is not None:
if isinstance(portal_type, str): if isinstance(portal_type, str):
portal_type = (portal_type,) portal_type = (portal_type,)
...@@ -158,6 +170,7 @@ class ERP5PersistentMappingFolder(PortalFolderBase): ...@@ -158,6 +170,7 @@ class ERP5PersistentMappingFolder(PortalFolderBase):
if userid is not None: if userid is not None:
ob.manage_setLocalRoles(userid, ['Owner']) ob.manage_setLocalRoles(userid, ['Owner'])
ob.manage_afterAdd(ob, self)
return id return id
def _cleanup(self): def _cleanup(self):
...@@ -174,13 +187,12 @@ class ERP5PersistentMappingFolder(PortalFolderBase): ...@@ -174,13 +187,12 @@ class ERP5PersistentMappingFolder(PortalFolderBase):
return cleaned return cleaned
def has_key(self, key): def has_key(self, key):
if (key in ('.', '..') or try:
key.startswith('_') or return self._object_dict.has_key(key)
key.startswith('aq_') or except AttributeError:
key.endswith('__') or if self._object_dict is None:
self._object_dict is None): return False
return False raise
return self._object_dict.get(key, None) is not None
hasObject = has_key hasObject = has_key
......
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