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 04:52:10 UTC
(couchdb) branch quickjs-scanner-plugin-emit-order updated (761503895 -> 00d4f13b0)
This is an automated email from the ASF dual-hosted git repository.
vatamane pushed a change to branch quickjs-scanner-plugin-emit-order
in repository https://gitbox.apache.org/repos/asf/couchdb.git
discard 761503895 Sort emitted KVs in quickjs scanner
new 00d4f13b0 Sort emitted KVs in quickjs scanner
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 (761503895)
\
N -- N -- N refs/heads/quickjs-scanner-plugin-emit-order (00d4f13b0)
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 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.
Summary of changes:
src/couch_quickjs/src/couch_quickjs_scanner_plugin.erl | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
(couchdb) 01/01: Sort emitted KVs in quickjs scanner
Posted by va...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
vatamane pushed a commit to branch quickjs-scanner-plugin-emit-order
in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit 00d4f13b0e34a3fc82e982c7ed55357c4f2dd253
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..45c91ab20 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"
+ "}"
+ >>
+ }
+ }
+ }.