| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2016, 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 |
#include "zlob0index.h" |
| 29 |
#include "lob0util.h" |
| 30 |
#include "trx0purge.h" |
| 31 |
#include "zlob0first.h" |
| 32 |
|
| 33 |
namespace lob { |
| 34 |
|
| 35 |
fil_addr_t z_index_entry_t::purge_version(dict_index_t *index, |
| 36 |
z_first_page_t &first, |
| 37 |
flst_base_node_t *lst, |
| 38 |
flst_base_node_t *free_list) { |
| 39 |
/* Save the location of next node. */ |
| 40 |
fil_addr_t next_loc = get_next(); |
| 41 |
|
| 42 |
/* Remove the current node from the list it belongs. */ |
| 43 |
remove(lst); |
| 44 |
|
| 45 |
/* Purge the current node. */ |
| 46 |
purge(index, first); |
| 47 |
|
| 48 |
/* Add the current node to the free list. */ |
| 49 |
push_front(free_list); |
| 50 |
|
| 51 |
/* Return the next node location. */ |
| 52 |
return (next_loc); |
| 53 |
} |
| 54 |
|
| 55 |
fil_addr_t z_index_entry_t::make_old_version_current(dict_index_t *index, |
| 56 |
z_first_page_t &first) { |
| 57 |
flst_base_node_t *idx_flst = first.index_list(); |
| 58 |
flst_base_node_t *free_list = first.free_list(); |
| 59 |
flst_base_node_t *version_list = get_versions_list(); |
| 60 |
|
| 61 |
if (flst_get_len(version_list) > 0) { |
| 62 |
space_id_t space = dict_index_get_space(index); |
| 63 |
const page_size_t page_size = dict_table_page_size(index->table); |
| 64 |
|
| 65 |
/* Remove the old version from versions list. */ |
| 66 |
fil_addr_t old_node_addr = flst_get_first(version_list, m_mtr); |
| 67 |
flst_node_t *old_node = |
| 68 |
fut_get_ptr(space, page_size, old_node_addr, RW_X_LATCH, m_mtr); |
| 69 |
flst_remove(version_list, old_node, m_mtr); |
| 70 |
|
| 71 |
/* Copy the version base node from current to old entry. */ |
| 72 |
z_index_entry_t old_entry(old_node, m_mtr, index); |
| 73 |
move_version_base_node(old_entry); |
| 74 |
|
| 75 |
/* Insert the old version after the current node. */ |
| 76 |
insert_after(idx_flst, old_entry); |
| 77 |
} |
| 78 |
|
| 79 |
fil_addr_t loc = purge_version(index, first, idx_flst, free_list); |
| 80 |
|
| 81 |
ut_d(flst_validate(idx_flst, m_mtr)); |
| 82 |
|
| 83 |
return (loc); |
| 84 |
} |
| 85 |
|
| 86 |
/** Purge the current index entry. An index entry points to either a FIRST |
| 87 |
page or DATA page. That LOB page will be freed if it is DATA page. A FIRST |
| 88 |
page should not be freed. */ |
| 89 |
void z_index_entry_t::purge(dict_index_t *index, z_first_page_t &first) { |
| 90 |
set_data_len(0); |
| 91 |
|
| 92 |
const space_id_t space_id = dict_index_get_space(index); |
| 93 |
const page_size_t page_size = dict_table_page_size(index->table); |
| 94 |
|
| 95 |
while (true) { |
| 96 |
page_no_t page_no = get_z_page_no(); |
| 97 |
|
| 98 |
if (page_no == FIL_NULL) { |
| 99 |
break; |
| 100 |
} |
| 101 |
|
| 102 |
buf_block_t *block = buf_page_get(page_id_t(space_id, page_no), page_size, |
| 103 |
RW_X_LATCH, UT_LOCATION_HERE, m_mtr); |
| 104 |
|
| 105 |
page_type_t type = fil_page_get_type(block->frame); |
| 106 |
page_no_t next = block->get_next_page_no(); |
| 107 |
set_z_page_no(next); |
| 108 |
|
| 109 |
switch (type) { |
| 110 |
case FIL_PAGE_TYPE_ZLOB_FIRST: { |
| 111 |
z_first_page_t first(block, m_mtr, index); |
| 112 |
first.set_data_len(0); |
| 113 |
first.set_trx_id(0); |
| 114 |
first.set_next_page_null(); |
| 115 |
} break; |
| 116 |
case FIL_PAGE_TYPE_ZLOB_DATA: |
| 117 |
btr_page_free_low(index, block, ULINT_UNDEFINED, m_mtr); |
| 118 |
break; |
| 119 |
case FIL_PAGE_TYPE_ZLOB_FRAG: { |
| 120 |
z_frag_page_t frag_page(block, m_mtr, index); |
| 121 |
frag_id_t fid = get_z_frag_id(); |
| 122 |
ut_ad(fid != FRAG_ID_NULL); |
| 123 |
ut_ad(frag_page.get_n_frags() > 0); |
| 124 |
|
| 125 |
frag_page.dealloc_fragment(fid); |
| 126 |
|
| 127 |
if (frag_page.get_n_frags() == 0) { |
| 128 |
frag_page.dealloc_with_entry(first, m_mtr); |
| 129 |
} |
| 130 |
} break; |
| 131 |
default: |
| 132 |
ut_d(ut_error); |
| 133 |
} |
| 134 |
|
| 135 |
if (type == FIL_PAGE_TYPE_ZLOB_FRAG) { |
| 136 |
break; |
| 137 |
} |
| 138 |
} |
| 139 |
init(); |
| 140 |
} |
| 141 |
|
| 142 |
std::ostream &z_index_entry_t::print(std::ostream &out) const { |
| 143 |
if (m_node == nullptr) { |
| 144 |
out << "[z_index_entry_t: m_node=null]"; |
| 145 |
} else { |
| 146 |
out << "[z_index_entry_t: m_node=" << (void *)m_node |
| 147 |
<< ", prev=" << get_prev() << ", next=" << get_next() |
| 148 |
<< ", versions=" << flst_bnode_t(get_versions_list(), m_mtr) |
| 149 |
<< ", trx_id=" << get_trx_id() |
| 150 |
<< ", modifier trx_id=" << get_trx_id_modifier() |
| 151 |
<< ", trx_undo_no=" << get_trx_undo_no() |
| 152 |
<< ", trx_undo_no_modifier=" << get_trx_undo_no_modifier() |
| 153 |
<< ", z_page_no=" << get_z_page_no() |
| 154 |
<< ", z_frag_id=" << get_z_frag_id() << ", data_len=" << get_data_len() |
| 155 |
<< ", zdata_len=" << get_zdata_len() << "]"; |
| 156 |
} |
| 157 |
return (out); |
| 158 |
} |
| 159 |
|
| 160 |
std::ostream &z_index_entry_t::print_pages(std::ostream &out) const { |
| 161 |
page_no_t page_no = get_z_page_no(); |
| 162 |
|
| 163 |
const space_id_t space_id = dict_index_get_space(m_index); |
| 164 |
const page_size_t page_size = dict_table_page_size(m_index->table); |
| 165 |
|
| 166 |
out << "[PAGES: "; |
| 167 |
while (page_no != FIL_NULL) { |
| 168 |
buf_block_t *block = buf_page_get(page_id_t(space_id, page_no), page_size, |
| 169 |
RW_S_LATCH, UT_LOCATION_HERE, m_mtr); |
| 170 |
|
| 171 |
page_type_t type = block->get_page_type(); |
| 172 |
out << "[page_no=" << page_no << ", type=" << block->get_page_type_str() |
| 173 |
<< "]"; |
| 174 |
|
| 175 |
page_no = block->get_next_page_no(); |
| 176 |
if (type == FIL_PAGE_TYPE_ZLOB_FRAG) { |
| 177 |
/* Reached the fragment page. Stop. */ |
| 178 |
break; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
out << "]"; |
| 183 |
return (out); |
| 184 |
} |
| 185 |
|
| 186 |
fil_addr_t z_index_entry_t::get_self() const { |
| 187 |
if (m_node == nullptr) { |
| 188 |
return (fil_addr_null); |
| 189 |
} |
| 190 |
page_t *frame = page_align(m_node); |
| 191 |
page_no_t page_no = mach_read_from_4(frame + FIL_PAGE_OFFSET); |
| 192 |
uint32_t offset = static_cast<uint32_t>(m_node - frame); |
| 193 |
ut_ad(offset < UNIV_PAGE_SIZE); |
| 194 |
return (fil_addr_t(page_no, offset)); |
| 195 |
} |
| 196 |
|
| 197 |
void z_index_entry_t::read(z_index_entry_mem_t &entry_mem) const { |
| 198 |
entry_mem.m_self = get_self(); |
| 199 |
entry_mem.m_prev = get_prev(); |
| 200 |
entry_mem.m_next = get_next(); |
| 201 |
entry_mem.m_versions = get_versions_mem(); |
| 202 |
entry_mem.m_trx_id = get_trx_id(); |
| 203 |
entry_mem.m_z_page_no = get_z_page_no(); |
| 204 |
entry_mem.m_z_frag_id = get_z_frag_id(); |
| 205 |
entry_mem.m_data_len = get_data_len(); |
| 206 |
entry_mem.m_z_data_len = get_zdata_len(); |
| 207 |
} |
| 208 |
|
| 209 |
void z_index_entry_t::move_version_base_node(z_index_entry_t &entry) { |
| 210 |
ut_ad(m_mtr != nullptr); |
| 211 |
|
| 212 |
flst_base_node_t *from_node = get_versions_list(); |
| 213 |
flst_base_node_t *to_node = entry.get_versions_list(); |
| 214 |
|
| 215 |
/* Copy with redo logging the version list base node. */ |
| 216 |
mlog_write_string(to_node, from_node, FLST_BASE_NODE_SIZE, m_mtr); |
| 217 |
|
| 218 |
ut_ad(flst_get_len(from_node) == flst_get_len(to_node)); |
| 219 |
fil_addr_t addr1 = flst_get_first(from_node, m_mtr); |
| 220 |
fil_addr_t addr2 = flst_get_first(to_node, m_mtr); |
| 221 |
ut_ad(addr1.is_equal(addr2)); |
| 222 |
addr1 = flst_get_last(from_node, m_mtr); |
| 223 |
addr2 = flst_get_last(to_node, m_mtr); |
| 224 |
ut_ad(addr1.is_equal(addr2)); |
| 225 |
flst_init(from_node, m_mtr); |
| 226 |
} |
| 227 |
|
| 228 |
/* The given entry becomes the old version of the current entry. |
| 229 |
Move the version base node from old entry to current entry. |
| 230 |
@param[in] entry the old entry */ |
| 231 |
void z_index_entry_t::set_old_version(z_index_entry_t &entry) { |
| 232 |
flst_base_node_t *version_list = get_versions_list(); |
| 233 |
ut_ad(flst_get_len(version_list) == 0); |
| 234 |
entry.move_version_base_node(*this); |
| 235 |
entry.push_front(version_list); |
| 236 |
} |
| 237 |
|
| 238 |
std::ostream &z_index_entry_mem_t::print(std::ostream &out) const { |
| 239 |
out << "[z_index_entry_mem_t: self=" << m_self << ", prev=" << m_prev |
| 240 |
<< ", next=" << m_next << ", versions=" << m_versions |
| 241 |
<< ", m_trx_id=" << m_trx_id |
| 242 |
<< ", m_trx_id_modifier=" << m_trx_id_modifier |
| 243 |
<< ", m_trx_undo_no=" << m_trx_undo_no |
| 244 |
<< ", m_trx_undo_no_modifier=" << m_trx_undo_no_modifier |
| 245 |
<< ", z_page_no=" << m_z_page_no << ", z_frag_id=" << m_z_frag_id |
| 246 |
<< ", data_len=" << m_data_len << ", z_data_len=" << m_z_data_len << "]"; |
| 247 |
return (out); |
| 248 |
} |
| 249 |
|
| 250 |
size_t z_index_entry_t::free_data_pages(mtr_t *mtr) { |
| 251 |
size_t n_pages = 0; |
| 252 |
page_no_t page_no = get_z_page_no(); |
| 253 |
space_id_t space_id = dict_index_get_space(m_index); |
| 254 |
|
| 255 |
while (page_no != FIL_NULL) { |
| 256 |
buf_block_t *block = |
| 257 |
buf_page_get(page_id_t(space_id, page_no), m_index->get_page_size(), |
| 258 |
RW_X_LATCH, UT_LOCATION_HERE, mtr); |
| 259 |
|
| 260 |
/* Save the next page number before freeing. */ |
| 261 |
page_no = block->get_next_page_no(); |
| 262 |
|
| 263 |
page_type_t ptype = block->get_page_type(); |
| 264 |
|
| 265 |
ut_ad(ptype == FIL_PAGE_TYPE_ZLOB_DATA || |
| 266 |
ptype == FIL_PAGE_TYPE_ZLOB_FRAG || |
| 267 |
ptype == FIL_PAGE_TYPE_ZLOB_FIRST); |
| 268 |
|
| 269 |
if (ptype == FIL_PAGE_TYPE_ZLOB_DATA) { |
| 270 |
z_data_page_t data_page(block, mtr, m_index); |
| 271 |
data_page.dealloc(); |
| 272 |
set_z_page_no_null(mtr); |
| 273 |
n_pages++; |
| 274 |
} else if (ptype == FIL_PAGE_TYPE_ZLOB_FRAG) { |
| 275 |
/* Reached the end of the stream. */ |
| 276 |
break; |
| 277 |
} else if (ptype == FIL_PAGE_TYPE_ZLOB_FIRST) { |
| 278 |
/* Do nothing. */ |
| 279 |
} |
| 280 |
} |
| 281 |
return (n_pages); |
| 282 |
} |
| 283 |
|
| 284 |
} /* namespace lob */ |