diff --git a/api/api.hh b/api/api.hh index 4a911ae3d7..7607ec4dc0 100644 --- a/api/api.hh +++ b/api/api.hh @@ -23,31 +23,6 @@ namespace api { -template -std::vector map_to_key_value(const std::map& map) { - std::vector res; - res.reserve(map.size()); - - for (const auto& [key, value] : map) { - res.push_back(T()); - res.back().key = key; - res.back().value = value; - } - return res; -} - -template -std::vector& map_to_key_value(const MAP& map, std::vector& res) { - res.reserve(res.size() + std::size(map)); - - for (const auto& [key, value] : map) { - T val; - val.key = fmt::to_string(key); - val.value = fmt::to_string(value); - res.push_back(val); - } - return res; -} template T map_sum(T&& dest, const S& src) { for (const auto& i : src) { diff --git a/api/storage_service.cc b/api/storage_service.cc index aeaceee092..4bd50bc014 100644 --- a/api/storage_service.cc +++ b/api/storage_service.cc @@ -536,13 +536,15 @@ void unset_sstables_loader(http_context& ctx, routes& r) { } void set_view_builder(http_context& ctx, routes& r, sharded& vb, sharded& g) { - ss::view_build_statuses.set(r, [&ctx, &vb, &g] (std::unique_ptr req) { + ss::view_build_statuses.set(r, [&ctx, &vb, &g] (std::unique_ptr req) -> future { auto keyspace = validate_keyspace(ctx, req); auto view = req->get_path_param("view"); - return vb.local().view_build_statuses(std::move(keyspace), std::move(view), g.local()).then([] (std::unordered_map status) { - std::vector res; - return make_ready_future(map_to_key_value(std::move(status), res)); - }); + co_return json::json_return_type(stream_range_as_array(co_await vb.local().view_build_statuses(std::move(keyspace), std::move(view), g.local()), [] (const auto& i) { + storage_service_json::mapper res; + res.key = i.first; + res.value = i.second; + return res; + })); }); cf::get_built_indexes.set(r, [&vb](std::unique_ptr req) -> future { @@ -580,6 +582,16 @@ static future describe_ring_as_json_for_table(const shar co_return json::json_return_type(stream_range_as_array(co_await ss.local().describe_ring_for_table(keyspace, table), token_range_endpoints_to_json)); } +namespace { +template +storage_service_json::mapper map_to_json(const std::pair& i) { + storage_service_json::mapper val; + val.key = fmt::to_string(i.first); + val.value = fmt::to_string(i.second); + return val; +} +} + static future rest_get_token_endpoint(http_context& ctx, sharded& ss, std::unique_ptr req) { @@ -597,12 +609,7 @@ rest_get_token_endpoint(http_context& ctx, sharded& ss throw bad_param_exception("Either provide both keyspace and table (for tablet table) or neither (for vnodes)"); } - co_return json::json_return_type(stream_range_as_array(token_endpoints, [](const auto& i) { - storage_service_json::mapper val; - val.key = fmt::to_string(i.first); - val.value = fmt::to_string(i.second); - return val; - })); + co_return json::json_return_type(stream_range_as_array(token_endpoints, &map_to_json)); } static @@ -1316,10 +1323,7 @@ rest_get_ownership(http_context& ctx, sharded& ss, std throw httpd::bad_param_exception("storage_service/ownership cannot be used when a keyspace uses tablets"); } - return ss.local().get_ownership().then([] (auto&& ownership) { - std::vector res; - return make_ready_future(map_to_key_value(ownership, res)); - }); + co_return json::json_return_type(stream_range_as_array(co_await ss.local().get_ownership(), &map_to_json)); } static @@ -1336,10 +1340,7 @@ rest_get_effective_ownership(http_context& ctx, sharded res; - return make_ready_future(map_to_key_value(ownership, res)); - }); + co_return json::json_return_type(stream_range_as_array(co_await ss.local().effective_ownership(keyspace_name, table_name), &map_to_json)); } static