counters: specialize fmt::formatter<counter_{shard,cell}_view>

this is a part of a series to migrating from `operator<<(ostream&, ..)`
based formatting to fmtlib based formatting. the goal here is to enable
fmtlib to print `counter_shard_view` and `counter_cell_view` without the
help of `operator<<`.

the corresponding `operator<<()` is removed in this change, as all its
callers are now using fmtlib for formatting now.

Refs #13245

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>

Closes #13967
This commit is contained in:
Kefu Chai
2023-05-20 17:36:14 +08:00
committed by Avi Kivity
parent 1e63cf6c50
commit 3928a9a4e9
4 changed files with 28 additions and 14 deletions

View File

@@ -12,17 +12,6 @@
#include <boost/range/algorithm/sort.hpp>
std::ostream& operator<<(std::ostream& os, counter_shard_view csv) {
fmt::print(os, "{{global_shard id: {} value: {}, clock: {}}}",
csv.id(), csv.value(), csv.logical_clock());
return os;
}
std::ostream& operator<<(std::ostream& os, counter_cell_view ccv) {
fmt::print(os, "{{counter_cell timestamp: {} shards: {{{}}}}}", ccv.timestamp(), fmt::join(ccv.shards(), ", "));
return os;
}
void counter_cell_builder::do_sort_and_remove_duplicates()
{
boost::range::sort(_shards, [] (auto& a, auto& b) { return a.id() < b.id(); });

View File

@@ -88,7 +88,14 @@ public:
using counter_shard_view = basic_counter_shard_view<mutable_view::no>;
std::ostream& operator<<(std::ostream& os, counter_shard_view csv);
template <>
struct fmt::formatter<counter_shard_view> : fmt::formatter<std::string_view> {
template <typename FormatContext>
auto format(const counter_shard_view& csv, FormatContext& ctx) const {
return fmt::format_to(ctx.out(), "{{global_shard id: {} value: {}, clock: {}}}",
csv.id(), csv.value(), csv.logical_clock());
}
};
class counter_shard {
counter_id _id;
@@ -346,8 +353,15 @@ struct counter_cell_view : basic_counter_cell_view<mutable_view::no> {
// Computes a counter cell containing minimal amount of data which, when
// applied to 'b' returns the same cell as 'a' and 'b' applied together.
static std::optional<atomic_cell> difference(atomic_cell_view a, atomic_cell_view b);
};
friend std::ostream& operator<<(std::ostream& os, counter_cell_view ccv);
template <>
struct fmt::formatter<counter_cell_view> : fmt::formatter<std::string_view> {
template <typename FormatContext>
auto format(const counter_cell_view& ccv, FormatContext& ctx) const {
return fmt::format_to(ctx.out(), "{{counter_cell timestamp: {} shards: {{{}}}}}",
ccv.timestamp(), fmt::join(ccv.shards(), ", "));
}
};
struct counter_cell_mutable_view : basic_counter_cell_view<mutable_view::yes> {

View File

@@ -23,6 +23,16 @@
#include "mutation/frozen_mutation.hh"
#include "mutation/mutation_partition_view.hh"
std::ostream& boost_test_print_type(std::ostream& os, const counter_shard_view& csv) {
fmt::print(os, "{}", csv);
return os;
}
std::ostream& boost_test_print_type(std::ostream& os, const counter_cell_view& ccv) {
fmt::print(os, "{}", ccv);
return os;
}
void verify_shard_order(counter_cell_view ccv) {
if (ccv.shards().begin() == ccv.shards().end()) {
return;

View File

@@ -2270,7 +2270,8 @@ SEASTAR_TEST_CASE(test_wrong_counter_shard_order) {
auto acv = ac_o_c.as_atomic_cell(s->regular_column_at(id));
counter_cell_view ccv(acv);
counter_shard_view::less_compare_by_id cmp;
BOOST_REQUIRE_MESSAGE(boost::algorithm::is_sorted(ccv.shards(), cmp), ccv << " is expected to be sorted");
BOOST_REQUIRE_MESSAGE(boost::algorithm::is_sorted(ccv.shards(), cmp),
fmt::format("{} is expected to be sorted", ccv));
BOOST_REQUIRE_EQUAL(ccv.total_value(), expected_value);
n++;
});