| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2020, 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/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" |
| 37 |
#include "ddl0impl-file-reader.h" |
| 38 |
#include "row0pread.h" |
| 39 |
|
| 40 |
namespace ddl { |
| 41 |
|
| 42 |
// Forward declaration. |
| 43 |
struct Copy_ctx; |
| 44 |
struct File_cursor; |
| 45 |
class RTree_inserter; |
| 46 |
|
| 47 |
/** For loading indexes. */ |
| 48 |
struct 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. */ |
| 58 |
SETUP_SORT, |
| 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. */ |
| 66 |
BTREE_BUILD, |
| 67 |
|
| 68 |
/** FTS sort and build, this is done in one "step" */ |
| 69 |
FTS_SORT_AND_BUILD, |
| 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/ */ |
| 88 |
~Builder() noexcept; |
| 89 |
|
| 90 |
/** @return the error status. */ |
| 91 |
dberr_t get_error() const noexcept { return m_ctx.get_error(); } |
| 92 |
|
| 93 |
/** Set the error code. |
| 94 |
@param[in] err Error code to set. */ |
| 95 |
void set_error(dberr_t err) noexcept { m_ctx.set_error(err, m_id); } |
| 96 |
|
| 97 |
/** @return the instance ID. */ |
| 98 |
[[nodiscard]] size_t id() const noexcept { return m_id; } |
| 99 |
|
| 100 |
/** @return the index being built. */ |
| 101 |
[[nodiscard]] dict_index_t *index() noexcept { return m_sort_index; } |
| 102 |
|
| 103 |
/** @return the DDL context. */ |
| 104 |
Context &ctx() noexcept { return m_ctx; } |
| 105 |
|
| 106 |
/** Parallel scan thread spawn failed, release the extra thread states. */ |
| 107 |
void fallback_to_single_thread() noexcept; |
| 108 |
|
| 109 |
/** @return true if the index is a spatial index. */ |
| 110 |
[[nodiscard]] bool is_spatial_index() const noexcept { |
| 111 |
return dict_index_is_spatial(m_index); |
| 112 |
} |
| 113 |
|
| 114 |
/** @return true if the index is an FTS index. */ |
| 115 |
[[nodiscard]] bool is_fts_index() const noexcept { |
| 116 |
return m_index->type & DICT_FTS; |
| 117 |
} |
| 118 |
|
| 119 |
/** @return true if the index is a unique index. */ |
| 120 |
[[nodiscard]] bool is_unique_index() const noexcept { |
| 121 |
ut_a(!is_fts_index()); |
| 122 |
return dict_index_is_unique(m_sort_index); |
| 123 |
} |
| 124 |
|
| 125 |
/** @return the current builder state. */ |
| 126 |
[[nodiscard]] State get_state() const noexcept { |
| 127 |
return m_state.load(std::memory_order_seq_cst); |
| 128 |
} |
| 129 |
|
| 130 |
/** Set the next state. |
| 131 |
@param[in] state State to set. */ |
| 132 |
void set_state(State state) noexcept { |
| 133 |
m_state.store(state, std::memory_order_seq_cst); |
| 134 |
} |
| 135 |
|
| 136 |
/** @return the PFS instance that is used to report progress (or nullptr). */ |
| 137 |
Alter_stage *stage() noexcept { return m_local_stage; } |
| 138 |
|
| 139 |
/** Set the next state. */ |
| 140 |
void set_next_state() noexcept; |
| 141 |
|
| 142 |
/** Initialize the cursor. |
| 143 |
@param[in,out] cursor Cursor to initialize. |
| 144 |
@param[in] n_threads Number of threads used for reading. |
| 145 |
@return DB_SUCCESS or error code. */ |
| 146 |
[[nodiscard]] dberr_t init(Cursor &cursor, size_t n_threads) noexcept; |
| 147 |
|
| 148 |
/** Add a row to the merge buffer. |
| 149 |
@param[in,out] cursor Current scan cursor. |
| 150 |
@param[in,out] row Row to add. |
| 151 |
@param[in] thread_id ID of current thread. |
| 152 |
@param[in,out] latch_release Called when a log free check is required. |
| 153 |
@return DB_SUCCESS or error code. */ |
| 154 |
[[nodiscard]] dberr_t add_row(Cursor &cursor, Row &row, size_t thread_id, |
| 155 |
Latch_release &&latch_release) noexcept; |
| 156 |
|
| 157 |
/** @return true if file sorting can be skipped. */ |
| 158 |
bool is_skip_file_sort() const noexcept { |
| 159 |
return m_ctx.m_skip_pk_sort && m_sort_index->is_clustered(); |
| 160 |
} |
| 161 |
|
| 162 |
/** FTS: Sort and insert the rows read. |
| 163 |
@return DB_SUCCESS or error code. */ |
| 164 |
[[nodiscard]] dberr_t fts_sort_and_build() noexcept; |
| 165 |
|
| 166 |
/** Non-FTS: Sort the rows read. |
| 167 |
@return DB_SUCCESS or error code. */ |
| 168 |
[[nodiscard]] dberr_t setup_sort() noexcept; |
| 169 |
|
| 170 |
/** Non-FTS: Sort the rows read. |
| 171 |
@param[in] thread_id Thread state ID. |
| 172 |
@return DB_SUCCESS or error code. */ |
| 173 |
[[nodiscard]] dberr_t merge_sort(size_t thread_id) noexcept; |
| 174 |
|
| 175 |
/** Load the sorted data into the B+Tree. |
| 176 |
@return DB_SUCESS or error code. */ |
| 177 |
[[nodiscard]] dberr_t btree_build() noexcept; |
| 178 |
|
| 179 |
/** Close temporary files, Flush all dirty pages, apply the row log |
| 180 |
and write the redo log record. |
| 181 |
@return DB_SUCCESS or error code. */ |
| 182 |
dberr_t finish() noexcept; |
| 183 |
|
| 184 |
/** Copy blobs to the tuple. |
| 185 |
@param[out] dtuple Tuple to copy to. |
| 186 |
@param[in,out] offsets Column offsets in the row. |
| 187 |
@param[in] mrec Current row. |
| 188 |
@param[in,out] heap Heap for the allocating tuple memory. |
| 189 |
@return DB_SUCCESS or error code. */ |
| 190 |
[[nodiscard]] dberr_t dtuple_copy_blobs(dtuple_t *dtuple, ulint *offsets, |
| 191 |
const mrec_t *mrec, |
| 192 |
mem_heap_t *heap) noexcept; |
| 193 |
|
| 194 |
/** Write data to disk - in append mode. Increment the file size. |
| 195 |
@param[in,out] file File handle. |
| 196 |
@param[in] file_buffer Write the buffer contents to disk. |
| 197 |
@return DB_SUCCESS or error code. */ |
| 198 |
[[nodiscard]] dberr_t append(ddl::file_t &file, |
| 199 |
IO_buffer file_buffer) noexcept; |
| 200 |
|
| 201 |
/** @return the path for temporary files. */ |
| 202 |
const char *tmpdir() const noexcept { return m_tmpdir; } |
| 203 |
|
| 204 |
/** Insert cached rows. |
| 205 |
@param[in] thread_id Insert cached rows for this thread ID. |
| 206 |
@param[in,out] latch_release Called when a log free check is required. |
| 207 |
@return DB_SUCCESS or error number */ |
| 208 |
[[nodiscard]] dberr_t batch_insert(size_t thread_id, |
| 209 |
Latch_release &&latch_release) noexcept; |
| 210 |
|
| 211 |
/** Note that the latches are going to be released. Do a deep copy of the |
| 212 |
tuples that are being inserted in batches by batch_insert |
| 213 |
@param[in] thread_id Deep copy cached rows for this thread ID. */ |
| 214 |
void batch_insert_deep_copy_tuples(size_t thread_id) noexcept; |
| 215 |
|
| 216 |
/** Check the state of the online build log for the index. |
| 217 |
@return DB_SUCCESS or error code. */ |
| 218 |
[[nodiscard]] dberr_t check_state_of_online_build_log() noexcept; |
| 219 |
|
| 220 |
/** Write an MLOG_INDEX_LOAD record to indicate in the redo-log |
| 221 |
that redo-logging of individual index pages was disabled, and |
| 222 |
the flushing of such pages to the data files was completed. |
| 223 |
@param[in] index Index on which redo logging was disabled */ |
| 224 |
static void write_redo(const dict_index_t *index) noexcept; |
| 225 |
|
| 226 |
private: |
| 227 |
/** State of a cluster index reader thread. */ |
| 228 |
struct Thread_ctx { |
| 229 |
/** Constructor. |
| 230 |
@param[in] id Thread state ID. |
| 231 |
@param[in,out] key_buffer Buffer for building the target index. Note, the |
| 232 |
thread state will own the key buffer and is |
| 233 |
responsible for deleting it. */ |
| 234 |
explicit Thread_ctx(size_t id, Key_sort_buffer *key_buffer) noexcept; |
| 235 |
|
| 236 |
/** Destructor. */ |
| 237 |
~Thread_ctx() noexcept; |
| 238 |
|
| 239 |
/** Thread ID. */ |
| 240 |
size_t m_id{}; |
| 241 |
|
| 242 |
/** Key sort buffer. */ |
| 243 |
Key_sort_buffer *m_key_buffer{}; |
| 244 |
|
| 245 |
/** Total number of records added to the key sort buffer. */ |
| 246 |
size_t m_n_recs{}; |
| 247 |
|
| 248 |
/** Merge file handle. */ |
| 249 |
ddl::file_t m_file{}; |
| 250 |
|
| 251 |
/** Buffer to use for file writes. */ |
| 252 |
ut::unique_ptr_aligned<byte[]> m_aligned_buffer{}; |
| 253 |
|
| 254 |
/** Buffer to use for file writes. */ |
| 255 |
IO_buffer m_io_buffer; |
| 256 |
|
| 257 |
/** Record list starting offset in the output file. */ |
| 258 |
Merge_offsets m_offsets{}; |
| 259 |
|
| 260 |
/** For spatial/Rtree rows handling. */ |
| 261 |
RTree_inserter *m_rtree_inserter{}; |
| 262 |
}; |
| 263 |
|
| 264 |
using Allocator = ut::allocator<Thread_ctx *>; |
| 265 |
using Thread_ctxs = std::vector<Thread_ctx *, Allocator>; |
| 266 |
|
| 267 |
/** Create the tasks to merge Sort the file before we load the file into |
| 268 |
the Btree index. |
| 269 |
@return DB_SUCCESS or error code. */ |
| 270 |
[[nodiscard]] dberr_t create_merge_sort_tasks() noexcept; |
| 271 |
|
| 272 |
/** Flush all dirty pages, apply the row log and write the redo log record. |
| 273 |
@return DB_SUCCESS or error code. */ |
| 274 |
dberr_t finalize() noexcept; |
| 275 |
|
| 276 |
/** Convert the field data from compact to redundant format. |
| 277 |
@param[in] clust_index Clustered index being built |
| 278 |
@param[in] row_field Field to copy from |
| 279 |
@param[out] field Field to copy to |
| 280 |
@param[in] len Length of the field data |
| 281 |
@param[in] page_size Compressed BLOB page size |
| 282 |
@param[in] is_sdi true for SDI Indexes |
| 283 |
@param[in,out] heap Memory heap where to allocate |
| 284 |
data when converting to ROW_FORMAT=REDUNDANT, |
| 285 |
or nullptr */ |
| 286 |
static void convert(const dict_index_t *clust_index, |
| 287 |
const dfield_t *row_field, dfield_t *field, ulint len, |
| 288 |
const page_size_t &page_size, |
| 289 |
IF_DEBUG(bool is_sdi, ) mem_heap_t *heap) noexcept; |
| 290 |
|
| 291 |
/** Copy externally stored columns to the data tuple. |
| 292 |
@param[in] index Index dictionary object. |
| 293 |
@param[in] mrec Record containing BLOB pointers, or |
| 294 |
nullptr to use tuple instead. |
| 295 |
@param[in] offsets Offsets of mrec. |
| 296 |
@param[in] page_size Compressed page size in bytes, or 0 |
| 297 |
@param[in,out] tuple Data tuple. |
| 298 |
@param[in] is_sdi True for SDI Indexes |
| 299 |
@param[in,out] heap Memory heap */ |
| 300 |
static void copy_blobs(const dict_index_t *index, const mrec_t *mrec, |
| 301 |
const ulint *offsets, const page_size_t &page_size, |
| 302 |
dtuple_t *tuple, |
| 303 |
IF_DEBUG(bool is_sdi, ) mem_heap_t *heap) noexcept; |
| 304 |
|
| 305 |
/** Cache a row for batch inserts. Currently used by spatial indexes. |
| 306 |
@param[in,out] row Row to add. |
| 307 |
@param[in] thread_id ID of current thread. |
| 308 |
@return DB_SUCCESS or error code. */ |
| 309 |
[[nodiscard]] dberr_t batch_add_row(Row &row, size_t thread_id) noexcept; |
| 310 |
|
| 311 |
/** Add a row to the merge buffer. |
| 312 |
@param[in,out] cursor Current scan cursor. |
| 313 |
@param[in,out] row Row to add. |
| 314 |
@param[in] thread_id ID of current thread. |
| 315 |
@param[in,out] latch_release Called when a log free check is required. |
| 316 |
@return DB_SUCCESS or error code. */ |
| 317 |
[[nodiscard]] dberr_t bulk_add_row(Cursor &cursor, Row &row, size_t thread_id, |
| 318 |
Latch_release &&latch_release) noexcept; |
| 319 |
|
| 320 |
/** Clear the heap used for virtual columns. */ |
| 321 |
void clear_virtual_heap() noexcept { m_v_heap.clear(); } |
| 322 |
|
| 323 |
/** Add the FTS document ID to the destination field. |
| 324 |
@param[in,out] dst Field to write to. |
| 325 |
@param[in] src Field to copy meta data from. |
| 326 |
@param[out] write_doc_id Buffer for copying the doc id. */ |
| 327 |
void fts_add_doc_id(dfield_t *dst, const dict_field_t *src, |
| 328 |
doc_id_t &write_doc_id) noexcept; |
| 329 |
|
| 330 |
/** Add a row to the write buffer. |
| 331 |
@param[in,out] ctx Copy context. |
| 332 |
@param[in,out] mv_rows_added Number of multi-value rows added. |
| 333 |
@return DB_SUCCESS or error code. */ |
| 334 |
[[nodiscard]] dberr_t copy_row(Copy_ctx &ctx, size_t &mv_rows_added) noexcept; |
| 335 |
|
| 336 |
/** Setup the virtual column src column. |
| 337 |
@param[in,out] ctx Copy context. |
| 338 |
@param[in] ifield Index field. |
| 339 |
@param[in] col Table column. |
| 340 |
@param[out] src_field Computed value. |
| 341 |
@param[in,out] mv_rows_added Number of multi-value rows added. |
| 342 |
@return DB_SUCCESS or error code. */ |
| 343 |
[[nodiscard]] dberr_t get_virtual_column(Copy_ctx &ctx, |
| 344 |
const dict_field_t *ifield, |
| 345 |
dict_col_t *col, |
| 346 |
dfield_t *&src_field, |
| 347 |
size_t &mv_rows_added) noexcept; |
| 348 |
|
| 349 |
/** Enqueue parsing of the field of the row |
| 350 |
@param[in] row The row which contains the field to parse |
| 351 |
@param[in] field_no Which field to parse |
| 352 |
@return DB_SUCCESS or error code. */ |
| 353 |
[[nodiscard]] dberr_t enqueue_parsing(const dtuple_t &row, |
| 354 |
size_t field_no) noexcept; |
| 355 |
|
| 356 |
/** Enqueue parsing of the row. Asserts we are building FTS index only. |
| 357 |
@param[in] row The row which contains fields to parse. |
| 358 |
@return DB_SUCCESS or error code. */ |
| 359 |
[[nodiscard]] dberr_t enqueue_parsing(const dtuple_t &row) noexcept; |
| 360 |
|
| 361 |
/** Copy the columns to the temporary file buffer. |
| 362 |
@param[in,out] ctx Copy context. |
| 363 |
@param[in,out] mv_rows_added Multi value rows added. |
| 364 |
@param[in,out] write_doc_id Buffer for storing the FTS doc ID. |
| 365 |
@return DB_SUCCESS or error code. */ |
| 366 |
[[nodiscard]] dberr_t copy_columns(Copy_ctx &ctx, size_t &mv_rows_added, |
| 367 |
doc_id_t &write_doc_id) noexcept; |
| 368 |
|
| 369 |
/** Add row to the key buffer. |
| 370 |
@param[in,out] ctx Copy context. |
| 371 |
@param[in,out] mv_rows_added Number of multi-value index rows added. |
| 372 |
@return DB_SUCCESS or error code. */ |
| 373 |
[[nodiscard]] dberr_t add_to_key_buffer(Copy_ctx &ctx, |
| 374 |
size_t &mv_rows_added) noexcept; |
| 375 |
|
| 376 |
/** Wait for FTS completion. |
| 377 |
@param[in] index Index being built. */ |
| 378 |
void fts_wait_for_completion(const dict_index_t *index) noexcept; |
| 379 |
|
| 380 |
/** Sort the data in the key buffer. |
| 381 |
@param[in] thread_id Thread ID of current thread. |
| 382 |
@return DB_SUCCESS or error code. */ |
| 383 |
[[nodiscard]] dberr_t key_buffer_sort(size_t thread_id) noexcept; |
| 384 |
|
| 385 |
/** Sort the buffer in memory and insert directly in the BTree loader, |
| 386 |
don't write to a temporary file. |
| 387 |
@param[in,out] cursor Current scan cursor. |
| 388 |
@param[in] thread_id ID of current thread. |
| 389 |
@return DB_SUCCESS or error code. */ |
| 390 |
dberr_t insert_direct(Cursor &cursor, size_t thread_id) noexcept; |
| 391 |
|
| 392 |
/** Create the merge file, if needed. |
| 393 |
@param[in,out] file File handle. |
| 394 |
@return true if file was opened successfully . */ |
| 395 |
[[nodiscard]] bool create_file(ddl::file_t &file) noexcept; |
| 396 |
|
| 397 |
/** Check for duplicates in the first block |
| 398 |
@param[in] dupcheck Files to check for duplicates. |
| 399 |
@param[in,out] dup For collecting duplicate key information. |
| 400 |
@return DB_SUCCESS or error code. */ |
| 401 |
[[nodiscard]] dberr_t check_duplicates(Thread_ctxs &dupcheck, |
| 402 |
Dup *dup) noexcept; |
| 403 |
|
| 404 |
/** Cleanup DDL after error in online build |
| 405 |
Note: To be called if DDL must cleanup due to error in online build. Pages |
| 406 |
which are buffer-fixed (in Page_load::release) until the next iteration, must |
| 407 |
be unfixed (with Page_load::latch) before returning the error. |
| 408 |
@note: Assumes that either m_btr_load->release is called before or |
| 409 |
m_n_recs is 0 (no records are inserted yet). |
| 410 |
@param[in] err Error hit in online build |
| 411 |
@return the cursor error status. */ |
| 412 |
[[nodiscard]] dberr_t handle_error(dberr_t err) noexcept; |
| 413 |
|
| 414 |
private: |
| 415 |
/** Buffer ID. */ |
| 416 |
size_t m_id{}; |
| 417 |
|
| 418 |
/** Initial phase. */ |
| 419 |
std::atomic<State> m_state{State::INIT}; |
| 420 |
|
| 421 |
/** DDL Context. */ |
| 422 |
ddl::Context &m_ctx; |
| 423 |
|
| 424 |
/** Loader that owns the instance. */ |
| 425 |
Loader &m_loader; |
| 426 |
|
| 427 |
/** Index to create (if not FTS index). */ |
| 428 |
dict_index_t *m_index{}; |
| 429 |
|
| 430 |
/** Temporary file path. */ |
| 431 |
const char *m_tmpdir{}; |
| 432 |
|
| 433 |
/** Per thread context. */ |
| 434 |
Thread_ctxs m_thread_ctxs{}; |
| 435 |
|
| 436 |
/** For tracking duplicates. */ |
| 437 |
dfield_t *m_prev_fields{}; |
| 438 |
|
| 439 |
/** For collecting duplicate entries (error reporting). */ |
| 440 |
Dup m_clust_dup{}; |
| 441 |
|
| 442 |
/** Scoped virtual column heap. */ |
| 443 |
Scoped_heap m_v_heap{}; |
| 444 |
|
| 445 |
/** Scoped conversion heap. */ |
| 446 |
Scoped_heap m_conv_heap{}; |
| 447 |
|
| 448 |
/** The index to be built, FTS or non-FTS. */ |
| 449 |
dict_index_t *m_sort_index{}; |
| 450 |
|
| 451 |
/** Number of active sort tasks. */ |
| 452 |
std::atomic<size_t> m_n_sort_tasks{}; |
| 453 |
|
| 454 |
/** Cluster index bulk load instance to use, direct insert without |
| 455 |
a file sort. */ |
| 456 |
Btree_load *m_btr_load{}; |
| 457 |
|
| 458 |
/** Stage per builder. */ |
| 459 |
Alter_stage *m_local_stage{}; |
| 460 |
}; |
| 461 |
|
| 462 |
struct Load_cursor : Btree_load::Cursor { |
| 463 |
/** Default constructor. */ |
| 464 |
explicit Load_cursor(Builder *builder, Dup *dup) noexcept |
| 465 |
: m_dup(dup), m_builder(builder) {} |
| 466 |
|
| 467 |
/** Default destructor. */ |
| 468 |
virtual ~Load_cursor() override = default; |
| 469 |
|
| 470 |
/** @return the cursor error status. */ |
| 471 |
[[nodiscard]] dberr_t get_err() const noexcept { return m_err; } |
| 472 |
|
| 473 |
/** @return true if duplicates detected. */ |
| 474 |
[[nodiscard]] bool duplicates_detected() const noexcept override; |
| 475 |
|
| 476 |
/** Duplicate checking and reporting. */ |
| 477 |
Dup *m_dup{}; |
| 478 |
|
| 479 |
/** Operation error code. */ |
| 480 |
dberr_t m_err{DB_SUCCESS}; |
| 481 |
|
| 482 |
/** Index meta data. */ |
| 483 |
Builder *m_builder{}; |
| 484 |
|
| 485 |
/** Heap for the raw row to dtuple_t conversion. */ |
| 486 |
Scoped_heap m_tuple_heap{}; |
| 487 |
}; |
| 488 |
|
| 489 |
/** Merge the sorted files. */ |
| 490 |
struct Merge_cursor : public Load_cursor { |
| 491 |
/** File cursors to use for the scan. */ |
| 492 |
using File_readers = std::vector<File_reader *, ut::allocator<File_reader *>>; |
| 493 |
|
| 494 |
/** Constructor. |
| 495 |
@param[in,out] builder Index builder. |
| 496 |
@param[in,out] dup If not nullptr, then report duplicates. |
| 497 |
@param[in,out] stage PFS stage monitoring. */ |
| 498 |
explicit Merge_cursor(Builder *builder, Dup *dup, |
| 499 |
Alter_stage *stage) noexcept; |
| 500 |
|
| 501 |
/** Destructor. */ |
| 502 |
~Merge_cursor() noexcept override; |
| 503 |
|
| 504 |
/** Add the cursor to use for merge load. |
| 505 |
@param[in] file File to merge from. |
| 506 |
@param[in] buffer_size IO buffer size to use for reading. |
| 507 |
@return DB_SUCCESS or error code. */ |
| 508 |
[[nodiscard]] dberr_t add_file(const ddl::file_t &file, |
| 509 |
size_t buffer_size) noexcept; |
| 510 |
|
| 511 |
/** Add the cursor to use for merge load. |
| 512 |
@param[in] file File file to read. |
| 513 |
@param[in] buffer_size IO buffer size to use for reading. |
| 514 |
@param[in] range Range to read from |
| 515 |
@return DB_SUCCESS or error code. */ |
| 516 |
[[nodiscard]] dberr_t add_file(const ddl::file_t &file, size_t buffer_size, |
| 517 |
const Range &range) noexcept; |
| 518 |
|
| 519 |
/** Open the cursor. |
| 520 |
@return DB_SUCCESS or error code. */ |
| 521 |
[[nodiscard]] dberr_t open() noexcept; |
| 522 |
|
| 523 |
/** Fetch the current row as a tuple. Note: Tuple columns are shallow copies. |
| 524 |
@param[out] dtuple Row represented as a tuple. |
| 525 |
@return DB_SUCCESS, DB_END_OF_INDEX or error code. */ |
| 526 |
[[nodiscard]] dberr_t fetch(dtuple_t *&dtuple) noexcept override; |
| 527 |
|
| 528 |
/** Fetch the current row. |
| 529 |
@param[out] mrec Current merge record. |
| 530 |
@param[out] offsets Columns offsets inside mrec. |
| 531 |
@return DB_SUCCESS, DB_END_OF_INDEX or error code. */ |
| 532 |
[[nodiscard]] dberr_t fetch(const mrec_t *&mrec, ulint *&offsets) noexcept; |
| 533 |
|
| 534 |
/** Move to the next record. |
| 535 |
@return DB_SUCCESS, DB_END_OF_INDEX or error code. */ |
| 536 |
[[nodiscard]] dberr_t next() noexcept override; |
| 537 |
|
| 538 |
/** @return the file reader instances. */ |
| 539 |
[[nodiscard]] File_readers file_readers() noexcept; |
| 540 |
|
| 541 |
/** Add the active cursors to the priority queue. */ |
| 542 |
void clear_eof() noexcept; |
| 543 |
|
| 544 |
/** @return the number of active readers. */ |
| 545 |
[[nodiscard]] size_t size() const noexcept { return m_pq.size(); } |
| 546 |
|
| 547 |
/** @return the number of rows read from the files. */ |
| 548 |
[[nodiscard]] uint64_t get_n_rows() const noexcept; |
| 549 |
|
| 550 |
/** @return the number of cursors being merged. */ |
| 551 |
[[nodiscard]] size_t number_of_cursors() const noexcept { |
| 552 |
return m_cursors.size(); |
| 553 |
} |
| 554 |
|
| 555 |
private: |
| 556 |
/** @return the current cursor at the head of the queue. */ |
| 557 |
[[nodiscard]] File_cursor *pop() noexcept; |
| 558 |
|
| 559 |
private: |
| 560 |
/** Comparator. */ |
| 561 |
struct Compare { |
| 562 |
/** Constructor. |
| 563 |
@param[in] index Index that the rows belong to. |
| 564 |
@param[in,out] dup For reporting duplicates, can be nullptr. */ |
| 565 |
explicit Compare(const dict_index_t *index, Dup *dup) |
| 566 |
: m_dup(dup), m_index(index) {} |
| 567 |
|
| 568 |
/** Destructor. */ |
| 569 |
Compare() = default; |
| 570 |
|
| 571 |
/** Compare the keys of two cursors. |
| 572 |
@param[in] lhs Left hand side. |
| 573 |
@param[in] rhs Right hand side. |
| 574 |
@return true if lhs strictly less than rhs. */ |
| 575 |
bool operator()(const File_cursor *lhs, |
| 576 |
const File_cursor *rhs) const noexcept; |
| 577 |
|
| 578 |
/** For reporting duplicates. */ |
| 579 |
Dup *m_dup{}; |
| 580 |
|
| 581 |
/** Index being built. */ |
| 582 |
const dict_index_t *m_index{}; |
| 583 |
}; |
| 584 |
|
| 585 |
/** File cursors to use for the scan. */ |
| 586 |
using File_cursors = std::vector<File_cursor *, ut::allocator<File_cursor *>>; |
| 587 |
|
| 588 |
/** Priority queue for ordering the rows. */ |
| 589 |
using Queue = std::priority_queue<File_cursor *, File_cursors, Compare>; |
| 590 |
|
| 591 |
/** Priority queue for merging the file cursors. */ |
| 592 |
Queue m_pq{}; |
| 593 |
|
| 594 |
/** Cursors to use for parallel loading of the index. */ |
| 595 |
File_cursors m_cursors{}; |
| 596 |
|
| 597 |
/** Current cursor. */ |
| 598 |
File_cursor *m_cursor{}; |
| 599 |
|
| 600 |
/** PFS stage monitoring. */ |
| 601 |
Alter_stage *m_stage{}; |
| 602 |
}; |
| 603 |
|
| 604 |
} // namespace ddl |
| 605 |
|
| 606 |
#endif /* !ddl0impl_builder_h */ |