| 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 <zlib.h> |
| 29 | #include <algorithm> |
| 30 | #include <iterator> |
| 31 | #include <map> |
| 32 | #include <memory> |
| 33 | |
| 34 | #include "db0err.h" |
| 35 | #include "fut0fut.h" |
| 36 | #include "lot0lob.h" |
| 37 | #include "zlob0int.h" |
| 38 | |
| 39 | namespace zlob { |
| 40 | |
| 41 | using lob::ref_t; |
| 42 | |
| 43 | /** The input is divided into Z_CHUNK_SIZE -> this is for uncompressed |
| 44 | data. */ |
| 45 | const ulint Z_CHUNK_SIZE = KB128; |
| 46 | |
| 47 | /** Given the lob:ref_t object get the first page number of zlob. */ |
| 48 | static std::map<ref_t, page_no_t> g_zlob; |
| 49 | |
| 50 | /** Purge one index entry. |
| 51 | @param[in] trxid purging data belonging to trxid. |
| 52 | @param[in,out] lst the list from which this entry will be removed. |
| 53 | @param[in,out] free_list the list to which this entry will be added. */ |
| 54 | fil_addr_t z_index_entry_t::purge_version(trx_id_t trxid, z_first_page_t &first, |
| 55 | flst_base_node_t *lst, |
| 56 | flst_base_node_t *free_list) { |
| 57 | /* Save the location of next node. */ |
| 58 | fil_addr_t next_loc = get_next(); |
| 59 | |
| 60 | /* Remove the current node from the list it belongs. */ |
| 61 | remove(lst); |
| 62 | |
| 63 | /* Purge the current node. */ |
| 64 | purge(first); |
| 65 | |
| 66 | /* Add the current node to the free list. */ |
| 67 | push_front(free_list); |
| 68 | |
| 69 | /* Return the next node location. */ |
| 70 | return (next_loc); |
| 71 | } |
| 72 | |
| 73 | /** The current index entry points to a latest LOB page. It may or may |
| 74 | not have older versions. If older version is there, bring it back to the |
| 75 | index list from the versions list. Then remove the current entry from |
| 76 | the index list. Move the versions list from current entry to older entry. |
| 77 | @param[in] trxid The transaction identifier. |
| 78 | @param[in] first The first lob page containing index list and free list. */ |
| 79 | fil_addr_t z_index_entry_t::make_old_version_current(trx_id_t trxid, |
| 80 | z_first_page_t &first) { |
| 81 | flst_base_node_t *idx_flst = first.index_list(); |
| 82 | flst_base_node_t *free_list = first.free_list(); |
| 83 | flst_base_node_t *version_list = get_versions_list(); |
| 84 | |
| 85 | if (flst_get_len(version_list) > 0) { |
| 86 | /* Remove the old version from versions list. */ |
| 87 | fil_addr_t old_node_addr = flst_get_first(version_list); |
| 88 | flst_node_t *old_node = fut_get_ptr(old_node_addr); |
| 89 | flst_remove(version_list, old_node); |
| 90 | |
| 91 | /* Copy the version base node from current to old entry. */ |
| 92 | z_index_entry_t old_entry(old_node); |
| 93 | move_version_base_node(old_entry); |
| 94 | |
| 95 | insert_before(idx_flst, old_entry); |
| 96 | } |
| 97 | |
| 98 | fil_addr_t loc = purge_version(trxid, first, idx_flst, free_list); |
| 99 | |
| 100 | ut_ad(flst_validate(idx_flst)); |
| 101 | |
| 102 | return (loc); |
| 103 | } |
| 104 | |
| 105 | /** Purge the current index entry. An index entry points to either a FIRST |
| 106 | page or DATA page. That LOB page will be freed if it is DATA page. A FIRST |
| 107 | page should not be freed. */ |
| 108 | void z_index_entry_t::purge(z_first_page_t &first_arg) { |
| 109 | set_data_len(0); |
| 110 | |
| 111 | while (true) { |
| 112 | page_no_t page_no = get_z_page_no(); |
| 113 | if (page_no == FIL_NULL) { |
| 114 | break; |
| 115 | } |
| 116 | buf_block_t *block = buf_page_get(page_no); |
| 117 | page_type_t type = fil_page_get_type(block->m_frame); |
| 118 | page_no_t next = block->get_next_page(); |
| 119 | set_z_page_no(next); |
| 120 | |
| 121 | switch (type) { |
| 122 | case FIL_PAGE_TYPE_LOB_FIRST: { |
| 123 | z_first_page_t first(block); |
| 124 | first.set_data_len(0); |
| 125 | first.set_trx_id(0); |
| 126 | } break; |
| 127 | case FIL_PAGE_TYPE_ZLOB_DATA: |
| 128 | btr_page_free(block); |
| 129 | break; |
| 130 | case FIL_PAGE_TYPE_ZLOB_FRAG: { |
| 131 | z_frag_page_t frag_page(block); |
| 132 | frag_id_t fid = get_z_frag_id(); |
| 133 | frag_page.dealloc_fragment(fid); |
| 134 | if (frag_page.get_n_frags() == 0) { |
| 135 | frag_page.dealloc(first_arg); |
| 136 | } |
| 137 | } break; |
| 138 | default: |
| 139 | ut_ad(0); |
| 140 | } |
| 141 | |
| 142 | if (type == FIL_PAGE_TYPE_ZLOB_FRAG) { |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | init(); |
| 148 | } |
| 149 | |
| 150 | std::ostream &z_index_entry_t::print(std::ostream &out) const { |
| 151 | out << "[z_index_entry_t: m_node=" << (void *)m_node |
| 152 | << ", prev=" << get_prev() << ", next=" << get_next() |
| 153 | << ", versions=" << flst_bnode_t(get_versions_list()) |
| 154 | << ", trx_id=" << get_trx_id() << ", z_page_no=" << get_z_page_no() |
| 155 | << ", z_frag_id=" << get_z_frag_id() << ", data_len=" << get_data_len() |
| 156 | << ", zdata_len=" << get_zdata_len() << "]"; |
| 157 | print_pages(out); |
| 158 | return (out); |
| 159 | } |
| 160 | |
| 161 | std::ostream &z_index_entry_t::print_pages(std::ostream &out) const { |
| 162 | page_no_t page_no = get_z_page_no(); |
| 163 | |
| 164 | out << "[PAGES: "; |
| 165 | while (page_no != FIL_NULL) { |
| 166 | buf_block_t *block = buf_page_get(page_no); |
| 167 | ulint type = block->get_page_type(); |
| 168 | out << "[page_no=" << page_no << ", type=" << block->get_page_type_str() |
| 169 | << "]"; |
| 170 | page_no = block->get_next_page(); |
| 171 | if (type == FIL_PAGE_TYPE_ZLOB_FRAG) { |
| 172 | /* Reached the fragment page. Stop. */ |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | out << "]"; |
| 177 | return (out); |
| 178 | } |
| 179 | |
| 180 | std::ostream &z_frag_entry_t::print(std::ostream &out) const { |
| 181 | out << "[z_frag_entry_t: prev=" << get_prev() << ", next=" << get_next() |
| 182 | << ", page_no=" << get_page_no() << ", n_frags=" << get_n_frags() |
| 183 | << ", used_len=" << get_used_len() |
| 184 | << ", total_free_len=" << get_total_free_len() |
| 185 | << ", big_free_len=" << get_big_free_len() << "]"; |
| 186 | return (out); |
| 187 | } |
| 188 | |
| 189 | void z_frag_entry_t::purge(flst_base_node_t *used_lst, |
| 190 | flst_base_node_t *free_lst) { |
| 191 | remove(used_lst); |
| 192 | init(); |
| 193 | push_front(free_lst); |
| 194 | } |
| 195 | |
| 196 | void z_frag_entry_t::update(const z_frag_page_t &frag_page) { |
| 197 | set_page_no(frag_page.get_page_no()); |
| 198 | set_n_frags(frag_page.get_n_frags()); |
| 199 | set_used_len(frag_page.get_total_stored_data()); |
| 200 | set_total_free_len(frag_page.get_total_free_len()); |
| 201 | set_big_free_len(frag_page.get_big_free_len()); |
| 202 | } |
| 203 | |
| 204 | /** Print the index entries. */ |
| 205 | std::ostream &z_first_page_t::print_index_entries(std::ostream &out) const { |
| 206 | flst_base_node_t *flst = index_list(); |
| 207 | fil_addr_t node_loc = flst_get_first(flst); |
| 208 | |
| 209 | out << "Index Entries: " << flst_bnode_t(flst) << std::endl; |
| 210 | |
| 211 | while (!fil_addr_is_null(node_loc)) { |
| 212 | flst_node_t *node = fut_get_ptr(node_loc); |
| 213 | z_index_entry_t entry(node); |
| 214 | out << entry << std::endl; |
| 215 | node_loc = entry.get_next(); |
| 216 | } |
| 217 | |
| 218 | return (out); |
| 219 | } |
| 220 | |
| 221 | /** Print the frag entries. */ |
| 222 | std::ostream &z_first_page_t::print_frag_entries(std::ostream &out) const { |
| 223 | flst_base_node_t *flst = frag_list(); |
| 224 | fil_addr_t node_loc = flst_get_first(flst); |
| 225 | |
| 226 | out << "Frag Entries: " << flst_bnode_t(flst) << std::endl; |
| 227 | |
| 228 | while (!fil_addr_is_null(node_loc)) { |
| 229 | flst_node_t *node = fut_get_ptr(node_loc); |
| 230 | z_frag_entry_t entry(node); |
| 231 | out << entry << std::endl; |
| 232 | node_loc = entry.get_next(); |
| 233 | } |
| 234 | |
| 235 | return (out); |
| 236 | } |
| 237 | |
| 238 | /** Allocate one index entry. If there is no free index entry, allocate |
| 239 | an index page (a page full of z_index_entry_t objects) and service the |
| 240 | request. |
| 241 | @return the allocated index entry. */ |
| 242 | z_index_entry_t z_first_page_t::alloc_index_entry() { |
| 243 | flst_base_node_t *free_lst = free_list(); |
| 244 | fil_addr_t first_loc = flst_get_first(free_lst); |
| 245 | if (fil_addr_is_null(first_loc)) { |
| 246 | z_index_page_t page; |
| 247 | page.alloc(*this); |
| 248 | first_loc = flst_get_first(free_lst); |
| 249 | } |
| 250 | ut_ad(!fil_addr_is_null(first_loc)); |
| 251 | flst_node_t *first_ptr = fut_get_ptr(first_loc); |
| 252 | z_index_entry_t entry(first_ptr); |
| 253 | entry.remove(free_lst); |
| 254 | return (entry); |
| 255 | } |
| 256 | |
| 257 | /** Allocate one frag page entry. If there is no free frag entry, allocate |
| 258 | an frag node page (a page full of z_frag_entry_t objects) and service the |
| 259 | request. |
| 260 | @return the allocated frag entry. */ |
| 261 | z_frag_entry_t z_first_page_t::alloc_frag_entry() { |
| 262 | flst_base_node_t *free_lst = free_frag_list(); |
| 263 | flst_base_node_t *used_lst = frag_list(); |
| 264 | fil_addr_t first_loc = flst_get_first(free_lst); |
| 265 | if (fil_addr_is_null(first_loc)) { |
| 266 | z_frag_node_page_t page; |
| 267 | page.alloc(*this); |
| 268 | first_loc = flst_get_first(free_lst); |
| 269 | } |
| 270 | ut_ad(!fil_addr_is_null(first_loc)); |
| 271 | flst_node_t *first_ptr = fut_get_ptr(first_loc); |
| 272 | z_frag_entry_t entry(first_ptr); |
| 273 | entry.remove(free_lst); |
| 274 | entry.push_front(used_lst); |
| 275 | return (entry); |
| 276 | } |
| 277 | |
| 278 | /** Find a fragment page, that has atleast len free space. */ |
| 279 | z_frag_entry_t z_first_page_t::find_frag_page(ulint len) { |
| 280 | z_frag_entry_t entry; |
| 281 | |
| 282 | /* Make sure that there will be some extra space for page directory |
| 283 | entry and meta data. Adding a margin to provide for this. */ |
| 284 | const ulint look_size = len + frag_node_t::OFFSET_DATA + 10; |
| 285 | |
| 286 | flst_base_node_t *frag_lst = frag_list(); |
| 287 | /* Iterate through the list of frag entries in the page. */ |
| 288 | fil_addr_t loc = flst_get_first(frag_lst); |
| 289 | while (!fil_addr_is_null(loc)) { |
| 290 | flst_node_t *node = fut_get_ptr(loc); |
| 291 | entry.reset(node); |
| 292 | |
| 293 | if (entry.get_big_free_len() >= look_size) { |
| 294 | break; |
| 295 | } |
| 296 | |
| 297 | loc = flst_get_next_addr(node); |
| 298 | } |
| 299 | |
| 300 | if (fil_addr_is_null(loc)) { |
| 301 | /* Need to allocate a new fragment page. */ |
| 302 | z_frag_page_t frag_page; |
| 303 | frag_page.alloc(); |
| 304 | |
| 305 | entry = alloc_frag_entry(); |
| 306 | entry.set_page_no(frag_page.get_page_no()); |
| 307 | entry.update(frag_page); |
| 308 | frag_page.set_frag_entry(entry.get_self_addr()); |
| 309 | } |
| 310 | |
| 311 | return (entry); |
| 312 | } |
| 313 | |
| 314 | /** Print the page. */ |
| 315 | std::ostream &z_first_page_t::print(std::ostream &out) const { |
| 316 | print_index_entries(out); |
| 317 | print_frag_entries(out); |
| 318 | return (out); |
| 319 | } |
| 320 | |
| 321 | /** Free all the z_frag_page_t pages. All the z_frag_page_t pages are |
| 322 | singly linked to each other. The head of the list is maintained in the |
| 323 | first page. */ |
| 324 | void z_first_page_t::free_all_frag_node_pages() { |
| 325 | while (true) { |
| 326 | page_no_t page_no = get_frag_node_page_no(); |
| 327 | if (page_no == FIL_NULL) { |
| 328 | break; |
| 329 | } |
| 330 | z_frag_node_page_t frag_node_page; |
| 331 | frag_node_page.load(page_no); |
| 332 | page_no_t next_page = frag_node_page.get_next_page_no(); |
| 333 | set_frag_node_page_no(next_page); |
| 334 | frag_node_page.dealloc(); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /** Free all the index pages. */ |
| 339 | void z_first_page_t::free_all_index_pages() { |
| 340 | while (true) { |
| 341 | page_no_t page_no = get_index_page_no(); |
| 342 | if (page_no == FIL_NULL) { |
| 343 | break; |
| 344 | } |
| 345 | z_index_page_t index_page; |
| 346 | index_page.load(page_no); |
| 347 | page_no_t next_page = index_page.get_next_page_no(); |
| 348 | set_index_page_no(next_page); |
| 349 | index_page.dealloc(); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | std::pair<ulint, ulint> z_insert_strm(z_first_page_t &first, trx_id_t trxid, |
| 354 | byte *blob, ulint len) { |
| 355 | ulint remain = len; |
| 356 | page_no_t start_page_no = FIL_NULL; |
| 357 | frag_id_t frag_id = FRAG_ID_NULL; |
| 358 | page_no_t prev_page_no; |
| 359 | byte *lob_ptr = blob; |
| 360 | |
| 361 | /* If the first page is empty, then make use of it. */ |
| 362 | if (first.get_data_len() == 0) { |
| 363 | /* First page is unused. Use it. */ |
| 364 | byte *ptr = first.begin_data_ptr(); |
| 365 | ulint size = first.payload(); |
| 366 | |
| 367 | ulint to_copy = remain > size ? size : remain; |
| 368 | memcpy(ptr, lob_ptr, to_copy); |
| 369 | remain -= to_copy; |
| 370 | lob_ptr += to_copy; |
| 371 | |
| 372 | start_page_no = first.get_page_no(); |
| 373 | prev_page_no = start_page_no; |
| 374 | |
| 375 | first.set_data_len(to_copy); |
| 376 | first.set_trx_id(trxid); |
| 377 | |
| 378 | } else if (remain >= z_frag_page_t::max_payload()) { |
| 379 | /* Data cannot fit into a fragment page. Allocate a data page. */ |
| 380 | |
| 381 | z_data_page_t data_page; |
| 382 | data_page.alloc(); |
| 383 | |
| 384 | byte *ptr = data_page.begin_data_ptr(); |
| 385 | ulint size = data_page.payload(); |
| 386 | ulint to_copy = remain > size ? size : remain; |
| 387 | memcpy(ptr, lob_ptr, to_copy); |
| 388 | remain -= to_copy; |
| 389 | lob_ptr += to_copy; |
| 390 | |
| 391 | start_page_no = data_page.get_page_no(); |
| 392 | prev_page_no = start_page_no; |
| 393 | |
| 394 | data_page.set_data_len(to_copy); |
| 395 | data_page.set_trx_id(trxid); |
| 396 | |
| 397 | } else { |
| 398 | /* Data can fit into a fragment page. */ |
| 399 | z_frag_entry_t frag_entry = first.find_frag_page(remain); |
| 400 | z_frag_page_t frag_page; |
| 401 | frag_page.load(frag_entry.get_page_no()); |
| 402 | frag_id = frag_page.alloc_fragment(remain); |
| 403 | frag_node_t node = frag_page.get_frag_node(frag_id); |
| 404 | byte *ptr = node.frag_begin(); |
| 405 | |
| 406 | ut_ad(remain == node.payload()); |
| 407 | memcpy(ptr, lob_ptr, remain); |
| 408 | remain = 0; |
| 409 | lob_ptr += remain; |
| 410 | start_page_no = frag_page.get_page_no(); |
| 411 | |
| 412 | /* Update the frag entry. */ |
| 413 | frag_entry.update(frag_page); |
| 414 | |
| 415 | return (std::pair<ulint, ulint>(start_page_no, frag_id)); |
| 416 | } |
| 417 | |
| 418 | while (remain > 0 && remain >= z_frag_page_t::max_payload()) { |
| 419 | z_data_page_t data_page; |
| 420 | data_page.alloc(); |
| 421 | |
| 422 | byte *ptr = data_page.begin_data_ptr(); |
| 423 | ulint size = data_page.payload(); |
| 424 | ulint to_copy = remain > size ? size : remain; |
| 425 | memcpy(ptr, lob_ptr, to_copy); |
| 426 | remain -= to_copy; |
| 427 | lob_ptr += to_copy; |
| 428 | |
| 429 | data_page.set_data_len(to_copy); |
| 430 | data_page.set_trx_id(trxid); |
| 431 | |
| 432 | /* Get the previous page and update its next page. */ |
| 433 | buf_block_t *block = buf_page_get(prev_page_no); |
| 434 | block->set_next_page_no(data_page.get_page_no()); |
| 435 | |
| 436 | prev_page_no = data_page.get_page_no(); |
| 437 | } |
| 438 | |
| 439 | if (remain > 0) { |
| 440 | ut_ad(remain < z_frag_page_t::max_payload()); |
| 441 | ut_ad(frag_id == FRAG_ID_NULL); |
| 442 | z_frag_page_t frag_page; |
| 443 | z_frag_entry_t frag_entry; |
| 444 | |
| 445 | frag_entry = first.find_frag_page(remain); |
| 446 | ut_ad(frag_entry.get_big_free_len() >= remain); |
| 447 | |
| 448 | frag_page.load(frag_entry.get_page_no()); |
| 449 | ut_ad(frag_page.get_big_free_len() >= remain); |
| 450 | |
| 451 | frag_id = frag_page.alloc_fragment(remain); |
| 452 | ut_ad(frag_id != FRAG_ID_NULL); |
| 453 | |
| 454 | frag_node_t node = frag_page.get_frag_node(frag_id); |
| 455 | byte *ptr = node.frag_begin(); |
| 456 | |
| 457 | ulint pl = node.payload(); |
| 458 | ut_ad(remain == node.payload()); |
| 459 | ut_ad(remain == pl); |
| 460 | memcpy(ptr, lob_ptr, remain); |
| 461 | remain = 0; |
| 462 | lob_ptr += remain; |
| 463 | |
| 464 | /* Update the frag entry. */ |
| 465 | frag_entry.update(frag_page); |
| 466 | |
| 467 | /* Get the previous page and update its next page. */ |
| 468 | buf_block_t *block = buf_page_get(prev_page_no); |
| 469 | block->set_next_page_no(frag_page.get_page_no()); |
| 470 | } |
| 471 | |
| 472 | return (std::pair<ulint, ulint>(start_page_no, frag_id)); |
| 473 | } |
| 474 | |
| 475 | /** Insert one chunk of input. The maximum size of a chunk is Z_CHUNK_SIZE. |
| 476 | @param[in] blob the uncompressed LOB. |
| 477 | @param[out] out_entry the newly inserted index entry. can be NULL. |
| 478 | */ |
| 479 | dberr_t z_insert_chunk(z_first_page_t &first, trx_id_t trxid, ref_t ref, |
| 480 | byte *blob, ulint len, z_index_entry_t *out_entry) { |
| 481 | ut_ad(len <= Z_CHUNK_SIZE); |
| 482 | z_stream strm; |
| 483 | |
| 484 | strm.zalloc = nullptr; |
| 485 | strm.zfree = nullptr; |
| 486 | strm.opaque = nullptr; |
| 487 | |
| 488 | // int ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION); |
| 489 | int ret = deflateInit(&strm, Z_NO_COMPRESSION); |
| 490 | |
| 491 | ut_a(ret == Z_OK); |
| 492 | |
| 493 | strm.avail_in = len; |
| 494 | strm.next_in = blob; |
| 495 | |
| 496 | /* It is possible that the compressed stream is actually bigger. So |
| 497 | making use of this call to find it out for sure. */ |
| 498 | const ulint max_buf = deflateBound(&strm, len); |
| 499 | |
| 500 | /** @todo We should use mem_heap here. */ |
| 501 | std::unique_ptr<byte[]> tmpbuf(new byte[max_buf]); |
| 502 | strm.avail_out = max_buf; |
| 503 | strm.next_out = tmpbuf.get(); |
| 504 | |
| 505 | ret = deflate(&strm, Z_FINISH); |
| 506 | ut_a(ret == Z_STREAM_END); |
| 507 | ut_a(strm.avail_in == 0); |
| 508 | ut_a(strm.total_out == (max_buf - strm.avail_out)); |
| 509 | |
| 510 | std::pair<ulint, ulint> result = |
| 511 | z_insert_strm(first, trxid, tmpbuf.get(), strm.total_out); |
| 512 | |
| 513 | z_index_entry_t entry = first.alloc_index_entry(); |
| 514 | |
| 515 | entry.set_trx_id(trxid); |
| 516 | entry.set_z_page_no(result.first); |
| 517 | entry.set_z_frag_id(result.second); |
| 518 | entry.set_data_len(len); |
| 519 | entry.set_zdata_len(strm.total_out); |
| 520 | |
| 521 | deflateEnd(&strm); |
| 522 | |
| 523 | if (out_entry != nullptr) { |
| 524 | out_entry->reset(entry); |
| 525 | } |
| 526 | |
| 527 | return (DB_SUCCESS); |
| 528 | } |
| 529 | |
| 530 | dberr_t z_insert(trx_id_t trxid, ref_t ref, byte *blob, ulint len) { |
| 531 | ulint remain = len; |
| 532 | byte *ptr = blob; |
| 533 | dberr_t err(DB_SUCCESS); |
| 534 | |
| 535 | z_first_page_t first; |
| 536 | first.alloc(); |
| 537 | flst_base_node_t *idx_list = first.index_list(); |
| 538 | |
| 539 | while (remain > 0) { |
| 540 | z_index_entry_t entry; |
| 541 | ulint size = (remain >= Z_CHUNK_SIZE) ? Z_CHUNK_SIZE : remain; |
| 542 | err = z_insert_chunk(first, trxid, ref, ptr, size, &entry); |
| 543 | ptr += size; |
| 544 | remain -= size; |
| 545 | |
| 546 | entry.push_back(idx_list); |
| 547 | } |
| 548 | |
| 549 | g_zlob.insert(std::pair<ref_t, page_no_t>(ref, first.get_page_no())); |
| 550 | return (err); |
| 551 | } |
| 552 | |
| 553 | /** Read one zlib stream completed, given its index entry. |
| 554 | @param[in] entry the index entry. |
| 555 | @param[in,out] zbuf the output buffer |
| 556 | @param[in] zbuf_size the size of the output buffer. |
| 557 | @return the size of the zlib stream.*/ |
| 558 | ulint z_read_strm(z_index_entry_t &entry, byte *zbuf, ulint zbuf_size) { |
| 559 | page_no_t page_no = entry.get_z_page_no(); |
| 560 | byte *ptr = zbuf; |
| 561 | ulint remain = zbuf_size; |
| 562 | |
| 563 | while (remain > 0 && page_no != FIL_NULL) { |
| 564 | buf_block_t *block = buf_page_get(page_no); |
| 565 | ulint ptype = block->get_page_type(); |
| 566 | byte *data = nullptr; |
| 567 | ulint data_size = 0; |
| 568 | if (ptype == FIL_PAGE_TYPE_ZLOB_FRAG) { |
| 569 | frag_id_t fid = entry.get_z_frag_id(); |
| 570 | z_frag_page_t frag_page(block); |
| 571 | frag_node_t node = frag_page.get_frag_node(fid); |
| 572 | data = node.data_begin(); |
| 573 | data_size = node.payload(); |
| 574 | } else if (ptype == FIL_PAGE_TYPE_ZLOB_FIRST) { |
| 575 | z_first_page_t first(block); |
| 576 | data = first.begin_data_ptr(); |
| 577 | data_size = first.get_data_len(); |
| 578 | } else { |
| 579 | ut_a(ptype == FIL_PAGE_TYPE_ZLOB_DATA); |
| 580 | z_data_page_t data_page(block); |
| 581 | data = data_page.begin_data_ptr(); |
| 582 | data_size = data_page.get_data_len(); |
| 583 | ut_a(data_size <= data_page.payload()); |
| 584 | } |
| 585 | memcpy(ptr, data, data_size); |
| 586 | ptr += data_size; |
| 587 | ut_a(data_size <= remain); |
| 588 | remain -= data_size; |
| 589 | page_no = block->get_next_page(); |
| 590 | } |
| 591 | |
| 592 | ulint zbytes = (zbuf_size - remain); |
| 593 | return (zbytes); |
| 594 | } |
| 595 | |
| 596 | /** Read one data chunk associated with one index entry. |
| 597 | @param[in] trxid the transaction doing the read. |
| 598 | @param[in] entry the index entry |
| 599 | @param[in] offset the offset from which to read the chunk. |
| 600 | @param[in,out] len the length of the output buffer. This length can |
| 601 | be greater than the chunk size. |
| 602 | @param[in,out] buf the output buffer. |
| 603 | */ |
| 604 | ulint z_read_chunk(trx_id_t trxid, z_index_entry_t &entry, ulint offset, |
| 605 | ulint &len, byte *&buf) { |
| 606 | ut_ad(entry.can_see(trxid)); |
| 607 | |
| 608 | if (entry.get_z_page_no() == FIL_NULL || entry.get_data_len() == 0) { |
| 609 | return (0); |
| 610 | } |
| 611 | |
| 612 | ulint zbuf_size = entry.get_zdata_len(); |
| 613 | |
| 614 | /** @todo This might need to be converted to a mem_heap call. */ |
| 615 | std::unique_ptr<byte[]> zbuf(new byte[zbuf_size]); |
| 616 | |
| 617 | ulint zbytes = z_read_strm(entry, zbuf.get(), zbuf_size); |
| 618 | ut_a(zbytes == zbuf_size); |
| 619 | |
| 620 | z_stream strm; |
| 621 | strm.zalloc = nullptr; |
| 622 | strm.zfree = nullptr; |
| 623 | strm.opaque = nullptr; |
| 624 | |
| 625 | int ret = inflateInit(&strm); |
| 626 | ut_a(ret == Z_OK); |
| 627 | |
| 628 | strm.avail_in = zbytes; |
| 629 | strm.next_in = zbuf.get(); |
| 630 | |
| 631 | ulint to_copy = 0; |
| 632 | if (offset == 0 && len >= entry.get_data_len()) { |
| 633 | /* The full chunk is needed for output. */ |
| 634 | // strm.avail_out = entry.get_data_len(); |
| 635 | strm.avail_out = len; |
| 636 | strm.next_out = buf; |
| 637 | |
| 638 | ret = inflate(&strm, Z_FINISH); |
| 639 | ut_a(ret == Z_STREAM_END); |
| 640 | |
| 641 | to_copy = strm.total_out; |
| 642 | } else { |
| 643 | /* Only part of the chunk is needed. */ |
| 644 | byte ubuf[Z_CHUNK_SIZE]; |
| 645 | strm.avail_out = Z_CHUNK_SIZE; |
| 646 | strm.next_out = ubuf; |
| 647 | |
| 648 | ret = inflate(&strm, Z_FINISH); |
| 649 | ut_a(ret == Z_STREAM_END); |
| 650 | |
| 651 | ulint chunk_size = strm.total_out; |
| 652 | ut_a(chunk_size == entry.get_data_len()); |
| 653 | ut_a(offset < chunk_size); |
| 654 | |
| 655 | byte *ptr = ubuf + offset; |
| 656 | ulint remain = chunk_size - offset; |
| 657 | to_copy = len > remain ? remain : len; |
| 658 | memcpy(buf, ptr, to_copy); |
| 659 | } |
| 660 | |
| 661 | len -= to_copy; |
| 662 | buf += to_copy; |
| 663 | inflateEnd(&strm); |
| 664 | return (to_copy); |
| 665 | } |
| 666 | |
| 667 | /** Fetch a compressed large object (ZLOB) from the system. |
| 668 | @param[in] trxid the transaction that is doing the read. |
| 669 | @param[in] ref the LOB reference identifying the LOB. |
| 670 | @param[in] offset read the LOB from the given offset. |
| 671 | @param[in] len the length of LOB data that needs to be fetched. |
| 672 | @param[out] buf the output buffer (owned by caller) of minimum len bytes. |
| 673 | @return the amount of data (in bytes) that was actually read. */ |
| 674 | ulint z_read(trx_id_t trxid, ref_t ref, ulint offset, ulint len, byte *buf) { |
| 675 | auto it = g_zlob.find(ref); |
| 676 | |
| 677 | if (it == g_zlob.end()) { |
| 678 | return (0); |
| 679 | } |
| 680 | |
| 681 | /* The current entry - it is the latest version. */ |
| 682 | z_index_entry_t cur_entry; |
| 683 | |
| 684 | /* The older version of the current entry. */ |
| 685 | z_index_entry_t old_version; |
| 686 | |
| 687 | page_no_t first_page_no = it->second; |
| 688 | |
| 689 | z_first_page_t first; |
| 690 | first.load(first_page_no); |
| 691 | |
| 692 | flst_base_node_t *flst = first.index_list(); |
| 693 | fil_addr_t node_loc = flst_get_first(flst); |
| 694 | |
| 695 | /* The skip loop. */ |
| 696 | ulint skipped = 0; |
| 697 | while (!fil_addr_is_null(node_loc)) { |
| 698 | ulint will_skip = 0; |
| 699 | flst_node_t *node = fut_get_ptr(node_loc); |
| 700 | cur_entry.reset(node); |
| 701 | |
| 702 | if (cur_entry.can_see(trxid)) { |
| 703 | /* Can read the entry. */ |
| 704 | ulint data_len = cur_entry.get_data_len(); |
| 705 | will_skip = skipped + data_len; |
| 706 | } else { |
| 707 | /* Cannot read the entry. Look at older versions. */ |
| 708 | flst_base_node_t *ver_lst = cur_entry.get_versions_list(); |
| 709 | fil_addr_t old_node_loc = flst_get_first(ver_lst); |
| 710 | while (!fil_addr_is_null(old_node_loc)) { |
| 711 | flst_node_t *old_node = fut_get_ptr(old_node_loc); |
| 712 | old_version.reset(old_node); |
| 713 | if (old_version.can_see(trxid)) { |
| 714 | /* Can read the entry. */ |
| 715 | ulint data_len = old_version.get_data_len(); |
| 716 | will_skip = skipped + data_len; |
| 717 | break; |
| 718 | } |
| 719 | old_node_loc = old_version.get_next(); |
| 720 | old_version.reset(nullptr); |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | if (will_skip >= offset) { |
| 725 | /* Reached the entry containing the offset. */ |
| 726 | break; |
| 727 | } |
| 728 | |
| 729 | skipped = will_skip; |
| 730 | node_loc = cur_entry.get_next(); |
| 731 | cur_entry.reset(nullptr); |
| 732 | } // skip loop end |
| 733 | |
| 734 | ut_ad(skipped <= offset); |
| 735 | |
| 736 | if (old_version.is_null() && cur_entry.is_null()) { |
| 737 | return (0); |
| 738 | } |
| 739 | |
| 740 | ulint yet_to_skip = offset - skipped; |
| 741 | |
| 742 | ut_ad(yet_to_skip < Z_CHUNK_SIZE); |
| 743 | |
| 744 | byte *ptr = buf; |
| 745 | ulint remain = len; |
| 746 | |
| 747 | if (old_version.is_null()) { |
| 748 | /* We need to read the latest version entry. */ |
| 749 | if (cur_entry.can_see(trxid)) { |
| 750 | z_read_chunk(trxid, cur_entry, yet_to_skip, remain, ptr); |
| 751 | } |
| 752 | } else { |
| 753 | /* We need to read the old version entry. */ |
| 754 | if (old_version.can_see(trxid)) { |
| 755 | z_read_chunk(trxid, old_version, yet_to_skip, remain, ptr); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | node_loc = cur_entry.get_next(); |
| 760 | |
| 761 | while (remain > 0 && !fil_addr_is_null(node_loc)) { |
| 762 | flst_node_t *node = fut_get_ptr(node_loc); |
| 763 | cur_entry.reset(node); |
| 764 | |
| 765 | if (cur_entry.can_see(trxid)) { |
| 766 | /* Can read the entry. */ |
| 767 | z_read_chunk(trxid, cur_entry, 0, remain, ptr); |
| 768 | |
| 769 | } else { |
| 770 | /* Cannot read the entry. Look at older versions. */ |
| 771 | flst_base_node_t *ver_lst = cur_entry.get_versions_list(); |
| 772 | fil_addr_t old_node_loc = flst_get_first(ver_lst); |
| 773 | while (!fil_addr_is_null(old_node_loc)) { |
| 774 | flst_node_t *old_node = fut_get_ptr(old_node_loc); |
| 775 | old_version.reset(old_node); |
| 776 | if (old_version.can_see(trxid)) { |
| 777 | /* Can read the entry. */ |
| 778 | z_read_chunk(trxid, old_version, 0, remain, ptr); |
| 779 | break; |
| 780 | } |
| 781 | old_node_loc = old_version.get_next(); |
| 782 | old_version.reset(nullptr); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | node_loc = cur_entry.get_next(); |
| 787 | cur_entry.reset(nullptr); |
| 788 | } |
| 789 | |
| 790 | return (len - remain); |
| 791 | } |
| 792 | |
| 793 | dberr_t z_print_info(lob::ref_t ref, std::ostream &out) { |
| 794 | auto it = g_zlob.find(ref); |
| 795 | |
| 796 | if (it == g_zlob.end()) { |
| 797 | return (DB_FAIL); |
| 798 | } |
| 799 | |
| 800 | out << "Found zlob: " << ref << ", first=" << it->second << std::endl; |
| 801 | |
| 802 | z_first_page_t first; |
| 803 | first.load(it->second); |
| 804 | |
| 805 | first.print(out); |
| 806 | |
| 807 | return (DB_SUCCESS); |
| 808 | } |
| 809 | |
| 810 | /** Replace a large object (LOB) with the given new data. |
| 811 | @param[in] trxid the transaction that is doing the read. |
| 812 | @param[in] ref the LOB reference identifying the LOB. |
| 813 | @param[in] offset replace the LOB from the given offset. |
| 814 | @param[in] len the length of LOB data that needs to be replaced. |
| 815 | @param[in] buf the buffer (owned by caller) with new data (len bytes). |
| 816 | @return the actual number of bytes replaced. */ |
| 817 | ulint z_replace(trx_id_t trxid, lob::ref_t ref, ulint offset, ulint len, |
| 818 | byte *buf) { |
| 819 | auto it = g_zlob.find(ref); |
| 820 | |
| 821 | if (it == g_zlob.end()) { |
| 822 | return (0); |
| 823 | } |
| 824 | |
| 825 | /* The current entry - it is the latest version. */ |
| 826 | z_index_entry_t cur_entry; |
| 827 | |
| 828 | page_no_t first_page_no = it->second; |
| 829 | |
| 830 | z_first_page_t first; |
| 831 | first.load(first_page_no); |
| 832 | |
| 833 | flst_base_node_t *flst = first.index_list(); |
| 834 | fil_addr_t node_loc = flst_get_first(flst); |
| 835 | |
| 836 | /* The skip loop. */ |
| 837 | ulint skipped = 0; |
| 838 | while (!fil_addr_is_null(node_loc)) { |
| 839 | ulint will_skip = 0; |
| 840 | flst_node_t *node = fut_get_ptr(node_loc); |
| 841 | cur_entry.reset(node); |
| 842 | |
| 843 | if (cur_entry.can_see(trxid)) { |
| 844 | /* Can read the entry. */ |
| 845 | ulint data_len = cur_entry.get_data_len(); |
| 846 | will_skip = skipped + data_len; |
| 847 | } else { |
| 848 | /* Cannot modify this lob. */ |
| 849 | return (0); |
| 850 | } |
| 851 | |
| 852 | if (will_skip >= offset) { |
| 853 | /* Reached the entry containing the offset. */ |
| 854 | break; |
| 855 | } |
| 856 | |
| 857 | skipped = will_skip; |
| 858 | node_loc = cur_entry.get_next(); |
| 859 | cur_entry.reset(nullptr); |
| 860 | } // skip loop end |
| 861 | |
| 862 | ut_ad(skipped <= offset); |
| 863 | |
| 864 | if (cur_entry.is_null()) { |
| 865 | return (0); |
| 866 | } |
| 867 | |
| 868 | /* The cur_entry points to the chunk that needs to be partially replaced. */ |
| 869 | byte chunk[Z_CHUNK_SIZE]; |
| 870 | ulint yet_to_skip = offset - skipped; |
| 871 | ut_ad(yet_to_skip < Z_CHUNK_SIZE); |
| 872 | |
| 873 | byte *ptr = chunk; |
| 874 | ulint replace_len = len; /* bytes remaining to be replaced. */ |
| 875 | ulint read_length = cur_entry.get_data_len(); |
| 876 | ulint length_1 = z_read_chunk(trxid, cur_entry, 0, read_length, ptr); |
| 877 | ut_ad(length_1 == cur_entry.get_data_len()); |
| 878 | ut_ad(yet_to_skip <= length_1); |
| 879 | |
| 880 | byte *to_ptr = chunk + yet_to_skip; |
| 881 | byte *from_ptr = buf; |
| 882 | ulint remain = length_1 - yet_to_skip; |
| 883 | ulint can_be_replaced = remain > replace_len ? replace_len : remain; |
| 884 | memcpy(to_ptr, from_ptr, can_be_replaced); |
| 885 | from_ptr += can_be_replaced; |
| 886 | replace_len -= can_be_replaced; |
| 887 | |
| 888 | /* chunk now has the new data to be inserted. */ |
| 889 | z_index_entry_t new_entry; |
| 890 | z_insert_chunk(first, trxid, ref, chunk, length_1, &new_entry); |
| 891 | cur_entry.insert_after(flst, new_entry); |
| 892 | cur_entry.remove(flst); |
| 893 | new_entry.set_old_version(cur_entry); |
| 894 | |
| 895 | node_loc = new_entry.get_next(); |
| 896 | new_entry.reset(nullptr); |
| 897 | cur_entry.reset(nullptr); |
| 898 | |
| 899 | /* Replace remaining. */ |
| 900 | while (replace_len > 0 && !fil_addr_is_null(node_loc)) { |
| 901 | flst_node_t *node = fut_get_ptr(node_loc); |
| 902 | cur_entry.reset(node); /* the old entry. */ |
| 903 | |
| 904 | if (!cur_entry.can_see(trxid)) { |
| 905 | /** @todo Rollback the operation that has been done so far. */ |
| 906 | return (0); |
| 907 | } |
| 908 | |
| 909 | ulint size = cur_entry.get_data_len(); |
| 910 | |
| 911 | if (replace_len < size) { |
| 912 | /* Only partial chunk is to be replaced. Read old data. */ |
| 913 | ulint read_len = size; |
| 914 | ptr = chunk; |
| 915 | ulint len1 = z_read_chunk(trxid, cur_entry, 0, read_len, ptr); |
| 916 | ut_ad(len1 == cur_entry.get_data_len()); |
| 917 | ut_ad(read_len == 0); |
| 918 | ut_ad(len1 == size); |
| 919 | memcpy(chunk, from_ptr, replace_len); |
| 920 | |
| 921 | /* Chunk now contains new data to be inserted. */ |
| 922 | /** @todo if there was error, rollback must happen. */ |
| 923 | z_insert_chunk(first, trxid, ref, chunk, len1, &new_entry); |
| 924 | cur_entry.insert_after(flst, new_entry); |
| 925 | cur_entry.remove(flst); |
| 926 | new_entry.set_old_version(cur_entry); |
| 927 | |
| 928 | /* Replace Completed. */ |
| 929 | replace_len = 0; |
| 930 | break; |
| 931 | |
| 932 | } else { |
| 933 | /* Full chunk is to be replaced. No need to read old data. */ |
| 934 | /** @todo if there was error, rollback must happen. */ |
| 935 | z_insert_chunk(first, trxid, ref, from_ptr, size, &new_entry); |
| 936 | |
| 937 | ut_ad(new_entry.get_trx_id() == trxid); |
| 938 | |
| 939 | from_ptr += size; |
| 940 | ut_a(size <= replace_len); |
| 941 | replace_len -= size; |
| 942 | |
| 943 | fil_addr_t next_loc1 = cur_entry.get_next(); |
| 944 | cur_entry.insert_after(flst, new_entry); |
| 945 | cur_entry.remove(flst); |
| 946 | |
| 947 | fil_addr_t next_loc2 = new_entry.get_next(); |
| 948 | ut_ad(next_loc1.page == next_loc2.page && |
| 949 | next_loc1.boffset == next_loc2.boffset); |
| 950 | |
| 951 | new_entry.set_old_version(cur_entry); |
| 952 | node_loc = new_entry.get_next(); |
| 953 | } |
| 954 | |
| 955 | new_entry.reset(nullptr); |
| 956 | cur_entry.reset(nullptr); |
| 957 | } |
| 958 | |
| 959 | const ulint actual_replace = len - replace_len; |
| 960 | |
| 961 | return (actual_replace); |
| 962 | } |
| 963 | |
| 964 | /** Insert data into the middle of an LOB */ |
| 965 | ulint z_insert_middle(trx_id_t trxid, lob::ref_t ref, ulint offset, byte *data, |
| 966 | ulint len) { |
| 967 | auto it = g_zlob.find(ref); |
| 968 | |
| 969 | if (it == g_zlob.end()) { |
| 970 | return (0); |
| 971 | } |
| 972 | |
| 973 | /* The current entry - it is the latest version. */ |
| 974 | z_index_entry_t cur_entry; |
| 975 | |
| 976 | page_no_t first_page_no = it->second; |
| 977 | |
| 978 | z_first_page_t first; |
| 979 | first.load(first_page_no); |
| 980 | |
| 981 | flst_base_node_t *flst = first.index_list(); |
| 982 | fil_addr_t node_loc = flst_get_first(flst); |
| 983 | |
| 984 | /* The skip loop. */ |
| 985 | ulint skipped = 0; |
| 986 | while (!fil_addr_is_null(node_loc)) { |
| 987 | ulint will_skip = skipped; |
| 988 | flst_node_t *node = fut_get_ptr(node_loc); |
| 989 | cur_entry.reset(node); |
| 990 | |
| 991 | if (cur_entry.can_see(trxid)) { |
| 992 | /* Can read the entry. */ |
| 993 | ulint data_len = cur_entry.get_data_len(); |
| 994 | will_skip = skipped + data_len; |
| 995 | } else { |
| 996 | /* Cannot modify this lob. */ |
| 997 | return (0); |
| 998 | } |
| 999 | |
| 1000 | /** @todo this should be > and not >= */ |
| 1001 | if (will_skip >= offset) { |
| 1002 | /* Reached the entry containing the offset. */ |
| 1003 | break; |
| 1004 | } |
| 1005 | |
| 1006 | skipped = will_skip; |
| 1007 | node_loc = cur_entry.get_next(); |
| 1008 | cur_entry.reset(nullptr); |
| 1009 | } // skip loop end |
| 1010 | |
| 1011 | ut_ad(skipped <= offset); |
| 1012 | |
| 1013 | if (cur_entry.is_null()) { |
| 1014 | return (0); |
| 1015 | } |
| 1016 | |
| 1017 | byte chunk[Z_CHUNK_SIZE]; |
| 1018 | byte saved[Z_CHUNK_SIZE]; |
| 1019 | ulint saved_len = 0; |
| 1020 | byte *insert_ptr = data; |
| 1021 | ulint insert_len = len; |
| 1022 | bool saved_data = false; |
| 1023 | bool insert_before = false; |
| 1024 | |
| 1025 | if (skipped < offset) { |
| 1026 | /* Inserting in the middle of the chunk. */ |
| 1027 | byte *ptr = chunk; |
| 1028 | ulint read_len = cur_entry.get_data_len(); |
| 1029 | ulint len1 = z_read_chunk(trxid, cur_entry, 0, read_len, ptr); |
| 1030 | ut_ad(read_len == 0); |
| 1031 | ut_ad(len1 == cur_entry.get_data_len()); |
| 1032 | |
| 1033 | ptr = chunk; |
| 1034 | ulint yet_to_skip = offset - skipped; |
| 1035 | saved_len = len1 - yet_to_skip; |
| 1036 | memcpy(saved, ptr + yet_to_skip, saved_len); |
| 1037 | saved_data = true; |
| 1038 | |
| 1039 | ulint can_insert = Z_CHUNK_SIZE - yet_to_skip; |
| 1040 | if (insert_len < can_insert) { |
| 1041 | /* If the requested length to be inserted is smaller. */ |
| 1042 | can_insert = insert_len; |
| 1043 | } |
| 1044 | memcpy(ptr + yet_to_skip, insert_ptr, can_insert); |
| 1045 | insert_ptr += can_insert; |
| 1046 | ut_ad(insert_len >= can_insert); |
| 1047 | insert_len -= can_insert; |
| 1048 | |
| 1049 | ulint chunk_size = yet_to_skip + can_insert; |
| 1050 | z_index_entry_t entry; |
| 1051 | z_insert_chunk(first, trxid, ref, ptr, chunk_size, &entry); |
| 1052 | cur_entry.insert_after(flst, entry); |
| 1053 | cur_entry.remove(flst); |
| 1054 | entry.set_old_version(cur_entry); |
| 1055 | |
| 1056 | cur_entry.reset(entry); |
| 1057 | } else { |
| 1058 | ut_ad(skipped == offset); |
| 1059 | insert_before = true; |
| 1060 | } |
| 1061 | |
| 1062 | while (insert_len > 0) { |
| 1063 | z_index_entry_t entry; |
| 1064 | ulint size = (insert_len >= Z_CHUNK_SIZE) ? Z_CHUNK_SIZE : insert_len; |
| 1065 | |
| 1066 | if (size < Z_CHUNK_SIZE && saved_data) { |
| 1067 | break; |
| 1068 | } |
| 1069 | |
| 1070 | z_insert_chunk(first, trxid, ref, insert_ptr, size, &entry); |
| 1071 | insert_ptr += size; |
| 1072 | |
| 1073 | ut_ad(insert_len >= size); |
| 1074 | insert_len -= size; |
| 1075 | |
| 1076 | if (insert_before) { |
| 1077 | cur_entry.insert_before(flst, entry); |
| 1078 | } else { |
| 1079 | cur_entry.insert_after(flst, entry); |
| 1080 | cur_entry.reset(entry); |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | if (saved_data) { |
| 1085 | ut_ad(insert_len < Z_CHUNK_SIZE); |
| 1086 | byte *ptr = chunk; |
| 1087 | memcpy(ptr, insert_ptr, insert_len); |
| 1088 | ulint remain_space = Z_CHUNK_SIZE - insert_len; |
| 1089 | ulint to_copy = saved_len > remain_space ? remain_space : saved_len; |
| 1090 | |
| 1091 | byte *saved_ptr = saved; |
| 1092 | memcpy(ptr + insert_len, saved_ptr, to_copy); |
| 1093 | |
| 1094 | z_index_entry_t entry; |
| 1095 | z_insert_chunk(first, trxid, ref, ptr, insert_len + to_copy, &entry); |
| 1096 | |
| 1097 | cur_entry.insert_after(flst, entry); |
| 1098 | cur_entry.reset(entry); |
| 1099 | |
| 1100 | if (to_copy < saved_len) { |
| 1101 | ulint saved_remain = saved_len - to_copy; |
| 1102 | z_insert_chunk(first, trxid, ref, saved_ptr + to_copy, saved_remain, |
| 1103 | &entry); |
| 1104 | |
| 1105 | cur_entry.insert_after(flst, entry); |
| 1106 | cur_entry.reset(entry); |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | return (DB_SUCCESS); |
| 1111 | } |
| 1112 | |
| 1113 | /** Delete a portion of the given large object (LOB) |
| 1114 | @param[in] trxid the transaction that is doing the read. |
| 1115 | @param[in] ref the LOB reference identifying the LOB. |
| 1116 | @param[in] offset remove the LOB from the given offset. |
| 1117 | @param[in] len the length of LOB data that needs to be removed. |
| 1118 | @return actual number of bytes removed. */ |
| 1119 | ulint z_remove_middle(trx_id_t trxid, lob::ref_t ref, ulint offset, ulint len) { |
| 1120 | auto it = g_zlob.find(ref); |
| 1121 | |
| 1122 | if (it == g_zlob.end()) { |
| 1123 | return (0); |
| 1124 | } |
| 1125 | |
| 1126 | /* The current entry - it is the latest version. */ |
| 1127 | z_index_entry_t cur_entry; |
| 1128 | |
| 1129 | page_no_t first_page_no = it->second; |
| 1130 | |
| 1131 | z_first_page_t first; |
| 1132 | first.load(first_page_no); |
| 1133 | |
| 1134 | flst_base_node_t *flst = first.index_list(); |
| 1135 | fil_addr_t node_loc = flst_get_first(flst); |
| 1136 | |
| 1137 | /* The skip loop. */ |
| 1138 | ulint skipped = 0; |
| 1139 | while (!fil_addr_is_null(node_loc)) { |
| 1140 | ulint will_skip = skipped; |
| 1141 | flst_node_t *node = fut_get_ptr(node_loc); |
| 1142 | cur_entry.reset(node); |
| 1143 | |
| 1144 | if (cur_entry.can_see(trxid)) { |
| 1145 | /* Can read the entry. */ |
| 1146 | ulint data_len = cur_entry.get_data_len(); |
| 1147 | will_skip = skipped + data_len; |
| 1148 | } else { |
| 1149 | /* Cannot modify this lob. */ |
| 1150 | return (0); |
| 1151 | } |
| 1152 | |
| 1153 | if (will_skip > offset) { |
| 1154 | /* Reached the entry containing the offset. */ |
| 1155 | break; |
| 1156 | } |
| 1157 | |
| 1158 | skipped = will_skip; |
| 1159 | node_loc = cur_entry.get_next(); |
| 1160 | cur_entry.reset(nullptr); |
| 1161 | } // skip loop end |
| 1162 | |
| 1163 | ut_ad(skipped <= offset); |
| 1164 | |
| 1165 | if (cur_entry.is_null()) { |
| 1166 | return (0); |
| 1167 | } |
| 1168 | |
| 1169 | ulint remove_len = len; |
| 1170 | |
| 1171 | if (skipped < offset) { |
| 1172 | /* Partial data of current chunk needs to be removed. */ |
| 1173 | byte chunk[Z_CHUNK_SIZE]; |
| 1174 | byte *ptr = chunk; |
| 1175 | ulint read_len = cur_entry.get_data_len(); |
| 1176 | ulint len1 = z_read_chunk(trxid, cur_entry, 0, read_len, ptr); |
| 1177 | ut_ad(read_len == 0); |
| 1178 | ut_ad(len1 == cur_entry.get_data_len()); |
| 1179 | |
| 1180 | ulint yet_to_skip = offset - skipped; |
| 1181 | ut_ad(len1 > yet_to_skip); |
| 1182 | |
| 1183 | ulint can_remove = len1 - yet_to_skip; |
| 1184 | |
| 1185 | ulint to_remove = can_remove < remove_len ? can_remove : remove_len; |
| 1186 | |
| 1187 | z_index_entry_t new_entry; |
| 1188 | |
| 1189 | ptr = chunk; |
| 1190 | byte *to = ptr + yet_to_skip; |
| 1191 | byte *from = ptr + yet_to_skip + to_remove; |
| 1192 | ulint n = len1 - yet_to_skip - to_remove; |
| 1193 | memmove(to, from, n); |
| 1194 | |
| 1195 | z_insert_chunk(first, trxid, ref, chunk, (len1 - to_remove), &new_entry); |
| 1196 | cur_entry.insert_after(flst, new_entry); |
| 1197 | cur_entry.remove(flst); |
| 1198 | new_entry.set_old_version(cur_entry); |
| 1199 | |
| 1200 | remove_len -= to_remove; |
| 1201 | node_loc = new_entry.get_next(); |
| 1202 | } |
| 1203 | |
| 1204 | while (remove_len > 0 && !fil_addr_is_null(node_loc)) { |
| 1205 | flst_node_t *node = fut_get_ptr(node_loc); |
| 1206 | cur_entry.reset(node); |
| 1207 | |
| 1208 | ulint read_len = cur_entry.get_data_len(); |
| 1209 | |
| 1210 | if (remove_len < read_len) { |
| 1211 | break; |
| 1212 | } |
| 1213 | |
| 1214 | /* Full chunk is to be removed. So just add a dummy entry. */ |
| 1215 | z_index_entry_t new_entry = first.alloc_index_entry(); |
| 1216 | new_entry.init(); |
| 1217 | new_entry.set_trx_id(trxid); |
| 1218 | cur_entry.insert_after(flst, new_entry); |
| 1219 | cur_entry.remove(flst); |
| 1220 | new_entry.set_old_version(cur_entry); |
| 1221 | |
| 1222 | node_loc = new_entry.get_next(); |
| 1223 | |
| 1224 | remove_len -= read_len; |
| 1225 | } |
| 1226 | |
| 1227 | if (fil_addr_is_null(node_loc)) { |
| 1228 | return (len - remove_len); |
| 1229 | } |
| 1230 | |
| 1231 | if (remove_len > 0) { |
| 1232 | /* Partial data of current chunk needs to be removed. */ |
| 1233 | byte chunk[Z_CHUNK_SIZE]; |
| 1234 | byte *ptr = chunk; |
| 1235 | ulint read_len = cur_entry.get_data_len(); |
| 1236 | ulint len1 = z_read_chunk(trxid, cur_entry, 0, read_len, ptr); |
| 1237 | ut_ad(read_len == 0); |
| 1238 | ut_ad(len1 == cur_entry.get_data_len()); |
| 1239 | |
| 1240 | ulint can_remove = len1; |
| 1241 | ulint to_remove = remove_len > can_remove ? can_remove : remove_len; |
| 1242 | |
| 1243 | z_index_entry_t new_entry; |
| 1244 | |
| 1245 | ptr = &chunk[to_remove]; |
| 1246 | z_insert_chunk(first, trxid, ref, ptr, (len1 - to_remove), &new_entry); |
| 1247 | cur_entry.insert_after(flst, new_entry); |
| 1248 | cur_entry.remove(flst); |
| 1249 | new_entry.set_old_version(cur_entry); |
| 1250 | |
| 1251 | remove_len -= to_remove; |
| 1252 | } |
| 1253 | |
| 1254 | return (len - remove_len); |
| 1255 | } |
| 1256 | |
| 1257 | void z_purge(trx_id_t trxid, lob::ref_t ref) { |
| 1258 | auto it = g_zlob.find(ref); |
| 1259 | |
| 1260 | if (it == g_zlob.end()) { |
| 1261 | return; |
| 1262 | } |
| 1263 | |
| 1264 | /* The current entry - it is the latest version. */ |
| 1265 | z_index_entry_t cur_entry; |
| 1266 | |
| 1267 | page_no_t first_page_no = it->second; |
| 1268 | |
| 1269 | z_first_page_t first; |
| 1270 | first.load(first_page_no); |
| 1271 | |
| 1272 | flst_base_node_t *flst = first.index_list(); |
| 1273 | flst_base_node_t *free_list = first.free_list(); |
| 1274 | fil_addr_t node_loc = flst_get_first(flst); |
| 1275 | |
| 1276 | while (!fil_addr_is_null(node_loc)) { |
| 1277 | flst_node_t *node = fut_get_ptr(node_loc); |
| 1278 | cur_entry.reset(node); |
| 1279 | |
| 1280 | flst_base_node_t *vers = cur_entry.get_versions_list(); |
| 1281 | fil_addr_t ver_loc = flst_get_first(vers); |
| 1282 | |
| 1283 | /* Scan the older versions. */ |
| 1284 | while (!fil_addr_is_null(ver_loc)) { |
| 1285 | flst_node_t *ver_node = fut_get_ptr(ver_loc); |
| 1286 | z_index_entry_t vers_entry(ver_node); |
| 1287 | if (vers_entry.can_be_purged(trxid)) { |
| 1288 | ver_loc = vers_entry.purge_version(trxid, first, vers, free_list); |
| 1289 | } else { |
| 1290 | ver_loc = vers_entry.get_next(); |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | /* Now process the current entry. */ |
| 1295 | if (cur_entry.can_be_purged(trxid)) { |
| 1296 | node_loc = cur_entry.make_old_version_current(trxid, first); |
| 1297 | } else { |
| 1298 | node_loc = cur_entry.get_next(); |
| 1299 | } |
| 1300 | |
| 1301 | cur_entry.reset(nullptr); |
| 1302 | } |
| 1303 | |
| 1304 | if (first.empty()) { |
| 1305 | first.free_all_frag_node_pages(); |
| 1306 | first.free_all_index_pages(); |
| 1307 | first.dealloc(); |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | /** Allocate the fragment page. |
| 1312 | @return the allocated buffer block. */ |
| 1313 | buf_block_t *z_frag_page_t::alloc() { |
| 1314 | m_block = btr_page_alloc(); |
| 1315 | |
| 1316 | /* Set page type to FIL_PAGE_TYPE_ZLOB_FRAG. */ |
| 1317 | set_page_type(); |
| 1318 | set_page_next(FIL_NULL); |
| 1319 | |
| 1320 | set_frag_entry_null(); |
| 1321 | |
| 1322 | /* Initialize the frag free list. */ |
| 1323 | plist_base_node_t fl = free_list(); |
| 1324 | fl.init(); |
| 1325 | |
| 1326 | /* Initialize the used frag list. */ |
| 1327 | plist_base_node_t frag_lst = frag_list(); |
| 1328 | frag_lst.init(); |
| 1329 | |
| 1330 | byte *f = frame(); |
| 1331 | |
| 1332 | /* Add the available space as free frag to free list. */ |
| 1333 | frag_node_t frag(f, f + OFFSET_FRAGS_BEGIN, payload()); |
| 1334 | fl.push_front(frag.m_node); |
| 1335 | frag.set_frag_id_null(); |
| 1336 | |
| 1337 | return (m_block); |
| 1338 | } |
| 1339 | |
| 1340 | z_frag_entry_t z_frag_page_t::get_frag_entry() { |
| 1341 | fil_addr_t node_loc = get_frag_entry_addr(); |
| 1342 | flst_node_t *node = fut_get_ptr(node_loc); |
| 1343 | z_frag_entry_t entry(node); |
| 1344 | ut_ad(entry.get_page_no() == get_page_no()); |
| 1345 | return (entry); |
| 1346 | } |
| 1347 | |
| 1348 | void z_frag_page_t::dealloc(z_first_page_t &first) { |
| 1349 | ut_ad(get_n_frags() == 0); |
| 1350 | z_frag_entry_t entry = get_frag_entry(); |
| 1351 | entry.purge(first.frag_list(), first.free_frag_list()); |
| 1352 | btr_page_free(m_block); |
| 1353 | m_block = nullptr; |
| 1354 | } |
| 1355 | |
| 1356 | std::ostream &z_frag_page_t::print_frags_in_order(std::ostream &out) const { |
| 1357 | if (m_block == nullptr) { |
| 1358 | return (out); |
| 1359 | } |
| 1360 | plist_base_node_t free_lst = free_list(); |
| 1361 | plist_base_node_t frag_lst = frag_list(); |
| 1362 | |
| 1363 | out << "[Free List: " << free_lst << "]" << std::endl; |
| 1364 | out << "[Frag List: " << frag_lst << "]" << std::endl; |
| 1365 | |
| 1366 | frag_node_t cur_free = free_lst.get_first_node(); |
| 1367 | frag_node_t cur_frag = frag_lst.get_first_node(); |
| 1368 | |
| 1369 | while (!cur_free.is_null() && !cur_frag.is_null()) { |
| 1370 | if (cur_free.is_before(cur_frag)) { |
| 1371 | out << "F: " << cur_free << std::endl; |
| 1372 | cur_free = cur_free.get_next_node(); |
| 1373 | } else { |
| 1374 | out << "U: " << cur_frag << std::endl; |
| 1375 | cur_frag = cur_frag.get_next_node(); |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | if (cur_free.is_null()) { |
| 1380 | while (!cur_frag.is_null()) { |
| 1381 | out << "U: " << cur_frag << std::endl; |
| 1382 | cur_frag = cur_frag.get_next_node(); |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | if (cur_frag.is_null()) { |
| 1387 | while (!cur_free.is_null()) { |
| 1388 | out << "F: " << cur_free << std::endl; |
| 1389 | cur_free = cur_free.get_next_node(); |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | return (out); |
| 1394 | } |
| 1395 | |
| 1396 | /** Get the total amount of stored data in this page. */ |
| 1397 | ulint z_frag_page_t::get_total_stored_data() const { |
| 1398 | ulint len = 0; |
| 1399 | |
| 1400 | ut_ad(m_block != nullptr); |
| 1401 | |
| 1402 | plist_base_node_t frag_lst = frag_list(); |
| 1403 | |
| 1404 | for (plist_node_t cur = frag_lst.get_first_node(); !cur.is_null(); |
| 1405 | cur = cur.get_next_node()) { |
| 1406 | frag_node_t frag(cur); |
| 1407 | len += frag.payload(); |
| 1408 | } |
| 1409 | |
| 1410 | return (len); |
| 1411 | } |
| 1412 | |
| 1413 | /** Get the total cumulative free space in this page. */ |
| 1414 | ulint z_frag_page_t::get_total_free_len() const { |
| 1415 | ulint len = 0; |
| 1416 | |
| 1417 | ut_ad(m_block != nullptr); |
| 1418 | |
| 1419 | plist_base_node_t free_lst = free_list(); |
| 1420 | for (plist_node_t cur = free_lst.get_first_node(); !cur.is_null(); |
| 1421 | cur = cur.get_next_node()) { |
| 1422 | frag_node_t frag(cur); |
| 1423 | len += frag.payload(); |
| 1424 | } |
| 1425 | return (len); |
| 1426 | } |
| 1427 | |
| 1428 | /** Get the big free space in this page. */ |
| 1429 | ulint z_frag_page_t::get_big_free_len() const { |
| 1430 | ulint big = 0; |
| 1431 | |
| 1432 | ut_ad(m_block != nullptr); |
| 1433 | |
| 1434 | plist_base_node_t free_lst = free_list(); |
| 1435 | for (plist_node_t cur = free_lst.get_first_node(); !cur.is_null(); |
| 1436 | cur = cur.get_next_node()) { |
| 1437 | frag_node_t frag(cur); |
| 1438 | ulint payload = frag.payload(); |
| 1439 | if (payload > big) { |
| 1440 | big = payload; |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | return (big); |
| 1445 | } |
| 1446 | |
| 1447 | /** Deallocate all the free slots from the end of the page directory. */ |
| 1448 | void z_frag_page_t::dealloc_frag_id() { |
| 1449 | plist_base_node_t free_lst = free_list(); |
| 1450 | plist_node_t last = free_lst.get_last_node(); |
| 1451 | frag_node_t frag(last); |
| 1452 | /* The last free fragment must be adjacent to the directory. |
| 1453 | Then only it can take space from one slot. */ |
| 1454 | if (frag.end_ptr() != slots_end_ptr()) { |
| 1455 | return; |
| 1456 | } |
| 1457 | |
| 1458 | ulint frag_id = get_n_dir_entries() - 1; |
| 1459 | paddr_t addr = frag_id_to_addr(frag_id); |
| 1460 | while (addr == 0) { |
| 1461 | frag.incr_length_by_2(); |
| 1462 | decr_n_dir_entries(); |
| 1463 | if (frag_id == 0) { |
| 1464 | break; |
| 1465 | } |
| 1466 | frag_id--; |
| 1467 | addr = frag_id_to_addr(frag_id); |
| 1468 | } |
| 1469 | } |
| 1470 | } // namespace zlob |