[ZODB-Dev] weird BTrees KeyError (CVS HEAD)

Thomas Guettler guettli at thomas-guettler.de
Mon Jun 30 00:21:20 EDT 2003


On Fri, Jun 27, 2003 at 03:47:24PM -0300, Christian Reis wrote:
> On Fri, Jun 27, 2003 at 02:22:40PM -0400, Jeremy Hylton wrote:
> > On Fri, 2003-06-27 at 14:13, Christian Reis wrote:
> > > I have a BTree (held by ListIndex for the IC-interested) that uses
> > > PersistentLists as keys. I'm running into the following situation:
> > 
> > Unless you are very careful, it is not safe to use a PersistnetList as a
> > BTree key.  If the PersistentList is ever mutated, it may not have the
> > same place in the total order of the keys.  Are you sure that these
> > lists are never mutated and that their contents are totally ordered?
> 
> Would a normal list be safe, even while mutating? 

I think it is not safe, but you can use tuples. BTrees are like
dictionaries:

===> python
Python 2.2.1 (#1, Sep  7 2002, 14:34:30) 
[GCC 2.95.4 20011002 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> d={}
>>> d[[]]=1
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: list objects are unhashable
>>> d[()]=1

But keep in mind, that btrees use obj.__cmp__() to store the data, not
__hash__().

The ZODB Guide has a good chapter about btrees.

Be very carefull if you use objects as keys. Objects fall back to use
something like: cmp(self.id) if you don't define a __cmp__ method. The
id is the address in memory of the objects. This works as long as you
don't write the BTree to disc. If you write it to disc an read the
objects again the address changes, and the BTree is "insane" because
the order of the elements is wrong.

 thomas

-- 
Thomas Guettler <guettli at thomas-guettler.de>
http://www.thomas-guettler.de




More information about the ZODB-Dev mailing list