You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ga...@apache.org on 2021/03/30 12:50:40 UTC

[couchdb] branch validate-ddoc created (now d279598)

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

garren pushed a change to branch validate-ddoc
in repository https://gitbox.apache.org/repos/asf/couchdb.git.


      at d279598  Validate ddoc uses couch_eval

This branch includes the following new commits:

     new d279598  Validate ddoc uses couch_eval

The 1 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.


[couchdb] 01/01: Validate ddoc uses couch_eval

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

garren pushed a commit to branch validate-ddoc
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit d279598b2e78f625fbb0828e931d8157b5033226
Author: garren smith <ga...@gmail.com>
AuthorDate: Tue Mar 30 14:49:09 2021 +0200

    Validate ddoc uses couch_eval
    
    Change the validate ddoc check to use couch_eval.
    Also add in some extra functions in couch_eval so that the try_compile
    will work.
---
 src/couch_eval/src/couch_eval.erl     | 30 +++++++++++++++++++++++++++++-
 src/couch_js/src/couch_js.erl         | 17 ++++++++++++++++-
 src/couch_mrview/src/couch_mrview.erl | 15 ++++++++-------
 src/fabric/src/fabric2_db.erl         |  2 +-
 4 files changed, 54 insertions(+), 10 deletions(-)

diff --git a/src/couch_eval/src/couch_eval.erl b/src/couch_eval/src/couch_eval.erl
index 3541a5b..cd160ec 100644
--- a/src/couch_eval/src/couch_eval.erl
+++ b/src/couch_eval/src/couch_eval.erl
@@ -17,7 +17,10 @@
 -export([
     acquire_map_context/6,
     release_map_context/1,
-    map_docs/2
+    map_docs/2,
+    acquire_context/1,
+    release_context/1,
+    try_compile/3
 ]).
 
 
@@ -35,6 +38,8 @@
 -type result() :: {doc_id(), [[{any(), any()}]]}.
 -type api_mod() :: atom().
 -type context() :: {api_mod(), any()}.
+-type function_name() :: binary().
+-type function_src() :: binary().
 
 -type context_opts() :: #{
     db_name := db_name(),
@@ -50,6 +55,9 @@
 -callback acquire_map_context(context_opts()) -> {ok, any()} | {error, any()}.
 -callback release_map_context(context()) -> ok | {error, any()}.
 -callback map_docs(context(), [doc()]) -> {ok, [result()]} | {error, any()}.
+-callback acquire_context() -> {ok, any()} | {error, any()}.
+-callback release_context(context()) -> ok | {error, any()}.
+-callback try_compile(context(), function_name(), function_src()) -> ok.
 
 
 -spec acquire_map_context(
@@ -86,6 +94,26 @@ release_map_context({ApiMod, Ctx}) ->
 map_docs({ApiMod, Ctx}, Docs) ->
     ApiMod:map_docs(Ctx, Docs).
 
+-spec acquire_context(language()) -> {ok, context()} | {error, any()}.
+acquire_context(Language) ->
+    ApiMod = get_api_mod(Language),
+    {ok, Ctx} = ApiMod:acquire_context(),
+    {ok, {ApiMod, Ctx}}.
+
+
+-spec release_context(context()) -> ok | {error, any()}.
+release_context(nil) ->
+    ok;
+
+release_context({ApiMod, Ctx}) ->
+    ApiMod:release_context(Ctx).
+
+
+-spec try_compile(context(), function_name(), function_src()) -> ok.
+try_compile({ApiMod, Ctx}, FuncName, FuncSrc) -> 
+    ApiMod:try_compile(Ctx, FuncName, FuncSrc).
+
+
 
 get_api_mod(Language) when is_binary(Language) ->
     try
diff --git a/src/couch_js/src/couch_js.erl b/src/couch_js/src/couch_js.erl
index 1bc0f19..7d6edeb 100644
--- a/src/couch_js/src/couch_js.erl
+++ b/src/couch_js/src/couch_js.erl
@@ -18,7 +18,10 @@
 -export([
     acquire_map_context/1,
     release_map_context/1,
-    map_docs/2
+    map_docs/2,
+    acquire_context/0,
+    release_context/1,
+    try_compile/3
 ]).
 
 
@@ -49,3 +52,15 @@ map_docs(Proc, Docs) ->
         end, Results),
         {Doc#doc.id, Tupled}
     end, Docs)}.
+
+acquire_context() ->
+    Ctx = couch_query_servers:get_os_process(?JS),
+    {ok, Ctx}.
+
+
+release_context(Proc) ->
+    couch_js_query_servers:ret_os_process(Proc).
+
+
+try_compile(Proc, FunName, FunSrc) ->
+    couch_query_servers:try_compile(Proc, map, FunName, FunSrc).
diff --git a/src/couch_mrview/src/couch_mrview.erl b/src/couch_mrview/src/couch_mrview.erl
index 880dfa7..d714c0d 100644
--- a/src/couch_mrview/src/couch_mrview.erl
+++ b/src/couch_mrview/src/couch_mrview.erl
@@ -188,8 +188,8 @@ validate(DbName, _IsDbPartitioned,  DDoc) ->
         (#mrview{reduce_funs = [{Name, _} | _]}) -> Name;
         (_) -> null
     end,
-    ValidateView = fun(Proc, #mrview{def=MapSrc, reduce_funs=Reds}=View) ->
-        couch_query_servers:try_compile(Proc, map, GetName(View), MapSrc),
+    ValidateView = fun(Ctx, #mrview{def=MapSrc, reduce_funs=Reds}=View) ->
+        couch_eval:try_compile(Ctx, GetName(View), MapSrc),
         lists:foreach(fun
             ({_RedName, <<"_sum", _/binary>>}) ->
                 ok;
@@ -203,7 +203,7 @@ validate(DbName, _IsDbPartitioned,  DDoc) ->
                 Msg = ["`", Bad, "` is not a supported reduce function."],
                 throw({invalid_design_doc, Msg});
             ({RedName, RedSrc}) ->
-                couch_query_servers:try_compile(Proc, reduce, RedName, RedSrc)
+                couch_eval:try_compile(Ctx, RedName, RedSrc)
         end, Reds)
     end,
     {ok, #mrst{
@@ -211,14 +211,15 @@ validate(DbName, _IsDbPartitioned,  DDoc) ->
         views = Views
     }} = couch_mrview_util:ddoc_to_mrst(DbName, DDoc),
 
-    try Views =/= [] andalso couch_query_servers:get_os_process(Lang) of
+    try Views =/= [] andalso couch_eval:acquire_context(Lang) of
         false ->
             ok;
-        Proc ->
+        {ok, Ctx} ->
             try
-                lists:foreach(fun(V) -> ValidateView(Proc, V) end, Views)
+                lists:foreach(fun(V) -> ValidateView(Ctx, V) end, Views)
             after
-                couch_query_servers:ret_os_process(Proc)
+                couch_eval:release_context(Ctx),
+                ok
             end
     catch {unknown_query_language, _Lang} ->
     %% Allow users to save ddocs written in unknown languages
diff --git a/src/fabric/src/fabric2_db.erl b/src/fabric/src/fabric2_db.erl
index 6d8bb72..4e0a9fd 100644
--- a/src/fabric/src/fabric2_db.erl
+++ b/src/fabric/src/fabric2_db.erl
@@ -2154,7 +2154,7 @@ validate_doc_update(Db, Doc, PrevDoc) ->
 
 validate_ddoc(Db, DDoc) ->
     try
-        ok = couch_index_server:validate(Db, couch_doc:with_ejson_body(DDoc))
+        ok = couch_mrview:validate(Db, couch_doc:with_ejson_body(DDoc))
     catch
         throw:{invalid_design_doc, Reason} ->
             throw({bad_request, invalid_design_doc, Reason});