durusmail: durus-users: Using Persistent objects as keys in a BTree
Using Persistent objects as keys in a BTree
2007-05-13
2007-05-13
2007-05-13
Using Persistent objects as keys in a BTree
David Binger
2007-05-13
On May 13, 2007, at 12:53 PM, David K. Hess wrote:

> Is it possible to use a Persistent object as a key in a BTree? I'm
> having difficulties and suspect the problem has to do with Python's
> default object comparison behavior since Persistent doesn't appear
> to implement __cmp__. Since Python uses a (dynamic) arbitrary
> ordering of in-memory objects for default cmp() behavior it seems
> to me that BTree will not work right. I.e. it needs a persistent
> ordering of the objects.

You are correct that Persistent objects won't work correctly in a
BTree unless you
do something yourself to make sure that the ordering never changes
from one
process to another.

>
> If that is the root of the problem, any thoughts on how best to
> make this work? Using OID for comparison is the obvious first
> choice except there isn't one until after the first commit of the
> object. Should I add a serial number to my Persistent subclass and
> implement __cmp__ as a comparison of it?

I don't think applications should depend on oid values for anything.
I think it is important to preserve the potential to
re-assign OIDs without mangling your application data.
A serial number, name, or some other application-level identifier
would probably work.

As keys in a BTree, Persistent instances
have a disadvantage over non-persistent values in that the
persistent state of the keys will need to be retrieved from the
database, if it is not already loaded, for every key that is compared
to a key that you are trying to find or insert.   If there
is a reasonable alternative for keys, be sure to
consider it.



reply