| 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-merge.h |
| 29 |
DDL cluster merge sort data structures. |
| 30 |
Created 2020-11-01 by Sunny Bains. */ |
| 31 |
|
| 32 |
#ifndef ddl0impl_merge_h |
| 33 |
#define ddl0impl_merge_h |
| 34 |
|
| 35 |
#include <vector> |
| 36 |
|
| 37 |
#include "ddl0impl-file-reader.h" |
| 38 |
|
| 39 |
namespace ddl { |
| 40 |
|
| 41 |
// Forward declaration. |
| 42 |
struct Builder; |
| 43 |
|
| 44 |
/** Merge the blocks in the file. */ |
| 45 |
struct Merge_file_sort { |
| 46 |
// Forward declarations. |
| 47 |
struct Cursor; |
| 48 |
struct Output_file; |
| 49 |
|
| 50 |
/** The design is generalized as an N way merge, however we stick |
| 51 |
with 2 for now. */ |
| 52 |
static constexpr size_t N_WAY_MERGE = 2; |
| 53 |
|
| 54 |
/** Context to use for merging the files/runs. */ |
| 55 |
struct Context { |
| 56 |
/** File to sort. */ |
| 57 |
ddl::file_t *m_file{}; |
| 58 |
|
| 59 |
/** For reporting duplicates, it has the index instance too. */ |
| 60 |
Dup *m_dup{}; |
| 61 |
|
| 62 |
/** Number of scan threads used, for memory buffer calculation. */ |
| 63 |
size_t m_n_threads{}; |
| 64 |
|
| 65 |
/** PFS progress monitoring. */ |
| 66 |
Alter_stage *m_stage{}; |
| 67 |
}; |
| 68 |
|
| 69 |
/** Offsets of record lists to merge. Two adjacent entries make one list or |
| 70 |
range */ |
| 71 |
using Ranges = std::vector<os_offset_t>; |
| 72 |
|
| 73 |
/** Constructor. |
| 74 |
@param[in,out] merge_ctx Data blocks merge meta data. */ |
| 75 |
explicit Merge_file_sort(Context *merge_ctx) noexcept |
| 76 |
: m_merge_ctx(merge_ctx) {} |
| 77 |
|
| 78 |
/** Merge the the blocks. |
| 79 |
@param[in,out] builder Builder instance used for building index. |
| 80 |
@param[in,out] offsets Offsets from where to start the merge. |
| 81 |
@return DB_SUCCESS or error code. */ |
| 82 |
[[nodiscard]] dberr_t sort(Builder *builder, Merge_offsets &offsets) noexcept; |
| 83 |
|
| 84 |
/** @return the number of rows in the sorted file. */ |
| 85 |
[[nodiscard]] uint64_t get_n_rows() const noexcept { return m_n_rows; } |
| 86 |
|
| 87 |
private: |
| 88 |
/** Merge the rows. |
| 89 |
@param[in,out] cursor To iterate over the rows to merge. |
| 90 |
@param[in,out] output_file Output file to write the merged rows. |
| 91 |
@return DB_SUCCESS or error code. */ |
| 92 |
[[nodiscard]] dberr_t merge_rows(Cursor &cursor, |
| 93 |
Output_file &output_file) noexcept; |
| 94 |
|
| 95 |
/** Merge the blocks in the ranges. |
| 96 |
@param[in,out] cursor To iterate over the rows to merge. |
| 97 |
@param[in,out] offsets Starting offsets of record lists to merge. |
| 98 |
@param[in,out] output_file Output file to write the merged rows. |
| 99 |
@param[in] buffer_size IO buffer size for reads. |
| 100 |
@return DB_SUCCESS or error code. */ |
| 101 |
[[nodiscard]] dberr_t merge_ranges(Cursor &cursor, Merge_offsets &offsets, |
| 102 |
Output_file &output_file, |
| 103 |
size_t buffer_size) noexcept; |
| 104 |
|
| 105 |
/** Move to the next ranges of pages to merge. |
| 106 |
@param[in,out] offsets Current offsets to start the merge from. |
| 107 |
@return the next range to merge. */ |
| 108 |
Ranges next_ranges(Merge_offsets &offsets) noexcept; |
| 109 |
|
| 110 |
private: |
| 111 |
/** To check and report duplicates. */ |
| 112 |
Dup *m_dup{}; |
| 113 |
|
| 114 |
/** Meta data for merging blocks. */ |
| 115 |
Context *m_merge_ctx{}; |
| 116 |
|
| 117 |
/** Page numbers to merge for the next pass. */ |
| 118 |
Merge_offsets m_next_offsets{}; |
| 119 |
|
| 120 |
/** Number of rows in the sorted file. */ |
| 121 |
uint64_t m_n_rows{}; |
| 122 |
}; |
| 123 |
|
| 124 |
} // namespace ddl |
| 125 |
|
| 126 |
#endif /* !ddl0impl_merge_h */ |