Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: build Mango indexes from dynamic expressions #3912

Open
wants to merge 6 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
Compile jq expressions once per index, not once per document
The first implementation of building Mango indexes from jq expressions
re-compiled the jq program for every document it processed. This is
wasteful and we can instead to this once, when the index definitions are
updated.

When the index definition is loaded in, field descriptions of the form
`{[{<<"$jq">>, Expr}]}` are converted to `{jq, CompiledExpr}`. This
non-JSON term is used because the compiled jq program can be held in
memory but not serialised back out into the design doc.
  • Loading branch information
jcoglan committed Jan 25, 2022
commit c311f83bdeaaa5c6b597b09fc5bf82ac2cc9a588
15 changes: 6 additions & 9 deletions src/mango/src/mango_doc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -416,16 +416,13 @@ get_field(Values, [Name | Rest], Validator) when is_list(Values) ->
get_field(_, [_ | _], _) ->
bad_path.

get_field_jq(Props, Jq) ->
io:format("----[get_field_jq] ~p ~p~n", [Props, Jq]),
case couch_jq:compile(Jq) of
{ok, Program} ->
case couch_jq:eval(Program, Props) of
{ok, Results} -> {jq, Results};
Else -> bad_path
end;
get_field_jq(Props, {ok, Program}) ->
case couch_jq:eval(Program, Props) of
{ok, Results} -> {jq, Results};
Else -> bad_path
end.
end;
get_field_jq(Props, _) ->
bad_path.

rem_field(Props, Field) when is_binary(Field) ->
{ok, Path} = mango_util:parse_field(Field),
Expand Down
26 changes: 24 additions & 2 deletions src/mango/src/mango_native_proc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ handle_call({prompt, [<<"add_fun">>, IndexInfo]}, _From, St) ->
Indexes =
case validate_index_info(IndexInfo) of
true ->
St#st.indexes ++ [IndexInfo];
IndexInfo1 = precompile_jq(IndexInfo),
St#st.indexes ++ [IndexInfo1];
false ->
couch_log:error("No Valid Indexes For: ~p", [IndexInfo]),
St#st.indexes
Expand Down Expand Up @@ -105,6 +106,27 @@ handle_info(Msg, St) ->
code_change(_OldVsn, St, _Extra) ->
{ok, St}.

precompile_jq({IdxProps}) ->
IdxProps1 = lists:map(fun(Prop) ->
case Prop of
{<<"fields">>, {Fields}} ->
{<<"fields">>, {precompile_jq_fields(Fields)}};
Else ->
Else
end
end, IdxProps),
{IdxProps1}.

precompile_jq_fields(Fields) ->
lists:map(fun(Field) ->
case Field of
{FieldName, {[{<<"$jq">>, Jq}]}} ->
{FieldName, {jq, couch_jq:compile(Jq)}};
Else ->
Else
end
end, Fields).

map_doc(#st{indexes = Indexes}, Doc) ->
lists:map(fun(Idx) -> get_index_entries(Idx, Doc) end, Indexes).

Expand All @@ -128,7 +150,7 @@ get_index_values(Fields, Doc) ->

get_index_field_value({Field, FieldDef}, Doc) ->
Value = case FieldDef of
{[{<<"$jq">>, Jq}]} -> mango_doc:get_field_jq(Doc, Jq);
{jq, Jq} -> mango_doc:get_field_jq(Doc, Jq);
_ -> mango_doc:get_field(Doc, Field)
end,
case Value of
Expand Down