Skip to content

Commit

Permalink
treewide: migrate from boost::adaptors::transformed to std::views::tr…
Browse files Browse the repository at this point in the history
…ansform

now that we are allowed to use C++23. we now have the luxury of using
`std::views::transform`.

in this change, we:

- replace `boost::adaptors::transformed` with `std::views::transform`
- use `fmt::join()` when appropriate where `boost::algorithm::join()`
  is not applicable to a range view returned by `std::view::transform`.
- use `std::ranges::fold_left()` to accumulate the range returned by
  `std::view::transform`
- use `std::ranges::fold_left()` to get the maximum element in the
  range returned by `std::view::transform`
- use `std::ranges::min()` to get the minimal element in the range
  returned by `std::view::transform`
- use `std::ranges::equal()` to compare the range views returned
  by `std::view::transform`
- remove unused `#include <boost/range/adaptor/transformed.hpp>`

to reduce the dependency to boost for better maintainability, and
leverage standard library features for better long-term support.

this change is part of our ongoing effort to modernize our codebase
and reduce external dependencies where possible.

limitations:

there are still a couple places where we are still using
`boost::adaptors::transformed` due to the lack of a C++23 alternative
for `boost::join()` and `boost::adaptors::uniqued`.

Signed-off-by: Kefu Chai <[email protected]>
  • Loading branch information
tchaikov committed Dec 2, 2024
1 parent 40abebe commit c752383
Show file tree
Hide file tree
Showing 92 changed files with 339 additions and 457 deletions.
14 changes: 7 additions & 7 deletions api/column_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ static future<json::json_return_type> get_cf_histogram(http_context& ctx, utils:
};
return ctx.db.map(fun).then([](const std::vector<utils::ihistogram> &res) {
std::vector<httpd::utils_json::histogram> r;
boost::copy(res | boost::adaptors::transformed(to_json), std::back_inserter(r));
std::ranges::copy(res | std::views::transform(to_json), std::back_inserter(r));
return make_ready_future<json::json_return_type>(r);
});
}
Expand All @@ -227,7 +227,7 @@ static future<json::json_return_type> get_cf_rate_and_histogram(http_context& ct
};
return ctx.db.map(fun).then([](const std::vector<utils::rate_moving_average_and_histogram> &res) {
std::vector<httpd::utils_json::rate_moving_average_and_histogram> r;
boost::copy(res | boost::adaptors::transformed(timer_to_json), std::back_inserter(r));
std::ranges::copy(res | std::views::transform(timer_to_json), std::back_inserter(r));
return make_ready_future<json::json_return_type>(r);
});
}
Expand Down Expand Up @@ -717,25 +717,25 @@ void set_column_family(http_context& ctx, routes& r, sharded<db::system_keyspace

cf::get_bloom_filter_false_ratio.set(r, [&ctx] (std::unique_ptr<http::request> req) {
return map_reduce_cf(ctx, req->get_path_param("name"), ratio_holder(), [] (replica::column_family& cf) {
return boost::accumulate(*cf.get_sstables() | boost::adaptors::transformed(filter_false_positive_as_ratio_holder), ratio_holder());
return std::ranges::fold_left(*cf.get_sstables() | std::views::transform(filter_false_positive_as_ratio_holder), ratio_holder(), std::plus{});
}, std::plus<>());
});

cf::get_all_bloom_filter_false_ratio.set(r, [&ctx] (std::unique_ptr<http::request> req) {
return map_reduce_cf(ctx, ratio_holder(), [] (replica::column_family& cf) {
return boost::accumulate(*cf.get_sstables() | boost::adaptors::transformed(filter_false_positive_as_ratio_holder), ratio_holder());
return std::ranges::fold_left(*cf.get_sstables() | std::views::transform(filter_false_positive_as_ratio_holder), ratio_holder(), std::plus{});
}, std::plus<>());
});

cf::get_recent_bloom_filter_false_ratio.set(r, [&ctx] (std::unique_ptr<http::request> req) {
return map_reduce_cf(ctx, req->get_path_param("name"), ratio_holder(), [] (replica::column_family& cf) {
return boost::accumulate(*cf.get_sstables() | boost::adaptors::transformed(filter_recent_false_positive_as_ratio_holder), ratio_holder());
return std::ranges::fold_left(*cf.get_sstables() | std::views::transform(filter_recent_false_positive_as_ratio_holder), ratio_holder(), std::plus{});
}, std::plus<>());
});

cf::get_all_recent_bloom_filter_false_ratio.set(r, [&ctx] (std::unique_ptr<http::request> req) {
return map_reduce_cf(ctx, ratio_holder(), [] (replica::column_family& cf) {
return boost::accumulate(*cf.get_sstables() | boost::adaptors::transformed(filter_recent_false_positive_as_ratio_holder), ratio_holder());
return std::ranges::fold_left(*cf.get_sstables() | std::views::transform(filter_recent_false_positive_as_ratio_holder), ratio_holder(), std::plus{});
}, std::plus<>());
});

Expand Down Expand Up @@ -1089,7 +1089,7 @@ void set_column_family(http_context& ctx, routes& r, sharded<db::system_keyspace

return ctx.db.map_reduce0([key, uuid] (replica::database& db) -> future<std::unordered_set<sstring>> {
auto sstables = co_await db.find_column_family(uuid).get_sstables_by_partition_key(key);
co_return boost::copy_range<std::unordered_set<sstring>>(sstables | boost::adaptors::transformed([] (auto s) { return s->get_filename(); }));
co_return sstables | std::views::transform([] (auto s) { return s->get_filename(); }) | std::ranges::to<std::unordered_set>();
}, std::unordered_set<sstring>(),
[](std::unordered_set<sstring> a, std::unordered_set<sstring>&& b) mutable {
a.merge(b);
Expand Down
11 changes: 5 additions & 6 deletions auth/resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ service_level_resource_view::service_level_resource_view(const resource &r) {

sstring encode_signature(std::string_view name, std::vector<data_type> args) {
return seastar::format("{}[{}]", name,
fmt::join(args | boost::adaptors::transformed([] (const data_type t) {
fmt::join(args | std::views::transform([] (const data_type t) {
return t->name();
}), "^"));
}
Expand All @@ -209,11 +209,10 @@ std::pair<sstring, std::vector<data_type>> decode_signature(std::string_view enc
}
std::vector<std::string_view> raw_types;
boost::split(raw_types, encoded_signature, boost::is_any_of("^"));
std::vector<data_type> decoded_types = boost::copy_range<std::vector<data_type>>(
raw_types | boost::adaptors::transformed([] (std::string_view raw_type) {
std::vector<data_type> decoded_types =
raw_types | std::views::transform([] (std::string_view raw_type) {
return db::marshal::type_parser::parse(raw_type);
})
);
}) | std::ranges::to<std::vector>();
return {sstring(function_name), decoded_types};
}

Expand All @@ -223,7 +222,7 @@ std::pair<sstring, std::vector<data_type>> decode_signature(std::string_view enc
static sstring decoded_signature_string(std::string_view encoded_signature) {
auto [function_name, arg_types] = decode_signature(encoded_signature);
return seastar::format("{}({})", cql3::util::maybe_quote(sstring(function_name)),
boost::algorithm::join(arg_types | boost::adaptors::transformed([] (data_type t) {
fmt::join(arg_types | std::views::transform([] (data_type t) {
return t->cql3_type_name();
}), ", "));
}
Expand Down
25 changes: 13 additions & 12 deletions compaction/compaction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -546,12 +546,12 @@ class compaction {
}
auto owned_ranges = dht::to_partition_ranges(*_owned_ranges, utils::can_yield::yes);

auto non_owned_ranges = boost::copy_range<dht::partition_range_vector>(sstables
| boost::adaptors::transformed([] (const shared_sstable& sst) {
auto non_owned_ranges = sstables
| std::views::transform([] (const shared_sstable& sst) {
seastar::thread::maybe_yield();
return dht::partition_range::make({sst->get_first_decorated_key(), true},
{sst->get_last_decorated_key(), true});
}));
}) | std::ranges::to<dht::partition_range_vector>();

return dht::subtract_ranges(*_schema, non_owned_ranges, std::move(owned_ranges)).get();
}
Expand Down Expand Up @@ -820,7 +820,7 @@ class compaction {
_rp = std::max(_rp, sst_stats.position);
}
}
log_info("{} [{}]", report_start_desc(), fmt::join(_sstables | boost::adaptors::transformed([] (auto sst) { return to_string(sst, true); }), ","));
log_info("{} [{}]", report_start_desc(), fmt::join(_sstables | std::views::transform([] (auto sst) { return to_string(sst, true); }), ","));
if (ssts->size() < _sstables.size()) {
log_debug("{} out of {} input sstables are fully expired sstables that will not be actually compacted",
_sstables.size() - ssts->size(), _sstables.size());
Expand Down Expand Up @@ -953,7 +953,7 @@ class compaction {
// By the time being, using estimated key count.
log_info("{} {} sstables to [{}]. {} to {} (~{}% of original) in {}ms = {}. ~{} total partitions merged to {}.",
report_finish_desc(), _input_sstable_generations.size(),
fmt::join(ret.new_sstables | boost::adaptors::transformed([] (auto sst) { return to_string(sst, false); }), ","),
fmt::join(ret.new_sstables | std::views::transform([] (auto sst) { return to_string(sst, false); }), ","),
utils::pretty_printed_data_size(_start_size), utils::pretty_printed_data_size(_end_size), int(ratio * 100),
std::chrono::duration_cast<std::chrono::milliseconds>(duration).count(), utils::pretty_printed_throughput(_start_size, duration),
_cdata.total_partitions, _cdata.total_keys_written);
Expand Down Expand Up @@ -1268,8 +1268,8 @@ class regular_compaction : public compaction {

auto exhausted_ssts = std::vector<shared_sstable>(exhausted, _sstables.end());
log_debug("Replacing earlier exhausted sstable(s) [{}] by new sstable(s) [{}]",
fmt::join(exhausted_ssts | boost::adaptors::transformed([] (auto sst) { return to_string(sst, false); }), ","),
fmt::join(_new_unused_sstables | boost::adaptors::transformed([] (auto sst) { return to_string(sst, true); }), ","));
fmt::join(exhausted_ssts | std::views::transform([] (auto sst) { return to_string(sst, false); }), ","),
fmt::join(_new_unused_sstables | std::views::transform([] (auto sst) { return to_string(sst, true); }), ","));
_replacer(get_compaction_completion_desc(exhausted_ssts, std::move(_new_unused_sstables)));
_sstables.erase(exhausted, _sstables.end());
dynamic_cast<compaction_read_monitor_generator&>(unwrap_monitor_generator()).remove_exhausted_sstables(exhausted_ssts);
Expand Down Expand Up @@ -1917,7 +1917,7 @@ static future<compaction_result> scrub_sstables_validate_mode(sstables::compacti
auto permit = table_s.make_compaction_reader_permit();

uint64_t validation_errors = 0;
cdata.compaction_size = boost::accumulate(descriptor.sstables | boost::adaptors::transformed([] (auto& sst) { return sst->data_size(); }), int64_t(0));
cdata.compaction_size = std::ranges::fold_left(descriptor.sstables | std::views::transform([] (auto& sst) { return sst->data_size(); }), int64_t(0), std::plus{});

for (const auto& sst : descriptor.sstables) {
clogger.info("Scrubbing in validate mode {}", sst->get_filename());
Expand Down Expand Up @@ -1992,8 +1992,9 @@ get_fully_expired_sstables(const table_state& table_s, const std::vector<sstable
}
}

auto compacted_undeleted_gens = boost::copy_range<std::unordered_set<generation_type>>(table_s.compacted_undeleted_sstables()
| boost::adaptors::transformed(std::mem_fn(&sstables::sstable::generation)));
auto compacted_undeleted_gens = table_s.compacted_undeleted_sstables()
| std::views::transform(std::mem_fn(&sstables::sstable::generation))
| std::ranges::to<std::unordered_set>();
auto has_undeleted_ancestor = [&compacted_undeleted_gens] (auto& candidate) {
// Get ancestors from sstable which is empty after restart. It works for this purpose because
// we only need to check that a sstable compacted *in this instance* hasn't an ancestor undeleted.
Expand Down Expand Up @@ -2036,11 +2037,11 @@ get_fully_expired_sstables(const table_state& table_s, const std::vector<sstable
}

unsigned compaction_descriptor::fan_in() const {
return boost::copy_range<std::unordered_set<run_id>>(sstables | boost::adaptors::transformed(std::mem_fn(&sstables::sstable::run_identifier))).size();
return std::ranges::distance(sstables | std::views::transform(std::mem_fn(&sstables::sstable::run_identifier)) | std::ranges::to<std::unordered_set>());;
}

uint64_t compaction_descriptor::sstables_size() const {
return boost::accumulate(sstables | boost::adaptors::transformed(std::mem_fn(&sstables::sstable::data_size)), uint64_t(0));
return std::ranges::fold_left(sstables | std::views::transform(std::mem_fn(&sstables::sstable::data_size)), uint64_t(0), std::plus{});
}

}
Expand Down
8 changes: 4 additions & 4 deletions compaction/compaction_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ unsigned compaction_manager::current_compaction_fan_in_threshold() const {
if (_tasks.empty()) {
return 0;
}
auto largest_fan_in = std::ranges::max(_tasks | boost::adaptors::transformed([] (auto& task) {
auto largest_fan_in = std::ranges::max(_tasks | std::views::transform([] (auto& task) {
return task.compaction_running() ? task.compaction_data().compaction_fan_in : 0;
}));
// conservatively limit fan-in threshold to 32, such that tons of small sstables won't accumulate if
Expand Down Expand Up @@ -1364,9 +1364,9 @@ future<> compaction_manager::maybe_wait_for_sstable_count_reduction(table_state&
auto num_runs_for_compaction = [&, this] {
auto& cs = t.get_compaction_strategy();
auto desc = cs.get_sstables_for_compaction(t, get_strategy_control());
return boost::copy_range<std::unordered_set<sstables::run_id>>(
desc.sstables
| boost::adaptors::transformed(std::mem_fn(&sstables::sstable::run_identifier))).size();
return std::ranges::size(desc.sstables
| std::views::transform(std::mem_fn(&sstables::sstable::run_identifier))
| std::ranges::to<std::unordered_set>());
};
const auto threshold = size_t(std::max(schema->max_compaction_threshold(), 32));
auto count = num_runs_for_compaction();
Expand Down
12 changes: 5 additions & 7 deletions compaction/compaction_strategy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#include "cql3/statements/property_definitions.hh"
#include "schema/schema.hh"
#include <boost/range/algorithm/find.hpp>
#include <boost/range/algorithm/remove_if.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include "size_tiered_compaction_strategy.hh"
#include "leveled_compaction_strategy.hh"
#include "time_window_compaction_strategy.hh"
Expand All @@ -49,10 +47,10 @@ compaction_descriptor compaction_strategy_impl::make_major_compaction_job(std::v
std::vector<compaction_descriptor> compaction_strategy_impl::get_cleanup_compaction_jobs(table_state& table_s, std::vector<shared_sstable> candidates) const {
// The default implementation is suboptimal and causes the writeamp problem described issue in #10097.
// The compaction strategy relying on it should strive to implement its own method, to make cleanup bucket aware.
return boost::copy_range<std::vector<compaction_descriptor>>(candidates | boost::adaptors::transformed([] (const shared_sstable& sst) {
return candidates | std::views::transform([] (const shared_sstable& sst) {
return compaction_descriptor({ sst },
sst->get_sstable_level(), sstables::compaction_descriptor::default_max_sstable_bytes, sst->run_identifier());
}));
}) | std::ranges::to<std::vector>();
}

bool compaction_strategy_impl::worth_dropping_tombstones(const shared_sstable& sst, gc_clock::time_point compaction_time, const table_state& t) {
Expand Down Expand Up @@ -246,9 +244,9 @@ size_tiered_backlog_tracker::sstables_backlog_contribution size_tiered_backlog_t
if (!size_tiered_compaction_strategy::is_bucket_interesting(bucket, threshold)) {
continue;
}
contrib.value += boost::accumulate(bucket | boost::adaptors::transformed([] (const shared_sstable& sst) -> double {
contrib.value += std::ranges::fold_left(bucket | std::views::transform([] (const shared_sstable& sst) -> double {
return sst->data_size() * log4(sst->data_size());
}), double(0.0f));
}), double(0.0f), std::plus{});
// Controller is disabled if exception is caught during add / remove calls, so not making any effort to make this exception safe
contrib.sstables.insert(bucket.begin(), bucket.end());
}
Expand All @@ -259,7 +257,7 @@ size_tiered_backlog_tracker::sstables_backlog_contribution size_tiered_backlog_t
double size_tiered_backlog_tracker::backlog(const compaction_backlog_tracker::ongoing_writes& ow, const compaction_backlog_tracker::ongoing_compactions& oc) const {
inflight_component compacted = compacted_backlog(oc);

auto total_backlog_bytes = boost::accumulate(_contrib.sstables | boost::adaptors::transformed(std::mem_fn(&sstables::sstable::data_size)), uint64_t(0));
auto total_backlog_bytes = std::ranges::fold_left(_contrib.sstables | std::views::transform(std::mem_fn(&sstables::sstable::data_size)), uint64_t(0), std::plus{});

// Bail out if effective backlog is zero, which happens in a small window where ongoing compaction exhausted
// input files but is still sealing output files or doing managerial stuff like updating history table
Expand Down
1 change: 0 additions & 1 deletion compaction/size_tiered_compaction_strategy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "size_tiered_compaction_strategy.hh"
#include "cql3/statements/property_definitions.hh"

#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/range/algorithm.hpp>

Expand Down
2 changes: 1 addition & 1 deletion compaction/task_manager_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ future<> table_resharding_compaction_task_impl::run() {
auto all_jobs = co_await collect_all_shared_sstables(_dir, _db, _status.keyspace, _status.table, _owned_ranges_ptr);
auto destinations = co_await distribute_reshard_jobs(std::move(all_jobs));

uint64_t total_size = boost::accumulate(destinations | boost::adaptors::transformed(std::mem_fn(&replica::reshard_shard_descriptor::size)), uint64_t(0));
uint64_t total_size = std::ranges::fold_left(destinations | std::views::transform(std::mem_fn(&replica::reshard_shard_descriptor::size)), uint64_t(0), std::plus{});
if (total_size == 0) {
co_return;
}
Expand Down
3 changes: 1 addition & 2 deletions compaction/time_window_compaction_strategy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <boost/range/algorithm/find.hpp>
#include <boost/range/algorithm/remove_if.hpp>
#include <boost/range/algorithm/min_element.hpp>
#include <boost/range/adaptor/transformed.hpp>

#include <ranges>

Expand Down Expand Up @@ -267,7 +266,7 @@ time_window_compaction_strategy::get_reshaping_job(std::vector<shared_sstable> i
single_window.size(), !single_window.empty() && sstable_set_overlapping_count(schema, single_window) == 0);

auto get_job_size = [] (const std::vector<shared_sstable>& ssts) {
return boost::accumulate(ssts | boost::adaptors::transformed(std::mem_fn(&sstable::bytes_on_disk)), uint64_t(0));
return std::ranges::fold_left(ssts | std::views::transform(std::mem_fn(&sstable::bytes_on_disk)), uint64_t(0), std::plus{});
};

// Targets a space overhead of 10%. All disjoint sstables can be compacted together as long as they won't
Expand Down
1 change: 1 addition & 0 deletions cql3/restrictions/statement_restrictions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <algorithm>
#include <boost/range/algorithm.hpp>
#include <boost/range/numeric.hpp>
#include <functional>
#include <ranges>
#include <stdexcept>
Expand Down
10 changes: 4 additions & 6 deletions cql3/selection/selection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ ::shared_ptr<selection> selection::from_selectors(data_dictionary::database db,
auto metadata = collect_metadata(*schema, prepared_selectors);
if (processes_selection(prepared_selectors) || prepared_selectors.size() != defs.size()) {
return ::make_shared<selection_with_processing>(schema, std::move(defs), std::move(metadata),
boost::copy_range<std::vector<expr::expression>>(prepared_selectors | boost::adaptors::transformed(std::mem_fn(&prepared_selector::expr))));
prepared_selectors | std::views::transform(std::mem_fn(&prepared_selector::expr)) | std::ranges::to<std::vector>());
} else {
return ::make_shared<simple_selection>(schema, std::move(defs), std::move(metadata), false);
}
Expand Down Expand Up @@ -583,11 +583,9 @@ bool result_set_builder::last_group_ended() const {
if (_last_group.empty()) {
return !_selectors->is_aggregate();
}
using boost::adaptors::reversed;
using boost::adaptors::transformed;
return !boost::equal(
_last_group | reversed,
_group_by_cell_indices | reversed | transformed([this](size_t i) { return current[i]; }));
return !std::ranges::equal(
_last_group | std::views::reverse,
_group_by_cell_indices | std::views::reverse | std::views::transform([this](size_t i) { return current[i]; }));
}

void result_set_builder::flush_selectors() {
Expand Down
Loading

0 comments on commit c752383

Please sign in to comment.