1
/*****************************************************************************
3
Copyright (c) 1996, 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., 51 Franklin
15
St, Fifth Floor, Boston, MA 02110-1301 USA
17
*****************************************************************************/
19
/********************************************************************//**
20
@file include/btr0sea.h
21
The index tree adaptive search
23
Created 2/17/1996 Heikki Tuuri
24
*************************************************************************/
32
#include "dict0dict.h"
33
#include "btr0types.h"
37
/*****************************************************************//**
38
Creates and initializes the adaptive search system at a database start. */
41
btr_search_sys_create(
42
/*==================*/
43
ulint hash_size); /*!< in: hash index hash table size */
44
/*****************************************************************//**
45
Frees the adaptive search system at a database shutdown. */
48
btr_search_sys_free(void);
49
/*=====================*/
51
/********************************************************************//**
52
Disable the adaptive hash search system and empty the index. */
55
btr_search_disable(void);
56
/*====================*/
57
/********************************************************************//**
58
Enable the adaptive hash search system. */
61
btr_search_enable(void);
62
/*====================*/
64
/********************************************************************//**
65
Returns search info for an index.
66
@return search info; search mutex reserved */
71
dict_index_t* index); /*!< in: index */
72
/*****************************************************************//**
73
Creates and initializes a search info struct.
74
@return own: search info struct */
77
btr_search_info_create(
78
/*===================*/
79
mem_heap_t* heap); /*!< in: heap where created */
80
/*****************************************************************//**
81
Returns the value of ref_count. The value is protected by
83
@return ref_count value. */
86
btr_search_info_get_ref_count(
87
/*==========================*/
88
btr_search_t* info); /*!< in: search info. */
89
/*********************************************************************//**
90
Updates the search info. */
93
btr_search_info_update(
94
/*===================*/
95
dict_index_t* index, /*!< in: index of the cursor */
96
btr_cur_t* cursor);/*!< in: cursor which was just positioned */
97
/******************************************************************//**
98
Tries to guess the right search position based on the hash search info
99
of the index. Note that if mode is PAGE_CUR_LE, which is used in inserts,
100
and the function returns TRUE, then cursor->up_match and cursor->low_match
101
both have sensible values.
102
@return TRUE if succeeded */
105
btr_search_guess_on_hash(
106
/*=====================*/
107
dict_index_t* index, /*!< in: index */
108
btr_search_t* info, /*!< in: index search info */
109
const dtuple_t* tuple, /*!< in: logical record */
110
ulint mode, /*!< in: PAGE_CUR_L, ... */
111
ulint latch_mode, /*!< in: BTR_SEARCH_LEAF, ... */
112
btr_cur_t* cursor, /*!< out: tree cursor */
113
ulint has_search_latch,/*!< in: latch mode the caller
114
currently has on btr_search_latch:
115
RW_S_LATCH, RW_X_LATCH, or 0 */
116
mtr_t* mtr); /*!< in: mtr */
117
/********************************************************************//**
118
Moves or deletes hash entries for moved records. If new_page is already hashed,
119
then the hash index for page, if any, is dropped. If new_page is not hashed,
120
and page is hashed, then a new hash index is built to new_page with the same
121
parameters as page (this often happens when a page is split). */
124
btr_search_move_or_delete_hash_entries(
125
/*===================================*/
126
buf_block_t* new_block, /*!< in: records are copied
128
buf_block_t* block, /*!< in: index page from which
129
records were copied, and the
130
copied records will be deleted
132
dict_index_t* index); /*!< in: record descriptor */
133
/********************************************************************//**
134
Drops a page hash index. */
137
btr_search_drop_page_hash_index(
138
/*============================*/
139
buf_block_t* block); /*!< in: block containing index page,
140
s- or x-latched, or an index page
141
for which we know that
142
block->buf_fix_count == 0 */
143
/********************************************************************//**
144
Drops a page hash index when a page is freed from a fseg to the file system.
145
Drops possible hash index if the page happens to be in the buffer pool. */
148
btr_search_drop_page_hash_when_freed(
149
/*=================================*/
150
ulint space, /*!< in: space id */
151
ulint zip_size, /*!< in: compressed page size in bytes
152
or 0 for uncompressed pages */
153
ulint page_no); /*!< in: page number */
154
/********************************************************************//**
155
Updates the page hash index when a single record is inserted on a page. */
158
btr_search_update_hash_node_on_insert(
159
/*==================================*/
160
btr_cur_t* cursor);/*!< in: cursor which was positioned to the
161
place to insert using btr_cur_search_...,
162
and the new record has been inserted next
164
/********************************************************************//**
165
Updates the page hash index when a single record is inserted on a page. */
168
btr_search_update_hash_on_insert(
169
/*=============================*/
170
btr_cur_t* cursor);/*!< in: cursor which was positioned to the
171
place to insert using btr_cur_search_...,
172
and the new record has been inserted next
174
/********************************************************************//**
175
Updates the page hash index when a single record is deleted from a page. */
178
btr_search_update_hash_on_delete(
179
/*=============================*/
180
btr_cur_t* cursor);/*!< in: cursor which was positioned on the
181
record to delete using btr_cur_search_...,
182
the record is not yet deleted */
183
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
184
/********************************************************************//**
185
Validates the search system.
186
@return TRUE if ok */
189
btr_search_validate(void);
190
/*======================*/
192
# define btr_search_validate() TRUE
193
#endif /* defined UNIV_AHI_DEBUG || defined UNIV_DEBUG */
195
/** Flag: has the search system been enabled?
196
Protected by btr_search_latch and btr_search_enabled_mutex. */
197
extern bool btr_search_enabled;
199
/** Flag: whether the search system has completed its disabling process,
200
It is set to TRUE right after buf_pool_drop_hash_index() in
201
btr_search_disable(), indicating hash index entries are cleaned up.
202
Protected by btr_search_latch and btr_search_enabled_mutex. */
203
extern ibool btr_search_fully_disabled;
205
/** The search info struct in an index */
206
struct btr_search_struct{
207
ulint ref_count; /*!< Number of blocks in this index tree
208
that have search index built
209
i.e. block->index points to this index.
210
Protected by btr_search_latch except
211
when during initialization in
212
btr_search_info_create(). */
214
/* @{ The following fields are not protected by any latch.
215
Unfortunately, this means that they must be aligned to
216
the machine word, i.e., they cannot be turned into bit-fields. */
217
buf_block_t* root_guess;/*!< the root page frame when it was last time
219
ulint hash_analysis; /*!< when this exceeds
220
BTR_SEARCH_HASH_ANALYSIS, the hash
221
analysis starts; this is reset if no
223
ibool last_hash_succ; /*!< TRUE if the last search would have
224
succeeded, or did succeed, using the hash
225
index; NOTE that the value here is not exact:
226
it is not calculated for every search, and the
227
calculation itself is not always accurate! */
228
ulint n_hash_potential;
229
/*!< number of consecutive searches
230
which would have succeeded, or did succeed,
231
using the hash index;
232
the range is 0 .. BTR_SEARCH_BUILD_LIMIT + 5 */
234
/*---------------------- @{ */
235
ulint n_fields; /*!< recommended prefix length for hash search:
236
number of full fields */
237
ulint n_bytes; /*!< recommended prefix: number of bytes in
239
@see BTR_PAGE_MAX_REC_SIZE */
240
ibool left_side; /*!< TRUE or FALSE, depending on whether
241
the leftmost record of several records with
242
the same prefix should be indexed in the
244
/*---------------------- @} */
245
#ifdef UNIV_SEARCH_PERF_STAT
246
ulint n_hash_succ; /*!< number of successful hash searches thus
248
ulint n_hash_fail; /*!< number of failed hash searches */
249
ulint n_patt_succ; /*!< number of successful pattern searches thus
251
ulint n_searches; /*!< number of searches */
252
#endif /* UNIV_SEARCH_PERF_STAT */
254
ulint magic_n; /*!< magic number @see BTR_SEARCH_MAGIC_N */
255
/** value of btr_search_struct::magic_n, used in assertions */
256
# define BTR_SEARCH_MAGIC_N 1112765
257
#endif /* UNIV_DEBUG */
260
/** The hash index system */
261
typedef struct btr_search_sys_struct btr_search_sys_t;
263
/** The hash index system */
264
struct btr_search_sys_struct{
265
hash_table_t* hash_index; /*!< the adaptive hash index,
266
mapping dtuple_fold values
267
to rec_t pointers on index pages */
270
/** The adaptive hash index */
271
extern btr_search_sys_t* btr_search_sys;
273
/** @brief The latch protecting the adaptive search system
275
This latch protects the
277
(2) columns of a record to which we have a pointer in the hash index;
279
but does NOT protect:
281
(3) next record offset field in a record;
282
(4) next or previous records on the same page.
284
Bear in mind (3) and (4) when using the hash index.
286
extern rw_lock_t* btr_search_latch_temp;
288
/** The latch protecting the adaptive search system */
289
#define btr_search_latch (*btr_search_latch_temp)
291
#ifdef UNIV_SEARCH_PERF_STAT
292
/** Number of successful adaptive hash index lookups */
293
extern ulint btr_search_n_succ;
294
/** Number of failed adaptive hash index lookups */
295
extern ulint btr_search_n_hash_fail;
296
#endif /* UNIV_SEARCH_PERF_STAT */
298
/** After change in n_fields or n_bytes in info, this many rounds are waited
299
before starting the hash analysis again: this is to save CPU time when there
300
is no hope in building a hash index. */
301
#define BTR_SEARCH_HASH_ANALYSIS 17
303
/** Limit of consecutive searches for trying a search shortcut on the search
305
#define BTR_SEARCH_ON_PATTERN_LIMIT 3
307
/** Limit of consecutive searches for trying a search shortcut using
309
#define BTR_SEARCH_ON_HASH_LIMIT 3
311
/** We do this many searches before trying to keep the search latch
312
over calls from MySQL. If we notice someone waiting for the latch, we
313
again set this much timeout. This is to reduce contention. */
314
#define BTR_SEA_TIMEOUT 10000
317
#include "btr0sea.ic"