tonysun83 commented on a change in pull request #962: Mango: generate a warning
instead of an error when an user-specified index cannot be used
URL: https://github.com/apache/couchdb/pull/962#discussion_r149549786
##########
File path: src/mango/src/mango_cursor.erl
##########
@@ -150,12 +151,53 @@ group_indexes_by_type(Indexes) ->
end, ?CURSOR_MODULES).
-maybe_add_warning(UserFun, #idx{type = IndexType}, UserAcc) ->
- case IndexType of
+maybe_add_warning(UserFun, #cursor{index = Index, opts = Opts}, UserAcc) ->
+
+ UseIndexInvalid = case lists:keyfind(use_index, 1, Opts) of
+ {use_index, []} ->
+ [];
+ {use_index, [DesignId]} ->
+ case filter_indexes([Index], DesignId) of
+ [] ->
+ [fmt("_design/~s was not used because it does not contain
a valid index for this query.", [ddoc_name(DesignId)])];
+ _ ->
+ []
+ end;
+ {use_index, [DesignId, ViewName]} ->
+ case filter_indexes([Index], DesignId, ViewName) of
+ [] ->
+ [fmt("_design/~s, ~s was not used because it is not a
valid index for this query.", [ddoc_name(DesignId), ViewName])];
+ _ ->
+ []
+ end
+ end,
+
+ NoIndex = case Index#idx.type of
<<"special">> ->
- Arg = {add_key, warning, <<"no matching index found, create an
index to optimize query time">>},
- {_Go, UserAcc0} = UserFun(Arg, UserAcc),
- UserAcc0;
+ [<<"no matching index found, create an index to optimize query
time">>];
_ ->
- UserAcc
- end.
\ No newline at end of file
+ []
+ end,
+
+ maybe_add_warning_int(UseIndexInvalid ++ NoIndex, UserFun, UserAcc).
+
+
+maybe_add_warning_int([], _, UserAcc) ->
+ UserAcc;
+
+maybe_add_warning_int(Warnings, UserFun, UserAcc) ->
+ % only include the first warning
+ Arg = {add_key, warning, hd(Warnings)},
+ {_Go, UserAcc0} = UserFun(Arg, UserAcc),
+ UserAcc0.
+
+
+fmt(Format, Args) ->
+ iolist_to_binary(io_lib:format(Format, Args)).
+
+
+ddoc_name(<<"_design/", Name/binary>>) ->
+ Name;
+
+ddoc_name(Name) ->
+ Name.
Review comment:
missing endline
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services