~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/innobase/include/ibuf0ibuf.ic

  • Committer: Monty Taylor
  • Date: 2008-10-30 19:42:06 UTC
  • mto: (520.4.38 devel)
  • mto: This revision was merged to the branch mainline in revision 572.
  • Revision ID: monty@inaugust.com-20081030194206-fzus6yqlw1ekru65
Removed handler from common_includes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************************
2
 
 
3
 
Copyright (C) 1997, 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/ibuf0ibuf.ic
21
 
Insert buffer
22
 
 
23
 
Created 7/19/1997 Heikki Tuuri
24
 
*******************************************************/
25
 
 
26
 
#include "page0page.h"
27
 
#include "page0zip.h"
28
 
#ifndef UNIV_HOTBACKUP
29
 
#include "buf0lru.h"
30
 
 
31
 
/** Counter for ibuf_should_try() */
32
 
extern ulint    ibuf_flush_count;
33
 
 
34
 
/** An index page must contain at least UNIV_PAGE_SIZE /
35
 
IBUF_PAGE_SIZE_PER_FREE_SPACE bytes of free space for ibuf to try to
36
 
buffer inserts to this page.  If there is this much of free space, the
37
 
corresponding bits are set in the ibuf bitmap. */
38
 
#define IBUF_PAGE_SIZE_PER_FREE_SPACE   32
39
 
 
40
 
/** Insert buffer struct */
41
 
struct ibuf_struct{
42
 
        ulint           size;           /*!< current size of the ibuf index
43
 
                                        tree, in pages */
44
 
        ulint           max_size;       /*!< recommended maximum size of the
45
 
                                        ibuf index tree, in pages */
46
 
        ulint           seg_size;       /*!< allocated pages of the file
47
 
                                        segment containing ibuf header and
48
 
                                        tree */
49
 
        ibool           empty;          /*!< Protected by the page
50
 
                                        latch of the root page of the
51
 
                                        insert buffer tree
52
 
                                        (FSP_IBUF_TREE_ROOT_PAGE_NO). TRUE
53
 
                                        if and only if the insert
54
 
                                        buffer tree is empty. */
55
 
        ulint           free_list_len;  /*!< length of the free list */
56
 
        ulint           height;         /*!< tree height */
57
 
        dict_index_t*   index;          /*!< insert buffer index */
58
 
 
59
 
        ulint           n_merges;       /*!< number of pages merged */
60
 
        ulint           n_merged_ops[IBUF_OP_COUNT];
61
 
                                        /*!< number of operations of each type
62
 
                                        merged to index pages */
63
 
        ulint           n_discarded_ops[IBUF_OP_COUNT];
64
 
                                        /*!< number of operations of each type
65
 
                                        discarded without merging due to the
66
 
                                        tablespace being deleted or the
67
 
                                        index being dropped */
68
 
};
69
 
 
70
 
/************************************************************************//**
71
 
Sets the free bit of the page in the ibuf bitmap. This is done in a separate
72
 
mini-transaction, hence this operation does not restrict further work to only
73
 
ibuf bitmap operations, which would result if the latch to the bitmap page
74
 
were kept. */
75
 
UNIV_INTERN
76
 
void
77
 
ibuf_set_free_bits_func(
78
 
/*====================*/
79
 
        buf_block_t*    block,  /*!< in: index page of a non-clustered index;
80
 
                                free bit is reset if page level is 0 */
81
 
#ifdef UNIV_IBUF_DEBUG
82
 
        ulint           max_val,/*!< in: ULINT_UNDEFINED or a maximum
83
 
                                value which the bits must have before
84
 
                                setting; this is for debugging */
85
 
#endif /* UNIV_IBUF_DEBUG */
86
 
        ulint           val);   /*!< in: value to set: < 4 */
87
 
#ifdef UNIV_IBUF_DEBUG
88
 
# define ibuf_set_free_bits(b,v,max) ibuf_set_free_bits_func(b,max,v)
89
 
#else /* UNIV_IBUF_DEBUG */
90
 
# define ibuf_set_free_bits(b,v,max) ibuf_set_free_bits_func(b,v)
91
 
#endif /* UNIV_IBUF_DEBUG */
92
 
 
93
 
/**********************************************************************//**
94
 
A basic partial test if an insert to the insert buffer could be possible and
95
 
recommended. */
96
 
UNIV_INLINE
97
 
ibool
98
 
ibuf_should_try(
99
 
/*============*/
100
 
        dict_index_t*   index,                  /*!< in: index where to insert */
101
 
        ulint           ignore_sec_unique)      /*!< in: if != 0, we should
102
 
                                                ignore UNIQUE constraint on
103
 
                                                a secondary index when we
104
 
                                                decide */
105
 
{
106
 
        if (ibuf_use != IBUF_USE_NONE
107
 
            && !dict_index_is_clust(index)
108
 
            && (ignore_sec_unique || !dict_index_is_unique(index))) {
109
 
 
110
 
                ibuf_flush_count++;
111
 
 
112
 
                if (ibuf_flush_count % 4 == 0) {
113
 
 
114
 
                        buf_LRU_try_free_flushed_blocks(NULL);
115
 
                }
116
 
 
117
 
                return(TRUE);
118
 
        }
119
 
 
120
 
        return(FALSE);
121
 
}
122
 
 
123
 
/***********************************************************************//**
124
 
Checks if a page address is an ibuf bitmap page address.
125
 
@return TRUE if a bitmap page */
126
 
UNIV_INLINE
127
 
ibool
128
 
ibuf_bitmap_page(
129
 
/*=============*/
130
 
        ulint   zip_size,/*!< in: compressed page size in bytes;
131
 
                        0 for uncompressed pages */
132
 
        ulint   page_no)/*!< in: page number */
133
 
{
134
 
        ut_ad(ut_is_2pow(zip_size));
135
 
 
136
 
        if (!zip_size) {
137
 
                return(UNIV_UNLIKELY((page_no & (UNIV_PAGE_SIZE - 1))
138
 
                                     == FSP_IBUF_BITMAP_OFFSET));
139
 
        }
140
 
 
141
 
        return(UNIV_UNLIKELY((page_no & (zip_size - 1))
142
 
                             == FSP_IBUF_BITMAP_OFFSET));
143
 
}
144
 
 
145
 
/*********************************************************************//**
146
 
Translates the free space on a page to a value in the ibuf bitmap.
147
 
@return value for ibuf bitmap bits */
148
 
UNIV_INLINE
149
 
ulint
150
 
ibuf_index_page_calc_free_bits(
151
 
/*===========================*/
152
 
        ulint   zip_size,       /*!< in: compressed page size in bytes;
153
 
                                0 for uncompressed pages */
154
 
        ulint   max_ins_size)   /*!< in: maximum insert size after reorganize
155
 
                                for the page */
156
 
{
157
 
        ulint   n;
158
 
        ut_ad(ut_is_2pow(zip_size));
159
 
        ut_ad(!zip_size || zip_size > IBUF_PAGE_SIZE_PER_FREE_SPACE);
160
 
        ut_ad(zip_size <= UNIV_PAGE_SIZE);
161
 
 
162
 
        if (zip_size) {
163
 
                n = max_ins_size
164
 
                        / (zip_size / IBUF_PAGE_SIZE_PER_FREE_SPACE);
165
 
        } else {
166
 
                n = max_ins_size
167
 
                        / (UNIV_PAGE_SIZE / IBUF_PAGE_SIZE_PER_FREE_SPACE);
168
 
        }
169
 
 
170
 
        if (n == 3) {
171
 
                n = 2;
172
 
        }
173
 
 
174
 
        if (n > 3) {
175
 
                n = 3;
176
 
        }
177
 
 
178
 
        return(n);
179
 
}
180
 
 
181
 
/*********************************************************************//**
182
 
Translates the ibuf free bits to the free space on a page in bytes.
183
 
@return maximum insert size after reorganize for the page */
184
 
UNIV_INLINE
185
 
ulint
186
 
ibuf_index_page_calc_free_from_bits(
187
 
/*================================*/
188
 
        ulint   zip_size,/*!< in: compressed page size in bytes;
189
 
                        0 for uncompressed pages */
190
 
        ulint   bits)   /*!< in: value for ibuf bitmap bits */
191
 
{
192
 
        ut_ad(bits < 4);
193
 
        ut_ad(ut_is_2pow(zip_size));
194
 
        ut_ad(!zip_size || zip_size > IBUF_PAGE_SIZE_PER_FREE_SPACE);
195
 
        ut_ad(zip_size <= UNIV_PAGE_SIZE);
196
 
 
197
 
        if (zip_size) {
198
 
                if (bits == 3) {
199
 
                        return(4 * zip_size / IBUF_PAGE_SIZE_PER_FREE_SPACE);
200
 
                }
201
 
 
202
 
                return(bits * zip_size / IBUF_PAGE_SIZE_PER_FREE_SPACE);
203
 
        }
204
 
 
205
 
        if (bits == 3) {
206
 
                return(4 * UNIV_PAGE_SIZE / IBUF_PAGE_SIZE_PER_FREE_SPACE);
207
 
        }
208
 
 
209
 
        return(bits * (UNIV_PAGE_SIZE / IBUF_PAGE_SIZE_PER_FREE_SPACE));
210
 
}
211
 
 
212
 
/*********************************************************************//**
213
 
Translates the free space on a compressed page to a value in the ibuf bitmap.
214
 
@return value for ibuf bitmap bits */
215
 
UNIV_INLINE
216
 
ulint
217
 
ibuf_index_page_calc_free_zip(
218
 
/*==========================*/
219
 
        ulint                   zip_size,
220
 
                                        /*!< in: compressed page size in bytes */
221
 
        const buf_block_t*      block)  /*!< in: buffer block */
222
 
{
223
 
        ulint                   max_ins_size;
224
 
        const page_zip_des_t*   page_zip;
225
 
        lint                    zip_max_ins;
226
 
 
227
 
        ut_ad(zip_size == buf_block_get_zip_size(block));
228
 
        ut_ad(zip_size);
229
 
 
230
 
        max_ins_size = page_get_max_insert_size_after_reorganize(
231
 
                buf_block_get_frame(block), 1);
232
 
 
233
 
        page_zip = buf_block_get_page_zip(block);
234
 
        zip_max_ins = page_zip_max_ins_size(page_zip,
235
 
                                            FALSE/* not clustered */);
236
 
 
237
 
        if (UNIV_UNLIKELY(zip_max_ins < 0)) {
238
 
                return(0);
239
 
        } else if (UNIV_LIKELY(max_ins_size > (ulint) zip_max_ins)) {
240
 
                max_ins_size = (ulint) zip_max_ins;
241
 
        }
242
 
 
243
 
        return(ibuf_index_page_calc_free_bits(zip_size, max_ins_size));
244
 
}
245
 
 
246
 
/*********************************************************************//**
247
 
Translates the free space on a page to a value in the ibuf bitmap.
248
 
@return value for ibuf bitmap bits */
249
 
UNIV_INLINE
250
 
ulint
251
 
ibuf_index_page_calc_free(
252
 
/*======================*/
253
 
        ulint                   zip_size,/*!< in: compressed page size in bytes;
254
 
                                        0 for uncompressed pages */
255
 
        const buf_block_t*      block)  /*!< in: buffer block */
256
 
{
257
 
        ut_ad(zip_size == buf_block_get_zip_size(block));
258
 
 
259
 
        if (!zip_size) {
260
 
                ulint   max_ins_size;
261
 
 
262
 
                max_ins_size = page_get_max_insert_size_after_reorganize(
263
 
                        buf_block_get_frame(block), 1);
264
 
 
265
 
                return(ibuf_index_page_calc_free_bits(0, max_ins_size));
266
 
        } else {
267
 
                return(ibuf_index_page_calc_free_zip(zip_size, block));
268
 
        }
269
 
}
270
 
 
271
 
/************************************************************************//**
272
 
Updates the free bits of an uncompressed page in the ibuf bitmap if
273
 
there is not enough free on the page any more.  This is done in a
274
 
separate mini-transaction, hence this operation does not restrict
275
 
further work to only ibuf bitmap operations, which would result if the
276
 
latch to the bitmap page were kept.  NOTE: The free bits in the insert
277
 
buffer bitmap must never exceed the free space on a page.  It is
278
 
unsafe to increment the bits in a separately committed
279
 
mini-transaction, because in crash recovery, the free bits could
280
 
momentarily be set too high.  It is only safe to use this function for
281
 
decrementing the free bits.  Should more free space become available,
282
 
we must not update the free bits here, because that would break crash
283
 
recovery. */
284
 
UNIV_INLINE
285
 
void
286
 
ibuf_update_free_bits_if_full(
287
 
/*==========================*/
288
 
        buf_block_t*    block,  /*!< in: index page to which we have added new
289
 
                                records; the free bits are updated if the
290
 
                                index is non-clustered and non-unique and
291
 
                                the page level is 0, and the page becomes
292
 
                                fuller */
293
 
        ulint           max_ins_size,/*!< in: value of maximum insert size with
294
 
                                reorganize before the latest operation
295
 
                                performed to the page */
296
 
        ulint           increase)/*!< in: upper limit for the additional space
297
 
                                used in the latest operation, if known, or
298
 
                                ULINT_UNDEFINED */
299
 
{
300
 
        ulint   before;
301
 
        ulint   after;
302
 
 
303
 
        ut_ad(!buf_block_get_page_zip(block));
304
 
 
305
 
        before = ibuf_index_page_calc_free_bits(0, max_ins_size);
306
 
 
307
 
        if (max_ins_size >= increase) {
308
 
#if ULINT32_UNDEFINED <= UNIV_PAGE_SIZE
309
 
# error "ULINT32_UNDEFINED <= UNIV_PAGE_SIZE"
310
 
#endif
311
 
                after = ibuf_index_page_calc_free_bits(0, max_ins_size
312
 
                                                       - increase);
313
 
#ifdef UNIV_IBUF_DEBUG
314
 
                ut_a(after <= ibuf_index_page_calc_free(0, block));
315
 
#endif
316
 
        } else {
317
 
                after = ibuf_index_page_calc_free(0, block);
318
 
        }
319
 
 
320
 
        if (after == 0) {
321
 
                /* We move the page to the front of the buffer pool LRU list:
322
 
                the purpose of this is to prevent those pages to which we
323
 
                cannot make inserts using the insert buffer from slipping
324
 
                out of the buffer pool */
325
 
 
326
 
                buf_page_make_young(&block->page);
327
 
        }
328
 
 
329
 
        if (before > after) {
330
 
                ibuf_set_free_bits(block, after, before);
331
 
        }
332
 
}
333
 
#endif /* !UNIV_HOTBACKUP */