Debian Codegraph @

storage/innobase/ddl/ddl0file-reader.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 2020, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 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, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file ddl/ddl0file-reader.cc
29 For reading the DDL temporary files.
30 Created 2020-11-01 by Sunny Bains. */
31
32 #include "ddl0impl-file-reader.h"
33 #include "dict0dict.h"
34 #include "rem/rec.h"
35 #include "rem0rec.h"
36
37 namespace ddl {
38
39 dberr_t File_reader::prepare() noexcept {
40 ut_a(m_ptr == nullptr);
41 ut_a(m_mrec == nullptr);
42 ut_a(m_buffer_size > 0);
43 ut_a(m_bounds.first == nullptr && m_bounds.second == nullptr);
44
45 if (end_of_range()) {
46 return DB_END_OF_INDEX;
47 }
48
49 m_aligned_buffer = ut::make_unique_aligned<byte[]>(
50 ut::make_psi_memory_key(mem_key_ddl), UNIV_SECTOR_SIZE, m_buffer_size);
51
52 if (!m_aligned_buffer) {
53 return DB_OUT_OF_MEMORY;
54 }
55
56 m_io_buffer = {m_aligned_buffer.get(), m_buffer_size};
57
58 m_mrec = m_io_buffer.first;
59 m_bounds.first = m_io_buffer.first;
60 m_bounds.second = m_bounds.first + m_io_buffer.second,
61
62 m_ptr = m_io_buffer.first;
63
64 const auto n_fields = dict_index_get_n_fields(m_index);
65 const auto n = 1 + REC_OFFS_HEADER_SIZE + n_fields;
66
67 ut_a(m_field_offsets.empty());
68
69 m_field_offsets.resize(n);
70
71 m_field_offsets[0] = n;
72 m_field_offsets[1] = n_fields;
73
74 ut_a(m_aux_buf == nullptr);
75 m_aux_buf = ut::new_arr_withkey<byte>(ut::make_psi_memory_key(mem_key_ddl),
76 ut::Count{UNIV_PAGE_SIZE_MAX / 2});
77
78 if (m_aux_buf == nullptr) {
79 return DB_OUT_OF_MEMORY;
80 }
81
82 /* Position the m_ptr to the first record. */
83 if (const auto err = seek(); err != DB_SUCCESS) {
84 return err;
85 }
86
87 /* Position m_mrec on the first record and advance to next record. */
88 return next();
89 }
90
91 dberr_t File_reader::seek() noexcept {
92 ut_a(m_range.second > m_range.first);
93
94 const auto len = std::min(m_io_buffer.second, m_range.second - m_range.first);
95 const auto err =
96 ddl::pread(m_file.get(), m_io_buffer.first, len, m_range.first);
97
98 if (err == DB_SUCCESS) {
99 /* Fetch the record. */
100 m_ptr = m_io_buffer.first;
101 }
102
103 return err;
104 }
105
106 dberr_t File_reader::read(const Range &range) noexcept {
107 ut_a(range.first < range.second);
108 m_range = range;
109
110 /* Read the page in the file buffer */
111 const auto err = seek();
112
113 if (err == DB_SUCCESS) {
114 /* Position m_mrec on the first record. */
115 return next();
116 } else {
117 return err;
118 }
119 }
120
121 dberr_t File_reader::read_next() noexcept {
122 m_range.first = m_range.first + m_io_buffer.second;
123 return seek();
124 }
125
126 dberr_t File_reader::next() noexcept {
127 ut_a(m_ptr >= m_bounds.first && m_ptr < m_bounds.second);
128
129 size_t extra_size = *m_ptr++;
130
131 if (extra_size == 0) {
132 /* Mark as end of range. */
133 m_range.first = m_range.second;
134 return DB_END_OF_INDEX;
135 }
136
137 if (extra_size >= 0x80) {
138 /* Read another byte of extra_size. */
139 if (m_ptr >= m_bounds.second) {
140 const auto err = read_next();
141 if (err != DB_SUCCESS) {
142 return err;
143 }
144 }
145
146 extra_size = (extra_size & 0x7f) << 8;
147 extra_size |= *m_ptr++;
148 }
149
150 /* Normalize extra_size. Above, value 0 signals "end of list". */
151 --extra_size;
152
153 /* Read the extra bytes. */
154
155 auto rec = const_cast<byte *>(m_ptr);
156
157 if (unlikely(rec + extra_size >= m_bounds.second)) {
158 /* The record spans two blocks. Copy the entire record to the auxiliary
159 buffer and handle this as a special case. */
160 const auto partial_size = std::ptrdiff_t(m_bounds.second - m_ptr);
161
162 ut_a(static_cast<size_t>(partial_size) < UNIV_PAGE_SIZE_MAX);
163
164 rec = m_aux_buf;
165
166 /* Copy the partial record from the file buffer to the aux buffer. */
167 memcpy(rec, m_ptr, partial_size);
168
169 {
170 const auto err = read_next();
171
172 if (err != DB_SUCCESS) {
173 return err;
174 }
175 }
176
177 {
178 /* Copy the remaining record from the file buffer to the aux buffer. */
179 const auto len = extra_size - partial_size;
180
181 memcpy(rec + partial_size, m_ptr, len);
182
183 m_ptr += len;
184 }
185
186 rec_deserialize_init_offsets(rec + extra_size, m_index,
187 &m_field_offsets[0]);
188
189 const auto data_size = rec_offs_data_size(&m_field_offsets[0]);
190
191 /* These overflows should be impossible given that records are much
192 smaller than either buffer, and the record starts near the beginning
193 of each buffer. */
194 ut_a(m_ptr + data_size < m_bounds.second);
195 ut_a(extra_size + data_size < UNIV_PAGE_SIZE_MAX);
196
197 /* Copy the data bytes. */
198 memcpy(rec + extra_size, m_ptr, data_size);
199
200 m_ptr += data_size;
201
202 } else {
203 rec_deserialize_init_offsets(rec + extra_size, m_index,
204 &m_field_offsets[0]);
205
206 const auto data_size = rec_offs_data_size(&m_field_offsets[0]);
207
208 ut_a(extra_size + data_size < UNIV_PAGE_SIZE_MAX);
209
210 const auto required = extra_size + data_size;
211
212 /* Check if the record fits entirely in the block. */
213 if (unlikely(m_ptr + required >= m_bounds.second)) {
214 /* The record spans two blocks. Copy prefix it to buf. */
215 const auto partial_size = std::ptrdiff_t(m_bounds.second - m_ptr);
216
217 rec = m_aux_buf;
218
219 memcpy(rec, m_ptr, partial_size);
220
221 /* We cannot invoke rec_offs_make_valid() here, because there
222 are no REC_N_NEW_EXTRA_BYTES between extra_size and data_size.
223 Similarly, rec_offs_validate() would fail, because it invokes
224 rec_get_status(). */
225 ut_d(m_field_offsets[3] = (ulint)m_index);
226 ut_d(m_field_offsets[2] = (ulint)rec + extra_size);
227
228 {
229 const auto err = read_next();
230
231 if (err != DB_SUCCESS) {
232 return err;
233 }
234 }
235
236 {
237 /* Copy the rest of the record. */
238 const auto len = extra_size + data_size - partial_size;
239
240 memcpy(rec + partial_size, m_ptr, len);
241 m_ptr += len;
242 }
243 } else {
244 m_ptr += required;
245 }
246 }
247
248 ++m_n_rows_read;
249
250 m_mrec = rec + extra_size;
251
252 return DB_SUCCESS;
253 }
254
255 } // namespace ddl