MySQL 9.6.0
Source Code Documentation
gtidset.h
Go to the documentation of this file.
1/* Copyright (c) 2021, 2025, 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 MYSQL_GTID_GTIDSET_H
25#define MYSQL_GTID_GTIDSET_H
26
27#include "mysql/utils/deprecate_header.h" // DEPRECATE_HEADER
28
29#ifndef NO_DEPRECATION_WARNING_FOR_HEADER_GTIDSET_H
30DEPRECATE_HEADER(libbinlogevents_include_gtids_gtidset)
31#endif
32
33#include <cstddef>
34#include <map>
35#include <set>
36#include <sstream>
37
38#include "mysql/gtid/global.h"
39#include "mysql/gtid/gtid.h"
40#include "mysql/gtid/tag.h"
41#include "mysql/gtid/tsid.h"
42
43/// @addtogroup GroupLibsMysqlGtid
44/// @{
45
46namespace mysql::gtid {
47
48/**
49 * @brief This class represents a range of transaction identifiers.
50 *
51 * A transaction identifier is composed of two parts, the UUID and the sequence
52 * number.
53 *
54 * There can be multiple transaction identifiers for a given UUID. When their
55 * sequence number are contiguous they can be represented as an interval. This
56 * is the class that represents on of those intervals.
57 *
58 */
60 public:
61 /// In 'UUID:GNO-GNO', this is the '-'
62 static const inline std::string SEPARATOR_GNO_START_END{"-"};
63
64 private:
67
68 public:
69 virtual ~Gno_interval() = default;
70
71 /**
72 * @brief Copy assignment.
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 other interval to copy.
78 * @return Gno_interval& a reference to the copied interval.
79 */
80 Gno_interval &operator=(const Gno_interval &other);
81
82 /**
83 * @brief Construct a new Gno_interval object from the other one provided.
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.
87 *
88 * @param other The object to copy.
89 */
90 Gno_interval(const Gno_interval &other);
91
92 /**
93 * @brief Compares this interval with another one. Returns true if there is a
94 * match.
95 *
96 * @note This operator does not perform any check about the other interval
97 * being valid or not. It is up to the caller to verify that if needed, before
98 * calling this member function.
99 *
100 * @param other The other interval to compare this one with.
101 * @return true If both intervals match.
102 * @return false If intervals do not match.
103 */
104 virtual bool operator==(const Gno_interval &other) const;
105
106 /**
107 * @brief Construct a new Gno_interval object.
108 *
109 * @param start The start of the interval (inclusive).
110 * @param end The end of the interval (inclusive).
111 */
113
114 /**
115 * @brief Establishes a total order between two intervals.
116 *
117 * Total order is determined using the start and end values of
118 * the interval.
119 *
120 * An interval A comes before interval B if its start value is smaller
121 * than the start value of B.
122 *
123 * If B's start value is smaller than A's start value then B comes
124 * before A.
125 *
126 * If A and B have the same start value, the the end of the intervals
127 * are checked and if A has a lower interval end than B, then A is
128 * considered to come before B in that case.
129 *
130 * @note This operator does not perform any check about the other interval
131 * being valid or not. It is up to the caller to verify that if needed, before
132 * calling this member function.
133 *
134 * @param other the other interval.
135 * @return true if this is smaller than the other
136 * @return false if this is equal or greater than the other.
137 */
138 virtual bool operator<(const Gno_interval &other) const;
139
140 /**
141 * @brief This checks whether this interval intersects with the other.
142 *
143 * @note This operator does not perform any check about the other interval
144 * being valid or not. It is up to the caller to verify that if needed, before
145 * calling this member function.
146 *
147 * @param other The other interval to check against this one.
148 * @return true if the intervals intersect.
149 * @return false if it does not intersect.
150 */
151 virtual bool intersects(const Gno_interval &other) const;
152
153 /**
154 * @brief Checks if this interval is contiguous with the other one.
155 *
156 * Two intervals are contiguous if they do not intersect but there
157 * are no gaps between them. No gaps means that the upper limit of
158 * interval A is the value immediately preceding the lower limit
159 * of interval B, under numeric natural ordering.
160 *
161 * @note This operator does not perform any check about the other interval
162 * being valid or not. It is up to the caller to verify that if needed, before
163 * calling this member function.
164 *
165 * @param other the interval to check against.
166 * @return true if the intervals are contiguous.
167 * @return false otherwise.
168 */
169 virtual bool contiguous(const Gno_interval &other) const;
170
171 /**
172 * @brief Checks if the other interval intersects or is contiguous with this
173 * one.
174 *
175 * @param other The interval to check against this one.
176 * @return true if the other interval intersects or is contiguous with this
177 * one.
178 * @return false otherwise.
179 */
180 virtual bool intersects_or_contiguous(const Gno_interval &other) const;
181
182 /**
183 * @brief Adds the other interval to this one.
184 *
185 * For this operation to complete successfully, the intervals must
186 * intersect or be contiguous. Otherwise, the operation will fail.
187 *
188 * @note This operator does not perform any check about the other interval
189 * being valid or not. It is up to the caller to verify that if needed, before
190 * calling this member function.
191 *
192 * @param other The interval to add to this one.
193 * @return true if the adding was not successful.
194 * @return false if the adding was successful.
195 */
196 virtual bool add(const Gno_interval &other);
197
198 /**
199 * @brief Gets the first sequence number in the interval.
200 *
201 * @return the first sequence number of the interval.
202 */
203 virtual gno_t get_start() const;
204
205 /**
206 * @brief Gets the last sequence number in the interval.
207 *
208 * @return the last sequence number in the interval.
209 */
210 virtual gno_t get_end() const;
211
212 /**
213 * @brief Number of entries in this interval.
214 *
215 * @return the size of the interval.
216 */
217 virtual std::size_t count() const;
218
219 /**
220 * @brief Gets a human readable representation of this identifier.
221 *
222 * @return A human readable representation of this identifier.
223 */
224 virtual std::string to_string() const;
225
226 /**
227 * @brief Checks whether this interval is valid or not.
228 *
229 * An interval is invalid if the start value is smaller than 0 or
230 * if the start is larger than the end of the interval.
231 *
232 * @return true if the interval is valid, false otherwise.
233 */
234 virtual bool is_valid() const;
235};
236
237/**
238 * @brief This class represents a set of transaction identifiers.
239 *
240 * A set of transaction identifiers contains zero or more entries. When there
241 * are multiple entries, there can multiple intervals as well. Different
242 * intervals may share the same UUID part or not. This class abstracts that
243 * in-memory representation.
244 *
245 */
246class Gtid_set {
247 public:
248 static const inline std::string empty_gtid_set_str{""};
249 /// In 'UUID:INTERVAL:INTERVAL', this is the second ':'
250 static const inline std::string separator_interval{":"};
251 /// In 'SID:GNO,SID:GNO', this is the ','
252 static const inline std::string separator_uuid_set{","};
255
256 protected:
257 [[nodiscard]] virtual bool do_add(const Tsid &tsid,
258 const Gno_interval &interval);
259 [[nodiscard]] virtual bool do_add(const Uuid &uuid, const Tag &tag,
260 const Gno_interval &interval);
261
262 public:
263 /**
264 * @brief Tsid_interval_map is a 2-level map between tsids and an ordered
265 * set of Gno_intervals.
266 * Uuid is mapped into GTID tags, each pair of Uuid and Tag (TSID) is mapped
267 * into ordered set of gno intervals
268 */
269 using Interval_set = std::set<Gno_interval>;
270 using Tag_interval_map = std::map<Tag, Interval_set>;
271 using Tsid_interval_map = std::map<Uuid, Tag_interval_map>;
272
273 Gtid_set() = default;
274 virtual ~Gtid_set();
275
276 Gtid_set(const Gtid_set &other) = delete;
277
278 /**
279 * @brief Copy assignment.
280 *
281 * @note This operator does not check whether the parameters are valid
282 * or not. The caller should perform such check before calling this member
283 * function.
284 *
285 * @param other the Gtid_set to be copied over to this one.
286 * @return Gtid_set& a reference to the copied Gtid_set.
287 */
288 Gtid_set &operator=(const Gtid_set &other);
289
290 /**
291 * @brief Compares this set with another one.
292 *
293 * @param other The other set to compare this one with.
294 * @return true If both sets match.
295 * @return false If sets do not match.
296 */
297 virtual bool operator==(const Gtid_set &other) const;
298
299 /**
300 * @brief Iterates through recorded TSIDs and returns
301 * format of the Gtid_set
302 *
303 * @return Format of this GTID set
304 * @see Gtid_format
305 */
306 virtual Gtid_format get_gtid_set_format() const;
307
308 /**
309 * @brief Adds a new interval indexed by the given uuid.
310 *
311 * @note This member function does not check whether the parameters are valid
312 * or not. The caller should perform such check before calling this member
313 * function.
314 *
315 * @return true if the there was an error adding the interval, false
316 * otherwise.
317 */
318 [[nodiscard]] virtual bool add(const Tsid &tsid,
319 const Gno_interval &interval);
320
321 /**
322 * @brief Gets a copy of the internal set.
323 *
324 * @return an internal copy of the given set.
325 */
326 virtual const Tsid_interval_map &get_gtid_set() const;
327
328 /**
329 * @brief Add a set of identifiers to this one.
330 *
331 * @note This member function does not check whether the parameters are valid
332 * or not. The caller should perform such check before calling this member
333 * function.
334 *
335 * @param other the set to add to this one.
336 * @return true if there was a failure adding the gtids.
337 * @return false otherwise.
338 */
339 virtual bool add(const Gtid_set &other);
340
341 /**
342 * @brief Get the num TSIDs held in the GTID set
343 *
344 * @return std::size_t Number of TSIDs
345 */
346 virtual std::size_t get_num_tsids() const;
347
348 /**
349 * @brief Adds the given identifier to this set.
350 *
351 * @note This member function does not check whether the parameters are valid
352 * or not. The caller should perform such check before calling this member
353 * function.
354 *
355 * @param gtid the identifier to add.
356 * @return true if it failed while adding the identifier.
357 * @return false otherwise.
358 */
359 virtual bool add(const Gtid &gtid);
360
361 /**
362 * @brief Checks whether this set contains the given identifier.
363 *
364 * @note This member function does not check whether the parameters are valid
365 * or not. The caller should perform such check before calling this member
366 * function.
367 *
368 * @param gtid the gtid to check whehther it exists in this set or not.
369 * @return true if the identifier exists in this set.
370 * @return false if the identifier does not exist in this set.
371 */
372 virtual bool contains(const Gtid &gtid) const;
373
374 /**
375 * @brief A human readable representation of this set.
376 *
377 * @return a human readable representation of this set.
378 */
379 virtual std::string to_string() const;
380
381 /**
382 * @brief Resets this set, making it empty.
383 *
384 */
385 virtual void reset();
386
387 /**
388 * @brief Returns true if this is an empty set.
389 *
390 * @return true
391 * @return false
392 */
393 virtual bool is_empty() const;
394
395 /**
396 * @brief Gets the number of entries in this set.
397 *
398 * @return the cardinality of this set.
399 */
400 virtual std::size_t count() const;
401
402 protected:
403 /**
404 * @brief An ordered map of entries mapping Uuid to a list of intervals.
405 */
407};
408
409} // namespace mysql::gtid
410
411/// @}
412
413#endif // MYSQL_GTID_GTIDSET_H
This class represents a range of transaction identifiers.
Definition: gtidset.h:59
virtual bool intersects_or_contiguous(const Gno_interval &other) const
Checks if the other interval intersects or is contiguous with this one.
Definition: gtidset.cpp:79
virtual bool operator==(const Gno_interval &other) const
Compares this interval with another one.
Definition: gtidset.cpp:50
static const std::string SEPARATOR_GNO_START_END
In 'UUID:GNO-GNO', this is the '-'.
Definition: gtidset.h:62
gno_t m_start
Definition: gtidset.h:65
virtual ~Gno_interval()=default
Gno_interval(const Gno_interval &other)
Construct a new Gno_interval object from the other one provided.
Definition: gtidset.cpp:41
virtual gno_t get_start() const
Gets the first sequence number in the interval.
Definition: gtidset.cpp:83
virtual std::string to_string() const
Gets a human readable representation of this identifier.
Definition: gtidset.cpp:95
gno_t m_next_gno_after_end
Definition: gtidset.h:66
Gno_interval & operator=(const Gno_interval &other)
Copy assignment.
Definition: gtidset.cpp:44
virtual bool intersects(const Gno_interval &other) const
This checks whether this interval intersects with the other.
Definition: gtidset.cpp:63
virtual bool add(const Gno_interval &other)
Adds the other interval to this one.
Definition: gtidset.cpp:86
virtual gno_t get_end() const
Gets the last sequence number in the interval.
Definition: gtidset.cpp:84
virtual std::size_t count() const
Number of entries in this interval.
Definition: gtidset.cpp:34
virtual bool operator<(const Gno_interval &other) const
Establishes a total order between two intervals.
Definition: gtidset.cpp:55
virtual bool contiguous(const Gno_interval &other) const
Checks if this interval is contiguous with the other one.
Definition: gtidset.cpp:73
virtual bool is_valid() const
Checks whether this interval is valid or not.
Definition: gtidset.cpp:104
This class represents a set of transaction identifiers.
Definition: gtidset.h:246
virtual std::size_t count() const
Gets the number of entries in this set.
Definition: gtidset.cpp:281
virtual std::string to_string() const
A human readable representation of this set.
Definition: gtidset.cpp:229
Gtid_set(const Gtid_set &other)=delete
static const std::string separator_uuid_set
In 'SID:GNO,SID:GNO', this is the ','.
Definition: gtidset.h:252
virtual bool operator==(const Gtid_set &other) const
Compares this set with another one.
Definition: gtidset.cpp:117
virtual bool do_add(const Tsid &tsid, const Gno_interval &interval)
Definition: gtidset.cpp:163
std::map< Uuid, Tag_interval_map > Tsid_interval_map
Definition: gtidset.h:271
std::set< Gno_interval > Interval_set
Tsid_interval_map is a 2-level map between tsids and an ordered set of Gno_intervals.
Definition: gtidset.h:269
static const std::string separator_interval
In 'UUID:INTERVAL:INTERVAL', this is the second ':'.
Definition: gtidset.h:250
virtual Gtid_format get_gtid_set_format() const
Iterates through recorded TSIDs and returns format of the Gtid_set.
Definition: gtidset.cpp:297
static const std::string empty_gtid_set_str
Definition: gtidset.h:248
virtual bool add(const Tsid &tsid, const Gno_interval &interval)
Adds a new interval indexed by the given uuid.
Definition: gtidset.cpp:209
virtual void reset()
Resets this set, making it empty.
Definition: gtidset.cpp:295
virtual const Tsid_interval_map & get_gtid_set() const
Gets a copy of the internal set.
Definition: gtidset.cpp:159
virtual bool contains(const Gtid &gtid) const
Checks whether this set contains the given identifier.
Definition: gtidset.cpp:256
virtual bool is_empty() const
Returns true if this is an empty set.
Definition: gtidset.cpp:279
std::map< Tag, Interval_set > Tag_interval_map
Definition: gtidset.h:270
Tsid_interval_map m_gtid_set
An ordered map of entries mapping Uuid to a list of intervals.
Definition: gtidset.h:406
Gtid_set & operator=(const Gtid_set &other)
Copy assignment.
Definition: gtidset.cpp:110
virtual std::size_t get_num_tsids() const
Get the num TSIDs held in the GTID set.
Definition: gtidset.cpp:271
Represents a MySQL Global Transaction Identifier.
Definition: gtid.h:47
Representation of the GTID tag.
Definition: tag.h:49
Represents Transaction Source Identifier which is composed of source UUID and transaction tag.
Definition: tsid.h:44
Experimental API header Contains macro that can be used to deprecate a header.
#define DEPRECATE_HEADER(header_name)
Definition: deprecate_header.h:34
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:180
static int interval
Definition: mysqladmin.cc:72
Definition: global.h:35
Gtid_format
Gtid binary format indicator.
Definition: gtid_format.h:39
std::int64_t gno_t
Definition: global.h:37
mysql::gtid::Tsid Tsid
Definition: rpl_gtid_state.cc:56
Uuid is a trivial and of standard layout The structure contains the following components.
Definition: uuid.h:64