MySQL 9.0.0
Source Code Documentation
table.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 2024, 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 private:
117 /** Index entry for storing index pointer as well
118 * as allocated memory size. */
119 struct Index_entry {
121
123 };
124
125 /** Check if the table is indexed.
126 *
127 * @retval true if there are any indexes defined and not disabled.
128 * @retval false table is not indexed.
129 */
130 bool indexed() const;
131
132 /** Create index for given key and append it to indexes table. */
133 template <class T>
134 void append_new_index(const KEY &mysql_index);
135
136 /** Create the indexes in `m_index_entries`
137 * from `m_mysql_table->key_info[]`. */
138 void indexes_create();
139
140 /** Destroy the indexes in `m_index_entries`. */
141 void indexes_destroy();
142
143 bool is_index_update_needed(const unsigned char *mysql_row_old,
144 const unsigned char *mysql_row_new) const;
145
147
149
151
152 /** Allocator for all members that need dynamic memory allocation. */
154
155 /** Rows of the table. */
157
159
161
163
164 std::vector<Index_entry, Allocator<Index_entry>> m_index_entries;
165
166 std::vector<Cursor, Allocator<Cursor>> m_insert_undo;
167
169
171};
172
173/* Implementation of inlined methods. */
174
176 return m_mysql_table_share;
177}
178
179inline size_t Table::mysql_row_length() const { return m_mysql_row_length; }
180
181inline size_t Table::number_of_indexes() const {
182 return m_index_entries.size();
183}
184
185inline size_t Table::number_of_columns() const { return m_columns.size(); }
186
187inline const Columns &Table::columns() const { return m_columns; }
188
189inline size_t Table::number_of_rows() const { return m_rows.size(); }
190
191inline const Index &Table::index(size_t i) const {
192 return *m_index_entries[i].m_index;
193}
194
195inline const Column &Table::column(size_t i) const {
196 assert(i < m_columns.size());
197 return m_columns[i];
198}
199
200inline const Storage &Table::rows() const { return m_rows; }
201
202inline void Table::row(const Storage::Iterator &pos,
203 unsigned char *mysql_row) const {
205
206 const Storage::Element *storage_element = *pos;
207
210
211 memcpy(mysql_row, storage_element, m_mysql_row_length);
212 } else {
213 assert(m_rows.element_size() == sizeof(Row));
214
215 const Row *row = static_cast<const Row *>(storage_element);
216 row->copy_to_mysql_row(m_columns, mysql_row, m_mysql_row_length);
217 }
218}
219
220inline void Table::truncate() {
222 for (auto element : m_rows) {
223 Row *row = static_cast<Row *>(element);
224 row->~Row();
225 }
226 }
227 m_rows.clear();
228
229 /* Truncate indexes even if `m_indexes_are_enabled` is false. Somebody may
230 * use truncate() before enabling indexes and we don't want an empty m_rows
231 * with some stale data inside the indexes. */
232 for (auto &entry : m_index_entries) {
233 entry.m_index->truncate();
234 }
235}
236
238 m_indexes_are_enabled = false;
239 return Result::OK;
240}
241
243 if (m_rows.size() == 0 || m_indexes_are_enabled) {
245 return Result::OK;
246 }
247
249}
250
251inline bool Table::indexed() const {
252 return m_indexes_are_enabled && !m_index_entries.empty();
253}
254
255/** Create index for given key and appends it to indexes table. */
256template <class T>
257inline void Table::append_new_index(const KEY &mysql_index) {
259
260 entry.m_alloc_size = sizeof(T);
261
262 auto mem_ptr = m_allocator.allocate(entry.m_alloc_size);
263 try {
264 entry.m_index = new (mem_ptr) T(*this, mysql_index, m_allocator);
265
266 m_index_entries.push_back(entry);
267 } catch (...) {
268 m_allocator.deallocate(mem_ptr, entry.m_alloc_size);
269 throw;
270 }
271}
272
273} /* namespace temptable */
274
275#endif /* TEMPTABLE_TABLE_H */
Definition: key.h:113
Custom memory allocator.
Definition: allocator.h:435
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:128
Definition: table.h:47
void row(const Storage::Iterator &pos, unsigned char *mysql_row) const
Definition: table.h:202
size_t number_of_indexes() const
Definition: table.h:181
Result disable_indexes()
Definition: table.h:237
bool indexed() const
Check if the table is indexed.
Definition: table.h:251
void append_new_index(const KEY &mysql_index)
Create index for given key and append it to indexes table.
Definition: table.h:257
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:56
Storage m_rows
Rows of the table.
Definition: table.h:156
Table & operator=(const Table &)=delete
TableResourceMonitor m_resource_monitor
Definition: table.h:150
bool m_indexes_are_enabled
Definition: table.h:160
Result indexes_insert(Storage::Element *row)
Definition: table.cc:322
const Columns & columns() const
Definition: table.h:187
const Index & index(size_t i) const
Definition: table.h:191
std::vector< Index_entry, Allocator< Index_entry > > m_index_entries
Definition: table.h:164
Result indexes_remove(Storage::Element *row)
Definition: table.cc:364
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:176
void truncate()
Definition: table.h:220
uint32_t m_mysql_row_length
Definition: table.h:162
Table & operator=(Table &&rhs)=delete
bool m_all_columns_are_fixed_size
Definition: table.h:158
Table(Table &&other)=delete
Result insert(const unsigned char *mysql_row)
Insert a new row in the table.
Definition: table.cc:129
Result remove(const unsigned char *mysql_row_must_be, const Storage::Iterator &victim_position)
Definition: table.cc:231
Result enable_indexes()
Definition: table.h:242
~Table()
Definition: table.cc:118
void indexes_destroy()
Destroy the indexes in m_index_entries.
Definition: table.cc:294
const Column & column(size_t i) const
Definition: table.h:195
size_t mysql_row_length() const
Definition: table.h:179
Allocator< uint8_t > m_allocator
Allocator for all members that need dynamic memory allocation.
Definition: table.h:153
size_t number_of_columns() const
Definition: table.h:185
std::vector< Cursor, Allocator< Cursor > > m_insert_undo
Definition: table.h:166
const TABLE_SHARE * mysql_table_share() const
Definition: table.h:175
TABLE_SHARE * m_mysql_table_share
Definition: table.h:170
const Storage & rows() const
Definition: table.h:200
void indexes_create()
Create the indexes in m_index_entries from m_mysql_table->key_info[].
Definition: table.cc:265
size_t number_of_rows() const
Definition: table.h:189
bool is_index_update_needed(const unsigned char *mysql_row_old, const unsigned char *mysql_row_new) const
Definition: table.cc:307
Columns m_columns
Definition: table.h:168
struct _entry entry
TempTable index cursor.
Definition: allocator.h:45
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 Row declarations.
TempTable custom allocator.
TempTable Column declaration.
TempTable Index declarations.
TempTable auxiliary Result enum.
TempTable Storage.
This structure is shared between different table objects.
Definition: table.h:701
uint rec_buff_length
Definition: table.h:847
Definition: table.h:1407
Definition: completion_hash.h:35
Index entry for storing index pointer as well as allocated memory size.
Definition: table.h:119
size_t m_alloc_size
Definition: table.h:122
Index * m_index
Definition: table.h:120