| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1996, 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/btr0sea.ic |
| 29 |
The index tree adaptive search |
| 30 |
|
| 31 |
Created 2/17/1996 Heikki Tuuri |
| 32 |
*************************************************************************/ |
| 33 |
|
| 34 |
#include "btr0cur.h" |
| 35 |
#include "buf0buf.h" |
| 36 |
#include "dict0mem.h" |
| 37 |
#include "sync0rw.h" |
| 38 |
|
| 39 |
/** Updates the search info statistics following a search in B-tree that was |
| 40 |
performed not using or not finding row with the AHI index. It may decide to try |
| 41 |
to update the searched record on which the supplied cursor in positioned at, or |
| 42 |
add the whole page to AHI. |
| 43 |
@param[in] cursor cursor which was just positioned */ |
| 44 |
void btr_search_info_update_slow(btr_cur_t *cursor); |
| 45 |
|
| 46 |
static inline void btr_search_info_update(btr_cur_t *cursor) { |
| 47 |
const auto index = cursor->index; |
| 48 |
ut_ad(!rw_lock_own(btr_get_search_latch(index), RW_LOCK_S)); |
| 49 |
ut_ad(!rw_lock_own(btr_get_search_latch(index), RW_LOCK_X)); |
| 50 |
|
| 51 |
if (dict_index_is_spatial(index) || !btr_search_enabled) { |
| 52 |
return; |
| 53 |
} |
| 54 |
if (cursor->flag == BTR_CUR_HASH_NOT_ATTEMPTED) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
const auto hash_analysis_value = ++index->search_info->hash_analysis; |
| 59 |
|
| 60 |
if (hash_analysis_value < BTR_SEARCH_HASH_ANALYSIS) { |
| 61 |
/* Do nothing */ |
| 62 |
|
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
ut_ad(cursor->flag != BTR_CUR_HASH); |
| 67 |
|
| 68 |
btr_search_info_update_slow(cursor); |
| 69 |
} |
| 70 |
|
| 71 |
static inline void btr_search_x_lock(const dict_index_t *index, |
| 72 |
ut::Location location) { |
| 73 |
rw_lock_x_lock_gen(btr_get_search_latch(index), 0, location); |
| 74 |
} |
| 75 |
|
| 76 |
static inline bool btr_search_x_lock_nowait(const dict_index_t *index, |
| 77 |
ut::Location location) { |
| 78 |
return rw_lock_x_lock_nowait(btr_get_search_latch(index), location); |
| 79 |
} |
| 80 |
|
| 81 |
static inline void btr_search_x_unlock(const dict_index_t *index) { |
| 82 |
rw_lock_x_unlock(btr_get_search_latch(index)); |
| 83 |
} |
| 84 |
|
| 85 |
static inline void btr_search_x_lock_all(ut::Location location) { |
| 86 |
for (ulint i = 0; i < btr_ahi_parts; ++i) { |
| 87 |
rw_lock_x_lock_gen(&btr_search_sys->parts[i].latch, 0, location); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
static inline void btr_search_x_unlock_all() { |
| 92 |
for (ulint i = 0; i < btr_ahi_parts; ++i) { |
| 93 |
rw_lock_x_unlock(&btr_search_sys->parts[i].latch); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
static inline void btr_search_s_lock(const dict_index_t *index, |
| 98 |
ut::Location location) { |
| 99 |
rw_lock_s_lock_gen(btr_get_search_latch(index), 0, location); |
| 100 |
} |
| 101 |
|
| 102 |
static inline bool btr_search_s_lock_nowait(const dict_index_t *index, |
| 103 |
ut::Location location) { |
| 104 |
return rw_lock_s_lock_nowait(btr_get_search_latch(index), location); |
| 105 |
} |
| 106 |
|
| 107 |
static inline void btr_search_s_unlock(const dict_index_t *index) { |
| 108 |
rw_lock_s_unlock(btr_get_search_latch(index)); |
| 109 |
} |
| 110 |
|
| 111 |
static inline void btr_search_s_lock_all(ut::Location location) { |
| 112 |
for (ulint i = 0; i < btr_ahi_parts; ++i) { |
| 113 |
rw_lock_s_lock_gen(&btr_search_sys->parts[i].latch, 0, location); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
static inline void btr_search_s_unlock_all() { |
| 118 |
for (ulint i = 0; i < btr_ahi_parts; ++i) { |
| 119 |
rw_lock_s_unlock(&btr_search_sys->parts[i].latch); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
#ifdef UNIV_DEBUG |
| 124 |
static inline bool btr_search_own_all(ulint mode) { |
| 125 |
for (ulint i = 0; i < btr_ahi_parts; ++i) { |
| 126 |
if (!rw_lock_own(&btr_search_sys->parts[i].latch, mode)) { |
| 127 |
return (false); |
| 128 |
} |
| 129 |
} |
| 130 |
return (true); |
| 131 |
} |
| 132 |
|
| 133 |
static inline bool btr_search_own_any(ulint mode) { |
| 134 |
for (ulint i = 0; i < btr_ahi_parts; ++i) { |
| 135 |
if (rw_lock_own(&btr_search_sys->parts[i].latch, mode)) { |
| 136 |
return (true); |
| 137 |
} |
| 138 |
} |
| 139 |
return (false); |
| 140 |
} |
| 141 |
#endif /* UNIV_DEBUG */ |
| 142 |
|
| 143 |
static inline size_t btr_search_hash_index_id(const dict_index_t *index) { |
| 144 |
return ut::hash_uint64_pair(index->space, index->id); |
| 145 |
} |
| 146 |
|
| 147 |
static inline btr_search_sys_t::search_part_t &btr_get_search_part( |
| 148 |
const dict_index_t *index) { |
| 149 |
ut_ad(index != nullptr); |
| 150 |
|
| 151 |
const auto index_slot = |
| 152 |
btr_search_hash_index_id(index) % btr_ahi_parts_fast_modulo; |
| 153 |
|
| 154 |
return btr_search_sys->parts[index_slot]; |
| 155 |
} |
| 156 |
|
| 157 |
static inline rw_lock_t *btr_get_search_latch(const dict_index_t *index) { |
| 158 |
return &btr_get_search_part(index).latch; |
| 159 |
} |