dht: ring_position, decorated_key: convert tri_comparators to std::strong_ordering
Convert tri_comparators to return std::strong_ordering rather than int, to prevent confusion with less comparators. Downstream users are either also converted, or adjust the return type back to int, whichever happens to be simpler; in all cases the change it trivial.
This commit is contained in:
@@ -43,7 +43,7 @@ public:
|
||||
const ::schema& schema() const {
|
||||
return *_schema;
|
||||
}
|
||||
friend int tri_compare(const compatible_ring_position_view& x, const compatible_ring_position_view& y) {
|
||||
friend std::strong_ordering tri_compare(const compatible_ring_position_view& x, const compatible_ring_position_view& y) {
|
||||
return dht::ring_position_tri_compare(*x._schema, *x._rpv, *y._rpv);
|
||||
}
|
||||
friend bool operator<(const compatible_ring_position_view& x, const compatible_ring_position_view& y) {
|
||||
@@ -83,7 +83,7 @@ public:
|
||||
const ::schema& schema() const {
|
||||
return *_schema;
|
||||
}
|
||||
friend int tri_compare(const compatible_ring_position& x, const compatible_ring_position& y) {
|
||||
friend std::strong_ordering tri_compare(const compatible_ring_position& x, const compatible_ring_position& y) {
|
||||
return dht::ring_position_tri_compare(*x._schema, *x._rp, *y._rp);
|
||||
}
|
||||
friend bool operator<(const compatible_ring_position& x, const compatible_ring_position& y) {
|
||||
@@ -133,7 +133,7 @@ public:
|
||||
};
|
||||
return std::visit(rpv_accessor{}, *_crp_or_view);
|
||||
}
|
||||
friend int tri_compare(const compatible_ring_position_or_view& x, const compatible_ring_position_or_view& y) {
|
||||
friend std::strong_ordering tri_compare(const compatible_ring_position_or_view& x, const compatible_ring_position_or_view& y) {
|
||||
struct schema_accessor {
|
||||
const ::schema& operator()(const compatible_ring_position& crp) {
|
||||
return crp.schema();
|
||||
|
||||
@@ -97,25 +97,25 @@ decorated_key::equal(const schema& s, const decorated_key& other) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
int
|
||||
std::strong_ordering
|
||||
decorated_key::tri_compare(const schema& s, const decorated_key& other) const {
|
||||
auto r = dht::tri_compare(_token, other._token);
|
||||
if (r != 0) {
|
||||
return r < 0 ? -1 : 1;
|
||||
return r;
|
||||
} else {
|
||||
return _key.legacy_tri_compare(s, other._key);
|
||||
return _key.legacy_tri_compare(s, other._key) <=> 0;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
std::strong_ordering
|
||||
decorated_key::tri_compare(const schema& s, const ring_position& other) const {
|
||||
auto r = dht::tri_compare(_token, other.token());
|
||||
if (r != 0) {
|
||||
return r < 0 ? -1 : 1;
|
||||
return r;
|
||||
} else if (other.has_key()) {
|
||||
return _key.legacy_tri_compare(s, *other.key());
|
||||
return _key.legacy_tri_compare(s, *other.key()) <=> 0;
|
||||
}
|
||||
return -other.relation_to_keys();
|
||||
return 0 <=> other.relation_to_keys();
|
||||
}
|
||||
|
||||
bool
|
||||
@@ -279,7 +279,7 @@ split_range_to_single_shard(const schema& s, const partition_range& pr, shard_id
|
||||
});
|
||||
}
|
||||
|
||||
int ring_position::tri_compare(const schema& s, const ring_position& o) const {
|
||||
std::strong_ordering ring_position::tri_compare(const schema& s, const ring_position& o) const {
|
||||
return ring_position_comparator(s)(*this, o);
|
||||
}
|
||||
|
||||
@@ -295,43 +295,43 @@ bool ring_position::less_compare(const schema& s, const ring_position& other) co
|
||||
return tri_compare(s, other) < 0;
|
||||
}
|
||||
|
||||
int ring_position_tri_compare(const schema& s, ring_position_view lh, ring_position_view rh) {
|
||||
std::strong_ordering ring_position_tri_compare(const schema& s, ring_position_view lh, ring_position_view rh) {
|
||||
auto token_cmp = tri_compare(*lh._token, *rh._token);
|
||||
if (token_cmp != 0) {
|
||||
return token_cmp < 0 ? -1 : 1;
|
||||
return token_cmp;
|
||||
}
|
||||
if (lh._key && rh._key) {
|
||||
auto c = lh._key->legacy_tri_compare(s, *rh._key);
|
||||
if (c) {
|
||||
return c;
|
||||
return c <=> 0;
|
||||
}
|
||||
return lh._weight - rh._weight;
|
||||
return (lh._weight - rh._weight) <=> 0;
|
||||
}
|
||||
if (!lh._key && !rh._key) {
|
||||
return lh._weight - rh._weight;
|
||||
return lh._weight - rh._weight <=> 0;
|
||||
} else if (!lh._key) {
|
||||
return lh._weight > 0 ? 1 : -1;
|
||||
return lh._weight > 0 ? std::strong_ordering::greater : std::strong_ordering::less;
|
||||
} else {
|
||||
return rh._weight > 0 ? -1 : 1;
|
||||
return rh._weight > 0 ? std::strong_ordering::less : std::strong_ordering::greater;
|
||||
}
|
||||
}
|
||||
|
||||
int ring_position_comparator_for_sstables::operator()(ring_position_view lh, sstables::decorated_key_view rh) const {
|
||||
std::strong_ordering ring_position_comparator_for_sstables::operator()(ring_position_view lh, sstables::decorated_key_view rh) const {
|
||||
auto token_cmp = tri_compare(*lh._token, rh.token());
|
||||
if (token_cmp != 0) {
|
||||
return token_cmp < 0 ? -1 : 1;
|
||||
return token_cmp;
|
||||
}
|
||||
if (lh._key) {
|
||||
auto rel = rh.key().tri_compare(s, *lh._key);
|
||||
if (rel) {
|
||||
return -rel;
|
||||
return 0 <=> rel;
|
||||
}
|
||||
}
|
||||
return lh._weight;
|
||||
return lh._weight <=> 0;
|
||||
}
|
||||
|
||||
int ring_position_comparator_for_sstables::operator()(sstables::decorated_key_view a, ring_position_view b) const {
|
||||
return -(*this)(b, a);
|
||||
std::strong_ordering ring_position_comparator_for_sstables::operator()(sstables::decorated_key_view a, ring_position_view b) const {
|
||||
return 0 <=> (*this)(b, a);
|
||||
}
|
||||
|
||||
dht::partition_range
|
||||
|
||||
@@ -49,6 +49,7 @@
|
||||
#include <random>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <compare>
|
||||
#include "range.hh"
|
||||
#include <byteswap.h>
|
||||
#include "dht/token.hh"
|
||||
@@ -110,8 +111,8 @@ public:
|
||||
|
||||
// Trichotomic comparators defining total ordering on the union of
|
||||
// decorated_key and ring_position objects.
|
||||
int tri_compare(const schema& s, const decorated_key& other) const;
|
||||
int tri_compare(const schema& s, const ring_position& other) const;
|
||||
std::strong_ordering tri_compare(const schema& s, const decorated_key& other) const;
|
||||
std::strong_ordering tri_compare(const schema& s, const ring_position& other) const;
|
||||
|
||||
const dht::token& token() const noexcept {
|
||||
return _token;
|
||||
@@ -309,7 +310,7 @@ public:
|
||||
bool equal(const schema&, const ring_position&) const;
|
||||
|
||||
// Trichotomic comparator defining a total ordering on ring_position objects
|
||||
int tri_compare(const schema&, const ring_position&) const;
|
||||
std::strong_ordering tri_compare(const schema&, const ring_position&) const;
|
||||
|
||||
// "less" comparator corresponding to tri_compare()
|
||||
bool less_compare(const schema&, const ring_position&) const;
|
||||
@@ -329,7 +330,7 @@ public:
|
||||
// Such range includes all keys k such that v1 <= k < v2, with order defined by ring_position_comparator.
|
||||
//
|
||||
class ring_position_view {
|
||||
friend int ring_position_tri_compare(const schema& s, ring_position_view lh, ring_position_view rh);
|
||||
friend std::strong_ordering ring_position_tri_compare(const schema& s, ring_position_view lh, ring_position_view rh);
|
||||
friend class ring_position_comparator;
|
||||
friend class ring_position_comparator_for_sstables;
|
||||
friend class ring_position_ext;
|
||||
@@ -566,7 +567,7 @@ public:
|
||||
friend std::ostream& operator<<(std::ostream&, const ring_position_ext&);
|
||||
};
|
||||
|
||||
int ring_position_tri_compare(const schema& s, ring_position_view lh, ring_position_view rh);
|
||||
std::strong_ordering ring_position_tri_compare(const schema& s, ring_position_view lh, ring_position_view rh);
|
||||
|
||||
template <typename T>
|
||||
requires std::is_convertible<T, ring_position_view>::value
|
||||
@@ -579,22 +580,22 @@ struct ring_position_comparator {
|
||||
const schema& s;
|
||||
ring_position_comparator(const schema& s_) : s(s_) {}
|
||||
|
||||
int operator()(ring_position_view lh, ring_position_view rh) const {
|
||||
std::strong_ordering operator()(ring_position_view lh, ring_position_view rh) const {
|
||||
return ring_position_tri_compare(s, lh, rh);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
int operator()(const T& lh, ring_position_view rh) const {
|
||||
std::strong_ordering operator()(const T& lh, ring_position_view rh) const {
|
||||
return ring_position_tri_compare(s, ring_position_view_to_compare(lh), rh);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
int operator()(ring_position_view lh, const T& rh) const {
|
||||
std::strong_ordering operator()(ring_position_view lh, const T& rh) const {
|
||||
return ring_position_tri_compare(s, lh, ring_position_view_to_compare(rh));
|
||||
}
|
||||
|
||||
template <typename T1, typename T2>
|
||||
int operator()(const T1& lh, const T2& rh) const {
|
||||
std::strong_ordering operator()(const T1& lh, const T2& rh) const {
|
||||
return ring_position_tri_compare(s, ring_position_view_to_compare(lh), ring_position_view_to_compare(rh));
|
||||
}
|
||||
};
|
||||
@@ -602,8 +603,8 @@ struct ring_position_comparator {
|
||||
struct ring_position_comparator_for_sstables {
|
||||
const schema& s;
|
||||
ring_position_comparator_for_sstables(const schema& s_) : s(s_) {}
|
||||
int operator()(ring_position_view, sstables::decorated_key_view) const;
|
||||
int operator()(sstables::decorated_key_view, ring_position_view) const;
|
||||
std::strong_ordering operator()(ring_position_view, sstables::decorated_key_view) const;
|
||||
std::strong_ordering operator()(sstables::decorated_key_view, ring_position_view) const;
|
||||
};
|
||||
|
||||
// "less" comparator giving the same order as ring_position_comparator
|
||||
|
||||
@@ -360,7 +360,11 @@ struct repair_sync_boundary {
|
||||
public:
|
||||
tri_compare(const schema& s) : _pk_cmp(s), _position_cmp(s) { }
|
||||
int operator()(const repair_sync_boundary& a, const repair_sync_boundary& b) const {
|
||||
int ret = _pk_cmp(a.pk, b.pk);
|
||||
auto tmp = _pk_cmp(a.pk, b.pk);
|
||||
auto ret = 0;
|
||||
if (tmp != 0) {
|
||||
ret = tmp < 0 ? -1 : 1;
|
||||
}
|
||||
if (ret == 0) {
|
||||
ret = _position_cmp(a.position, b.position);
|
||||
}
|
||||
|
||||
@@ -2968,7 +2968,7 @@ class data_read_resolver : public abstract_read_resolver {
|
||||
|
||||
bool operator()(const primary_key& a, const primary_key& b) const {
|
||||
auto pk_result = a.partition.tri_compare(_schema, b.partition);
|
||||
if (pk_result) {
|
||||
if (pk_result != 0) {
|
||||
return pk_result < 0;
|
||||
}
|
||||
return _ck_cmp(a, b);
|
||||
|
||||
@@ -2257,7 +2257,7 @@ const dht::decorated_key& sstable::get_last_decorated_key() const {
|
||||
return *_last;
|
||||
}
|
||||
|
||||
int sstable::compare_by_first_key(const sstable& other) const {
|
||||
std::strong_ordering sstable::compare_by_first_key(const sstable& other) const {
|
||||
return get_first_decorated_key().tri_compare(*_schema, other.get_first_decorated_key());
|
||||
}
|
||||
|
||||
|
||||
@@ -311,8 +311,7 @@ public:
|
||||
const dht::decorated_key& get_last_decorated_key() const;
|
||||
|
||||
// SSTable comparator using the first key (decorated key).
|
||||
// Return values are those of a trichotomic comparison.
|
||||
int compare_by_first_key(const sstable& other) const;
|
||||
std::strong_ordering compare_by_first_key(const sstable& other) const;
|
||||
|
||||
// SSTable comparator using the max timestamp.
|
||||
// Return values are those of a trichotomic comparison.
|
||||
|
||||
Reference in New Issue
Block a user