MySQL 8.0.33
Source Code Documentation
btr0load.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2014, 2023, 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 also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25*****************************************************************************/
26
27/** @file include/btr0load.h
28 The B-tree bulk load
29
30 Created 03/11/2014 Shaohua Wang
31 *************************************************************************/
32
33#ifndef btr0load_h
34#define btr0load_h
35
36#include <stddef.h>
37#include <vector>
38
39#include "dict0dict.h"
40#include "page0cur.h"
41#include "ut0class_life_cycle.h"
42#include "ut0new.h"
43
44// Forward declaration.
45class Page_load;
46
47/** @note We should call commit(false) for a Page_load object, which is not in
48m_page_loaders after page_commit, and we will commit or abort Page_load
49objects in function "finish". */
51 public:
52 /** Interface to consume from. */
53 struct Cursor {
54 /** Constructor. */
55 Cursor() = default;
56
57 /** Destructor. */
58 virtual ~Cursor() = default;
59
60 /** Fetch the current row as a tuple.
61 @param[out] dtuple Row represented as a tuple.
62 @return DB_SUCCESS, DB_END_OF_INDEX or error code. */
63 [[nodiscard]] virtual dberr_t fetch(dtuple_t *&dtuple) noexcept = 0;
64
65 /** @return true if duplicates detected. */
66 virtual bool duplicates_detected() const noexcept = 0;
67
68 /** Move to the next record.
69 @return DB_SUCCESS, DB_END_OF_INDEX or error code. */
70 [[nodiscard]] virtual dberr_t next() noexcept = 0;
71 };
72
73 public:
74 using Page_loaders = std::vector<Page_load *, ut::allocator<Page_load *>>;
75
76 /** Constructor
77 @param[in] index B-tree index.
78 @param[in] trx_id Transaction id.
79 @param[in] observer Flush observer */
80 Btree_load(dict_index_t *index, trx_id_t trx_id,
81 Flush_observer *observer) noexcept;
82
83 /** Destructor */
84 ~Btree_load() noexcept;
85
86 /** Load the btree from the cursor.
87 @param[in,out] cursor Cursor to read tuples from.
88 @return DB_SUCCESS or error code. */
89 [[nodiscard]] dberr_t build(Cursor &cursor) noexcept;
90
91 /** Btree bulk load finish. We commit the last page in each level
92 and copy the last page in top level to the root page of the index
93 if no error occurs.
94 @param[in] err Whether bulk load was successful until now
95 @return error code */
96 [[nodiscard]] dberr_t finish(dberr_t err) noexcept;
97
98 /** Release latch on the rightmost leaf page in the index tree. */
99 void release() noexcept;
100
101 /** Re-latch latch on the rightmost leaf page in the index tree. */
102 void latch() noexcept;
103
104 /** Insert a tuple to a page in a level
105 @param[in] dtuple Tuple to insert
106 @param[in] level B-tree level
107 @return error code */
108 [[nodiscard]] dberr_t insert(dtuple_t *dtuple, size_t level) noexcept;
109
110 private:
111 /** Set the root page on completion.
112 @param[in] last_page_no Last page number (the new root).
113 @return DB_SUCCESS or error code. */
114 dberr_t load_root_page(page_no_t last_page_no) noexcept;
115
116 /** Split a page
117 @param[in] page_load Page to split
118 @param[in] next_page_load Next page
119 @return error code */
120 [[nodiscard]] dberr_t page_split(Page_load *page_load,
121 Page_load *next_page_load) noexcept;
122
123 /** Commit(finish) a page. We set next/prev page no, compress a page of
124 compressed table and split the page if compression fails, insert a node
125 pointer to father page if needed, and commit mini-transaction.
126 @param[in] page_load Page to commit
127 @param[in] next_page_load Next page
128 @param[in] insert_father Flag whether need to insert node ptr
129 @return error code */
130 [[nodiscard]] dberr_t page_commit(Page_load *page_load,
131 Page_load *next_page_load,
132 bool insert_father) noexcept;
133
134 /** Prepare space to insert a tuple.
135 @param[in,out] page_load Page bulk that will be used to store the record.
136 It may be replaced if there is not enough space
137 to hold the record.
138 @param[in] level B-tree level
139 @param[in] rec_size Record size
140 @return error code */
141 [[nodiscard]] dberr_t prepare_space(Page_load *&page_load, size_t level,
142 size_t rec_size) noexcept;
143
144 /** Insert a tuple to a page.
145 @param[in] page_load Page bulk object
146 @param[in] tuple Tuple to insert
147 @param[in] big_rec Big record vector, maybe NULL if there is no
148 Data to be stored externally.
149 @param[in] rec_size Record size
150 @return error code */
151 [[nodiscard]] dberr_t insert(Page_load *page_load, dtuple_t *tuple,
152 big_rec_t *big_rec, size_t rec_size) noexcept;
153
154 /** Log free check */
155 void log_free_check() noexcept;
156
157 /** Btree page bulk load finish. Commits the last page in each level
158 if no error occurs. Also releases all page bulks.
159 @param[in] err Whether bulk load was successful until now
160 @param[out] last_page_no Last page number
161 @return error code */
163 page_no_t &last_page_no) noexcept;
164
165 private:
166 /** Number of records inserted. */
167 uint64_t m_n_recs{};
168
169 /** B-tree index */
171
172 /** Transaction id */
174
175 /** Root page level */
176 size_t m_root_level{};
177
178 /** Flush observer */
180
181 /** Page cursor vector for all level */
183
184 /** State of the index. Used for asserting at the end of a
185 bulk load operation to ensure that the online status of the
186 index does not change */
188};
189
190#endif /* btr0load_h */
uint32_t page_no_t
Page number.
Definition: api0api.h:48
Definition: btr0load.h:50
dberr_t load_root_page(page_no_t last_page_no) noexcept
Set the root page on completion.
Definition: btr0load.cc:1214
dict_index_t * m_index
B-tree index.
Definition: btr0load.h:170
dberr_t insert(dtuple_t *dtuple, size_t level) noexcept
Insert a tuple to a page in a level.
Definition: btr0load.cc:1084
dberr_t page_split(Page_load *page_load, Page_load *next_page_load) noexcept
Split a page.
Definition: btr0load.cc:886
Page_loaders m_page_loaders
Page cursor vector for all level.
Definition: btr0load.h:182
unsigned m_index_online
State of the index.
Definition: btr0load.h:187
dberr_t build(Cursor &cursor) noexcept
Load the btree from the cursor.
Definition: btr0load.cc:1288
void log_free_check() noexcept
Log free check.
Definition: btr0load.cc:976
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:1012
dberr_t finalize_page_loads(dberr_t err, page_no_t &last_page_no) noexcept
Btree page bulk load finish.
Definition: btr0load.cc:1185
void latch() noexcept
Re-latch latch on the rightmost leaf page in the index tree.
Definition: btr0load.cc:1003
dberr_t page_commit(Page_load *page_load, Page_load *next_page_load, bool insert_father) noexcept
Commit(finish) a page.
Definition: btr0load.cc:930
void release() noexcept
Release latch on the rightmost leaf page in the index tree.
Definition: btr0load.cc:998
std::vector< Page_load *, ut::allocator< Page_load * > > Page_loaders
Definition: btr0load.h:74
dberr_t finish(dberr_t err) noexcept
Btree bulk load finish.
Definition: btr0load.cc:1258
trx_id_t m_trx_id
Transaction id.
Definition: btr0load.h:173
Flush_observer * m_flush_observer
Flush observer.
Definition: btr0load.h:179
uint64_t m_n_recs
Number of records inserted.
Definition: btr0load.h:167
size_t m_root_level
Root page level.
Definition: btr0load.h:176
We use Flush_observer to track flushing of non-redo logged pages in bulk create index(btr0load....
Definition: buf0flu.h:268
The proper function call sequence of Page_load is as below: – Page_load::init – Page_load::insert – P...
Definition: btr0load.cc:53
A utility class which, if inherited from, prevents the descendant class from being copied,...
Definition: ut0class_life_cycle.h:40
dberr_t
Definition: db0err.h:38
Data dictionary system.
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:909
Definition: varlen_sort.h:183
This file contains a set of libraries providing overloads for regular dynamic allocation routines whi...
Definition: aligned_alloc.h:47
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2872
The page cursor.
Interface to consume from.
Definition: btr0load.h:53
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:829
Data structure for an index.
Definition: dict0mem.h:1045
Structure for an SQL data tuple of fields (logical record)
Definition: data0data.h:681
ib_id_t trx_id_t
Transaction identifier (DB_TRX_ID, DATA_TRX_ID)
Definition: trx0types.h:137
#define IF_DEBUG(...)
Definition: univ.i:673
Utilities related to class lifecycle.
Dynamic memory allocation routines and custom allocators specifically crafted to support memory instr...