4.10. Query Servers

4.10.1. Query Servers Definition

[queryservers]


Changed in version 2.3: Changed configuration method for Query Serversand Native Query Servers.


CouchDB delegates computation of design documents functionsto external query servers. The external query server is a special OSprocess which communicates with CouchDB over standard input/output using avery simple line-based protocol with JSON messages.

An external query server may be defined with environment variables followingthis pattern:




  1. COUCHDB_QUERY_SERVER_LANGUAGE="PATH ARGS"




Where:

-
LANGUAGE: is a programming language which code this query server mayexecute. For instance, there are _PYTHON
, RUBY, CLOJURE and otherquery servers in the wild. This value in lowercase is also used for ddoc_field language to determine which query server processes the functions.

Note, that you may set up multiple query servers for the same programminglanguage, but you have to name them differently (like _PYTHONDEV
etc.).

-
PATH: is a system path to the executable binary program that runs thequery server.

-
ARGS: optionally, you may specify additional command line argumentsfor the executable PATH.

The default query server is written in JavaScript,running via Mozilla SpiderMonkey. It requires no special environmentsettings to enable, but is the equivalent of these two variables:




  1. COUCHDBQUERY_SERVER_JAVASCRIPT="/opt/couchdb/bin/couchjs /opt/couchdb/share/server/main.js"
    COUCHDB_QUERY_SERVER_COFFEESCRIPT="/opt/couchdb/bin/couchjs /opt/couchdb/share/server/main-coffee.js"




By default, couchjs limits the max runtime allocation to 64MiB.If you run into out of memory issue in your ddoc functions,you can adjust the memory limitation (here, increasing to 512 MiB):




  1. COUCHDB_QUERY_SERVER_JAVASCRIPT="/usr/bin/couchjs -S 536870912 /usr/share/server/main.js"




For more info about the available options, please consult couchjs -h.


See also

The Mango Query Server is a declarative languagethat requires _no programming
, allowing for easier indexing and findingof data in documents.

The Native Erlang Query Serverallows running ddocs written in Erlang natively, bypassingstdio communication and JSON serialization/deserialization round tripoverhead.

4.10.2. Query Servers Configuration

[queryserver_config]
commit_freq

Specifies the delay in seconds before view index changes are committedto disk. The default value is 5:




  1. [query_server_config]
    commit_freq = 5



os_process_limit
limit

Hard limit on the number of OS processes usable by QueryServers. The default value is 100:




  1. [query_server_config]
    os_process_limit = 100




Setting _os_process_limit
too low can result in starvation ofQuery Servers, and manifest in os_process_timeout errors,while setting it too high can potentially use too many systemresources. Production settings are typically 10-20 times thedefault value.
osprocess_soft_limit
soft limit

Soft limit on the number of OS processes usable by QueryServers. The default value is 100:




  1. [query_server_config]
    os_process_soft_limit = 100




Idle OS processes are closed until the total reaches the softlimit.

For example, if the hard limit is 200 and the soft limit is100, the total number of OS processes will never exceed 200,and CouchDB will close all idle OS processes until it reaches100, at which point it will leave the rest intact, even ifsome are idle.
reduce_limit

Controls _Reduce overflow
error that raises when output ofreduce functions is too big:




  1. [queryserver_config]
    reduce_limit = true




Normally, you don’t have to disable (by setting false value) thisoption since main propose of _reduce
functions is to reduce theinput.

4.10.3. Native Erlang Query Server

[nativequery_servers]


Warning

Due to security restrictions, the Erlang query server is disabled bydefault.

Unlike the JavaScript query server, the Erlang one does not runs in asandbox mode. This means that Erlang code has full access to your OS,file system and network, which may lead to security issues. While Erlangfunctions are faster than JavaScript ones, you need to be carefulabout running them, especially if they were written by someone else.


CouchDB has a native Erlang query server, allowing you to write yourmap/reduce functions in Erlang.

First, you’ll need to edit your _local.ini
to include a[nativequery_servers] section:




  1. [native_query_servers]
    enable_erlang_query_server = true




To see these changes you will also need to restart the server.

Let’s try an example of map/reduce functions which count the totaldocuments at each number of revisions (there are x many documents atversion “1”, and y documents at “2”… etc). Add a few documents to thedatabase, then enter the following functions as a view:




  1. %% Map Function
    fun({Doc}) ->
    <<K,
    /binary>> = proplists:get_value(<<"_rev">>, Doc, null),
    V = proplists:get_value(<<"_id">>, Doc, null),
    Emit(<<K>>, V)
    end.

    %% Reduce Function
    fun(Keys, Values, ReReduce) -> length(Values) end.




If all has gone well, after running the view you should see a list of thetotal number of documents at each revision number.

原文: http://docs.couchdb.org/en/stable/config/query-servers.html