MySQL 9.4.0
Source Code Documentation
table.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 2025, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify it under
4the terms of the GNU General Public License, version 2.0, as published by the
5Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
18for more details.
19
20You should have received a copy of the GNU General Public License along with
21this program; if not, write to the Free Software Foundation, Inc.,
2251 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24/** @file storage/temptable/include/temptable/table.h
25TempTable Table declarations. */
26
27#ifndef TEMPTABLE_TABLE_H
28#define TEMPTABLE_TABLE_H
29
30#include <cstddef>
31#include <functional>
32#include <string>
33#include <utility>
34#include <vector>
35
36#include "sql/table.h"
44
45namespace temptable {
46
47class Table {
48 public:
49 Table(TABLE *mysql_table, Block *shared_block,
50 bool all_columns_are_fixed_size, size_t tmp_table_size_limit);
51
52 /* The `m_rows` member is too expensive to copy around. */
53 Table(const Table &) = delete;
54 Table &operator=(const Table &) = delete;
55
56 Table(Table &&other) = delete;
57 Table &operator=(Table &&rhs) = delete;
58
59 ~Table();
60
61 const TABLE_SHARE *mysql_table_share() const;
62
63 size_t mysql_row_length() const;
64
65 size_t number_of_indexes() const;
66
67 size_t number_of_columns() const;
68
69 const Columns &columns() const;
70
71 size_t number_of_rows() const;
72
73 const Index &index(size_t i) const;
74
75 const Column &column(size_t i) const;
76
77 const Storage &rows() const;
78
79 void row(const Storage::Iterator &pos, unsigned char *mysql_row) const;
80
81 /** Insert a new row in the table. If something else than Result::OK is
82 * returned, then the state of the table and its indexes is unchanged by the
83 * call of this method (ie the method takes care to clean up any half
84 * completed work in case of an error and restore the table to its state
85 * before this method was called).
86 * @return result code */
88 /** [in] The contents of the new row to be inserted. */
89 const unsigned char *mysql_row);
90
91 /** Update a row in the table. The semantics of this are enforced by the
92 * handler API. If something else than Result::OK is returned, then the state
93 * of the table and its indexes is unchanged by the call of this method (ie
94 * the method takes care to clean up any half completed work in case of an
95 * error and restore the table to its state before this method was called).
96 * @return result code */
98 /** [in] The contents of the old row to be updated. The row pointed to by
99 * `target_row` must equal this. */
100 const unsigned char *mysql_row_old,
101 /** [in] The contents of the new row. */
102 const unsigned char *mysql_row_new,
103 /** [in,out] Position in the list of rows to update. The row pointed to
104 * by this must equal `mysql_row_old`. */
105 Storage::Element *target_row);
106
107 Result remove(const unsigned char *mysql_row_must_be,
108 const Storage::Iterator &victim_position);
109
110 void truncate();
111
113
115
116 /** Returns the ID of the owner THD.
117 * @retval ID of the THD of the session using this temptable */
118 my_thread_id owner_id() const;
119
120 private:
121 /** Index entry for storing index pointer as well
122 * as allocated memory size. */
123 struct Index_entry {
125
127 };
128
129 /** Check if the table is indexed.
130 *
131 * @retval true if there are any indexes defined and not disabled.
132 * @retval false table is not indexed.
133 */
134 bool indexed() const;
135
136 /** Create index for given key and append it to indexes table. */
137 template <class T>
138 void append_new_index(const KEY &mysql_index);
139
140 /** Create the indexes in `m_index_entries`
141 * from `m_mysql_table->key_info[]`. */
142 void indexes_create();
143
144 /** Destroy the indexes in `m_index_entries`. */
145 void indexes_destroy();
146
147 bool is_index_update_needed(const unsigned char *mysql_row_old,
148 const unsigned char *mysql_row_new) const;
149
151
153
155
156 /** Allocator for all members that need dynamic memory allocation. */
158
159 /** Rows of the table. */
161
163
165
167
168 std::vector<Index_entry, Allocator<Index_entry>> m_index_entries;
169
170 std::vector<Cursor, Allocator<Cursor>> m_insert_undo;
171
173
175
177};
178
179/* Implementation of inlined methods. */
180
182 return m_mysql_table_share;
183}
184
185inline size_t Table::mysql_row_length() const { return m_mysql_row_length; }
186
187inline size_t Table::number_of_indexes() const {
188 return m_index_entries.size();
189}
190
191inline size_t Table::number_of_columns() const { return m_columns.size(); }
192
193inline const Columns &Table::columns() const { return m_columns; }
194
195inline size_t Table::number_of_rows() const { return m_rows.size(); }
196
197inline const Index &Table::index(size_t i) const {
198 return *m_index_entries[i].m_index;
199}
200
201inline const Column &Table::column(size_t i) const {
202 assert(i < m_columns.size());
203 return m_columns[i];
204}
205
206inline const Storage &Table::rows() const { return m_rows; }
207
208inline void Table::row(const Storage::Iterator &pos,
209 unsigned char *mysql_row) const {
211
212 const Storage::Element *storage_element = *pos;
213
216
217 memcpy(mysql_row, storage_element, m_mysql_row_length);
218 } else {
219 assert(m_rows.element_size() == sizeof(Row));
220
221 const Row *row = static_cast<const Row *>(storage_element);
222 row->copy_to_mysql_row(m_columns, mysql_row, m_mysql_row_length);
223 }
224}
225
226inline void Table::truncate() {
228 for (auto element : m_rows) {
229 Row *row = static_cast<Row *>(element);
230 row->~Row();
231 }
232 }
233 m_rows.clear();
234
235 /* Truncate indexes even if `m_indexes_are_enabled` is false. Somebody may
236 * use truncate() before enabling indexes and we don't want an empty m_rows
237 * with some stale data inside the indexes. */
238 for (auto &entry : m_index_entries) {
239 entry.m_index->truncate();
240 }
241}
242
244 m_indexes_are_enabled = false;
245 return Result::OK;
246}
247
249 if (m_rows.size() == 0 || m_indexes_are_enabled) {
251 return Result::OK;
252 }
253
255}
256
257inline bool Table::indexed() const {
258 return m_indexes_are_enabled && !m_index_entries.empty();
259}
260
261/** Create index for given key and appends it to indexes table. */
262template <class T>
263inline void Table::append_new_index(const KEY &mysql_index) {
265
266 entry.m_alloc_size = sizeof(T);
267
268 auto mem_ptr = m_allocator.allocate(entry.m_alloc_size);
269 try {
270 entry.m_index = new (mem_ptr) T(*this, mysql_index, m_allocator);
271
272 m_index_entries.push_back(entry);
273 } catch (...) {
274 m_allocator.deallocate(mem_ptr, entry.m_alloc_size);
275 throw;
276 }
277}
278
279} /* namespace temptable */
280
281#endif /* TEMPTABLE_TABLE_H */
Definition: key.h:113
Custom memory allocator.
Definition: allocator.h:439
Memory-block abstraction whose purpose is to serve as a building block for custom memory-allocator im...
Definition: block.h:163
A column class that describes the metadata of a column.
Definition: column.h:41
Index interface.
Definition: index.h:45
A row representation.
Definition: row.h:402
Iterator over a Storage object.
Definition: storage.h:51
Storage container.
Definition: storage.h:43
size_t size() const
Get the number of elements in the storage.
Definition: storage.h:509
void clear()
Delete all elements in the storage.
Definition: storage.h:618
void element_size(size_t element_size)
Set the element size.
Definition: storage.h:489
void Element
Type used for elements.
Definition: storage.h:46
Definition: allocator.h:126
Definition: table.h:47
void row(const Storage::Iterator &pos, unsigned char *mysql_row) const
Definition: table.h:208
size_t number_of_indexes() const
Definition: table.h:187
Result disable_indexes()
Definition: table.h:243
bool indexed() const
Check if the table is indexed.
Definition: table.h:257
void append_new_index(const KEY &mysql_index)
Create index for given key and append it to indexes table.
Definition: table.h:263
Table(const Table &)=delete
Table(TABLE *mysql_table, Block *shared_block, bool all_columns_are_fixed_size, size_t tmp_table_size_limit)
Definition: table.cc:57
Storage m_rows
Rows of the table.
Definition: table.h:160
Table & operator=(const Table &)=delete
TableResourceMonitor m_resource_monitor
Definition: table.h:154
bool m_indexes_are_enabled
Definition: table.h:164
my_thread_id m_owner_id
Definition: table.h:176
Result indexes_insert(Storage::Element *row)
Definition: table.cc:326
const Columns & columns() const
Definition: table.h:193
const Index & index(size_t i) const
Definition: table.h:197
std::vector< Index_entry, Allocator< Index_entry > > m_index_entries
Definition: table.h:168
Result indexes_remove(Storage::Element *row)
Definition: table.cc:368
Result update(const unsigned char *mysql_row_old, const unsigned char *mysql_row_new, Storage::Element *target_row)
Update a row in the table.
Definition: table.cc:178
void truncate()
Definition: table.h:226
uint32_t m_mysql_row_length
Definition: table.h:166
Table & operator=(Table &&rhs)=delete
bool m_all_columns_are_fixed_size
Definition: table.h:162
Table(Table &&other)=delete
Result insert(const unsigned char *mysql_row)
Insert a new row in the table.
Definition: table.cc:131
Result remove(const unsigned char *mysql_row_must_be, const Storage::Iterator &victim_position)
Definition: table.cc:233
Result enable_indexes()
Definition: table.h:248
~Table()
Definition: table.cc:120
void indexes_destroy()
Destroy the indexes in m_index_entries.
Definition: table.cc:298
const Column & column(size_t i) const
Definition: table.h:201
size_t mysql_row_length() const
Definition: table.h:185
Allocator< uint8_t > m_allocator
Allocator for all members that need dynamic memory allocation.
Definition: table.h:157
size_t number_of_columns() const
Definition: table.h:191
my_thread_id owner_id() const
Returns the ID of the owner THD.
Definition: table.cc:267
std::vector< Cursor, Allocator< Cursor > > m_insert_undo
Definition: table.h:170
const TABLE_SHARE * mysql_table_share() const
Definition: table.h:181
TABLE_SHARE * m_mysql_table_share
Definition: table.h:174
const Storage & rows() const
Definition: table.h:206
void indexes_create()
Create the indexes in m_index_entries from m_mysql_table->key_info[].
Definition: table.cc:269
size_t number_of_rows() const
Definition: table.h:195
bool is_index_update_needed(const unsigned char *mysql_row_old, const unsigned char *mysql_row_new) const
Definition: table.cc:311
Columns m_columns
Definition: table.h:172
struct _entry entry
TempTable index cursor.
#define T
Definition: jit_executor_value.cc:373
uint32 my_thread_id
Definition: my_thread_local.h:34
Definition: allocator.h:48
Result
Definition: result.h:34
std::vector< Column, Allocator< Column > > Columns
A type that designates all the columns of a table.
Definition: column.h:227
TempTable custom allocator.
TempTable Column declaration.
TempTable Index declarations.
TempTable auxiliary Result enum.
TempTable Row declarations.
TempTable Storage.
This structure is shared between different table objects.
Definition: table.h:716
uint rec_buff_length
Definition: table.h:862
Definition: table.h:1433
Definition: completion_hash.h:35
Index entry for storing index pointer as well as allocated memory size.
Definition: table.h:123
size_t m_alloc_size
Definition: table.h:126
Index * m_index
Definition: table.h:124