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