1
/*****************************************************************************
3
Copyright (c) 1994, 2009, Innobase Oy. All Rights Reserved.
5
This program is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free Software
7
Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License along with
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15
Place, Suite 330, Boston, MA 02111-1307 USA
17
*****************************************************************************/
19
/************************************************************************
20
The hash table with external chains
22
Created 8/22/1994 Heikki Tuuri
23
*************************************************************************/
32
#endif /* UNIV_DEBUG */
33
#ifdef UNIV_SYNC_DEBUG
35
#endif /* UNIV_SYNC_DEBUG */
36
#include "page0page.h"
38
/*****************************************************************
39
Creates a hash table with >= n array cells. The actual number of cells is
40
chosen to be a prime number slightly bigger than n. */
45
/* out, own: created table */
46
ulint n, /* in: number of array cells */
47
#ifdef UNIV_SYNC_DEBUG
48
ulint mutex_level, /* in: level of the mutexes in the latching
49
order: this is used in the debug version */
50
#endif /* UNIV_SYNC_DEBUG */
51
ulint n_mutexes) /* in: number of mutexes to protect the
52
hash table: must be a power of 2, or 0 */
55
#ifndef UNIV_HOTBACKUP
57
#endif /* !UNIV_HOTBACKUP */
59
table = hash_create(n);
61
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
62
# ifndef UNIV_HOTBACKUP
63
table->adaptive = TRUE;
64
# endif /* !UNIV_HOTBACKUP */
65
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
66
/* Creating MEM_HEAP_BTR_SEARCH type heaps can potentially fail,
67
but in practise it never should in this case, hence the asserts. */
70
table->heap = mem_heap_create_in_btr_search(
71
ut_min(4096, MEM_MAX_ALLOC_IN_BUF));
77
#ifndef UNIV_HOTBACKUP
78
hash_create_mutexes(table, n_mutexes, mutex_level);
80
table->heaps = mem_alloc(n_mutexes * sizeof(void*));
82
for (i = 0; i < n_mutexes; i++) {
83
table->heaps[i] = mem_heap_create_in_btr_search(4096);
84
ut_a(table->heaps[i]);
86
#endif /* !UNIV_HOTBACKUP */
91
/*****************************************************************
92
Empties a hash table and frees the memory heaps. */
97
hash_table_t* table) /* in, own: hash table */
102
#ifdef UNIV_SYNC_DEBUG
103
ut_ad(rw_lock_own(&btr_search_latch, RW_LOCK_EXCLUSIVE));
104
#endif /* UNIV_SYNC_DEBUG */
106
#ifndef UNIV_HOTBACKUP
107
/* Free the memory heaps. */
108
n = table->n_mutexes;
110
for (i = 0; i < n; i++) {
111
mem_heap_free(table->heaps[i]);
113
#endif /* !UNIV_HOTBACKUP */
115
/* Clear the hash table. */
116
n = hash_get_n_cells(table);
118
for (i = 0; i < n; i++) {
119
hash_get_nth_cell(table, i)->node = NULL;
123
/*****************************************************************
124
Inserts an entry into a hash table. If an entry with the same fold number
125
is found, its node is updated to point to the new data, and no new node
129
ha_insert_for_fold_func(
130
/*====================*/
131
/* out: TRUE if succeed, FALSE if no more
132
memory could be allocated */
133
hash_table_t* table, /* in: hash table */
134
ulint fold, /* in: folded value of data; if a node with
135
the same fold value already exists, it is
136
updated to point to the same data, and no new
138
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
139
buf_block_t* block, /* in: buffer block containing the data */
140
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
141
void* data) /* in: data, must not be NULL */
145
ha_node_t* prev_node;
148
ut_ad(table && data);
149
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
150
ut_a(block->frame == page_align(data));
151
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
152
ASSERT_HASH_MUTEX_OWN(table, fold);
154
hash = hash_calc_hash(fold, table);
156
cell = hash_get_nth_cell(table, hash);
158
prev_node = cell->node;
160
while (prev_node != NULL) {
161
if (prev_node->fold == fold) {
162
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
163
# ifndef UNIV_HOTBACKUP
164
if (table->adaptive) {
165
buf_block_t* prev_block = prev_node->block;
166
ut_a(prev_block->frame
167
== page_align(prev_node->data));
168
ut_a(prev_block->n_pointers > 0);
169
prev_block->n_pointers--;
172
# endif /* !UNIV_HOTBACKUP */
174
prev_node->block = block;
175
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
176
prev_node->data = data;
181
prev_node = prev_node->next;
184
/* We have to allocate a new chain node */
186
node = mem_heap_alloc(hash_get_heap(table, fold), sizeof(ha_node_t));
189
/* It was a btr search type memory heap and at the moment
190
no more memory could be allocated: return */
192
ut_ad(hash_get_heap(table, fold)->type & MEM_HEAP_BTR_SEARCH);
197
ha_node_set_data(node, block, data);
199
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
200
# ifndef UNIV_HOTBACKUP
201
if (table->adaptive) {
204
# endif /* !UNIV_HOTBACKUP */
205
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
211
prev_node = cell->node;
213
if (prev_node == NULL) {
220
while (prev_node->next != NULL) {
222
prev_node = prev_node->next;
225
prev_node->next = node;
230
/***************************************************************
231
Deletes a hash node. */
236
hash_table_t* table, /* in: hash table */
237
ha_node_t* del_node) /* in: node to be deleted */
239
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
240
# ifndef UNIV_HOTBACKUP
241
if (table->adaptive) {
242
ut_a(del_node->block->frame = page_align(del_node->data));
243
ut_a(del_node->block->n_pointers > 0);
244
del_node->block->n_pointers--;
246
# endif /* !UNIV_HOTBACKUP */
247
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
249
HASH_DELETE_AND_COMPACT(ha_node_t, next, table, del_node);
252
/*****************************************************************
253
Deletes an entry from a hash table. */
258
hash_table_t* table, /* in: hash table */
259
ulint fold, /* in: folded value of data */
260
void* data) /* in: data, must not be NULL and must exist
265
ASSERT_HASH_MUTEX_OWN(table, fold);
267
node = ha_search_with_data(table, fold, data);
271
ha_delete_hash_node(table, node);
274
/*************************************************************
275
Looks for an element when we know the pointer to the data, and updates
276
the pointer to data, if found. */
279
ha_search_and_update_if_found_func(
280
/*===============================*/
281
hash_table_t* table, /* in: hash table */
282
ulint fold, /* in: folded value of the searched data */
283
void* data, /* in: pointer to the data */
284
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
285
buf_block_t* new_block,/* in: block containing new_data */
286
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
287
void* new_data)/* in: new pointer to the data */
291
ASSERT_HASH_MUTEX_OWN(table, fold);
292
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
293
ut_a(new_block->frame == page_align(new_data));
294
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
296
node = ha_search_with_data(table, fold, data);
299
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
300
# ifndef UNIV_HOTBACKUP
301
if (table->adaptive) {
302
ut_a(node->block->n_pointers > 0);
303
node->block->n_pointers--;
304
new_block->n_pointers++;
306
# endif /* !UNIV_HOTBACKUP */
308
node->block = new_block;
309
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
310
node->data = new_data;
314
#ifndef UNIV_HOTBACKUP
315
/*********************************************************************
316
Removes from the chain determined by fold all nodes whose data pointer
317
points to the page given. */
320
ha_remove_all_nodes_to_page(
321
/*========================*/
322
hash_table_t* table, /* in: hash table */
323
ulint fold, /* in: fold value */
324
const page_t* page) /* in: buffer page */
328
ASSERT_HASH_MUTEX_OWN(table, fold);
330
node = ha_chain_get_first(table, fold);
333
if (page_align(ha_node_get_data(node)) == page) {
335
/* Remove the hash node */
337
ha_delete_hash_node(table, node);
339
/* Start again from the first node in the chain
340
because the deletion may compact the heap of
341
nodes and move other nodes! */
343
node = ha_chain_get_first(table, fold);
345
node = ha_chain_get_next(node);
349
/* Check that all nodes really got deleted */
351
node = ha_chain_get_first(table, fold);
354
ut_a(page_align(ha_node_get_data(node)) != page);
356
node = ha_chain_get_next(node);
361
/*****************************************************************
362
Validates a given range of the cells in hash table. */
367
/* out: TRUE if ok */
368
hash_table_t* table, /* in: hash table */
369
ulint start_index, /* in: start index */
370
ulint end_index) /* in: end index */
377
ut_a(start_index <= end_index);
378
ut_a(start_index < hash_get_n_cells(table));
379
ut_a(end_index < hash_get_n_cells(table));
381
for (i = start_index; i <= end_index; i++) {
383
cell = hash_get_nth_cell(table, i);
388
if (hash_calc_hash(node->fold, table) != i) {
389
ut_print_timestamp(stderr);
391
"InnoDB: Error: hash table node"
392
" fold value %lu does not\n"
393
"InnoDB: match the cell number %lu.\n",
394
(ulong) node->fold, (ulong) i);
406
/*****************************************************************
407
Prints info of a hash table. */
412
FILE* file, /* in: file where to print */
413
hash_table_t* table) /* in: hash table */
415
#ifdef PRINT_USED_CELLS
419
#endif /* PRINT_USED_CELLS */
422
#ifdef PRINT_USED_CELLS
423
for (i = 0; i < hash_get_n_cells(table); i++) {
425
cell = hash_get_nth_cell(table, i);
432
#endif /* PRINT_USED_CELLS */
434
fprintf(file, "Hash table size %lu",
435
(ulong) hash_get_n_cells(table));
437
#ifdef PRINT_USED_CELLS
438
fprintf(file, ", used cells %lu", (ulong) cells);
439
#endif /* PRINT_USED_CELLS */
441
if (table->heaps == NULL && table->heap != NULL) {
443
/* This calculation is intended for the adaptive hash
444
index: how many buffer frames we have reserved? */
446
n_bufs = UT_LIST_GET_LEN(table->heap->base) - 1;
448
if (table->heap->free_block) {
452
fprintf(file, ", node heap has %lu buffer(s)\n",
456
#endif /* !UNIV_HOTBACKUP */