You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Demetrius Nunes <de...@gmail.com> on 2008/08/01 19:17:30 UTC

API within view code

Hi there,

What's the API available within the scope of the view code? Do I have
anything else besides the pure JavaScript API? I noticed there is a
"main.js", should I add any utility functions I wish there?

Also, can I share some state between "map" calls or pass external parameters
to it besides the default "doc" parameter?

Cheers,
Dema

-- 
____________________________
http://www.demetriusnunes.com

Re: API within view code

Posted by Dean Landolt <de...@deanlandolt.com>.
> What's the API available within the scope of the view code? Do I have
> anything else besides the pure JavaScript API?


If you're asking is there any way you can write views, there is -- I've
played around with python views in Chris Lenz's beautiful couchdb-python
library. I've read the python views are quite a bit slower, but I've been
working with tiny datasets so far so I wouldn't know.

If you're asking whether you can talk to couch or tinker with the internals
in the view code, I don't know for sure, but I doubt it. In order to get the
benefit of instant reads once the view indexes are built, the only data that
can really matter to the view is data from documents themselves -- otherwise
couch wouldn't know how to incrementally update the views...

But I could be wrong (and kinda hope I am)...


> I noticed there is a
> "main.js", should I add any utility functions I wish there?


I think that's the idea.

Also, can I share some state between "map" calls or pass external parameters
> to it besides the default "doc" parameter?


While I also find myself yearning for a way to get at a view from another
view, you might want to watch your language -- I think "*share some state*"
is a curse word to the functional programmers among us ;)

Re: API within view code

Posted by Jan Lehnardt <ja...@apache.org>.
On Aug 1, 2008, at 19:17, Demetrius Nunes wrote:

> Hi there,
>
> What's the API available within the scope of the view code? Do I have
> anything else besides the pure JavaScript API? I noticed there is a
> "main.js", should I add any utility functions I wish there?

Yes.


> Also, can I share some state between "map" calls or pass external  
> parameters
> to it besides the default "doc" parameter?

map and reduce functions are meant to run in their own isolated  
environment.
if you need external resources you are possibly doing it wrong[tm].  
While it is
not implemented this way at the moment, map and reduce are meant to  
run on
a distributed set of machines. You'd have to maintain your state on  
all the machines
and synchronise it. This is something you don't really want to do.

Cheers
Jan
--

Re: API within view code

Posted by Chris Anderson <jc...@grabb.it>.
On Fri, Aug 1, 2008 at 10:17 AM, Demetrius Nunes
<de...@gmail.com> wrote:
> Also, can I share some state between "map" calls or pass external parameters
> to it besides the default "doc" parameter?

You have to be very careful with things like this - shared state is a
definite no-no, as you must ensure the map functions output the same
thing regardless of the order in which documents are processed.

I include utility functions in a separate file automatically when I
push views to CouchDB. My design documents turn out longer in Futon
than in my editor, with lots of code duplicated across the views, but
it doesn't really impact performance, as the functions are compiled
only once per document batch.




-- 
Chris Anderson
http://jchris.mfdz.com