locator: token_metadata: make token_metadata_impl::tokens_iterator a non-nested class
In order to pimplify token_metadata_impl::tokens_iterator, we must make it a non-nested class, since eventually token_metadata_impl will be an incomplete class for users and nested classes cannot be forward declared. So this patch makes it a non-nested class. Two inline functions that referred to it were moved out of class scope so they can see the definition. No functional changes.
This commit is contained in:
@@ -133,6 +133,7 @@ private:
|
||||
};
|
||||
|
||||
class token_metadata;
|
||||
class tokens_iterator_impl;
|
||||
|
||||
class token_metadata_impl final {
|
||||
public:
|
||||
@@ -165,71 +166,7 @@ private:
|
||||
|
||||
std::vector<token> sort_tokens();
|
||||
|
||||
class tokens_iterator :
|
||||
public std::iterator<std::input_iterator_tag, token> {
|
||||
private:
|
||||
tokens_iterator(std::vector<token>::const_iterator it, size_t pos)
|
||||
: _cur_it(it), _ring_pos(pos), _insert_min(false) {}
|
||||
|
||||
public:
|
||||
tokens_iterator(const token& start, const token_metadata_impl* token_metadata, bool include_min = false)
|
||||
: _token_metadata(token_metadata) {
|
||||
_cur_it = _token_metadata->sorted_tokens().begin() + _token_metadata->first_token_index(start);
|
||||
_insert_min = include_min && *_token_metadata->sorted_tokens().begin() != dht::minimum_token();
|
||||
if (_token_metadata->sorted_tokens().empty()) {
|
||||
_min = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool operator==(const tokens_iterator& it) const {
|
||||
return _min == it._min && _cur_it == it._cur_it;
|
||||
}
|
||||
|
||||
bool operator!=(const tokens_iterator& it) const {
|
||||
return _min != it._min || _cur_it != it._cur_it;
|
||||
}
|
||||
|
||||
const token& operator*() {
|
||||
if (_min) {
|
||||
return _min_token;
|
||||
} else {
|
||||
return *_cur_it;
|
||||
}
|
||||
}
|
||||
|
||||
tokens_iterator& operator++() {
|
||||
if (!_min) {
|
||||
if (_ring_pos >= _token_metadata->sorted_tokens().size()) {
|
||||
_cur_it = _token_metadata->sorted_tokens().end();
|
||||
} else {
|
||||
++_cur_it;
|
||||
++_ring_pos;
|
||||
|
||||
if (_cur_it == _token_metadata->sorted_tokens().end()) {
|
||||
_cur_it = _token_metadata->sorted_tokens().begin();
|
||||
_min = _insert_min;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_min = false;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<token>::const_iterator _cur_it;
|
||||
//
|
||||
// position on the token ring starting from token corresponding to
|
||||
// "start"
|
||||
//
|
||||
size_t _ring_pos = 0;
|
||||
bool _insert_min;
|
||||
bool _min = false;
|
||||
const token _min_token = dht::minimum_token();
|
||||
const token_metadata_impl* _token_metadata = nullptr;
|
||||
|
||||
friend class token_metadata_impl;
|
||||
};
|
||||
using tokens_iterator = tokens_iterator_impl;
|
||||
|
||||
public:
|
||||
token_metadata_impl(std::unordered_map<token, inet_address> token_to_endpoint_map, std::unordered_map<inet_address, utils::UUID> endpoints_map, topology topology);
|
||||
@@ -258,9 +195,7 @@ public:
|
||||
_topology.update_endpoint(ep);
|
||||
}
|
||||
|
||||
tokens_iterator tokens_end() const {
|
||||
return tokens_iterator(sorted_tokens().end(), sorted_tokens().size());
|
||||
}
|
||||
tokens_iterator tokens_end() const;
|
||||
|
||||
/**
|
||||
* Creates an iterable range of the sorted tokens starting at the token next
|
||||
@@ -270,11 +205,7 @@ public:
|
||||
*
|
||||
* @return The requested range (see the description above)
|
||||
*/
|
||||
boost::iterator_range<tokens_iterator> ring_range(const token& start, bool include_min = false) const {
|
||||
auto begin = tokens_iterator(start, this, include_min);
|
||||
auto end = tokens_end();
|
||||
return boost::make_iterator_range(begin, end);
|
||||
}
|
||||
boost::iterator_range<tokens_iterator> ring_range(const token& start, bool include_min = false) const;
|
||||
|
||||
boost::iterator_range<tokens_iterator> ring_range(
|
||||
const std::optional<dht::partition_range::bound>& start, bool include_min = false) const;
|
||||
@@ -1025,13 +956,94 @@ public:
|
||||
friend class token_metadata;
|
||||
};
|
||||
|
||||
class tokens_iterator_impl :
|
||||
public std::iterator<std::input_iterator_tag, token> {
|
||||
private:
|
||||
tokens_iterator_impl(std::vector<token>::const_iterator it, size_t pos)
|
||||
: _cur_it(it), _ring_pos(pos), _insert_min(false) {}
|
||||
|
||||
public:
|
||||
tokens_iterator_impl(const token& start, const token_metadata_impl* token_metadata, bool include_min = false)
|
||||
: _token_metadata(token_metadata) {
|
||||
_cur_it = _token_metadata->sorted_tokens().begin() + _token_metadata->first_token_index(start);
|
||||
_insert_min = include_min && *_token_metadata->sorted_tokens().begin() != dht::minimum_token();
|
||||
if (_token_metadata->sorted_tokens().empty()) {
|
||||
_min = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool operator==(const tokens_iterator_impl& it) const {
|
||||
return _min == it._min && _cur_it == it._cur_it;
|
||||
}
|
||||
|
||||
bool operator!=(const tokens_iterator_impl& it) const {
|
||||
return _min != it._min || _cur_it != it._cur_it;
|
||||
}
|
||||
|
||||
const token& operator*() {
|
||||
if (_min) {
|
||||
return _min_token;
|
||||
} else {
|
||||
return *_cur_it;
|
||||
}
|
||||
}
|
||||
|
||||
tokens_iterator_impl& operator++() {
|
||||
if (!_min) {
|
||||
if (_ring_pos >= _token_metadata->sorted_tokens().size()) {
|
||||
_cur_it = _token_metadata->sorted_tokens().end();
|
||||
} else {
|
||||
++_cur_it;
|
||||
++_ring_pos;
|
||||
|
||||
if (_cur_it == _token_metadata->sorted_tokens().end()) {
|
||||
_cur_it = _token_metadata->sorted_tokens().begin();
|
||||
_min = _insert_min;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_min = false;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<token>::const_iterator _cur_it;
|
||||
//
|
||||
// position on the token ring starting from token corresponding to
|
||||
// "start"
|
||||
//
|
||||
size_t _ring_pos = 0;
|
||||
bool _insert_min;
|
||||
bool _min = false;
|
||||
const token _min_token = dht::minimum_token();
|
||||
const token_metadata_impl* _token_metadata = nullptr;
|
||||
|
||||
friend class token_metadata_impl;
|
||||
};
|
||||
|
||||
inline
|
||||
token_metadata_impl::tokens_iterator
|
||||
token_metadata_impl::tokens_end() const {
|
||||
return tokens_iterator(sorted_tokens().end(), sorted_tokens().size());
|
||||
}
|
||||
|
||||
inline
|
||||
boost::iterator_range<token_metadata_impl::tokens_iterator>
|
||||
token_metadata_impl::ring_range(const token& start, bool include_min) const {
|
||||
auto begin = tokens_iterator(start, this, include_min);
|
||||
auto end = tokens_end();
|
||||
return boost::make_iterator_range(begin, end);
|
||||
}
|
||||
|
||||
|
||||
class token_metadata final {
|
||||
std::unique_ptr<token_metadata_impl> _impl;
|
||||
public:
|
||||
using UUID = utils::UUID;
|
||||
using inet_address = gms::inet_address;
|
||||
private:
|
||||
using tokens_iterator = token_metadata_impl::tokens_iterator;
|
||||
using tokens_iterator = tokens_iterator_impl;
|
||||
token_metadata(std::unordered_map<token, inet_address> token_to_endpoint_map, std::unordered_map<inet_address, utils::UUID> endpoints_map, topology topology);
|
||||
public:
|
||||
token_metadata();
|
||||
|
||||
Reference in New Issue
Block a user