| 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/ddl0rtree.cc |
| 29 |
DDL cluster index scan implementation. |
| 30 |
Created 2020-11-01 by Sunny Bains. */ |
| 31 |
|
| 32 |
#include "btr0load.h" |
| 33 |
#include "ddl0impl-cursor.h" |
| 34 |
#include "ddl0impl-rtree.h" |
| 35 |
#include "log0chkp.h" |
| 36 |
#include "row0vers.h" |
| 37 |
|
| 38 |
namespace ddl { |
| 39 |
|
| 40 |
RTree_inserter::RTree_inserter(Context &ctx, dict_index_t *index) noexcept |
| 41 |
: m_dtuples(ut::new_withkey<Tuples>(ut::make_psi_memory_key(mem_key_ddl))), |
| 42 |
m_index(index), |
| 43 |
m_ctx(ctx) { |
| 44 |
m_dml_heap = mem_heap_create(512, UT_LOCATION_HERE); |
| 45 |
m_dtuple_heap = mem_heap_create(512, UT_LOCATION_HERE); |
| 46 |
} |
| 47 |
|
| 48 |
RTree_inserter::~RTree_inserter() noexcept { |
| 49 |
if (m_dtuples != nullptr) { |
| 50 |
ut::delete_(m_dtuples); |
| 51 |
} |
| 52 |
if (m_dtuple_heap != nullptr) { |
| 53 |
mem_heap_free(m_dtuple_heap); |
| 54 |
} |
| 55 |
if (m_dml_heap != nullptr) { |
| 56 |
mem_heap_free(m_dml_heap); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
void RTree_inserter::deep_copy_tuples(Tuples::iterator it) noexcept { |
| 61 |
/* Since the data of the tuple pk fields are pointers to cluster index rows. |
| 62 |
After mtr commit, these pointers could point to invalid data. Therefore, |
| 63 |
we need to do a deep copy of the data from scanned index buffer. */ |
| 64 |
for (; it != m_dtuples->end(); ++it) { |
| 65 |
auto dtuple = *it; |
| 66 |
|
| 67 |
/* The first field is the spatial MBR field, which is the key for |
| 68 |
spatial indexes. So skip it. */ |
| 69 |
for (size_t i = 1; i < dtuple_get_n_fields(dtuple); ++i) { |
| 70 |
dfield_dup(&dtuple->fields[i], m_dtuple_heap); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
void RTree_inserter::add_to_batch(const dtuple_t *row, |
| 76 |
const row_ext_t *ext) noexcept { |
| 77 |
auto dtuple = row_build_index_entry(row, ext, m_index, m_dtuple_heap); |
| 78 |
ut_a(dtuple != nullptr); |
| 79 |
m_dtuples->push_back(dtuple); |
| 80 |
} |
| 81 |
|
| 82 |
dberr_t RTree_inserter::batch_insert(trx_id_t trx_id, |
| 83 |
Latch_release &&latch_release) noexcept { |
| 84 |
rec_t *rec{}; |
| 85 |
btr_cur_t cursor; |
| 86 |
ulint *offsets{}; |
| 87 |
rtr_info_t rtr_info; |
| 88 |
big_rec_t *big_rec{}; |
| 89 |
bool latches_released{}; |
| 90 |
dberr_t err{DB_SUCCESS}; |
| 91 |
IF_DEBUG(bool force_log_free_check{};) |
| 92 |
|
| 93 |
static constexpr ulint flag = BTR_NO_UNDO_LOG_FLAG | BTR_NO_LOCKING_FLAG | |
| 94 |
BTR_KEEP_SYS_FLAG | BTR_CREATE_FLAG; |
| 95 |
|
| 96 |
ut_a(dict_index_is_spatial(m_index)); |
| 97 |
|
| 98 |
IF_ENABLED("ddl_instrument_log_check_flush", force_log_free_check = true;) |
| 99 |
|
| 100 |
mtr_t mtr; |
| 101 |
|
| 102 |
cursor.index = m_index; |
| 103 |
|
| 104 |
for (auto it = m_dtuples->begin(); it != m_dtuples->end(); ++it) { |
| 105 |
auto dtuple = *it; |
| 106 |
|
| 107 |
ut_ad(dtuple != nullptr); |
| 108 |
|
| 109 |
/* If there is no sufficient space in the redo logs then deep copy the |
| 110 |
cached records and release the page(s) latches as that would be required to |
| 111 |
commit the mini-transaction. */ |
| 112 |
if (log_free_check_is_required() IF_DEBUG(|| force_log_free_check)) { |
| 113 |
if (!latches_released) { |
| 114 |
deep_copy_tuples(it); |
| 115 |
|
| 116 |
err = latch_release(); |
| 117 |
|
| 118 |
if (err != DB_SUCCESS) { |
| 119 |
return err; |
| 120 |
} |
| 121 |
|
| 122 |
latches_released = true; |
| 123 |
|
| 124 |
IF_DEBUG(force_log_free_check = false;) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
mtr.start(); |
| 129 |
|
| 130 |
rtr_init_rtr_info(&rtr_info, false, &cursor, m_index, false); |
| 131 |
|
| 132 |
rtr_info_update_btr(&cursor, &rtr_info); |
| 133 |
|
| 134 |
btr_cur_search_to_nth_level(m_index, 0, dtuple, PAGE_CUR_RTREE_INSERT, |
| 135 |
BTR_MODIFY_LEAF, &cursor, 0, __FILE__, __LINE__, |
| 136 |
&mtr); |
| 137 |
|
| 138 |
/* Update MBR in parent entry, change search mode to BTR_MODIFY_TREE */ |
| 139 |
if (rtr_info.mbr_adj) { |
| 140 |
mtr.commit(); |
| 141 |
|
| 142 |
rtr_clean_rtr_info(&rtr_info, true); |
| 143 |
|
| 144 |
rtr_init_rtr_info(&rtr_info, false, &cursor, m_index, false); |
| 145 |
|
| 146 |
rtr_info_update_btr(&cursor, &rtr_info); |
| 147 |
|
| 148 |
mtr.start(); |
| 149 |
|
| 150 |
btr_cur_search_to_nth_level(m_index, 0, dtuple, PAGE_CUR_RTREE_INSERT, |
| 151 |
BTR_MODIFY_TREE, &cursor, 0, __FILE__, |
| 152 |
__LINE__, &mtr); |
| 153 |
} |
| 154 |
|
| 155 |
err = btr_cur_optimistic_insert(flag, &cursor, &offsets, &m_dml_heap, |
| 156 |
dtuple, &rec, &big_rec, nullptr, &mtr); |
| 157 |
|
| 158 |
if (err == DB_FAIL) { |
| 159 |
ut_ad(big_rec == nullptr); |
| 160 |
|
| 161 |
mtr.commit(); |
| 162 |
|
| 163 |
mtr.start(); |
| 164 |
|
| 165 |
rtr_clean_rtr_info(&rtr_info, true); |
| 166 |
|
| 167 |
rtr_init_rtr_info(&rtr_info, false, &cursor, m_index, false); |
| 168 |
|
| 169 |
rtr_info_update_btr(&cursor, &rtr_info); |
| 170 |
|
| 171 |
btr_cur_search_to_nth_level(m_index, 0, dtuple, PAGE_CUR_RTREE_INSERT, |
| 172 |
BTR_MODIFY_TREE, &cursor, 0, __FILE__, |
| 173 |
__LINE__, &mtr); |
| 174 |
|
| 175 |
err = btr_cur_pessimistic_insert(flag, &cursor, &offsets, &m_dml_heap, |
| 176 |
dtuple, &rec, &big_rec, nullptr, &mtr); |
| 177 |
} |
| 178 |
|
| 179 |
IF_ENABLED("ddl_ins_spatial_fail", err = DB_FAIL;) |
| 180 |
|
| 181 |
if (err == DB_SUCCESS) { |
| 182 |
if (rtr_info.mbr_adj) { |
| 183 |
err = rtr_ins_enlarge_mbr(&cursor, &mtr); |
| 184 |
} |
| 185 |
|
| 186 |
if (err == DB_SUCCESS) { |
| 187 |
page_update_max_trx_id(btr_cur_get_block(&cursor), |
| 188 |
btr_cur_get_page_zip(&cursor), trx_id, &mtr); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
mtr.commit(); |
| 193 |
|
| 194 |
rtr_clean_rtr_info(&rtr_info, true); |
| 195 |
|
| 196 |
if (err != DB_SUCCESS) { |
| 197 |
m_ctx.set_error(err); |
| 198 |
} else { |
| 199 |
err = m_ctx.get_error(); |
| 200 |
} |
| 201 |
|
| 202 |
if (err != DB_SUCCESS) { |
| 203 |
break; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
m_dtuples->clear(); |
| 208 |
|
| 209 |
mem_heap_empty(m_dml_heap); |
| 210 |
mem_heap_empty(m_dtuple_heap); |
| 211 |
|
| 212 |
return err; |
| 213 |
} |
| 214 |
|
| 215 |
} // namespace ddl |