You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by da...@apache.org on 2019/09/23 17:26:28 UTC

[couchdb] branch prototype/fdb-layer-couch-eval updated (8d01cdf -> b36d1ab)

This is an automated email from the ASF dual-hosted git repository.

davisp pushed a change to branch prototype/fdb-layer-couch-eval
in repository https://gitbox.apache.org/repos/asf/couchdb.git.


 discard 8d01cdf  Update couch_views to use couch_eval
 discard f672644  Add tests for couch_js application
 discard 3e0691f  Implement couch_js callbacks for couch_eval
     new 0b1e047  Implement couch_js callbacks for couch_eval
     new 1b6ef9a  Add tests for couch_js application
     new b36d1ab  Update couch_views to use couch_eval

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (8d01cdf)
            \
             N -- N -- N   refs/heads/prototype/fdb-layer-couch-eval (b36d1ab)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 rel/overlay/etc/default.ini | 3 +++
 1 file changed, 3 insertions(+)


[couchdb] 02/03: Add tests for couch_js application

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davisp pushed a commit to branch prototype/fdb-layer-couch-eval
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 1b6ef9aef1854121b98edf0a94dcdbc688d43109
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Tue Aug 20 14:21:00 2019 -0500

    Add tests for couch_js application
    
    These are ported over from the existing couch Eunit suite and updated to
    be less racey hopefully.
---
 src/couch_js/src/couch_js.app.src                  |   2 +-
 src/couch_js/test/couch_js_proc_manager_tests.erl  | 373 +++++++++++++++++++++
 src/couch_js/test/couch_js_query_servers_tests.erl |  96 ++++++
 3 files changed, 470 insertions(+), 1 deletion(-)

diff --git a/src/couch_js/src/couch_js.app.src b/src/couch_js/src/couch_js.app.src
index 0db37b6..44efd6d 100644
--- a/src/couch_js/src/couch_js.app.src
+++ b/src/couch_js/src/couch_js.app.src
@@ -22,6 +22,6 @@
         stdlib,
         config,
         couch_log,
-        couch
+        ioq
     ]}
  ]}.
diff --git a/src/couch_js/test/couch_js_proc_manager_tests.erl b/src/couch_js/test/couch_js_proc_manager_tests.erl
new file mode 100644
index 0000000..f138dd6
--- /dev/null
+++ b/src/couch_js/test/couch_js_proc_manager_tests.erl
@@ -0,0 +1,373 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(couch_js_proc_manager_tests).
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+
+
+-define(TDEF(A), {atom_to_list(A), fun A/0}).
+
+-define(NUM_PROCS, 3).
+-define(TIMEOUT, 1000).
+
+-define(TIMEOUT_ERROR(Msg), erlang:error({assertion_failed, [
+        {module, ?MODULE},
+        {line, ?LINE},
+        {reason, Msg}
+    ]})).
+
+
+start() ->
+    ok = application:set_env(config, ini_files, ?CONFIG_CHAIN),
+    {ok, Started} = application:ensure_all_started(couch_js),
+    config:set("native_query_servers", "enable_erlang_query_server", "true", false),
+    config:set("query_server_config", "os_process_limit", "3", false),
+    config:set("query_server_config", "os_process_soft_limit", "2", false),
+    config:set("query_server_config", "os_process_idle_limit", "1", false),
+    ok = config_wait("os_process_idle_limit", "1"),
+    Started.
+
+
+stop(Apps) ->
+    lists:foreach(fun(App) ->
+        ok = application:stop(App)
+    end, lists:reverse(Apps)).
+
+
+couch_js_proc_manager_test_() ->
+    {
+        "couch_js_proc_manger tests",
+        {
+            setup,
+            fun start/0,
+            fun stop/1,
+            [
+                ?TDEF(should_block_new_proc_on_full_pool),
+                ?TDEF(should_free_slot_on_proc_unexpected_exit),
+                ?TDEF(should_reuse_known_proc),
+                ?TDEF(should_process_waiting_queue_as_fifo),
+                ?TDEF(should_reduce_pool_on_idle_os_procs)
+            ]
+        }
+    }.
+
+
+should_block_new_proc_on_full_pool() ->
+    ok = couch_js_proc_manager:reload(),
+
+    Clients = [
+        spawn_client(),
+        spawn_client(),
+        spawn_client()
+    ],
+
+    lists:foreach(fun(Client) ->
+        ?assertEqual(ok, ping_client(Client))
+    end, Clients),
+
+    % Make sure everyone got a different proc
+    Procs = [get_client_proc(Client) || Client <- Clients],
+    ?assertEqual(lists:sort(Procs), lists:usort(Procs)),
+
+    % This client will be stuck waiting for someone
+    % to give up their proc.
+    Client4 = spawn_client(),
+    ?assert(is_client_waiting(Client4)),
+
+    Client1 = hd(Clients),
+    Proc1 = hd(Procs),
+
+    ?assertEqual(ok, stop_client(Client1)),
+    ?assertEqual(ok, ping_client(Client4)),
+
+    Proc4 = get_client_proc(Client4),
+
+    ?assertEqual(Proc1#proc.pid, Proc4#proc.pid),
+    ?assertNotEqual(Proc1#proc.client, Proc4#proc.client),
+
+    lists:map(fun(C) ->
+        ?assertEqual(ok, stop_client(C))
+    end, [Client4 | tl(Clients)]).
+
+
+should_free_slot_on_proc_unexpected_exit() ->
+    ok = couch_js_proc_manager:reload(),
+
+    Clients = [
+        spawn_client(),
+        spawn_client(),
+        spawn_client()
+    ],
+
+    lists:foreach(fun(Client) ->
+        ?assertEqual(ok, ping_client(Client))
+    end, Clients),
+
+    Procs1 = [get_client_proc(Client) || Client <- Clients],
+    ProcClients1 = [Proc#proc.client || Proc <- Procs1],
+    ?assertEqual(lists:sort(Procs1), lists:usort(Procs1)),
+    ?assertEqual(lists:sort(ProcClients1), lists:usort(ProcClients1)),
+
+    Client1 = hd(Clients),
+    Proc1 = hd(Procs1),
+    ?assertEqual(ok, kill_client(Client1)),
+
+    Client4 = spawn_client(),
+    ?assertEqual(ok, ping_client(Client4)),
+    Proc4 = get_client_proc(Client4),
+
+    ?assertEqual(Proc1#proc.pid, Proc4#proc.pid),
+    ?assertNotEqual(Proc1#proc.client, Proc4#proc.client),
+
+    Procs2 = [Proc4 | tl(Procs1)],
+    ProcClients2 = [Proc4#proc.client | tl(ProcClients1)],
+    ?assertEqual(lists:sort(Procs2), lists:usort(Procs2)),
+    ?assertEqual(lists:sort(ProcClients2), lists:usort(ProcClients2)),
+
+    lists:map(fun(C) ->
+        ?assertEqual(ok, stop_client(C))
+    end, [Client4 | tl(Clients)]).
+
+
+should_reuse_known_proc() ->
+    ok = couch_js_proc_manager:reload(),
+
+    Clients = [
+        spawn_client(<<"ddoc1">>),
+        spawn_client(<<"ddoc2">>)
+    ],
+
+    lists:foreach(fun(Client) ->
+        ?assertEqual(ok, ping_client(Client))
+    end, Clients),
+
+    Procs = [get_client_proc(Client) || Client <- Clients],
+    ?assertEqual(lists:sort(Procs), lists:usort(Procs)),
+
+    lists:foreach(fun(Client) ->
+        ?assertEqual(ok, stop_client(Client))
+    end, Clients),
+
+    lists:foreach(fun(Proc) ->
+        ?assert(is_process_alive(Proc#proc.pid))
+    end, Procs),
+
+    Client = spawn_client(<<"ddoc1">>),
+    ?assertEqual(ok, ping_client(Client)),
+
+    OldProc = hd(Procs),
+    NewProc = get_client_proc(Client),
+
+    ?assertEqual(OldProc#proc.pid, NewProc#proc.pid),
+    ?assertNotEqual(OldProc#proc.client, NewProc#proc.client),
+    ?assertEqual(ok, stop_client(Client)).
+
+
+should_process_waiting_queue_as_fifo() ->
+    Clients = [
+        spawn_client(<<"ddoc1">>),
+        spawn_client(<<"ddoc2">>),
+        spawn_client(<<"ddoc3">>),
+        spawn_client(<<"ddoc4">>),
+        spawn_client(<<"ddoc5">>),
+        spawn_client(<<"ddoc6">>)
+    ],
+
+    lists:foldl(fun(Client, Pos) ->
+        case Pos =< ?NUM_PROCS of
+            true ->
+                ?assertEqual(ok, ping_client(Client));
+            false ->
+                ?assert(is_client_waiting(Client))
+        end,
+        Pos + 1
+    end, 1, Clients),
+
+    LastClients = lists:foldl(fun(_Iteration, ClientAcc) ->
+        FirstClient = hd(ClientAcc),
+        FirstProc = get_client_proc(FirstClient),
+        ?assertEqual(ok, stop_client(FirstClient)),
+
+        RestClients = tl(ClientAcc),
+
+        lists:foldl(fun(Client, Pos) ->
+            case Pos =< ?NUM_PROCS of
+                true ->
+                    ?assertEqual(ok, ping_client(Client));
+                false ->
+                    ?assert(is_client_waiting(Client))
+            end,
+            if Pos /= ?NUM_PROCS -> ok; true ->
+                BubbleProc = get_client_proc(Client),
+                ?assertEqual(FirstProc#proc.pid, BubbleProc#proc.pid),
+                ?assertNotEqual(FirstProc#proc.client, BubbleProc#proc.client)
+            end,
+            Pos + 1
+        end, 1, RestClients),
+
+        RestClients
+    end, Clients, lists:seq(1, 3)),
+
+    lists:foreach(fun(Client) ->
+        ?assertEqual(ok, stop_client(Client))
+    end, LastClients).
+
+
+should_reduce_pool_on_idle_os_procs() ->
+    Clients = [
+        spawn_client(<<"ddoc1">>),
+        spawn_client(<<"ddoc2">>),
+        spawn_client(<<"ddoc3">>)
+    ],
+
+    lists:foreach(fun(Client) ->
+        ?assertEqual(ok, ping_client(Client))
+    end, Clients),
+
+    ?assertEqual(3, couch_js_proc_manager:get_proc_count()),
+
+    lists:foreach(fun(Client) ->
+        ?assertEqual(ok, stop_client(Client))
+    end, Clients),
+
+    ?assertEqual(3, couch_js_proc_manager:get_proc_count()),
+
+    timer:sleep(1200),
+
+    ?assertEqual(1, couch_js_proc_manager:get_proc_count()).
+
+
+spawn_client() ->
+    Parent = self(),
+    Ref = make_ref(),
+    {Pid, _} = spawn_monitor(fun() ->
+        Parent ! {self(), initialized},
+        Proc = couch_js_query_servers:get_os_process(<<"erlang">>),
+        loop(Parent, Ref, Proc)
+    end),
+    receive
+        {Pid, initialized} ->
+            ok
+    after ?TIMEOUT ->
+        ?TIMEOUT_ERROR("Error creating client.")
+    end,
+    {Pid, Ref}.
+
+
+spawn_client(DDocId) ->
+    Parent = self(),
+    Ref = make_ref(),
+    {Pid, _} = spawn_monitor(fun() ->
+        DDocKey = {DDocId, <<"1-abcdefgh">>},
+        DDoc = #doc{body={[{<<"language">>, <<"erlang">>}]}},
+        Parent ! {self(), initialized},
+        Proc = couch_js_query_servers:get_ddoc_process(DDoc, DDocKey),
+        loop(Parent, Ref, Proc)
+    end),
+    receive
+        {Pid, initialized} ->
+            ok
+    after ?TIMEOUT ->
+        ?TIMEOUT_ERROR("Error creating ddoc client.")
+    end,
+    {Pid, Ref}.
+
+
+loop(Parent, Ref, Proc) ->
+    receive
+        ping ->
+            Parent ! {pong, Ref},
+            loop(Parent, Ref, Proc);
+        get_proc  ->
+            Parent ! {proc, Ref, Proc},
+            loop(Parent, Ref, Proc);
+        stop ->
+            couch_js_query_servers:ret_os_process(Proc),
+            Parent ! {stop, Ref};
+        die ->
+            Parent ! {die, Ref},
+            exit(some_error)
+    end.
+
+
+ping_client({Pid, Ref}) ->
+    Pid ! ping,
+    receive
+        {pong, Ref} ->
+            ok
+    after ?TIMEOUT ->
+        ?TIMEOUT_ERROR("Timeout pinging client")
+    end.
+
+
+is_client_waiting({Pid, _Ref}) ->
+    {status, Status} = process_info(Pid, status),
+    {current_function, {M, F, A}} = process_info(Pid, current_function),
+    Status == waiting andalso {M, F, A} == {gen, do_call, 4}.
+
+
+get_client_proc({Pid, Ref}) ->
+    Pid ! get_proc,
+    receive
+        {proc, Ref, Proc} -> Proc
+    after ?TIMEOUT ->
+        ?TIMEOUT_ERROR("Timeout getting proc from client")
+    end.
+
+
+stop_client({Pid, Ref}) ->
+    Pid ! stop,
+    receive
+        {stop, Ref} ->
+            ok
+    after ?TIMEOUT ->
+        ?TIMEOUT_ERROR("Timeout stopping client")
+    end,
+    receive
+        {'DOWN', _, _, Pid, _} ->
+            ok
+    after ?TIMEOUT ->
+        ?TIMEOUT_ERROR("Timeout waiting for stopped client 'DOWN'")
+    end.
+
+
+kill_client({Pid, Ref}) ->
+    Pid ! die,
+    receive
+        {die, Ref} ->
+            ok
+    after ?TIMEOUT ->
+        ?TIMEOUT_ERROR("Timeout killing client")
+    end,
+    receive
+        {'DOWN', _, _, Pid, _} ->
+            ok
+    after ?TIMEOUT ->
+        ?TIMEOUT_ERROR("Timeout waiting for killed client 'DOWN'")
+    end.
+
+
+config_wait(Key, Value) ->
+    config_wait(Key, Value, 0).
+
+config_wait(Key, Value, Count) ->
+    case config:get("query_server_config", Key) of
+        Value ->
+            ok;
+        _ when Count > 10 ->
+            ?TIMEOUT_ERROR("Error waiting for config changes.");
+        _ ->
+            timer:sleep(10),
+            config_wait(Key, Value, Count + 1)
+    end.
diff --git a/src/couch_js/test/couch_js_query_servers_tests.erl b/src/couch_js/test/couch_js_query_servers_tests.erl
new file mode 100644
index 0000000..bc4ecc7
--- /dev/null
+++ b/src/couch_js/test/couch_js_query_servers_tests.erl
@@ -0,0 +1,96 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(couch_js_query_servers_tests).
+
+-include_lib("couch/include/couch_eunit.hrl").
+
+
+setup() ->
+    meck:new([config, couch_log]).
+
+
+teardown(_) ->
+    meck:unload().
+
+
+sum_overflow_test_() ->
+    {
+        "Test overflow detection in the _sum reduce function",
+        {
+            setup,
+            fun setup/0,
+            fun teardown/1,
+            [
+                fun should_return_error_on_overflow/0,
+                fun should_return_object_on_log/0,
+                fun should_return_object_on_false/0
+            ]
+        }
+    }.
+
+
+should_return_error_on_overflow() ->
+    setup_reduce_limit_mock("true"),
+
+    KVs = gen_sum_kvs(),
+    {ok, [Result]} = couch_query_servers:reduce(<<"foo">>, [<<"_sum">>], KVs),
+    ?assertMatch({[{<<"error">>, <<"builtin_reduce_error">>} | _]}, Result),
+
+    check_reduce_limit_mock().
+
+
+should_return_object_on_log() ->
+    setup_reduce_limit_mock("log"),
+
+    KVs = gen_sum_kvs(),
+    {ok, [Result]} = couch_query_servers:reduce(<<"foo">>, [<<"_sum">>], KVs),
+    ?assertMatch({[_ | _]}, Result),
+    Keys = [K || {K, _} <- element(1, Result)],
+    ?assert(not lists:member(<<"error">>, Keys)),
+
+    check_reduce_limit_mock().
+
+
+should_return_object_on_false() ->
+    setup_reduce_limit_mock("false"),
+
+    KVs = gen_sum_kvs(),
+    {ok, [Result]} = couch_query_servers:reduce(<<"foo">>, [<<"_sum">>], KVs),
+    ?assertMatch({[_ | _]}, Result),
+    Keys = [K || {K, _} <- element(1, Result)],
+    ?assert(not lists:member(<<"error">>, Keys)),
+
+    ?assert(meck:called(config, get, '_')),
+    ?assertNot(meck:called(couch_log, error, '_')).
+
+
+gen_sum_kvs() ->
+    lists:map(fun(I) ->
+        Props = lists:map(fun(_) ->
+            K = couch_util:encodeBase64Url(crypto:strong_rand_bytes(16)),
+            {K, 1}
+        end, lists:seq(1, 20)),
+        [I, {Props}]
+    end, lists:seq(1, 10)).
+
+
+setup_reduce_limit_mock(Value) ->
+    ConfigArgs = ["query_server_config", "reduce_limit", "true"],
+    meck:reset([config, couch_log]),
+    meck:expect(config, get, ConfigArgs, Value),
+    meck:expect(couch_log, error, ['_', '_'], ok).
+
+
+check_reduce_limit_mock() ->
+    ?assert(meck:called(config, get, '_')),
+    ?assert(meck:called(couch_log, error, '_')).


[couchdb] 03/03: Update couch_views to use couch_eval

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davisp pushed a commit to branch prototype/fdb-layer-couch-eval
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit b36d1ab3780c2e53dae4e7dad40b59fa32d083b3
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Tue Aug 20 16:16:57 2019 -0500

    Update couch_views to use couch_eval
---
 src/couch_views/src/couch_views.app.src           |  3 +-
 src/couch_views/src/couch_views_indexer.erl       | 65 +++++++++++++++++------
 src/couch_views/test/couch_views_indexer_test.erl |  1 +
 src/couch_views/test/couch_views_map_test.erl     |  7 ++-
 4 files changed, 57 insertions(+), 19 deletions(-)

diff --git a/src/couch_views/src/couch_views.app.src b/src/couch_views/src/couch_views.app.src
index c80c30b..0d666af 100644
--- a/src/couch_views/src/couch_views.app.src
+++ b/src/couch_views/src/couch_views.app.src
@@ -26,6 +26,7 @@
         config,
         couch_stats,
         fabric,
-        couch_jobs
+        couch_jobs,
+        couch_eval
     ]}
 ]}.
diff --git a/src/couch_views/src/couch_views_indexer.erl b/src/couch_views/src/couch_views_indexer.erl
index 83d1b6a..55ce063 100644
--- a/src/couch_views/src/couch_views_indexer.erl
+++ b/src/couch_views/src/couch_views_indexer.erl
@@ -120,7 +120,7 @@ update(#{} = Db, Mrst0, State0) ->
 
     case State4 of
         finished ->
-            couch_query_servers:stop_doc_map(Mrst2#mrst.qserver);
+            couch_eval:release_map_context(Mrst2#mrst.qserver);
         _ ->
             update(Db, Mrst2, State4)
     end.
@@ -171,20 +171,42 @@ map_docs(Mrst, Docs) ->
     % Run all the non deleted docs through the view engine and
     Mrst1 = start_query_server(Mrst),
     QServer = Mrst1#mrst.qserver,
-    MapFun = fun
-        (#{deleted := true} = Change) ->
-            Change#{results => []};
-        (#{deleted := false} = Change) ->
-            #{doc := Doc} = Change,
-            couch_stats:increment_counter([couchdb, mrview, map_doc]),
-            {ok, RawResults} = couch_query_servers:map_doc_raw(QServer, Doc),
-            JsonResults = couch_query_servers:raw_to_ejson(RawResults),
-            ListResults = lists:map(fun(ViewResults) ->
-                [list_to_tuple(Res) || Res <- ViewResults]
-            end, JsonResults),
-            Change#{results => ListResults}
-    end,
-    {Mrst1, lists:map(MapFun, Docs)}.
+
+    {Deleted0, NotDeleted0} = lists:partition(fun(Doc) ->
+        #{deleted := Deleted} = Doc,
+        Deleted
+    end, Docs),
+
+    Deleted1 = lists:map(fun(Doc) ->
+        Doc#{results => []}
+    end, Deleted0),
+
+    DocsToMap = lists:map(fun(Doc) ->
+        #{doc := DocRec} = Doc,
+        DocRec
+    end, NotDeleted0),
+
+    {ok, AllResults} = couch_eval:map_docs(QServer, DocsToMap),
+
+    % The expanded function head here is making an assertion
+    % that the results match the given doc
+    NotDeleted1 = lists:zipwith(fun(#{id := DocId} = Doc, {DocId, Results}) ->
+        Doc#{results => Results}
+    end, NotDeleted0, AllResults),
+
+    % I'm being a bit careful here resorting the docs
+    % in order of the changes feed. Theoretically this is
+    % unnecessary since we're inside a single transaction.
+    % However, I'm concerned if we ever split this up
+    % into multiple transactions that this detail might
+    % be important but forgotten.
+    MappedDocs = lists:sort(fun(A, B) ->
+        #{sequence := ASeq} = A,
+        #{sequence := BSeq} = B,
+        ASeq =< BSeq
+    end, Deleted1 ++ NotDeleted1),
+
+    {Mrst1, MappedDocs}.
 
 
 write_docs(TxDb, Mrst, Docs, State) ->
@@ -249,12 +271,21 @@ fetch_docs(Db, Changes) ->
 
 start_query_server(#mrst{qserver = nil} = Mrst) ->
     #mrst{
+        db_name = DbName,
+        idx_name = DDocId,
         language = Language,
+        sig = Sig,
         lib = Lib,
         views = Views
     } = Mrst,
-    Defs = [View#mrview.def || View <- Views],
-    {ok, QServer} = couch_query_servers:start_doc_map(Language, Defs, Lib),
+    {ok, QServer} = couch_eval:acquire_map_context(
+            DbName,
+            DDocId,
+            Language,
+            Sig,
+            Lib,
+            [View#mrview.def || View <- Views]
+        ),
     Mrst#mrst{qserver = QServer};
 
 start_query_server(#mrst{} = Mrst) ->
diff --git a/src/couch_views/test/couch_views_indexer_test.erl b/src/couch_views/test/couch_views_indexer_test.erl
index 02c8cee..20ad0dc 100644
--- a/src/couch_views/test/couch_views_indexer_test.erl
+++ b/src/couch_views/test/couch_views_indexer_test.erl
@@ -52,6 +52,7 @@ setup() ->
     Ctx = test_util:start_couch([
             fabric,
             couch_jobs,
+            couch_js,
             couch_views
         ]),
     Ctx.
diff --git a/src/couch_views/test/couch_views_map_test.erl b/src/couch_views/test/couch_views_map_test.erl
index 0b0ab68..f8ba183 100644
--- a/src/couch_views/test/couch_views_map_test.erl
+++ b/src/couch_views/test/couch_views_map_test.erl
@@ -20,7 +20,12 @@
 
 
 setup() ->
-    test_util:start_couch([fabric, couch_jobs, couch_views]).
+    test_util:start_couch([
+            fabric,
+            couch_jobs,
+            couch_js,
+            couch_views
+        ]).
 
 
 teardown(State) ->


[couchdb] 01/03: Implement couch_js callbacks for couch_eval

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davisp pushed a commit to branch prototype/fdb-layer-couch-eval
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 0b1e04750540fbca3cc1e5b21006180841905a97
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Tue Aug 20 13:06:46 2019 -0500

    Implement couch_js callbacks for couch_eval
---
 rel/overlay/etc/default.ini   |  6 +++++
 src/couch_js/src/couch_js.erl | 51 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)

diff --git a/rel/overlay/etc/default.ini b/rel/overlay/etc/default.ini
index 51d450b..79555f3 100644
--- a/rel/overlay/etc/default.ini
+++ b/rel/overlay/etc/default.ini
@@ -313,6 +313,12 @@ os_process_limit = 100
 ;query_limit = 268435456
 ;partition_query_limit = 268435456
 
+[couch_eval.languages]
+; The list of modules that implement the couch_eval
+; beahvior for executing provided code in design
+; documents.
+javascript = couch_js
+
 [mango]
 ; Set to true to disable the "index all fields" text index, which can lead
 ; to out of memory issues when users have documents with nested array fields.
diff --git a/src/couch_js/src/couch_js.erl b/src/couch_js/src/couch_js.erl
new file mode 100644
index 0000000..1bc0f19
--- /dev/null
+++ b/src/couch_js/src/couch_js.erl
@@ -0,0 +1,51 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+
+-module(couch_js).
+-behavior(couch_eval).
+
+
+-export([
+    acquire_map_context/1,
+    release_map_context/1,
+    map_docs/2
+]).
+
+
+-include_lib("couch/include/couch_db.hrl").
+
+
+-define(JS, <<"javascript">>).
+
+
+acquire_map_context(Opts) ->
+    #{
+        map_funs := MapFuns,
+        lib := Lib
+    } = Opts,
+    couch_js_query_servers:start_doc_map(?JS, MapFuns, Lib).
+
+
+release_map_context(Proc) ->
+    couch_js_query_servers:stop_doc_map(Proc).
+
+
+map_docs(Proc, Docs) ->
+    {ok, lists:map(fun(Doc) ->
+        {ok, RawResults} = couch_js_query_servers:map_doc_raw(Proc, Doc),
+        Results = couch_js_query_servers:raw_to_ejson(RawResults),
+        Tupled = lists:map(fun(ViewResult) ->
+            lists:map(fun([K, V]) -> {K, V} end, ViewResult)
+        end, Results),
+        {Doc#doc.id, Tupled}
+    end, Docs)}.