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., 59 Temple
15
Place, Suite 330, Boston, MA 02111-1307 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 */
45
/********************************************************************//**
46
Disable the adaptive hash search system and empty the index. */
49
btr_search_disable(void);
50
/*====================*/
51
/********************************************************************//**
52
Enable the adaptive hash search system. */
55
btr_search_enable(void);
56
/*====================*/
58
/********************************************************************//**
59
Returns search info for an index.
60
@return search info; search mutex reserved */
65
dict_index_t* index); /*!< in: index */
66
/*****************************************************************//**
67
Creates and initializes a search info struct.
68
@return own: search info struct */
71
btr_search_info_create(
72
/*===================*/
73
mem_heap_t* heap); /*!< in: heap where created */
74
/*****************************************************************//**
75
Returns the value of ref_count. The value is protected by
77
@return ref_count value. */
80
btr_search_info_get_ref_count(
81
/*==========================*/
82
btr_search_t* info); /*!< in: search info. */
83
/*********************************************************************//**
84
Updates the search info. */
87
btr_search_info_update(
88
/*===================*/
89
dict_index_t* index, /*!< in: index of the cursor */
90
btr_cur_t* cursor);/*!< in: cursor which was just positioned */
91
/******************************************************************//**
92
Tries to guess the right search position based on the hash search info
93
of the index. Note that if mode is PAGE_CUR_LE, which is used in inserts,
94
and the function returns TRUE, then cursor->up_match and cursor->low_match
95
both have sensible values.
96
@return TRUE if succeeded */
99
btr_search_guess_on_hash(
100
/*=====================*/
101
dict_index_t* index, /*!< in: index */
102
btr_search_t* info, /*!< in: index search info */
103
const dtuple_t* tuple, /*!< in: logical record */
104
ulint mode, /*!< in: PAGE_CUR_L, ... */
105
ulint latch_mode, /*!< in: BTR_SEARCH_LEAF, ... */
106
btr_cur_t* cursor, /*!< out: tree cursor */
107
ulint has_search_latch,/*!< in: latch mode the caller
108
currently has on btr_search_latch:
109
RW_S_LATCH, RW_X_LATCH, or 0 */
110
mtr_t* mtr); /*!< in: mtr */
111
/********************************************************************//**
112
Moves or deletes hash entries for moved records. If new_page is already hashed,
113
then the hash index for page, if any, is dropped. If new_page is not hashed,
114
and page is hashed, then a new hash index is built to new_page with the same
115
parameters as page (this often happens when a page is split). */
118
btr_search_move_or_delete_hash_entries(
119
/*===================================*/
120
buf_block_t* new_block, /*!< in: records are copied
122
buf_block_t* block, /*!< in: index page from which
123
records were copied, and the
124
copied records will be deleted
126
dict_index_t* index); /*!< in: record descriptor */
127
/********************************************************************//**
128
Drops a page hash index. */
131
btr_search_drop_page_hash_index(
132
/*============================*/
133
buf_block_t* block); /*!< in: block containing index page,
134
s- or x-latched, or an index page
135
for which we know that
136
block->buf_fix_count == 0 */
137
/********************************************************************//**
138
Drops a page hash index when a page is freed from a fseg to the file system.
139
Drops possible hash index if the page happens to be in the buffer pool. */
142
btr_search_drop_page_hash_when_freed(
143
/*=================================*/
144
ulint space, /*!< in: space id */
145
ulint zip_size, /*!< in: compressed page size in bytes
146
or 0 for uncompressed pages */
147
ulint page_no); /*!< in: page number */
148
/********************************************************************//**
149
Updates the page hash index when a single record is inserted on a page. */
152
btr_search_update_hash_node_on_insert(
153
/*==================================*/
154
btr_cur_t* cursor);/*!< in: cursor which was positioned to the
155
place to insert using btr_cur_search_...,
156
and the new record has been inserted next
158
/********************************************************************//**
159
Updates the page hash index when a single record is inserted on a page. */
162
btr_search_update_hash_on_insert(
163
/*=============================*/
164
btr_cur_t* cursor);/*!< in: cursor which was positioned to the
165
place to insert using btr_cur_search_...,
166
and the new record has been inserted next
168
/********************************************************************//**
169
Updates the page hash index when a single record is deleted from a page. */
172
btr_search_update_hash_on_delete(
173
/*=============================*/
174
btr_cur_t* cursor);/*!< in: cursor which was positioned on the
175
record to delete using btr_cur_search_...,
176
the record is not yet deleted */
177
/********************************************************************//**
178
Validates the search system.
179
@return TRUE if ok */
182
btr_search_validate(void);
183
/*======================*/
185
/** Flag: has the search system been enabled?
186
Protected by btr_search_latch and btr_search_enabled_mutex. */
187
extern bool btr_search_enabled;
189
/** The search info struct in an index */
190
struct btr_search_struct{
191
ulint ref_count; /*!< Number of blocks in this index tree
192
that have search index built
193
i.e. block->index points to this index.
194
Protected by btr_search_latch except
195
when during initialization in
196
btr_search_info_create(). */
198
/* @{ The following fields are not protected by any latch.
199
Unfortunately, this means that they must be aligned to
200
the machine word, i.e., they cannot be turned into bit-fields. */
201
buf_block_t* root_guess;/*!< the root page frame when it was last time
203
ulint hash_analysis; /*!< when this exceeds
204
BTR_SEARCH_HASH_ANALYSIS, the hash
205
analysis starts; this is reset if no
207
ibool last_hash_succ; /*!< TRUE if the last search would have
208
succeeded, or did succeed, using the hash
209
index; NOTE that the value here is not exact:
210
it is not calculated for every search, and the
211
calculation itself is not always accurate! */
212
ulint n_hash_potential;
213
/*!< number of consecutive searches
214
which would have succeeded, or did succeed,
215
using the hash index;
216
the range is 0 .. BTR_SEARCH_BUILD_LIMIT + 5 */
218
/*---------------------- @{ */
219
ulint n_fields; /*!< recommended prefix length for hash search:
220
number of full fields */
221
ulint n_bytes; /*!< recommended prefix: number of bytes in
223
@see BTR_PAGE_MAX_REC_SIZE */
224
ibool left_side; /*!< TRUE or FALSE, depending on whether
225
the leftmost record of several records with
226
the same prefix should be indexed in the
228
/*---------------------- @} */
229
#ifdef UNIV_SEARCH_PERF_STAT
230
ulint n_hash_succ; /*!< number of successful hash searches thus
232
ulint n_hash_fail; /*!< number of failed hash searches */
233
ulint n_patt_succ; /*!< number of successful pattern searches thus
235
ulint n_searches; /*!< number of searches */
236
#endif /* UNIV_SEARCH_PERF_STAT */
238
ulint magic_n; /*!< magic number @see BTR_SEARCH_MAGIC_N */
239
/** value of btr_search_struct::magic_n, used in assertions */
240
# define BTR_SEARCH_MAGIC_N 1112765
241
#endif /* UNIV_DEBUG */
244
/** The hash index system */
245
typedef struct btr_search_sys_struct btr_search_sys_t;
247
/** The hash index system */
248
struct btr_search_sys_struct{
249
hash_table_t* hash_index; /*!< the adaptive hash index,
250
mapping dtuple_fold values
251
to rec_t pointers on index pages */
254
/** The adaptive hash index */
255
extern btr_search_sys_t* btr_search_sys;
257
/** @brief The latch protecting the adaptive search system
259
This latch protects the
261
(2) columns of a record to which we have a pointer in the hash index;
263
but does NOT protect:
265
(3) next record offset field in a record;
266
(4) next or previous records on the same page.
268
Bear in mind (3) and (4) when using the hash index.
270
extern rw_lock_t* btr_search_latch_temp;
272
/** The latch protecting the adaptive search system */
273
#define btr_search_latch (*btr_search_latch_temp)
275
#ifdef UNIV_SEARCH_PERF_STAT
276
/** Number of successful adaptive hash index lookups */
277
extern ulint btr_search_n_succ;
278
/** Number of failed adaptive hash index lookups */
279
extern ulint btr_search_n_hash_fail;
280
#endif /* UNIV_SEARCH_PERF_STAT */
282
/** After change in n_fields or n_bytes in info, this many rounds are waited
283
before starting the hash analysis again: this is to save CPU time when there
284
is no hope in building a hash index. */
285
#define BTR_SEARCH_HASH_ANALYSIS 17
287
/** Limit of consecutive searches for trying a search shortcut on the search
289
#define BTR_SEARCH_ON_PATTERN_LIMIT 3
291
/** Limit of consecutive searches for trying a search shortcut using
293
#define BTR_SEARCH_ON_HASH_LIMIT 3
295
/** We do this many searches before trying to keep the search latch
296
over calls from MySQL. If we notice someone waiting for the latch, we
297
again set this much timeout. This is to reduce contention. */
298
#define BTR_SEA_TIMEOUT 10000
301
#include "btr0sea.ic"