/* * Copyright (C) 2015-present ScyllaDB */ /* * SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0 */ #pragma once #include "replica/database.hh" #include #include #include "api/api_init.hh" namespace api { void set_column_family(http_context& ctx, httpd::routes& r, sharded& db); void unset_column_family(http_context& ctx, httpd::routes& r); table_info parse_table_info(const sstring& name, const replica::database& db); template future map_reduce_cf_raw(sharded& db, const sstring& name, I init, Mapper mapper, Reducer reducer) { auto uuid = parse_table_info(name, db.local()).id; using mapper_type = std::function>(replica::database&)>; using reducer_type = std::function(std::unique_ptr, std::unique_ptr)>; return db.map_reduce0(mapper_type([mapper, uuid](replica::database& db) { return futurize_invoke([mapper, &db, uuid] { return mapper(db.find_column_family(uuid)); }).then([] (auto result) { return std::make_unique(I(std::move(result))); }); }), 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(sharded& db, const sstring& name, I init, Mapper mapper, Reducer reducer) { return map_reduce_cf_raw(db, name, init, mapper, reducer).then([](const I& res) { return make_ready_future(res); }); } template future map_reduce_cf(sharded& db, const sstring& name, I init, Mapper mapper, Reducer reducer, Result result) { return map_reduce_cf_raw(db, name, init, mapper, reducer).then([result](const I& res) mutable { result = res; return make_ready_future(result); }); } 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 db.get_tables_metadata().for_each_table_gently([res, this] (table_id, seastar::lw_shared_ptr table) -> future<> { *res = reducer(std::move(*res), co_await mapper(*table.get())); }).then([res] () { return std::move(*res); }); } }; template future map_reduce_cf_raw(sharded& db, 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 futurize_invoke([&cf, mapper] { return mapper(cf); }).then([] (auto result) { return std::make_unique(I(std::move(result))); }); }); 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 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(sharded& db, I init, Mapper mapper, Reducer reducer) { return map_reduce_cf_raw(db, init, mapper, reducer).then([](const I& res) { return make_ready_future(res); }); } std::tuple parse_fully_qualified_cf_name(sstring name); }