Saturday, March 26, 2011

Cleaner pseudo-randomized views in CouchDB

Lately I've been running into several explanations on how to efficiently get random documents out a Couch database, and so far, the majority of the approaches settle on storing a random value in the document itself, as demonstrated here. This seems like a waste (it adds administrative clutter to the document).

Of course, it's an understandable approach, since couch view functions are required to be idempotent (running the same function with the same input must produce the same output), and at face value, you'd think that generating a random number in the view function would produce different results each time, even with the same input. However, that's not necessarily the case.

Using closures to optimize speed of couchdb-python view functions

Python's standard library (and 3rd party modules) brings a lot of pluggable functionality that you can use in Couch view (map/reduce) and list/show functions.

Here, we'll using the couchdb-python library, which is, to my knowledge, the only well known couchdb library for Python that implements a view server (couchdbkit does not, which makes it fall out of consideration for the things I commonly need to do).