3.3. Erlang

    The Erlang query server is disabled by default. Read configuration guide about reasons why and how to enable it.

    (Id, Value)

    Emits key-value pairs to view indexer process.

    FoldRows(Fun, Acc)

    Helper to iterate over all rows in a list function.

      • Fun – Function object.

      • Acc – The value previously returned by Fun.

    1. fun(Head, {Req}) ->
    2. Fun = fun({Row}, Acc) ->
    3. Id = couch_util:get_value(<<"id">>, Row),
    4. Send(list_to_binary(io_lib:format("Previous doc id: ~p~n", [Acc]))),
    5. Send(list_to_binary(io_lib:format("Current doc id: ~p~n", [Id]))),
    6. {ok, Id}
    7. end,
    8. FoldRows(Fun, nil),
    9. end.

    ()

    Retrieves the next row from a related view result.

    Log(Msg)

      • Msg – Log a message at the INFO level.
    1. fun({Doc}) ->
    2. <<K,_/binary>> = proplists:get_value(<<"_rev">>, Doc, null),
    3. V = proplists:get_value(<<"_id">>, Doc, null),
    4. Log(lists:flatten(io_lib:format("Hello from ~s doc!", [V]))),
    5. Emit(<<K>>, V)
    6. end.

    After the map function has run, the following line can be found in CouchDB logs (e.g. at /var/log/couchdb/couch.log):

    Send(Chunk)

    Sends a single string Chunk in response.

    1. fun(Head, {Req}) ->
    2. Send("Hello,"),
    3. "!"
    4. end.

    The function above produces the following response:

    Start(Headers)

      • Headers – Proplist of .

    Initialize List Functions response. At this point, response code and headers may be defined. For example, this function redirects to the CouchDB web site:

    1. fun(Head, {Req}) ->
    2. Start({[{<<"code">>, 302},
    3. {<<"headers">>, {[
    4. {<<"Location">>, <<"http://couchdb.apache.org">>}]
    5. }}
    6. ]}),
    7. end.