| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2015, 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 "lob0del.h" |
| 29 |
|
| 30 |
namespace lob { |
| 31 |
|
| 32 |
/* Obtain an x-latch on the clustered index record page.*/ |
| 33 |
void Deleter::x_latch_rec_page() { |
| 34 |
bool found; |
| 35 |
page_t *rec_page = m_ctx.m_blobref.page_align(); |
| 36 |
page_no_t rec_page_no = page_get_page_no(rec_page); |
| 37 |
space_id_t rec_space_id = page_get_space_id(rec_page); |
| 38 |
|
| 39 |
const page_size_t &rec_page_size = |
| 40 |
fil_space_get_page_size(rec_space_id, &found); |
| 41 |
ut_ad(found); |
| 42 |
|
| 43 |
buf_page_get(page_id_t(rec_space_id, rec_page_no), rec_page_size, RW_X_LATCH, |
| 44 |
UT_LOCATION_HERE, &m_mtr); |
| 45 |
} |
| 46 |
|
| 47 |
/** Returns the page number where the next BLOB part is stored. |
| 48 |
@param[in] blob_header the BLOB header. |
| 49 |
@return page number or FIL_NULL if no more pages */ |
| 50 |
static inline page_no_t btr_blob_get_next_page_no(const byte *blob_header) { |
| 51 |
return (mach_read_from_4(blob_header + LOB_HDR_NEXT_PAGE_NO)); |
| 52 |
} |
| 53 |
|
| 54 |
/** Free the first page of the BLOB and update the BLOB reference |
| 55 |
in the clustered index. |
| 56 |
@return DB_SUCCESS on pass, error code on failure. */ |
| 57 |
dberr_t Deleter::free_first_page() { |
| 58 |
dberr_t err(DB_SUCCESS); |
| 59 |
page_no_t next_page_no; |
| 60 |
|
| 61 |
mtr_start(&m_mtr); |
| 62 |
m_mtr.set_log_mode(m_ctx.m_mtr->get_log_mode()); |
| 63 |
|
| 64 |
ut_ad(m_ctx.m_pcur == nullptr || !m_ctx.table()->is_temporary() || |
| 65 |
m_ctx.m_mtr->get_log_mode() == MTR_LOG_NO_REDO); |
| 66 |
|
| 67 |
page_no_t page_no = m_ctx.m_blobref.page_no(); |
| 68 |
space_id_t space_id = m_ctx.m_blobref.space_id(); |
| 69 |
|
| 70 |
x_latch_rec_page(); |
| 71 |
|
| 72 |
buf_block_t *blob_block = |
| 73 |
buf_page_get(page_id_t(space_id, page_no), m_ctx.m_page_size, RW_X_LATCH, |
| 74 |
UT_LOCATION_HERE, &m_mtr); |
| 75 |
|
| 76 |
buf_block_dbg_add_level(blob_block, SYNC_EXTERN_STORAGE); |
| 77 |
page_t *page = buf_block_get_frame(blob_block); |
| 78 |
|
| 79 |
ut_a(validate_page_type(page)); |
| 80 |
|
| 81 |
if (m_ctx.is_compressed()) { |
| 82 |
next_page_no = mach_read_from_4(page + FIL_PAGE_NEXT); |
| 83 |
} else { |
| 84 |
next_page_no = btr_blob_get_next_page_no(page + FIL_PAGE_DATA); |
| 85 |
} |
| 86 |
|
| 87 |
btr_page_free_low(m_ctx.m_index, blob_block, ULINT_UNDEFINED, &m_mtr); |
| 88 |
|
| 89 |
if (m_ctx.is_compressed() && m_ctx.get_page_zip() != nullptr) { |
| 90 |
m_ctx.m_blobref.set_page_no(next_page_no, nullptr); |
| 91 |
m_ctx.m_blobref.set_length(0, nullptr); |
| 92 |
page_zip_write_blob_ptr(m_ctx.get_page_zip(), m_ctx.m_rec, m_ctx.m_index, |
| 93 |
m_ctx.m_offsets, m_ctx.m_field_no, &m_mtr); |
| 94 |
} else { |
| 95 |
m_ctx.m_blobref.set_page_no(next_page_no, &m_mtr); |
| 96 |
m_ctx.m_blobref.set_length(0, &m_mtr); |
| 97 |
} |
| 98 |
|
| 99 |
/* Commit mtr and release the BLOB block to save memory. */ |
| 100 |
blob_free(m_ctx.m_index, blob_block, true, &m_mtr); |
| 101 |
|
| 102 |
return (err); |
| 103 |
} |
| 104 |
|
| 105 |
/** Free the LOB object. |
| 106 |
@return DB_SUCCESS on success. */ |
| 107 |
dberr_t Deleter::destroy() { |
| 108 |
dberr_t err(DB_SUCCESS); |
| 109 |
|
| 110 |
if (!can_free()) { |
| 111 |
return (DB_SUCCESS); |
| 112 |
} |
| 113 |
|
| 114 |
if (dict_index_is_online_ddl(m_ctx.index())) { |
| 115 |
row_log_table_blob_free(m_ctx.index(), m_ctx.m_blobref.page_no()); |
| 116 |
} |
| 117 |
|
| 118 |
while (m_ctx.m_blobref.page_no() != FIL_NULL) { |
| 119 |
ut_ad(m_ctx.m_blobref.page_no() > 0); |
| 120 |
|
| 121 |
err = free_first_page(); |
| 122 |
if (err != DB_SUCCESS) { |
| 123 |
break; |
| 124 |
} |
| 125 |
|
| 126 |
DBUG_EXECUTE_IF("ib_zlob_deleter_middle_crash", { |
| 127 |
m_mtr.wait_for_flush(); |
| 128 |
DBUG_SUICIDE(); |
| 129 |
}); |
| 130 |
} |
| 131 |
|
| 132 |
return (err); |
| 133 |
} |
| 134 |
|
| 135 |
/** Check if the BLOB can be freed. If the clustered index record |
| 136 |
is not the owner of the LOB, then it cannot be freed. Also, during |
| 137 |
rollback, if inherited flag is set, then LOB will not be freed. |
| 138 |
@return true if the BLOB can be freed, false otherwise. */ |
| 139 |
bool Deleter::can_free() const { |
| 140 |
if (m_ctx.m_blobref.is_null()) { |
| 141 |
/* In the rollback, we may encounter a clustered index |
| 142 |
record with some unwritten off-page columns. There is |
| 143 |
nothing to free then. */ |
| 144 |
ut_a(m_ctx.m_rollback); |
| 145 |
return (false); |
| 146 |
} |
| 147 |
|
| 148 |
if (!m_ctx.m_blobref.is_owner() || m_ctx.m_blobref.page_no() == FIL_NULL || |
| 149 |
(m_ctx.m_rollback && m_ctx.m_blobref.is_inherited())) { |
| 150 |
return (false); |
| 151 |
} |
| 152 |
|
| 153 |
return (true); |
| 154 |
} |
| 155 |
|
| 156 |
} // namespace lob |