/* * Copyright (C) 2015-present ScyllaDB */ /* * SPDX-License-Identifier: AGPL-3.0-or-later */ #pragma once #include "api.hh" #include "api/api-doc/column_family.json.hh" #include "replica/database.hh" #include #include namespace api { void set_column_family(http_context& ctx, routes& r); const utils::UUID& get_uuid(const sstring& name, const replica::database& db); future<> foreach_column_family(http_context& ctx, const sstring& name, std::function f); template future map_reduce_cf_raw(http_context& ctx, const sstring& name, I init, Mapper mapper, Reducer reducer) { auto uuid = get_uuid(name, ctx.db.local()); using mapper_type = std::function(replica::database&)>; using reducer_type = std::function(std::unique_ptr, std::unique_ptr)>; return ctx.db.map_reduce0(mapper_type([mapper, uuid](replica::database& db) { return std::make_unique(I(mapper(db.find_column_family(uuid)))); }), std::make_unique(std::move(init)), reducer_type([reducer = std::move(reducer)] (std::unique_ptr a, std::unique_ptr b) mutable { return std::make_unique(I(reducer(std::any_cast(std::move(*a)), std::any_cast(std::move(*b))))); })).then([] (std::unique_ptr r) { return std::any_cast(std::move(*r)); }); } template future map_reduce_cf(http_context& ctx, const sstring& name, I init, Mapper mapper, Reducer reducer) { return map_reduce_cf_raw(ctx, name, init, mapper, reducer).then([](const I& res) { return make_ready_future(res); }); } template future map_reduce_cf(http_context& ctx, const sstring& name, I init, Mapper mapper, Reducer reducer, Result result) { return map_reduce_cf_raw(ctx, name, init, mapper, reducer).then([result](const I& res) mutable { result = res; return make_ready_future(result); }); } future map_reduce_cf_time_histogram(http_context& ctx, const sstring& name, std::function f); struct map_reduce_column_families_locally { std::any init; std::function(replica::column_family&)> mapper; std::function(std::unique_ptr, std::unique_ptr)> reducer; future> operator()(replica::database& db) const { auto res = seastar::make_lw_shared>(std::make_unique(init)); return do_for_each(db.get_column_families(), [res, this](const std::pair>& i) { *res = reducer(std::move(*res), mapper(*i.second.get())); }).then([res] { return std::move(*res); }); } }; template future map_reduce_cf_raw(http_context& ctx, I init, Mapper mapper, Reducer reducer) { using mapper_type = std::function(replica::column_family&)>; using reducer_type = std::function(std::unique_ptr, std::unique_ptr)>; auto wrapped_mapper = mapper_type([mapper = std::move(mapper)] (replica::column_family& cf) mutable { return std::make_unique(I(mapper(cf))); }); auto wrapped_reducer = reducer_type([reducer = std::move(reducer)] (std::unique_ptr a, std::unique_ptr b) mutable { return std::make_unique(I(reducer(std::any_cast(std::move(*a)), std::any_cast(std::move(*b))))); }); return ctx.db.map_reduce0(map_reduce_column_families_locally{init, std::move(wrapped_mapper), wrapped_reducer}, std::make_unique(init), wrapped_reducer).then([] (std::unique_ptr res) { return std::any_cast(std::move(*res)); }); } template future map_reduce_cf(http_context& ctx, I init, Mapper mapper, Reducer reducer) { return map_reduce_cf_raw(ctx, init, mapper, reducer).then([](const I& res) { return make_ready_future(res); }); } future get_cf_stats(http_context& ctx, const sstring& name, int64_t replica::column_family_stats::*f); future get_cf_stats(http_context& ctx, int64_t replica::column_family_stats::*f); std::tuple parse_fully_qualified_cf_name(sstring name); }