[couchdb] 04/04: Support `GET /_dbs_info` endpoint
This is an automated email from the ASF dual-hosted git repository. davisp pushed a commit to branch prototype/fdb-layer-get-dbs-info in repository https://gitbox.apache.org/repos/asf/couchdb.git commit a2f9eea7cc0b9d1ef5b1ef14925ad92e193b9e69 Author: Paul J. Davis AuthorDate: Tue Dec 3 13:44:35 2019 -0600 Support `GET /_dbs_info` endpoint Previously only `POST` with a list of keys was supported. The new `GET` support just dumps all database info blobs in a single ordered response. --- src/chttpd/src/chttpd_misc.erl | 52 +- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/chttpd/src/chttpd_misc.erl b/src/chttpd/src/chttpd_misc.erl index 186ec9f..21a4fef 100644 --- a/src/chttpd/src/chttpd_misc.erl +++ b/src/chttpd/src/chttpd_misc.erl @@ -157,6 +157,40 @@ all_dbs_callback({error, Reason}, #vacc{resp=Resp0}=Acc) -> {ok, Resp1} = chttpd:send_delayed_error(Resp0, Reason), {ok, Acc#vacc{resp=Resp1}}. +handle_dbs_info_req(#httpd{method = 'GET'} = Req) -> +ok = chttpd:verify_is_server_admin(Req), + +#mrargs{ +start_key = StartKey, +end_key = EndKey, +direction = Dir, +limit = Limit, +skip = Skip +} = couch_mrview_http:parse_params(Req, undefined), + +Options = [ +{start_key, StartKey}, +{end_key, EndKey}, +{dir, Dir}, +{limit, Limit}, +{skip, Skip} +], + +% Eventually the Etag for this request will be derived +% from the \xFFmetadataVersion key in fdb +Etag = <<"foo">>, + +{ok, Resp} = chttpd:etag_respond(Req, Etag, fun() -> +Headers = [{"ETag", Etag}], +{ok, Resp} = chttpd:start_delayed_json_response(Req, 200, Headers), +Callback = fun dbs_info_callback/2, +Acc = #vacc{req = Req, resp = Resp}, +fabric2_db:list_dbs_info(Callback, Acc, Options) +end), +case is_record(Resp, vacc) of +true -> {ok, Resp#vacc.resp}; +_ -> {ok, Resp} +end; handle_dbs_info_req(#httpd{method='POST', user_ctx=UserCtx}=Req) -> chttpd:validate_ctype(Req, "application/json"), Props = chttpd:json_body_obj(Req), @@ -188,7 +222,23 @@ handle_dbs_info_req(#httpd{method='POST', user_ctx=UserCtx}=Req) -> send_chunk(Resp, "]"), chttpd:end_json_response(Resp); handle_dbs_info_req(Req) -> -send_method_not_allowed(Req, "POST"). +send_method_not_allowed(Req, "GET,HEAD,POST"). + +dbs_info_callback({meta, _Meta}, #vacc{resp = Resp0} = Acc) -> +{ok, Resp1} = chttpd:send_delayed_chunk(Resp0, "["), +{ok, Acc#vacc{resp = Resp1}}; +dbs_info_callback({row, Props}, #vacc{resp = Resp0} = Acc) -> +Prepend = couch_mrview_http:prepend_val(Acc), +Chunk = [Prepend, ?JSON_ENCODE({Props})], +{ok, Resp1} = chttpd:send_delayed_chunk(Resp0, Chunk), +{ok, Acc#vacc{prepend = ",", resp = Resp1}}; +dbs_info_callback(complete, #vacc{resp = Resp0} = Acc) -> +{ok, Resp1} = chttpd:send_delayed_chunk(Resp0, "]"), +{ok, Resp2} = chttpd:end_delayed_json_response(Resp1), +{ok, Acc#vacc{resp = Resp2}}; +dbs_info_callback({error, Reason}, #vacc{resp = Resp0} = Acc) -> +{ok, Resp1} = chttpd:send_delayed_error(Resp0, Reason), +{ok, Acc#vacc{resp = Resp1}}. handle_task_status_req(#httpd{method='GET'}=Req) -> ok = chttpd:verify_is_server_admin(Req),
[couchdb] 04/04: Support `GET /_dbs_info` endpoint
This is an automated email from the ASF dual-hosted git repository. davisp pushed a commit to branch prototype/fdb-layer-get-dbs-info in repository https://gitbox.apache.org/repos/asf/couchdb.git commit 771acd5ae8837dbc889b93135a1d08eeb7456732 Author: Paul J. Davis AuthorDate: Tue Dec 3 13:44:35 2019 -0600 Support `GET /_dbs_info` endpoint Previously only `POST` with a list of keys was supported. The new `GET` support just dumps all database info blobs in a single ordered response. --- src/chttpd/src/chttpd_misc.erl | 52 +- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/chttpd/src/chttpd_misc.erl b/src/chttpd/src/chttpd_misc.erl index 186ec9f..21a4fef 100644 --- a/src/chttpd/src/chttpd_misc.erl +++ b/src/chttpd/src/chttpd_misc.erl @@ -157,6 +157,40 @@ all_dbs_callback({error, Reason}, #vacc{resp=Resp0}=Acc) -> {ok, Resp1} = chttpd:send_delayed_error(Resp0, Reason), {ok, Acc#vacc{resp=Resp1}}. +handle_dbs_info_req(#httpd{method = 'GET'} = Req) -> +ok = chttpd:verify_is_server_admin(Req), + +#mrargs{ +start_key = StartKey, +end_key = EndKey, +direction = Dir, +limit = Limit, +skip = Skip +} = couch_mrview_http:parse_params(Req, undefined), + +Options = [ +{start_key, StartKey}, +{end_key, EndKey}, +{dir, Dir}, +{limit, Limit}, +{skip, Skip} +], + +% Eventually the Etag for this request will be derived +% from the \xFFmetadataVersion key in fdb +Etag = <<"foo">>, + +{ok, Resp} = chttpd:etag_respond(Req, Etag, fun() -> +Headers = [{"ETag", Etag}], +{ok, Resp} = chttpd:start_delayed_json_response(Req, 200, Headers), +Callback = fun dbs_info_callback/2, +Acc = #vacc{req = Req, resp = Resp}, +fabric2_db:list_dbs_info(Callback, Acc, Options) +end), +case is_record(Resp, vacc) of +true -> {ok, Resp#vacc.resp}; +_ -> {ok, Resp} +end; handle_dbs_info_req(#httpd{method='POST', user_ctx=UserCtx}=Req) -> chttpd:validate_ctype(Req, "application/json"), Props = chttpd:json_body_obj(Req), @@ -188,7 +222,23 @@ handle_dbs_info_req(#httpd{method='POST', user_ctx=UserCtx}=Req) -> send_chunk(Resp, "]"), chttpd:end_json_response(Resp); handle_dbs_info_req(Req) -> -send_method_not_allowed(Req, "POST"). +send_method_not_allowed(Req, "GET,HEAD,POST"). + +dbs_info_callback({meta, _Meta}, #vacc{resp = Resp0} = Acc) -> +{ok, Resp1} = chttpd:send_delayed_chunk(Resp0, "["), +{ok, Acc#vacc{resp = Resp1}}; +dbs_info_callback({row, Props}, #vacc{resp = Resp0} = Acc) -> +Prepend = couch_mrview_http:prepend_val(Acc), +Chunk = [Prepend, ?JSON_ENCODE({Props})], +{ok, Resp1} = chttpd:send_delayed_chunk(Resp0, Chunk), +{ok, Acc#vacc{prepend = ",", resp = Resp1}}; +dbs_info_callback(complete, #vacc{resp = Resp0} = Acc) -> +{ok, Resp1} = chttpd:send_delayed_chunk(Resp0, "]"), +{ok, Resp2} = chttpd:end_delayed_json_response(Resp1), +{ok, Acc#vacc{resp = Resp2}}; +dbs_info_callback({error, Reason}, #vacc{resp = Resp0} = Acc) -> +{ok, Resp1} = chttpd:send_delayed_error(Resp0, Reason), +{ok, Acc#vacc{resp = Resp1}}. handle_task_status_req(#httpd{method='GET'}=Req) -> ok = chttpd:verify_is_server_admin(Req),
[couchdb] 04/04: Support `GET /_dbs_info` endpoint
This is an automated email from the ASF dual-hosted git repository. davisp pushed a commit to branch prototype/fdb-layer-get-dbs-info in repository https://gitbox.apache.org/repos/asf/couchdb.git commit 123c241f66f05423599481dbfeb7d417a0951b81 Author: Paul J. Davis AuthorDate: Tue Dec 3 13:44:35 2019 -0600 Support `GET /_dbs_info` endpoint Previously only `POST` with a list of keys was supported. The new `GET` support just dumps all database info blobs in a single ordered response. --- src/chttpd/src/chttpd_misc.erl | 49 +- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/chttpd/src/chttpd_misc.erl b/src/chttpd/src/chttpd_misc.erl index 186ec9f..b181f3c 100644 --- a/src/chttpd/src/chttpd_misc.erl +++ b/src/chttpd/src/chttpd_misc.erl @@ -157,6 +157,37 @@ all_dbs_callback({error, Reason}, #vacc{resp=Resp0}=Acc) -> {ok, Resp1} = chttpd:send_delayed_error(Resp0, Reason), {ok, Acc#vacc{resp=Resp1}}. +handle_dbs_info_req(#httpd{method = 'GET'} = Req) -> +ok = chttpd:verify_is_server_admin(Req), + +#mrargs{ +start_key = StartKey, +end_key = EndKey, +direction = Dir, +limit = Limit, +skip = Skip +} = couch_mrview_http:parse_params(Req, undefined), + +Options = [ +{start_key, StartKey}, +{end_key, EndKey}, +{dir, Dir}, +{limit, Limit}, +{skip, Skip} +], + +% TODO: Figure out if we can't calculate a valid +% ETag for this request. \xFFmetadataVersion won't +% work as we don't bump versions on size changes + +{ok, Resp} = chttpd:start_delayed_json_response(Req, 200, []), +Callback = fun dbs_info_callback/2, +Acc = #vacc{req = Req, resp = Resp}, +{ok, Resp} = fabric2_db:list_dbs_info(Callback, Acc, Options), +case is_record(Resp, vacc) of +true -> {ok, Resp#vacc.resp}; +_ -> {ok, Resp} +end; handle_dbs_info_req(#httpd{method='POST', user_ctx=UserCtx}=Req) -> chttpd:validate_ctype(Req, "application/json"), Props = chttpd:json_body_obj(Req), @@ -188,7 +219,23 @@ handle_dbs_info_req(#httpd{method='POST', user_ctx=UserCtx}=Req) -> send_chunk(Resp, "]"), chttpd:end_json_response(Resp); handle_dbs_info_req(Req) -> -send_method_not_allowed(Req, "POST"). +send_method_not_allowed(Req, "GET,HEAD,POST"). + +dbs_info_callback({meta, _Meta}, #vacc{resp = Resp0} = Acc) -> +{ok, Resp1} = chttpd:send_delayed_chunk(Resp0, "["), +{ok, Acc#vacc{resp = Resp1}}; +dbs_info_callback({row, Props}, #vacc{resp = Resp0} = Acc) -> +Prepend = couch_mrview_http:prepend_val(Acc), +Chunk = [Prepend, ?JSON_ENCODE({Props})], +{ok, Resp1} = chttpd:send_delayed_chunk(Resp0, Chunk), +{ok, Acc#vacc{prepend = ",", resp = Resp1}}; +dbs_info_callback(complete, #vacc{resp = Resp0} = Acc) -> +{ok, Resp1} = chttpd:send_delayed_chunk(Resp0, "]"), +{ok, Resp2} = chttpd:end_delayed_json_response(Resp1), +{ok, Acc#vacc{resp = Resp2}}; +dbs_info_callback({error, Reason}, #vacc{resp = Resp0} = Acc) -> +{ok, Resp1} = chttpd:send_delayed_error(Resp0, Reason), +{ok, Acc#vacc{resp = Resp1}}. handle_task_status_req(#httpd{method='GET'}=Req) -> ok = chttpd:verify_is_server_admin(Req),