1
/*****************************************************************************
3
Copyright (c) 1994, 2011, Oracle and/or its affiliates. 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.,
15
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
17
*****************************************************************************/
19
/**************************************************//**
21
The hash table with external chains
23
Created 8/18/1994 Heikki Tuuri
24
*******************************************************/
31
#include "hash0hash.h"
32
#include "page0types.h"
33
#include "buf0types.h"
34
#include "rem0types.h"
36
/*************************************************************//**
37
Looks for an element in a hash table.
38
@return pointer to the data of the first hash table node in chain
39
having the fold number, NULL if not found */
42
ha_search_and_get_data(
43
/*===================*/
44
hash_table_t* table, /*!< in: hash table */
45
ulint fold); /*!< in: folded value of the searched data */
46
/*********************************************************//**
47
Looks for an element when we know the pointer to the data and updates
48
the pointer to data if found.
49
@return TRUE if found */
52
ha_search_and_update_if_found_func(
53
/*===============================*/
54
hash_table_t* table, /*!< in/out: hash table */
55
ulint fold, /*!< in: folded value of the searched data */
56
const rec_t* data, /*!< in: pointer to the data */
57
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
58
buf_block_t* new_block,/*!< in: block containing new_data */
59
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
60
const rec_t* new_data);/*!< in: new pointer to the data */
62
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
63
/** Looks for an element when we know the pointer to the data and
64
updates the pointer to data if found.
65
@param table in/out: hash table
66
@param fold in: folded value of the searched data
67
@param data in: pointer to the data
68
@param new_block in: block containing new_data
69
@param new_data in: new pointer to the data */
70
# define ha_search_and_update_if_found(table,fold,data,new_block,new_data) \
71
ha_search_and_update_if_found_func(table,fold,data,new_block,new_data)
72
#else /* UNIV_AHI_DEBUG || UNIV_DEBUG */
73
/** Looks for an element when we know the pointer to the data and
74
updates the pointer to data if found.
75
@param table in/out: hash table
76
@param fold in: folded value of the searched data
77
@param data in: pointer to the data
78
@param new_block ignored: block containing new_data
79
@param new_data in: new pointer to the data */
80
# define ha_search_and_update_if_found(table,fold,data,new_block,new_data) \
81
ha_search_and_update_if_found_func(table,fold,data,new_data)
82
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
83
/*************************************************************//**
84
Creates a hash table with at least n array cells. The actual number
85
of cells is chosen to be a prime number slightly bigger than n.
86
@return own: created table */
91
ulint n, /*!< in: number of array cells */
92
#ifdef UNIV_SYNC_DEBUG
93
ulint mutex_level, /*!< in: level of the mutexes in the latching
94
order: this is used in the debug version */
95
#endif /* UNIV_SYNC_DEBUG */
96
ulint n_mutexes, /*!< in: number of mutexes to protect the
97
hash table: must be a power of 2, or 0 */
98
ulint type); /*!< in: type of datastructure for which
99
the memory heap is going to be used e.g.:
100
MEM_HEAP_FOR_BTR_SEARCH or
101
MEM_HEAP_FOR_PAGE_HASH */
102
#ifdef UNIV_SYNC_DEBUG
103
/** Creates a hash table.
104
@return own: created table
105
@param n_c in: number of array cells. The actual number of cells is
106
chosen to be a slightly bigger prime number.
107
@param level in: level of the mutexes in the latching order
108
@param n_m in: number of mutexes to protect the hash table;
109
must be a power of 2, or 0 */
110
# define ha_create(n_c,n_m,type,level) ha_create_func(n_c,level,n_m,type)
111
#else /* UNIV_SYNC_DEBUG */
112
/** Creates a hash table.
113
@return own: created table
114
@param n_c in: number of array cells. The actual number of cells is
115
chosen to be a slightly bigger prime number.
116
@param level in: level of the mutexes in the latching order
117
@param n_m in: number of mutexes to protect the hash table;
118
must be a power of 2, or 0 */
119
# define ha_create(n_c,n_m,type,level) ha_create_func(n_c,n_m,type)
120
#endif /* UNIV_SYNC_DEBUG */
122
/*************************************************************//**
123
Empties a hash table and frees the memory heaps. */
128
hash_table_t* table); /*!< in, own: hash table */
130
/*************************************************************//**
131
Inserts an entry into a hash table. If an entry with the same fold number
132
is found, its node is updated to point to the new data, and no new node
134
@return TRUE if succeed, FALSE if no more memory could be allocated */
137
ha_insert_for_fold_func(
138
/*====================*/
139
hash_table_t* table, /*!< in: hash table */
140
ulint fold, /*!< in: folded value of data; if a node with
141
the same fold value already exists, it is
142
updated to point to the same data, and no new
144
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
145
buf_block_t* block, /*!< in: buffer block containing the data */
146
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
147
const rec_t* data); /*!< in: data, must not be NULL */
149
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
151
Inserts an entry into a hash table. If an entry with the same fold number
152
is found, its node is updated to point to the new data, and no new node
154
@return TRUE if succeed, FALSE if no more memory could be allocated
155
@param t in: hash table
156
@param f in: folded value of data
157
@param b in: buffer block containing the data
158
@param d in: data, must not be NULL */
159
# define ha_insert_for_fold(t,f,b,d) do { \
160
ha_insert_for_fold_func(t,f,b,d); \
161
MONITOR_INC(MONITOR_ADAPTIVE_HASH_ROW_ADDED); \
163
#else /* UNIV_AHI_DEBUG || UNIV_DEBUG */
165
Inserts an entry into a hash table. If an entry with the same fold number
166
is found, its node is updated to point to the new data, and no new node
168
@return TRUE if succeed, FALSE if no more memory could be allocated
169
@param t in: hash table
170
@param f in: folded value of data
171
@param b ignored: buffer block containing the data
172
@param d in: data, must not be NULL */
173
# define ha_insert_for_fold(t,f,b,d) do { \
174
ha_insert_for_fold_func(t,f,d); \
175
MONITOR_INC(MONITOR_ADAPTIVE_HASH_ROW_ADDED); \
177
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
179
/*********************************************************//**
180
Looks for an element when we know the pointer to the data and deletes
181
it from the hash table if found.
182
@return TRUE if found */
185
ha_search_and_delete_if_found(
186
/*==========================*/
187
hash_table_t* table, /*!< in: hash table */
188
ulint fold, /*!< in: folded value of the searched data */
189
const rec_t* data); /*!< in: pointer to the data */
190
#ifndef UNIV_HOTBACKUP
191
/*****************************************************************//**
192
Removes from the chain determined by fold all nodes whose data pointer
193
points to the page given. */
196
ha_remove_all_nodes_to_page(
197
/*========================*/
198
hash_table_t* table, /*!< in: hash table */
199
ulint fold, /*!< in: fold value */
200
const page_t* page); /*!< in: buffer page */
201
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
202
/*************************************************************//**
203
Validates a given range of the cells in hash table.
204
@return TRUE if ok */
209
hash_table_t* table, /*!< in: hash table */
210
ulint start_index, /*!< in: start index */
211
ulint end_index); /*!< in: end index */
212
#endif /* defined UNIV_AHI_DEBUG || defined UNIV_DEBUG */
213
/*************************************************************//**
214
Prints info of a hash table. */
219
FILE* file, /*!< in: file where to print */
220
hash_table_t* table); /*!< in: hash table */
221
#endif /* !UNIV_HOTBACKUP */
223
/** The hash table external chain node */
225
ha_node_t* next; /*!< next chain node or NULL if none */
226
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
227
buf_block_t* block; /*!< buffer block containing the data, or NULL */
228
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
229
const rec_t* data; /*!< pointer to the data */
230
ulint fold; /*!< fold value for the data */
234
/********************************************************************//**
235
Assert that the synchronization object in a hash operation involving
236
possible change in the hash table is held.
237
Note that in case of mutexes we assert that mutex is owned while in case
238
of rw-locks we assert that it is held in exclusive mode. */
241
hash_assert_can_modify(
242
/*===================*/
243
hash_table_t* table, /*!< in: hash table */
244
ulint fold); /*!< in: fold value */
245
/********************************************************************//**
246
Assert that the synchronization object in a hash search operation is held.
247
Note that in case of mutexes we assert that mutex is owned while in case
248
of rw-locks we assert that it is held either in x-mode or s-mode. */
251
hash_assert_can_search(
252
/*===================*/
253
hash_table_t* table, /*!< in: hash table */
254
ulint fold); /*!< in: fold value */
255
#else /* UNIV_DEBUG */
256
#define hash_assert_can_modify(t, f)
257
#define hash_assert_can_search(t, f)
258
#endif /* UNIV_DEBUG */