[Erp5-report] r13736 - /erp5/trunk/products/ERP5/Tool/IdTool.py
nobody at svn.erp5.org
nobody at svn.erp5.org
Wed Mar 28 10:41:41 CEST 2007
Author: vincent
Date: Wed Mar 28 10:41:40 2007
New Revision: 13736
URL: http://svn.erp5.org?rev=13736&view=rev
Log:
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).
Modified:
erp5/trunk/products/ERP5/Tool/IdTool.py
Modified: erp5/trunk/products/ERP5/Tool/IdTool.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5/Tool/IdTool.py?rev=13736&r1=13735&r2=13736&view=diff
==============================================================================
--- erp5/trunk/products/ERP5/Tool/IdTool.py (original)
+++ erp5/trunk/products/ERP5/Tool/IdTool.py Wed Mar 28 10:41:40 2007
@@ -168,37 +168,55 @@
return self.dict_length_ids.items()
security.declareProtected(Permissions.AccessContentsInformation,
- 'generateNewLengthId')
- def generateNewLengthId(self, id_group=None, default=None):
- """
- Generates an Id.
- The id is generated by mysql and then stored in a Length object in a
+ 'generateNewLengthIdList')
+ def generateNewLengthIdList(self, id_group=None, id_count=1, default=1):
+ """
+ Generates a list of Ids.
+ The ids are generated using mysql and then stored in a Length object in a
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
some IDs might be skipped because of failed transactions.
"Length" is because the id is stored in a python object inspired by
BTrees.Length. It doesn't have to be a length.
"""
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()
new_id = None
- if id_group not in (None, 'None'):
- if not isinstance(id_group, str):
- id_group = repr(id_group)
- default = isinstance(default, int) and default or 1
- portal_catalog = getToolByName(self, 'portal_catalog').getSQLCatalog()
- query = getattr(portal_catalog, 'z_portal_ids_generate_id')
- commit = getattr(portal_catalog, 'z_portal_ids_commit')
- if None not in (query, commit):
- result = query(id_group=id_group, default=default)
- commit()
- new_id = result[0]['LAST_INSERT_ID()']
- 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].set(new_id)
- return new_id
-
+ if id_group in (None, 'None'):
+ raise ValueError, '%s is not a valid group Id.' % (repr(id_group), )
+ if not isinstance(id_group, str):
+ id_group = repr(id_group)
+ 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()
+ query = getattr(portal_catalog, 'z_portal_ids_generate_id')
+ commit = getattr(portal_catalog, 'z_portal_ids_commit')
+ if None in (query, commit):
+ 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()
+ new_id = result[0]['LAST_INSERT_ID()']
+ 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].set(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)
More information about the Erp5-report
mailing list