| 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.ic |
| 29 |
The hash table with external chains |
| 30 |
|
| 31 |
Created 8/18/1994 Heikki Tuuri |
| 32 |
*************************************************************************/ |
| 33 |
|
| 34 |
#include "btr0types.h" |
| 35 |
#include "mem0mem.h" |
| 36 |
#include "ut0rnd.h" |
| 37 |
|
| 38 |
/** Deletes a hash node. */ |
| 39 |
void ha_delete_hash_node(hash_table_t *table, /*!< in: hash table */ |
| 40 |
ha_node_t *del_node); /*!< in: node to be deleted */ |
| 41 |
|
| 42 |
/** Gets a hash node data. |
| 43 |
@return pointer to the data */ |
| 44 |
static inline const rec_t *ha_node_get_data( |
| 45 |
const ha_node_t *node) /*!< in: hash chain node */ |
| 46 |
{ |
| 47 |
return node->data; |
| 48 |
} |
| 49 |
|
| 50 |
/** Sets hash node data. |
| 51 |
@param[in] node chain node |
| 52 |
@param[in] block buffer block containing the data |
| 53 |
@param[in] data pointer to the data */ |
| 54 |
static inline void ha_node_set_data(ha_node_t *node, |
| 55 |
IF_AHI_DEBUG(buf_block_t *block, ) |
| 56 |
const rec_t *data) { |
| 57 |
IF_AHI_DEBUG(node->block = block;) |
| 58 |
node->data = data; |
| 59 |
} |
| 60 |
|
| 61 |
/** Gets the next node in a hash chain. |
| 62 |
@return next node, NULL if none */ |
| 63 |
static inline ha_node_t *ha_chain_get_next( |
| 64 |
const ha_node_t *node) /*!< in: hash chain node */ |
| 65 |
{ |
| 66 |
return node->next; |
| 67 |
} |
| 68 |
|
| 69 |
/** Gets the first node in a hash chain. |
| 70 |
@return first node, NULL if none */ |
| 71 |
static inline ha_node_t *ha_chain_get_first( |
| 72 |
hash_table_t *table, /*!< in: hash table */ |
| 73 |
uint64_t hash_value) /*!< in: hash value determining the chain */ |
| 74 |
{ |
| 75 |
return (ha_node_t *)hash_get_nth_cell(table, |
| 76 |
hash_calc_cell_id(hash_value, table)) |
| 77 |
->node; |
| 78 |
} |
| 79 |
|
| 80 |
/** Looks for an element in a hash table. |
| 81 |
@return pointer to the data of the first hash table node in chain |
| 82 |
having the hash number, NULL if not found */ |
| 83 |
static inline const rec_t *ha_search_and_get_data( |
| 84 |
hash_table_t *table, /*!< in: hash table */ |
| 85 |
uint64_t hash_value) /*!< in: hashed value of the searched data */ |
| 86 |
{ |
| 87 |
hash_assert_can_search(table, hash_value); |
| 88 |
ut_ad(btr_search_enabled); |
| 89 |
|
| 90 |
for (const ha_node_t *node = ha_chain_get_first(table, hash_value); |
| 91 |
node != nullptr; node = ha_chain_get_next(node)) { |
| 92 |
if (node->hash_value == hash_value) { |
| 93 |
return node->data; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return nullptr; |
| 98 |
} |
| 99 |
|
| 100 |
/** Looks for an element when we know the pointer to the data. |
| 101 |
@return pointer to the hash table node, NULL if not found in the table */ |
| 102 |
static inline ha_node_t *ha_search_with_data( |
| 103 |
hash_table_t *table, /*!< in: hash table */ |
| 104 |
uint64_t hash_value, /*!< in: hashed value of the searched data */ |
| 105 |
const rec_t *data) /*!< in: pointer to the data */ |
| 106 |
{ |
| 107 |
ha_node_t *node; |
| 108 |
|
| 109 |
hash_assert_can_search(table, hash_value); |
| 110 |
|
| 111 |
ut_ad(btr_search_enabled); |
| 112 |
|
| 113 |
node = ha_chain_get_first(table, hash_value); |
| 114 |
|
| 115 |
while (node) { |
| 116 |
if (node->data == data) { |
| 117 |
return node; |
| 118 |
} |
| 119 |
|
| 120 |
node = ha_chain_get_next(node); |
| 121 |
} |
| 122 |
|
| 123 |
return nullptr; |
| 124 |
} |
| 125 |
|
| 126 |
/** Looks for an element when we know the pointer to the data, and deletes |
| 127 |
it from the hash table, if found. |
| 128 |
@return true if found */ |
| 129 |
static inline bool ha_search_and_delete_if_found( |
| 130 |
hash_table_t *table, /*!< in: hash table */ |
| 131 |
uint64_t hash_value, /*!< in: hashed value of the searched data */ |
| 132 |
const rec_t *data) /*!< in: pointer to the data */ |
| 133 |
{ |
| 134 |
ha_node_t *node; |
| 135 |
|
| 136 |
hash_assert_can_modify(table, hash_value); |
| 137 |
ut_ad(btr_search_enabled); |
| 138 |
|
| 139 |
node = ha_search_with_data(table, hash_value, data); |
| 140 |
|
| 141 |
if (node) { |
| 142 |
ha_delete_hash_node(table, node); |
| 143 |
|
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
return false; |
| 148 |
} |