1 /*****************************************************************************
2 
3 Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8 
9 This program is also distributed 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 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License, version 2.0, for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
24 
25 *****************************************************************************/
26 
27 /**************************************************//**
28 @file include/ha0ha.h
29 The hash table with external chains
30 
31 Created 8/18/1994 Heikki Tuuri
32 *******************************************************/
33 
34 #ifndef ha0ha_h
35 #define ha0ha_h
36 
37 #include "univ.i"
38 
39 #include "hash0hash.h"
40 #include "page0types.h"
41 #include "buf0types.h"
42 #include "rem0types.h"
43 
44 /*************************************************************//**
45 Looks for an element in a hash table.
46 @return pointer to the data of the first hash table node in chain
47 having the fold number, NULL if not found */
48 UNIV_INLINE
49 const rec_t*
50 ha_search_and_get_data(
51 /*===================*/
52 	hash_table_t*	table,	/*!< in: hash table */
53 	ulint		fold);	/*!< in: folded value of the searched data */
54 /*********************************************************//**
55 Looks for an element when we know the pointer to the data and updates
56 the pointer to data if found.
57 @return TRUE if found */
58 UNIV_INTERN
59 ibool
60 ha_search_and_update_if_found_func(
61 /*===============================*/
62 	hash_table_t*	table,	/*!< in/out: hash table */
63 	ulint		fold,	/*!< in: folded value of the searched data */
64 	const rec_t*	data,	/*!< in: pointer to the data */
65 #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
66 	buf_block_t*	new_block,/*!< in: block containing new_data */
67 #endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
68 	const rec_t*	new_data);/*!< in: new pointer to the data */
69 
70 #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
71 /** Looks for an element when we know the pointer to the data and
72 updates the pointer to data if found.
73 @param table		in/out: hash table
74 @param fold		in: folded value of the searched data
75 @param data		in: pointer to the data
76 @param new_block	in: block containing new_data
77 @param new_data		in: new pointer to the data */
78 # define ha_search_and_update_if_found(table,fold,data,new_block,new_data) \
79 	ha_search_and_update_if_found_func(table,fold,data,new_block,new_data)
80 #else /* UNIV_AHI_DEBUG || UNIV_DEBUG */
81 /** Looks for an element when we know the pointer to the data and
82 updates the pointer to data if found.
83 @param table		in/out: hash table
84 @param fold		in: folded value of the searched data
85 @param data		in: pointer to the data
86 @param new_block	ignored: block containing new_data
87 @param new_data		in: new pointer to the data */
88 # define ha_search_and_update_if_found(table,fold,data,new_block,new_data) \
89 	ha_search_and_update_if_found_func(table,fold,data,new_data)
90 #endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
91 /*************************************************************//**
92 Creates a hash table with at least n array cells.  The actual number
93 of cells is chosen to be a prime number slightly bigger than n.
94 @return	own: created table */
95 UNIV_INTERN
96 hash_table_t*
97 ha_create_func(
98 /*===========*/
99 	ulint	n,		/*!< in: number of array cells */
100 #ifdef UNIV_SYNC_DEBUG
101 	ulint	mutex_level,	/*!< in: level of the mutexes in the latching
102 				order: this is used in the debug version */
103 #endif /* UNIV_SYNC_DEBUG */
104 	ulint	n_mutexes,	/*!< in: number of mutexes to protect the
105 				hash table: must be a power of 2, or 0 */
106 	ulint	type);		/*!< in: type of datastructure for which
107 				the memory heap is going to be used e.g.:
108 				MEM_HEAP_FOR_BTR_SEARCH or
109 				MEM_HEAP_FOR_PAGE_HASH */
110 #ifdef UNIV_SYNC_DEBUG
111 /** Creates a hash table.
112 @return		own: created table
113 @param n_c	in: number of array cells.  The actual number of cells is
114 chosen to be a slightly bigger prime number.
115 @param level	in: level of the mutexes in the latching order
116 @param n_m	in: number of mutexes to protect the hash table;
117 		must be a power of 2, or 0 */
118 # define ib_create(n_c,n_m,type,level) ha_create_func(n_c,level,n_m,type)
119 #else /* UNIV_SYNC_DEBUG */
120 /** Creates a hash table.
121 @return		own: created table
122 @param n_c	in: number of array cells.  The actual number of cells is
123 chosen to be a slightly bigger prime number.
124 @param level	in: level of the mutexes in the latching order
125 @param n_m	in: number of mutexes to protect the hash table;
126 		must be a power of 2, or 0 */
127 # define ib_create(n_c,n_m,type,level) ha_create_func(n_c,n_m,type)
128 #endif /* UNIV_SYNC_DEBUG */
129 
130 /*************************************************************//**
131 Empties a hash table and frees the memory heaps. */
132 UNIV_INTERN
133 void
134 ha_clear(
135 /*=====*/
136 	hash_table_t*	table);	/*!< in, own: hash table */
137 
138 /*************************************************************//**
139 Inserts an entry into a hash table. If an entry with the same fold number
140 is found, its node is updated to point to the new data, and no new node
141 is inserted.
142 @return	TRUE if succeed, FALSE if no more memory could be allocated */
143 UNIV_INTERN
144 ibool
145 ha_insert_for_fold_func(
146 /*====================*/
147 	hash_table_t*	table,	/*!< in: hash table */
148 	ulint		fold,	/*!< in: folded value of data; if a node with
149 				the same fold value already exists, it is
150 				updated to point to the same data, and no new
151 				node is created! */
152 #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
153 	buf_block_t*	block,	/*!< in: buffer block containing the data */
154 #endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
155 	const rec_t*	data);	/*!< in: data, must not be NULL */
156 
157 #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
158 /**
159 Inserts an entry into a hash table. If an entry with the same fold number
160 is found, its node is updated to point to the new data, and no new node
161 is inserted.
162 @return	TRUE if succeed, FALSE if no more memory could be allocated
163 @param t	in: hash table
164 @param f	in: folded value of data
165 @param b	in: buffer block containing the data
166 @param d	in: data, must not be NULL */
167 # define ha_insert_for_fold(t,f,b,d) 	do {		\
168 	ha_insert_for_fold_func(t,f,b,d);		\
169 	MONITOR_INC(MONITOR_ADAPTIVE_HASH_ROW_ADDED);	\
170 } while(0)
171 #else /* UNIV_AHI_DEBUG || UNIV_DEBUG */
172 /**
173 Inserts an entry into a hash table. If an entry with the same fold number
174 is found, its node is updated to point to the new data, and no new node
175 is inserted.
176 @return	TRUE if succeed, FALSE if no more memory could be allocated
177 @param t	in: hash table
178 @param f	in: folded value of data
179 @param b	ignored: buffer block containing the data
180 @param d	in: data, must not be NULL */
181 # define ha_insert_for_fold(t,f,b,d)	do {		\
182 	ha_insert_for_fold_func(t,f,d);			\
183 	MONITOR_INC(MONITOR_ADAPTIVE_HASH_ROW_ADDED);	\
184 } while (0)
185 #endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
186 
187 /*********************************************************//**
188 Looks for an element when we know the pointer to the data and deletes
189 it from the hash table if found.
190 @return	TRUE if found */
191 UNIV_INLINE
192 ibool
193 ha_search_and_delete_if_found(
194 /*==========================*/
195 	hash_table_t*	table,	/*!< in: hash table */
196 	ulint		fold,	/*!< in: folded value of the searched data */
197 	const rec_t*	data);	/*!< in: pointer to the data */
198 #ifndef UNIV_HOTBACKUP
199 /*****************************************************************//**
200 Removes from the chain determined by fold all nodes whose data pointer
201 points to the page given. */
202 UNIV_INTERN
203 void
204 ha_remove_all_nodes_to_page(
205 /*========================*/
206 	hash_table_t*	table,	/*!< in: hash table */
207 	ulint		fold,	/*!< in: fold value */
208 	const page_t*	page);	/*!< in: buffer page */
209 #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
210 /*************************************************************//**
211 Validates a given range of the cells in hash table.
212 @return	TRUE if ok */
213 UNIV_INTERN
214 ibool
215 ha_validate(
216 /*========*/
217 	hash_table_t*	table,		/*!< in: hash table */
218 	ulint		start_index,	/*!< in: start index */
219 	ulint		end_index);	/*!< in: end index */
220 #endif /* defined UNIV_AHI_DEBUG || defined UNIV_DEBUG */
221 /*************************************************************//**
222 Prints info of a hash table. */
223 UNIV_INTERN
224 void
225 ha_print_info(
226 /*==========*/
227 	FILE*		file,	/*!< in: file where to print */
228 	hash_table_t*	table);	/*!< in: hash table */
229 #endif /* !UNIV_HOTBACKUP */
230 
231 /** The hash table external chain node */
232 struct ha_node_t {
233 	ha_node_t*	next;	/*!< next chain node or NULL if none */
234 #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
235 	buf_block_t*	block;	/*!< buffer block containing the data, or NULL */
236 #endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
237 	const rec_t*	data;	/*!< pointer to the data */
238 	ulint		fold;	/*!< fold value for the data */
239 };
240 
241 #ifdef UNIV_DEBUG
242 /********************************************************************//**
243 Assert that the synchronization object in a hash operation involving
244 possible change in the hash table is held.
245 Note that in case of mutexes we assert that mutex is owned while in case
246 of rw-locks we assert that it is held in exclusive mode. */
247 UNIV_INLINE
248 void
249 hash_assert_can_modify(
250 /*===================*/
251 	hash_table_t*	table,	/*!< in: hash table */
252 	ulint		fold);	/*!< in: fold value */
253 /********************************************************************//**
254 Assert that the synchronization object in a hash search operation is held.
255 Note that in case of mutexes we assert that mutex is owned while in case
256 of rw-locks we assert that it is held either in x-mode or s-mode. */
257 UNIV_INLINE
258 void
259 hash_assert_can_search(
260 /*===================*/
261 	hash_table_t*	table,	/*!< in: hash table */
262 	ulint		fold);	/*!< in: fold value */
263 #else /* UNIV_DEBUG */
264 #define hash_assert_can_modify(t, f)
265 #define hash_assert_can_search(t, f)
266 #endif /* UNIV_DEBUG */
267 
268 
269 #ifndef UNIV_NONINL
270 #include "ha0ha.ic"
271 #endif
272 
273 #endif
274