MySQL 8.4.9
Source Code Documentation
ddl0impl-buffer.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-buffer.h
29 DDL buffer infrastructure.
30 Created 2020-11-01 by Sunny Bains. */
31
32#ifndef ddl0impl_buffer_h
33#define ddl0impl_buffer_h
34
35#include "ddl0impl.h"
36#include "dict0dict.h"
37
38namespace ddl {
39
40/** Buffer for sorting in main memory. */
42 /** Callback for writing serialized data to to disk.
43 @param[in] io_buffer Buffer to persist - aligned to IO_BLOCK_SIZE.
44 @return DB_SUCCES or error code. */
45 using Function = std::function<dberr_t(IO_buffer io_buffer)>;
46
47 /** Constructor.
48 @param[in,out] index Sort buffer is for this index.
49 @param[in] size Sort buffer size in bytes. */
50 explicit Key_sort_buffer(dict_index_t *index, size_t size) noexcept;
51
52 /** Destructor. */
54
55 /** Sort the elements in m_dtuples.
56 @param[in,out] dup For collecting the duplicate rows. */
57 void sort(ddl::Dup *dup) noexcept;
58
59 /** Serialize the contents for storing to disk.
60 @param[in] io_buffer Buffer for serializing.
61 @param[in] persist Function for persisting the data.
62 @return DB_SUCCESS or error code. */
63 dberr_t serialize(IO_buffer io_buffer, Function persist) noexcept;
64
65 /** Reset the sort buffer. clear the heap and entries. */
66 void clear() noexcept;
67
68 /** @return true if the index is clustered. */
69 [[nodiscard]] bool is_clustered() const noexcept {
70 return m_index->is_clustered();
71 }
72
73 /** @return true if the index is an FTS index. */
74 [[nodiscard]] bool is_fts() const noexcept {
75 return m_index->type & DICT_FTS;
76 }
77
78 /** @return true if the index has a unique constraint. */
79 [[nodiscard]] bool is_unique() const noexcept {
81 }
82
83 /** @return the heap to use. */
84 [[nodiscard]] mem_heap_t *heap() noexcept { return m_heap; }
85
86 /** @return number of tuples stored so far. */
87 [[nodiscard]] size_t size() const noexcept { return m_n_tuples; }
88
89 /** @return true if the buffer is empty. */
90 [[nodiscard]] bool empty() const noexcept { return size() == 0; }
91
92 /** @return a references to the last element. */
93 [[nodiscard]] dfield_t *&back() noexcept {
94 ut_a(!empty());
95 return m_dtuples[size() - 1];
96 }
97
98 /** Allocate fields from the heap.
99 @param[in] n Number of fields to allocate.
100 @return an array of n dfields. */
101 dfield_t *alloc(size_t n) noexcept {
102 const auto sz = sizeof(dfield_t) * n;
103 m_total_size += sz;
104 return static_cast<dfield_t *>(mem_heap_alloc(m_heap, sz));
105 }
106
107 /** Pops the unfinished tuple which was allocated with alloc(), but
108 deep_copy() wasn't called yet for it yet. */
109 void pop_unfinished_tuple() noexcept {
110 ut_a(m_n_tuples + 1 == m_dtuples.size());
111 m_dtuples.pop_back();
112 }
113
114 /** Check if n bytes will fit in the buffer, or it is the first tuple,
115 in which case we ignore the limit to ensure forward progress.
116 @param[in] n Number of bytes to check.
117 @return true if n bytes can be added to the buffer. */
118 bool will_fit(size_t n) const noexcept {
119 /* Reserve one byte for the end marker and adjust for meta-data overhead. */
120 return m_n_tuples == 0 ||
121 m_total_size + m_dtuples.size() * 2 * sizeof(m_dtuples[0]) + n <=
122 m_buffer_size - 1;
123 }
124
125 /** Deep copy the field data starting from the back.
126 @param[in] n_fields Number of fields to copy.
127 @param[in] data_size Size in bytes of the data to copy. */
128 void deep_copy(size_t n_fields, size_t data_size) noexcept;
129
130 /** Compare two merge data tuples.
131 @param[in] lhs Fields to compare on the LHS
132 @param[in] rhs Fields to compare on the RHS
133 @param[in,out] dup For capturing duplicates (or nullptr).
134 @retval +ve - if lhs > rhs
135 @retval -ve - if lhs < rhs
136 @retval 0 - if lhs == rhs */
137 [[nodiscard]] static int compare(const dfield_t *lhs, const dfield_t *rhs,
138 Dup *dup) noexcept;
139
140 /** DTuple is an array of dfield_t objects. */
141 using DTuple = dfield_t *;
142 using DTuples = std::vector<DTuple, ut::allocator<DTuple>>;
143
144 /** Memory heap where allocated */
146
147 /** The index the tuples belong to */
149
150 /** Total amount of data allocated from m_heap, which includes:
151 i. one dfield_t[n_fields] array per row, allocated via alloc(n_fields) calls
152 ii. actual field's data cloned into the m_heap, via deep_clone(...,) calls
153 This is updated by alloc() and deep_clone().
154 It should roughly match the value of:
155 mem_heap_get_size(m_heap)-mem_block_get_free(UT_LIST_GET_LAST(m_heap->base)).
156 This, when combined with the memory consumption of m_dtuples, should not
157 exceed the m_buffer_size budget */
158 size_t m_total_size{};
159
160 /** Number of data tuples */
161 size_t m_n_tuples{};
162
163 /** Array of data tuples */
165
166 /** Buffer size. */
168};
169
170} // namespace ddl
171
172#endif /* !ddl0impl_buffer_h */
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
DDL implementation include file.
Data dictionary system.
static ulint dict_index_is_unique(const dict_index_t *index)
Check whether the index is unique.
constexpr uint32_t DICT_FTS
FTS index; can't be combined with the other flags.
Definition: dict0mem.h:104
static void * mem_heap_alloc(mem_heap_t *heap, ulint n)
Allocates n bytes of memory from a memory heap.
static void mem_heap_free(mem_heap_t *heap)
Frees the space occupied by a memory heap.
The general architecture is that the work is done in two phases, roughly the read and write phase.
Definition: btr0load.cc:42
std::pair< byte *, os_offset_t > IO_buffer
Block size for DDL I/O operations.
Definition: ddl0impl.h:47
Structure for reporting duplicate records.
Definition: ddl0ddl.h:132
Buffer for sorting in main memory.
Definition: ddl0impl-buffer.h:41
std::function< dberr_t(IO_buffer io_buffer)> Function
Callback for writing serialized data to to disk.
Definition: ddl0impl-buffer.h:45
void pop_unfinished_tuple() noexcept
Pops the unfinished tuple which was allocated with alloc(), but deep_copy() wasn't called yet for it ...
Definition: ddl0impl-buffer.h:109
bool is_fts() const noexcept
Definition: ddl0impl-buffer.h:74
bool will_fit(size_t n) const noexcept
Check if n bytes will fit in the buffer, or it is the first tuple, in which case we ignore the limit ...
Definition: ddl0impl-buffer.h:118
Key_sort_buffer(dict_index_t *index, size_t size) noexcept
Constructor.
Definition: ddl0buffer.cc:81
size_t m_buffer_size
Buffer size.
Definition: ddl0impl-buffer.h:167
size_t size() const noexcept
Definition: ddl0impl-buffer.h:87
dfield_t * alloc(size_t n) noexcept
Allocate fields from the heap.
Definition: ddl0impl-buffer.h:101
std::vector< DTuple, ut::allocator< DTuple > > DTuples
Definition: ddl0impl-buffer.h:142
static int compare(const dfield_t *lhs, const dfield_t *rhs, Dup *dup) noexcept
Compare two merge data tuples.
Definition: ddl0buffer.cc:245
bool is_clustered() const noexcept
Definition: ddl0impl-buffer.h:69
dberr_t serialize(IO_buffer io_buffer, Function persist) noexcept
Serialize the contents for storing to disk.
Definition: ddl0buffer.cc:116
size_t m_n_tuples
Number of data tuples.
Definition: ddl0impl-buffer.h:161
bool is_unique() const noexcept
Definition: ddl0impl-buffer.h:79
size_t m_total_size
Total amount of data allocated from m_heap, which includes: i.
Definition: ddl0impl-buffer.h:158
mem_heap_t * heap() noexcept
Definition: ddl0impl-buffer.h:84
dict_index_t * m_index
The index the tuples belong to.
Definition: ddl0impl-buffer.h:148
void deep_copy(size_t n_fields, size_t data_size) noexcept
Deep copy the field data starting from the back.
Definition: ddl0buffer.cc:86
DTuples m_dtuples
Array of data tuples.
Definition: ddl0impl-buffer.h:164
dfield_t *& back() noexcept
Definition: ddl0impl-buffer.h:93
~Key_sort_buffer() noexcept
Destructor.
Definition: ddl0impl-buffer.h:53
mem_heap_t * m_heap
Memory heap where allocated.
Definition: ddl0impl-buffer.h:145
void sort(ddl::Dup *dup) noexcept
Sort the elements in m_dtuples.
Definition: ddl0buffer.cc:103
void clear() noexcept
Reset the sort buffer.
Definition: ddl0buffer.cc:96
bool empty() const noexcept
Definition: ddl0impl-buffer.h:90
Structure for an SQL data field.
Definition: data0data.h:617
Data structure for an index.
Definition: dict0mem.h:1046
unsigned type
index type (DICT_CLUSTERED, DICT_UNIQUE, DICT_IBUF, DICT_CORRUPT)
Definition: dict0mem.h:1073
bool is_clustered() const
Definition: dict0mem.h:1311
The info structure stored at the beginning of a heap block.
Definition: mem0mem.h:302
#define ut_a(EXPR)
Abort execution if EXPR does not evaluate to nonzero.
Definition: ut0dbg.h:93
int n
Definition: xcom_base.cc:509