couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <>
Subject [Couchdb Wiki] Update of "ViewServer" by NoahSlater
Date Sat, 29 Mar 2008 18:49:16 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.

The following page has been changed by NoahSlater:

The comment on the change is:
Copied from original CouchDB wiki

New page:
A simple introduction to the CouchDB view server.

== The View Server ==

CouchDB delegates computation of ["Views"] to external query servers. It communicates with
them over standard input/output, using a very simple, line-based protocol. The default query
server is written in Javascript, running via Mozilla !SpiderMonkey. You can use other languages
by setting a MIME type in the ''language'' property of a design document or the Content-Type
header of a temporary view. Design documents that do not specify a ''language'' property are
assumed to be of type ''text/javascript'', as are ad hoc queries that are ''POST''ed to ''_temp_view''
without a ''Content-Type'' header.

To register query servers with CouchDB, add a line for each server to ''couch.ini''. The basic
syntax is:

[Couch Query Servers]

text/javascript=/usr/local/bin/couchjs -f /usr/local/share/couchdb/server/main.js

== Basic API ==

This shows you how the view server implementation for your language should behave. If in doubt,
look at the ''share/server/main.js'' file in your CouchDB source tree for reference.

CouchDB launches the view server and starts sending commands. The server responds according
to its evaluation of the commands. There are only three commands the view server needs to

=== reset ===

This resets the state of the view server and makes it forget all previous input. If applicable,
this is the point to run garbage collection.

CouchDB sends:


The view server responds:


=== add_fun ===

When creating a view, the view server gets sent the view function for evaluation. The view
server should parse/compile/evaluate the function he receives to make it callable later. If
this fails, the view server returns an error. CouchDB might store several functions before
sending in any actual documents.

CouchDB sends:

["add_fun", "function(doc) { map(null, doc); }"]\n

When the view server can evaluate the function and make it callable, it returns:


If not:

{"error": {"id": "some_error_code", "reason": "error message"}}\n

=== map_doc ===

When the view function is stored in the view server, CouchDB starts sending in all the documents
in the database, one at a time. The view server calls the previously stored functions one
after another with the document and stores its result. When all functions have been called,
the result is returned as a JSON string.

CouchDB sends:

["map_doc", {"_id":"8877AFF9789988EE","_rev":46874684684,"field":"value","otherfield":"othervalue"}]\n

If the function above is the only function stored, the views server answers:

[[[null, {"_id":"8877AFF9789988EE", "_rev":46874684684, "field":"value", "otherfield":"othervalue"}]]]\n

That is, an array with the result for every function for the given document. If a document
is to be excluded from the View, the array should be empty.

== Implementations ==

  * [ JavaScript]
(CouchDB native)
  * [ PHP]
  * [ Ruby]
  * [ Python]

View raw message