1
/*****************************************************************************
3
Copyright (C) 1997, 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/ibuf0ibuf.ic
23
Created 7/19/1997 Heikki Tuuri
24
*******************************************************/
26
#include "page0page.h"
28
#ifndef UNIV_HOTBACKUP
31
/** Counter for ibuf_should_try() */
32
extern ulint ibuf_flush_count;
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
40
/** Insert buffer struct */
42
ulint size; /*!< current size of the ibuf index
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
49
ibool empty; /*!< Protected by the page
50
latch of the root page of the
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 */
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 */
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
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 */
93
/**********************************************************************//**
94
A basic partial test if an insert to the insert buffer could be possible and
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
106
if (ibuf_use != IBUF_USE_NONE
107
&& !dict_index_is_clust(index)
108
&& (ignore_sec_unique || !dict_index_is_unique(index))) {
112
if (ibuf_flush_count % 4 == 0) {
114
buf_LRU_try_free_flushed_blocks(NULL);
123
/***********************************************************************//**
124
Checks if a page address is an ibuf bitmap page address.
125
@return TRUE if a bitmap page */
130
ulint zip_size,/*!< in: compressed page size in bytes;
131
0 for uncompressed pages */
132
ulint page_no)/*!< in: page number */
134
ut_ad(ut_is_2pow(zip_size));
137
return(UNIV_UNLIKELY((page_no & (UNIV_PAGE_SIZE - 1))
138
== FSP_IBUF_BITMAP_OFFSET));
141
return(UNIV_UNLIKELY((page_no & (zip_size - 1))
142
== FSP_IBUF_BITMAP_OFFSET));
145
/*********************************************************************//**
146
Translates the free space on a page to a value in the ibuf bitmap.
147
@return value for ibuf bitmap bits */
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
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);
164
/ (zip_size / IBUF_PAGE_SIZE_PER_FREE_SPACE);
167
/ (UNIV_PAGE_SIZE / IBUF_PAGE_SIZE_PER_FREE_SPACE);
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 */
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 */
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);
199
return(4 * zip_size / IBUF_PAGE_SIZE_PER_FREE_SPACE);
202
return(bits * zip_size / IBUF_PAGE_SIZE_PER_FREE_SPACE);
206
return(4 * UNIV_PAGE_SIZE / IBUF_PAGE_SIZE_PER_FREE_SPACE);
209
return(bits * (UNIV_PAGE_SIZE / IBUF_PAGE_SIZE_PER_FREE_SPACE));
212
/*********************************************************************//**
213
Translates the free space on a compressed page to a value in the ibuf bitmap.
214
@return value for ibuf bitmap bits */
217
ibuf_index_page_calc_free_zip(
218
/*==========================*/
220
/*!< in: compressed page size in bytes */
221
const buf_block_t* block) /*!< in: buffer block */
224
const page_zip_des_t* page_zip;
227
ut_ad(zip_size == buf_block_get_zip_size(block));
230
max_ins_size = page_get_max_insert_size_after_reorganize(
231
buf_block_get_frame(block), 1);
233
page_zip = buf_block_get_page_zip(block);
234
zip_max_ins = page_zip_max_ins_size(page_zip,
235
FALSE/* not clustered */);
237
if (UNIV_UNLIKELY(zip_max_ins < 0)) {
239
} else if (UNIV_LIKELY(max_ins_size > (ulint) zip_max_ins)) {
240
max_ins_size = (ulint) zip_max_ins;
243
return(ibuf_index_page_calc_free_bits(zip_size, max_ins_size));
246
/*********************************************************************//**
247
Translates the free space on a page to a value in the ibuf bitmap.
248
@return value for ibuf bitmap bits */
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 */
257
ut_ad(zip_size == buf_block_get_zip_size(block));
262
max_ins_size = page_get_max_insert_size_after_reorganize(
263
buf_block_get_frame(block), 1);
265
return(ibuf_index_page_calc_free_bits(0, max_ins_size));
267
return(ibuf_index_page_calc_free_zip(zip_size, block));
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
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
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
303
ut_ad(!buf_block_get_page_zip(block));
305
before = ibuf_index_page_calc_free_bits(0, max_ins_size);
307
if (max_ins_size >= increase) {
308
#if ULINT32_UNDEFINED <= UNIV_PAGE_SIZE
309
# error "ULINT32_UNDEFINED <= UNIV_PAGE_SIZE"
311
after = ibuf_index_page_calc_free_bits(0, max_ins_size
313
#ifdef UNIV_IBUF_DEBUG
314
ut_a(after <= ibuf_index_page_calc_free(0, block));
317
after = ibuf_index_page_calc_free(0, block);
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 */
326
buf_page_make_young(&block->page);
329
if (before > after) {
330
ibuf_set_free_bits(block, after, before);
333
#endif /* !UNIV_HOTBACKUP */