MySQL 9.0.0
Source Code Documentation
btr0load.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2014, 2024, 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:
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 */
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 */
176
177 /** Transaction id */
179
180 /** Root page level */
181 size_t m_root_level{};
182
183 /** Flush observer */
185
186 /** Page cursor vector for all level */
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 */
193};
194
195#endif /* btr0load_h */
uint32_t page_no_t
Page number.
Definition: api0api.h:45
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:1230
dict_index_t * m_index
B-tree index.
Definition: btr0load.h:175
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:895
Page_loaders m_page_loaders
Page cursor vector for all level.
Definition: btr0load.h:187
unsigned m_index_online
State of the index.
Definition: btr0load.h:192
dberr_t build(Cursor &cursor) noexcept
Load the btree from the cursor.
Definition: btr0load.cc:1304
void log_free_check() noexcept
Log free check.
Definition: btr0load.cc:985
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:1024
dberr_t finalize_page_loads(dberr_t err, page_no_t &last_page_no) noexcept
Btree page bulk load finish.
Definition: btr0load.cc:1201
void latch() noexcept
Re-latch latch on the rightmost leaf page in the index tree.
Definition: btr0load.cc:1015
uint64_t get_n_recs() noexcept
Public getter function to return number of records inserted.
Definition: btr0load.h:113
dberr_t page_commit(Page_load *page_load, Page_load *next_page_load, bool insert_father) noexcept
Commit(finish) a page.
Definition: btr0load.cc:939
void release() noexcept
Release latch on the rightmost leaf page in the index tree.
Definition: btr0load.cc:1009
std::vector< Page_load *, ut::allocator< Page_load * > > Page_loaders
Definition: btr0load.h:75
dberr_t finish(dberr_t err) noexcept
Btree bulk load finish.
Definition: btr0load.cc:1274
trx_id_t m_trx_id
Transaction id.
Definition: btr0load.h:178
Flush_observer * m_flush_observer
Flush observer.
Definition: btr0load.h:184
uint64_t m_n_recs
Number of records inserted.
Definition: btr0load.h:172
size_t m_root_level
Root page level.
Definition: btr0load.h:181
We use Flush_observer to track flushing of non-redo logged pages in bulk create index(btr0load....
Definition: buf0flu.h:274
The proper function call sequence of Page_load is as below: – Page_load::init – Page_load::insert – P...
Definition: btr0load.cc:54
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.
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:927
Definition: gcs_xcom_synode.h:64
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:2875
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:840
Data structure for an index.
Definition: dict0mem.h:1046
Structure for an SQL data tuple of fields (logical record)
Definition: data0data.h:684
ib_id_t trx_id_t
Transaction identifier (DB_TRX_ID, DATA_TRX_ID)
Definition: trx0types.h:138
#define IF_DEBUG(...)
Definition: univ.i:674
Utilities related to class lifecycle.
Dynamic memory allocation routines and custom allocators specifically crafted to support memory instr...