Debian Codegraph @

storage/innobase/ha/ha0ha.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 1994, 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 ha/ha0ha.cc
29 The hash table with external chains
30
31 Created 8/22/1994 Heikki Tuuri
32 *************************************************************************/
33
34 #include "ha0ha.h"
35
36 #include <sys/types.h>
37
38 #ifdef UNIV_DEBUG
39 #include "buf0buf.h"
40 #endif /* UNIV_DEBUG */
41 #include "btr0sea.h"
42 #include "page0page.h"
43
44 hash_table_t *ib_create(size_t n, latch_id_t id, size_t n_sync_obj,
45 uint32_t type) {
46 hash_table_t *table;
47
48 ut_a(type == MEM_HEAP_FOR_BTR_SEARCH || type == MEM_HEAP_FOR_PAGE_HASH);
49
50 ut_ad(ut_is_2pow(n_sync_obj));
51 table = ut::new_<hash_table_t>(n);
52 ut_ad(table->heap == nullptr);
53
54 /* Creating MEM_HEAP_BTR_SEARCH type heaps can potentially fail,
55 but in practice it never should in this case, hence the asserts. */
56
57 if (n_sync_obj == 0) {
58 table->heap =
59 mem_heap_create(std::min(uint64_t{4096}, MEM_MAX_ALLOC_IN_BUF / 2 -
60 MEM_BLOCK_HEADER_SIZE -
61 MEM_SPACE_NEEDED(0)),
62 UT_LOCATION_HERE, type);
63 ut_a(table->heap);
64
65 return table;
66 }
67 ut_ad(type == MEM_HEAP_FOR_PAGE_HASH);
68 /* We create a hash table protected by rw_locks for buf_pool->page_hash. */
69 hash_create_sync_obj(table, id, n_sync_obj);
70 return table;
71 }
72
73 /** Empties a hash table and frees the memory heaps. */
74 void ha_clear(hash_table_t *table) /*!< in, own: hash table */
75 {
76 ut_ad(table->magic_n == hash_table_t::HASH_TABLE_MAGIC_N);
77 ut_ad(!table->adaptive || btr_search_own_all(RW_LOCK_X));
78 ut_ad(table->type == HASH_TABLE_SYNC_RW_LOCK);
79 ut_ad(table->heap == nullptr);
80
81 for (size_t i = 0; i < table->n_sync_obj; ++i) {
82 rw_lock_free(&table->rw_locks[i]);
83 }
84
85 ut::free(table->rw_locks);
86 table->rw_locks = nullptr;
87
88 table->n_sync_obj = 0;
89 table->type = HASH_TABLE_SYNC_NONE;
90
91 /* Clear the hash table. */
92 size_t n = hash_get_n_cells(table);
93
94 for (size_t i = 0; i < n; i++) {
95 hash_get_nth_cell(table, i)->node = nullptr;
96 }
97 }
98
99 #ifdef UNIV_DEBUG
100 /** Verify if latch corresponding to the hash table is x-latched
101 @param[in] table hash table */
102 static void ha_btr_search_latch_x_locked(const hash_table_t *table) {
103 ulong i;
104 for (i = 0; i < btr_ahi_parts; ++i) {
105 if (btr_search_sys->parts[i].hash_table == table) {
106 break;
107 }
108 }
109
110 ut_ad(i < btr_ahi_parts);
111 ut_ad(rw_lock_own(&btr_search_sys->parts[i].latch, RW_LOCK_X));
112 }
113 #endif /* UNIV_DEBUG */
114
115 bool ha_insert_for_hash_func(hash_table_t *table, uint64_t hash_value,
116 IF_AHI_DEBUG(buf_block_t *block, )
117 const rec_t *data) {
118 ha_node_t *node;
119 ha_node_t *prev_node;
120
121 ut_ad(data);
122 ut_ad(table);
123 ut_ad(table->magic_n == hash_table_t::HASH_TABLE_MAGIC_N);
124 #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
125 ut_a(block->frame == page_align(data));
126 #endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
127 hash_assert_can_modify(table, hash_value);
128
129 ut_d(ha_btr_search_latch_x_locked(table));
130 ut_ad(btr_search_enabled);
131 ut_ad(block->ahi.index != nullptr);
132
133 auto &first_node =
134 hash_get_first(table, hash_calc_cell_id(hash_value, table));
135
136 prev_node = static_cast<ha_node_t *>(first_node);
137
138 while (prev_node != nullptr) {
139 if (prev_node->hash_value == hash_value) {
140 #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
141 if (table->adaptive) {
142 buf_block_t *prev_block = prev_node->block;
143 ut_a(prev_block->frame == page_align(prev_node->data));
144 ut_a(prev_block->ahi.n_pointers.fetch_sub(1) - 1 <
145 (int)MAX_REC_PER_PAGE);
146 ut_a(block->ahi.n_pointers.fetch_add(1) + 1 < (int)MAX_REC_PER_PAGE);
147 }
148
149 prev_node->block = block;
150 #endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
151 prev_node->data = data;
152
153 return true;
154 }
155
156 prev_node = prev_node->next;
157 }
158
159 /* We have to allocate a new chain node */
160
161 node = static_cast<ha_node_t *>(
162 mem_heap_alloc(hash_get_heap(table), sizeof(ha_node_t)));
163
164 if (node == nullptr) {
165 /* It was a btr search type memory heap and at the moment
166 no more memory could be allocated: return */
167
168 ut_ad(hash_get_heap(table)->type & MEM_HEAP_BTR_SEARCH);
169
170 return false;
171 }
172
173 ha_node_set_data(node, IF_AHI_DEBUG(block, ) data);
174
175 #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
176 if (table->adaptive) {
177 ut_a(block->ahi.n_pointers.fetch_add(1) + 1 < (int)MAX_REC_PER_PAGE);
178 }
179 #endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
180
181 node->hash_value = hash_value;
182
183 node->next = nullptr;
184
185 prev_node = static_cast<ha_node_t *>(first_node);
186
187 if (prev_node == nullptr) {
188 first_node = node;
189
190 return true;
191 }
192
193 while (prev_node->next != nullptr) {
194 prev_node = prev_node->next;
195 }
196
197 prev_node->next = node;
198
199 return true;
200 }
201
202 /** Deletes a hash node. */
203 void ha_delete_hash_node(hash_table_t *table, /*!< in: hash table */
204 ha_node_t *del_node) /*!< in: node to be deleted */
205 {
206 ut_ad(table);
207 ut_ad(table->magic_n == hash_table_t::HASH_TABLE_MAGIC_N);
208 ut_d(ha_btr_search_latch_x_locked(table));
209
210 ut_ad(btr_search_enabled);
211 ut_ad(del_node->block->ahi.index != nullptr);
212
213 #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
214 if (table->adaptive) {
215 ut_a(del_node->block->frame = page_align(del_node->data));
216 ut_a(del_node->block->ahi.n_pointers.fetch_sub(1) - 1 <
217 (int)MAX_REC_PER_PAGE);
218 }
219 #endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
220
221 HASH_DELETE_AND_COMPACT(ha_node_t, next, table, del_node);
222 }
223
224 bool ha_search_and_update_if_found_func(hash_table_t *table,
225 uint64_t hash_value, const rec_t *data,
226 IF_AHI_DEBUG(buf_block_t *new_block, )
227 const rec_t *new_data) {
228 ha_node_t *node;
229
230 ut_ad(table);
231 ut_ad(table->magic_n == hash_table_t::HASH_TABLE_MAGIC_N);
232 hash_assert_can_modify(table, hash_value);
233 #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
234 ut_a(new_block->frame == page_align(new_data));
235 #endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
236
237 ut_d(ha_btr_search_latch_x_locked(table));
238
239 ut_ad(btr_search_enabled);
240 ut_ad(new_block->ahi.index != nullptr);
241 node = ha_search_with_data(table, hash_value, data);
242
243 if (node) {
244 #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
245 if (table->adaptive) {
246 ut_a(node->block->ahi.n_pointers.fetch_sub(1) - 1 <
247 (int)MAX_REC_PER_PAGE);
248 ut_a(new_block->ahi.n_pointers.fetch_add(1) + 1 < (int)MAX_REC_PER_PAGE);
249 }
250
251 node->block = new_block;
252 #endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
253 node->data = new_data;
254
255 return true;
256 }
257
258 return false;
259 }
260
261 void ha_remove_a_node_to_page(hash_table_t *table, uint64_t hash_value,
262 const page_t *page) {
263 ha_node_t *node;
264
265 ut_ad(table);
266 ut_ad(table->magic_n == hash_table_t::HASH_TABLE_MAGIC_N);
267 hash_assert_can_modify(table, hash_value);
268
269 ut_ad(btr_search_enabled);
270
271 node = ha_chain_get_first(table, hash_value);
272
273 while (node) {
274 if (page_align(ha_node_get_data(node)) == page) {
275 /* Remove the hash node. It may be a node with a different fold, but we
276 don't care about it - we delete any entry in this chain for the
277 specified page. */
278
279 ha_delete_hash_node(table, node);
280 break;
281 } else {
282 node = ha_chain_get_next(node);
283 }
284 }
285 }
286
287 #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
288
289 bool ha_validate(hash_table_t *table, uint64_t start_index,
290 uint64_t end_index) {
291 bool ok = true;
292
293 ut_ad(table);
294 ut_ad(table->magic_n == hash_table_t::HASH_TABLE_MAGIC_N);
295 ut_a(start_index <= end_index);
296 ut_a(start_index < hash_get_n_cells(table));
297 ut_a(end_index < hash_get_n_cells(table));
298
299 for (uint64_t i = start_index; i <= end_index; i++) {
300 ha_node_t *node;
301 hash_cell_t *cell;
302
303 cell = hash_get_nth_cell(table, i);
304
305 for (node = static_cast<ha_node_t *>(cell->node); node != nullptr;
306 node = node->next) {
307 if (hash_calc_cell_id(node->hash_value, table) != i) {
308 ib::error(ER_IB_MSG_522)
309 << "Hash table node hash value " << node->hash_value
310 << " does not match the"
311 " cell number "
312 << i << ".";
313
314 ok = false;
315 }
316 }
317 }
318
319 return (ok);
320 }
321 #endif /* defined UNIV_AHI_DEBUG || defined UNIV_DEBUG */
322
323 /** Prints info of a hash table.
324 @param[in] file File where to print
325 @param[in] table Hash table */
326 void ha_print_info(FILE *file, hash_table_t *table) {
327 #ifdef UNIV_DEBUG
328 /* Some of the code here is disabled for performance reasons in production
329 builds, see http://bugs.mysql.com/36941 */
330 #define PRINT_USED_CELLS
331 #endif /* UNIV_DEBUG */
332
333 #ifdef PRINT_USED_CELLS
334 hash_cell_t *cell;
335 size_t cells = 0;
336 size_t i;
337 #endif /* PRINT_USED_CELLS */
338 size_t n_bufs;
339
340 ut_ad(table);
341 ut_ad(table->magic_n == hash_table_t::HASH_TABLE_MAGIC_N);
342 #ifdef PRINT_USED_CELLS
343 for (i = 0; i < hash_get_n_cells(table); i++) {
344 cell = hash_get_nth_cell(table, i);
345
346 if (cell->node) {
347 cells++;
348 }
349 }
350 #endif /* PRINT_USED_CELLS */
351
352 fprintf(file, "Hash table size %zu", hash_get_n_cells(table));
353
354 #ifdef PRINT_USED_CELLS
355 fprintf(file, ", used cells %lu", (ulong)cells);
356 #endif /* PRINT_USED_CELLS */
357
358 if (table->heap != nullptr) {
359 /* This calculation is intended for the adaptive hash
360 index: how many buffer frames we have reserved? */
361
362 n_bufs = UT_LIST_GET_LEN(table->heap->base) - 1;
363
364 ut_ad(table->heap->free_block_ptr != nullptr);
365
366 if (table->heap->free_block_ptr->load()) {
367 n_bufs++;
368 }
369
370 fprintf(file, ", node heap has %zu buffer(s)\n", n_bufs);
371 }
372 }