Back to index

plone3  3.1.7
UniqueIdGeneratorTool.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2004 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """Uid Generator.
00014 
00015 Provides support for generating unique ids.
00016 
00017 $Id: UniqueIdGeneratorTool.py 77186 2007-06-28 19:06:19Z yuppie $
00018 """
00019 
00020 from AccessControl import ClassSecurityInfo
00021 from BTrees.Length import Length
00022 from Globals import InitializeClass
00023 from OFS.SimpleItem import SimpleItem
00024 from zope.interface import implements
00025 
00026 from Products.CMFCore.utils import registerToolInterface
00027 from Products.CMFCore.utils import UniqueObject
00028 
00029 from Products.CMFUid.interfaces import IUniqueIdGenerator
00030 
00031 
00032 class UniqueIdGeneratorTool(UniqueObject, SimpleItem):
00033 
00034     """Generator of unique ids.
00035 
00036     This is a dead simple implementation using a counter. May cause
00037     ConflictErrors under high load and the values are predictable.
00038     """
00039 
00040     implements(IUniqueIdGenerator)
00041     __implements__ = (
00042         SimpleItem.__implements__,
00043     )
00044 
00045     id = 'portal_uidgenerator'
00046     alternative_id = 'portal_standard_uidgenerator'
00047     meta_type = 'Unique Id Generator Tool'
00048 
00049     security = ClassSecurityInfo()
00050 
00051     security.declarePrivate('__init__')
00052     def __init__(self):
00053         """Initialize the generator
00054         """
00055         # The previous ``BTrees.Length.Length`` implementation may cause 
00056         # double unique ids under high load. So for the moment we just use 
00057         # a simple counter.
00058         self._uid_counter = 0
00059 
00060     security.declarePrivate('__call__')
00061     def __call__(self):
00062         """See IUniqueIdGenerator.
00063         """
00064         # For sites that have already used CMF 1.5.1 (and older) the
00065         # BTrees.Length.Length object has to be migrated to an integer.
00066         if isinstance(self._uid_counter, Length):
00067             self._uid_counter = self._uid_counter()
00068         self._uid_counter += 1
00069         return self._uid_counter
00070 
00071     security.declarePrivate('convert')
00072     def convert(self, uid):
00073         """See IUniqueIdGenerator.
00074         """
00075         return int(uid)
00076 
00077 InitializeClass(UniqueIdGeneratorTool)
00078 registerToolInterface('portal_uidgenerator', IUniqueIdGenerator)