Debian Codegraph @

storage/innobase/include/ddl0impl-buffer.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 2020, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 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
38 namespace ddl {
39 /** Buffer for sorting in main memory. */
40 struct Key_sort_buffer : private ut::Non_copyable {
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. */
52 ~Key_sort_buffer() noexcept { mem_heap_free(m_heap); }
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 {
79 return dict_index_is_unique(m_index);
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 empty. */
89 [[nodiscard]] bool empty() const noexcept { return size() == 0; }
90
91 /** @return a references to the last element. */
92 [[nodiscard]] dfield_t *&back() noexcept {
93 ut_a(!empty());
94 return m_dtuples[size() - 1];
95 }
96
97 /** Allocate fields from the heap.
98 @param[in] n Number of fields to allocate.
99 @return an array of n dfields. */
100 dfield_t *alloc(size_t n) noexcept {
101 const auto sz = sizeof(dfield_t) * n;
102 m_total_size += sz;
103 return static_cast<dfield_t *>(mem_heap_alloc(m_heap, sz));
104 }
105
106 /** Pops the unfinished tuple which was allocated with alloc(), but
107 deep_copy() wasn't called yet for it yet. */
108 void pop_unfinished_tuple() noexcept {
109 ut_a(m_n_tuples + 1 == m_dtuples.size());
110 m_dtuples.pop_back();
111 }
112
113 /** Check if n bytes will fit in the buffer, or it is the first tuple,
114 in which case we ignore the limit to ensure forward progress.
115 @param[in] n Number of bytes to check.
116 @return true if n bytes can be added to the buffer. */
117 bool will_fit(size_t n) const noexcept {
118 /* Reserve one byte for the end marker and adjust for meta-data overhead. */
119 return m_n_tuples == 0 ||
120 m_total_size + m_dtuples.size() * 2 * sizeof(m_dtuples[0]) + n <=
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 */
143 mem_heap_t *m_heap{};
144
145 /** The index the tuples belong to */
146 dict_index_t *m_index{};
147
148 /** Total amount of data allocated from m_heap, which includes:
149 i. one dfield_t[n_fields] array per row, allocated via alloc(n_fields) calls
150 ii. actual field's data cloned into the m_heap, via deep_clone(...,) calls
151 This is updated by alloc() and deep_clone().
152 It should roughly match the value of:
153 mem_heap_get_size(m_heap)-mem_block_get_free(UT_LIST_GET_LAST(m_heap->base)).
154 This, when combined with the memory consumption of m_dtuples, should not
155 exceed the m_buffer_size budget */
156 size_t m_total_size{};
157
158 /** Number of data tuples */
159 size_t m_n_tuples{};
160
161 /** Array of data tuples */
162 DTuples m_dtuples{};
163
164 /** Buffer size. */
165 size_t m_buffer_size{};
166 };
167
168 } // namespace ddl
169
170 #endif /* !ddl0impl_buffer_h */