diff options
author | Tony Sun <tony.sun427@gmail.com> | 2020-03-23 10:43:34 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-23 10:43:34 -0700 |
commit | f81f117033422c463d6230569973c9c70a1d2565 (patch) | |
tree | 34dcb922b27cbbec8eea0130f04d999aa12e2963 | |
parent | 3fba9ff5bea733b4c722d2bb4f924afe416f6e35 (diff) | |
download | couchdb-f81f117033422c463d6230569973c9c70a1d2565.tar.gz |
add info endpoint for fdb stored views (#2706)
* add info endpoint
This commit adds the info endpoint for design docs stored in fdb.
-rw-r--r-- | src/chttpd/src/chttpd_db.erl | 2 | ||||
-rw-r--r-- | src/couch_views/src/couch_views.erl | 52 | ||||
-rw-r--r-- | src/couch_views/test/couch_views_info_test.erl | 154 |
3 files changed, 205 insertions, 3 deletions
diff --git a/src/chttpd/src/chttpd_db.erl b/src/chttpd/src/chttpd_db.erl index b7a149b09..dea992c23 100644 --- a/src/chttpd/src/chttpd_db.erl +++ b/src/chttpd/src/chttpd_db.erl @@ -368,7 +368,7 @@ bad_action_req(#httpd{path_parts=[_, _, Name|FileNameParts]}=Req, Db, _DDoc) -> handle_design_info_req(#httpd{method='GET'}=Req, Db, #doc{} = DDoc) -> [_, _, Name, _] = Req#httpd.path_parts, - {ok, GroupInfoList} = fabric:get_view_group_info(Db, DDoc), + {ok, GroupInfoList} = couch_views:get_info(Db, DDoc), send_json(Req, 200, {[ {name, Name}, {view_index, {GroupInfoList}} diff --git a/src/couch_views/src/couch_views.erl b/src/couch_views/src/couch_views.erl index 58cfb2467..2268052f8 100644 --- a/src/couch_views/src/couch_views.erl +++ b/src/couch_views/src/couch_views.erl @@ -20,10 +20,11 @@ query/6, % fabric2_index behavior - build_indices/2 + build_indices/2, + get_info/2 ]). - +-include("couch_views.hrl"). -include_lib("couch_mrview/include/couch_mrview.hrl"). @@ -74,6 +75,45 @@ build_indices(#{} = Db, DDocs) when is_list(DDocs) -> end, DDocs). +get_info(Db, DDoc) -> + DbName = fabric2_db:name(Db), + {ok, Mrst} = couch_views_util:ddoc_to_mrst(DbName, DDoc), + Sig = fabric2_util:to_hex(Mrst#mrst.sig), + JobId = <<DbName/binary, "-", Sig/binary>>, + {UpdateSeq, DataSize, Status0} = fabric2_fdb:transactional(Db, fun(TxDb) -> + #{ + tx := Tx + } = TxDb, + Seq = couch_views_fdb:get_update_seq(TxDb, Mrst), + DataSize = get_total_view_size(TxDb, Mrst), + Status = couch_jobs:get_job_state(Tx, ?INDEX_JOB_TYPE, JobId), + {Seq, DataSize, Status} + end), + Status1 = case Status0 of + pending -> true; + running -> true; + _ -> false + end, + UpdateOptions = get_update_options(Mrst), + {ok, [ + {language, Mrst#mrst.language}, + {signature, Sig}, + {sizes, {[ + {active, DataSize} + ]}}, + {update_seq, UpdateSeq}, + {updater_running, Status1}, + {update_options, UpdateOptions} + ]}. + + +get_total_view_size(TxDb, Mrst) -> + ViewIds = [View#mrview.id_num || View <- Mrst#mrst.views], + lists:foldl(fun (ViewId, Total) -> + Total + couch_views_fdb:get_kv_size(TxDb, Mrst, ViewId) + end, 0, ViewIds). + + read_view(Db, Mrst, ViewName, Callback, Acc0, Args) -> fabric2_fdb:transactional(Db, fun(TxDb) -> try @@ -163,3 +203,11 @@ view_cmp(SK, SKD, EK, EKD) -> PackedSK = erlfdb_tuple:pack({BinSK, SKD}), PackedEK = erlfdb_tuple:pack({BinEK, EKD}), PackedSK =< PackedEK. + + +get_update_options(#mrst{design_opts = Opts}) -> + IncDesign = couch_util:get_value(<<"include_design">>, Opts, false), + LocalSeq = couch_util:get_value(<<"local_seq">>, Opts, false), + UpdateOptions = if IncDesign -> [include_design]; true -> [] end + ++ if LocalSeq -> [local_seq]; true -> [] end, + [atom_to_binary(O, latin1) || O <- UpdateOptions]. diff --git a/src/couch_views/test/couch_views_info_test.erl b/src/couch_views/test/couch_views_info_test.erl new file mode 100644 index 000000000..777cdb3dc --- /dev/null +++ b/src/couch_views/test/couch_views_info_test.erl @@ -0,0 +1,154 @@ +% Licensed under the Apache License, Version 2.0 (the "License"); you may not +% use this file except in compliance with the License. You may obtain a copy of +% the License at +% +% http://www.apache.org/licenses/LICENSE-2.0 +% +% Unless required by applicable law or agreed to in writing, software +% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +% License for the specific language governing permissions and limitations under +% the License. + +-module(couch_views_info_test). + + +-include_lib("couch/include/couch_eunit.hrl"). +-include_lib("couch/include/couch_db.hrl"). +-include_lib("couch_mrview/include/couch_mrview.hrl"). +-include_lib("fabric/test/fabric2_test.hrl"). + + +-define(MAP_FUN1, <<"map_fun1">>). + + +setup() -> + Ctx = test_util:start_couch([ + fabric, + couch_jobs, + couch_js, + couch_views + ]), + Ctx. + + +cleanup(Ctx) -> + test_util:stop_couch(Ctx). + + +foreach_setup() -> + {ok, Db} = fabric2_db:create(?tempdb(), [{user_ctx, ?ADMIN_USER}]), + DDoc = create_ddoc(), + Doc1 = doc(0, 1), + + {ok, _} = fabric2_db:update_doc(Db, DDoc, []), + {ok, _} = fabric2_db:update_doc(Db, Doc1, []), + + run_query(Db, DDoc, ?MAP_FUN1), + {ok, Info} = couch_views:get_info(Db, DDoc), + {Db, Info}. + + +foreach_teardown({Db, _}) -> + meck:unload(), + ok = fabric2_db:delete(fabric2_db:name(Db), []). + + +views_info_test_() -> + { + "Views index info test", + { + setup, + fun setup/0, + fun cleanup/1, + { + foreach, + fun foreach_setup/0, + fun foreach_teardown/1, + [ + fun sig_is_binary/1, + fun language_is_js/1, + fun update_seq_is_binary/1, + fun updater_running_is_boolean/1, + fun active_size_is_non_neg_int/1, + fun update_opts_is_bin_list/1 + ] + } + } + }. + + +sig_is_binary({_, Info}) -> + ?_assert(is_binary(prop(signature, Info))). + + +language_is_js({_, Info}) -> + ?_assertEqual(<<"javascript">>, prop(language, Info)). + + +active_size_is_non_neg_int({_, Info}) -> + ?_assert(check_non_neg_int([sizes, active], Info)). + + +updater_running_is_boolean({_, Info}) -> + ?_assert(is_boolean(prop(updater_running, Info))). + + +update_seq_is_binary({_, Info}) -> + ?_assert(is_binary(prop(update_seq, Info))). + + +update_opts_is_bin_list({_, Info}) -> + Opts = prop(update_options, Info), + ?_assert(is_list(Opts) andalso + (Opts == [] orelse lists:all([is_binary(B) || B <- Opts]))). + + +check_non_neg_int(Key, Info) -> + Size = prop(Key, Info), + is_integer(Size) andalso Size >= 0. + + +prop(Key, {Props}) when is_list(Props) -> + prop(Key, Props); + +prop([Key], Info) -> + prop(Key, Info); + +prop([Key | Rest], Info) -> + prop(Rest, prop(Key, Info)); + +prop(Key, Info) when is_atom(Key), is_list(Info) -> + couch_util:get_value(Key, Info). + + +create_ddoc() -> + couch_doc:from_json_obj({[ + {<<"_id">>, <<"_design/bar">>}, + {<<"views">>, {[ + {?MAP_FUN1, {[ + {<<"map">>, <<"function(doc) {emit(doc.val, doc.val);}">>} + ]}} + ]}} + ]}). + + +doc(Id, Val) -> + couch_doc:from_json_obj({[ + {<<"_id">>, list_to_binary(integer_to_list(Id))}, + {<<"val">>, Val} + ]}). + + +fold_fun({meta, _Meta}, Acc) -> + {ok, Acc}; + +fold_fun({row, _} = Row, Acc) -> + {ok, [Row | Acc]}; + +fold_fun(complete, Acc) -> + {ok, lists:reverse(Acc)}. + + +run_query(#{} = Db, DDoc, <<_/binary>> = View) -> + couch_views:query(Db, DDoc, View, fun fold_fun/2, [], #mrargs{}). |