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