apache/couchdb — pull request #38 by darobin (2012-10-25 16:19:05+00:00)
Title: Rewrite using functions

This pull request implements the ability of using functions for rewriting in addition to the rewriting microlanguage that is currently available. The motivation for doing so comes from a discussion on the users' list in which it turned out that I wasn't the only one who found the microlanguage limited (the latter still works just the same though).

It uses the same "rewrites" ddoc key so that you can't have both. I think that this is a feature: specifying both and having to figure out which takes precedence would be excessive complication IMHO. And it's of course possible to have the function implement the microlanguage if for some reason one wishes to use both simultaneously.

I've provided some docs in the comments — if the patch is accepted and those need to be mirrored (and expanded) somewhere else, simply tell me where is the preferred location. I've also written some tests; I guess that there could be more but it seems clear that it works and doesn't break existing rewrites.

The only change that it makes to the current HTTP interface is that previously if you added a ddoc with a string as the value for "rewrites" you'd get a 400. Now, if that strings is a valid function is just works, and if it's not you get a 500. I doubt that this will break anything.

Some caveats:

• This is essentially my Hello World to Erlang. It's not out of the question that I've written something that looks terrifyingly stupid or woefully more convoluted than it needs to be.

• I've tried to follow the coding conventions that I noticed, apologies if I messed it up here or there.

• The rewrite functions are meant to be cacheable at least for the lifetime of a given ddoc but I haven't added such caching. I don't know how much it would affect performance (though I would expect some). I also don't know whether it should vary on userCtx or not. Either way, I think that it's an optimisation that can be added later.