MySQL 26.7.0
Source Code Documentation
btr0load.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2014, 2026, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 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.
46class Page_load;
47
48/** @note We should call commit(false) for a Page_load object, which is not in
49m_page_loaders after page_commit, and we will commit or abort Page_load
50objects in function "finish". */
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:
78
79 /** Constructor
80 @param[in] index B-tree index.
81 @param[in] trx_id Transaction id.
82 @param[in] observer Flush observer */
84 Flush_observer *observer) noexcept;
85
86 /** Destructor */
88
89 /** Load the btree from the cursor.
90 @param[in,out] cursor Cursor to read tuples from.
91 @return DB_SUCCESS or error code. */
92 [[nodiscard]] dberr_t build(Cursor &cursor) noexcept;
93
94 /** Btree bulk load finish. We commit the last page in each level
95 and copy the last page in top level to the root page of the index
96 if no error occurs.
97 @param[in] err Whether bulk load was successful until now
98 @return error code */
99 [[nodiscard]] dberr_t finish(dberr_t err) noexcept;
100
101 /** Release latch on the rightmost leaf page in the index tree. */
102 void release() noexcept;
103
104 /** Re-latch latch on the rightmost leaf page in the index tree. */
105 void latch() noexcept;
106
107 /** Insert a tuple to a page in a level
108 @param[in] dtuple Tuple to insert
109 @param[in] level B-tree level
110 @return error code */
111 [[nodiscard]] dberr_t insert(dtuple_t *dtuple, size_t level) noexcept;
112
113 /** Public getter function to return number of records inserted
114 @return Number of records inserted */
115 [[nodiscard]] inline uint64_t get_n_recs() noexcept { return m_n_recs; }
116
117 private:
118 /** Set the root page on completion.
119 @param[in] last_page_no Last page number (the new root).
120 @return DB_SUCCESS or error code. */
121 dberr_t load_root_page(page_no_t last_page_no) noexcept;
122
123 /** Split a page
124 @param[in] page_load Page to split
125 @param[in] next_page_load Next page
126 @return error code */
127 [[nodiscard]] dberr_t page_split(Page_load *page_load,
128 Page_load *next_page_load) noexcept;
129
130 /** Commit(finish) a page. We set next/prev page no, compress a page of
131 compressed table and split the page if compression fails, insert a node
132 pointer to father page if needed, and commit mini-transaction.
133 @param[in] page_load Page to commit
134 @param[in] next_page_load Next page
135 @param[in] insert_father Flag whether need to insert node ptr
136 @return error code */
137 [[nodiscard]] dberr_t page_commit(Page_load *page_load,
138 Page_load *next_page_load,
139 bool insert_father) noexcept;
140
141 /** Prepare space to insert a tuple.
142 @param[in,out] page_load Page bulk that will be used to store the record.
143 It may be replaced if there is not enough space
144 to hold the record.
145 @param[in] level B-tree level
146 @param[in] rec_size Record size
147 @return error code */
148 [[nodiscard]] dberr_t prepare_space(Page_load *&page_load, size_t level,
149 size_t rec_size) noexcept;
150
151 /** Insert a tuple to a page.
152 @param[in] page_load Page bulk object
153 @param[in] tuple Tuple to insert
154 @param[in] big_rec Big record vector, maybe NULL if there is no
155 Data to be stored externally.
156 @param[in] rec_size Record size
157 @return error code */
158 [[nodiscard]] dberr_t insert(Page_load *page_load, dtuple_t *tuple,
159 big_rec_t *big_rec, size_t rec_size) noexcept;
160
161 /** Log free check */
163
164 /** Btree page bulk load finish. Commits the last page in each level
165 if no error occurs. Also releases all page bulks.
166 @param[in] err Whether bulk load was successful until now
167 @param[out] last_page_no Last page number
168 @return error code */
170 page_no_t &last_page_no) noexcept;
171
172 private:
173 /** Number of records inserted. */
174 uint64_t m_n_recs{};
175
176 /** B-tree index */
178
179 /** Transaction id */
181
182 /** Root page level */
183 size_t m_root_level{};
184
185 /** Flush observer */
187
188 /** Page cursor vector for all level */
190
191 /** State of the index. Used for asserting at the end of a
192 bulk load operation to ensure that the online status of the
193 index does not change */
195};
196
197#endif /* btr0load_h */
uint32_t page_no_t
Page number.
Definition: api0api.h:47
Definition: btr0load.h:51
dberr_t load_root_page(page_no_t last_page_no) noexcept
Set the root page on completion.
Definition: btr0load.cc:1228
dict_index_t * m_index
B-tree index.
Definition: btr0load.h:177
dberr_t insert(dtuple_t *dtuple, size_t level) noexcept
Insert a tuple to a page in a level.
Definition: btr0load.cc:1097
dberr_t page_split(Page_load *page_load, Page_load *next_page_load) noexcept
Split a page.
Definition: btr0load.cc:899
Page_loaders m_page_loaders
Page cursor vector for all level.
Definition: btr0load.h:189
unsigned m_index_online
State of the index.
Definition: btr0load.h:194
dberr_t build(Cursor &cursor) noexcept
Load the btree from the cursor.
Definition: btr0load.cc:1306
ut::unique_ptr< Page_load > Page_loader_ptr
Definition: btr0load.h:75
void log_free_check() noexcept
Log free check.
Definition: btr0load.cc:989
dberr_t prepare_space(Page_load *&page_load, size_t level, size_t rec_size) noexcept
Prepare space to insert a tuple.
Definition: btr0load.cc:1026
dberr_t finalize_page_loads(dberr_t err, page_no_t &last_page_no) noexcept
Btree page bulk load finish.
Definition: btr0load.cc:1200
void latch() noexcept
Re-latch latch on the rightmost leaf page in the index tree.
Definition: btr0load.cc:1018
std::vector< Page_loader_ptr, ut::allocator< Page_loader_ptr > > Page_loaders
Definition: btr0load.h:77
uint64_t get_n_recs() noexcept
Public getter function to return number of records inserted.
Definition: btr0load.h:115
dberr_t page_commit(Page_load *page_load, Page_load *next_page_load, bool insert_father) noexcept
Commit(finish) a page.
Definition: btr0load.cc:943
void release() noexcept
Release latch on the rightmost leaf page in the index tree.
Definition: btr0load.cc:1013
dberr_t finish(dberr_t err) noexcept
Btree bulk load finish.
Definition: btr0load.cc:1272
trx_id_t m_trx_id
Transaction id.
Definition: btr0load.h:180
Flush_observer * m_flush_observer
Flush observer.
Definition: btr0load.h:186
uint64_t m_n_recs
Number of records inserted.
Definition: btr0load.h:174
size_t m_root_level
Root page level.
Definition: btr0load.h:183
We use Flush_observer to track flushing of non-redo logged pages in bulk create index(btr0load....
Definition: buf0flu.h:283
The proper function call sequence of Page_load is as below: – Page_load::init – Page_load::insert – P...
Definition: btr0load.cc:55
A utility class which, if inherited from, prevents the descendant class from being copied,...
Definition: ut0class_life_cycle.h:41
dberr_t
Definition: db0err.h:39
Data dictionary system.
bool index(const std::string &value, const String &search_for, uint32_t *idx)
Definition: contains.h:76
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:943
noexcept
The return type for any call_and_catch(f, args...) call where f(args...) returns Type.
Definition: call_and_catch.h:76
Define std::hash<Gtid>.
Definition: gtid.h:355
This file contains a set of libraries providing overloads for regular dynamic allocation routines whi...
Definition: aligned_alloc.h:48
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2724
std::conditional_t< !std::is_array< T >::value, std::unique_ptr< T, detail::Deleter< T > >, std::conditional_t< detail::is_unbounded_array_v< T >, std::unique_ptr< T, detail::Array_deleter< std::remove_extent_t< T > > >, void > > unique_ptr
The following is a common type that is returned by all the ut::make_unique (non-aligned) specializati...
Definition: ut0new.h:2284
The page cursor.
Interface to consume from.
Definition: btr0load.h:54
virtual dberr_t fetch(dtuple_t *&dtuple) noexcept=0
Fetch the current row as a tuple.
virtual ~Cursor()=default
Destructor.
virtual dberr_t next() noexcept=0
Move to the next record.
Cursor()=default
Constructor.
virtual bool duplicates_detected() const noexcept=0
Storage format for overflow data in a big record, that is, a clustered index record which needs exter...
Definition: data0data.h:865
Data structure for an index.
Definition: dict0mem.h:1069
Structure for an SQL data tuple of fields (logical record)
Definition: data0data.h:706
ib_id_t trx_id_t
Transaction identifier (DB_TRX_ID, DATA_TRX_ID)
Definition: trx0types.h:138
#define IF_DEBUG(...)
Definition: univ.i:677
Utilities related to class lifecycle.
Dynamic memory allocation routines and custom allocators specifically crafted to support memory instr...