mirror of
https://github.com/scylladb/scylladb.git
synced 2026-04-26 03:20:37 +00:00
This class exists for one purpose only: to serve as glue code between dht::ring_position and boost::icl::interval_map. The latter requires that keys in its intervals are: * default constructible * copyable * have standalone compare operations For this reason we have to wrap `dht::ring_position` in a class, together with a schema to provide all this. This is `compatible_ring_position`. There is one further requirement by code using the interval map: it wants to do lookups without copying the lookup key(s). To solve this, we came up with `compatible_ring_position_or_view` which is a union of a key or a key view + schema. As we recently found out, boost::icl copies its keys **a lot**. It seems to assume these keys are cheap to copy and carelessly copies them around even when iterating over the map. But `compatible_ring_position_or_view` is not cheap to copy as it copies a `dht::ring_position` which allocates, and it does that via an `std::optional` and `std::variant` to add insult to injury. This patch make said class cheap to copy, by getting rid of the variant and storing the `dht::ring_position` via a shared pointer. The view is stored separately and either points to the ring position stored in the shared pointer or to an outside ring position (for lookups). Fixes: #11669 Closes #11670
56 lines
2.4 KiB
C++
56 lines
2.4 KiB
C++
/*
|
|
* Copyright (C) 2019-present ScyllaDB
|
|
*/
|
|
|
|
/*
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
|
|
#pragma once
|
|
|
|
#include "dht/i_partitioner.hh"
|
|
|
|
// Wraps ring_position or ring_position_view so either is compatible with old-style C++: default
|
|
// constructor, stateless comparators, yada yada.
|
|
// The motivations for supporting both types are to make containers self-sufficient by not relying
|
|
// on callers to keep ring position alive, allow lookup on containers that don't support different
|
|
// key types, and also avoiding unnecessary copies.
|
|
class compatible_ring_position_or_view {
|
|
schema_ptr _schema;
|
|
lw_shared_ptr<dht::ring_position> _rp;
|
|
dht::ring_position_view_opt _rpv; // optional only for default ctor, nothing more
|
|
public:
|
|
compatible_ring_position_or_view() = default;
|
|
explicit compatible_ring_position_or_view(schema_ptr s, dht::ring_position rp)
|
|
: _schema(std::move(s)), _rp(make_lw_shared<dht::ring_position>(std::move(rp))), _rpv(dht::ring_position_view(*_rp)) {
|
|
}
|
|
explicit compatible_ring_position_or_view(const schema& s, dht::ring_position_view rpv)
|
|
: _schema(s.shared_from_this()), _rpv(rpv) {
|
|
}
|
|
const dht::ring_position_view& position() const {
|
|
return *_rpv;
|
|
}
|
|
friend std::strong_ordering tri_compare(const compatible_ring_position_or_view& x, const compatible_ring_position_or_view& y) {
|
|
return dht::ring_position_tri_compare(*x._schema, x.position(), y.position());
|
|
}
|
|
friend bool operator<(const compatible_ring_position_or_view& x, const compatible_ring_position_or_view& y) {
|
|
return tri_compare(x, y) < 0;
|
|
}
|
|
friend bool operator<=(const compatible_ring_position_or_view& x, const compatible_ring_position_or_view& y) {
|
|
return tri_compare(x, y) <= 0;
|
|
}
|
|
friend bool operator>(const compatible_ring_position_or_view& x, const compatible_ring_position_or_view& y) {
|
|
return tri_compare(x, y) > 0;
|
|
}
|
|
friend bool operator>=(const compatible_ring_position_or_view& x, const compatible_ring_position_or_view& y) {
|
|
return tri_compare(x, y) >= 0;
|
|
}
|
|
friend bool operator==(const compatible_ring_position_or_view& x, const compatible_ring_position_or_view& y) {
|
|
return tri_compare(x, y) == 0;
|
|
}
|
|
friend bool operator!=(const compatible_ring_position_or_view& x, const compatible_ring_position_or_view& y) {
|
|
return tri_compare(x, y) != 0;
|
|
}
|
|
};
|