| 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/ddl0buffer.cc |
| 29 |
DDL key buffer implementation. |
| 30 |
Created 2020-11-01 by Sunny Bains. */ |
| 31 |
|
| 32 |
#include "ddl0impl-buffer.h" |
| 33 |
#include "ddl0impl-compare.h" |
| 34 |
|
| 35 |
namespace ddl { |
| 36 |
|
| 37 |
/** Merge sort a given array. |
| 38 |
@param[in, out] arr Array to sort. |
| 39 |
@param[in, out] aux_arr Auxiliary space to use for sort. |
| 40 |
@param[in] low First element (inclusive). |
| 41 |
@param[in] high Number of elements to sort from low. |
| 42 |
@param[in] compare Function to compare two elements. */ |
| 43 |
template <typename T, typename Compare> |
| 44 |
inline void merge_sort(T *arr, T *aux_arr, const size_t low, const size_t high, |
| 45 |
Compare compare) { |
| 46 |
ut_a(low < high); |
| 47 |
|
| 48 |
if (low == high - 2) { |
| 49 |
if (compare(arr[low], arr[high - 1]) > 0) { |
| 50 |
aux_arr[low] = arr[low]; |
| 51 |
arr[low] = arr[high - 1]; |
| 52 |
arr[high - 1] = aux_arr[low]; |
| 53 |
} |
| 54 |
return; |
| 55 |
} else if (unlikely(low == high - 1)) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
auto l = low; |
| 60 |
auto h = (low + high) >> 1; |
| 61 |
const auto m = h; |
| 62 |
|
| 63 |
merge_sort(arr, aux_arr, low, m, compare); |
| 64 |
merge_sort(arr, aux_arr, m, high, compare); |
| 65 |
|
| 66 |
for (auto i = low; i < high; ++i) { |
| 67 |
if (l >= m) { |
| 68 |
aux_arr[i] = arr[h++]; |
| 69 |
} else if (h >= high) { |
| 70 |
aux_arr[i] = arr[l++]; |
| 71 |
} else if (compare(arr[l], arr[h]) > 0) { |
| 72 |
aux_arr[i] = arr[h++]; |
| 73 |
} else { |
| 74 |
aux_arr[i] = arr[l++]; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
memcpy(arr + low, aux_arr + low, (high - low) * sizeof(*arr)); |
| 79 |
} |
| 80 |
|
| 81 |
Key_sort_buffer::Key_sort_buffer(dict_index_t *index, size_t size) noexcept |
| 82 |
: m_index(index), m_buffer_size(size) { |
| 83 |
m_heap = mem_heap_create(1024, UT_LOCATION_HERE); |
| 84 |
} |
| 85 |
|
| 86 |
void Key_sort_buffer::deep_copy(size_t n_fields, size_t data_size) noexcept { |
| 87 |
auto field = m_dtuples[m_n_tuples++]; |
| 88 |
|
| 89 |
do { |
| 90 |
dfield_dup(field++, m_heap); |
| 91 |
} while (--n_fields > 0); |
| 92 |
|
| 93 |
m_total_size += data_size; |
| 94 |
} |
| 95 |
|
| 96 |
void Key_sort_buffer::clear() noexcept { |
| 97 |
m_n_tuples = 0; |
| 98 |
m_total_size = 0; |
| 99 |
m_dtuples = {}; |
| 100 |
mem_heap_empty(m_heap); |
| 101 |
} |
| 102 |
|
| 103 |
void Key_sort_buffer::sort(Dup *dup) noexcept { |
| 104 |
ut_ad(!dict_index_is_spatial(m_index)); |
| 105 |
|
| 106 |
DTuples aux{}; |
| 107 |
|
| 108 |
aux.resize(m_n_tuples); |
| 109 |
|
| 110 |
/* Compare all the columns of the key to preserve order in the index. */ |
| 111 |
Compare_key compare_key(m_index, dup, true); |
| 112 |
|
| 113 |
merge_sort(&m_dtuples[0], &aux[0], 0, m_n_tuples, compare_key); |
| 114 |
} |
| 115 |
|
| 116 |
dberr_t Key_sort_buffer::serialize(IO_buffer io_buffer, |
| 117 |
Function persist) noexcept { |
| 118 |
std::pair<const byte *, const byte *> bounds{ |
| 119 |
io_buffer.first, io_buffer.first + io_buffer.second}; |
| 120 |
|
| 121 |
/* Points past the filled part of buffer */ |
| 122 |
auto ptr = io_buffer.first; |
| 123 |
|
| 124 |
/* Move as many blocks as possible out of the buffer by persisting them */ |
| 125 |
auto write_buffer = [io_buffer, &ptr, persist]() -> dberr_t { |
| 126 |
auto persist_buffer = io_buffer; |
| 127 |
|
| 128 |
const size_t buf_filled = ptr - io_buffer.first; |
| 129 |
persist_buffer.second = ut_uint64_align_down(buf_filled, IO_BLOCK_SIZE); |
| 130 |
|
| 131 |
auto err = persist(persist_buffer); |
| 132 |
|
| 133 |
if (err != DB_SUCCESS) { |
| 134 |
return err; |
| 135 |
} |
| 136 |
|
| 137 |
const os_offset_t bytes_written = persist_buffer.second; |
| 138 |
const auto bytes_remaining = buf_filled - bytes_written; |
| 139 |
|
| 140 |
ptr = io_buffer.first; |
| 141 |
memmove(ptr, ptr + bytes_written, bytes_remaining); |
| 142 |
ptr += bytes_remaining; |
| 143 |
|
| 144 |
/* Remaining contents of buffer must be less than the needed alignment.*/ |
| 145 |
ut_ad(bytes_remaining < IO_BLOCK_SIZE); |
| 146 |
|
| 147 |
return DB_SUCCESS; |
| 148 |
}; |
| 149 |
|
| 150 |
size_t i{}; |
| 151 |
const auto n_fields = dict_index_get_n_fields(m_index); |
| 152 |
|
| 153 |
for (const auto &fields : m_dtuples) { |
| 154 |
if (i++ >= m_n_tuples) { |
| 155 |
break; |
| 156 |
} |
| 157 |
|
| 158 |
ulint extra_size; |
| 159 |
|
| 160 |
const auto size = rec_get_serialize_size(m_index, fields, n_fields, nullptr, |
| 161 |
&extra_size, MAX_ROW_VERSION); |
| 162 |
|
| 163 |
{ |
| 164 |
const auto rec_size = size + extra_size + 2; |
| 165 |
|
| 166 |
if (rec_size >= io_buffer.second) { |
| 167 |
/* Single row doesn't fit into our IO buffer. */ |
| 168 |
return DB_TOO_BIG_RECORD; |
| 169 |
} |
| 170 |
|
| 171 |
ut_a(size >= extra_size); |
| 172 |
} |
| 173 |
|
| 174 |
size_t need; |
| 175 |
char prefix[sizeof(uint16_t)]; |
| 176 |
|
| 177 |
/* Encode extra_size + 1 */ |
| 178 |
if (extra_size + 1 < 0x80) { |
| 179 |
need = 1; |
| 180 |
prefix[0] = (byte)(extra_size + 1); |
| 181 |
} else { |
| 182 |
need = 2; |
| 183 |
ut_a((extra_size + 1) < 0x8000); |
| 184 |
prefix[0] = (byte)(0x80 | ((extra_size + 1) >> 8)); |
| 185 |
prefix[1] = (byte)(extra_size + 1); |
| 186 |
} |
| 187 |
|
| 188 |
const auto rec_size = need + size; |
| 189 |
|
| 190 |
/* If serialized record won't fit in buffer, make space in the buffer |
| 191 |
by persisting a portion of it */ |
| 192 |
if (unlikely(ptr + rec_size > bounds.second)) { |
| 193 |
const auto err = write_buffer(); |
| 194 |
if (err != DB_SUCCESS) { |
| 195 |
return err; |
| 196 |
} |
| 197 |
ut_a(ptr + rec_size <= bounds.second); |
| 198 |
} |
| 199 |
|
| 200 |
memcpy(ptr, prefix, need); |
| 201 |
ptr += need; |
| 202 |
|
| 203 |
{ |
| 204 |
const auto p = ptr + extra_size; |
| 205 |
rec_serialize_dtuple(p, m_index, fields, n_fields, nullptr, |
| 206 |
MAX_ROW_VERSION); |
| 207 |
} |
| 208 |
|
| 209 |
ptr += size; |
| 210 |
} |
| 211 |
|
| 212 |
ut_a(ptr <= bounds.second); |
| 213 |
|
| 214 |
/* At this point there is some data remaining in buffer. It needs |
| 215 |
to be persisted, followed by zero-filled region at least 1 byte in |
| 216 |
length and aligned to IO_BLOCK_SIZE ("end-of-chunk" marker) */ |
| 217 |
size_t buf_filled = ptr - io_buffer.first; |
| 218 |
size_t aligned_size = ut_uint64_align_up(buf_filled + 1, IO_BLOCK_SIZE); |
| 219 |
|
| 220 |
/* Check if adding the end-of-chunk marker would overflow the buffer */ |
| 221 |
if (aligned_size > io_buffer.second) { |
| 222 |
/* If so, persist a portion of the buffer to free it up */ |
| 223 |
const auto err = write_buffer(); |
| 224 |
if (err != DB_SUCCESS) { |
| 225 |
return err; |
| 226 |
} |
| 227 |
ut_ad(ptr >= io_buffer.first); |
| 228 |
ut_a(size_t(ptr - io_buffer.first) < IO_BLOCK_SIZE); |
| 229 |
/* After writing buffer contains [0, IO_BLOCK_SIZE) bytes, |
| 230 |
so aligning it to IO_BLOCK_SIZE guarantees space for |
| 231 |
end-of-chunk marker */ |
| 232 |
aligned_size = IO_BLOCK_SIZE; |
| 233 |
} |
| 234 |
|
| 235 |
/* Append the end-of-chunk marker. */ |
| 236 |
ut_a(ptr < bounds.second); |
| 237 |
auto pad_end = io_buffer.first + aligned_size; |
| 238 |
ut_ad(pad_end > ptr); |
| 239 |
size_t pad_length = pad_end - ptr; |
| 240 |
memset(ptr, 0, pad_length); |
| 241 |
|
| 242 |
return persist({io_buffer.first, aligned_size}); |
| 243 |
} |
| 244 |
|
| 245 |
int Key_sort_buffer::compare(const dfield_t *lhs, const dfield_t *rhs, |
| 246 |
Dup *dup) noexcept { |
| 247 |
ut_ad(dup->m_index->is_clustered()); |
| 248 |
|
| 249 |
Compare_key compare_key(dup->m_index, dup, false); |
| 250 |
|
| 251 |
return compare_key(lhs, rhs); |
| 252 |
} |
| 253 |
|
| 254 |
} // namespace ddl |