~drizzle-trunk/drizzle/development

641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
1
/*****************************************************************************
2
3
Copyright (c) 1996, 2009, Innobase Oy. All Rights Reserved.
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 as published by the Free Software
7
Foundation; version 2 of the License.
8
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.
12
13
You should have received a copy of the GNU General Public License along with
1802.10.2 by Monty Taylor
Update all of the copyright headers to include the correct address.
14
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15
St, Fifth Floor, Boston, MA 02110-1301 USA
641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
16
17
*****************************************************************************/
18
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
19
/********************************************************************//**
20
@file include/btr0sea.h
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
21
The index tree adaptive search
22
23
Created 2/17/1996 Heikki Tuuri
24
*************************************************************************/
25
26
#ifndef btr0sea_h
27
#define btr0sea_h
28
29
#include "univ.i"
30
31
#include "rem0rec.h"
32
#include "dict0dict.h"
33
#include "btr0types.h"
34
#include "mtr0mtr.h"
35
#include "ha0ha.h"
36
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
37
/*****************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
38
Creates and initializes the adaptive search system at a database start. */
39
UNIV_INTERN
40
void
41
btr_search_sys_create(
42
/*==================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
43
	ulint	hash_size);	/*!< in: hash index hash table size */
1819.5.106 by stewart at flamingspork
[patch 106/129] Merge patch for revision 1915 from InnoDB SVN:
44
/*****************************************************************//**
45
Frees the adaptive search system at a database shutdown. */
46
UNIV_INTERN
47
void
48
btr_search_sys_free(void);
49
/*=====================*/
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
50
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
51
/********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
52
Disable the adaptive hash search system and empty the index. */
53
UNIV_INTERN
54
void
55
btr_search_disable(void);
56
/*====================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
57
/********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
58
Enable the adaptive hash search system. */
59
UNIV_INTERN
60
void
61
btr_search_enable(void);
62
/*====================*/
63
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
64
/********************************************************************//**
65
Returns search info for an index.
66
@return	search info; search mutex reserved */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
67
UNIV_INLINE
68
btr_search_t*
69
btr_search_get_info(
70
/*================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
71
	dict_index_t*	index);	/*!< in: index */
72
/*****************************************************************//**
73
Creates and initializes a search info struct.
74
@return	own: search info struct */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
75
UNIV_INTERN
76
btr_search_t*
77
btr_search_info_create(
78
/*===================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
79
	mem_heap_t*	heap);	/*!< in: heap where created */
80
/*****************************************************************//**
641.2.1 by Monty Taylor
InnoDB Plugin 1.0.2
81
Returns the value of ref_count. The value is protected by
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
82
btr_search_latch.
83
@return	ref_count value. */
641.2.1 by Monty Taylor
InnoDB Plugin 1.0.2
84
UNIV_INTERN
85
ulint
86
btr_search_info_get_ref_count(
87
/*==========================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
88
	btr_search_t*   info);	/*!< in: search info. */
89
/*********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
90
Updates the search info. */
91
UNIV_INLINE
92
void
93
btr_search_info_update(
94
/*===================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
95
	dict_index_t*	index,	/*!< in: index of the cursor */
96
	btr_cur_t*	cursor);/*!< in: cursor which was just positioned */
97
/******************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
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
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
101
both have sensible values.
102
@return	TRUE if succeeded */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
103
UNIV_INTERN
104
ibool
105
btr_search_guess_on_hash(
106
/*=====================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
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
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
114
					currently has on btr_search_latch:
115
					RW_S_LATCH, RW_X_LATCH, or 0 */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
116
	mtr_t*		mtr);		/*!< in: mtr */
117
/********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
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). */
122
UNIV_INTERN
123
void
124
btr_search_move_or_delete_hash_entries(
125
/*===================================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
126
	buf_block_t*	new_block,	/*!< in: records are copied
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
127
					to this page */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
128
	buf_block_t*	block,		/*!< in: index page from which
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
129
					records were copied, and the
130
					copied records will be deleted
131
					from this page */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
132
	dict_index_t*	index);		/*!< in: record descriptor */
133
/********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
134
Drops a page hash index. */
135
UNIV_INTERN
136
void
137
btr_search_drop_page_hash_index(
138
/*============================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
139
	buf_block_t*	block);	/*!< in: block containing index page,
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
140
				s- or x-latched, or an index page
141
				for which we know that
142
				block->buf_fix_count == 0 */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
143
/********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
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. */
146
UNIV_INTERN
147
void
148
btr_search_drop_page_hash_when_freed(
149
/*=================================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
150
	ulint	space,		/*!< in: space id */
151
	ulint	zip_size,	/*!< in: compressed page size in bytes
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
152
				or 0 for uncompressed pages */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
153
	ulint	page_no);	/*!< in: page number */
154
/********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
155
Updates the page hash index when a single record is inserted on a page. */
156
UNIV_INTERN
157
void
158
btr_search_update_hash_node_on_insert(
159
/*==================================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
160
	btr_cur_t*	cursor);/*!< in: cursor which was positioned to the
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
161
				place to insert using btr_cur_search_...,
162
				and the new record has been inserted next
163
				to the cursor */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
164
/********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
165
Updates the page hash index when a single record is inserted on a page. */
166
UNIV_INTERN
167
void
168
btr_search_update_hash_on_insert(
169
/*=============================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
170
	btr_cur_t*	cursor);/*!< in: cursor which was positioned to the
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
171
				place to insert using btr_cur_search_...,
172
				and the new record has been inserted next
173
				to the cursor */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
174
/********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
175
Updates the page hash index when a single record is deleted from a page. */
176
UNIV_INTERN
177
void
178
btr_search_update_hash_on_delete(
179
/*=============================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
180
	btr_cur_t*	cursor);/*!< in: cursor which was positioned on the
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
181
				record to delete using btr_cur_search_...,
182
				the record is not yet deleted */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
183
/********************************************************************//**
184
Validates the search system.
185
@return	TRUE if ok */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
186
UNIV_INTERN
187
ibool
188
btr_search_validate(void);
189
/*======================*/
190
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
191
/** Flag: has the search system been enabled?
641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
192
Protected by btr_search_latch and btr_search_enabled_mutex. */
933.1.2 by Monty Taylor
Fixed merge weirdness errors.
193
extern bool btr_search_enabled;
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
194
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
195
/** The search info struct in an index */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
196
struct btr_search_struct{
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
197
	ulint	ref_count;	/*!< Number of blocks in this index tree
641.2.1 by Monty Taylor
InnoDB Plugin 1.0.2
198
				that have search index built
199
				i.e. block->index points to this index.
200
				Protected by btr_search_latch except
201
				when during initialization in
202
				btr_search_info_create(). */
203
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
204
	/* @{ The following fields are not protected by any latch.
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
205
	Unfortunately, this means that they must be aligned to
206
	the machine word, i.e., they cannot be turned into bit-fields. */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
207
	buf_block_t* root_guess;/*!< the root page frame when it was last time
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
208
				fetched, or NULL */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
209
	ulint	hash_analysis;	/*!< when this exceeds
210
				BTR_SEARCH_HASH_ANALYSIS, the hash
211
				analysis starts; this is reset if no
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
212
				success noticed */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
213
	ibool	last_hash_succ;	/*!< TRUE if the last search would have
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
214
				succeeded, or did succeed, using the hash
215
				index; NOTE that the value here is not exact:
216
				it is not calculated for every search, and the
217
				calculation itself is not always accurate! */
218
	ulint	n_hash_potential;
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
219
				/*!< number of consecutive searches
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
220
				which would have succeeded, or did succeed,
221
				using the hash index;
222
				the range is 0 .. BTR_SEARCH_BUILD_LIMIT + 5 */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
223
	/* @} */
224
	/*---------------------- @{ */
225
	ulint	n_fields;	/*!< recommended prefix length for hash search:
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
226
				number of full fields */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
227
	ulint	n_bytes;	/*!< recommended prefix: number of bytes in
228
				an incomplete field
229
				@see BTR_PAGE_MAX_REC_SIZE */
230
	ibool	left_side;	/*!< TRUE or FALSE, depending on whether
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
231
				the leftmost record of several records with
232
				the same prefix should be indexed in the
233
				hash index */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
234
	/*---------------------- @} */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
235
#ifdef UNIV_SEARCH_PERF_STAT
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
236
	ulint	n_hash_succ;	/*!< number of successful hash searches thus
237
				far */
238
	ulint	n_hash_fail;	/*!< number of failed hash searches */
239
	ulint	n_patt_succ;	/*!< number of successful pattern searches thus
240
				far */
241
	ulint	n_searches;	/*!< number of searches */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
242
#endif /* UNIV_SEARCH_PERF_STAT */
243
#ifdef UNIV_DEBUG
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
244
	ulint	magic_n;	/*!< magic number @see BTR_SEARCH_MAGIC_N */
245
/** value of btr_search_struct::magic_n, used in assertions */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
246
# define BTR_SEARCH_MAGIC_N	1112765
247
#endif /* UNIV_DEBUG */
248
};
249
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
250
/** The hash index system */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
251
typedef struct btr_search_sys_struct	btr_search_sys_t;
252
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
253
/** The hash index system */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
254
struct btr_search_sys_struct{
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
255
	hash_table_t*	hash_index;	/*!< the adaptive hash index,
256
					mapping dtuple_fold values
257
					to rec_t pointers on index pages */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
258
};
259
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
260
/** The adaptive hash index */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
261
extern btr_search_sys_t*	btr_search_sys;
262
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
263
/** @brief The latch protecting the adaptive search system
264
265
This latch protects the
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
266
(1) hash index;
267
(2) columns of a record to which we have a pointer in the hash index;
268
269
but does NOT protect:
270
271
(3) next record offset field in a record;
272
(4) next or previous records on the same page.
273
274
Bear in mind (3) and (4) when using the hash index.
275
*/
276
extern rw_lock_t*	btr_search_latch_temp;
277
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
278
/** The latch protecting the adaptive search system */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
279
#define btr_search_latch	(*btr_search_latch_temp)
280
281
#ifdef UNIV_SEARCH_PERF_STAT
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
282
/** Number of successful adaptive hash index lookups */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
283
extern ulint	btr_search_n_succ;
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
284
/** Number of failed adaptive hash index lookups */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
285
extern ulint	btr_search_n_hash_fail;
286
#endif /* UNIV_SEARCH_PERF_STAT */
287
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
288
/** After change in n_fields or n_bytes in info, this many rounds are waited
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
289
before starting the hash analysis again: this is to save CPU time when there
290
is no hope in building a hash index. */
291
#define BTR_SEARCH_HASH_ANALYSIS	17
292
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
293
/** Limit of consecutive searches for trying a search shortcut on the search
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
294
pattern */
295
#define BTR_SEARCH_ON_PATTERN_LIMIT	3
296
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
297
/** Limit of consecutive searches for trying a search shortcut using
298
the hash index */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
299
#define BTR_SEARCH_ON_HASH_LIMIT	3
300
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
301
/** We do this many searches before trying to keep the search latch
302
over calls from MySQL. If we notice someone waiting for the latch, we
303
again set this much timeout. This is to reduce contention. */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
304
#define BTR_SEA_TIMEOUT			10000
305
306
#ifndef UNIV_NONINL
307
#include "btr0sea.ic"
308
#endif
309
310
#endif