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
*************************************************************************/
33
#include "dict0dict.h"
34
#include "btr0types.h"
38
/*****************************************************************//**
39
Creates and initializes the adaptive search system at a database start. */
42
btr_search_sys_create(
43
/*==================*/
44
ulint hash_size); /*!< in: hash index hash table size */
45
/*****************************************************************//**
46
Frees the adaptive search system at a database shutdown. */
49
btr_search_sys_free(void);
50
/*=====================*/
52
/********************************************************************//**
53
Disable the adaptive hash search system and empty the index. */
56
btr_search_disable(void);
57
/*====================*/
58
/********************************************************************//**
59
Enable the adaptive hash search system. */
62
btr_search_enable(void);
63
/*====================*/
65
/********************************************************************//**
66
Returns search info for an index.
67
@return search info; search mutex reserved */
72
dict_index_t* index); /*!< in: index */
73
/*****************************************************************//**
74
Creates and initializes a search info struct.
75
@return own: search info struct */
78
btr_search_info_create(
79
/*===================*/
80
mem_heap_t* heap); /*!< in: heap where created */
81
/*****************************************************************//**
82
Returns the value of ref_count. The value is protected by
84
@return ref_count value. */
87
btr_search_info_get_ref_count(
88
/*==========================*/
89
btr_search_t* info); /*!< in: search info. */
90
/*********************************************************************//**
91
Updates the search info. */
94
btr_search_info_update(
95
/*===================*/
96
dict_index_t* index, /*!< in: index of the cursor */
97
btr_cur_t* cursor);/*!< in: cursor which was just positioned */
98
/******************************************************************//**
99
Tries to guess the right search position based on the hash search info
100
of the index. Note that if mode is PAGE_CUR_LE, which is used in inserts,
101
and the function returns TRUE, then cursor->up_match and cursor->low_match
102
both have sensible values.
103
@return TRUE if succeeded */
106
btr_search_guess_on_hash(
107
/*=====================*/
108
dict_index_t* index, /*!< in: index */
109
btr_search_t* info, /*!< in: index search info */
110
const dtuple_t* tuple, /*!< in: logical record */
111
ulint mode, /*!< in: PAGE_CUR_L, ... */
112
ulint latch_mode, /*!< in: BTR_SEARCH_LEAF, ... */
113
btr_cur_t* cursor, /*!< out: tree cursor */
114
ulint has_search_latch,/*!< in: latch mode the caller
115
currently has on btr_search_latch:
116
RW_S_LATCH, RW_X_LATCH, or 0 */
117
mtr_t* mtr); /*!< in: mtr */
118
/********************************************************************//**
119
Moves or deletes hash entries for moved records. If new_page is already hashed,
120
then the hash index for page, if any, is dropped. If new_page is not hashed,
121
and page is hashed, then a new hash index is built to new_page with the same
122
parameters as page (this often happens when a page is split). */
125
btr_search_move_or_delete_hash_entries(
126
/*===================================*/
127
buf_block_t* new_block, /*!< in: records are copied
129
buf_block_t* block, /*!< in: index page from which
130
records were copied, and the
131
copied records will be deleted
133
dict_index_t* index); /*!< in: record descriptor */
134
/********************************************************************//**
135
Drops a page hash index. */
138
btr_search_drop_page_hash_index(
139
/*============================*/
140
buf_block_t* block); /*!< in: block containing index page,
141
s- or x-latched, or an index page
142
for which we know that
143
block->buf_fix_count == 0 */
144
/********************************************************************//**
145
Drops a page hash index when a page is freed from a fseg to the file system.
146
Drops possible hash index if the page happens to be in the buffer pool. */
149
btr_search_drop_page_hash_when_freed(
150
/*=================================*/
151
ulint space, /*!< in: space id */
152
ulint zip_size, /*!< in: compressed page size in bytes
153
or 0 for uncompressed pages */
154
ulint page_no); /*!< in: page number */
155
/********************************************************************//**
156
Updates the page hash index when a single record is inserted on a page. */
159
btr_search_update_hash_node_on_insert(
160
/*==================================*/
161
btr_cur_t* cursor);/*!< in: cursor which was positioned to the
162
place to insert using btr_cur_search_...,
163
and the new record has been inserted next
165
/********************************************************************//**
166
Updates the page hash index when a single record is inserted on a page. */
169
btr_search_update_hash_on_insert(
170
/*=============================*/
171
btr_cur_t* cursor);/*!< in: cursor which was positioned to the
172
place to insert using btr_cur_search_...,
173
and the new record has been inserted next
175
/********************************************************************//**
176
Updates the page hash index when a single record is deleted from a page. */
179
btr_search_update_hash_on_delete(
180
/*=============================*/
181
btr_cur_t* cursor);/*!< in: cursor which was positioned on the
182
record to delete using btr_cur_search_...,
183
the record is not yet deleted */
184
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
185
/********************************************************************//**
186
Validates the search system.
187
@return TRUE if ok */
190
btr_search_validate(void);
191
/*======================*/
193
# define btr_search_validate() TRUE
194
#endif /* defined UNIV_AHI_DEBUG || defined UNIV_DEBUG */
196
/** Flag: has the search system been enabled?
197
Protected by btr_search_latch and btr_search_enabled_mutex. */
198
extern bool btr_search_enabled;
200
/** Flag: whether the search system has completed its disabling process,
201
It is set to TRUE right after buf_pool_drop_hash_index() in
202
btr_search_disable(), indicating hash index entries are cleaned up.
203
Protected by btr_search_latch and btr_search_enabled_mutex. */
204
extern ibool btr_search_fully_disabled;
206
/** The search info struct in an index */
207
struct btr_search_struct{
208
ulint ref_count; /*!< Number of blocks in this index tree
209
that have search index built
210
i.e. block->index points to this index.
211
Protected by btr_search_latch except
212
when during initialization in
213
btr_search_info_create(). */
215
/* @{ The following fields are not protected by any latch.
216
Unfortunately, this means that they must be aligned to
217
the machine word, i.e., they cannot be turned into bit-fields. */
218
buf_block_t* root_guess;/*!< the root page frame when it was last time
220
ulint hash_analysis; /*!< when this exceeds
221
BTR_SEARCH_HASH_ANALYSIS, the hash
222
analysis starts; this is reset if no
224
ibool last_hash_succ; /*!< TRUE if the last search would have
225
succeeded, or did succeed, using the hash
226
index; NOTE that the value here is not exact:
227
it is not calculated for every search, and the
228
calculation itself is not always accurate! */
229
ulint n_hash_potential;
230
/*!< number of consecutive searches
231
which would have succeeded, or did succeed,
232
using the hash index;
233
the range is 0 .. BTR_SEARCH_BUILD_LIMIT + 5 */
235
/*---------------------- @{ */
236
ulint n_fields; /*!< recommended prefix length for hash search:
237
number of full fields */
238
ulint n_bytes; /*!< recommended prefix: number of bytes in
240
@see BTR_PAGE_MAX_REC_SIZE */
241
ibool left_side; /*!< TRUE or FALSE, depending on whether
242
the leftmost record of several records with
243
the same prefix should be indexed in the
245
/*---------------------- @} */
246
#ifdef UNIV_SEARCH_PERF_STAT
247
ulint n_hash_succ; /*!< number of successful hash searches thus
249
ulint n_hash_fail; /*!< number of failed hash searches */
250
ulint n_patt_succ; /*!< number of successful pattern searches thus
252
ulint n_searches; /*!< number of searches */
253
#endif /* UNIV_SEARCH_PERF_STAT */
255
ulint magic_n; /*!< magic number @see BTR_SEARCH_MAGIC_N */
256
/** value of btr_search_struct::magic_n, used in assertions */
257
# define BTR_SEARCH_MAGIC_N 1112765
258
#endif /* UNIV_DEBUG */
261
/** The hash index system */
262
typedef struct btr_search_sys_struct btr_search_sys_t;
264
/** The hash index system */
265
struct btr_search_sys_struct{
266
hash_table_t* hash_index; /*!< the adaptive hash index,
267
mapping dtuple_fold values
268
to rec_t pointers on index pages */
271
/** The adaptive hash index */
272
extern btr_search_sys_t* btr_search_sys;
274
/** @brief The latch protecting the adaptive search system
276
This latch protects the
278
(2) columns of a record to which we have a pointer in the hash index;
280
but does NOT protect:
282
(3) next record offset field in a record;
283
(4) next or previous records on the same page.
285
Bear in mind (3) and (4) when using the hash index.
287
extern rw_lock_t* btr_search_latch_temp;
289
/** The latch protecting the adaptive search system */
290
#define btr_search_latch (*btr_search_latch_temp)
292
#ifdef UNIV_SEARCH_PERF_STAT
293
/** Number of successful adaptive hash index lookups */
294
extern ulint btr_search_n_succ;
295
/** Number of failed adaptive hash index lookups */
296
extern ulint btr_search_n_hash_fail;
297
#endif /* UNIV_SEARCH_PERF_STAT */
299
/** After change in n_fields or n_bytes in info, this many rounds are waited
300
before starting the hash analysis again: this is to save CPU time when there
301
is no hope in building a hash index. */
302
#define BTR_SEARCH_HASH_ANALYSIS 17
304
/** Limit of consecutive searches for trying a search shortcut on the search
306
#define BTR_SEARCH_ON_PATTERN_LIMIT 3
308
/** Limit of consecutive searches for trying a search shortcut using
310
#define BTR_SEARCH_ON_HASH_LIMIT 3
312
/** We do this many searches before trying to keep the search latch
313
over calls from MySQL. If we notice someone waiting for the latch, we
314
again set this much timeout. This is to reduce contention. */
315
#define BTR_SEA_TIMEOUT 10000
318
#include "btr0sea.ic"