| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2014, 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 |
/** @file include/btr0load.h |
| 29 |
The B-tree bulk load |
| 30 |
|
| 31 |
Created 03/11/2014 Shaohua Wang |
| 32 |
*************************************************************************/ |
| 33 |
|
| 34 |
#ifndef btr0load_h |
| 35 |
#define btr0load_h |
| 36 |
|
| 37 |
#include <stddef.h> |
| 38 |
#include <vector> |
| 39 |
|
| 40 |
#include "dict0dict.h" |
| 41 |
#include "page0cur.h" |
| 42 |
#include "ut0class_life_cycle.h" |
| 43 |
#include "ut0new.h" |
| 44 |
|
| 45 |
// Forward declaration. |
| 46 |
class Page_load; |
| 47 |
|
| 48 |
/** @note We should call commit(false) for a Page_load object, which is not in |
| 49 |
m_page_loaders after page_commit, and we will commit or abort Page_load |
| 50 |
objects in function "finish". */ |
| 51 |
class Btree_load : private ut::Non_copyable { |
| 52 |
public: |
| 53 |
/** Interface to consume from. */ |
| 54 |
struct Cursor { |
| 55 |
/** Constructor. */ |
| 56 |
Cursor() = default; |
| 57 |
|
| 58 |
/** Destructor. */ |
| 59 |
virtual ~Cursor() = default; |
| 60 |
|
| 61 |
/** Fetch the current row as a tuple. |
| 62 |
@param[out] dtuple Row represented as a tuple. |
| 63 |
@return DB_SUCCESS, DB_END_OF_INDEX or error code. */ |
| 64 |
[[nodiscard]] virtual dberr_t fetch(dtuple_t *&dtuple) noexcept = 0; |
| 65 |
|
| 66 |
/** @return true if duplicates detected. */ |
| 67 |
virtual bool duplicates_detected() const noexcept = 0; |
| 68 |
|
| 69 |
/** Move to the next record. |
| 70 |
@return DB_SUCCESS, DB_END_OF_INDEX or error code. */ |
| 71 |
[[nodiscard]] virtual dberr_t next() noexcept = 0; |
| 72 |
}; |
| 73 |
|
| 74 |
public: |
| 75 |
using Page_loaders = std::vector<Page_load *, ut::allocator<Page_load *>>; |
| 76 |
|
| 77 |
/** Constructor |
| 78 |
@param[in] index B-tree index. |
| 79 |
@param[in] trx_id Transaction id. |
| 80 |
@param[in] observer Flush observer */ |
| 81 |
Btree_load(dict_index_t *index, trx_id_t trx_id, |
| 82 |
Flush_observer *observer) noexcept; |
| 83 |
|
| 84 |
/** Destructor */ |
| 85 |
~Btree_load() noexcept; |
| 86 |
|
| 87 |
/** Load the btree from the cursor. |
| 88 |
@param[in,out] cursor Cursor to read tuples from. |
| 89 |
@return DB_SUCCESS or error code. */ |
| 90 |
[[nodiscard]] dberr_t build(Cursor &cursor) noexcept; |
| 91 |
|
| 92 |
/** Btree bulk load finish. We commit the last page in each level |
| 93 |
and copy the last page in top level to the root page of the index |
| 94 |
if no error occurs. |
| 95 |
@param[in] err Whether bulk load was successful until now |
| 96 |
@return error code */ |
| 97 |
[[nodiscard]] dberr_t finish(dberr_t err) noexcept; |
| 98 |
|
| 99 |
/** Release latch on the rightmost leaf page in the index tree. */ |
| 100 |
void release() noexcept; |
| 101 |
|
| 102 |
/** Re-latch latch on the rightmost leaf page in the index tree. */ |
| 103 |
void latch() noexcept; |
| 104 |
|
| 105 |
/** Insert a tuple to a page in a level |
| 106 |
@param[in] dtuple Tuple to insert |
| 107 |
@param[in] level B-tree level |
| 108 |
@return error code */ |
| 109 |
[[nodiscard]] dberr_t insert(dtuple_t *dtuple, size_t level) noexcept; |
| 110 |
|
| 111 |
/** Public getter function to return number of records inserted |
| 112 |
@return Number of records inserted */ |
| 113 |
[[nodiscard]] inline uint64_t get_n_recs() noexcept { return m_n_recs; } |
| 114 |
|
| 115 |
private: |
| 116 |
/** Set the root page on completion. |
| 117 |
@param[in] last_page_no Last page number (the new root). |
| 118 |
@return DB_SUCCESS or error code. */ |
| 119 |
dberr_t load_root_page(page_no_t last_page_no) noexcept; |
| 120 |
|
| 121 |
/** Split a page |
| 122 |
@param[in] page_load Page to split |
| 123 |
@param[in] next_page_load Next page |
| 124 |
@return error code */ |
| 125 |
[[nodiscard]] dberr_t page_split(Page_load *page_load, |
| 126 |
Page_load *next_page_load) noexcept; |
| 127 |
|
| 128 |
/** Commit(finish) a page. We set next/prev page no, compress a page of |
| 129 |
compressed table and split the page if compression fails, insert a node |
| 130 |
pointer to father page if needed, and commit mini-transaction. |
| 131 |
@param[in] page_load Page to commit |
| 132 |
@param[in] next_page_load Next page |
| 133 |
@param[in] insert_father Flag whether need to insert node ptr |
| 134 |
@return error code */ |
| 135 |
[[nodiscard]] dberr_t page_commit(Page_load *page_load, |
| 136 |
Page_load *next_page_load, |
| 137 |
bool insert_father) noexcept; |
| 138 |
|
| 139 |
/** Prepare space to insert a tuple. |
| 140 |
@param[in,out] page_load Page bulk that will be used to store the record. |
| 141 |
It may be replaced if there is not enough space |
| 142 |
to hold the record. |
| 143 |
@param[in] level B-tree level |
| 144 |
@param[in] rec_size Record size |
| 145 |
@return error code */ |
| 146 |
[[nodiscard]] dberr_t prepare_space(Page_load *&page_load, size_t level, |
| 147 |
size_t rec_size) noexcept; |
| 148 |
|
| 149 |
/** Insert a tuple to a page. |
| 150 |
@param[in] page_load Page bulk object |
| 151 |
@param[in] tuple Tuple to insert |
| 152 |
@param[in] big_rec Big record vector, maybe NULL if there is no |
| 153 |
Data to be stored externally. |
| 154 |
@param[in] rec_size Record size |
| 155 |
@return error code */ |
| 156 |
[[nodiscard]] dberr_t insert(Page_load *page_load, dtuple_t *tuple, |
| 157 |
big_rec_t *big_rec, size_t rec_size) noexcept; |
| 158 |
|
| 159 |
/** Log free check */ |
| 160 |
void log_free_check() noexcept; |
| 161 |
|
| 162 |
/** Btree page bulk load finish. Commits the last page in each level |
| 163 |
if no error occurs. Also releases all page bulks. |
| 164 |
@param[in] err Whether bulk load was successful until now |
| 165 |
@param[out] last_page_no Last page number |
| 166 |
@return error code */ |
| 167 |
[[nodiscard]] dberr_t finalize_page_loads(dberr_t err, |
| 168 |
page_no_t &last_page_no) noexcept; |
| 169 |
|
| 170 |
private: |
| 171 |
/** Number of records inserted. */ |
| 172 |
uint64_t m_n_recs{}; |
| 173 |
|
| 174 |
/** B-tree index */ |
| 175 |
dict_index_t *m_index{}; |
| 176 |
|
| 177 |
/** Transaction id */ |
| 178 |
trx_id_t m_trx_id{}; |
| 179 |
|
| 180 |
/** Root page level */ |
| 181 |
size_t m_root_level{}; |
| 182 |
|
| 183 |
/** Flush observer */ |
| 184 |
Flush_observer *m_flush_observer{}; |
| 185 |
|
| 186 |
/** Page cursor vector for all level */ |
| 187 |
Page_loaders m_page_loaders{}; |
| 188 |
|
| 189 |
/** State of the index. Used for asserting at the end of a |
| 190 |
bulk load operation to ensure that the online status of the |
| 191 |
index does not change */ |
| 192 |
IF_DEBUG(unsigned m_index_online{};) |
| 193 |
}; |
| 194 |
|
| 195 |
#endif /* btr0load_h */ |