1
/*****************************************************************************
3
Copyright (c) 1997, 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 simple hash table utility
22
Created 5/20/1997 Heikki Tuuri
23
*******************************************************/
30
#include "sync0sync.h"
32
typedef struct hash_table_struct hash_table_t;
33
typedef struct hash_cell_struct hash_cell_t;
35
typedef void* hash_node_t;
37
/* Fix Bug #13859: symbol collision between imap/mysql */
38
#define hash_create hash0_create
40
/*****************************************************************
41
Creates a hash table with >= n array cells. The actual number
42
of cells is chosen to be a prime number slightly bigger than n. */
47
/* out, own: created table */
48
ulint n); /* in: number of array cells */
49
/*****************************************************************
50
Creates a mutex array to protect a hash table. */
53
hash_create_mutexes_func(
54
/*=====================*/
55
hash_table_t* table, /* in: hash table */
56
#ifdef UNIV_SYNC_DEBUG
57
ulint sync_level, /* in: latching order level of the
58
mutexes: used in the debug version */
59
#endif /* UNIV_SYNC_DEBUG */
60
ulint n_mutexes); /* in: number of mutexes */
61
#ifdef UNIV_SYNC_DEBUG
62
# define hash_create_mutexes(t,n,level) hash_create_mutexes_func(t,level,n)
63
#else /* UNIV_SYNC_DEBUG */
64
# define hash_create_mutexes(t,n,level) hash_create_mutexes_func(t,n)
65
#endif /* UNIV_SYNC_DEBUG */
67
/*****************************************************************
68
Frees a hash table. */
73
hash_table_t* table); /* in, own: hash table */
74
/******************************************************************
75
Calculates the hash value from a folded value. */
80
/* out: hashed value */
81
ulint fold, /* in: folded value */
82
hash_table_t* table); /* in: hash table */
83
/************************************************************************
84
Assert that the mutex for the table in a hash operation is owned. */
85
#define HASH_ASSERT_OWNED(TABLE, FOLD) \
86
ut_ad(!(TABLE)->mutexes || mutex_own(hash_get_mutex(TABLE, FOLD)));
88
/***********************************************************************
89
Inserts a struct to a hash table. */
91
#define HASH_INSERT(TYPE, NAME, TABLE, FOLD, DATA)\
93
hash_cell_t* cell3333;\
96
HASH_ASSERT_OWNED(TABLE, FOLD)\
100
cell3333 = hash_get_nth_cell(TABLE, hash_calc_hash(FOLD, TABLE));\
102
if (cell3333->node == NULL) {\
103
cell3333->node = DATA;\
105
struct3333 = (TYPE*) cell3333->node;\
107
while (struct3333->NAME != NULL) {\
109
struct3333 = (TYPE*) struct3333->NAME;\
112
struct3333->NAME = DATA;\
116
#ifdef UNIV_HASH_DEBUG
117
# define HASH_ASSERT_VALID(DATA) ut_a((void*) (DATA) != (void*) -1)
118
# define HASH_INVALIDATE(DATA, NAME) DATA->NAME = (void*) -1
120
# define HASH_ASSERT_VALID(DATA) do {} while (0)
121
# define HASH_INVALIDATE(DATA, NAME) do {} while (0)
124
/***********************************************************************
125
Deletes a struct from a hash table. */
127
#define HASH_DELETE(TYPE, NAME, TABLE, FOLD, DATA)\
129
hash_cell_t* cell3333;\
132
HASH_ASSERT_OWNED(TABLE, FOLD)\
134
cell3333 = hash_get_nth_cell(TABLE, hash_calc_hash(FOLD, TABLE));\
136
if (cell3333->node == DATA) {\
137
HASH_ASSERT_VALID(DATA->NAME);\
138
cell3333->node = DATA->NAME;\
140
struct3333 = (TYPE*) cell3333->node;\
142
while (struct3333->NAME != DATA) {\
144
struct3333 = (TYPE*) struct3333->NAME;\
148
struct3333->NAME = DATA->NAME;\
150
HASH_INVALIDATE(DATA, NAME);\
153
/***********************************************************************
154
Gets the first struct in a hash chain, NULL if none. */
156
#define HASH_GET_FIRST(TABLE, HASH_VAL)\
157
(hash_get_nth_cell(TABLE, HASH_VAL)->node)
159
/***********************************************************************
160
Gets the next struct in a hash chain, NULL if none. */
162
#define HASH_GET_NEXT(NAME, DATA) ((DATA)->NAME)
164
/************************************************************************
165
Looks for a struct in a hash table. */
166
#define HASH_SEARCH(NAME, TABLE, FOLD, TYPE, DATA, ASSERTION, TEST)\
169
HASH_ASSERT_OWNED(TABLE, FOLD)\
171
(DATA) = (TYPE) HASH_GET_FIRST(TABLE, hash_calc_hash(FOLD, TABLE));\
172
HASH_ASSERT_VALID(DATA);\
174
while ((DATA) != NULL) {\
179
HASH_ASSERT_VALID(HASH_GET_NEXT(NAME, DATA));\
180
(DATA) = (TYPE) HASH_GET_NEXT(NAME, DATA);\
185
/************************************************************************
186
Looks for an item in all hash buckets. */
187
#define HASH_SEARCH_ALL(NAME, TABLE, TYPE, DATA, ASSERTION, TEST) \
191
for (i3333 = (TABLE)->n_cells; i3333--; ) { \
192
(DATA) = (TYPE) HASH_GET_FIRST(TABLE, i3333); \
194
while ((DATA) != NULL) { \
195
HASH_ASSERT_VALID(DATA); \
202
(DATA) = (TYPE) HASH_GET_NEXT(NAME, DATA); \
205
if ((DATA) != NULL) { \
211
/****************************************************************
212
Gets the nth cell in a hash table. */
217
/* out: pointer to cell */
218
hash_table_t* table, /* in: hash table */
219
ulint n); /* in: cell index */
221
/*****************************************************************
222
Clears a hash table so that all the cells become empty. */
227
hash_table_t* table); /* in/out: hash table */
229
/*****************************************************************
230
Returns the number of cells in a hash table. */
235
/* out: number of cells */
236
hash_table_t* table); /* in: table */
237
/***********************************************************************
238
Deletes a struct which is stored in the heap of the hash table, and compacts
239
the heap. The fold value must be stored in the struct NODE in a field named
242
#define HASH_DELETE_AND_COMPACT(TYPE, NAME, TABLE, NODE)\
246
hash_cell_t* cell111;\
249
fold111 = (NODE)->fold;\
251
HASH_DELETE(TYPE, NAME, TABLE, fold111, NODE);\
253
top_node111 = (TYPE*)mem_heap_get_top(\
254
hash_get_heap(TABLE, fold111),\
257
/* If the node to remove is not the top node in the heap, compact the\
258
heap of nodes by moving the top node in the place of NODE. */\
260
if (NODE != top_node111) {\
262
/* Copy the top node in place of NODE */\
264
*(NODE) = *top_node111;\
266
cell111 = hash_get_nth_cell(TABLE,\
267
hash_calc_hash(top_node111->fold, TABLE));\
269
/* Look for the pointer to the top node, to update it */\
271
if (cell111->node == top_node111) {\
272
/* The top node is the first in the chain */\
274
cell111->node = NODE;\
276
/* We have to look for the predecessor of the top\
278
node111 = cell111->node;\
280
while (top_node111 != HASH_GET_NEXT(NAME, node111)) {\
282
node111 = HASH_GET_NEXT(NAME, node111);\
285
/* Now we have the predecessor node */\
287
node111->NAME = NODE;\
291
/* Free the space occupied by the top node */\
293
mem_heap_free_top(hash_get_heap(TABLE, fold111), sizeof(TYPE));\
296
/********************************************************************
297
Move all hash table entries from OLD_TABLE to NEW_TABLE.*/
299
#define HASH_MIGRATE(OLD_TABLE, NEW_TABLE, NODE_TYPE, PTR_NAME, FOLD_FUNC) \
302
ulint cell_count2222;\
304
cell_count2222 = hash_get_n_cells(OLD_TABLE);\
306
for (i2222 = 0; i2222 < cell_count2222; i2222++) {\
307
NODE_TYPE* node2222 = HASH_GET_FIRST((OLD_TABLE), i2222);\
310
NODE_TYPE* next2222 = node2222->PTR_NAME;\
311
ulint fold2222 = FOLD_FUNC(node2222);\
313
HASH_INSERT(NODE_TYPE, PTR_NAME, (NEW_TABLE),\
314
fold2222, node2222);\
316
node2222 = next2222;\
322
/****************************************************************
323
Gets the mutex index for a fold value in a hash table. */
328
/* out: mutex number */
329
hash_table_t* table, /* in: hash table */
330
ulint fold); /* in: fold */
331
/****************************************************************
332
Gets the nth heap in a hash table. */
338
hash_table_t* table, /* in: hash table */
339
ulint i); /* in: index of the heap */
340
/****************************************************************
341
Gets the heap for a fold value in a hash table. */
347
hash_table_t* table, /* in: hash table */
348
ulint fold); /* in: fold */
349
/****************************************************************
350
Gets the nth mutex in a hash table. */
356
hash_table_t* table, /* in: hash table */
357
ulint i); /* in: index of the mutex */
358
/****************************************************************
359
Gets the mutex for a fold value in a hash table. */
365
hash_table_t* table, /* in: hash table */
366
ulint fold); /* in: fold */
367
/****************************************************************
368
Reserves the mutex for a fold value in a hash table. */
373
hash_table_t* table, /* in: hash table */
374
ulint fold); /* in: fold */
375
/****************************************************************
376
Releases the mutex for a fold value in a hash table. */
381
hash_table_t* table, /* in: hash table */
382
ulint fold); /* in: fold */
383
/****************************************************************
384
Reserves all the mutexes of a hash table, in an ascending order. */
387
hash_mutex_enter_all(
388
/*=================*/
389
hash_table_t* table); /* in: hash table */
390
/****************************************************************
391
Releases all the mutexes of a hash table. */
396
hash_table_t* table); /* in: hash table */
399
struct hash_cell_struct{
400
void* node; /* hash chain node, NULL if none */
403
/* The hash table structure */
404
struct hash_table_struct {
405
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
406
ibool adaptive;/* TRUE if this is the hash table of the
407
adaptive hash index */
408
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
409
ulint n_cells;/* number of cells in the hash table */
410
hash_cell_t* array; /* pointer to cell array */
411
ulint n_mutexes;/* if mutexes != NULL, then the number of
412
mutexes, must be a power of 2 */
413
mutex_t* mutexes;/* NULL, or an array of mutexes used to
414
protect segments of the hash table */
415
mem_heap_t** heaps; /* if this is non-NULL, hash chain nodes for
416
external chaining can be allocated from these
417
memory heaps; there are then n_mutexes many of
423
#define HASH_TABLE_MAGIC_N 76561114
426
#include "hash0hash.ic"