forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i_partitioner.hh
133 lines (108 loc) · 4.64 KB
/
i_partitioner.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Modified by ScyllaDB
* Copyright (C) 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: (AGPL-3.0-or-later and Apache-2.0)
*/
#pragma once
#include <seastar/core/shared_ptr.hh>
#include <seastar/core/sstring.hh>
#include <seastar/util/optimized_optional.hh>
#include "keys.hh"
#include <memory>
#include <utility>
#include <byteswap.h>
#include "dht/token.hh"
#include "dht/token-sharding.hh"
#include "dht/decorated_key.hh"
#include "dht/ring_position.hh"
#include "utils/maybe_yield.hh"
namespace dht {
class i_partitioner {
public:
using ptr_type = std::unique_ptr<i_partitioner>;
i_partitioner() = default;
virtual ~i_partitioner() {}
/**
* Transform key to object representation of the on-disk format.
*
* @param key the raw, client-facing key
* @return decorated version of key
*/
decorated_key decorate_key(const schema& s, const partition_key& key) const {
return { get_token(s, key), key };
}
/**
* Transform key to object representation of the on-disk format.
*
* @param key the raw, client-facing key
* @return decorated version of key
*/
decorated_key decorate_key(const schema& s, partition_key&& key) const {
auto token = get_token(s, key);
return { std::move(token), std::move(key) };
}
/**
* @return a token that can be used to route a given key
* (This is NOT a method to create a token from its string representation;
* for that, use tokenFactory.fromString.)
*/
virtual token get_token(const schema& s, partition_key_view key) const = 0;
virtual token get_token(const sstables::key_view& key) const = 0;
// FIXME: token.tokenFactory
//virtual token.tokenFactory gettokenFactory() = 0;
/**
* @return name of partitioner.
*/
virtual const sstring name() const = 0;
bool operator==(const i_partitioner& o) const {
return name() == o.name();
}
};
// Returns the owning shard number for vnode-based replication strategies.
// For the general case, use the sharder obtained from table's effective replication map.
//
// table& tbl;
// auto erm = tbl.get_effective_replication_map();
// auto& sharder = erm->get_sharder();
//
unsigned static_shard_of(const schema&, const token&);
inline decorated_key decorate_key(const schema& s, const partition_key& key) {
return s.get_partitioner().decorate_key(s, key);
}
inline decorated_key decorate_key(const schema& s, partition_key&& key) {
return s.get_partitioner().decorate_key(s, std::move(key));
}
inline token get_token(const schema& s, partition_key_view key) {
return s.get_partitioner().get_token(s, key);
}
dht::partition_range to_partition_range(dht::token_range);
dht::partition_range_vector to_partition_ranges(const dht::token_range_vector& ranges, utils::can_yield can_yield = utils::can_yield::no);
// Each shard gets a sorted, disjoint vector of ranges
std::map<unsigned, dht::partition_range_vector>
split_range_to_shards(dht::partition_range pr, const schema& s, const sharder& sharder);
// Intersect a partition_range with a shard and return the resulting sub-ranges, in sorted order
future<utils::chunked_vector<partition_range>> split_range_to_single_shard(const schema& s,
const static_sharder& sharder, const dht::partition_range& pr, shard_id shard);
std::unique_ptr<dht::i_partitioner> make_partitioner(sstring name);
// Returns a sorted and deoverlapped list of ranges that are
// the result of subtracting all ranges from ranges_to_subtract.
// ranges_to_subtract must be sorted and deoverlapped.
future<dht::partition_range_vector> subtract_ranges(const schema& schema, const dht::partition_range_vector& ranges, dht::partition_range_vector ranges_to_subtract);
// Returns a token_range vector split based on the given number of most-significant bits
dht::token_range_vector split_token_range_msb(unsigned most_significant_bits);
// Returns the first token included by a partition range.
// May return tokens for which is_minimum() or is_maximum() is true.
dht::token first_token(const dht::partition_range&);
// Returns true iff a given partition range is wholly owned by a single shard.
// If so, returns that shard. Otherwise, return std::nullopt.
// During tablet migration, uses the view on shard ownership for reads.
std::optional<shard_id> is_single_shard(const dht::sharder&, const schema&, const dht::partition_range&);
} // dht
template <> struct fmt::formatter<dht::i_partitioner> : fmt::formatter<string_view> {
template <typename FormatContext>
auto format(const dht::i_partitioner& p, FormatContext& ctx) const {
return fmt::format_to(ctx.out(), "{{partitioner name = {}}}", p.name());
}
};