| 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 "lob0pages.h" |
| 29 |
#include "lob0impl.h" |
| 30 |
#include "my_dbug.h" |
| 31 |
#include "trx0trx.h" |
| 32 |
|
| 33 |
namespace lob { |
| 34 |
|
| 35 |
void data_page_t::replace_inline(ulint offset, const byte *&ptr, ulint &want, |
| 36 |
mtr_t *mtr) { |
| 37 |
byte *old_ptr = data_begin() + offset; |
| 38 |
|
| 39 |
ulint data_len = get_data_len(); |
| 40 |
ut_ad(data_len > offset); |
| 41 |
|
| 42 |
/** Copy the new data to page. */ |
| 43 |
ulint data_avail = data_len - offset; |
| 44 |
ulint data_to_copy = want > data_avail ? data_avail : want; |
| 45 |
mlog_write_string(old_ptr, ptr, data_to_copy, mtr); |
| 46 |
|
| 47 |
ptr += data_to_copy; |
| 48 |
want -= data_to_copy; |
| 49 |
} |
| 50 |
|
| 51 |
/** Create a new data page and replace some or all parts of the old data |
| 52 |
with data. |
| 53 |
@param[in] trx Current transaction. |
| 54 |
@param[in] offset Offset where replace begins. |
| 55 |
@param[in,out] ptr Pointer to new data. |
| 56 |
@param[in] want Amount of data the caller wants to replace. |
| 57 |
@param[in] mtr Mini-transaction context. |
| 58 |
@return the buffer block of the new data page. */ |
| 59 |
buf_block_t *data_page_t::replace(trx_t *trx, ulint offset, const byte *&ptr, |
| 60 |
ulint &want, mtr_t *mtr) { |
| 61 |
ulint cur_data_len = get_data_len(); |
| 62 |
ut_a(offset > 0 || want < cur_data_len); |
| 63 |
buf_block_t *new_block = nullptr; |
| 64 |
|
| 65 |
/** Allocate a new data page. */ |
| 66 |
data_page_t new_page(mtr, m_index); |
| 67 |
new_block = new_page.alloc(mtr, false); |
| 68 |
|
| 69 |
if (new_block == nullptr) { |
| 70 |
return (nullptr); |
| 71 |
} |
| 72 |
|
| 73 |
byte *new_ptr = new_page.data_begin(); |
| 74 |
byte *old_ptr = data_begin(); |
| 75 |
|
| 76 |
DBUG_LOG("data_page_t", PrintBuffer(old_ptr, cur_data_len)); |
| 77 |
DBUG_LOG("data_page_t", "offset=" << offset << ", want=" << want); |
| 78 |
|
| 79 |
new_page.set_trx_id(trx->id); |
| 80 |
new_page.set_data_len(get_data_len()); |
| 81 |
|
| 82 |
/** Copy contents from old page to new page. */ |
| 83 |
mlog_write_string(new_ptr, old_ptr, offset, mtr); |
| 84 |
|
| 85 |
new_ptr += offset; |
| 86 |
old_ptr += offset; |
| 87 |
|
| 88 |
/** Copy the new data to new page. */ |
| 89 |
ulint data_avail = get_data_len() - offset; |
| 90 |
ulint data_to_copy = want > data_avail ? data_avail : want; |
| 91 |
mlog_write_string(new_ptr, ptr, data_to_copy, mtr); |
| 92 |
|
| 93 |
new_ptr += data_to_copy; |
| 94 |
old_ptr += data_to_copy; |
| 95 |
ptr += data_to_copy; |
| 96 |
|
| 97 |
/** Copy contents from old page to new page. */ |
| 98 |
if (want < data_avail) { |
| 99 |
ut_ad(data_to_copy == want); |
| 100 |
ulint remain = data_avail - want; |
| 101 |
mlog_write_string(new_ptr, old_ptr, remain, mtr); |
| 102 |
} |
| 103 |
|
| 104 |
want -= data_to_copy; |
| 105 |
|
| 106 |
DBUG_LOG("data_page_t", PrintBuffer(new_page.data_begin(), cur_data_len)); |
| 107 |
|
| 108 |
return (new_block); |
| 109 |
} |
| 110 |
|
| 111 |
/** Append given data in data page. |
| 112 |
@param[in] trxid transaction doing append. |
| 113 |
@param[in,out] data data to be appended. |
| 114 |
@param[in,out] len length of data. |
| 115 |
@return number of bytes appended. */ |
| 116 |
ulint data_page_t::append(trx_id_t trxid, byte *&data, ulint &len) { |
| 117 |
DBUG_TRACE; |
| 118 |
|
| 119 |
ulint old_data_len = get_data_len(); |
| 120 |
|
| 121 |
byte *ptr = data_begin() + old_data_len; |
| 122 |
ulint space_available = max_space_available() - old_data_len; |
| 123 |
|
| 124 |
if (space_available == 0 || len == 0) { |
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
ulint written = (len > space_available) ? space_available : len; |
| 129 |
|
| 130 |
mlog_write_string(ptr, data, written, m_mtr); |
| 131 |
|
| 132 |
set_data_len(old_data_len + written); |
| 133 |
set_trx_id(trxid); |
| 134 |
|
| 135 |
data += written; |
| 136 |
len -= written; |
| 137 |
|
| 138 |
return written; |
| 139 |
} |
| 140 |
|
| 141 |
ulint data_page_t::space_left() const { return (payload() - get_data_len()); } |
| 142 |
|
| 143 |
buf_block_t *data_page_t::alloc(mtr_t *alloc_mtr, bool is_bulk) { |
| 144 |
ut_ad(m_block == nullptr); |
| 145 |
ut_ad(m_index != nullptr); |
| 146 |
ut_ad(m_mtr != nullptr); |
| 147 |
ut_ad(alloc_mtr != nullptr); |
| 148 |
|
| 149 |
page_no_t hint = FIL_NULL; |
| 150 |
|
| 151 |
/* For testing purposes, pretend that the LOB page allocation failed.*/ |
| 152 |
DBUG_EXECUTE_IF("innodb_lob_data_page_alloc_failed", return (nullptr);); |
| 153 |
|
| 154 |
m_block = alloc_lob_page(m_index, alloc_mtr, hint, is_bulk); |
| 155 |
|
| 156 |
if (m_block == nullptr) { |
| 157 |
return (m_block); |
| 158 |
} |
| 159 |
|
| 160 |
set_page_type(); |
| 161 |
set_version_0(); |
| 162 |
set_next_page_null(); |
| 163 |
|
| 164 |
page_type_t type = fil_page_get_type(m_block->frame); |
| 165 |
ut_a(type == FIL_PAGE_TYPE_LOB_DATA); |
| 166 |
|
| 167 |
return (m_block); |
| 168 |
} |
| 169 |
|
| 170 |
ulint data_page_t::write(const byte *&data, ulint &len) { |
| 171 |
byte *ptr = data_begin(); |
| 172 |
ulint written = (len > payload()) ? payload() : len; |
| 173 |
|
| 174 |
/* Write data into page. */ |
| 175 |
mlog_write_string(ptr, data, written, m_mtr); |
| 176 |
set_data_len(written); |
| 177 |
|
| 178 |
data += written; |
| 179 |
len -= written; |
| 180 |
|
| 181 |
return (written); |
| 182 |
} |
| 183 |
|
| 184 |
buf_block_t *data_page_t::load_x(page_no_t page_no) { |
| 185 |
ut_ad(m_mtr != nullptr); |
| 186 |
ut_ad(m_index != nullptr); |
| 187 |
|
| 188 |
space_id_t space_id = dict_index_get_space(m_index); |
| 189 |
const page_id_t page_id(space_id, page_no); |
| 190 |
const page_size_t page_size = dict_table_page_size(m_index->table); |
| 191 |
|
| 192 |
m_block = |
| 193 |
buf_page_get(page_id, page_size, RW_X_LATCH, UT_LOCATION_HERE, m_mtr); |
| 194 |
return (m_block); |
| 195 |
} |
| 196 |
|
| 197 |
/** Read data from the data page. |
| 198 |
@param[in] offset read begins at this offset. |
| 199 |
@param[out] ptr the output buffer. |
| 200 |
@param[in] want bytes to read |
| 201 |
@return bytes actually read. */ |
| 202 |
ulint data_page_t::read(ulint offset, byte *ptr, ulint want) { |
| 203 |
DBUG_TRACE; |
| 204 |
|
| 205 |
byte *start = data_begin(); |
| 206 |
start += offset; |
| 207 |
ulint avail_data = get_data_len() - offset; |
| 208 |
|
| 209 |
ulint copy_len = want < avail_data ? want : avail_data; |
| 210 |
memcpy(ptr, start, copy_len); |
| 211 |
|
| 212 |
DBUG_LOG("lob", "page_no=" << get_page_no()); |
| 213 |
DBUG_LOG("lob", PrintBuffer(ptr, copy_len)); |
| 214 |
|
| 215 |
return copy_len; |
| 216 |
} |
| 217 |
|
| 218 |
} // namespace lob |