Hi Kent,

Kent Sin wrote:
> I want to develope a unique sequence id generator product, I have the
> following code
>     def newid(self, increment=1):
>         """ return new id """
>         self.counter = self.counter+increment
>         return self.counter
> I am not quite sure if this works when more than one process asking for a
> newid concurrently. 

Do you need unique sequential ids, or will just unique ids do?

The problem with sequential ids is that you need to store the next id in
the sequence somewhere, and access to and incrementing this value
becomes a performance bottleneck for your server.

Also, if you store the sequence value in the ZODB, you will be creating
an undo record each time the id is incremented, and this is an
additional performance and storage overhead.

If you just want ids that are unique to a folder, try the following
algorithm (taken originally from the Discussion object code in the PTK).
The variable "self" is the one passed to the constructor method of a
particular Python class, and represents the folder you want to put the
new object into. I'm assuming the object is a "FooBar Item". The
"foobar-%06d" bit generates a unique id for the object that looks like
"foobar-290172837", based on the current time.

        id = int(DateTime().timeTime())
        while hasattr(self, str(id)):
            id = id +1
        id = 'foobar-%09d' % id    

The advantage of this approach is that there is very little contention
in most cases.

> Is that zodb automatically serial all transaction?

For the purposes of generating ids, yes.

Steve Alexander
Software Engineer
Cat-Box limited

Zope maillist  -  [EMAIL PROTECTED]
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://lists.zope.org/mailman/listinfo/zope-dev )

Reply via email to