Commit a3192f15 authored by Vincent Pelletier's avatar Vincent Pelletier

generateNewLengthIdList: New method to generate multiple consecutive Ids at once.

Modify generateNewLengthId to call generateNewLengthIdList.
generateNewLengthIdList contains code which belonged to generateNewLengthId with more comments and safe fallbacks in case of problem (which means that this function now raises when its requirements are not met).


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@13736 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent e8a1d995
...@@ -168,37 +168,55 @@ class IdTool(UniqueObject, Folder): ...@@ -168,37 +168,55 @@ class IdTool(UniqueObject, Folder):
return self.dict_length_ids.items() return self.dict_length_ids.items()
security.declareProtected(Permissions.AccessContentsInformation, security.declareProtected(Permissions.AccessContentsInformation,
'generateNewLengthId') 'generateNewLengthIdList')
def generateNewLengthId(self, id_group=None, default=None): def generateNewLengthIdList(self, id_group=None, id_count=1, default=1):
""" """
Generates an Id. Generates a list of Ids.
The id is generated by mysql and then stored in a Length object in a The ids are generated using mysql and then stored in a Length object in a
persistant mapping to be persistent. persistant mapping to be persistent.
We use MySQL to generate an ID, because it is atomic and we don't want We use MySQL to generate IDs, because it is atomic and we don't want
to generate any conflict at zope level. The possible downfall is that to generate any conflict at zope level. The possible downfall is that
some IDs might be skipped because of failed transactions. some IDs might be skipped because of failed transactions.
"Length" is because the id is stored in a python object inspired by "Length" is because the id is stored in a python object inspired by
BTrees.Length. It doesn't have to be a length. BTrees.Length. It doesn't have to be a length.
""" """
if getattr(self, 'dict_length_ids', None) is None: if getattr(self, 'dict_length_ids', None) is None:
# Length objects are stored in a persistent mapping: there is one
# Length object per id_group.
self.dict_length_ids = PersistentMapping() self.dict_length_ids = PersistentMapping()
new_id = None new_id = None
if id_group not in (None, 'None'): if id_group in (None, 'None'):
raise ValueError, '%s is not a valid group Id.' % (repr(id_group), )
if not isinstance(id_group, str): if not isinstance(id_group, str):
id_group = repr(id_group) id_group = repr(id_group)
default = isinstance(default, int) and default or 1 if not isinstance(default, int):
default = 1
# FIXME: A skin folder should be used to contain ZSQLMethods instead of
# default catalog, like activity tool (anyway, it uses activity tool
# ZSQLConnection, so hot reindexing is not helping here).
portal_catalog = getToolByName(self, 'portal_catalog').getSQLCatalog() portal_catalog = getToolByName(self, 'portal_catalog').getSQLCatalog()
query = getattr(portal_catalog, 'z_portal_ids_generate_id') query = getattr(portal_catalog, 'z_portal_ids_generate_id')
commit = getattr(portal_catalog, 'z_portal_ids_commit') commit = getattr(portal_catalog, 'z_portal_ids_commit')
if None not in (query, commit): if None in (query, commit):
result = query(id_group=id_group, default=default) raise AttributeError, 'Error while generating Id: ' \
'z_portal_ids_generate_id and/or z_portal_ids_commit could not ' \
'be found.'
result = query(id_group=id_group, id_count=id_count, default=default)
commit() commit()
new_id = result[0]['LAST_INSERT_ID()'] new_id = result[0]['LAST_INSERT_ID()']
if self.dict_length_ids.get(id_group) is None: if self.dict_length_ids.get(id_group) is None:
self.dict_length_ids[id_group]=Length(new_id) self.dict_length_ids[id_group] = Length(new_id)
self.dict_length_ids[id_group].set(new_id) self.dict_length_ids[id_group].set(new_id)
return new_id return range(new_id - id_count, new_id)
security.declareProtected(Permissions.AccessContentsInformation,
'generateNewLengthId')
def generateNewLengthId(self, id_group=None, default=None):
"""
Generates an Id.
See generateNewLengthIdList documentation for details.
"""
return self.generateNewLengthIdList(id_group=id_group, id_count=1, default=default)[0]
InitializeClass(IdTool) InitializeClass(IdTool)
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