Files
scylla/mutation/tombstone.hh
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

99 lines
2.4 KiB
C++

/*
* Copyright (C) 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include <functional>
#include <compare>
#include "timestamp.hh"
#include "gc_clock.hh"
#include "utils/hashing.hh"
/**
* Represents deletion operation. Can be commuted with other tombstones via apply() method.
* Can be empty.
*/
struct tombstone final {
api::timestamp_type timestamp;
gc_clock::time_point deletion_time;
tombstone(api::timestamp_type timestamp, gc_clock::time_point deletion_time)
: timestamp(timestamp)
, deletion_time(deletion_time)
{ }
tombstone()
: tombstone(api::missing_timestamp, {})
{ }
std::strong_ordering operator<=>(const tombstone& t) const = default;
bool operator==(const tombstone&) const = default;
explicit operator bool() const {
return timestamp != api::missing_timestamp;
}
void apply(const tombstone& t) noexcept {
if (*this < t) {
*this = t;
}
}
// See reversibly_mergeable.hh
void apply_reversibly(tombstone& t) noexcept {
std::swap(*this, t);
apply(t);
}
// See reversibly_mergeable.hh
void revert(tombstone& t) noexcept {
std::swap(*this, t);
}
tombstone operator+(const tombstone& t) {
auto result = *this;
result.apply(t);
return result;
}
};
template <>
struct fmt::formatter<tombstone> : fmt::formatter<std::string_view> {
template <typename FormatContext>
auto format(const tombstone& t, FormatContext& ctx) const {
if (t) {
return fmt::format_to(ctx.out(),
"{{tombstone: timestamp={}, deletion_time={}}}",
t.timestamp, t.deletion_time.time_since_epoch().count());
} else {
return fmt::format_to(ctx.out(),
"{{tombstone: none}}");
}
}
};
static inline std::ostream& operator<<(std::ostream& out, const tombstone& t) {
fmt::print(out, "{}", t);
return out;
}
template<>
struct appending_hash<tombstone> {
template<typename Hasher>
void operator()(Hasher& h, const tombstone& t) const {
feed_hash(h, t.timestamp);
feed_hash(h, t.deletion_time);
}
};
// Determines whether tombstone may be GC-ed.
using can_gc_fn = std::function<bool(tombstone)>;
extern can_gc_fn always_gc;