| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1994, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work 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 either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 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 "buf0types.h" |
| 40 |
#include "hash0hash.h" |
| 41 |
#include "page0types.h" |
| 42 |
#include "rem0types.h" |
| 43 |
|
| 44 |
/** Looks for an element in a hash table. |
| 45 |
@param[in] table hash table |
| 46 |
@param[in] hash_value hashed value of the searched data |
| 47 |
@return pointer to the data of the first hash table node in chain |
| 48 |
having the hash number, NULL if not found */ |
| 49 |
static inline const rec_t *ha_search_and_get_data(hash_table_t *table, |
| 50 |
uint64_t hash_value); |
| 51 |
|
| 52 |
/** Looks for an element when we know the pointer to the data and updates |
| 53 |
the pointer to data if found. |
| 54 |
@param[in,out] table hash table |
| 55 |
@param[in] hash_value hashed value of the searched data |
| 56 |
@param[in] data pointer to the data |
| 57 |
@param[in] new_block block containing new data |
| 58 |
@param[in] new_data new pointer to the data |
| 59 |
@return true if found */ |
| 60 |
bool ha_search_and_update_if_found_func(hash_table_t *table, |
| 61 |
uint64_t hash_value, const rec_t *data, |
| 62 |
IF_AHI_DEBUG(buf_block_t *new_block, ) |
| 63 |
const rec_t *new_data); |
| 64 |
|
| 65 |
/** Looks for an element when we know the pointer to the data and |
| 66 |
updates the pointer to data if found. |
| 67 |
@param table in/out: hash table |
| 68 |
@param hash_value in: hashed value of the searched data |
| 69 |
@param data in: pointer to the data |
| 70 |
@param new_block in: block containing new_data |
| 71 |
@param new_data in: new pointer to the data */ |
| 72 |
static inline bool ha_search_and_update_if_found(hash_table_t *table, |
| 73 |
uint64_t hash_value, |
| 74 |
const rec_t *data, |
| 75 |
buf_block_t *new_block, |
| 76 |
const rec_t *new_data) { |
| 77 |
return ha_search_and_update_if_found_func(table, hash_value, data, |
| 78 |
IF_AHI_DEBUG(new_block, ) new_data); |
| 79 |
} |
| 80 |
/** Creates a hash table with at least n array cells. The actual number |
| 81 |
of cells is chosen to be a prime number slightly bigger than n. |
| 82 |
@param[in] n number of array cells |
| 83 |
@param[in] id latch ID |
| 84 |
@param[in] n_sync_obj Number of sync objects protecting the hash table. |
| 85 |
Must be a power of 2, or 0. |
| 86 |
@param[in] type type of datastructure for which the memory heap is going |
| 87 |
to be used: |
| 88 |
MEM_HEAP_FOR_BTR_SEARCH or |
| 89 |
MEM_HEAP_FOR_PAGE_HASH |
| 90 |
@return own: created table */ |
| 91 |
hash_table_t *ib_create(size_t n, latch_id_t id, size_t n_sync_obj, |
| 92 |
uint32_t type); |
| 93 |
|
| 94 |
/** Empties a hash table and frees the memory heaps. */ |
| 95 |
void ha_clear(hash_table_t *table); /*!< in, own: hash table */ |
| 96 |
|
| 97 |
/** Inserts an entry into a hash table. If an entry with the same hash number |
| 98 |
is found, its node is updated to point to the new data, and no new node |
| 99 |
is inserted. If btr_search_enabled is set to false, we will only allow |
| 100 |
updating existing nodes, but no new node is allowed to be added. |
| 101 |
@param[in] table hash table |
| 102 |
@param[in] hash_value hashed value of data; if a node with the same hash value |
| 103 |
already exists, it is updated to point to the same data, and no new node is |
| 104 |
created! |
| 105 |
@param[in] block buffer block containing the data |
| 106 |
@param[in] data data, must not be NULL |
| 107 |
@return true if succeeded, false if no more memory could be allocated */ |
| 108 |
bool ha_insert_for_hash_func(hash_table_t *table, uint64_t hash_value, |
| 109 |
IF_AHI_DEBUG(buf_block_t *block, ) |
| 110 |
const rec_t *data); |
| 111 |
|
| 112 |
/** |
| 113 |
Inserts an entry into a hash table. If an entry with the same hash number |
| 114 |
is found, its node is updated to point to the new data, and no new node |
| 115 |
is inserted. |
| 116 |
@param[in] t hash table |
| 117 |
@param[in] f hashed value of data |
| 118 |
@param[in] b buffer block containing the data |
| 119 |
@param[in] d data, must not be NULL */ |
| 120 |
static inline void ha_insert_for_hash(hash_table_t *t, uint64_t f, |
| 121 |
buf_block_t *b, const rec_t *d) { |
| 122 |
ha_insert_for_hash_func(t, f, IF_AHI_DEBUG(b, ) d); |
| 123 |
MONITOR_ATOMIC_INC(MONITOR_ADAPTIVE_HASH_ROW_ADDED); |
| 124 |
} |
| 125 |
|
| 126 |
/** Looks for an element when we know the pointer to the data and deletes it |
| 127 |
from the hash table if found. |
| 128 |
@param[in] table hash table |
| 129 |
@param[in] hash_value hashed value of the searched data |
| 130 |
@param[in] data pointer to the data |
| 131 |
@return true if found */ |
| 132 |
static inline bool ha_search_and_delete_if_found(hash_table_t *table, |
| 133 |
uint64_t hash_value, |
| 134 |
const rec_t *data); |
| 135 |
|
| 136 |
#ifndef UNIV_HOTBACKUP |
| 137 |
|
| 138 |
/** Removes from the chain determined by hash value a single node whose data |
| 139 |
pointer points to the page given. Note that the node deleted can have a |
| 140 |
different hash value. |
| 141 |
@param[in] table Hash table |
| 142 |
@param[in] hash_value Hash value |
| 143 |
@param[in] page Buffer page */ |
| 144 |
void ha_remove_a_node_to_page(hash_table_t *table, uint64_t hash_value, |
| 145 |
const page_t *page); |
| 146 |
|
| 147 |
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG |
| 148 |
/** Validates a given range of the cells in hash table. |
| 149 |
@param[in] table Hash table |
| 150 |
@param[in] start_index Start index |
| 151 |
@param[in] end_index End index |
| 152 |
@return true if ok */ |
| 153 |
bool ha_validate(hash_table_t *table, uint64_t start_index, uint64_t end_index); |
| 154 |
#endif /* defined UNIV_AHI_DEBUG || defined UNIV_DEBUG */ |
| 155 |
|
| 156 |
/** Prints info of a hash table. |
| 157 |
@param[in] file File where to print |
| 158 |
@param[in] table Hash table */ |
| 159 |
void ha_print_info(FILE *file, hash_table_t *table); |
| 160 |
|
| 161 |
#endif /* !UNIV_HOTBACKUP */ |
| 162 |
|
| 163 |
/** The hash table external chain node */ |
| 164 |
struct ha_node_t { |
| 165 |
/** hash value for the data */ |
| 166 |
uint64_t hash_value; |
| 167 |
/** next chain node or NULL if none */ |
| 168 |
ha_node_t *next; |
| 169 |
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG |
| 170 |
/** buffer block containing the data, or NULL */ |
| 171 |
buf_block_t *block; |
| 172 |
#endif |
| 173 |
/** pointer to the data */ |
| 174 |
const rec_t *data; |
| 175 |
}; |
| 176 |
|
| 177 |
#include "ha0ha.ic" |
| 178 |
|
| 179 |
#endif |