summaryrefslogtreecommitdiff
path: root/innobase/include/ibuf0ibuf.ic
blob: 68f7ce9c1d09bb5b15a35b8051c52452433715cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/******************************************************
Insert buffer

(c) 1997 Innobase Oy

Created 7/19/1997 Heikki Tuuri
*******************************************************/

#include "buf0lru.h"
#include "page0page.h"

extern ulint	ibuf_flush_count;

/* If this number is n, an index page must contain at least the page size
per n bytes of free space for ibuf to try to buffer inserts to this page.
If there is this much of free space, the corresponding bits are set in the
ibuf bitmap. */
#define IBUF_PAGE_SIZE_PER_FREE_SPACE	32

/* Insert buffer data struct for a single tablespace */
struct ibuf_data_struct{
	ulint		space;	/* space id */
	ulint		seg_size;/* allocated pages if the file segment
				containing ibuf header and tree */
	ulint		size;	/* size of the insert buffer tree in pages */
	ibool		empty;	/* after an insert to the ibuf tree is
				performed, this is set to FALSE, and if a
				contract operation finds the tree empty, this
				is set to TRUE */
	ulint		free_list_len;
				/* length of the free list */
	ulint		height;	/* tree height */
	dict_index_t*	index;	/* insert buffer index */
	UT_LIST_NODE_T(ibuf_data_t) data_list;
				/* list of ibuf data structs */
	ulint		n_inserts;/* number of inserts made to the insert
				buffer */
	ulint		n_merges;/* number of pages merged */
	ulint		n_merged_recs;/* number of records merged */
};

/* If the ibuf meter exceeds this value, then the suitable inserts are made to
the insert buffer instead of directly to the disk page */
#define IBUF_THRESHOLD	50	

struct ibuf_struct{
	ulint		size;		/* current size of the ibuf index
					trees in pages */
	ulint		max_size;	/* recommended maximum size in pages
					for the ibuf index tree */
	ulint		meter;		/* heuristic meter which measures
					desirability of doing inserts to the
					insert buffer instead of directly to
					the disk page */
	UT_LIST_BASE_NODE_T(ibuf_data_t) data_list;
					/* list of ibuf data structs for
					each tablespace */
};

/****************************************************************************
Sets the free bit of the page in the ibuf bitmap. This is done in a separate
mini-transaction, hence this operation does not restrict further work to only
ibuf bitmap operations, which would result if the latch to the bitmap page
were kept. */

void
ibuf_set_free_bits(
/*===============*/
	ulint	type,	/* in: index type */
	page_t*	page,	/* in: index page; free bit is reset if the index is
			a non-clustered non-unique, and page level is 0 */
	ulint	val,	/* in: value to set: < 4 */
	ulint	max_val);/* in: ULINT_UNDEFINED or a maximum value which
			the bits must have before setting; this is for
			debugging */

/**************************************************************************
A basic partial test if an insert to the insert buffer could be possible and
recommended. */
UNIV_INLINE
ibool
ibuf_should_try(
/*============*/
	dict_index_t*	index,			/* in: index where to insert */
	ulint		ignore_sec_unique)	/* in: if != 0, we should
						ignore UNIQUE constraint on
						a secondary index when we
						decide */
{
	if (!(index->type & DICT_CLUSTERED)
	    && (ignore_sec_unique || !(index->type & DICT_UNIQUE))
	    && ibuf->meter > IBUF_THRESHOLD) {

		ibuf_flush_count++;

		if (ibuf_flush_count % 8 == 0) {
	    
			buf_LRU_try_free_flushed_blocks();
		}

		return(TRUE);
	}

	return(FALSE);
}

/***************************************************************************
Checks if a page address is an ibuf bitmap page address. */
UNIV_INLINE
ibool
ibuf_bitmap_page(
/*=============*/
			/* out: TRUE if a bitmap page */
	ulint	page_no)/* in: page number */
{
	if (page_no % XDES_DESCRIBED_PER_PAGE == FSP_IBUF_BITMAP_OFFSET) {

		return(TRUE);
	}

	return(FALSE);
}

/*************************************************************************
Translates the free space on a page to a value in the ibuf bitmap.*/
UNIV_INLINE
ulint
ibuf_index_page_calc_free_bits(
/*===========================*/
				/* out: value for ibuf bitmap bits */
	ulint	max_ins_size)	/* in: maximum insert size after reorganize
				for the page */
{
	ulint	n;
	
	n = max_ins_size / (UNIV_PAGE_SIZE / IBUF_PAGE_SIZE_PER_FREE_SPACE);

	if (n == 3) {
		n = 2;
	}
	
	if (n > 3) {
		n = 3;
	}

	return(n);
}

/*************************************************************************
Translates the ibuf free bits to the free space on a page in bytes. */
UNIV_INLINE
ulint
ibuf_index_page_calc_free_from_bits(
/*================================*/
			/* out: maximum insert size after reorganize for the
			page */
	ulint	bits)	/* in: value for ibuf bitmap bits */
{
	ut_ad(bits < 4);

	if (bits == 3) {
		return(4 * UNIV_PAGE_SIZE / IBUF_PAGE_SIZE_PER_FREE_SPACE);
	}

	return(bits * UNIV_PAGE_SIZE / IBUF_PAGE_SIZE_PER_FREE_SPACE);
}

/*************************************************************************
Translates the free space on a page to a value in the ibuf bitmap.*/
UNIV_INLINE
ulint
ibuf_index_page_calc_free(
/*======================*/
			/* out: value for ibuf bitmap bits */
	page_t*	page)	/* in: non-unique secondary index page */
{
	return(ibuf_index_page_calc_free_bits(
		page_get_max_insert_size_after_reorganize(page, 1)));
}

/****************************************************************************
Updates the free bits of the page in the ibuf bitmap if there is not enough
free on the page any more. This is done in a separate mini-transaction, hence
this operation does not restrict further work to only ibuf bitmap operations,
which would result if the latch to the bitmap page were kept. */
UNIV_INLINE
void
ibuf_update_free_bits_if_full(
/*==========================*/
	dict_index_t*	index,	/* in: index */
	page_t*		page,	/* in: index page to which we have added new
				records; the free bits are updated if the
				index is non-clustered and non-unique and
				the page level is 0, and the page becomes
				fuller */
	ulint		max_ins_size,/* in: value of maximum insert size with
				reorganize before the latest operation
				performed to the page */
	ulint		increase)/* in: upper limit for the additional space
				used in the latest operation, if known, or
				ULINT_UNDEFINED */
{
	ulint	before;
	ulint	after;

	before = ibuf_index_page_calc_free_bits(max_ins_size);

	if (max_ins_size >= increase) {
		ut_ad(ULINT_UNDEFINED > UNIV_PAGE_SIZE);
		
		after = ibuf_index_page_calc_free_bits(max_ins_size
								- increase);
#ifdef UNIV_IBUF_DEBUG
		ut_a(after <= ibuf_index_page_calc_free(page));
#endif
	} else {
		after = ibuf_index_page_calc_free(page);
	}

	if (after == 0) {
		/* We move the page to the front of the buffer pool LRU list:
		the purpose of this is to prevent those pages to which we
		cannot make inserts using the insert buffer from slipping
		out of the buffer pool */

		buf_page_make_young(page);
	}

	if (before > after) {
		ibuf_set_free_bits(index->type, page, after, before);
	}
}