| 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 "zlob0first.h" |
| 29 |
#include "trx0trx.h" |
| 30 |
#include "zlob0index.h" |
| 31 |
#include "zlob0read.h" |
| 32 |
|
| 33 |
namespace lob { |
| 34 |
|
| 35 |
/** Given the page size, what is the number of index entries the first page |
| 36 |
can contain. */ |
| 37 |
ulint z_first_page_t::get_n_index_entries() const { |
| 38 |
ut_ad(m_index != nullptr); |
| 39 |
|
| 40 |
const page_size_t page_size(dict_table_page_size(m_index->table)); |
| 41 |
|
| 42 |
ulint len = page_size.physical(); |
| 43 |
switch (len) { |
| 44 |
case KB16: |
| 45 |
/* For a page size of 16KB, there are 100 index entries in |
| 46 |
the first page of the zlob. */ |
| 47 |
return (100); |
| 48 |
case 8192: |
| 49 |
/* 8KB. */ |
| 50 |
return (80); |
| 51 |
case 4096: |
| 52 |
/* 4KB. */ |
| 53 |
return (40); |
| 54 |
case 2048: |
| 55 |
return (20); |
| 56 |
case 1024: |
| 57 |
return (5); |
| 58 |
default: |
| 59 |
ut_error; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/** Given the page size, what is the number of frag entries the first page |
| 64 |
can contain. */ |
| 65 |
ulint z_first_page_t::get_n_frag_entries() const { |
| 66 |
ut_ad(m_index != nullptr); |
| 67 |
|
| 68 |
DBUG_EXECUTE_IF("innodb_zlob_first_use_only_1_frag_entries", return (1);); |
| 69 |
|
| 70 |
const page_size_t page_size(dict_table_page_size(m_index->table)); |
| 71 |
ulint len = page_size.physical(); |
| 72 |
switch (len) { |
| 73 |
case KB16: |
| 74 |
/* For a page size of 16KB, there are 200 frag entries in |
| 75 |
the first page of the zlob. */ |
| 76 |
return (200); |
| 77 |
case 8192: |
| 78 |
return (100); |
| 79 |
case 4096: |
| 80 |
return (40); |
| 81 |
case 2048: |
| 82 |
return (20); |
| 83 |
case 1024: |
| 84 |
return (5); |
| 85 |
default: |
| 86 |
ut_error; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
buf_block_t *z_first_page_t::alloc(bool bulk) { |
| 91 |
ut_ad(m_block == nullptr); |
| 92 |
|
| 93 |
page_no_t hint = FIL_NULL; |
| 94 |
m_block = alloc_lob_page(m_index, m_mtr, hint, bulk); |
| 95 |
|
| 96 |
if (m_block == nullptr) { |
| 97 |
return (nullptr); |
| 98 |
} |
| 99 |
|
| 100 |
init(); |
| 101 |
|
| 102 |
ut_ad(m_block->get_page_type() == FIL_PAGE_TYPE_ZLOB_FIRST); |
| 103 |
return (m_block); |
| 104 |
} |
| 105 |
|
| 106 |
/** Print the index entries. */ |
| 107 |
std::ostream &z_first_page_t::print_index_entries(std::ostream &out) const { |
| 108 |
flst_base_node_t *flst = index_list(); |
| 109 |
fil_addr_t node_loc = flst_get_first(flst, m_mtr); |
| 110 |
|
| 111 |
space_id_t space = dict_index_get_space(m_index); |
| 112 |
const page_size_t page_size = dict_table_page_size(m_index->table); |
| 113 |
|
| 114 |
out << "Index Entries: " << flst_bnode_t(flst, m_mtr) << std::endl; |
| 115 |
|
| 116 |
while (!fil_addr_is_null(node_loc)) { |
| 117 |
flst_node_t *node = |
| 118 |
fut_get_ptr(space, page_size, node_loc, RW_X_LATCH, m_mtr); |
| 119 |
z_index_entry_t entry(node, m_mtr, m_index); |
| 120 |
out << entry << std::endl; |
| 121 |
|
| 122 |
flst_base_node_t *vers = entry.get_versions_list(); |
| 123 |
fil_addr_t ver_loc = flst_get_first(vers, m_mtr); |
| 124 |
|
| 125 |
uint32_t depth = 0; |
| 126 |
while (!fil_addr_is_null(ver_loc)) { |
| 127 |
depth++; |
| 128 |
|
| 129 |
for (uint32_t i = 0; i < depth; ++i) { |
| 130 |
out << "+"; |
| 131 |
} |
| 132 |
flst_node_t *ver_node = addr2ptr_x(ver_loc); |
| 133 |
z_index_entry_t vers_entry(ver_node, m_mtr, m_index); |
| 134 |
out << vers_entry << std::endl; |
| 135 |
ver_loc = vers_entry.get_next(); |
| 136 |
} |
| 137 |
|
| 138 |
node_loc = entry.get_next(); |
| 139 |
} |
| 140 |
|
| 141 |
return (out); |
| 142 |
} |
| 143 |
|
| 144 |
/** Print the frag entries. */ |
| 145 |
std::ostream &z_first_page_t::print_frag_entries(std::ostream &out) const { |
| 146 |
flst_base_node_t *flst = frag_list(); |
| 147 |
fil_addr_t node_loc = flst_get_first(flst, m_mtr); |
| 148 |
space_id_t space = dict_index_get_space(m_index); |
| 149 |
const page_size_t page_size = dict_table_page_size(m_index->table); |
| 150 |
|
| 151 |
out << "Frag Entries: " << flst_bnode_t(flst, m_mtr) << std::endl; |
| 152 |
|
| 153 |
while (!fil_addr_is_null(node_loc)) { |
| 154 |
flst_node_t *node = |
| 155 |
fut_get_ptr(space, page_size, node_loc, RW_X_LATCH, m_mtr); |
| 156 |
z_frag_entry_t entry(node, m_mtr); |
| 157 |
out << entry << std::endl; |
| 158 |
node_loc = entry.get_next(); |
| 159 |
} |
| 160 |
|
| 161 |
return (out); |
| 162 |
} |
| 163 |
|
| 164 |
/** Allocate one index entry. If there is no free index entry, allocate |
| 165 |
an index page (a page full of z_index_entry_t objects) and service the |
| 166 |
request. |
| 167 |
@return the allocated index entry. */ |
| 168 |
z_index_entry_t z_first_page_t::alloc_index_entry(bool bulk) { |
| 169 |
flst_base_node_t *free_lst = free_list(); |
| 170 |
fil_addr_t first_loc = flst_get_first(free_lst, m_mtr); |
| 171 |
|
| 172 |
if (fil_addr_is_null(first_loc)) { |
| 173 |
z_index_page_t page(m_mtr, m_index); |
| 174 |
page.alloc(*this, bulk); |
| 175 |
first_loc = flst_get_first(free_lst, m_mtr); |
| 176 |
} |
| 177 |
|
| 178 |
if (fil_addr_is_null(first_loc)) { |
| 179 |
return (z_index_entry_t()); |
| 180 |
} |
| 181 |
|
| 182 |
flst_node_t *first_ptr = addr2ptr_x(first_loc); |
| 183 |
z_index_entry_t entry(first_ptr, m_mtr); |
| 184 |
entry.remove(free_lst); |
| 185 |
|
| 186 |
return (entry); |
| 187 |
} |
| 188 |
|
| 189 |
/** Allocate one frag page entry. If there is no free frag entry, allocate |
| 190 |
an frag node page (a page full of z_frag_entry_t objects) and service the |
| 191 |
request. |
| 192 |
@return the allocated frag entry. */ |
| 193 |
z_frag_entry_t z_first_page_t::alloc_frag_entry(bool bulk) { |
| 194 |
flst_base_node_t *free_lst = free_frag_list(); |
| 195 |
flst_base_node_t *used_lst = frag_list(); |
| 196 |
|
| 197 |
fil_addr_t first_loc = flst_get_first(free_lst, m_mtr); |
| 198 |
|
| 199 |
if (fil_addr_is_null(first_loc)) { |
| 200 |
z_frag_node_page_t page(m_mtr, m_index); |
| 201 |
page.alloc(*this, bulk); |
| 202 |
first_loc = flst_get_first(free_lst, m_mtr); |
| 203 |
} |
| 204 |
|
| 205 |
if (fil_addr_is_null(first_loc)) { |
| 206 |
return (z_frag_entry_t()); |
| 207 |
} |
| 208 |
|
| 209 |
flst_node_t *first_ptr = addr2ptr_x(first_loc); |
| 210 |
z_frag_entry_t entry(first_ptr, m_mtr); |
| 211 |
entry.remove(free_lst); |
| 212 |
entry.push_front(used_lst); |
| 213 |
return (entry); |
| 214 |
} |
| 215 |
|
| 216 |
frag_id_t z_first_page_t::alloc_fragment(bool bulk, ulint len, |
| 217 |
z_frag_page_t &frag_page, |
| 218 |
z_frag_entry_t &entry) { |
| 219 |
ut_ad(m_mtr != nullptr); |
| 220 |
|
| 221 |
frag_id_t frag_id = FRAG_ID_NULL; |
| 222 |
|
| 223 |
frag_page.set_mtr(m_mtr); |
| 224 |
frag_page.set_index(m_index); |
| 225 |
frag_page.set_block_null(); |
| 226 |
|
| 227 |
const page_no_t first_page_no = get_page_no(); |
| 228 |
|
| 229 |
/* Make sure that there will be some extra space for page directory |
| 230 |
entry and meta data. Adding a margin to provide for this. This is |
| 231 |
for exact fit. */ |
| 232 |
const ulint look_size = len + frag_node_t::header_size(); |
| 233 |
|
| 234 |
ut_ad(look_size <= z_frag_page_t::max_payload(m_index)); |
| 235 |
|
| 236 |
flst_base_node_t *frag_lst = frag_list(); |
| 237 |
|
| 238 |
/* Iterate through the list of frag entries in the page. */ |
| 239 |
fil_addr_t loc = flst_get_first(frag_lst, m_mtr); |
| 240 |
|
| 241 |
while (!fil_addr_is_null(loc)) { |
| 242 |
flst_node_t *node = addr2ptr_x(loc); |
| 243 |
entry.reset(node); |
| 244 |
|
| 245 |
ulint big_free = entry.get_big_free_len(); |
| 246 |
|
| 247 |
if (big_free >= look_size) { |
| 248 |
/* Double check if the information in the index |
| 249 |
entry matches with the fragment page. If not, update |
| 250 |
the index entry. */ |
| 251 |
frag_page.load_x(entry.get_page_no()); |
| 252 |
|
| 253 |
const ulint big_free_len_1 = frag_page.get_big_free_len(); |
| 254 |
const ulint big_free_len_2 = entry.get_big_free_len(); |
| 255 |
|
| 256 |
if (big_free_len_1 == big_free_len_2) { |
| 257 |
frag_id = frag_page.alloc_fragment(len, entry); |
| 258 |
if (frag_id != FRAG_ID_NULL) { |
| 259 |
break; |
| 260 |
} |
| 261 |
} else { |
| 262 |
entry.update(frag_page); |
| 263 |
|
| 264 |
/* Check again */ |
| 265 |
big_free = entry.get_big_free_len(); |
| 266 |
|
| 267 |
if (big_free >= look_size) { |
| 268 |
frag_id = frag_page.alloc_fragment(len, entry); |
| 269 |
if (frag_id != FRAG_ID_NULL) { |
| 270 |
break; |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
loc = flst_get_next_addr(node, m_mtr); |
| 277 |
entry.reset(nullptr); |
| 278 |
} |
| 279 |
|
| 280 |
if (frag_id != FRAG_ID_NULL) { |
| 281 |
return (frag_id); |
| 282 |
} |
| 283 |
|
| 284 |
if (fil_addr_is_null(loc)) { |
| 285 |
/* Need to allocate a new fragment page. */ |
| 286 |
buf_block_t *tmp_block = frag_page.alloc(*this, first_page_no + 1, bulk); |
| 287 |
|
| 288 |
if (tmp_block == nullptr) { |
| 289 |
return (FRAG_ID_NULL); |
| 290 |
} |
| 291 |
|
| 292 |
entry = alloc_frag_entry(bulk); |
| 293 |
|
| 294 |
if (entry.is_null()) { |
| 295 |
return (FRAG_ID_NULL); |
| 296 |
} |
| 297 |
|
| 298 |
entry.set_page_no(frag_page.get_page_no()); |
| 299 |
frag_page.set_frag_entry(entry.get_self_addr()); |
| 300 |
|
| 301 |
/* Update the index entry with new space information. */ |
| 302 |
entry.update(frag_page); |
| 303 |
} |
| 304 |
|
| 305 |
#ifdef UNIV_DEBUG |
| 306 |
/* Adding more checks to ensure that an alloc fragment doesn't fail |
| 307 |
for the selected fragment page. */ |
| 308 |
fil_addr_t addr1 = frag_page.get_frag_entry(); |
| 309 |
fil_addr_t addr2 = entry.get_self_addr(); |
| 310 |
ut_ad(addr1.is_equal(addr2)); |
| 311 |
|
| 312 |
const ulint big_free_len_1 = frag_page.get_big_free_len(); |
| 313 |
const ulint big_free_len_2 = entry.get_big_free_len(); |
| 314 |
ut_ad(big_free_len_1 == big_free_len_2); |
| 315 |
|
| 316 |
ut_ad(big_free_len_1 >= look_size); |
| 317 |
ut_ad(big_free_len_1 > len); |
| 318 |
#endif /* UNIV_DEBUG */ |
| 319 |
|
| 320 |
frag_id = frag_page.alloc_fragment(len, entry); |
| 321 |
|
| 322 |
ut_ad(frag_id != FRAG_ID_NULL); |
| 323 |
|
| 324 |
return (frag_id); |
| 325 |
} |
| 326 |
|
| 327 |
/** Print the page. */ |
| 328 |
std::ostream &z_first_page_t::print(std::ostream &out) const { |
| 329 |
print_index_entries(out); |
| 330 |
print_frag_entries(out); |
| 331 |
return (out); |
| 332 |
} |
| 333 |
|
| 334 |
/** Free all the z_frag_page_t pages. All the z_frag_page_t pages are |
| 335 |
singly linked to each other. The head of the list is maintained in the |
| 336 |
first page. */ |
| 337 |
size_t z_first_page_t::free_all_frag_node_pages() { |
| 338 |
size_t n_pages_freed = 0; |
| 339 |
mtr_t local_mtr; |
| 340 |
mtr_start(&local_mtr); |
| 341 |
local_mtr.set_log_mode(m_mtr->get_log_mode()); |
| 342 |
|
| 343 |
load_x(&local_mtr); |
| 344 |
|
| 345 |
while (true) { |
| 346 |
page_no_t page_no = get_frag_node_page_no(); |
| 347 |
if (page_no == FIL_NULL) { |
| 348 |
break; |
| 349 |
} |
| 350 |
|
| 351 |
z_frag_node_page_t frag_node_page(&local_mtr, m_index); |
| 352 |
frag_node_page.load_x(page_no); |
| 353 |
page_no_t next_page = frag_node_page.get_next_page_no(); |
| 354 |
|
| 355 |
/* Make all changes to the first page using local_mtr. */ |
| 356 |
set_frag_node_page_no(next_page, &local_mtr); |
| 357 |
frag_node_page.dealloc(); |
| 358 |
n_pages_freed++; |
| 359 |
|
| 360 |
ut_ad(!local_mtr.conflicts_with(m_mtr)); |
| 361 |
restart_mtr(&local_mtr); |
| 362 |
} |
| 363 |
ut_ad(!local_mtr.conflicts_with(m_mtr)); |
| 364 |
mtr_commit(&local_mtr); |
| 365 |
|
| 366 |
return (n_pages_freed); |
| 367 |
} |
| 368 |
|
| 369 |
/** Free all the index pages. */ |
| 370 |
size_t z_first_page_t::free_all_index_pages() { |
| 371 |
size_t n_pages_freed = 0; |
| 372 |
mtr_t local_mtr; |
| 373 |
mtr_start(&local_mtr); |
| 374 |
local_mtr.set_log_mode(m_mtr->get_log_mode()); |
| 375 |
|
| 376 |
load_x(&local_mtr); |
| 377 |
while (true) { |
| 378 |
page_no_t page_no = get_index_page_no(); |
| 379 |
if (page_no == FIL_NULL) { |
| 380 |
break; |
| 381 |
} |
| 382 |
z_index_page_t index_page(&local_mtr, m_index); |
| 383 |
index_page.load_x(page_no); |
| 384 |
page_no_t next_page = index_page.get_next_page_no(); |
| 385 |
|
| 386 |
/* Make all changes to the first page using local_mtr. */ |
| 387 |
set_index_page_no(next_page, &local_mtr); |
| 388 |
|
| 389 |
index_page.dealloc(); |
| 390 |
n_pages_freed++; |
| 391 |
ut_ad(!local_mtr.conflicts_with(m_mtr)); |
| 392 |
restart_mtr(&local_mtr); |
| 393 |
} |
| 394 |
ut_ad(!local_mtr.conflicts_with(m_mtr)); |
| 395 |
mtr_commit(&local_mtr); |
| 396 |
return (n_pages_freed); |
| 397 |
} |
| 398 |
|
| 399 |
ulint z_first_page_t::size_of_index_entries() const { |
| 400 |
return (z_index_entry_t::SIZE * get_n_index_entries()); |
| 401 |
} |
| 402 |
|
| 403 |
void z_first_page_t::init_index_entries() { |
| 404 |
flst_base_node_t *flst = free_list(); |
| 405 |
ulint n = get_n_index_entries(); |
| 406 |
for (ulint i = 0; i < n; ++i) { |
| 407 |
flst_node_t *ptr = frame() + OFFSET_INDEX_BEGIN; |
| 408 |
ptr += (i * z_index_entry_t::SIZE); |
| 409 |
z_index_entry_t entry(ptr, m_mtr); |
| 410 |
entry.init(); |
| 411 |
entry.push_back(flst); |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
void z_first_page_t::load_entry_s(fil_addr_t &addr, z_index_entry_t &entry) { |
| 416 |
entry.load_s(addr); |
| 417 |
} |
| 418 |
|
| 419 |
void z_first_page_t::load_entry_x(fil_addr_t &addr, z_index_entry_t &entry) { |
| 420 |
entry.load_x(addr); |
| 421 |
} |
| 422 |
|
| 423 |
/** Deallocate the first page of a compressed LOB. */ |
| 424 |
void z_first_page_t::dealloc() { |
| 425 |
ut_ad(m_mtr != nullptr); |
| 426 |
btr_page_free_low(m_index, m_block, ULINT_UNDEFINED, m_mtr); |
| 427 |
m_block = nullptr; |
| 428 |
} |
| 429 |
|
| 430 |
buf_block_t *z_first_page_t::load_x(const page_id_t &page_id, |
| 431 |
const page_size_t &page_size) { |
| 432 |
m_block = |
| 433 |
buf_page_get(page_id, page_size, RW_X_LATCH, UT_LOCATION_HERE, m_mtr); |
| 434 |
|
| 435 |
#ifdef UNIV_DEBUG |
| 436 |
/* Dump the page into the log file, if the page type is not matching |
| 437 |
one of the first page types. */ |
| 438 |
page_type_t page_type = get_page_type(); |
| 439 |
|
| 440 |
switch (page_type) { |
| 441 |
case FIL_PAGE_TYPE_ZBLOB: |
| 442 |
case FIL_PAGE_TYPE_ZLOB_FIRST: |
| 443 |
/* Valid first page type for compressed LOB.*/ |
| 444 |
break; |
| 445 |
default: |
| 446 |
ut_print_buf(std::cout, m_block->frame, page_size.physical()); |
| 447 |
ut_error; |
| 448 |
} |
| 449 |
#endif /* UNIV_DEBUG */ |
| 450 |
|
| 451 |
return (m_block); |
| 452 |
} |
| 453 |
|
| 454 |
/** Increment the LOB version by 1. */ |
| 455 |
uint32_t z_first_page_t::incr_lob_version() { |
| 456 |
ut_ad(m_mtr != nullptr); |
| 457 |
|
| 458 |
const uint32_t cur = get_lob_version(); |
| 459 |
const uint32_t val = cur + 1; |
| 460 |
mlog_write_ulint(frame() + OFFSET_LOB_VERSION, val, MLOG_4BYTES, m_mtr); |
| 461 |
|
| 462 |
return (val); |
| 463 |
} |
| 464 |
|
| 465 |
/** When the bit is set, the LOB is not partially updatable anymore. |
| 466 |
Enable the bit. |
| 467 |
@param[in] trx the current transaction.*/ |
| 468 |
void z_first_page_t::mark_cannot_be_partially_updated(trx_t *trx) { |
| 469 |
const trx_id_t trxid = (trx == nullptr) ? 0 : trx->id; |
| 470 |
const undo_no_t undo_no = (trx == nullptr) ? 0 : (trx->undo_no - 1); |
| 471 |
|
| 472 |
#ifdef LOB_DEBUG |
| 473 |
std::cout << "thread=" << std::this_thread::get_id() |
| 474 |
<< ", ZLOB first page=" << get_page_id() |
| 475 |
<< ", mark_cannot_be_partially_updated()" << std::endl; |
| 476 |
#endif /* LOB_DEBUG */ |
| 477 |
|
| 478 |
uint8_t flags = get_flags(); |
| 479 |
flags |= 0x01; |
| 480 |
mlog_write_ulint(frame() + OFFSET_FLAGS, flags, MLOG_1BYTE, m_mtr); |
| 481 |
|
| 482 |
set_last_trx_id(trxid); |
| 483 |
set_last_trx_undo_no(undo_no); |
| 484 |
} |
| 485 |
|
| 486 |
size_t z_first_page_t::free_all_data_pages() { |
| 487 |
size_t n_pages_freed = 0; |
| 488 |
mtr_t local_mtr; |
| 489 |
mtr_start(&local_mtr); |
| 490 |
local_mtr.set_log_mode(m_mtr->get_log_mode()); |
| 491 |
load_x(&local_mtr); |
| 492 |
|
| 493 |
flst_base_node_t *flst = index_list(); |
| 494 |
fil_addr_t node_loc = flst_get_first(flst, &local_mtr); |
| 495 |
|
| 496 |
z_index_entry_t cur_entry(&local_mtr, m_index); |
| 497 |
|
| 498 |
while (!fil_addr_is_null(node_loc)) { |
| 499 |
flst_node_t *node = addr2ptr_x(node_loc, &local_mtr); |
| 500 |
cur_entry.reset(node); |
| 501 |
n_pages_freed += cur_entry.free_data_pages(&local_mtr); |
| 502 |
|
| 503 |
flst_base_node_t *vers = cur_entry.get_versions_list(); |
| 504 |
fil_addr_t ver_loc = flst_get_first(vers, &local_mtr); |
| 505 |
|
| 506 |
while (!fil_addr_is_null(ver_loc)) { |
| 507 |
flst_node_t *ver_node = addr2ptr_x(ver_loc, &local_mtr); |
| 508 |
z_index_entry_t vers_entry(ver_node, &local_mtr, m_index); |
| 509 |
n_pages_freed += vers_entry.free_data_pages(&local_mtr); |
| 510 |
ver_loc = vers_entry.get_next(); |
| 511 |
|
| 512 |
ut_ad(!local_mtr.conflicts_with(m_mtr)); |
| 513 |
restart_mtr(&local_mtr); |
| 514 |
node = addr2ptr_x(node_loc, &local_mtr); |
| 515 |
cur_entry.reset(node); |
| 516 |
} |
| 517 |
|
| 518 |
node_loc = cur_entry.get_next(); |
| 519 |
cur_entry.reset(nullptr); |
| 520 |
ut_ad(!local_mtr.conflicts_with(m_mtr)); |
| 521 |
restart_mtr(&local_mtr); |
| 522 |
} |
| 523 |
flst_init(flst, &local_mtr); |
| 524 |
flst_base_node_t *free_flst = free_list(); |
| 525 |
flst_init(free_flst, &local_mtr); |
| 526 |
ut_ad(!local_mtr.conflicts_with(m_mtr)); |
| 527 |
mtr_commit(&local_mtr); |
| 528 |
|
| 529 |
return (n_pages_freed); |
| 530 |
} |
| 531 |
|
| 532 |
#ifdef UNIV_DEBUG |
| 533 |
bool z_first_page_t::validate_low() { |
| 534 |
mtr_t local_mtr; |
| 535 |
mtr_start(&local_mtr); |
| 536 |
local_mtr.set_log_mode(m_mtr->get_log_mode()); |
| 537 |
load_x(&local_mtr); |
| 538 |
|
| 539 |
ut_ad(get_page_type() == FIL_PAGE_TYPE_ZLOB_FIRST); |
| 540 |
|
| 541 |
flst_base_node_t *flst = index_list(); |
| 542 |
fil_addr_t node_loc = flst_get_first(flst, &local_mtr); |
| 543 |
|
| 544 |
z_index_entry_t cur_entry(&local_mtr, m_index); |
| 545 |
|
| 546 |
while (!fil_addr_is_null(node_loc)) { |
| 547 |
flst_node_t *node = addr2ptr_x(node_loc, &local_mtr); |
| 548 |
cur_entry.reset(node); |
| 549 |
|
| 550 |
ut_ad(z_validate_strm(m_index, cur_entry, &local_mtr)); |
| 551 |
|
| 552 |
flst_base_node_t *vers = cur_entry.get_versions_list(); |
| 553 |
fil_addr_t ver_loc = flst_get_first(vers, &local_mtr); |
| 554 |
|
| 555 |
while (!fil_addr_is_null(ver_loc)) { |
| 556 |
flst_node_t *ver_node = addr2ptr_x(ver_loc, &local_mtr); |
| 557 |
z_index_entry_t vers_entry(ver_node, &local_mtr, m_index); |
| 558 |
ut_ad(z_validate_strm(m_index, vers_entry, &local_mtr)); |
| 559 |
ver_loc = vers_entry.get_next(); |
| 560 |
restart_mtr(&local_mtr); |
| 561 |
node = addr2ptr_x(node_loc, &local_mtr); |
| 562 |
cur_entry.reset(node); |
| 563 |
} |
| 564 |
|
| 565 |
node_loc = cur_entry.get_next(); |
| 566 |
cur_entry.reset(nullptr); |
| 567 |
|
| 568 |
restart_mtr(&local_mtr); |
| 569 |
} |
| 570 |
|
| 571 |
mtr_commit(&local_mtr); |
| 572 |
return (true); |
| 573 |
} |
| 574 |
#endif /* UNIV_DEBUG */ |
| 575 |
|
| 576 |
void z_first_page_t::import(trx_id_t trx_id) { |
| 577 |
set_trx_id_no_redo(trx_id); |
| 578 |
set_last_trx_id_no_redo(trx_id); |
| 579 |
|
| 580 |
ulint n = get_n_index_entries(); |
| 581 |
for (ulint i = 0; i < n; ++i) { |
| 582 |
flst_node_t *ptr = frame() + OFFSET_INDEX_BEGIN; |
| 583 |
ptr += (i * z_index_entry_t::SIZE); |
| 584 |
z_index_entry_t entry(ptr); |
| 585 |
entry.set_trx_id_no_redo(trx_id); |
| 586 |
entry.set_trx_id_modifier_no_redo(trx_id); |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
size_t z_first_page_t::free_all_frag_pages_old() { |
| 591 |
size_t n_pages_freed = 0; |
| 592 |
mtr_t local_mtr; |
| 593 |
mtr_start(&local_mtr); |
| 594 |
local_mtr.set_log_mode(m_mtr->get_log_mode()); |
| 595 |
load_x(&local_mtr); |
| 596 |
|
| 597 |
/* There is no list of fragment pages maintained. We have to identify the |
| 598 |
list of fragment pages from the following two lists. */ |
| 599 |
flst_base_node_t *frag_lst = frag_list(); |
| 600 |
flst_base_node_t *free_frag_lst = free_frag_list(); |
| 601 |
|
| 602 |
std::vector<flst_base_node_t *> two_list = {frag_lst, free_frag_lst}; |
| 603 |
|
| 604 |
for (auto cur_lst : two_list) { |
| 605 |
while (flst_get_len(cur_lst) > 0) { |
| 606 |
fil_addr_t loc = flst_get_first(cur_lst, &local_mtr); |
| 607 |
flst_node_t *node = addr2ptr_x(loc, &local_mtr); |
| 608 |
z_frag_entry_t entry(node, &local_mtr); |
| 609 |
page_no_t frag_page_no = entry.get_page_no(); |
| 610 |
loc = entry.get_next(); |
| 611 |
entry.remove(cur_lst); |
| 612 |
|
| 613 |
if (frag_page_no == FIL_NULL) { |
| 614 |
continue; |
| 615 |
} |
| 616 |
|
| 617 |
/* Multiple entries can point to the same fragment page. So scan through |
| 618 |
the list and remove all entries pointing to the same fragment page. */ |
| 619 |
while (!fil_addr_is_null(loc)) { |
| 620 |
node = addr2ptr_x(loc, &local_mtr); |
| 621 |
z_frag_entry_t entry2(node, &local_mtr); |
| 622 |
|
| 623 |
loc = entry2.get_next(); |
| 624 |
if (frag_page_no == entry2.get_page_no()) { |
| 625 |
entry2.set_page_no(FIL_NULL); |
| 626 |
entry2.remove(cur_lst); |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
/* Free the fragment page. */ |
| 631 |
entry.free_frag_page(&local_mtr, m_index); |
| 632 |
n_pages_freed++; |
| 633 |
ut_ad(!local_mtr.conflicts_with(m_mtr)); |
| 634 |
restart_mtr(&local_mtr); |
| 635 |
} |
| 636 |
} |
| 637 |
flst_init(frag_lst, &local_mtr); |
| 638 |
flst_init(free_frag_lst, &local_mtr); |
| 639 |
|
| 640 |
ut_ad(!local_mtr.conflicts_with(m_mtr)); |
| 641 |
mtr_commit(&local_mtr); |
| 642 |
return (n_pages_freed); |
| 643 |
} |
| 644 |
|
| 645 |
size_t z_first_page_t::free_all_frag_pages() { |
| 646 |
size_t n_pages_freed = 0; |
| 647 |
if (get_frag_page_no() == 0) { |
| 648 |
n_pages_freed = free_all_frag_pages_old(); |
| 649 |
} else { |
| 650 |
n_pages_freed = free_all_frag_pages_new(); |
| 651 |
} |
| 652 |
return (n_pages_freed); |
| 653 |
} |
| 654 |
|
| 655 |
size_t z_first_page_t::free_all_frag_pages_new() { |
| 656 |
size_t n_pages_freed = 0; |
| 657 |
mtr_t local_mtr; |
| 658 |
mtr_start(&local_mtr); |
| 659 |
local_mtr.set_log_mode(m_mtr->get_log_mode()); |
| 660 |
load_x(&local_mtr); |
| 661 |
|
| 662 |
while (true) { |
| 663 |
page_no_t page_no = get_frag_page_no(&local_mtr); |
| 664 |
if (page_no == FIL_NULL) { |
| 665 |
break; |
| 666 |
} |
| 667 |
z_frag_page_t frag_page(&local_mtr, m_index); |
| 668 |
frag_page.load_x(page_no); |
| 669 |
page_no_t next_page = frag_page.get_next_page_no(); |
| 670 |
set_frag_page_no(&local_mtr, next_page); |
| 671 |
frag_page.dealloc(); |
| 672 |
n_pages_freed++; |
| 673 |
ut_ad(!local_mtr.conflicts_with(m_mtr)); |
| 674 |
restart_mtr(&local_mtr); |
| 675 |
} |
| 676 |
ut_ad(!local_mtr.conflicts_with(m_mtr)); |
| 677 |
mtr_commit(&local_mtr); |
| 678 |
return (n_pages_freed); |
| 679 |
} |
| 680 |
|
| 681 |
size_t z_first_page_t::destroy() { |
| 682 |
size_t n_pages_freed = make_empty(); |
| 683 |
dealloc(); |
| 684 |
n_pages_freed++; |
| 685 |
return (n_pages_freed); |
| 686 |
} |
| 687 |
|
| 688 |
size_t z_first_page_t::make_empty() { |
| 689 |
size_t n_pages_freed = 0; |
| 690 |
n_pages_freed += free_all_data_pages(); |
| 691 |
n_pages_freed += free_all_frag_pages(); |
| 692 |
n_pages_freed += free_all_frag_node_pages(); |
| 693 |
n_pages_freed += free_all_index_pages(); |
| 694 |
return (n_pages_freed); |
| 695 |
} |
| 696 |
|
| 697 |
#ifdef UNIV_DEBUG |
| 698 |
bool z_first_page_t::verify_frag_page_no() { |
| 699 |
mtr_t local_mtr; |
| 700 |
mtr_start(&local_mtr); |
| 701 |
page_no_t page_no = get_frag_page_no(); |
| 702 |
|
| 703 |
/* If the page_no is 0, then FIL_PAGE_PREV is not used to store the list of |
| 704 |
fragment pages. So modifying it is not allowed and hence verification is |
| 705 |
not needed. */ |
| 706 |
ut_ad(page_no != 0); |
| 707 |
|
| 708 |
if (page_no == FIL_NULL) { |
| 709 |
return (true); |
| 710 |
} |
| 711 |
|
| 712 |
z_frag_page_t frag_page(&local_mtr, m_index); |
| 713 |
frag_page.load_x(page_no); |
| 714 |
page_type_t ptype = frag_page.get_page_type(); |
| 715 |
mtr_commit(&local_mtr); |
| 716 |
|
| 717 |
ut_ad(ptype == FIL_PAGE_TYPE_ZLOB_FRAG); |
| 718 |
return (ptype == FIL_PAGE_TYPE_ZLOB_FRAG); |
| 719 |
} |
| 720 |
#endif /* UNIV_DEBUG */ |
| 721 |
|
| 722 |
} /* namespace lob */ |