~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/innobase/include/btr0sea.h

  • Committer: Daniel Nichter
  • Date: 2011-10-23 16:01:37 UTC
  • mto: This revision was merged to the branch mainline in revision 2448.
  • Revision ID: daniel@percona.com-20111023160137-7ac3blgz8z4tf8za
Add Administration Getting Started and Logging.  Capitalize SQL clause keywords.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
14
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
 
15
St, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
*****************************************************************************/
 
18
 
 
19
/********************************************************************//**
 
20
@file include/btr0sea.h
 
21
The index tree adaptive search
 
22
 
 
23
Created 2/17/1996 Heikki Tuuri
 
24
*************************************************************************/
 
25
 
 
26
#pragma once
 
27
#ifndef btr0sea_h
 
28
#define btr0sea_h
 
29
 
 
30
#include "univ.i"
 
31
 
 
32
#include "rem0rec.h"
 
33
#include "dict0dict.h"
 
34
#include "btr0types.h"
 
35
#include "mtr0mtr.h"
 
36
#include "ha0ha.h"
 
37
 
 
38
/*****************************************************************//**
 
39
Creates and initializes the adaptive search system at a database start. */
 
40
UNIV_INTERN
 
41
void
 
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. */
 
47
UNIV_INTERN
 
48
void
 
49
btr_search_sys_free(void);
 
50
/*=====================*/
 
51
 
 
52
/********************************************************************//**
 
53
Disable the adaptive hash search system and empty the index. */
 
54
UNIV_INTERN
 
55
void
 
56
btr_search_disable(void);
 
57
/*====================*/
 
58
/********************************************************************//**
 
59
Enable the adaptive hash search system. */
 
60
UNIV_INTERN
 
61
void
 
62
btr_search_enable(void);
 
63
/*====================*/
 
64
 
 
65
/********************************************************************//**
 
66
Returns search info for an index.
 
67
@return search info; search mutex reserved */
 
68
UNIV_INLINE
 
69
btr_search_t*
 
70
btr_search_get_info(
 
71
/*================*/
 
72
        dict_index_t*   index); /*!< in: index */
 
73
/*****************************************************************//**
 
74
Creates and initializes a search info struct.
 
75
@return own: search info struct */
 
76
UNIV_INTERN
 
77
btr_search_t*
 
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
 
83
btr_search_latch.
 
84
@return ref_count value. */
 
85
UNIV_INTERN
 
86
ulint
 
87
btr_search_info_get_ref_count(
 
88
/*==========================*/
 
89
        btr_search_t*   info);  /*!< in: search info. */
 
90
/*********************************************************************//**
 
91
Updates the search info. */
 
92
UNIV_INLINE
 
93
void
 
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 */
 
104
UNIV_INTERN
 
105
ibool
 
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). */
 
123
UNIV_INTERN
 
124
void
 
125
btr_search_move_or_delete_hash_entries(
 
126
/*===================================*/
 
127
        buf_block_t*    new_block,      /*!< in: records are copied
 
128
                                        to this page */
 
129
        buf_block_t*    block,          /*!< in: index page from which
 
130
                                        records were copied, and the
 
131
                                        copied records will be deleted
 
132
                                        from this page */
 
133
        dict_index_t*   index);         /*!< in: record descriptor */
 
134
/********************************************************************//**
 
135
Drops a page hash index. */
 
136
UNIV_INTERN
 
137
void
 
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. */
 
147
UNIV_INTERN
 
148
void
 
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. */
 
157
UNIV_INTERN
 
158
void
 
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
 
164
                                to the cursor */
 
165
/********************************************************************//**
 
166
Updates the page hash index when a single record is inserted on a page. */
 
167
UNIV_INTERN
 
168
void
 
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
 
174
                                to the cursor */
 
175
/********************************************************************//**
 
176
Updates the page hash index when a single record is deleted from a page. */
 
177
UNIV_INTERN
 
178
void
 
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 */
 
188
UNIV_INTERN
 
189
ibool
 
190
btr_search_validate(void);
 
191
/*======================*/
 
192
#else
 
193
# define btr_search_validate()  TRUE
 
194
#endif /* defined UNIV_AHI_DEBUG || defined UNIV_DEBUG */
 
195
 
 
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;
 
199
 
 
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;
 
205
 
 
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(). */
 
214
 
 
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
 
219
                                fetched, or NULL */
 
220
        ulint   hash_analysis;  /*!< when this exceeds
 
221
                                BTR_SEARCH_HASH_ANALYSIS, the hash
 
222
                                analysis starts; this is reset if no
 
223
                                success noticed */
 
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 */
 
234
        /* @} */
 
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
 
239
                                an incomplete field
 
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
 
244
                                hash index */
 
245
        /*---------------------- @} */
 
246
#ifdef UNIV_SEARCH_PERF_STAT
 
247
        ulint   n_hash_succ;    /*!< number of successful hash searches thus
 
248
                                far */
 
249
        ulint   n_hash_fail;    /*!< number of failed hash searches */
 
250
        ulint   n_patt_succ;    /*!< number of successful pattern searches thus
 
251
                                far */
 
252
        ulint   n_searches;     /*!< number of searches */
 
253
#endif /* UNIV_SEARCH_PERF_STAT */
 
254
#ifdef UNIV_DEBUG
 
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 */
 
259
};
 
260
 
 
261
/** The hash index system */
 
262
typedef struct btr_search_sys_struct    btr_search_sys_t;
 
263
 
 
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 */
 
269
};
 
270
 
 
271
/** The adaptive hash index */
 
272
extern btr_search_sys_t*        btr_search_sys;
 
273
 
 
274
/** @brief The latch protecting the adaptive search system
 
275
 
 
276
This latch protects the
 
277
(1) hash index;
 
278
(2) columns of a record to which we have a pointer in the hash index;
 
279
 
 
280
but does NOT protect:
 
281
 
 
282
(3) next record offset field in a record;
 
283
(4) next or previous records on the same page.
 
284
 
 
285
Bear in mind (3) and (4) when using the hash index.
 
286
*/
 
287
extern rw_lock_t*       btr_search_latch_temp;
 
288
 
 
289
/** The latch protecting the adaptive search system */
 
290
#define btr_search_latch        (*btr_search_latch_temp)
 
291
 
 
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 */
 
298
 
 
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
 
303
 
 
304
/** Limit of consecutive searches for trying a search shortcut on the search
 
305
pattern */
 
306
#define BTR_SEARCH_ON_PATTERN_LIMIT     3
 
307
 
 
308
/** Limit of consecutive searches for trying a search shortcut using
 
309
the hash index */
 
310
#define BTR_SEARCH_ON_HASH_LIMIT        3
 
311
 
 
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
 
316
 
 
317
#ifndef UNIV_NONINL
 
318
#include "btr0sea.ic"
 
319
#endif
 
320
 
 
321
#endif