MySQL 8.0.40
Source Code Documentation
ddl0impl-buffer.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2020, 2024, 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/** Buffer for sorting in main memory. */
41 /** Callback for writing serialized data to to disk.
42 @param[in] io_buffer Buffer to persist - aligned to IO_BLOCK_SIZE.
43 @return DB_SUCCES or error code. */
44 using Function = std::function<dberr_t(IO_buffer io_buffer)>;
45
46 /** Constructor.
47 @param[in,out] index Sort buffer is for this index.
48 @param[in] size Sort buffer size in bytes. */
49 explicit Key_sort_buffer(dict_index_t *index, size_t size) noexcept;
50
51 /** Destructor. */
53
54 /** Sort the elements in m_dtuples.
55 @param[in,out] dup For collecting the duplicate rows. */
56 void sort(ddl::Dup *dup) noexcept;
57
58 /** Serialize the contents for storing to disk.
59 @param[in] io_buffer Buffer for serializing.
60 @param[in] persist Function for persisting the data.
61 @return DB_SUCCESS or error code. */
62 dberr_t serialize(IO_buffer io_buffer, Function persist) noexcept;
63
64 /** Reset the sort buffer. clear the heap and entries. */
65 void clear() noexcept;
66
67 /** @return true if the index is clustered. */
68 [[nodiscard]] bool is_clustered() const noexcept {
69 return m_index->is_clustered();
70 }
71
72 /** @return true if the index is an FTS index. */
73 [[nodiscard]] bool is_fts() const noexcept {
74 return m_index->type & DICT_FTS;
75 }
76
77 /** @return true if the index has a unique constraint. */
78 [[nodiscard]] bool is_unique() const noexcept {
80 }
81
82 /** @return the heap to use. */
83 [[nodiscard]] mem_heap_t *heap() noexcept { return m_heap; }
84
85 /** @return number of tuples stored so far. */
86 [[nodiscard]] size_t size() const noexcept { return m_n_tuples; }
87
88 /** @return true if the buffer is full. */
89 [[nodiscard]] bool full() const noexcept { return size() >= m_max_tuples; }
90
91 /** @return true if the buffer is empty. */
92 [[nodiscard]] bool empty() const noexcept { return size() == 0; }
93
94 /** @return a references to the last element. */
95 [[nodiscard]] dfield_t *&back() noexcept {
96 ut_a(!empty());
97 return m_dtuples[size() - 1];
98 }
99
100 /** Allocate fields from the heap.
101 @param[in] n Number of fields to allocate.
102 @return an array of n dfields. */
103 dfield_t *alloc(size_t n) noexcept {
104 const auto sz = sizeof(dfield_t) * n;
105 return static_cast<dfield_t *>(mem_heap_alloc(m_heap, sz));
106 }
107
108 /** Check if n bytes will fit in the buffer.
109 @param[in] n Number of bytes to check.
110 @return true if n bytes will fit in the buffer. */
111 bool will_fit(size_t n) const noexcept {
112 /* Reserve one byte for the end marker and adjust for meta-data overhead. */
113 return m_total_size + n +
114 (sizeof(std::remove_pointer<
115 decltype(m_dtuples)::value_type>::type) *
116 (m_n_tuples + 1)) <=
117 m_buffer_size - 1;
118 }
119
120 /** Deep copy the field data starting from the back.
121 @param[in] n_fields Number of fields to copy.
122 @param[in] data_size Size in bytes of the data to copy. */
123 void deep_copy(size_t n_fields, size_t data_size) noexcept;
124
125 /** Compare two merge data tuples.
126 @param[in] lhs Fields to compare on the LHS
127 @param[in] rhs Fields to compare on the RHS
128 @param[in,out] dup For capturing duplicates (or nullptr).
129 @retval +ve - if lhs > rhs
130 @retval -ve - if lhs < rhs
131 @retval 0 - if lhs == rhs */
132 [[nodiscard]] static int compare(const dfield_t *lhs, const dfield_t *rhs,
133 Dup *dup) noexcept;
134
135 using DTuple = dfield_t *;
136 using DTuples = std::vector<DTuple, ut::allocator<DTuple>>;
137
138 /** Memory heap where allocated */
140
141 /** The index the tuples belong to */
143
144 /** Total amount of data bytes */
145 size_t m_total_size{};
146
147 /** Number of data tuples */
148 size_t m_n_tuples{};
149
150 /** Maximum number of data tuples */
151 size_t m_max_tuples{};
152
153 /** Array of data tuples */
155
156 /** Buffer size. */
158};
159
160} // namespace ddl
161
162#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.
uint16_t value_type
Definition: vt100.h:184
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
required string type
Definition: replication_group_member_actions.proto:34
Structure for reporting duplicate records.
Definition: ddl0ddl.h:132
Buffer for sorting in main memory.
Definition: ddl0impl-buffer.h:40
bool full() const noexcept
Definition: ddl0impl-buffer.h:89
std::function< dberr_t(IO_buffer io_buffer)> Function
Callback for writing serialized data to to disk.
Definition: ddl0impl-buffer.h:44
bool is_fts() const noexcept
Definition: ddl0impl-buffer.h:73
bool will_fit(size_t n) const noexcept
Check if n bytes will fit in the buffer.
Definition: ddl0impl-buffer.h:111
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:157
size_t size() const noexcept
Definition: ddl0impl-buffer.h:86
dfield_t * alloc(size_t n) noexcept
Allocate fields from the heap.
Definition: ddl0impl-buffer.h:103
std::vector< DTuple, ut::allocator< DTuple > > DTuples
Definition: ddl0impl-buffer.h:136
static int compare(const dfield_t *lhs, const dfield_t *rhs, Dup *dup) noexcept
Compare two merge data tuples.
Definition: ddl0buffer.cc:246
bool is_clustered() const noexcept
Definition: ddl0impl-buffer.h:68
dberr_t serialize(IO_buffer io_buffer, Function persist) noexcept
Serialize the contents for storing to disk.
Definition: ddl0buffer.cc:117
size_t m_n_tuples
Number of data tuples.
Definition: ddl0impl-buffer.h:148
bool is_unique() const noexcept
Definition: ddl0impl-buffer.h:78
size_t m_total_size
Total amount of data bytes.
Definition: ddl0impl-buffer.h:145
mem_heap_t * heap() noexcept
Definition: ddl0impl-buffer.h:83
dict_index_t * m_index
The index the tuples belong to.
Definition: ddl0impl-buffer.h:142
void deep_copy(size_t n_fields, size_t data_size) noexcept
Deep copy the field data starting from the back.
Definition: ddl0buffer.cc:88
DTuples m_dtuples
Array of data tuples.
Definition: ddl0impl-buffer.h:154
dfield_t *& back() noexcept
Definition: ddl0impl-buffer.h:95
~Key_sort_buffer() noexcept
Destructor.
Definition: ddl0impl-buffer.h:52
size_t m_max_tuples
Maximum number of data tuples.
Definition: ddl0impl-buffer.h:151
mem_heap_t * m_heap
Memory heap where allocated.
Definition: ddl0impl-buffer.h:139
void sort(ddl::Dup *dup) noexcept
Sort the elements in m_dtuples.
Definition: ddl0buffer.cc:104
void clear() noexcept
Reset the sort buffer.
Definition: ddl0buffer.cc:98
bool empty() const noexcept
Definition: ddl0impl-buffer.h:92
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:57
int n
Definition: xcom_base.cc:509