[ZODB-Dev] BTrees and PersistentDict keys: bug or feature

Casey Duncan casey at zope.com
Fri Aug 15 10:25:39 EDT 2003


On Friday 15 August 2003 12:04 am, Christian Reis wrote:
> On Thu, Aug 14, 2003 at 11:52:55PM -0400, Casey Duncan wrote:
> > > To explain a tiny bit further, I'm using an OOBTree that maps
> > > dictionaries to objects. This is part of the indexing mechanism in
> > > IndexedCatalog, which is a simple indexing and query mechanism for the
> > > ZODB. This allows us to do queries like:
> > > 
> > >     "5 in mydict"
> > 
> > I'm not sure how using dictionaries as keys helps here (it just sounds 
evil to 
> > me ;^), but this is one place where the Python hashing rules for 
dictionary 
> 
> I'm suffering from lack of oxygen (hopefully). The important query here
> is, or course:
> 
>     "mydict == {foo: bar}"
> 
> -- which is handled correctly (AFAICT, and I can't tell much yet because
> I still need to test a lot) by simple comparison.

Here are some thoughts, both involving reducing the complexity by converting 
the mutable objects to an immutable representation:

A list and a dict can both be represented in immutable form. The former as a 
tuple and the later as a tuple of key/value pair tuples.

Have you considered converting the values to an immutable form and using that 
as a key? This might have another advantage (or maybe disadvantage), which is 
that all list-like things and all dict-like things would get a LCD 
representation which would compare consistently.

So PersistentMapping({1:2, 2:3}) would become ((1,2),(2,3)) as would {1:2, 
2:3}. Determining what is a mapping could be done with some introspection 
logic (like looking for an "items" method) or by some explicit configuration. 
Now, this would still fall down if the key/values themselves were mutable.

So, in that case I might suggest instead pickling the object and using that as 
a key. Then all the keys would be strings. This would never consider 
PersistentMappings equal to dicts though, but maybe that's what you want. I 
used this technique recently when I needed to use arbitrary types/classes as 
BTree keys.

-Casey



More information about the ZODB-Dev mailing list