You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2024/09/04 16:15:02 UTC

(couchdb) branch main updated: Sort emitted KVs in quickjs scanner

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

vatamane pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb.git


The following commit(s) were added to refs/heads/main by this push:
     new 34e86a25a Sort emitted KVs in quickjs scanner
34e86a25a is described below

commit 34e86a25a48cbaf6bb3985317a52070073e3089f
Author: Nick Vatamaniuc <va...@gmail.com>
AuthorDate: Wed Sep 4 00:43:40 2024 -0400

    Sort emitted KVs in quickjs scanner
    
    KVs emitted from a single view are sorted in
    couch_mrview_updater:insert_results/4, so we have to do the same to avoid
    logging false positives. That may happen if emit order is dependent on
    dictionary key order, for instance.
---
 .../src/couch_quickjs_scanner_plugin.erl           | 13 +++++-
 .../test/couch_quickjs_scanner_plugin_tests.erl    | 54 +++++++++++++++++++++-
 2 files changed, 65 insertions(+), 2 deletions(-)

diff --git a/src/couch_quickjs/src/couch_quickjs_scanner_plugin.erl b/src/couch_quickjs/src/couch_quickjs_scanner_plugin.erl
index ca227cdd8..b29be2c26 100644
--- a/src/couch_quickjs/src/couch_quickjs_scanner_plugin.erl
+++ b/src/couch_quickjs/src/couch_quickjs_scanner_plugin.erl
@@ -527,13 +527,24 @@ expected_error({error, {_, {<<"SyntaxError">>, _}}}, {error, {_, {<<"SyntaxError
 expected_error(_, _) ->
     false.
 
+map_sort_kvs(KVs) when is_list(KVs) ->
+    lists:sort(KVs);
+map_sort_kvs(KVs) ->
+    KVs.
+
 % Proc commands
 
 add_lib(#proc{} = Proc, #{} = Lib) ->
     true = prompt(Proc, [<<"add_lib">>, Lib]).
 
 map_doc(#proc{} = Proc, {[_ | _]} = Doc) ->
-    prompt(Proc, [<<"map_doc">>, Doc]).
+    % couch_mrview_updater:insert_results/4 sorts the KVs, so we have to do the
+    % same. Otherwise, we risk showing false positives if emit order changes
+    % between engine versions, for instance.
+    case prompt(Proc, [<<"map_doc">>, Doc]) of
+        Results when is_list(Results) -> lists:map(fun map_sort_kvs/1, Results);
+        Results -> Results
+    end.
 
 reduce(#proc{} = Proc, <<_/binary>> = Src, KVs) ->
     prompt(Proc, [<<"reduce">>, [Src], KVs]).
diff --git a/src/couch_quickjs/test/couch_quickjs_scanner_plugin_tests.erl b/src/couch_quickjs/test/couch_quickjs_scanner_plugin_tests.erl
index 7f0b07448..d3f2e5d27 100644
--- a/src/couch_quickjs/test/couch_quickjs_scanner_plugin_tests.erl
+++ b/src/couch_quickjs/test/couch_quickjs_scanner_plugin_tests.erl
@@ -27,7 +27,8 @@ couch_quickjs_scanner_plugin_test_() ->
             ?TDEF_FE(t_vdu_only, 10),
             ?TDEF_FE(t_filter_only, 10),
             ?TDEF_FE(t_filter_with_expected_error, 10),
-            ?TDEF_FE(t_empty_ddoc, 10)
+            ?TDEF_FE(t_empty_ddoc, 10),
+            ?TDEF_FE(t_multi_emit_map, 10)
         ]
     }.
 
@@ -283,6 +284,26 @@ t_empty_ddoc({_, DbName}) ->
             ok
     end.
 
+t_multi_emit_map({_, DbName}) ->
+    ok = add_doc(DbName, ?DDOC1, ddoc_view_multi_emit(#{})),
+    meck:reset(couch_scanner_server),
+    meck:reset(?PLUGIN),
+    config:set("couch_scanner_plugins", atom_to_list(?PLUGIN), "true", false),
+    wait_exit(10000),
+    ?assertEqual(1, num_calls(start, 2)),
+    case couch_server:with_spidermonkey() of
+        true ->
+            ?assertEqual(1, num_calls(complete, 1)),
+            ?assert(num_calls(doc, 3) >= 5),
+            ?assertEqual(0, couch_stats:sample([couchdb, query_server, process_error_exits])),
+            ?assertEqual(0, couch_stats:sample([couchdb, query_server, process_errors])),
+            ?assertEqual(0, couch_stats:sample([couchdb, query_server, process_exits])),
+            % start and complete = 2, no errors
+            ?assertEqual(2, log_calls(warning));
+        false ->
+            ok
+    end.
+
 reset_stats() ->
     Counters = [
         [couchdb, query_server, process_error_exits],
@@ -432,3 +453,34 @@ ddoc_view(Doc) ->
             }
         }
     }.
+
+ddoc_view_multi_emit(Doc) ->
+    % String.prototype.startsWith used as a differentiator between
+    % SM and QuickJS. Make both emit items in different order. But at the
+    % end of the day, that doesn't matter as those are sorted anyway
+    Doc#{
+        views => #{
+            v1 => #{
+                map => <<
+                    "function(doc) {\n"
+                    "  if(String.prototype.startsWith === undefined) {\n"
+                    "    emit(42,1); emit(40,2); emit(42,3); emit(39,4);\n"
+                    "  } else {\n"
+                    "    emit(40,2); emit(39,4); emit(42,1); emit(42,3)\n"
+                    "  }\n"
+                    "}"
+                >>
+            },
+            v2 => #{
+                map => <<
+                    "function(doc) {\n"
+                    "  if(String.prototype.startsWith === undefined) {\n"
+                    "    emit(99,1); emit(98,2);\n"
+                    "  } else {\n"
+                    "    emit(98,2); emit(99,1)\n"
+                    "  }\n"
+                    "}"
+                >>
+            }
+        }
+    }.