diff options
author | garren smith <garren.smith@gmail.com> | 2019-03-07 15:46:18 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-07 15:46:18 +0200 |
commit | d98fd880bfa97f799c1bc6a05f2c888160616c53 (patch) | |
tree | f41a13bc1ab407cf685859e586717a85f35626d7 | |
parent | 8ef42f7241f8788afc1b6e7255ce78ce5d5ea5c3 (diff) | |
download | couchdb-d98fd880bfa97f799c1bc6a05f2c888160616c53.tar.gz |
Added more info for a mango sort error (#1970)
Added more info for a mango sort error
The mango sort error can be a bit confusing with using a partitioned
database this gives a little more detail on why mango could not find
a specific index when a sort is used.
-rw-r--r-- | src/mango/src/mango_error.erl | 16 | ||||
-rw-r--r-- | src/mango/src/mango_idx.erl | 27 | ||||
-rw-r--r-- | test/elixir/test/partition_mango_test.exs | 27 |
3 files changed, 65 insertions, 5 deletions
diff --git a/src/mango/src/mango_error.erl b/src/mango/src/mango_error.erl index dcf4b9a7e..2f22552c9 100644 --- a/src/mango/src/mango_error.erl +++ b/src/mango/src/mango_error.erl @@ -25,7 +25,21 @@ info(mango_idx, {no_usable_index, missing_sort_index}) -> { 400, <<"no_usable_index">>, - <<"No index exists for this sort, try indexing by the sort fields.">> + <<"No index exists for this sort, " + "try indexing by the sort fields.">> + }; +info(mango_idx, {no_usable_index, missing_sort_index_partitioned}) -> + { + 400, + <<"no_usable_index">>, + <<"No partitioned index exists for this sort, " + "try indexing by the sort fields.">> + }; +info(mango_idx, {no_usable_index, missing_sort_index_global}) -> + { + 400, + <<"no_usable_index">>, + <<"No global index exists for this sort, try indexing by the sort fields.">> }; info(mango_json_bookmark, {invalid_bookmark, BadBookmark}) -> { diff --git a/src/mango/src/mango_idx.erl b/src/mango/src/mango_idx.erl index 6e2abca5c..c2c26958c 100644 --- a/src/mango/src/mango_idx.erl +++ b/src/mango/src/mango_idx.erl @@ -72,12 +72,23 @@ get_usable_indexes(Db, Selector, Opts) -> case lists:filter(UsableFilter, UsableIndexes1) of [] -> - ?MANGO_ERROR({no_usable_index, missing_sort_index}); + mango_sort_error(Db, Opts); UsableIndexes -> UsableIndexes end. +mango_sort_error(Db, Opts) -> + case {fabric_util:is_partitioned(Db), is_opts_partitioned(Opts)} of + {false, _} -> + ?MANGO_ERROR({no_usable_index, missing_sort_index}); + {true, true} -> + ?MANGO_ERROR({no_usable_index, missing_sort_index_partitioned}); + {true, false} -> + ?MANGO_ERROR({no_usable_index, missing_sort_index_global}) + end. + + recover(Db) -> {ok, DDocs0} = mango_util:open_ddocs(Db), Pred = fun({Props}) -> @@ -410,12 +421,20 @@ get_idx_partitioned(Db, DDocProps) -> Default end. +is_opts_partitioned(Opts) -> + case couch_util:get_value(partition, Opts, <<>>) of + <<>> -> + false; + Partition when is_binary(Partition) -> + true + end. + filter_partition_indexes(Indexes, Opts) -> - PFilt = case couch_util:get_value(partition, Opts) of - <<>> -> + PFilt = case is_opts_partitioned(Opts) of + false -> fun(#idx{partitioned = P}) -> not P end; - Partition when is_binary(Partition) -> + true -> fun(#idx{partitioned = P}) -> P end end, Filt = fun(Idx) -> type(Idx) == <<"special">> orelse PFilt(Idx) end, diff --git a/test/elixir/test/partition_mango_test.exs b/test/elixir/test/partition_mango_test.exs index 5a5978915..3fd38d52b 100644 --- a/test/elixir/test/partition_mango_test.exs +++ b/test/elixir/test/partition_mango_test.exs @@ -633,4 +633,31 @@ defmodule PartitionMangoTest do %{:body => %{"reason" => reason}} = resp assert Regex.match?(~r/Partition must not start/, reason) end + + @tag :with_partitioned_db + test "partitioned query sends correct errors for sort errors", context do + db_name = context[:db_name] + create_partition_docs(db_name) + + url = "/#{db_name}/_partition/foo/_find" + + selector = %{ + selector: %{ + some: "field" + }, + sort: ["some"], + limit: 50 + } + + resp = Couch.post(url, body: selector) + assert resp.status_code == 400 + %{:body => %{"reason" => reason}} = resp + assert Regex.match?(~r/No partitioned index exists for this sort/, reason) + + url = "/#{db_name}/_find" + resp = Couch.post(url, body: selector) + assert resp.status_code == 400 + %{:body => %{"reason" => reason}} = resp + assert Regex.match?(~r/No global index exists for this sort/, reason) + end end |