1
/************************************************************************
2
The hash table with external chains
4
(c) 1994-1997 Innobase Oy
6
Created 8/22/1994 Heikki Tuuri
7
*************************************************************************/
16
#endif /* UNIV_DEBUG */
17
#ifdef UNIV_SYNC_DEBUG
19
#endif /* UNIV_SYNC_DEBUG */
20
#include "page0page.h"
22
/*****************************************************************
23
Creates a hash table with >= n array cells. The actual number of cells is
24
chosen to be a prime number slightly bigger than n. */
29
/* out, own: created table */
30
ulint n, /* in: number of array cells */
31
#ifdef UNIV_SYNC_DEBUG
32
ulint mutex_level, /* in: level of the mutexes in the latching
33
order: this is used in the debug version */
34
#endif /* UNIV_SYNC_DEBUG */
35
ulint n_mutexes) /* in: number of mutexes to protect the
36
hash table: must be a power of 2, or 0 */
41
table = hash_create(n);
44
table->adaptive = TRUE;
45
#endif /* UNIV_DEBUG */
46
/* Creating MEM_HEAP_BTR_SEARCH type heaps can potentially fail,
47
but in practise it never should in this case, hence the asserts. */
50
table->heap = mem_heap_create_in_btr_search(
51
ut_min(4096, MEM_MAX_ALLOC_IN_BUF));
57
hash_create_mutexes(table, n_mutexes, mutex_level);
59
table->heaps = mem_alloc(n_mutexes * sizeof(void*));
61
for (i = 0; i < n_mutexes; i++) {
62
table->heaps[i] = mem_heap_create_in_btr_search(4096);
63
ut_a(table->heaps[i]);
69
/*****************************************************************
70
Empties a hash table and frees the memory heaps. */
75
hash_table_t* table) /* in, own: hash table */
80
#ifdef UNIV_SYNC_DEBUG
81
ut_ad(rw_lock_own(&btr_search_latch, RW_LOCK_EXCLUSIVE));
82
#endif /* UNIV_SYNC_DEBUG */
84
/* Free the memory heaps. */
87
for (i = 0; i < n; i++) {
88
mem_heap_free(table->heaps[i]);
91
/* Clear the hash table. */
92
n = hash_get_n_cells(table);
94
for (i = 0; i < n; i++) {
95
hash_get_nth_cell(table, i)->node = NULL;
99
/*****************************************************************
100
Inserts an entry into a hash table. If an entry with the same fold number
101
is found, its node is updated to point to the new data, and no new node
105
ha_insert_for_fold_func(
106
/*====================*/
107
/* out: TRUE if succeed, FALSE if no more
108
memory could be allocated */
109
hash_table_t* table, /* in: hash table */
110
ulint fold, /* in: folded value of data; if a node with
111
the same fold value already exists, it is
112
updated to point to the same data, and no new
115
buf_block_t* block, /* in: buffer block containing the data */
116
#endif /* UNIV_DEBUG */
117
void* data) /* in: data, must not be NULL */
121
ha_node_t* prev_node;
124
ut_ad(table && data);
125
ut_ad(block->frame == page_align(data));
126
ut_ad(!table->mutexes || mutex_own(hash_get_mutex(table, fold)));
128
hash = hash_calc_hash(fold, table);
130
cell = hash_get_nth_cell(table, hash);
132
prev_node = cell->node;
134
while (prev_node != NULL) {
135
if (prev_node->fold == fold) {
137
if (table->adaptive) {
138
buf_block_t* prev_block = prev_node->block;
139
ut_a(prev_block->frame
140
== page_align(prev_node->data));
141
ut_a(prev_block->n_pointers > 0);
142
prev_block->n_pointers--;
146
prev_node->block = block;
147
#endif /* UNIV_DEBUG */
148
prev_node->data = data;
153
prev_node = prev_node->next;
156
/* We have to allocate a new chain node */
158
node = mem_heap_alloc(hash_get_heap(table, fold), sizeof(ha_node_t));
161
/* It was a btr search type memory heap and at the moment
162
no more memory could be allocated: return */
164
ut_ad(hash_get_heap(table, fold)->type & MEM_HEAP_BTR_SEARCH);
169
ha_node_set_data(node, block, data);
172
if (table->adaptive) {
175
#endif /* UNIV_DEBUG */
180
prev_node = cell->node;
182
if (prev_node == NULL) {
189
while (prev_node->next != NULL) {
191
prev_node = prev_node->next;
194
prev_node->next = node;
199
/***************************************************************
200
Deletes a hash node. */
205
hash_table_t* table, /* in: hash table */
206
ha_node_t* del_node) /* in: node to be deleted */
209
if (table->adaptive) {
210
ut_a(del_node->block->frame = page_align(del_node->data));
211
ut_a(del_node->block->n_pointers > 0);
212
del_node->block->n_pointers--;
214
#endif /* UNIV_DEBUG */
215
HASH_DELETE_AND_COMPACT(ha_node_t, next, table, del_node);
218
/*****************************************************************
219
Deletes an entry from a hash table. */
224
hash_table_t* table, /* in: hash table */
225
ulint fold, /* in: folded value of data */
226
void* data) /* in: data, must not be NULL and must exist
231
ut_ad(!table->mutexes || mutex_own(hash_get_mutex(table, fold)));
233
node = ha_search_with_data(table, fold, data);
237
ha_delete_hash_node(table, node);
240
/*************************************************************
241
Looks for an element when we know the pointer to the data, and updates
242
the pointer to data, if found. */
245
ha_search_and_update_if_found_func(
246
/*===============================*/
247
hash_table_t* table, /* in: hash table */
248
ulint fold, /* in: folded value of the searched data */
249
void* data, /* in: pointer to the data */
251
buf_block_t* new_block,/* in: block containing new_data */
253
void* new_data)/* in: new pointer to the data */
257
ut_ad(!table->mutexes || mutex_own(hash_get_mutex(table, fold)));
258
ut_ad(new_block->frame == page_align(new_data));
260
node = ha_search_with_data(table, fold, data);
264
if (table->adaptive) {
265
ut_a(node->block->n_pointers > 0);
266
node->block->n_pointers--;
267
new_block->n_pointers++;
270
node->block = new_block;
271
#endif /* UNIV_DEBUG */
272
node->data = new_data;
276
/*********************************************************************
277
Removes from the chain determined by fold all nodes whose data pointer
278
points to the page given. */
281
ha_remove_all_nodes_to_page(
282
/*========================*/
283
hash_table_t* table, /* in: hash table */
284
ulint fold, /* in: fold value */
285
const page_t* page) /* in: buffer page */
289
ut_ad(!table->mutexes || mutex_own(hash_get_mutex(table, fold)));
291
node = ha_chain_get_first(table, fold);
294
if (page_align(ha_node_get_data(node)) == page) {
296
/* Remove the hash node */
298
ha_delete_hash_node(table, node);
300
/* Start again from the first node in the chain
301
because the deletion may compact the heap of
302
nodes and move other nodes! */
304
node = ha_chain_get_first(table, fold);
306
node = ha_chain_get_next(node);
310
/* Check that all nodes really got deleted */
312
node = ha_chain_get_first(table, fold);
315
ut_a(page_align(ha_node_get_data(node)) != page);
317
node = ha_chain_get_next(node);
322
/*****************************************************************
323
Validates a given range of the cells in hash table. */
328
/* out: TRUE if ok */
329
hash_table_t* table, /* in: hash table */
330
ulint start_index, /* in: start index */
331
ulint end_index) /* in: end index */
338
ut_a(start_index <= end_index);
339
ut_a(start_index < hash_get_n_cells(table));
340
ut_a(end_index < hash_get_n_cells(table));
342
for (i = start_index; i <= end_index; i++) {
344
cell = hash_get_nth_cell(table, i);
349
if (hash_calc_hash(node->fold, table) != i) {
350
ut_print_timestamp(stderr);
352
"InnoDB: Error: hash table node"
353
" fold value %lu does not\n"
354
"InnoDB: match the cell number %lu.\n",
355
(ulong) node->fold, (ulong) i);
367
/*****************************************************************
368
Prints info of a hash table. */
373
FILE* file, /* in: file where to print */
374
hash_table_t* table) /* in: hash table */
377
/* Some of the code here is disabled for performance reasons in production
378
builds, see http://bugs.mysql.com/36941 */
379
#define PRINT_USED_CELLS
380
#endif /* UNIV_DEBUG */
382
#ifdef PRINT_USED_CELLS
386
#endif /* PRINT_USED_CELLS */
389
#ifdef PRINT_USED_CELLS
390
for (i = 0; i < hash_get_n_cells(table); i++) {
392
cell = hash_get_nth_cell(table, i);
399
#endif /* PRINT_USED_CELLS */
401
fprintf(file, "Hash table size %lu",
402
(ulong) hash_get_n_cells(table));
404
#ifdef PRINT_USED_CELLS
405
fprintf(file, ", used cells %lu", (ulong) cells);
406
#endif /* PRINT_USED_CELLS */
408
if (table->heaps == NULL && table->heap != NULL) {
410
/* This calculation is intended for the adaptive hash
411
index: how many buffer frames we have reserved? */
413
n_bufs = UT_LIST_GET_LEN(table->heap->base) - 1;
415
if (table->heap->free_block) {
419
fprintf(file, ", node heap has %lu buffer(s)\n",