Re: [Zope] emulate a sequence
Yuri wrote at 2007-2-8 15:09 +0100: > I need a field in zope object which act as a sequence. I mean every >time I create an object, the value is the previous sequence value of the >last object created + 1. > > also I need to browse the objects by sequence (from the object 1 to the >last, by the sequence number) > > I think I need a catalog FieldIndex to store the sequence for searches, >a property in the object. > > Can I use the catalog to retrieve the max value of the index, so when I >create the object, I just add 1 and store it in the property? You can -- but it is unsafe (concurrent transaction can see the same value and you may use the same id for different objects). A better alternative is to implement the sequence object directly -- as a persistent object with an integer attribute (and a method to fetch the next value). -- Dieter ___ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
Re: [Zope] emulate a sequence
- Original Message - From: "Tino Wildenhain" <[EMAIL PROTECTED]> To: "Jonathan" <[EMAIL PROTECTED]> Cc: "Yuri" <[EMAIL PROTECTED]>; Sent: Thursday, February 08, 2007 9:36 AM Subject: Re: [Zope] emulate a sequence Jonathan schrieb: - Original Message - From: "Yuri" <[EMAIL PROTECTED]> To: Sent: Thursday, February 08, 2007 9:09 AM Subject: [Zope] emulate a sequence I need a field in zope object which act as a sequence. I mean every time I create an object, the value is the previous sequence value of the last object created + 1. also I need to browse the objects by sequence (from the object 1 to the last, by the sequence number) I think I need a catalog FieldIndex to store the sequence for searches, a property in the object. Can I use the catalog to retrieve the max value of the index, so when I create the object, I just add 1 and store it in the property? You could use a catalog if you have other searching requirements, but the method objectIds() (see the zopebook for more info) will return a list of contained Ids, which you can then sort to get you what you need. Also, len(somefolder.objectIds()) will give you the next available id (assuming your object ids have some sequential numbering). Which both would not exacly emulate a sequence. Sequences live outside transaction borders so they are inremented every time under any circumstances. All the above methods would fall flat on the tummy when you are hit with variuos rollback or retries while other transactions concurrently add objects. Tino has a good point. For sites that will be saving (committing) many objects to the same folder (from simultaneous transactions) we use a randomly generated 10 digit id (which is then checked against existing object ids). I have done some load testing with this approach and it has worked (so far!). Jonathan ___ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
Re: [Zope] emulate a sequence
Jonathan schrieb: - Original Message - From: "Yuri" <[EMAIL PROTECTED]> To: Sent: Thursday, February 08, 2007 9:09 AM Subject: [Zope] emulate a sequence I need a field in zope object which act as a sequence. I mean every time I create an object, the value is the previous sequence value of the last object created + 1. also I need to browse the objects by sequence (from the object 1 to the last, by the sequence number) I think I need a catalog FieldIndex to store the sequence for searches, a property in the object. Can I use the catalog to retrieve the max value of the index, so when I create the object, I just add 1 and store it in the property? You could use a catalog if you have other searching requirements, but the method objectIds() (see the zopebook for more info) will return a list of contained Ids, which you can then sort to get you what you need. Also, len(somefolder.objectIds()) will give you the next available id (assuming your object ids have some sequential numbering). Which both would not exacly emulate a sequence. Sequences live outside transaction borders so they are inremented every time under any circumstances. All the above methods would fall flat on the tummy when you are hit with variuos rollback or retries while other transactions concurrently add objects. Regards Tino ___ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
Re: [Zope] emulate a sequence
- Original Message - From: "Yuri" <[EMAIL PROTECTED]> To: Sent: Thursday, February 08, 2007 9:09 AM Subject: [Zope] emulate a sequence I need a field in zope object which act as a sequence. I mean every time I create an object, the value is the previous sequence value of the last object created + 1. also I need to browse the objects by sequence (from the object 1 to the last, by the sequence number) I think I need a catalog FieldIndex to store the sequence for searches, a property in the object. Can I use the catalog to retrieve the max value of the index, so when I create the object, I just add 1 and store it in the property? You could use a catalog if you have other searching requirements, but the method objectIds() (see the zopebook for more info) will return a list of contained Ids, which you can then sort to get you what you need. Also, len(somefolder.objectIds()) will give you the next available id (assuming your object ids have some sequential numbering). hth Jonathan ___ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )