MySQL 9.7.0
Source Code Documentation
ddl0impl-builder.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2020, 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/ddl0impl-builder.h
29 DDL index builder data interface.
30 Created 2020-11-01 by Sunny Bains. */
31
32#ifndef ddl0impl_builder_h
33#define ddl0impl_builder_h
34
35#include "btr0load.h"
36#include "ddl0impl-buffer.h"
38#include "row0pread.h"
39
40namespace ddl {
41
42// Forward declaration.
43struct Copy_ctx;
44struct File_cursor;
45class RTree_inserter;
46
47/** For loading indexes. */
48struct Builder {
49 /** Build phase/states. */
50 enum class State : uint8_t {
51 /** Initial phase. */
52 INIT,
53
54 /** Collect the rows for the index to build. */
55 ADD,
56
57 /** Setup the merge sort and add the tasks to the task queue. */
59
60 /** Sort the collected rows, if required. The builder moves to state
61 BTREE_BUILD after all sort tasks are completed successfully or there
62 was an error during the sort phase. */
63 SORT,
64
65 /** Build the btree. */
67
68 /** FTS sort and build, this is done in one "step" */
70
71 /** Finish the loading of the index. */
72 FINISH,
73
74 /** Stop on success. */
75 STOP,
76
77 /** Stop on error. */
78 ERROR
79 };
80
81 /** Constructor.
82 @param[in,out] ctx DDL context.
83 @param[in,out] loader Owner of the instance.
84 @param[in] i Index ordinal value. */
85 Builder(ddl::Context &ctx, Loader &loader, size_t i) noexcept;
86
87 /** Destructor/ */
89
90 /** @return the error status. */
92
93 /** Set the error code.
94 @param[in] err Error code to set.
95 @return true iff this thread successfully set the error code. */
96 bool set_error(dberr_t err) noexcept { return m_ctx.set_error(err, m_id); }
97
98 /** @return the instance ID. */
99 [[nodiscard]] size_t id() const noexcept { return m_id; }
100
101 /** @return the index being built. */
102 [[nodiscard]] dict_index_t *index() noexcept { return m_sort_index; }
103
104 /** @return the DDL context. */
105 Context &ctx() noexcept { return m_ctx; }
106
107 /** Parallel scan thread spawn failed, release the extra thread states. */
109
110 /** @return true if the index is a spatial index. */
111 [[nodiscard]] bool is_spatial_index() const noexcept {
113 }
114
115 /** @return true if the index is an FTS index. */
116 [[nodiscard]] bool is_fts_index() const noexcept {
117 return m_index->type & DICT_FTS;
118 }
119
120 /** @return true if the index is a unique index. */
121 [[nodiscard]] bool is_unique_index() const noexcept {
122 ut_a(!is_fts_index());
124 }
125
126 /** @return the current builder state. */
127 [[nodiscard]] State get_state() const noexcept {
128 return m_state.load(std::memory_order_seq_cst);
129 }
130
131 /** Set the next state.
132 @param[in] state State to set. */
133 void set_state(State state) noexcept {
134 m_state.store(state, std::memory_order_seq_cst);
135 }
136
137 /** @return the PFS instance that is used to report progress (or nullptr). */
139
140 /** Set the next state. */
142
143 /** Initialize the cursor.
144 @param[in,out] cursor Cursor to initialize.
145 @param[in] n_threads Number of threads used for reading.
146 @return DB_SUCCESS or error code. */
147 [[nodiscard]] dberr_t init(Cursor &cursor, size_t n_threads) noexcept;
148
149 /** Add a row to the merge buffer.
150 @param[in,out] cursor Current scan cursor.
151 @param[in,out] row Row to add.
152 @param[in] thread_id ID of current thread.
153 @param[in,out] latch_release Called when a log free check is required.
154 @return DB_SUCCESS or error code. */
155 [[nodiscard]] dberr_t add_row(Cursor &cursor, Row &row, size_t thread_id,
156 Latch_release &&latch_release) noexcept;
157
158 /** @return true if file sorting can be skipped. */
161 }
162
163 /** FTS: Sort and insert the rows read.
164 @return DB_SUCCESS or error code. */
165 [[nodiscard]] dberr_t fts_sort_and_build() noexcept;
166
167 /** Non-FTS: Sort the rows read.
168 @return DB_SUCCESS or error code. */
169 [[nodiscard]] dberr_t setup_sort() noexcept;
170
171 /** Non-FTS: Sort the rows read.
172 @param[in] thread_id Thread state ID.
173 @return DB_SUCCESS or error code. */
174 [[nodiscard]] dberr_t merge_sort(size_t thread_id) noexcept;
175
176 /** Load the sorted data into the B+Tree.
177 @return DB_SUCESS or error code. */
178 [[nodiscard]] dberr_t btree_build() noexcept;
179
180 /** Close temporary files, Flush all dirty pages, apply the row log
181 and write the redo log record.
182 @return DB_SUCCESS or error code. */
184
185 /** Copy blobs to the tuple.
186 @param[out] dtuple Tuple to copy to.
187 @param[in,out] offsets Column offsets in the row.
188 @param[in] mrec Current row.
189 @param[in,out] heap Heap for the allocating tuple memory.
190 @return DB_SUCCESS or error code. */
191 [[nodiscard]] dberr_t dtuple_copy_blobs(dtuple_t *dtuple, ulint *offsets,
192 const mrec_t *mrec,
193 mem_heap_t *heap) noexcept;
194
195 /** Write data to disk - in append mode. Increment the file size.
196 @param[in,out] file File handle.
197 @param[in] file_buffer Write the buffer contents to disk.
198 @return DB_SUCCESS or error code. */
199 [[nodiscard]] dberr_t append(ddl::file_t &file,
201
202 /** @return the path for temporary files. */
203 const char *tmpdir() const noexcept { return m_tmpdir; }
204
205 /** Insert cached rows.
206 @param[in] thread_id Insert cached rows for this thread ID.
207 @param[in,out] latch_release Called when a log free check is required.
208 @return DB_SUCCESS or error number */
209 [[nodiscard]] dberr_t batch_insert(size_t thread_id,
210 Latch_release &&latch_release) noexcept;
211
212 /** Note that the latches are going to be released. Do a deep copy of the
213 tuples that are being inserted in batches by batch_insert
214 @param[in] thread_id Deep copy cached rows for this thread ID. */
215 void batch_insert_deep_copy_tuples(size_t thread_id) noexcept;
216
217 /** Check the state of the online build log for the index.
218 @return DB_SUCCESS or error code. */
220
221 /** Write an MLOG_INDEX_LOAD record to indicate in the redo-log
222 that redo-logging of individual index pages was disabled, and
223 the flushing of such pages to the data files was completed.
224 @param[in] index Index on which redo logging was disabled */
225 static void write_redo(const dict_index_t *index) noexcept;
226
227 private:
228 /** State of a cluster index reader thread. */
229 struct Thread_ctx {
230 /** Constructor.
231 @param[in] id Thread state ID.
232 @param[in,out] key_buffer Buffer for building the target index. Note, the
233 thread state will own the key buffer and is
234 responsible for deleting it. */
235 explicit Thread_ctx(size_t id, Key_sort_buffer *key_buffer) noexcept;
236
237 /** Destructor. */
238 ~Thread_ctx() noexcept;
239
240 /** Thread ID. */
241 size_t m_id{};
242
243 /** Key sort buffer. */
244 Key_sort_buffer *m_key_buffer{};
245
246 /** Total number of records added to the key sort buffer. */
247 size_t m_n_recs{};
248
249 /** Merge file handle. */
250 ddl::file_t m_file{};
251
252 /** Buffer to use for file writes. */
254
255 /** Buffer to use for file writes. */
257
258 /** Record list starting offset in the output file. */
259 Merge_offsets m_offsets{};
260
261 /** For spatial/Rtree rows handling. */
262 RTree_inserter *m_rtree_inserter{};
263 };
264
266 using Thread_ctxs = std::vector<Thread_ctx *, Allocator>;
267
268 /** Create the tasks to merge Sort the file before we load the file into
269 the Btree index.
270 @return DB_SUCCESS or error code. */
272
273 /** Flush all dirty pages, apply the row log and write the redo log record.
274 @return DB_SUCCESS or error code. */
276
277 /** Convert the field data from compact to redundant format.
278 @param[in] clust_index Clustered index being built
279 @param[in] row_field Field to copy from
280 @param[out] field Field to copy to
281 @param[in] len Length of the field data
282 @param[in] page_size Compressed BLOB page size
283 @param[in] is_sdi true for SDI Indexes
284 @param[in,out] heap Memory heap where to allocate
285 data when converting to ROW_FORMAT=REDUNDANT,
286 or nullptr */
287 static void convert(const dict_index_t *clust_index,
288 const dfield_t *row_field, dfield_t *field, ulint len,
289 const page_size_t &page_size,
290 IF_DEBUG(bool is_sdi, ) mem_heap_t *heap) noexcept;
291
292 /** Copy externally stored columns to the data tuple.
293 @param[in] index Index dictionary object.
294 @param[in] mrec Record containing BLOB pointers, or
295 nullptr to use tuple instead.
296 @param[in] offsets Offsets of mrec.
297 @param[in] page_size Compressed page size in bytes, or 0
298 @param[in,out] tuple Data tuple.
299 @param[in] is_sdi True for SDI Indexes
300 @param[in,out] heap Memory heap */
301 static void copy_blobs(const dict_index_t *index, const mrec_t *mrec,
302 const ulint *offsets, const page_size_t &page_size,
303 dtuple_t *tuple,
304 IF_DEBUG(bool is_sdi, ) mem_heap_t *heap) noexcept;
305
306 /** Cache a row for batch inserts. Currently used by spatial indexes.
307 @param[in,out] row Row to add.
308 @param[in] thread_id ID of current thread.
309 @return DB_SUCCESS or error code. */
310 [[nodiscard]] dberr_t batch_add_row(Row &row, size_t thread_id) noexcept;
311
312 /** Add a row to the merge buffer.
313 @param[in,out] cursor Current scan cursor.
314 @param[in,out] row Row to add.
315 @param[in] thread_id ID of current thread.
316 @param[in,out] latch_release Called when a log free check is required.
317 @return DB_SUCCESS or error code. */
318 [[nodiscard]] dberr_t bulk_add_row(Cursor &cursor, Row &row, size_t thread_id,
319 Latch_release &&latch_release) noexcept;
320
321 /** Clear the heap used for virtual columns. */
323
324 /** Add the FTS document ID to the destination field.
325 @param[in,out] dst Field to write to.
326 @param[in] src Field to copy meta data from.
327 @param[out] write_doc_id Buffer for copying the doc id. */
328 void fts_add_doc_id(dfield_t *dst, const dict_field_t *src,
329 doc_id_t &write_doc_id) noexcept;
330
331 /** Add a row to the write buffer.
332 @param[in,out] ctx Copy context.
333 @param[in,out] mv_rows_added Number of multi-value rows added.
334 @return DB_SUCCESS or error code. */
335 [[nodiscard]] dberr_t copy_row(Copy_ctx &ctx, size_t &mv_rows_added) noexcept;
336
337 /** Setup the virtual column src column.
338 @param[in,out] ctx Copy context.
339 @param[in] ifield Index field.
340 @param[in] col Table column.
341 @param[out] src_field Computed value.
342 @param[in,out] mv_rows_added Number of multi-value rows added.
343 @return DB_SUCCESS or error code. */
344 [[nodiscard]] dberr_t get_virtual_column(Copy_ctx &ctx,
345 const dict_field_t *ifield,
346 dict_col_t *col,
347 dfield_t *&src_field,
348 size_t &mv_rows_added) noexcept;
349
350 /** Enqueue parsing of the field of the row
351 @param[in] row The row which contains the field to parse
352 @param[in] field_no Which field to parse
353 @return DB_SUCCESS or error code. */
354 [[nodiscard]] dberr_t enqueue_parsing(const dtuple_t &row,
355 size_t field_no) noexcept;
356
357 /** Enqueue parsing of the row. Asserts we are building FTS index only.
358 @param[in] row The row which contains fields to parse.
359 @return DB_SUCCESS or error code. */
360 [[nodiscard]] dberr_t enqueue_parsing(const dtuple_t &row) noexcept;
361
362 /** Copy the columns to the temporary file buffer.
363 @param[in,out] ctx Copy context.
364 @param[in,out] mv_rows_added Multi value rows added.
365 @param[in,out] write_doc_id Buffer for storing the FTS doc ID.
366 @return DB_SUCCESS or error code. */
367 [[nodiscard]] dberr_t copy_columns(Copy_ctx &ctx, size_t &mv_rows_added,
368 doc_id_t &write_doc_id) noexcept;
369
370 /** Add row to the key buffer.
371 @param[in,out] ctx Copy context.
372 @param[in,out] mv_rows_added Number of multi-value index rows added.
373 @return DB_SUCCESS or error code. */
374 [[nodiscard]] dberr_t add_to_key_buffer(Copy_ctx &ctx,
375 size_t &mv_rows_added) noexcept;
376
377 /** Wait for FTS completion.
378 @param[in] index Index being built. */
380
381 /** Sort the data in the key buffer.
382 @param[in] thread_id Thread ID of current thread.
383 @return DB_SUCCESS or error code. */
384 [[nodiscard]] dberr_t key_buffer_sort(size_t thread_id) noexcept;
385
386 /** Sort the buffer in memory and insert directly in the BTree loader,
387 don't write to a temporary file.
388 @param[in,out] cursor Current scan cursor.
389 @param[in] thread_id ID of current thread.
390 @return DB_SUCCESS or error code. */
391 dberr_t insert_direct(Cursor &cursor, size_t thread_id) noexcept;
392
393 /** Create the merge file, if needed.
394 @param[in,out] file File handle.
395 @return true if file was opened successfully . */
396 [[nodiscard]] bool create_file(ddl::file_t &file) noexcept;
397
398 /** Check for duplicates in the first block
399 @param[in] dupcheck Files to check for duplicates.
400 @return DB_SUCCESS or error code. */
401 [[nodiscard]] dberr_t check_duplicates(Thread_ctxs &dupcheck) noexcept;
402
403 /** Cleanup DDL after error in online build
404 Note: To be called if DDL must cleanup due to error in online build. Pages
405 which are buffer-fixed (in Page_load::release) until the next iteration, must
406 be unfixed (with Page_load::latch) before returning the error.
407 @note: Assumes that either m_btr_load->release is called before or
408 m_n_recs is 0 (no records are inserted yet).
409 @param[in] err Error hit in online build
410 @return the cursor error status. */
411 [[nodiscard]] dberr_t handle_error(dberr_t err) noexcept;
412
413 private:
414 /** Buffer ID. */
415 size_t m_id{};
416
417 /** Initial phase. */
418 std::atomic<State> m_state{State::INIT};
419
420 /** DDL Context. */
422
423 /** Loader that owns the instance. */
425
426 /** Index to create (if not FTS index). */
428
429 /** Temporary file path. */
430 const char *m_tmpdir{};
431
432 /** Per thread context. */
434
435 /** For tracking duplicates. */
437
438 /** For collecting duplicate entries (error reporting). */
440
441 /** Scoped virtual column heap. */
443
444 /** Scoped conversion heap. */
446
447 /** The index to be built, FTS or non-FTS. */
449
450 /** Number of active sort tasks. */
451 std::atomic<size_t> m_n_sort_tasks{};
452
453 /** Cluster index bulk load instance to use, direct insert without
454 a file sort. */
456
457 /** Stage per builder. */
459};
460
462 /** Default constructor. */
463 explicit Load_cursor(Builder *builder, Dup *dup) noexcept
464 : m_dup(dup), m_builder(builder) {}
465
466 /** Default destructor. */
467 virtual ~Load_cursor() override = default;
468
469 /** @return the cursor error status. */
470 [[nodiscard]] dberr_t get_err() const noexcept { return m_err; }
471
472 /** @return true if duplicates detected. */
473 [[nodiscard]] bool duplicates_detected() const noexcept override;
474
475 /** Duplicate checking and reporting. */
477
478 /** Operation error code. */
480
481 /** Index meta data. */
483
484 /** Heap for the raw row to dtuple_t conversion. */
486};
487
488/** Merge the sorted files. */
489struct Merge_cursor : public Load_cursor {
490 /** File cursors to use for the scan. */
491 using File_readers = std::vector<File_reader *, ut::allocator<File_reader *>>;
492
493 /** Constructor.
494 @param[in,out] builder Index builder.
495 @param[in,out] dup If not nullptr, then report duplicates.
496 @param[in,out] stage PFS stage monitoring. */
497 explicit Merge_cursor(Builder *builder, Dup *dup,
498 Alter_stage *stage) noexcept;
499
500 /** Destructor. */
501 ~Merge_cursor() noexcept override;
502
503 /** Add the cursor to use for merge load.
504 @param[in] file File to merge from.
505 @param[in] buffer_size IO buffer size to use for reading.
506 @return DB_SUCCESS or error code. */
507 [[nodiscard]] dberr_t add_file(const ddl::file_t &file,
508 size_t buffer_size) noexcept;
509
510 /** Add the cursor to use for merge load.
511 @param[in] file File file to read.
512 @param[in] buffer_size IO buffer size to use for reading.
513 @param[in] range Range to read from
514 @return DB_SUCCESS or error code. */
515 [[nodiscard]] dberr_t add_file(const ddl::file_t &file, size_t buffer_size,
516 const Range &range) noexcept;
517
518 /** Open the cursor.
519 @return DB_SUCCESS or error code. */
520 [[nodiscard]] dberr_t open() noexcept;
521
522 /** Fetch the current row as a tuple. Note: Tuple columns are shallow copies.
523 @param[out] dtuple Row represented as a tuple.
524 @return DB_SUCCESS, DB_END_OF_INDEX or error code. */
525 [[nodiscard]] dberr_t fetch(dtuple_t *&dtuple) noexcept override;
526
527 /** Fetch the current row.
528 @param[out] mrec Current merge record.
529 @param[out] offsets Columns offsets inside mrec.
530 @return DB_SUCCESS, DB_END_OF_INDEX or error code. */
531 [[nodiscard]] dberr_t fetch(const mrec_t *&mrec, ulint *&offsets) noexcept;
532
533 /** Move to the next record.
534 @return DB_SUCCESS, DB_END_OF_INDEX or error code. */
535 [[nodiscard]] dberr_t next() noexcept override;
536
537 /** @return the file reader instances. */
538 [[nodiscard]] File_readers file_readers() noexcept;
539
540 /** Add the active cursors to the priority queue. */
541 void clear_eof() noexcept;
542
543 /** @return the number of active readers. */
544 [[nodiscard]] size_t size() const noexcept { return m_pq.size(); }
545
546 /** @return the number of rows read from the files. */
547 [[nodiscard]] uint64_t get_n_rows() const noexcept;
548
549 /** @return the number of cursors being merged. */
550 [[nodiscard]] size_t number_of_cursors() const noexcept {
551 return m_cursors.size();
552 }
553
554 private:
555 /** @return the current cursor at the head of the queue. */
556 [[nodiscard]] File_cursor *pop() noexcept;
557
558 private:
559 /** Comparator. */
560 struct Compare {
561 /** Constructor.
562 @param[in] index Index that the rows belong to.
563 @param[in,out] dup For reporting duplicates, can be nullptr. */
564 explicit Compare(const dict_index_t *index, Dup *dup)
565 : m_dup(dup), m_index(index) {}
566
567 /** Destructor. */
568 Compare() = default;
569
570 /** Compare the keys of two cursors.
571 @param[in] lhs Left hand side.
572 @param[in] rhs Right hand side.
573 @return true if lhs strictly less than rhs. */
574 bool operator()(const File_cursor *lhs,
575 const File_cursor *rhs) const noexcept;
576
577 /** For reporting duplicates. */
579
580 /** Index being built. */
581 const dict_index_t *m_index{};
582 };
583
584 /** File cursors to use for the scan. */
585 using File_cursors = std::vector<File_cursor *, ut::allocator<File_cursor *>>;
586
587 /** Priority queue for ordering the rows. */
588 using Queue = std::priority_queue<File_cursor *, File_cursors, Compare>;
589
590 /** Priority queue for merging the file cursors. */
592
593 /** Cursors to use for parallel loading of the index. */
595
596 /** Current cursor. */
598
599 /** PFS stage monitoring. */
601};
602
603} // namespace ddl
604
605#endif /* !ddl0impl_builder_h */
The B-tree bulk load.
static Mysys_charset_loader * loader
Definition: charset.cc:197
Class used to report ALTER TABLE progress via performance_schema.
Definition: ut0stage.h:81
Definition: btr0load.h:51
Implements a persistent FIFO using server List method names.
Definition: sql_profile.h:76
Build indexes on a table by reading a clustered index, creating a temporary file containing index ent...
Definition: ddl0impl-loader.h:44
Class that caches RTree index tuples made from a single cluster index page scan, and then insert into...
Definition: ddl0impl-rtree.h:44
Page size descriptor.
Definition: page0size.h:50
Allocator that allows std::* containers to manage their memory through ut::malloc* and ut::free libra...
Definition: ut0new.h:2183
dberr_t
Definition: db0err.h:39
@ DB_SUCCESS
Definition: db0err.h:43
DDL buffer infrastructure.
For scanning the temporary file.
static ulint dict_index_is_unique(const dict_index_t *index)
Check whether the index is unique.
static ulint dict_index_is_spatial(const dict_index_t *index)
Check whether the index is a Spatial Index.
constexpr uint32_t DICT_FTS
FTS index; can't be combined with the other flags.
Definition: dict0mem.h:105
uint64_t doc_id_t
Document id type.
Definition: fts0fts.h:76
static my_thread_id thread_id
Definition: my_thr_init.cc:60
static struct st_file_buffer file_buffer
Definition: myisampack.cc:209
The general architecture is that the work is done in two phases, roughly the read and write phase.
Definition: btr0load.cc:42
std::pair< os_offset_t, os_offset_t > Range
Represents the chunk in bytes : first element represents the beginning offset of the chunk and,...
Definition: ddl0impl-file-reader.h:44
std::deque< os_offset_t, ut::allocator< os_offset_t > > Merge_offsets
Start offsets in the file, from where to merge records.
Definition: ddl0impl.h:64
byte mrec_t
Merge record in Aligned_buffer.
Definition: ddl0ddl.h:78
std::function< dberr_t()> Latch_release
Called when a log free check is required.
Definition: ddl0impl.h:50
std::pair< byte *, os_offset_t > IO_buffer
Block size for DDL I/O operations.
Definition: ddl0impl.h:47
Definition: os0file.h:89
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
size_t buffer_size(const ConstBufferSequence &buffers) noexcept
Definition: buffer.h:313
std::conditional_t< !std::is_array< T >::value, std::unique_ptr< T, detail::Aligned_deleter< T > >, std::conditional_t< detail::is_unbounded_array_v< T >, std::unique_ptr< T, detail::Aligned_array_deleter< std::remove_extent_t< T > > >, void > > unique_ptr_aligned
The following is a common type that is returned by all the ut::make_unique_aligned (non-aligned) spec...
Definition: ut0new.h:2578
Parallel read interface.
Interface to consume from.
Definition: btr0load.h:54
Heap wrapper that destroys the heap instance when it goes out of scope.
Definition: mem0mem.h:439
void clear() noexcept
Empty the heap.
Definition: mem0mem.h:482
State of a cluster index reader thread.
Definition: ddl0impl-builder.h:229
IO_buffer m_io_buffer
Buffer to use for file writes.
Definition: ddl0impl-builder.h:256
For loading indexes.
Definition: ddl0impl-builder.h:48
dberr_t finalize() noexcept
Flush all dirty pages, apply the row log and write the redo log record.
Definition: ddl0builder.cc:1981
dberr_t insert_direct(Cursor &cursor, size_t thread_id) noexcept
Sort the buffer in memory and insert directly in the BTree loader, don't write to a temporary file.
Definition: ddl0builder.cc:1223
dberr_t btree_build() noexcept
Load the sorted data into the B+Tree.
Definition: ddl0builder.cc:1785
bool set_error(dberr_t err) noexcept
Set the error code.
Definition: ddl0impl-builder.h:96
void set_state(State state) noexcept
Set the next state.
Definition: ddl0impl-builder.h:133
std::atomic< size_t > m_n_sort_tasks
Number of active sort tasks.
Definition: ddl0impl-builder.h:451
void fts_wait_for_completion(const dict_index_t *index) noexcept
Wait for FTS completion.
dberr_t add_row(Cursor &cursor, Row &row, size_t thread_id, Latch_release &&latch_release) noexcept
Add a row to the merge buffer.
Definition: ddl0builder.cc:1600
void clear_virtual_heap() noexcept
Clear the heap used for virtual columns.
Definition: ddl0impl-builder.h:322
bool is_skip_file_sort() const noexcept
Definition: ddl0impl-builder.h:159
void set_next_state() noexcept
Set the next state.
Definition: ddl0builder.cc:2117
dberr_t fts_sort_and_build() noexcept
FTS: Sort and insert the rows read.
Definition: ddl0builder.cc:1956
static void write_redo(const dict_index_t *index) noexcept
Write an MLOG_INDEX_LOAD record to indicate in the redo-log that redo-logging of individual index pag...
Definition: ddl0builder.cc:1937
static void copy_blobs(const dict_index_t *index, const mrec_t *mrec, const ulint *offsets, const page_size_t &page_size, dtuple_t *tuple, bool is_sdi, mem_heap_t *heap) noexcept
Copy externally stored columns to the data tuple.
Definition: ddl0builder.cc:1627
bool is_spatial_index() const noexcept
Definition: ddl0impl-builder.h:111
bool create_file(ddl::file_t &file) noexcept
Create the merge file, if needed.
Definition: ddl0builder.cc:1143
std::atomic< State > m_state
Initial phase.
Definition: ddl0impl-builder.h:418
dberr_t key_buffer_sort(size_t thread_id) noexcept
Sort the data in the key buffer.
Definition: ddl0builder.cc:1184
dberr_t handle_error(dberr_t err) noexcept
Cleanup DDL after error in online build Note: To be called if DDL must cleanup due to error in online...
Definition: ddl0builder.cc:1206
static void convert(const dict_index_t *clust_index, const dfield_t *row_field, dfield_t *field, ulint len, const page_size_t &page_size, bool is_sdi, mem_heap_t *heap) noexcept
Convert the field data from compact to redundant format.
Definition: ddl0builder.cc:482
dberr_t create_merge_sort_tasks() noexcept
Create the tasks to merge Sort the file before we load the file into the Btree index.
Definition: ddl0builder.cc:1857
Context & ctx() noexcept
Definition: ddl0impl-builder.h:105
Alter_stage * m_local_stage
Stage per builder.
Definition: ddl0impl-builder.h:458
dberr_t check_state_of_online_build_log() noexcept
Check the state of the online build log for the index.
Definition: ddl0builder.cc:624
dberr_t check_duplicates(Thread_ctxs &dupcheck) noexcept
Check for duplicates in the first block.
Definition: ddl0builder.cc:1713
dberr_t dtuple_copy_blobs(dtuple_t *dtuple, ulint *offsets, const mrec_t *mrec, mem_heap_t *heap) noexcept
Copy blobs to the tuple.
Definition: ddl0builder.cc:1676
dberr_t init(Cursor &cursor, size_t n_threads) noexcept
Initialize the cursor.
Definition: ddl0builder.cc:634
Scoped_heap m_conv_heap
Scoped conversion heap.
Definition: ddl0impl-builder.h:445
void fallback_to_single_thread() noexcept
Parallel scan thread spawn failed, release the extra thread states.
Definition: ddl0builder.cc:2107
dict_index_t * m_sort_index
The index to be built, FTS or non-FTS.
Definition: ddl0impl-builder.h:448
Loader & m_loader
Loader that owns the instance.
Definition: ddl0impl-builder.h:424
const char * tmpdir() const noexcept
Definition: ddl0impl-builder.h:203
Builder(ddl::Context &ctx, Loader &loader, size_t i) noexcept
Constructor.
Definition: ddl0builder.cc:589
bool is_unique_index() const noexcept
Definition: ddl0impl-builder.h:121
std::vector< Thread_ctx *, Allocator > Thread_ctxs
Definition: ddl0impl-builder.h:266
void fts_add_doc_id(dfield_t *dst, const dict_field_t *src, doc_id_t &write_doc_id) noexcept
Add the FTS document ID to the destination field.
Definition: ddl0builder.cc:770
Dup m_clust_dup
For collecting duplicate entries (error reporting).
Definition: ddl0impl-builder.h:439
dberr_t enqueue_parsing(const dtuple_t &row, size_t field_no) noexcept
Enqueue parsing of the field of the row.
Definition: ddl0builder.cc:850
dberr_t bulk_add_row(Cursor &cursor, Row &row, size_t thread_id, Latch_release &&latch_release) noexcept
Add a row to the merge buffer.
Definition: ddl0builder.cc:1435
dict_index_t * index() noexcept
Definition: ddl0impl-builder.h:102
ddl::Context & m_ctx
DDL Context.
Definition: ddl0impl-builder.h:421
State
Build phase/states.
Definition: ddl0impl-builder.h:50
@ STOP
Stop on success.
@ ADD
Collect the rows for the index to build.
@ SORT
Sort the collected rows, if required.
@ ERROR
Stop on error.
@ BTREE_BUILD
Build the btree.
@ SETUP_SORT
Setup the merge sort and add the tasks to the task queue.
@ FINISH
Finish the loading of the index.
@ FTS_SORT_AND_BUILD
FTS sort and build, this is done in one "step".
@ INIT
Initial phase.
void batch_insert_deep_copy_tuples(size_t thread_id) noexcept
Note that the latches are going to be released.
Definition: ddl0builder.cc:1178
dict_index_t * m_index
Index to create (if not FTS index).
Definition: ddl0impl-builder.h:427
dberr_t merge_sort(size_t thread_id) noexcept
Non-FTS: Sort the rows read.
Definition: ddl0builder.cc:2017
~Builder() noexcept
Destructor/.
Definition: ddl0builder.cc:606
dberr_t append(ddl::file_t &file, IO_buffer file_buffer) noexcept
Write data to disk - in append mode.
Definition: ddl0builder.cc:1155
dberr_t finish() noexcept
Close temporary files, Flush all dirty pages, apply the row log and write the redo log record.
Definition: ddl0builder.cc:2070
dberr_t get_virtual_column(Copy_ctx &ctx, const dict_field_t *ifield, dict_col_t *col, dfield_t *&src_field, size_t &mv_rows_added) noexcept
Setup the virtual column src column.
Definition: ddl0builder.cc:787
dberr_t copy_columns(Copy_ctx &ctx, size_t &mv_rows_added, doc_id_t &write_doc_id) noexcept
Copy the columns to the temporary file buffer.
Definition: ddl0builder.cc:886
dberr_t add_to_key_buffer(Copy_ctx &ctx, size_t &mv_rows_added) noexcept
Add row to the key buffer.
Definition: ddl0builder.cc:1371
State get_state() const noexcept
Definition: ddl0impl-builder.h:127
dberr_t batch_add_row(Row &row, size_t thread_id) noexcept
Cache a row for batch inserts.
Definition: ddl0builder.cc:1313
dberr_t batch_insert(size_t thread_id, Latch_release &&latch_release) noexcept
Insert cached rows.
Definition: ddl0builder.cc:1168
size_t m_id
Buffer ID.
Definition: ddl0impl-builder.h:415
dberr_t copy_row(Copy_ctx &ctx, size_t &mv_rows_added) noexcept
Add a row to the write buffer.
Definition: ddl0builder.cc:1023
dberr_t setup_sort() noexcept
Non-FTS: Sort the rows read.
Definition: ddl0builder.cc:2054
dberr_t get_error() const noexcept
Definition: ddl0impl-builder.h:91
const char * m_tmpdir
Temporary file path.
Definition: ddl0impl-builder.h:430
Btree_load * m_btr_load
Cluster index bulk load instance to use, direct insert without a file sort.
Definition: ddl0impl-builder.h:455
Scoped_heap m_v_heap
Scoped virtual column heap.
Definition: ddl0impl-builder.h:442
dfield_t * m_prev_fields
For tracking duplicates.
Definition: ddl0impl-builder.h:436
size_t id() const noexcept
Definition: ddl0impl-builder.h:99
bool is_fts_index() const noexcept
Definition: ddl0impl-builder.h:116
Alter_stage * stage() noexcept
Definition: ddl0impl-builder.h:138
Thread_ctxs m_thread_ctxs
Per thread context.
Definition: ddl0impl-builder.h:433
DDL context/configuration.
Definition: ddl0ddl.h:360
void set_error(dberr_t err) noexcept
Set the error code, when it's not specific to an index.
Definition: ddl0ddl.h:466
bool m_skip_pk_sort
Skip the sorting phase if true.
Definition: ddl0ddl.h:676
dberr_t get_error() const noexcept
Definition: ddl0ddl.h:462
Context for copying cluster index row for the index to being created.
Definition: ddl0builder.cc:50
Cursor for reading the data.
Definition: ddl0impl-cursor.h:41
Structure for reporting duplicate records.
Definition: ddl0ddl.h:132
For loading a Btree index from a file.
Definition: ddl0builder.cc:177
Buffer for sorting in main memory.
Definition: ddl0impl-buffer.h:41
Definition: ddl0impl-builder.h:461
virtual ~Load_cursor() override=default
Default destructor.
Scoped_heap m_tuple_heap
Heap for the raw row to dtuple_t conversion.
Definition: ddl0impl-builder.h:485
Builder * m_builder
Index meta data.
Definition: ddl0impl-builder.h:482
dberr_t get_err() const noexcept
Definition: ddl0impl-builder.h:470
Load_cursor(Builder *builder, Dup *dup) noexcept
Default constructor.
Definition: ddl0impl-builder.h:463
bool duplicates_detected() const noexcept override
Definition: ddl0builder.cc:234
dberr_t m_err
Operation error code.
Definition: ddl0impl-builder.h:479
Dup * m_dup
Duplicate checking and reporting.
Definition: ddl0impl-builder.h:476
Comparator.
Definition: ddl0impl-builder.h:560
Compare(const dict_index_t *index, Dup *dup)
Constructor.
Definition: ddl0impl-builder.h:564
Compare()=default
Destructor.
Merge the sorted files.
Definition: ddl0impl-builder.h:489
std::vector< File_cursor *, ut::allocator< File_cursor * > > File_cursors
File cursors to use for the scan.
Definition: ddl0impl-builder.h:585
void clear_eof() noexcept
Add the active cursors to the priority queue.
Definition: ddl0builder.cc:385
dberr_t add_file(const ddl::file_t &file, size_t buffer_size) noexcept
Add the cursor to use for merge load.
Definition: ddl0builder.cc:380
std::vector< File_reader *, ut::allocator< File_reader * > > File_readers
File cursors to use for the scan.
Definition: ddl0impl-builder.h:491
size_t number_of_cursors() const noexcept
Definition: ddl0impl-builder.h:550
Merge_cursor(Builder *builder, Dup *dup, Alter_stage *stage) noexcept
Constructor.
Definition: ddl0builder.cc:332
dberr_t fetch(dtuple_t *&dtuple) noexcept override
Fetch the current row as a tuple.
Definition: ddl0builder.cc:433
~Merge_cursor() noexcept override
Destructor.
Definition: ddl0builder.cc:338
File_readers file_readers() noexcept
Definition: ddl0builder.cc:322
Queue m_pq
Priority queue for merging the file cursors.
Definition: ddl0impl-builder.h:591
File_cursor * pop() noexcept
Definition: ddl0builder.cc:421
dberr_t next() noexcept override
Move to the next record.
Definition: ddl0builder.cc:455
size_t size() const noexcept
Definition: ddl0impl-builder.h:544
Alter_stage * m_stage
PFS stage monitoring.
Definition: ddl0impl-builder.h:600
dberr_t open() noexcept
Open the cursor.
Definition: ddl0builder.cc:401
uint64_t get_n_rows() const noexcept
Definition: ddl0builder.cc:472
File_cursors m_cursors
Cursors to use for parallel loading of the index.
Definition: ddl0impl-builder.h:594
File_cursor * m_cursor
Current cursor.
Definition: ddl0impl-builder.h:597
Physical row context.
Definition: ddl0impl.h:121
Information about temporary files used in merge sort.
Definition: ddl0impl.h:67
Structure for an SQL data field.
Definition: data0data.h:617
Data structure for a column in a table.
Definition: dict0mem.h:485
Data structure for a field in an index.
Definition: dict0mem.h:891
Data structure for an index.
Definition: dict0mem.h:1067
unsigned type
index type (DICT_CLUSTERED, DICT_UNIQUE, DICT_IBUF, DICT_CORRUPT)
Definition: dict0mem.h:1094
bool is_clustered() const
Definition: dict0mem.h:1314
Structure for an SQL data tuple of fields (logical record)
Definition: data0data.h:696
The info structure stored at the beginning of a heap block.
Definition: mem0mem.h:302
Definition: gen_lex_token.cc:149
#define IF_DEBUG(...)
Definition: univ.i:674
unsigned long int ulint
Definition: univ.i:406
#define ut_a(EXPR)
Abort execution if EXPR does not evaluate to nonzero.
Definition: ut0dbg.h:93