Search code examples
javascripterlangcouchdbtrimseamonkey

CouchDB: list of Erlang functions


As I know, CouchDB allows to use some integrated Erlang functions like a sort (and something else).

Doing text trimming in JS again, I thought: what if Couch already built this feature in? Maybe trim also integrated? And how much functions are ready to use?

If they are already done in Erlang, why I must use slow JS-versions?

So, that's my question: where I can find full list of available from JS functions for Couch?


Conclusion: it only a few of functions available and there's no trim. You can test your luck by writing your own functions at couch_query_server.erl and then rebuilding Couch from source.


Solution

  • CouchDB has three built-in reduce functions. These are implemented in Erlang and run right inside CouchDB, so they are much faster than the equivalent JavaScript functions.

    They are _count, _sum and _stats. You can find more details and examples here. They are implemented in couch_query_server.erl file.

    Also you can use built-in Erlang functions and features with writing Native Erlang Query Server. But notice that it is disabled by default.

    An example from CouchDB documentation for implementing a native Erlang query server:

    %% 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) -> erlang:length(Values) end.
    

    It uses proplists:get_value/3 and erlang:length/1 MFAs (Module Function Arity) which are in Erlang standard library.

    Edit: This thread can be a possible duplicate which seems to be outdated.