MySQL 9.4.0
Source Code Documentation
gtid.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_GTID_H
25#define MYSQL_GTID_GTID_H
26
27#include <set>
28#include <sstream>
29
30#include "mysql/gtid/global.h"
31#include "mysql/gtid/tsid.h"
33
34/// @addtogroup GroupLibsMysqlGtid
35/// @{
36
37namespace mysql::gtid {
38
39/**
40 * @brief Represents a MySQL Global Transaction Identifier.
41 *
42 * This class abstracts the representation of a Global Transaction Identifier.
43 *
44 * It contains two fields, a TSID, composed of UUID and tag, and a sequence
45 * number.
46 */
47class Gtid {
48 public:
49 /// In 'UUID:SEQNO', this is the ':'
50 static constexpr auto separator_gtid{':'};
51
52 protected:
55
56 public:
57 /**
58 * @brief Construct an empty GTID
59 */
60 Gtid() = default;
61
62 /**
63 * @brief Construct a new Gtid object
64 *
65 * @param tsid TSID part of the transaction identifier
66 * @param gno The gno part of the transaction identfier.
67 */
68 Gtid(const Tsid &tsid, gno_t gno);
69
70 /**
71 * @brief Destroy the Gtid object
72 *
73 */
74 virtual ~Gtid();
75
76 /**
77 * @brief Get the sequence number of this transaction identifier.
78 *
79 * @return The sequence number part of this transaction identifier.
80 */
81 virtual gno_t get_gno() const;
82
83 /**
84 * @brief Get the uuid of this transaction identifier.
85 *
86 * @return The uuid part of this transaction identifier.
87 */
88 virtual const Uuid &get_uuid() const;
89
90 /**
91 * @brief Get the tsid of this transaction identifier.
92 *
93 * @return The tsid part of this transaction identifier.
94 */
95 virtual const Tsid &get_tsid() const;
96
97 /**
98 * @brief Get the tag of this transaction identifier.
99 *
100 * @return The tag part of this transaction identifier.
101 */
102 virtual const Tag &get_tag() const;
103
104 /**
105 * @brief Gets a human readable representation of this transaction identifier.
106 *
107 * @return A human readable representation of this transaction identifier.
108 */
109 virtual std::string to_string() const;
110
111 /**
112 * @brief Encodes GTID into a binary format. Supported is only tagged format
113 * of a GTID. Buf must be preallocated with a required number of bytes
114 *
115 * @param buf Buffer to write to
116 *
117 * @return Number of bytes written
118 */
119 virtual std::size_t encode_gtid_tagged(unsigned char *buf) const;
120
121 /**
122 * @brief Decodes GTID from a given buffer. Supported is only tagged format
123 * of a GTID. Buf must contain required number of bytes
124 *
125 * @param buf Buffer to read from
126 * @param buf_len Buffer length in bytes
127 *
128 * @return Number of bytes read from the buffer or 0 in case decoding is not
129 * possible
130 */
131 [[nodiscard]] virtual std::size_t decode_gtid_tagged(const unsigned char *buf,
132 std::size_t buf_len);
133
134 /**
135 * @brief Gets maximum length of encoded GTID in compile time
136 *
137 * @return Maximum number of bytes needed to store GTID
138 */
139 static constexpr std::size_t get_max_encoded_length() {
141 mysql::serialization::Archive_binary::get_max_size<int64_t, 0>();
142 }
143
144 /**
145 * @brief Compares two identifiers and returns whether they match or not.
146 *
147 * @param other The other transaction identifier to compare to this one.
148 * @return true if the identifiers are equal.
149 * @return false otherwise.
150 */
151 virtual bool operator==(const Gtid &other) const;
152
153 /**
154 * @brief Compares two identifiers and returns whether they are different.
155 *
156 * @param other The other transaction identifier to compare to this one.
157 * @return true if the identifiers are different.
158 * @return false otherwise.
159 */
160 virtual bool operator!=(const Gtid &other) const;
161
162 /**
163 * @brief Copy assignment.
164 *
165 * Note that this operator will do a deep copy of the other identifier.
166 *
167 * @param other Copies the uuid and gno of the other identifier.
168 * @return a copy of the other identifier.
169 */
170 virtual Gtid &operator=(const Gtid &other);
171
172 /**
173 * @brief Construct a new Gtid object from another one, by deep copying its
174 * contents.
175 *
176 * @param other the other gtid to construct this gtid from.
177 */
178 Gtid(const Gtid &other);
179};
180
181} // namespace mysql::gtid
182
183/// @}
184
185#endif // MYSQL_GTID_GTID_H
Experimental API header.
Represents a MySQL Global Transaction Identifier.
Definition: gtid.h:47
virtual std::size_t encode_gtid_tagged(unsigned char *buf) const
Encodes GTID into a binary format.
Definition: gtid.cpp:65
static constexpr std::size_t get_max_encoded_length()
Gets maximum length of encoded GTID in compile time.
Definition: gtid.h:139
virtual ~Gtid()
Destroy the Gtid object.
virtual std::string to_string() const
Gets a human readable representation of this transaction identifier.
Definition: gtid.cpp:45
virtual bool operator==(const Gtid &other) const
Compares two identifiers and returns whether they match or not.
Definition: gtid.cpp:59
static constexpr auto separator_gtid
In 'UUID:SEQNO', this is the ':'.
Definition: gtid.h:50
virtual Gtid & operator=(const Gtid &other)
Copy assignment.
Definition: gtid.cpp:51
Tsid m_tsid
Definition: gtid.h:53
virtual const Tsid & get_tsid() const
Get the tsid of this transaction identifier.
Definition: gtid.cpp:41
Gtid()=default
Construct an empty GTID.
virtual gno_t get_gno() const
Get the sequence number of this transaction identifier.
Definition: gtid.cpp:38
virtual const Uuid & get_uuid() const
Get the uuid of this transaction identifier.
Definition: gtid.cpp:39
gno_t m_gno
Definition: gtid.h:54
virtual std::size_t decode_gtid_tagged(const unsigned char *buf, std::size_t buf_len)
Decodes GTID from a given buffer.
Definition: gtid.cpp:73
virtual bool operator!=(const Gtid &other) const
Compares two identifiers and returns whether they are different.
Definition: gtid.cpp:63
virtual const Tag & get_tag() const
Get the tag of this transaction identifier.
Definition: gtid.cpp:43
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
static constexpr std::size_t get_max_encoded_length()
Obtains maximum length of encoded TSID (compile time)
Definition: tsid.h:146
Definition: buf0block_hint.cc:30
Definition: global.h:35
std::int64_t gno_t
Definition: global.h:37
Uuid is a trivial and of standard layout The structure contains the following components.
Definition: uuid.h:64