| 1 |
/* Copyright (c) 2021, 2026, Oracle and/or its affiliates. |
| 2 |
|
| 3 |
This program is free software; you can redistribute it and/or modify |
| 4 |
it under the terms of the GNU General Public License, version 2.0, |
| 5 |
as published by the Free Software Foundation. |
| 6 |
|
| 7 |
This program is designed to work with certain software (including |
| 8 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 9 |
as designated in a particular file or component or in included license |
| 10 |
documentation. The authors of MySQL hereby grant you an additional |
| 11 |
permission to link the program and your derivative works with the |
| 12 |
separately licensed software that they have either included with |
| 13 |
the program or referenced in the documentation. |
| 14 |
|
| 15 |
This program is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU General Public License, version 2.0, for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with this program; if not, write to the Free Software |
| 22 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 |
|
| 24 |
#ifndef BINARY_LOG_GTIDS_GTID_SET_INCLUDED |
| 25 |
#define BINARY_LOG_GTIDS_GTID_SET_INCLUDED |
| 26 |
|
| 27 |
#include <cstddef> |
| 28 |
#include <map> |
| 29 |
#include <set> |
| 30 |
#include <sstream> |
| 31 |
|
| 32 |
#include "libbinlogevents/include/gtids/global.h" |
| 33 |
#include "libbinlogevents/include/gtids/gtid.h" |
| 34 |
|
| 35 |
namespace binary_log::gtids { |
| 36 |
|
| 37 |
/** |
| 38 |
* @brief This class represents a range of transaction identifiers. |
| 39 |
* |
| 40 |
* A transaction identifier is composed of two parts, the UUID and the sequence |
| 41 |
* number. |
| 42 |
* |
| 43 |
* There can be multiple transaction identifiers for a given UUID. When their |
| 44 |
* sequence number are contiguous they can be represented as an interval. This |
| 45 |
* is the class that represents on of those intervals. |
| 46 |
* |
| 47 |
*/ |
| 48 |
class Gno_interval { |
| 49 |
public: |
| 50 |
/// In 'UUID:GNO-GNO', this is the '-' |
| 51 |
static const inline std::string SEPARATOR_GNO_START_END{"-"}; |
| 52 |
|
| 53 |
private: |
| 54 |
gno_t m_start{0}; |
| 55 |
gno_t m_next_gno_after_end{0}; |
| 56 |
|
| 57 |
public: |
| 58 |
virtual ~Gno_interval() = default; |
| 59 |
|
| 60 |
/** |
| 61 |
* @brief Copy assignment. |
| 62 |
* |
| 63 |
* @note This operator does not perform any check about the other interval |
| 64 |
* being valid or not. It is up to the caller to verify that if needed. |
| 65 |
* |
| 66 |
* @param other The other interval to copy. |
| 67 |
* @return Gno_interval& a reference to the copied interval. |
| 68 |
*/ |
| 69 |
Gno_interval &operator=(const Gno_interval &other); |
| 70 |
|
| 71 |
/** |
| 72 |
* @brief Construct a new Gno_interval object from the other one provided. |
| 73 |
* |
| 74 |
* @note This operator does not perform any check about the other interval |
| 75 |
* being valid or not. It is up to the caller to verify that if needed. |
| 76 |
* |
| 77 |
* @param other The object to copy. |
| 78 |
*/ |
| 79 |
Gno_interval(const Gno_interval &other); |
| 80 |
|
| 81 |
/** |
| 82 |
* @brief Compares this interval with another one. Returns true if there is a |
| 83 |
* match. |
| 84 |
* |
| 85 |
* @note This operator does not perform any check about the other interval |
| 86 |
* being valid or not. It is up to the caller to verify that if needed, before |
| 87 |
* calling this member function. |
| 88 |
* |
| 89 |
* @param other The other interval to compare this one with. |
| 90 |
* @return true If both intervals match. |
| 91 |
* @return false If intervals do not match. |
| 92 |
*/ |
| 93 |
virtual bool operator==(const Gno_interval &other) const; |
| 94 |
|
| 95 |
/** |
| 96 |
* @brief Construct a new Gno_interval object. |
| 97 |
* |
| 98 |
* @param start The start of the interval (inclusive). |
| 99 |
* @param end The end of the interval (inclusive). |
| 100 |
*/ |
| 101 |
Gno_interval(gno_t start, gno_t end); |
| 102 |
|
| 103 |
/** |
| 104 |
* @brief Establishes a total order between two intervals. |
| 105 |
* |
| 106 |
* Total order is determined using the start and end values of |
| 107 |
* the interval. |
| 108 |
* |
| 109 |
* An interval A comes before interval B if its start value is smaller |
| 110 |
* than the start value of B. |
| 111 |
* |
| 112 |
* If B's start value is smaller than A's start value then B comes |
| 113 |
* before A. |
| 114 |
* |
| 115 |
* If A and B have the same start value, the the end of the intervals |
| 116 |
* are checked and if A has a lower interval end than B, then A is |
| 117 |
* considered to come before B in that case. |
| 118 |
* |
| 119 |
* @note This operator does not perform any check about the other interval |
| 120 |
* being valid or not. It is up to the caller to verify that if needed, before |
| 121 |
* calling this member function. |
| 122 |
* |
| 123 |
* @param other the other interval. |
| 124 |
* @return true if this is smaller than the other |
| 125 |
* @return false if this is equal or greater than the other. |
| 126 |
*/ |
| 127 |
virtual bool operator<(const Gno_interval &other) const; |
| 128 |
|
| 129 |
/** |
| 130 |
* @brief This checks whether this interval intersects with the other. |
| 131 |
* |
| 132 |
* @note This operator does not perform any check about the other interval |
| 133 |
* being valid or not. It is up to the caller to verify that if needed, before |
| 134 |
* calling this member function. |
| 135 |
* |
| 136 |
* @param other The other interval to check against this one. |
| 137 |
* @return true if the intervals intersect. |
| 138 |
* @return false if it does not intersect. |
| 139 |
*/ |
| 140 |
virtual bool intersects(const Gno_interval &other) const; |
| 141 |
|
| 142 |
/** |
| 143 |
* @brief Checks if this interval is contiguous with the other one. |
| 144 |
* |
| 145 |
* Two intervals are contiguous if they do not intersect but there |
| 146 |
* are no gaps between them. No gaps means that the upper limit of |
| 147 |
* interval A is the value immediately preceding the lower limit |
| 148 |
* of interval B, under numeric natural ordering. |
| 149 |
* |
| 150 |
* @note This operator does not perform any check about the other interval |
| 151 |
* being valid or not. It is up to the caller to verify that if needed, before |
| 152 |
* calling this member function. |
| 153 |
* |
| 154 |
* @param other the interval to check against. |
| 155 |
* @return true if the intervals are contiguous. |
| 156 |
* @return false otherwise. |
| 157 |
*/ |
| 158 |
virtual bool contiguous(const Gno_interval &other) const; |
| 159 |
|
| 160 |
/** |
| 161 |
* @brief Checks if the other interval intersects or is contiguous with this |
| 162 |
* one. |
| 163 |
* |
| 164 |
* @param other The interval to check against this one. |
| 165 |
* @return true if the other interval intersects or is contiguous with this |
| 166 |
* one. |
| 167 |
* @return false otherwise. |
| 168 |
*/ |
| 169 |
virtual bool intersects_or_contiguous(const Gno_interval &other) const; |
| 170 |
|
| 171 |
/** |
| 172 |
* @brief Adds the other interval to this one. |
| 173 |
* |
| 174 |
* For this operation to complete successfully, the intervals must |
| 175 |
* intersect or be contiguous. Otherwise, the operation will fail. |
| 176 |
* |
| 177 |
* @note This operator does not perform any check about the other interval |
| 178 |
* being valid or not. It is up to the caller to verify that if needed, before |
| 179 |
* calling this member function. |
| 180 |
* |
| 181 |
* @param other The interval to add to this one. |
| 182 |
* @return true if the adding was not successful. |
| 183 |
* @return false if the adding was successful. |
| 184 |
*/ |
| 185 |
virtual bool add(const Gno_interval &other); |
| 186 |
|
| 187 |
/** |
| 188 |
* @brief Gets the first sequence number in the interval. |
| 189 |
* |
| 190 |
* @return the first sequence number of the interval. |
| 191 |
*/ |
| 192 |
virtual gno_t get_start() const; |
| 193 |
|
| 194 |
/** |
| 195 |
* @brief Gets the last sequence number in the interval. |
| 196 |
* |
| 197 |
* @return the last sequence number in the interval. |
| 198 |
*/ |
| 199 |
virtual gno_t get_end() const; |
| 200 |
|
| 201 |
/** |
| 202 |
* @brief Number of entries in this interval. |
| 203 |
* |
| 204 |
* @return the size of the interval. |
| 205 |
*/ |
| 206 |
virtual std::size_t count() const; |
| 207 |
|
| 208 |
/** |
| 209 |
* @brief Gets a human readable representation of this identifier. |
| 210 |
* |
| 211 |
* @return A human readable representation of this identifier. |
| 212 |
*/ |
| 213 |
virtual std::string to_string() const; |
| 214 |
|
| 215 |
/** |
| 216 |
* @brief Checks whether this interval is valid or not. |
| 217 |
* |
| 218 |
* An interval is invalid if the start value is smaller than 0 or |
| 219 |
* if the start is larger than the end of the interval. |
| 220 |
* |
| 221 |
* @return true if the interval is valid, false otherwise. |
| 222 |
*/ |
| 223 |
virtual bool is_valid() const; |
| 224 |
}; |
| 225 |
|
| 226 |
/** |
| 227 |
* @brief This class represents a set of transaction identifiers. |
| 228 |
* |
| 229 |
* A set of transaction identifiers contains zero or more entries. When there |
| 230 |
* are multiple entries, there can multiple intervals as well. Different |
| 231 |
* intervals may share the same UUID part or not. This class abstracts that |
| 232 |
* in-memory representation. |
| 233 |
* |
| 234 |
*/ |
| 235 |
class Gtid_set { |
| 236 |
public: |
| 237 |
static const inline std::string EMPTY_GTID_SET{""}; |
| 238 |
/// In 'UUID:INTERVAL:INTERVAL', this is the second ':' |
| 239 |
static const inline std::string SEPARATOR_SEQNO_INTERVALS{":"}; |
| 240 |
/// In 'SID:GNO,SID:GNO', this is the ',' |
| 241 |
static const inline std::string SEPARATOR_UUID_SETS{","}; |
| 242 |
|
| 243 |
struct Uuid_comparator { |
| 244 |
bool operator()(const Uuid &lhs, const Uuid &rhs) const; |
| 245 |
}; |
| 246 |
|
| 247 |
protected: |
| 248 |
virtual bool do_add(const Uuid &uuid, const Gno_interval &interval); |
| 249 |
|
| 250 |
public: |
| 251 |
/** |
| 252 |
* @brief Gno_interval_list is a map between uuids and an ordered set of |
| 253 |
* Gno_intervals. |
| 254 |
*/ |
| 255 |
typedef std::map<Uuid, std::set<Gno_interval>, Uuid_comparator> |
| 256 |
Gno_interval_list; |
| 257 |
|
| 258 |
Gtid_set() = default; |
| 259 |
virtual ~Gtid_set(); |
| 260 |
/** |
| 261 |
* @brief Construct a new Gtid_set object from the other one provided. |
| 262 |
* |
| 263 |
* @note This operator does not check whether the parameters are valid |
| 264 |
* or not. The caller should perform such check before calling this member |
| 265 |
* function. |
| 266 |
* |
| 267 |
* @param other Gtid_set to be copied |
| 268 |
*/ |
| 269 |
Gtid_set(const Gtid_set &other); |
| 270 |
|
| 271 |
/** |
| 272 |
* @brief Copy assignment. |
| 273 |
* |
| 274 |
* @note This operator does not check whether the parameters are valid |
| 275 |
* or not. The caller should perform such check before calling this member |
| 276 |
* function. |
| 277 |
* |
| 278 |
* @param other the Gtid_set to be copied over to this one. |
| 279 |
* @return Gtid_set& a reference to the copied Gtid_set. |
| 280 |
*/ |
| 281 |
Gtid_set &operator=(const Gtid_set &other); |
| 282 |
|
| 283 |
/** |
| 284 |
* @brief Compares this set with another one. |
| 285 |
* |
| 286 |
* @param other The other set to compare this one with. |
| 287 |
* @return true If both sets match. |
| 288 |
* @return false If sets do not match. |
| 289 |
*/ |
| 290 |
virtual bool operator==(const Gtid_set &other) const; |
| 291 |
|
| 292 |
/** |
| 293 |
* @brief Adds a new interval indexed by the given uuid. |
| 294 |
* |
| 295 |
* @note This member function does not check whether the parameters are valid |
| 296 |
* or not. The caller should perform such check before calling this member |
| 297 |
* function. |
| 298 |
* |
| 299 |
* @return true if the there was an error adding the interval, false |
| 300 |
* otherwise. |
| 301 |
*/ |
| 302 |
virtual bool add(const Uuid &uuid, const Gno_interval &interval); |
| 303 |
|
| 304 |
/** |
| 305 |
* @brief Gets a copy of the internal set. |
| 306 |
* |
| 307 |
* @return an internal copy of the given set. |
| 308 |
*/ |
| 309 |
virtual const Gno_interval_list &get_gtid_set() const; |
| 310 |
|
| 311 |
/** |
| 312 |
* @brief Add a set of identifiers to this one. |
| 313 |
* |
| 314 |
* @note This member function does not check whether the parameters are valid |
| 315 |
* or not. The caller should perform such check before calling this member |
| 316 |
* function. |
| 317 |
* |
| 318 |
* @param other the set to add to this one. |
| 319 |
* @return true if there was a failure adding the gtids. |
| 320 |
* @return false otherwise. |
| 321 |
*/ |
| 322 |
virtual bool add(const Gtid_set &other); |
| 323 |
|
| 324 |
/** |
| 325 |
* @brief Adds the given identifier to this set. |
| 326 |
* |
| 327 |
* @note This member function does not check whether the parameters are valid |
| 328 |
* or not. The caller should perform such check before calling this member |
| 329 |
* function. |
| 330 |
* |
| 331 |
* @param gtid the identifier to add. |
| 332 |
* @return true if it failed while adding the identifier. |
| 333 |
* @return false otherwise. |
| 334 |
*/ |
| 335 |
virtual bool add(const Gtid >id); |
| 336 |
|
| 337 |
/** |
| 338 |
* @brief Checks whether this set contains the given identifier. |
| 339 |
* |
| 340 |
* @note This member function does not check whether the parameters are valid |
| 341 |
* or not. The caller should perform such check before calling this member |
| 342 |
* function. |
| 343 |
* |
| 344 |
* @param gtid the gtid to check whehther it exists in this set or not. |
| 345 |
* @return true if the identifier exists in this set. |
| 346 |
* @return false if the identifier does not exist in this set. |
| 347 |
*/ |
| 348 |
virtual bool contains(const Gtid >id) const; |
| 349 |
|
| 350 |
/** |
| 351 |
* @brief A human readable representation of this set. |
| 352 |
* |
| 353 |
* @return a human readable representation of this set. |
| 354 |
*/ |
| 355 |
virtual std::string to_string() const; |
| 356 |
|
| 357 |
/** |
| 358 |
* @brief Resets this set, making it empty. |
| 359 |
* |
| 360 |
*/ |
| 361 |
virtual void reset(); |
| 362 |
|
| 363 |
/** |
| 364 |
* @brief Returns true if this is an empty set. |
| 365 |
* |
| 366 |
* @return true |
| 367 |
* @return false |
| 368 |
*/ |
| 369 |
virtual bool is_empty() const; |
| 370 |
|
| 371 |
/** |
| 372 |
* @brief Gets the number of entries in this set. |
| 373 |
* |
| 374 |
* @return the cardinality of this set. |
| 375 |
*/ |
| 376 |
virtual std::size_t count() const; |
| 377 |
|
| 378 |
protected: |
| 379 |
/** |
| 380 |
* @brief An ordered map of entries mapping Uuid to a list of intervals. |
| 381 |
* |
| 382 |
* The order is established using the Uuid_comparator. |
| 383 |
*/ |
| 384 |
Gno_interval_list m_gtid_set{}; |
| 385 |
}; |
| 386 |
|
| 387 |
} // namespace binary_log::gtids |
| 388 |
|
| 389 |
#endif |