MySQL 8.3.0
Source Code Documentation
Go to the documentation of this file.
3Copyright (c) 2020, 2023, Oracle and/or its affiliates.
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.
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.
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.
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
27/** @file include/ddl0impl-buffer.h
28 DDL buffer infrastructure.
29 Created 2020-11-01 by Sunny Bains. */
31#ifndef ddl0impl_buffer_h
32#define ddl0impl_buffer_h
34#include "ddl0impl.h"
35#include "dict0dict.h"
37namespace 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.
43 @param[in,out] n Number of bytes written is returned.
44 Input value semantics:
45 0 - Write up to aligned length.
46 >0 - All data will be written and
47 last block will be padded with zeros.
48 @return DB_SUCCES or error code. */
49 using Function = std::function<dberr_t(IO_buffer io_buffer, os_offset_t &n)>;
51 /** Constructor.
52 @param[in,out] index Sort buffer is for this index.
53 @param[in] size Sort buffer size in bytes. */
54 explicit Key_sort_buffer(dict_index_t *index, size_t size) noexcept;
56 /** Destructor. */
59 /** Sort the elements in m_dtuples.
60 @param[in,out] dup For collecting the duplicate rows. */
61 void sort(ddl::Dup *dup) noexcept;
63 /** Serialize the contents for storing to disk.
64 @param[in] io_buffer Buffer for serializing.
65 @param[in] f Function for persisting the data.
66 @return DB_SUCCESS or error code. */
67 dberr_t serialize(IO_buffer io_buffer, Function &&f) noexcept;
69 /** Reset the sort buffer. clear the heap and entries. */
70 void clear() noexcept;
72 /** @return true if the index is clustered. */
73 [[nodiscard]] bool is_clustered() const noexcept {
74 return m_index->is_clustered();
75 }
77 /** @return true if the index is an FTS index. */
78 [[nodiscard]] bool is_fts() const noexcept {
79 return m_index->type & DICT_FTS;
80 }
82 /** @return true if the index has a unique constraint. */
83 [[nodiscard]] bool is_unique() const noexcept {
85 }
87 /** @return the heap to use. */
88 [[nodiscard]] mem_heap_t *heap() noexcept { return m_heap; }
90 /** @return number of tuples stored so far. */
91 [[nodiscard]] size_t size() const noexcept { return m_n_tuples; }
93 /** @return true if the buffer is full. */
94 [[nodiscard]] bool full() const noexcept { return size() >= m_max_tuples; }
96 /** @return true if the buffer is empty. */
97 [[nodiscard]] bool empty() const noexcept { return size() == 0; }
99 /** @return a references to the last element. */
100 [[nodiscard]] dfield_t *&back() noexcept {
101 ut_a(!empty());
102 return m_dtuples[size() - 1];
103 }
105 /** Allocate fields from the heap.
106 @param[in] n Number of fields to allocate.
107 @return an array of n dfields. */
108 dfield_t *alloc(size_t n) noexcept {
109 const auto sz = sizeof(dfield_t) * n;
110 return static_cast<dfield_t *>(mem_heap_alloc(m_heap, sz));
111 }
113 /** Check if n bytes will fit in the buffer.
114 @param[in] n Number of bytes to check.
115 @return true if n bytes will fit in the buffer. */
116 bool will_fit(size_t n) const noexcept {
117 /* Reserve one byte for the end marker and adjust for meta-data overhead. */
118 return m_total_size + n +
119 (sizeof(std::remove_pointer<decltype(
121 (m_n_tuples + 1)) <=
122 m_buffer_size - 1;
123 }
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;
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;
140 /** DTuple is an array of dfield_t objects. */
141 using DTuple = dfield_t *;
142 using DTuples = std::vector<DTuple, ut::allocator<DTuple>>;
144 /** Memory heap where allocated */
147 /** The index the tuples belong to */
150 /** Total amount of data bytes */
151 size_t m_total_size{};
153 /** Number of data tuples */
154 size_t m_n_tuples{};
156 /** Maximum number of data tuples */
157 size_t m_max_tuples{};
159 /** Array of data tuples */
162 /** Buffer size. */
166} // namespace ddl
168#endif /* !ddl0impl_buffer_h */
A utility class which, if inherited from, prevents the descendant class from being copied,...
Definition: ut0class_life_cycle.h:40
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.
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:86
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:40
bool full() const noexcept
Definition: ddl0impl-buffer.h:94
dberr_t serialize(IO_buffer io_buffer, Function &&f) noexcept
Serialize the contents for storing to disk.
bool is_fts() const noexcept
Definition: ddl0impl-buffer.h:78
bool will_fit(size_t n) const noexcept
Check if n bytes will fit in the buffer.
Definition: ddl0impl-buffer.h:116
Key_sort_buffer(dict_index_t *index, size_t size) noexcept
size_t m_buffer_size
Buffer size.
Definition: ddl0impl-buffer.h:163
size_t size() const noexcept
Definition: ddl0impl-buffer.h:91
dfield_t * alloc(size_t n) noexcept
Allocate fields from the heap.
Definition: ddl0impl-buffer.h:108
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.
bool is_clustered() const noexcept
Definition: ddl0impl-buffer.h:73
size_t m_n_tuples
Number of data tuples.
Definition: ddl0impl-buffer.h:154
bool is_unique() const noexcept
Definition: ddl0impl-buffer.h:83
size_t m_total_size
Total amount of data bytes.
Definition: ddl0impl-buffer.h:151
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:49
mem_heap_t * heap() noexcept
Definition: ddl0impl-buffer.h:88
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.
DTuples m_dtuples
Array of data tuples.
Definition: ddl0impl-buffer.h:160
dfield_t *& back() noexcept
Definition: ddl0impl-buffer.h:100
~Key_sort_buffer() noexcept
Definition: ddl0impl-buffer.h:57
size_t m_max_tuples
Maximum number of data tuples.
Definition: ddl0impl-buffer.h:157
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.
void clear() noexcept
Reset the sort buffer.
bool empty() const noexcept
Definition: ddl0impl-buffer.h:97
Structure for an SQL data field.
Definition: data0data.h:604
Data structure for an index.
Definition: dict0mem.h:1045
unsigned type
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:92
int n