[ZODB-Dev] BTree ranges

Christian Robottom Reis kiko at async.com.br
Sat Dec 13 07:25:35 EST 2003


On Fri, Dec 12, 2003 at 04:06:09PM -0500, Tim Peters wrote:
> [Christian Robottom Reis]
> > This is dead and buried, but note that DateTime objects used as keys
> > are also converted to floats, and a BTree that allows you to grab all
> > values between a certain range of dates isn't such an unexpected use
> > case, I hope.
> 
> Yup, I was wrong.  It doesn't have any consequence, as floats work fine as
> BTree keys, and there was never a plan to drop that.

Note that for posterity (and perhaps John's benefit, since he was using
a very evil "delta" hack), one way to get "open" ranges is to use
(surprise, surprise) to use the difference operator to subtract from
your BTree a Set containing the edge(s) you want open:

        >>> x = OIBTree.OIBTree({1.1:1, 1.2:2, 1.9:3, 2.1:4})

        >>> print list(x.items())
        [(1.1000000000000001, 1), (1.2, 2), (1.8999999999999999, 3),
        (2.1000000000000001, 4)]

        >>> OIBTree.difference(x, OIBTree.OISet([1.1]))
        OIBucket([(1.2, 2), (1.8999999999999999, 3), 
                  (2.1000000000000001, 4)])

I do this routinely to do "foo < 5" and "foo > 10" queries, and more
recently, to implement optimized "5 < foo < 10" queries.

The BTree operators are magical things -- by learning how to use them,
I've been able to work wonders in IndexedCatalog's indexes. 

(Thanks to Sidnei for providing me with the incentive to try and
understand them, btw -- sometimes reading Interfaces.py just doesn't
occur to you naturally.)

Take care,
--
Christian Robottom Reis | http://async.com.br/~kiko/ | [+55 16] 261 2331



More information about the ZODB-Dev mailing list