Files
scylladb/keys.cc
Kefu Chai 3738fcbe05 keys: specialize fmt::formatter<partition_key> and friends
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 following classes without the help of `operator<<`.

- partition_key_view
- partition_key
- partition_key::with_schema_wrapper
- key_with_schema
- clustering_key_prefix
- clustering_key_prefix::with_schema_wrapper

the corresponding `operator<<()` are dropped dropped in this change,
as all its callers are now using fmtlib for formatting now. the helper
of `print_key()` is removed, as its only caller is
`operator<<(std::ostream&, const
clustering_key_prefix::with_schema_wrapper&)`.

the reason why all these operators are replaced in one go is that
we have a template function of `key_to_str()` in `db/large_data_handler.cc`.
this template function is actually the caller of operator<< of
`partition_key::with_schema_wrapper` and
`clustering_key_prefix::with_schema_wrapper`.
so, in order to drop either of these two operator<<, we need to remove
both of them, so that we can switch over to `fmt::to_string()` in this
template function.

Refs scylladb#13245

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
2023-04-14 13:21:30 +08:00

104 lines
3.1 KiB
C++

/*
* Copyright (C) 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <iostream>
#include "keys.hh"
#include "dht/i_partitioner.hh"
#include "clustering_bounds_comparator.hh"
#include <boost/algorithm/string.hpp>
logging::logger klog("keys");
const legacy_compound_view<partition_key_view::c_type>
partition_key_view::legacy_form(const schema& s) const {
return { *get_compound_type(s), _bytes };
}
std::strong_ordering
partition_key_view::legacy_tri_compare(const schema& s, partition_key_view o) const {
auto cmp = legacy_compound_view<c_type>::tri_comparator(*get_compound_type(s));
return cmp(this->representation(), o.representation());
}
std::strong_ordering
partition_key_view::ring_order_tri_compare(const schema& s, partition_key_view k2) const {
auto t1 = dht::get_token(s, *this);
auto t2 = dht::get_token(s, k2);
if (t1 != t2) {
return t1 < t2 ? std::strong_ordering::less : std::strong_ordering::greater;
}
return legacy_tri_compare(s, k2);
}
partition_key partition_key::from_nodetool_style_string(const schema_ptr s, const sstring& key) {
std::vector<sstring> vec;
boost::split(vec, key, boost::is_any_of(":"));
auto it = std::begin(vec);
if (vec.size() != s->partition_key_type()->types().size()) {
throw std::invalid_argument("partition key '" + key + "' has mismatch number of components");
}
std::vector<bytes> r;
r.reserve(vec.size());
for (auto t : s->partition_key_type()->types()) {
r.emplace_back(t->from_string(*it++));
}
return partition_key::from_range(std::move(r));
}
std::ostream& operator<<(std::ostream& out, const bound_kind k) {
switch (k) {
case bound_kind::excl_end:
return out << "excl end";
case bound_kind::incl_start:
return out << "incl start";
case bound_kind::incl_end:
return out << "incl end";
case bound_kind::excl_start:
return out << "excl start";
}
abort();
}
bound_kind invert_kind(bound_kind k) {
switch (k) {
case bound_kind::excl_start: return bound_kind::incl_end;
case bound_kind::incl_start: return bound_kind::excl_end;
case bound_kind::excl_end: return bound_kind::incl_start;
case bound_kind::incl_end: return bound_kind::excl_start;
}
abort();
}
bound_kind reverse_kind(bound_kind k) {
switch (k) {
case bound_kind::excl_start: return bound_kind::excl_end;
case bound_kind::incl_start: return bound_kind::incl_end;
case bound_kind::excl_end: return bound_kind::excl_start;
case bound_kind::incl_end: return bound_kind::incl_start;
}
on_internal_error(klog, format("reverse_kind(): invalid value for `bound_kind`: {}", static_cast<std::underlying_type_t<bound_kind>>(k)));
}
int32_t weight(bound_kind k) {
switch (k) {
case bound_kind::excl_end:
return -2;
case bound_kind::incl_start:
return -1;
case bound_kind::incl_end:
return 1;
case bound_kind::excl_start:
return 2;
}
abort();
}
const thread_local clustering_key_prefix bound_view::_empty_prefix = clustering_key::make_empty();