MySQL 9.1.0
Source Code Documentation
index.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/index.h
25TempTable Index declarations. */
26
27#ifndef TEMPTABLE_INDEX_H
28#define TEMPTABLE_INDEX_H
29
30#include <assert.h>
31
32#include "sql/key.h"
39
40namespace temptable {
41
42class Table;
43
44/** Index interface. */
45class Index {
46 public:
47 /** Index lookup (search) result. */
48 enum class Lookup {
49 /** The searched for indexed cells were found and the cursor is
50 * positioned
51 * on them. */
52 FOUND,
53
54 /** The searched for indexed cells were not found and the cursor is
55 * positioned on the next indexed cells in index order. */
57
58 /** The searched for indexed cells were not found and the cursor
59 * position is undefined. */
61 };
62
63 /** Constructor. */
64 Index(
65 /** [in] Table where this index belongs, used only for fetching metadata
66 * of its columns. */
67 const Table &table,
68 /** [in] MySQL index, for querying metadata. */
69 const KEY &mysql_index);
70
71 /** Destructor. */
72 virtual ~Index();
73
74 /** Insert a new entry into the index.
75 * @return Result::OK or another Result::* error code */
76 virtual Result insert(
77 /** [in] Indexed cells to insert. */
78 const Indexed_cells &indexed_cells,
79 /** [out] If insert succeeds (Result::OK) this will be set to the position
80 * inside the index where the new indexed cells where inserted. */
81 Cursor *insert_position) = 0;
82
83 /** Lookup (search) an indexed cells.
84 * @retval Lookup::FOUND the provided `search_cells` were found and `first`
85 * was positioned on them (the first entry, if there are duplicates).
86 * @retval Lookup::NOT_FOUND_CURSOR_POSITIONED_ON_NEXT the provided
87 * `search_cells` were not found and `first` was positioned on the next
88 * indexed cells in index order.
89 * @retval Lookup::NOT_FOUND_CURSOR_UNDEFINED the provided `search_cells` were
90 * not found and `first` is undefined. */
91 virtual Lookup lookup(
92 /** [in] Indexed cells to search for. */
93 const Indexed_cells &search_cells,
94 /** [out] First indexed cells that were found, see above. */
95 Cursor *first) const = 0;
96
97 /** Lookup (search) an indexed cells.
98 * @retval Lookup::FOUND the provided `search_cells` were found, `first`
99 * was positioned on them (the first entry, if there are duplicates) and
100 * `after_last` was positioned after the last matching entry.
101 * @retval Lookup::NOT_FOUND_CURSOR_POSITIONED_ON_NEXT the provided
102 * `search_cells` were not found and `first` and `after_last` were
103 * positioned on the next indexed cells in index order.
104 * @retval Lookup::NOT_FOUND_CURSOR_UNDEFINED the provided `search_cells` were
105 * not found and `first` and `after_last` are undefined. */
106 virtual Lookup lookup(const Indexed_cells &search_cells, Cursor *first,
107 Cursor *after_last) const = 0;
108
109 /** Erase the indexed cells pointer to by a cursor. */
110 virtual void erase(
111 /** [in] Position to erase. */
112 const Cursor &target) = 0;
113
114 /** Truncate the index, deleting all of its entries. */
115 virtual void truncate() = 0;
116
117 /** Get a cursor to the first entry.
118 * @return cursor to the first indexed cells in index order */
119 virtual Cursor begin() const = 0;
120
121 /** Get a cursor after the last entry.
122 * @return a cursor after the last indexed cells in index order */
123 virtual Cursor end() const = 0;
124
125 /** Get the number of indexed columns by this index.
126 * @return number of indexed columns */
127 size_t number_of_indexed_columns() const;
128
129 /** Get the Nth indexed column.
130 * @return the Nth indexed column */
132 /** [in] Index of the column to fetch,
133 * must be < `number_of_indexed_columns()`. */
134 size_t i) const;
135
136 /** Get the table of the index.
137 * @return table */
138 const Table &table() const;
139
140 /** Get the MySQL index structure which corresponds to this index.
141 * @return mysql index */
142 const KEY &mysql_index() const;
143
144 private:
145 /** Number of indexed columns. */
147
148 /** Table of the index. */
150
151 /** Indexed columns metadata, from [0, m_number_of_indexed_columns). */
152 std::array<Indexed_column, MAX_REF_PARTS> m_indexed_columns;
153
154 /** MySQL index. */
156};
157
158class Tree : public Index {
159 public:
161
162 Tree(const Table &table, const KEY &mysql_index,
163 const Allocator<Indexed_cells> &allocator);
164
165 Result insert(const Indexed_cells &indexed_cells,
166 Cursor *insert_position) override;
167
168 Lookup lookup(const Indexed_cells &search_cells,
169 Cursor *first) const override;
170
171 Lookup lookup(const Indexed_cells &search_cells, Cursor *first,
172 Cursor *after_last) const override;
173
174 void erase(const Cursor &target) override;
175
176 void truncate() override;
177
178 Cursor begin() const override;
179
180 Cursor end() const override;
181
184};
185
186class Hash_duplicates : public Index {
187 public:
189
191 const Allocator<Indexed_cells> &allocator);
192
193 Result insert(const Indexed_cells &indexed_cells,
194 Cursor *insert_position) override;
195
196 Lookup lookup(const Indexed_cells &search_cells,
197 Cursor *first) const override;
198
199 Lookup lookup(const Indexed_cells &search_cells, Cursor *first,
200 Cursor *after_last) const override;
201
202 void erase(const Cursor &target) override;
203
204 void truncate() override;
205
206 Cursor begin() const override;
207
208 Cursor end() const override;
209
211};
212
213class Hash_unique : public Index {
214 public:
216
217 Hash_unique(const Table &table, const KEY &mysql_index,
218 const Allocator<Indexed_cells> &allocator);
219
220 Result insert(const Indexed_cells &indexed_cells,
221 Cursor *insert_position) override;
222
223 Lookup lookup(const Indexed_cells &search_cells,
224 Cursor *first) const override;
225
226 Lookup lookup(const Indexed_cells &search_cells, Cursor *first,
227 Cursor *after_last) const override;
228
229 void erase(const Cursor &target) override;
230
231 void truncate() override;
232
233 Cursor begin() const override;
234
235 Cursor end() const override;
236
238};
239
240/* Implementation of inlined methods. */
241
242inline size_t Index::number_of_indexed_columns() const {
244}
245
246inline const Indexed_column &Index::indexed_column(size_t i) const {
247 assert(i < m_number_of_indexed_columns);
248 return m_indexed_columns[i];
249}
250
251inline const Table &Index::table() const { return m_table; }
252
253inline const KEY &Index::mysql_index() const { return *m_mysql_index; }
254
255} /* namespace temptable */
256
257#endif /* TEMPTABLE_INDEX_H */
Definition: key.h:113
Custom memory allocator.
Definition: allocator.h:445
A cursor for iterating over an Index.
Definition: cursor.h:41
Definition: index.h:186
Cursor end() const override
Get a cursor after the last entry.
Definition: index.cc:223
Lookup lookup(const Indexed_cells &search_cells, Cursor *first) const override
Lookup (search) an indexed cells.
Definition: index.cc:184
void truncate() override
Truncate the index, deleting all of its entries.
Definition: index.cc:209
Result insert(const Indexed_cells &indexed_cells, Cursor *insert_position) override
Insert a new entry into the index.
Definition: index.cc:169
Hash_duplicates(const Table &table, const KEY &mysql_index, const Allocator< Indexed_cells > &allocator)
Definition: index.cc:163
Hash_duplicates_container Container
Definition: index.h:188
Container m_hash_table
Definition: index.h:210
void erase(const Cursor &target) override
Erase the indexed cells pointer to by a cursor.
Definition: index.cc:205
Cursor begin() const override
Get a cursor to the first entry.
Definition: index.cc:221
Definition: index.h:213
void truncate() override
Truncate the index, deleting all of its entries.
Definition: index.cc:278
Hash_unique_container Container
Definition: index.h:215
Hash_unique(const Table &table, const KEY &mysql_index, const Allocator< Indexed_cells > &allocator)
Definition: index.cc:225
void erase(const Cursor &target) override
Erase the indexed cells pointer to by a cursor.
Definition: index.cc:274
Container m_hash_table
Definition: index.h:237
Cursor end() const override
Get a cursor after the last entry.
Definition: index.cc:292
Cursor begin() const override
Get a cursor to the first entry.
Definition: index.cc:290
Lookup lookup(const Indexed_cells &search_cells, Cursor *first) const override
Lookup (search) an indexed cells.
Definition: index.cc:253
Result insert(const Indexed_cells &indexed_cells, Cursor *insert_position) override
Insert a new entry into the index.
Definition: index.cc:231
Index interface.
Definition: index.h:45
virtual Lookup lookup(const Indexed_cells &search_cells, Cursor *first) const =0
Lookup (search) an indexed cells.
size_t m_number_of_indexed_columns
Number of indexed columns.
Definition: index.h:146
const KEY * m_mysql_index
MySQL index.
Definition: index.h:155
Index(const Table &table, const KEY &mysql_index)
Constructor.
Definition: index.cc:41
virtual Result insert(const Indexed_cells &indexed_cells, Cursor *insert_position)=0
Insert a new entry into the index.
Lookup
Index lookup (search) result.
Definition: index.h:48
@ NOT_FOUND_CURSOR_UNDEFINED
The searched for indexed cells were not found and the cursor position is undefined.
@ FOUND
The searched for indexed cells were found and the cursor is positioned on them.
@ NOT_FOUND_CURSOR_POSITIONED_ON_NEXT
The searched for indexed cells were not found and the cursor is positioned on the next indexed cells ...
virtual ~Index()
Destructor.
Definition: index.cc:56
virtual void erase(const Cursor &target)=0
Erase the indexed cells pointer to by a cursor.
const Table & m_table
Table of the index.
Definition: index.h:149
const Indexed_column & indexed_column(size_t i) const
Get the Nth indexed column.
Definition: index.h:246
const KEY & mysql_index() const
Get the MySQL index structure which corresponds to this index.
Definition: index.h:253
virtual void truncate()=0
Truncate the index, deleting all of its entries.
virtual Lookup lookup(const Indexed_cells &search_cells, Cursor *first, Cursor *after_last) const =0
Lookup (search) an indexed cells.
std::array< Indexed_column, MAX_REF_PARTS > m_indexed_columns
Indexed columns metadata, from [0, m_number_of_indexed_columns).
Definition: index.h:152
virtual Cursor begin() const =0
Get a cursor to the first entry.
size_t number_of_indexed_columns() const
Get the number of indexed columns by this index.
Definition: index.h:242
virtual Cursor end() const =0
Get a cursor after the last entry.
const Table & table() const
Get the table of the index.
Definition: index.h:251
Indexed cells represent one or more cells that are covered by an index.
Definition: indexed_cells.h:46
Definition: indexed_column.h:39
Definition: table.h:47
Definition: index.h:158
void erase(const Cursor &target) override
Erase the indexed cells pointer to by a cursor.
Definition: index.cc:155
Cursor begin() const override
Get a cursor to the first entry.
Definition: index.cc:159
void truncate() override
Truncate the index, deleting all of its entries.
Definition: index.cc:157
Tree_container Container
Definition: index.h:160
Tree(const Table &table, const KEY &mysql_index, const Allocator< Indexed_cells > &allocator)
Definition: index.cc:63
Result insert(const Indexed_cells &indexed_cells, Cursor *insert_position) override
Insert a new entry into the index.
Definition: index.cc:69
Lookup lookup(const Indexed_cells &search_cells, Cursor *first) const override
Lookup (search) an indexed cells.
Definition: index.cc:114
Container m_tree
Definition: index.h:182
bool m_allow_duplicates
Definition: index.h:183
Cursor end() const override
Get a cursor after the last entry.
Definition: index.cc:161
TempTable index containers declarations.
TempTable index cursor.
TempTable Indexed Cells declaration.
TempTable Indexed Column.
Definition: allocator.h:48
Result
Definition: result.h:34
std::multiset< Indexed_cells, Indexed_cells_less, Allocator< Indexed_cells > > Tree_container
The container used by tree unique and non-unique indexes.
Definition: containers.h:42
std::unordered_set< Indexed_cells, Indexed_cells_hash, Indexed_cells_equal_to, Allocator< Indexed_cells > > Hash_unique_container
The container used by hash unique indexes.
Definition: containers.h:53
std::unordered_multiset< Indexed_cells, Indexed_cells_hash, Indexed_cells_equal_to, Allocator< Indexed_cells > > Hash_duplicates_container
The container used by hash non-unique indexes.
Definition: containers.h:48
TempTable custom allocator.
TempTable auxiliary Result enum.