Files
scylla/tombstone_gc_options.cc
Kefu Chai f5b05cf981 treewide: use defaulted operator!=() and operator==()
in C++20, compiler generate operator!=() if the corresponding
operator==() is already defined, the language now understands
that the comparison is symmetric in the new standard.

fortunately, our operator!=() is always equivalent to
`! operator==()`, this matches the behavior of the default
generated operator!=(). so, in this change, all `operator!=`
are removed.

in addition to the defaulted operator!=, C++20 also brings to us
the defaulted operator==() -- it is able to generated the
operator==() if the member-wise lexicographical comparison.
under some circumstances, this is exactly what we need. so,
in this change, if the operator==() is also implemented as
a lexicographical comparison of all memeber variables of the
class/struct in question, it is implemented using the default
generated one by removing its body and mark the function as
`default`. moreover, if the class happen to have other comparison
operators which are implemented using lexicographical comparison,
the default generated `operator<=>` is used in place of
the defaulted `operator==`.

sometimes, we fail to mark the operator== with the `const`
specifier, in this change, to fulfil the need of C++ standard,
and to be more correct, the `const` specifier is added.

also, to generate the defaulted operator==, the operand should
be `const class_name&`, but it is not always the case, in the
class of `version`, we use `version` as the parameter type, to
fulfill the need of the C++ standard, the parameter type is
changed to `const version&` instead. this does not change
the semantic of the comparison operator. and is a more idiomatic
way to pass non-trivial struct as function parameters.

please note, because in C++20, both operator= and operator<=> are
symmetric, some of the operators in `multiprecision` are removed.
they are the symmetric form of the another variant. if they were
not removed, compiler would, for instance, find ambiguous
overloaded operator '=='.

this change is a cleanup to modernize the code base with C++20
features.

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

Closes #13687
2023-04-27 10:24:46 +03:00

68 lines
2.5 KiB
C++

/*
* Copyright (C) 2021-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include "tombstone_gc_options.hh"
#include "exceptions/exceptions.hh"
#include <boost/lexical_cast.hpp>
#include <seastar/core/sstring.hh>
#include <map>
#include "utils/rjson.hh"
tombstone_gc_options::tombstone_gc_options(const std::map<seastar::sstring, seastar::sstring>& map) {
for (const auto& x : map) {
if (x.first == "mode") {
if (x.second == "disabled") {
_mode = tombstone_gc_mode::disabled;
} else if (x.second == "repair") {
_mode = tombstone_gc_mode::repair;
} else if (x.second == "timeout") {
_mode = tombstone_gc_mode::timeout;
} else if (x.second == "immediate") {
_mode = tombstone_gc_mode::immediate;
} else {
throw exceptions::configuration_exception(format("Invalid value for tombstone_gc option mode: {}", x.second));
}
} else if (x.first == "propagation_delay_in_seconds") {
try {
auto seconds = boost::lexical_cast<int64_t>(x.second);
if (seconds < 0) {
throw exceptions::configuration_exception(format("Invalid value for tombstone_gc option propagation_delay_in_seconds: {}", x.second));
}
_propagation_delay_in_seconds = std::chrono::seconds(seconds);
} catch (...) {
throw exceptions::configuration_exception(format("Invalid value for tombstone_gc option propagation_delay_in_seconds: {}", x.second));
}
} else {
throw exceptions::configuration_exception(format("Invalid tombstone_gc option: {}", x.first));
}
}
}
std::map<seastar::sstring, seastar::sstring> tombstone_gc_options::to_map() const {
std::map<seastar::sstring, seastar::sstring> res = {
{"mode", format("{}", _mode)},
{"propagation_delay_in_seconds", format("{}", _propagation_delay_in_seconds.count())},
};
return res;
}
seastar::sstring tombstone_gc_options::to_sstring() const {
return rjson::print(rjson::from_string_map(to_map()));
}
std::ostream& operator<<(std::ostream& os, const tombstone_gc_mode& mode) {
switch (mode) {
case tombstone_gc_mode::timeout: return os << "timeout";
case tombstone_gc_mode::disabled: return os << "disabled";
case tombstone_gc_mode::immediate: return os << "immediate";
case tombstone_gc_mode::repair: return os << "repair";
}
return os << "unknown";
}