MySQL 8.0.32
Source Code Documentation
ddl0impl-buffer.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2020, 2022, 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/ddl0impl-buffer.h
28 DDL buffer infrastrucure.
29 Created 2020-11-01 by Sunny Bains. */
30
31#ifndef ddl0impl_buffer_h
32#define ddl0impl_buffer_h
33
34#include "ddl0impl.h"
35#include "dict0dict.h"
36
37namespace ddl {
38
39/** For DDL memory allocations that use the mem_key_ddl handle. */
40struct PFS_buffer : private ut::Non_copyable {
41 using Type = byte;
43
44 /** Constructor. */
45 PFS_buffer() = default;
46
47 /** Destructor. */
48 ~PFS_buffer() noexcept { deallocate(); }
49
50 /** @return allocated and initialized memory or nullptr on failure.
51 @param[in] size Number of bytes to allocate. */
52 byte *allocate(size_t size) noexcept {
53 ut_a(m_size == 0);
54 ut_a(m_ptr == nullptr);
55
56 m_ptr = static_cast<byte *>(
59
60 if (m_ptr != nullptr) {
61 m_size = size;
62 memset(m_ptr, 0x0, m_size);
63 }
64
65 return m_ptr;
66 }
67
68 /** Deallocate the memory. */
69 void deallocate() noexcept {
71 m_ptr = nullptr;
72 }
73
74 /** Pointer to allocated buffer. */
75 byte *m_ptr{};
76
77 /** Number of bytes allocated. */
78 size_t m_size{};
79};
80
81/** Buffer used for reading and writing to the temporary files. */
83 /** Constructor. */
84 Aligned_buffer() = default;
85
86 /** Destructor. */
87 ~Aligned_buffer() = default;
88
89 /** Allocate the buffer.
90 @param[in] size Size of the buffer.
91 @return true on success. */
92 bool allocate(size_t size) noexcept {
93 ut_a(m_io_buffer.second == 0);
94 ut_a(m_io_buffer.first == nullptr);
95
96 /* Extra space to align memory for O_DIRECT. */
97 auto ptr = m_buffer.allocate(size + UNIV_SECTOR_SIZE);
98
99 if (ptr == nullptr) {
100 return false;
101 }
102
103 m_io_buffer.second = size;
104 m_io_buffer.first = static_cast<byte *>(ut_align(ptr, UNIV_SECTOR_SIZE));
105
106 return true;
107 }
108
109 /** Get the IO buffer.
110 @return the io buffer suitably aligned. */
111 IO_buffer io_buffer() noexcept { return m_io_buffer; }
112
113 private:
114 /** Raw buffer (unaligned pointer). */
116
117 /** The IO buffer. */
119};
120
121/** Buffer for sorting in main memory. */
123 /** Callback for writing serialized data to to disk.
124 @param[in] io_buffer Buffer to persist.
125 @param[in,out] n Number of bytes written is returned.
126 Input value semantics:
127 0 - Write up to aligned length.
128 >0 - All data will be written and
129 last block will be padded with zeros.
130 @return DB_SUCCES or error code. */
131 using Function = std::function<dberr_t(IO_buffer io_buffer, os_offset_t &n)>;
132
133 /** Constructor.
134 @param[in,out] index Sort buffer is for this index.
135 @param[in] size Sort buffer size in bytes. */
136 explicit Key_sort_buffer(dict_index_t *index, size_t size) noexcept;
137
138 /** Destructor. */
140
141 /** Sort the elements in m_dtuples.
142 @param[in,out] dup For collecting the duplicate rows. */
143 void sort(ddl::Dup *dup) noexcept;
144
145 /** Serialize the contents for storing to disk.
146 @param[in] io_buffer Buffer for serializing.
147 @param[in] f Function for persisting the data.
148 @return DB_SUCCESS or error code. */
149 dberr_t serialize(IO_buffer io_buffer, Function &&f) noexcept;
150
151 /** Reset the sort buffer. clear the heap and entries. */
152 void clear() noexcept;
153
154 /** @return true if the index is clustered. */
155 [[nodiscard]] bool is_clustered() const noexcept {
156 return m_index->is_clustered();
157 }
158
159 /** @return true if the index is an FTS index. */
160 [[nodiscard]] bool is_fts() const noexcept {
161 return m_index->type & DICT_FTS;
162 }
163
164 /** @return true if the index has a unique constraint. */
165 [[nodiscard]] bool is_unique() const noexcept {
167 }
168
169 /** @return the heap to use. */
170 [[nodiscard]] mem_heap_t *heap() noexcept { return m_heap; }
171
172 /** @return number of tuples stored so far. */
173 [[nodiscard]] size_t size() const noexcept { return m_n_tuples; }
174
175 /** @return true if the buffer is full. */
176 [[nodiscard]] bool full() const noexcept { return size() >= m_max_tuples; }
177
178 /** @return true if the buffer is empty. */
179 [[nodiscard]] bool empty() const noexcept { return size() == 0; }
180
181 /** @return a references to the last element. */
182 [[nodiscard]] dfield_t *&back() noexcept {
183 ut_a(!empty());
184 return m_dtuples[size() - 1];
185 }
186
187 /** Allocate fields from the heap.
188 @param[in] n Number of fields to allocate.
189 @return an array of n dfields. */
190 dfield_t *alloc(size_t n) noexcept {
191 const auto sz = sizeof(dfield_t) * n;
192 return static_cast<dfield_t *>(mem_heap_alloc(m_heap, sz));
193 }
194
195 /** Check if n bytes will fit in the buffer.
196 @param[in] n Number of bytes to check.
197 @return true if n bytes will fit in the buffer. */
198 bool will_fit(size_t n) const noexcept {
199 /* Reserve one byte for the end marker and adjust for meta-data overhead. */
200 return m_total_size + n +
201 (sizeof(std::remove_pointer<decltype(
203 (m_n_tuples + 1)) <=
204 m_buffer_size - 1;
205 }
206
207 /** Deep copy the field data starting from the back.
208 @param[in] n_fields Number of fields to copy.
209 @param[in] data_size Size in bytes of the data to copy. */
210 void deep_copy(size_t n_fields, size_t data_size) noexcept;
211
212 /** Compare two merge data tuples.
213 @param[in] lhs Fields to compare on the LHS
214 @param[in] rhs Fields to compare on the RHS
215 @param[in,out] dup For capturing duplicates (or nullptr).
216 @retval +ve - if lhs > rhs
217 @retval -ve - if lhs < rhs
218 @retval 0 - if lhs == rhs */
219 [[nodiscard]] static int compare(const dfield_t *lhs, const dfield_t *rhs,
220 Dup *dup) noexcept;
221
222 using DTuple = dfield_t *;
223 using DTuples = std::vector<DTuple, ut::allocator<DTuple>>;
224
225 /** Memory heap where allocated */
227
228 /** The index the tuples belong to */
230
231 /** Total amount of data bytes */
232 size_t m_total_size{};
233
234 /** Number of data tuples */
235 size_t m_n_tuples{};
236
237 /** Maximum number of data tuples */
238 size_t m_max_tuples{};
239
240 /** Array of data tuples */
242
243 /** Buffer size. */
245};
246
247} // namespace ddl
248
249#endif /* !ddl0impl_buffer_h */
A utility class which, if inherited from, prevents the descendant class from being copied,...
Definition: ut0class_life_cycle.h:40
Allocator that allows std::* containers to manage their memory through ut::malloc* and ut::free libra...
Definition: ut0new.h:2179
dberr_t
Definition: db0err.h:38
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:103
unsigned char byte
Blob class.
Definition: common.h:150
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.
uint16_t value_type
Definition: vt100.h:183
The general architecture is that the work is done in two phases, roughly the read and write phase.
Definition: btr0load.cc:41
std::pair< byte *, os_offset_t > IO_buffer
Block size for DDL I/O operations.
Definition: ddl0impl.h:46
void * malloc_large_page_withkey(PSI_memory_key_t key, std::size_t size) noexcept
Dynamically allocates memory backed up by large (huge) pages.
Definition: ut0new.h:1245
PSI_memory_key_t make_psi_memory_key(PSI_memory_key key)
Convenience helper function to create type-safe representation of PSI_memory_key.
Definition: ut0new.h:188
bool free_large_page(void *ptr) noexcept
Releases storage which has been dynamically allocated through any of the ut::malloc_large_page*() var...
Definition: ut0new.h:1322
uint64_t os_offset_t
File offset in bytes.
Definition: os0file.h:83
required string type
Definition: replication_group_member_actions.proto:33
Buffer used for reading and writing to the temporary files.
Definition: ddl0impl-buffer.h:82
IO_buffer io_buffer() noexcept
Get the IO buffer.
Definition: ddl0impl-buffer.h:111
PFS_buffer m_buffer
Raw buffer (unaligned pointer).
Definition: ddl0impl-buffer.h:115
bool allocate(size_t size) noexcept
Allocate the buffer.
Definition: ddl0impl-buffer.h:92
~Aligned_buffer()=default
Destructor.
Aligned_buffer()=default
Constructor.
IO_buffer m_io_buffer
The IO buffer.
Definition: ddl0impl-buffer.h:118
Structure for reporting duplicate records.
Definition: ddl0ddl.h:131
Buffer for sorting in main memory.
Definition: ddl0impl-buffer.h:122
bool full() const noexcept
Definition: ddl0impl-buffer.h:176
dberr_t serialize(IO_buffer io_buffer, Function &&f) noexcept
Serialize the contents for storing to disk.
Definition: ddl0buffer.cc:116
bool is_fts() const noexcept
Definition: ddl0impl-buffer.h:160
bool will_fit(size_t n) const noexcept
Check if n bytes will fit in the buffer.
Definition: ddl0impl-buffer.h:198
Key_sort_buffer(dict_index_t *index, size_t size) noexcept
Constructor.
Definition: ddl0buffer.cc:80
size_t m_buffer_size
Buffer size.
Definition: ddl0impl-buffer.h:244
size_t size() const noexcept
Definition: ddl0impl-buffer.h:173
dfield_t * alloc(size_t n) noexcept
Allocate fields from the heap.
Definition: ddl0impl-buffer.h:190
std::vector< DTuple, ut::allocator< DTuple > > DTuples
Definition: ddl0impl-buffer.h:223
static int compare(const dfield_t *lhs, const dfield_t *rhs, Dup *dup) noexcept
Compare two merge data tuples.
Definition: ddl0buffer.cc:218
bool is_clustered() const noexcept
Definition: ddl0impl-buffer.h:155
size_t m_n_tuples
Number of data tuples.
Definition: ddl0impl-buffer.h:235
bool is_unique() const noexcept
Definition: ddl0impl-buffer.h:165
size_t m_total_size
Total amount of data bytes.
Definition: ddl0impl-buffer.h:232
std::function< dberr_t(IO_buffer io_buffer, os_offset_t &n)> Function
Callback for writing serialized data to to disk.
Definition: ddl0impl-buffer.h:131
mem_heap_t * heap() noexcept
Definition: ddl0impl-buffer.h:170
dict_index_t * m_index
The index the tuples belong to.
Definition: ddl0impl-buffer.h:229
void deep_copy(size_t n_fields, size_t data_size) noexcept
Deep copy the field data starting from the back.
Definition: ddl0buffer.cc:87
DTuples m_dtuples
Array of data tuples.
Definition: ddl0impl-buffer.h:241
dfield_t *& back() noexcept
Definition: ddl0impl-buffer.h:182
~Key_sort_buffer() noexcept
Destructor.
Definition: ddl0impl-buffer.h:139
size_t m_max_tuples
Maximum number of data tuples.
Definition: ddl0impl-buffer.h:238
mem_heap_t * m_heap
Memory heap where allocated.
Definition: ddl0impl-buffer.h:226
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:97
bool empty() const noexcept
Definition: ddl0impl-buffer.h:179
For DDL memory allocations that use the mem_key_ddl handle.
Definition: ddl0impl-buffer.h:40
size_t m_size
Number of bytes allocated.
Definition: ddl0impl-buffer.h:78
byte * m_ptr
Pointer to allocated buffer.
Definition: ddl0impl-buffer.h:75
byte * allocate(size_t size) noexcept
Definition: ddl0impl-buffer.h:52
PFS_buffer()=default
Constructor.
byte Type
Definition: ddl0impl-buffer.h:41
~PFS_buffer() noexcept
Destructor.
Definition: ddl0impl-buffer.h:48
void deallocate() noexcept
Deallocate the memory.
Definition: ddl0impl-buffer.h:69
Structure for an SQL data field.
Definition: data0data.h:604
Data structure for an index.
Definition: dict0mem.h:1045
unsigned type
index type (DICT_CLUSTERED, DICT_UNIQUE, DICT_IBUF, DICT_CORRUPT)
Definition: dict0mem.h:1072
bool is_clustered() const
Definition: dict0mem.h:1310
The info structure stored at the beginning of a heap block.
Definition: mem0mem.h:301
Definition: ut0new.h:1329
static const size_t UNIV_SECTOR_SIZE
Definition: univ.i:640
static void * ut_align(const void *ptr, ulint align_no)
The following function rounds up a pointer to the nearest aligned address.
#define ut_a(EXPR)
Abort execution if EXPR does not evaluate to nonzero.
Definition: ut0dbg.h:56
PSI_memory_key mem_key_ddl
Definition: ut0new.cc:59
int n
Definition: xcom_base.cc:508