[pybsddb] Making calls to keys() more efficient (or, how to do inequality gets on keys?)

Francisco Olarte (M) folarte at peoplecall.com
Thu Jun 10 22:23:36 CEST 2010


Hi:

On Wed, Jun 9, 2010 at 9:46 PM, Denis Papathanasiou <
denis.papathanasiou at gmail.com> wrote:

> I know that using keys() can be inefficient, as per the warnings in the
> docs.
>
> I do need to calls keys() in the following scenario, though, so I was
> wondering if there was anything I could do to make it run faster:
>
> A db that stores temporary session data has as its key a guid, prefixed by
> a unix timestamp. The associated value is just another guid identifier
> (which is unrelated to the one in the key).
>
> Since the session data is temporary, I have a function which examines all
> the keys, and removes every key/value pair older than n seconds (it's a
> simple calc using the unix timestamp prefix in the key).
>
> I realize that I could restructure the db so that the unix timestamp is
> part of the value, and by adding a secondary index on it, I could get all
> the keys older than n.
>
> But, restructuring it and doing all the tests will take time, so in the
> interim, is there anything I can do to make using keys() more effective?
>

I don't know how to make keys() faster, but if you have prefixed the
timestamp properly, that means making sure it sorts properly, you don't need
a keys(), just a cursor. The key will sort properly if you have used a
decimal o hex ascii representation ( as presently timestamps are all 9 dec /
8 hex digit numbers ), or if you store it in binary starting with the MSB (
I never remeber wether this is little or big endian ). In this case just
make a key which is only the limit timestamp and start iterating from the
start until you get to a greater value and you're done, this can be done
easily using a cursor.

F.O.S.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.jcea.es/pipermail/pybsddb/attachments/20100610/a214a297/attachment.htm>


More information about the pybsddb mailing list