Debian Codegraph @

libbinlogevents/src/gtids/gtidset.cpp

raw
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 #include "libbinlogevents/include/gtids/gtidset.h"
25 #include <map>
26 #include <string>
27
28 namespace binary_log::gtids {
29
30 std::size_t Gno_interval::count() const {
31 return m_next_gno_after_end - m_start;
32 }
33
34 Gno_interval::Gno_interval(gno_t start, gno_t end)
35 : m_start(start), m_next_gno_after_end(end + 1) {}
36
37 Gno_interval::Gno_interval(const Gno_interval &other)
38 : m_start(other.get_start()), m_next_gno_after_end(other.get_end() + 1) {}
39
40 Gno_interval &Gno_interval::operator=(const Gno_interval &other) {
41 m_start = other.get_start();
42 m_next_gno_after_end = other.get_end() + 1;
43 return *this;
44 }
45
46 bool Gno_interval::operator==(const Gno_interval &other) const {
47 return ((other.get_end() + 1) == m_next_gno_after_end) &&
48 other.get_start() == m_start;
49 }
50
51 bool Gno_interval::operator<(const Gno_interval &other) const {
52 if (m_start < other.get_start()) return true;
53 if (m_start > other.get_start()) return false;
54 if ((m_next_gno_after_end - 1) < other.get_end()) return true;
55
56 return false;
57 }
58
59 bool Gno_interval::intersects(const Gno_interval &other) const {
60 bool other_starts_in_this_interval =
61 (other.get_start() >= m_start &&
62 other.get_start() < m_next_gno_after_end);
63 bool this_starts_in_other_interval =
64 (m_start >= other.get_start() && m_start <= other.get_end());
65
66 return other_starts_in_this_interval || this_starts_in_other_interval;
67 }
68
69 bool Gno_interval::contiguous(const Gno_interval &other) const {
70 if (other.get_start() == m_next_gno_after_end) return true;
71 if (other.get_end() + 1 == m_start) return true;
72 return false;
73 }
74
75 bool Gno_interval::intersects_or_contiguous(const Gno_interval &other) const {
76 return intersects(other) || contiguous(other);
77 }
78
79 gno_t Gno_interval::get_start() const { return m_start; }
80 gno_t Gno_interval::get_end() const { return m_next_gno_after_end - 1; }
81
82 bool Gno_interval::add(const Gno_interval &other) {
83 if (intersects_or_contiguous(other)) {
84 m_next_gno_after_end = std::max(other.get_end() + 1, m_next_gno_after_end);
85 m_start = std::min(other.get_start(), m_start);
86 return false;
87 }
88 return true;
89 }
90
91 std::string Gno_interval::to_string() const {
92 std::stringstream ss;
93 if (m_start == get_end())
94 ss << m_start;
95 else
96 ss << m_start << SEPARATOR_GNO_START_END << get_end();
97 return ss.str();
98 }
99
100 bool Gno_interval::is_valid() const {
101 return m_start < m_next_gno_after_end && m_start > 0;
102 }
103
104 Gtid_set::~Gtid_set() = default;
105
106 Gtid_set &Gtid_set::operator=(const Gtid_set &other) {
107 reset();
108 add(other);
109
110 return *this;
111 }
112
113 Gtid_set::Gtid_set(const Gtid_set &other) { *this = other; }
114
115 bool Gtid_set::operator==(const Gtid_set &other) const {
116 const auto &other_interval_list = other.get_gtid_set();
117 const auto &this_interval_list = m_gtid_set;
118
119 if (other_interval_list.size() != this_interval_list.size()) return false;
120
121 for (auto const &[uuid, other_intervals] : other_interval_list) {
122 auto it = this_interval_list.find(uuid);
123
124 // uuid does not exist in this set
125 if (it == this_interval_list.end()) return false;
126
127 // uuid exists, lets check if the set of intervals match
128 const auto &this_intervals = it->second;
129 if (this_intervals.size() != other_intervals.size()) return false;
130
131 // check each interval for this uuid
132 for (const auto &interval : this_intervals) {
133 if (other_intervals.find(interval) == other_intervals.end()) return false;
134 }
135 }
136
137 return true;
138 }
139
140 bool Gtid_set::Uuid_comparator::operator()(const Uuid &lhs,
141 const Uuid &rhs) const {
142 return memcmp(rhs.bytes, lhs.bytes, Uuid::BYTE_LENGTH) > 0;
143 }
144
145 const Gtid_set::Gno_interval_list &Gtid_set::get_gtid_set() const {
146 return m_gtid_set;
147 }
148
149 bool Gtid_set::do_add(const Uuid &uuid, const Gno_interval &interval) {
150 auto it = m_gtid_set.find(uuid);
151 if (it == m_gtid_set.end()) {
152 std::set<Gno_interval> intervals;
153 intervals.insert(interval);
154 m_gtid_set.insert(std::pair<Uuid, std::set<Gno_interval>>(uuid, intervals));
155 return false;
156 }
157
158 auto &intervals = it->second;
159
160 /*
161 * Iterate over the list of intervals and whenever we find a contiguous
162 * interval remove it, merge the intervals and move on to the next interval.
163 *
164 * Eventually at the end, we will have the interval adjusted to merge.
165 */
166 auto current = interval;
167 for (auto iterator = intervals.begin(); iterator != intervals.end();) {
168 auto &next = *iterator;
169 if (next.intersects_or_contiguous(current)) {
170 current.add(next);
171 /* returns the next element after the one erased */
172 iterator = intervals.erase(iterator);
173 } else {
174 ++iterator;
175 }
176 }
177
178 intervals.insert(current);
179
180 return false;
181 }
182
183 bool Gtid_set::add(const Uuid &uuid, const Gno_interval &interval) {
184 return do_add(uuid, interval);
185 }
186
187 bool Gtid_set::add(const Gtid_set &other) {
188 for (auto const &[uuid, intervals] : other.m_gtid_set) {
189 for (auto &interval : intervals) {
190 if (do_add(uuid, interval)) return true;
191 }
192 }
193
194 return false;
195 }
196
197 bool Gtid_set::add(const Gtid &gtid) {
198 return do_add(gtid.get_uuid(), Gno_interval{gtid.get_gno(), gtid.get_gno()});
199 }
200
201 std::string Gtid_set::to_string() const {
202 if (m_gtid_set.empty()) {
203 return EMPTY_GTID_SET;
204 }
205
206 std::stringstream ss;
207 for (auto &[uuid, intervals] : m_gtid_set) {
208 ss << uuid.to_string() << Gtid::SEPARATOR_UUID_SEQNO;
209 assert(!intervals.empty());
210
211 for (auto &interval : intervals) {
212 ss << interval.to_string() << SEPARATOR_SEQNO_INTERVALS;
213 }
214 ss.seekp(-1, std::ios_base::end);
215 ss << SEPARATOR_UUID_SETS;
216 }
217
218 ss.seekp(-1, std::ios_base::end);
219 return std::string(ss.str(), 0, ss.str().size() - 1);
220 }
221
222 bool Gtid_set::contains(const Gtid &gtid) const {
223 auto it = m_gtid_set.find(gtid.get_uuid());
224 if (it == m_gtid_set.end()) return false;
225
226 for (const auto &interval : it->second) {
227 if (gtid.get_gno() >= interval.get_start() &&
228 gtid.get_gno() <= interval.get_end())
229 return true;
230 }
231 return false;
232 }
233
234 bool Gtid_set::is_empty() const { return m_gtid_set.empty(); }
235
236 std::size_t Gtid_set::count() const {
237 std::size_t count{0};
238
239 for (auto const &[uuid, intervals] : m_gtid_set) {
240 for (auto &interval : intervals) {
241 count += interval.count();
242 }
243 }
244
245 return count;
246 }
247
248 void Gtid_set::reset() { m_gtid_set.clear(); }
249
250 } // namespace binary_log::gtids