Debian Codegraph @

storage/innobase/log/log0files_dict.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 2019, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /**************************************************/ /**
29 @file log/log0files_dict.cc
30
31 Redo log - in-memory dictionary of log files (their meta data).
32
33 *******************************************************/
34
35 /* std::find_if, std::count_if */
36 #include <algorithm>
37
38 /* std::distance */
39 #include <iterator>
40
41 /* std::accumulate */
42 #include <numeric>
43
44 #include "log0files_dict.h"
45
46 /**************************************************/ /**
47
48 @name Log_files_dict methods
49
50 *******************************************************/
51
52 /** @{ */
53
54 Log_files_dict::Log_files_dict(Log_files_context &files_ctx)
55 : m_files_ctx{files_ctx} {}
56
57 Log_files_dict::Log_files_dict(Log_files_dict &&other)
58 : m_files_ctx{other.m_files_ctx},
59 m_files_by_id{std::move(other.m_files_by_id)} {}
60
61 Log_files_dict &Log_files_dict::operator=(Log_files_dict &&other) {
62 m_files_by_id = std::move(other.m_files_by_id);
63 ut_a(&m_files_ctx == &other.m_files_ctx);
64 return *this;
65 }
66
67 const Log_files_context &Log_files_dict::ctx() const { return m_files_ctx; }
68
69 void Log_files_dict::clear() { m_files_by_id.clear(); }
70
71 void Log_files_dict::erase(Log_file_id file_id) {
72 auto it = m_files_by_id.find(file_id);
73 ut_a(it != m_files_by_id.end());
74
75 m_files_by_id.erase(it);
76
77 ut_a(m_files_by_id.find(file_id) == m_files_by_id.end());
78 }
79
80 Log_files_dict::Const_iterator Log_files_dict::find(lsn_t lsn) const {
81 /* The performance of Log_files_dict::find is not important.
82 If it became important, one could add another dictionary to
83 this class: m_files_by_start_lsn. */
84 const Const_iterator it =
85 std::find_if(begin(), end(),
86 [lsn](const Log_file &file) { return file.contains(lsn); });
87
88 if (it != end()) {
89 DBUG_PRINT("ib_log",
90 ("found file for lsn=" LSN_PF ": file_id=%zu "
91 "[" LSN_PF "," LSN_PF "), ",
92 lsn, size_t{it->m_id}, it->m_start_lsn, it->m_end_lsn));
93 } else {
94 DBUG_PRINT("ib_log", ("found no file for lsn=" LSN_PF, lsn));
95 }
96 return it;
97 }
98
99 Log_files_dict::Const_iterator Log_files_dict::file(Log_file_id file_id) const {
100 return Const_iterator{m_files_by_id.find(file_id)};
101 }
102
103 void Log_files_dict::add(Log_file_id file_id, os_offset_t size_in_bytes,
104 lsn_t start_lsn, bool full,
105 Encryption_metadata &encryption_metadata) {
106 add(file_id, size_in_bytes, start_lsn, full, false, encryption_metadata);
107 }
108
109 void Log_files_dict::add(Log_file_id file_id, os_offset_t size_in_bytes,
110 lsn_t start_lsn, bool full, bool consumed,
111 Encryption_metadata &encryption_metadata) {
112 ut_a(start_lsn == 0 || LOG_START_LSN <= start_lsn);
113 ut_a(start_lsn < LSN_MAX);
114 ut_a(start_lsn % OS_FILE_LOG_BLOCK_SIZE == 0);
115 ut_a(size_in_bytes > 0);
116 ut_a(m_files_by_id.find(file_id) == m_files_by_id.end());
117
118 lsn_t end_lsn;
119 if (start_lsn > 0) {
120 const bool success =
121 log_file_compute_end_lsn(start_lsn, size_in_bytes, end_lsn);
122 ut_a(success);
123 } else {
124 end_lsn = 0;
125 }
126 ut_a(end_lsn % OS_FILE_LOG_BLOCK_SIZE == 0);
127
128 Log_file meta_info{m_files_ctx, file_id, consumed, full,
129 size_in_bytes, start_lsn, end_lsn, encryption_metadata};
130
131 m_files_by_id.emplace(file_id, meta_info);
132 }
133
134 void Log_files_dict::set_consumed(Log_file_id file_id) {
135 const auto it = m_files_by_id.find(file_id);
136 ut_a(it != m_files_by_id.end());
137 it->second.m_consumed = true;
138 }
139
140 void Log_files_dict::set_full(Log_file_id file_id) {
141 const auto it = m_files_by_id.find(file_id);
142 ut_a(it != m_files_by_id.end());
143 it->second.m_full = true;
144 }
145
146 void Log_files_dict::set_incomplete(Log_file_id file_id) {
147 const auto it = m_files_by_id.find(file_id);
148 ut_a(it != m_files_by_id.end());
149 it->second.m_full = false;
150 }
151
152 void Log_files_dict::set_size(Log_file_id file_id, os_offset_t new_size) {
153 const auto it = m_files_by_id.find(file_id);
154 ut_a(it != m_files_by_id.end());
155 auto &meta_info = it->second;
156
157 meta_info.m_size_in_bytes = new_size;
158
159 /* meta_info.m_start_lsn == 0 holds only for redo files in legacy format,
160 but for them Log_files_dict::set_size must not be called */
161 ut_a(meta_info.m_start_lsn > 0);
162 const bool success = log_file_compute_end_lsn(
163 meta_info.m_start_lsn, meta_info.m_size_in_bytes, meta_info.m_end_lsn);
164 ut_a(success);
165 }
166
167 bool Log_files_dict::empty() const { return m_files_by_id.empty(); }
168
169 const Log_file &Log_files_dict::front() const {
170 ut_a(!m_files_by_id.empty());
171 return m_files_by_id.begin()->second;
172 }
173
174 const Log_file &Log_files_dict::back() const {
175 ut_a(!m_files_by_id.empty());
176 return m_files_by_id.rbegin()->second;
177 }
178
179 /** @} */
180
181 /**************************************************/ /**
182
183 @name Log_files_dict::Const_iterator and related
184
185 *******************************************************/
186
187 /** @{ */
188
189 Log_files_dict::Const_iterator::Const_iterator(
190 Log_files_dict::Log_files_map_iterator it)
191 : m_it(it) {}
192
193 const Log_file &Log_files_dict::Const_iterator::operator*() const {
194 return m_it->second;
195 }
196
197 const Log_file *Log_files_dict::Const_iterator::operator->() const {
198 return &(m_it->second);
199 }
200
201 Log_files_dict::Const_iterator &Log_files_dict::Const_iterator::operator++() {
202 ++m_it;
203 return *this;
204 }
205
206 Log_files_dict::Const_iterator Log_files_dict::Const_iterator::operator++(int) {
207 Const_iterator tmp = *this;
208 ++*this;
209 return tmp;
210 }
211
212 Log_files_dict::Const_iterator &Log_files_dict::Const_iterator::operator--() {
213 --m_it;
214 return *this;
215 }
216
217 Log_files_dict::Const_iterator Log_files_dict::Const_iterator::operator--(int) {
218 Const_iterator tmp = *this;
219 --*this;
220 return tmp;
221 }
222
223 bool Log_files_dict::Const_iterator::operator==(
224 const Const_iterator &rhs) const {
225 return m_it == rhs.m_it;
226 }
227
228 bool Log_files_dict::Const_iterator::operator!=(
229 const Const_iterator &rhs) const {
230 return m_it != rhs.m_it;
231 }
232
233 Log_files_dict::Const_iterator Log_files_dict::begin() const {
234 return Const_iterator{m_files_by_id.cbegin()};
235 }
236
237 Log_files_dict::Const_iterator Log_files_dict::end() const {
238 return Const_iterator{m_files_by_id.cend()};
239 }
240
241 /** @} */
242
243 /**************************************************/ /**
244
245 @name Helpers
246
247 *******************************************************/
248
249 /** @{ */
250
251 bool log_file_compute_logical_capacity(os_offset_t file_size_in_bytes,
252 lsn_t &lsn_capacity) {
253 if (file_size_in_bytes < LOG_FILE_HDR_SIZE ||
254 file_size_in_bytes < UNIV_PAGE_SIZE) {
255 return false;
256 }
257 lsn_capacity = file_size_in_bytes - LOG_FILE_HDR_SIZE;
258 return true;
259 }
260
261 bool log_file_compute_end_lsn(lsn_t start_lsn, os_offset_t file_size_in_bytes,
262 lsn_t &end_lsn) {
263 const lsn_t MAX_FILE_END_LSN = LSN_MAX - 1;
264
265 lsn_t lsn_capacity;
266 if (!log_file_compute_logical_capacity(file_size_in_bytes, lsn_capacity)) {
267 return false;
268 }
269
270 if (start_lsn < LOG_START_LSN || MAX_FILE_END_LSN <= start_lsn ||
271 MAX_FILE_END_LSN - start_lsn <= lsn_capacity) {
272 return false;
273 }
274
275 end_lsn = start_lsn + lsn_capacity;
276 return true;
277 }
278
279 size_t log_files_number_of_existing_files(const Log_files_dict &files) {
280 return std::distance(files.begin(), files.end());
281 }
282
283 size_t log_files_number_of_consumed_files(const Log_files_dict &files) {
284 return std::count_if(files.begin(), files.end(),
285 [](const Log_file &file) { return file.m_consumed; });
286 }
287
288 os_offset_t log_files_size_of_existing_files(const Log_files_dict &files) {
289 return std::accumulate(files.begin(), files.end(), os_offset_t{0},
290 [](os_offset_t total_size, const Log_file &file) {
291 return total_size + file.m_size_in_bytes;
292 });
293 }
294
295 lsn_t log_files_capacity_of_existing_files(const Log_files_dict &files) {
296 return std::accumulate(files.begin(), files.end(), lsn_t{0},
297 [](lsn_t total_capacity, const Log_file &file) {
298 lsn_t logical_capacity;
299 const bool ret = log_file_compute_logical_capacity(
300 file.m_size_in_bytes, logical_capacity);
301 ut_a(ret);
302 return total_capacity + logical_capacity;
303 });
304 }
305
306 Log_files_dict::Const_iterator log_files_find_largest(
307 const Log_files_dict &files) {
308 return std::max_element(files.begin(), files.end(),
309 [](const Log_file &a, const Log_file &b) {
310 return a.m_size_in_bytes < b.m_size_in_bytes;
311 });
312 }
313
314 /** @} */