btr0cur.h 32.6 KB
Newer Older
1 2
/*****************************************************************************

3
Copyright (c) 1994, 2010, Innobase Oy. All Rights Reserved.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA

*****************************************************************************/

19 20
/**************************************************//**
@file include/btr0cur.h
osku's avatar
osku committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
The index tree cursor

Created 10/16/1994 Heikki Tuuri
*******************************************************/

#ifndef btr0cur_h
#define btr0cur_h

#include "univ.i"
#include "dict0dict.h"
#include "page0cur.h"
#include "btr0types.h"

/* Mode flags for btr_cur operations; these can be ORed */
#define BTR_NO_UNDO_LOG_FLAG	1	/* do no undo logging */
#define BTR_NO_LOCKING_FLAG	2	/* do no record lock checking */
#define BTR_KEEP_SYS_FLAG	4	/* sys fields will be found from the
					update vector or inserted entry */

40 41 42 43 44
#ifndef UNIV_HOTBACKUP
#include "que0types.h"
#include "row0types.h"
#include "ha0ha.h"

osku's avatar
osku committed
45 46 47
#define BTR_CUR_ADAPT
#define BTR_CUR_HASH_ADAPT

48
#ifdef UNIV_DEBUG
49
/*********************************************************//**
50 51
Returns the page cursor component of a tree cursor.
@return	pointer to page cursor component */
osku's avatar
osku committed
52 53 54 55
UNIV_INLINE
page_cur_t*
btr_cur_get_page_cur(
/*=================*/
56
	const btr_cur_t*	cursor);/*!< in: tree cursor */
57 58 59
#else /* UNIV_DEBUG */
# define btr_cur_get_page_cur(cursor) (&(cursor)->page_cur)
#endif /* UNIV_DEBUG */
60
/*********************************************************//**
61 62
Returns the buffer block on which the tree cursor is positioned.
@return	pointer to buffer block */
63 64 65 66
UNIV_INLINE
buf_block_t*
btr_cur_get_block(
/*==============*/
67
	btr_cur_t*	cursor);/*!< in: tree cursor */
68
/*********************************************************//**
69 70
Returns the record pointer of a tree cursor.
@return	pointer to record */
osku's avatar
osku committed
71 72 73 74
UNIV_INLINE
rec_t*
btr_cur_get_rec(
/*============*/
75
	btr_cur_t*	cursor);/*!< in: tree cursor */
76
/*********************************************************//**
77 78
Returns the compressed page on which the tree cursor is positioned.
@return	pointer to compressed page, or NULL if the page is not compressed */
79 80 81 82
UNIV_INLINE
page_zip_des_t*
btr_cur_get_page_zip(
/*=================*/
83
	btr_cur_t*	cursor);/*!< in: tree cursor */
84
/*********************************************************//**
osku's avatar
osku committed
85 86 87 88 89
Invalidates a tree cursor by setting record pointer to NULL. */
UNIV_INLINE
void
btr_cur_invalidate(
/*===============*/
90
	btr_cur_t*	cursor);/*!< in: tree cursor */
91
/*********************************************************//**
92 93
Returns the page of a tree cursor.
@return	pointer to page */
osku's avatar
osku committed
94 95 96 97
UNIV_INLINE
page_t*
btr_cur_get_page(
/*=============*/
98
	btr_cur_t*	cursor);/*!< in: tree cursor */
99
/*********************************************************//**
100 101
Returns the index of a cursor.
@return	index */
osku's avatar
osku committed
102
UNIV_INLINE
103 104 105
dict_index_t*
btr_cur_get_index(
/*==============*/
106
	btr_cur_t*	cursor);/*!< in: B-tree cursor */
107
/*********************************************************//**
osku's avatar
osku committed
108 109 110 111 112
Positions a tree cursor at a given record. */
UNIV_INLINE
void
btr_cur_position(
/*=============*/
113 114 115 116
	dict_index_t*	index,	/*!< in: index */
	rec_t*		rec,	/*!< in: record in tree */
	buf_block_t*	block,	/*!< in: buffer block of rec */
	btr_cur_t*	cursor);/*!< in: cursor */
117
/********************************************************************//**
osku's avatar
osku committed
118 119 120 121 122 123
Searches an index tree and positions a tree cursor on a given level.
NOTE: n_fields_cmp in tuple must be set so that it cannot be compared
to node pointer page number fields on the upper levels of the tree!
Note that if mode is PAGE_CUR_LE, which is used in inserts, then
cursor->up_match and cursor->low_match both will have sensible values.
If mode is PAGE_CUR_GE, then up_match will a have a sensible value. */
124
UNIV_INTERN
osku's avatar
osku committed
125 126 127
void
btr_cur_search_to_nth_level(
/*========================*/
128 129 130
	dict_index_t*	index,	/*!< in: index */
	ulint		level,	/*!< in: the tree level of search */
	const dtuple_t*	tuple,	/*!< in: data tuple; NOTE: n_fields_cmp in
osku's avatar
osku committed
131 132
				tuple must be set so that it cannot get
				compared to the node ptr page number field! */
133
	ulint		mode,	/*!< in: PAGE_CUR_L, ...;
osku's avatar
osku committed
134 135 136 137 138 139
				NOTE that if the search is made using a unique
				prefix of a record, mode should be PAGE_CUR_LE,
				not PAGE_CUR_GE, as the latter may end up on
				the previous page of the record! Inserts
				should always be made using PAGE_CUR_LE to
				search the position! */
140
	ulint		latch_mode, /*!< in: BTR_SEARCH_LEAF, ..., ORed with
141 142
				at most one of BTR_INSERT, BTR_DELETE_MARK,
				BTR_DELETE, or BTR_ESTIMATE;
143
				cursor->left_block is used to store a pointer
osku's avatar
osku committed
144 145 146 147 148 149 150
				to the left neighbor page, in the cases
				BTR_SEARCH_PREV and BTR_MODIFY_PREV;
				NOTE that if has_search_latch
				is != 0, we maybe do not have a latch set
				on the cursor page, we assume
				the caller uses his search latch
				to protect the record! */
151
	btr_cur_t*	cursor, /*!< in/out: tree cursor; the cursor page is
osku's avatar
osku committed
152
				s- or x-latched, but see also above! */
153
	ulint		has_search_latch,/*!< in: latch mode the caller
osku's avatar
osku committed
154 155
				currently has on btr_search_latch:
				RW_S_LATCH, or 0 */
156 157
	const char*	file,	/*!< in: file name */
	ulint		line,	/*!< in: line where called */
158
	mtr_t*		mtr);	/*!< in: mtr */
159
/*****************************************************************//**
osku's avatar
osku committed
160
Opens a cursor at either end of an index. */
161
UNIV_INTERN
osku's avatar
osku committed
162
void
163 164
btr_cur_open_at_index_side_func(
/*============================*/
165
	ibool		from_left,	/*!< in: TRUE if open to the low end,
osku's avatar
osku committed
166
					FALSE if to the high end */
167 168 169
	dict_index_t*	index,		/*!< in: index */
	ulint		latch_mode,	/*!< in: latch mode */
	btr_cur_t*	cursor,		/*!< in: cursor */
170 171
	const char*	file,		/*!< in: file name */
	ulint		line,		/*!< in: line where called */
172
	mtr_t*		mtr);		/*!< in: mtr */
173 174
#define btr_cur_open_at_index_side(f,i,l,c,m)				\
	btr_cur_open_at_index_side_func(f,i,l,c,__FILE__,__LINE__,m)
175
/**********************************************************************//**
osku's avatar
osku committed
176
Positions a cursor at a randomly chosen position within a B-tree. */
177
UNIV_INTERN
osku's avatar
osku committed
178
void
179 180
btr_cur_open_at_rnd_pos_func(
/*=========================*/
181 182 183
	dict_index_t*	index,		/*!< in: index */
	ulint		latch_mode,	/*!< in: BTR_SEARCH_LEAF, ... */
	btr_cur_t*	cursor,		/*!< in/out: B-tree cursor */
184 185
	const char*	file,		/*!< in: file name */
	ulint		line,		/*!< in: line where called */
186
	mtr_t*		mtr);		/*!< in: mtr */
187 188
#define btr_cur_open_at_rnd_pos(i,l,c,m)				\
	btr_cur_open_at_rnd_pos_func(i,l,c,__FILE__,__LINE__,m)
189
/*************************************************************//**
osku's avatar
osku committed
190 191 192 193
Tries to perform an insert to a page in an index tree, next to cursor.
It is assumed that mtr holds an x-latch on the page. The operation does
not succeed if there is too little space on the page. If there is just
one record on the page, the insert will always succeed; this is to
194 195
prevent trying to split a page with just one record.
@return	DB_SUCCESS, DB_WAIT_LOCK, DB_FAIL, or error number */
196
UNIV_INTERN
osku's avatar
osku committed
197 198 199
ulint
btr_cur_optimistic_insert(
/*======================*/
200
	ulint		flags,	/*!< in: undo logging and locking flags: if not
osku's avatar
osku committed
201 202
				zero, the parameters index and thr should be
				specified */
203
	btr_cur_t*	cursor,	/*!< in: cursor on page after which to insert;
osku's avatar
osku committed
204
				cursor stays valid */
205 206
	dtuple_t*	entry,	/*!< in/out: entry to insert */
	rec_t**		rec,	/*!< out: pointer to inserted record if
osku's avatar
osku committed
207
				succeed */
208
	big_rec_t**	big_rec,/*!< out: big rec vector whose fields have to
osku's avatar
osku committed
209 210
				be stored externally by the caller, or
				NULL */
211 212 213
	ulint		n_ext,	/*!< in: number of externally stored columns */
	que_thr_t*	thr,	/*!< in: query thread or NULL */
	mtr_t*		mtr);	/*!< in: mtr; if this function returns
214 215 216 217
				DB_SUCCESS on a leaf page of a secondary
				index in a compressed tablespace, the
				mtr must be committed before latching
				any further pages */
218
/*************************************************************//**
osku's avatar
osku committed
219 220 221
Performs an insert on a page of an index tree. It is assumed that mtr
holds an x-latch on the tree and on the cursor page. If the insert is
made on the leaf level, to avoid deadlocks, mtr must also own x-latches
222 223
to brothers of page, if those brothers exist.
@return	DB_SUCCESS or error number */
224
UNIV_INTERN
osku's avatar
osku committed
225 226 227
ulint
btr_cur_pessimistic_insert(
/*=======================*/
228
	ulint		flags,	/*!< in: undo logging and locking flags: if not
osku's avatar
osku committed
229 230 231 232 233
				zero, the parameter thr should be
				specified; if no undo logging is specified,
				then the caller must have reserved enough
				free extents in the file space so that the
				insertion will certainly succeed */
234
	btr_cur_t*	cursor,	/*!< in: cursor after which to insert;
osku's avatar
osku committed
235
				cursor stays valid */
236 237
	dtuple_t*	entry,	/*!< in/out: entry to insert */
	rec_t**		rec,	/*!< out: pointer to inserted record if
osku's avatar
osku committed
238
				succeed */
239
	big_rec_t**	big_rec,/*!< out: big rec vector whose fields have to
osku's avatar
osku committed
240 241
				be stored externally by the caller, or
				NULL */
242 243 244
	ulint		n_ext,	/*!< in: number of externally stored columns */
	que_thr_t*	thr,	/*!< in: query thread or NULL */
	mtr_t*		mtr);	/*!< in: mtr */
245
/*************************************************************//**
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
See if there is enough place in the page modification log to log
an update-in-place.
@return	TRUE if enough place */
UNIV_INTERN
ibool
btr_cur_update_alloc_zip(
/*=====================*/
	page_zip_des_t*	page_zip,/*!< in/out: compressed page */
	buf_block_t*	block,	/*!< in/out: buffer page */
	dict_index_t*	index,	/*!< in: the index corresponding to the block */
	ulint		length,	/*!< in: size needed */
	ibool		create,	/*!< in: TRUE=delete-and-insert,
				FALSE=update-in-place */
	mtr_t*		mtr)	/*!< in: mini-transaction */
	__attribute__((nonnull, warn_unused_result));
/*************************************************************//**
262 263
Updates a record when the update causes no size changes in its fields.
@return	DB_SUCCESS or error number */
264
UNIV_INTERN
osku's avatar
osku committed
265 266 267
ulint
btr_cur_update_in_place(
/*====================*/
268 269
	ulint		flags,	/*!< in: undo logging and locking flags */
	btr_cur_t*	cursor,	/*!< in: cursor on the record to update;
osku's avatar
osku committed
270 271
				cursor stays valid and positioned on the
				same record */
272 273
	const upd_t*	update,	/*!< in: update vector */
	ulint		cmpl_info,/*!< in: compiler info on secondary index
osku's avatar
osku committed
274
				updates */
275 276
	que_thr_t*	thr,	/*!< in: query thread */
	mtr_t*		mtr);	/*!< in: mtr; must be committed before
277
				latching any further pages */
278
/*************************************************************//**
osku's avatar
osku committed
279 280 281
Tries to update a record on a page in an index tree. It is assumed that mtr
holds an x-latch on the page. The operation does not succeed if there is too
little space on the page or if the update would result in too empty a page,
282
so that tree compression is recommended.
283 284 285
@return DB_SUCCESS, or DB_OVERFLOW if the updated record does not fit,
DB_UNDERFLOW if the page would become too empty, or DB_ZIP_OVERFLOW if
there is not enough space left on the compressed page */
286
UNIV_INTERN
osku's avatar
osku committed
287 288 289
ulint
btr_cur_optimistic_update(
/*======================*/
290 291
	ulint		flags,	/*!< in: undo logging and locking flags */
	btr_cur_t*	cursor,	/*!< in: cursor on the record to update;
osku's avatar
osku committed
292 293
				cursor stays valid and positioned on the
				same record */
294
	const upd_t*	update,	/*!< in: update vector; this must also
osku's avatar
osku committed
295
				contain trx id and roll ptr fields */
296
	ulint		cmpl_info,/*!< in: compiler info on secondary index
osku's avatar
osku committed
297
				updates */
298 299
	que_thr_t*	thr,	/*!< in: query thread */
	mtr_t*		mtr);	/*!< in: mtr; must be committed before
300
				latching any further pages */
301
/*************************************************************//**
osku's avatar
osku committed
302 303 304
Performs an update of a record on a page of a tree. It is assumed
that mtr holds an x-latch on the tree and on the cursor page. If the
update is made on the leaf level, to avoid deadlocks, mtr must also
305 306
own x-latches to brothers of page, if those brothers exist.
@return	DB_SUCCESS or error code */
307
UNIV_INTERN
osku's avatar
osku committed
308 309 310
ulint
btr_cur_pessimistic_update(
/*=======================*/
311
	ulint		flags,	/*!< in: undo logging, locking, and rollback
osku's avatar
osku committed
312
				flags */
313 314 315
	btr_cur_t*	cursor,	/*!< in: cursor on the record to update */
	mem_heap_t**	heap,	/*!< in/out: pointer to memory heap, or NULL */
	big_rec_t**	big_rec,/*!< out: big rec vector whose fields have to
osku's avatar
osku committed
316
				be stored externally by the caller, or NULL */
317
	const upd_t*	update,	/*!< in: update vector; this is allowed also
osku's avatar
osku committed
318 319
				contain trx id and roll ptr fields, but
				the values in update vector have no effect */
320
	ulint		cmpl_info,/*!< in: compiler info on secondary index
osku's avatar
osku committed
321
				updates */
322 323
	que_thr_t*	thr,	/*!< in: query thread */
	mtr_t*		mtr);	/*!< in: mtr; must be committed before
324
				latching any further pages */
325
/***********************************************************//**
osku's avatar
osku committed
326 327 328
Marks a clustered index record deleted. Writes an undo log record to
undo log on this delete marking. Writes in the trx id field the id
of the deleting transaction, and in the roll ptr field pointer to the
329 330
undo log record created.
@return	DB_SUCCESS, DB_LOCK_WAIT, or error number */
331
UNIV_INTERN
osku's avatar
osku committed
332 333 334
ulint
btr_cur_del_mark_set_clust_rec(
/*===========================*/
335 336 337 338 339
	ulint		flags,	/*!< in: undo logging and locking flags */
	btr_cur_t*	cursor,	/*!< in: cursor */
	ibool		val,	/*!< in: value to set */
	que_thr_t*	thr,	/*!< in: query thread */
	mtr_t*		mtr);	/*!< in: mtr */
340
/***********************************************************//**
341 342
Sets a secondary index record delete mark to TRUE or FALSE.
@return	DB_SUCCESS, DB_LOCK_WAIT, or error number */
343
UNIV_INTERN
osku's avatar
osku committed
344 345 346
ulint
btr_cur_del_mark_set_sec_rec(
/*=========================*/
347 348 349 350 351
	ulint		flags,	/*!< in: locking flag */
	btr_cur_t*	cursor,	/*!< in: cursor */
	ibool		val,	/*!< in: value to set */
	que_thr_t*	thr,	/*!< in: query thread */
	mtr_t*		mtr);	/*!< in: mtr */
352
/*************************************************************//**
osku's avatar
osku committed
353 354 355 356
Tries to compress a page of the tree if it seems useful. It is assumed
that mtr holds an x-latch on the tree and on the cursor page. To avoid
deadlocks, mtr must also own x-latches to brothers of page, if those
brothers exist. NOTE: it is assumed that the caller has reserved enough
357 358
free extents so that the compression will always succeed if done!
@return	TRUE if compression occurred */
359
UNIV_INTERN
osku's avatar
osku committed
360 361 362
ibool
btr_cur_compress_if_useful(
/*=======================*/
363
	btr_cur_t*	cursor,	/*!< in: cursor on the page to compress;
osku's avatar
osku committed
364 365
				cursor does not stay valid if compression
				occurs */
366
	mtr_t*		mtr);	/*!< in: mtr */
367
/*******************************************************//**
osku's avatar
osku committed
368 369
Removes the record on which the tree cursor is positioned. It is assumed
that the mtr has an x-latch on the page where the cursor is positioned,
370 371
but no latch on the whole tree.
@return	TRUE if success, i.e., the page did not become too empty */
372
UNIV_INTERN
osku's avatar
osku committed
373 374 375
ibool
btr_cur_optimistic_delete(
/*======================*/
376
	btr_cur_t*	cursor,	/*!< in: cursor on the record to delete;
osku's avatar
osku committed
377 378 379
				cursor stays valid: if deletion succeeds,
				on function exit it points to the successor
				of the deleted record */
380
	mtr_t*		mtr);	/*!< in: mtr; if this function returns
381 382 383
				TRUE on a leaf page of a secondary
				index, the mtr must be committed
				before latching any further pages */
384
/*************************************************************//**
osku's avatar
osku committed
385 386 387 388 389
Removes the record on which the tree cursor is positioned. Tries
to compress the page if its fillfactor drops below a threshold
or if it is the only page on the level. It is assumed that mtr holds
an x-latch on the tree and on the cursor page. To avoid deadlocks,
mtr must also own x-latches to brothers of page, if those brothers
390 391
exist.
@return	TRUE if compression occurred */
392
UNIV_INTERN
osku's avatar
osku committed
393 394 395
ibool
btr_cur_pessimistic_delete(
/*=======================*/
396
	ulint*		err,	/*!< out: DB_SUCCESS or DB_OUT_OF_FILE_SPACE;
osku's avatar
osku committed
397 398 399 400
				the latter may occur because we may have
				to update node pointers on upper levels,
				and in the case of variable length keys
				these may actually grow in size */
401
	ibool		has_reserved_extents, /*!< in: TRUE if the
osku's avatar
osku committed
402 403 404
				caller has already reserved enough free
				extents so that he knows that the operation
				will succeed */
405
	btr_cur_t*	cursor,	/*!< in: cursor on the record to delete;
osku's avatar
osku committed
406 407 408
				if compression does not occur, the cursor
				stays valid: it points to successor of
				deleted record on function exit */
409 410
	enum trx_rb_ctx	rb_ctx,	/*!< in: rollback context */
	mtr_t*		mtr);	/*!< in: mtr */
411
#endif /* !UNIV_HOTBACKUP */
412
/***********************************************************//**
413 414
Parses a redo log record of updating a record in-place.
@return	end of log record or NULL */
415
UNIV_INTERN
osku's avatar
osku committed
416 417 418
byte*
btr_cur_parse_update_in_place(
/*==========================*/
419 420 421 422 423
	byte*		ptr,	/*!< in: buffer */
	byte*		end_ptr,/*!< in: buffer end */
	page_t*		page,	/*!< in/out: page or NULL */
	page_zip_des_t*	page_zip,/*!< in/out: compressed page, or NULL */
	dict_index_t*	index);	/*!< in: index corresponding to page */
424
/****************************************************************//**
osku's avatar
osku committed
425
Parses the redo log record for delete marking or unmarking of a clustered
426 427
index record.
@return	end of log record or NULL */
428
UNIV_INTERN
osku's avatar
osku committed
429 430 431
byte*
btr_cur_parse_del_mark_set_clust_rec(
/*=================================*/
432 433 434 435 436
	byte*		ptr,	/*!< in: buffer */
	byte*		end_ptr,/*!< in: buffer end */
	page_t*		page,	/*!< in/out: page or NULL */
	page_zip_des_t*	page_zip,/*!< in/out: compressed page, or NULL */
	dict_index_t*	index);	/*!< in: index corresponding to page */
437
/****************************************************************//**
osku's avatar
osku committed
438
Parses the redo log record for delete marking or unmarking of a secondary
439 440
index record.
@return	end of log record or NULL */
441
UNIV_INTERN
osku's avatar
osku committed
442 443 444
byte*
btr_cur_parse_del_mark_set_sec_rec(
/*===============================*/
445 446 447 448
	byte*		ptr,	/*!< in: buffer */
	byte*		end_ptr,/*!< in: buffer end */
	page_t*		page,	/*!< in/out: page or NULL */
	page_zip_des_t*	page_zip);/*!< in/out: compressed page, or NULL */
449
#ifndef UNIV_HOTBACKUP
450
/*******************************************************************//**
451 452
Estimates the number of rows in a given index range.
@return	estimated number of rows */
453
UNIV_INTERN
454
ib_int64_t
osku's avatar
osku committed
455 456
btr_estimate_n_rows_in_range(
/*=========================*/
457 458 459 460 461
	dict_index_t*	index,	/*!< in: index */
	const dtuple_t*	tuple1,	/*!< in: range start, may also be empty tuple */
	ulint		mode1,	/*!< in: search mode for range start */
	const dtuple_t*	tuple2,	/*!< in: range end, may also be empty tuple */
	ulint		mode2);	/*!< in: search mode for range end */
462
/*******************************************************************//**
osku's avatar
osku committed
463 464 465
Estimates the number of different key values in a given index, for
each n-column prefix of the index where n <= dict_index_get_n_unique(index).
The estimates are stored in the array index->stat_n_diff_key_vals. */
466
UNIV_INTERN
osku's avatar
osku committed
467 468 469
void
btr_estimate_number_of_different_key_vals(
/*======================================*/
470
	dict_index_t*	index);	/*!< in: index */
471
/*******************************************************************//**
osku's avatar
osku committed
472 473 474
Marks not updated extern fields as not-owned by this record. The ownership
is transferred to the updated record which is inserted elsewhere in the
index tree. In purge only the owner of externally stored field is allowed
475 476
to free the field.
@return TRUE if BLOB ownership was transferred */
477
UNIV_INTERN
478
ibool
osku's avatar
osku committed
479 480
btr_cur_mark_extern_inherited_fields(
/*=================================*/
481
	page_zip_des_t*	page_zip,/*!< in/out: compressed page whose uncompressed
482
				part will be updated, or NULL */
483 484 485 486 487
	rec_t*		rec,	/*!< in/out: record in a clustered index */
	dict_index_t*	index,	/*!< in: index of the page */
	const ulint*	offsets,/*!< in: array returned by rec_get_offsets() */
	const upd_t*	update,	/*!< in: update vector */
	mtr_t*		mtr);	/*!< in: mtr, or NULL if not logged */
488
/*******************************************************************//**
osku's avatar
osku committed
489 490 491
The complement of the previous function: in an update entry may inherit
some externally stored fields from a record. We must mark them as inherited
in entry, so that they are not freed in a rollback. */
492
UNIV_INTERN
osku's avatar
osku committed
493 494 495
void
btr_cur_mark_dtuple_inherited_extern(
/*=================================*/
496
	dtuple_t*	entry,		/*!< in/out: updated entry to be
497
					inserted to clustered index */
498
	const upd_t*	update);	/*!< in: update vector */
499
/*******************************************************************//**
osku's avatar
osku committed
500
Marks all extern fields in a dtuple as owned by the record. */
501
UNIV_INTERN
osku's avatar
osku committed
502 503 504
void
btr_cur_unmark_dtuple_extern_fields(
/*================================*/
505
	dtuple_t*	entry);		/*!< in/out: clustered index entry */
506
/*******************************************************************//**
osku's avatar
osku committed
507
Stores the fields in big_rec_vec to the tablespace and puts pointers to
508 509
them in rec.  The extern flags in rec will have to be set beforehand.
The fields are stored on pages allocated from leaf node
510
file segment of the index tree.
511
@return	DB_SUCCESS or DB_OUT_OF_FILE_SPACE */
512
UNIV_INTERN
osku's avatar
osku committed
513 514 515
ulint
btr_store_big_rec_extern_fields(
/*============================*/
516
	dict_index_t*	index,		/*!< in: index of rec; the index tree
osku's avatar
osku committed
517
					MUST be X-latched */
518 519 520
	buf_block_t*	rec_block,	/*!< in/out: block containing rec */
	rec_t*		rec,		/*!< in: record */
	const ulint*	offsets,	/*!< in: rec_get_offsets(rec, index);
521 522 523
					the "external storage" flags in offsets
					will not correspond to rec when
					this function returns */
524
	big_rec_t*	big_rec_vec,	/*!< in: vector containing fields
osku's avatar
osku committed
525
					to be stored externally */
526
	mtr_t*		local_mtr);	/*!< in: mtr containing the latch to
osku's avatar
osku committed
527
					rec and to the tree */
528
/*******************************************************************//**
osku's avatar
osku committed
529 530 531 532
Frees the space in an externally stored field to the file space
management if the field in data is owned the externally stored field,
in a rollback we may have the additional condition that the field must
not be inherited. */
533
UNIV_INTERN
osku's avatar
osku committed
534 535 536
void
btr_free_externally_stored_field(
/*=============================*/
537
	dict_index_t*	index,		/*!< in: index of the data, the index
osku's avatar
osku committed
538 539 540 541 542 543 544
					tree MUST be X-latched; if the tree
					height is 1, then also the root page
					must be X-latched! (this is relevant
					in the case this function is called
					from purge where 'data' is located on
					an undo log page, not an index
					page) */
545 546
	byte*		field_ref,	/*!< in/out: field reference */
	const rec_t*	rec,		/*!< in: record containing field_ref, for
marko's avatar
marko committed
547
					page_zip_write_blob_ptr(), or NULL */
548
	const ulint*	offsets,	/*!< in: rec_get_offsets(rec, index),
549
					or NULL */
550
	page_zip_des_t*	page_zip,	/*!< in: compressed page corresponding
marko's avatar
marko committed
551
					to rec, or NULL if rec == NULL */
552
	ulint		i,		/*!< in: field number of field_ref;
marko's avatar
marko committed
553
					ignored if rec == NULL */
554 555
	enum trx_rb_ctx	rb_ctx,		/*!< in: rollback context */
	mtr_t*		local_mtr);	/*!< in: mtr containing the latch to
osku's avatar
osku committed
556 557
					data an an X-latch to the index
					tree */
558
/*******************************************************************//**
559
Copies the prefix of an externally stored field of a record.  The
560
clustered index record must be protected by a lock or a page latch.
561 562
@return the length of the copied field, or 0 if the column was being
or has been deleted */
563
UNIV_INTERN
564 565 566
ulint
btr_copy_externally_stored_field_prefix(
/*====================================*/
567 568 569
	byte*		buf,	/*!< out: the field, or a prefix of it */
	ulint		len,	/*!< in: length of buf, in bytes */
	ulint		zip_size,/*!< in: nonzero=compressed BLOB page size,
570
				zero for uncompressed BLOBs */
571
	const byte*	data,	/*!< in: 'internally' stored part of the
572
				field containing also the reference to
573 574
				the external part; must be protected by
				a lock or a page latch */
575
	ulint		local_len);/*!< in: length of data, in bytes */
576
/*******************************************************************//**
577
Copies an externally stored field of a record to mem heap.
578
@return	the field copied to heap, or NULL if the field is incomplete */
579
UNIV_INTERN
osku's avatar
osku committed
580 581 582
byte*
btr_rec_copy_externally_stored_field(
/*=================================*/
583
	const rec_t*	rec,	/*!< in: record in a clustered index;
584
				must be protected by a lock or a page latch */
585 586
	const ulint*	offsets,/*!< in: array returned by rec_get_offsets() */
	ulint		zip_size,/*!< in: nonzero=compressed BLOB page size,
587
				zero for uncompressed BLOBs */
588 589 590
	ulint		no,	/*!< in: field number */
	ulint*		len,	/*!< out: length of the field */
	mem_heap_t*	heap);	/*!< in: mem heap */
591
/*******************************************************************//**
592 593
Flags the data tuple fields that are marked as extern storage in the
update vector.  We use this function to remember which fields we must
594 595
mark as extern storage in a record inserted for an update.
@return	number of flagged external columns */
596
UNIV_INTERN
osku's avatar
osku committed
597 598 599
ulint
btr_push_update_extern_fields(
/*==========================*/
600 601 602
	dtuple_t*	tuple,	/*!< in/out: data tuple */
	const upd_t*	update,	/*!< in: update vector */
	mem_heap_t*	heap)	/*!< in: memory heap */
603
	__attribute__((nonnull));
604
/***********************************************************//**
605 606
Sets a secondary index record's delete mark to the given value. This
function is only used by the insert buffer merge mechanism. */
marko's avatar
marko committed
607
UNIV_INTERN
608 609 610
void
btr_cur_set_deleted_flag_for_ibuf(
/*==============================*/
611 612
	rec_t*		rec,		/*!< in/out: record */
	page_zip_des_t*	page_zip,	/*!< in/out: compressed page
613 614 615
					corresponding to rec, or NULL
					when the tablespace is
					uncompressed */
616 617
	ibool		val,		/*!< in: value to set */
	mtr_t*		mtr);		/*!< in: mtr */
osku's avatar
osku committed
618 619
/*######################################################################*/

620
/** In the pessimistic delete, if the page data size drops below this
osku's avatar
osku committed
621 622 623
limit, merging it to a neighbor is tried */
#define BTR_CUR_PAGE_COMPRESS_LIMIT	(UNIV_PAGE_SIZE / 2)

624
/** A slot in the path array. We store here info on a search path down the
osku's avatar
osku committed
625 626 627 628
tree. Each slot contains data on a single level of the tree. */

typedef struct btr_path_struct	btr_path_t;
struct btr_path_struct{
629
	ulint	nth_rec;	/*!< index of the record
osku's avatar
osku committed
630 631 632 633
				where the page cursor stopped on
				this level (index in alphabetical
				order); value ULINT_UNDEFINED
				denotes array end */
634
	ulint	n_recs;		/*!< number of records on the page */
635 636 637 638 639
	ulint	page_no;	/*!< no of the page containing the record */
	ulint	page_level;	/*!< level of the page, if later we fetch
				the page under page_no and it is no different
				level then we know that the tree has been
				reorganized */
osku's avatar
osku committed
640 641
};

642
#define BTR_PATH_ARRAY_N_SLOTS	250	/*!< size of path array (in slots) */
osku's avatar
osku committed
643

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
/** Values for the flag documenting the used search method */
enum btr_cur_method {
	BTR_CUR_HASH = 1,	/*!< successful shortcut using
				the hash index */
	BTR_CUR_HASH_FAIL,	/*!< failure using hash, success using
				binary search: the misleading hash
				reference is stored in the field
				hash_node, and might be necessary to
				update */
	BTR_CUR_BINARY,		/*!< success using the binary search */
	BTR_CUR_INSERT_TO_IBUF,	/*!< performed the intended insert to
				the insert buffer */
	BTR_CUR_DEL_MARK_IBUF,	/*!< performed the intended delete
				mark in the insert/delete buffer */
	BTR_CUR_DELETE_IBUF,	/*!< performed the intended delete in
				the insert/delete buffer */
660
	BTR_CUR_DELETE_REF	/*!< row_purge_poss_sec() failed */
661
};
osku's avatar
osku committed
662

663 664
/** The tree cursor: the definition appears here only for the compiler
to know struct size! */
osku's avatar
osku committed
665
struct btr_cur_struct {
666 667 668 669
	dict_index_t*	index;		/*!< index where positioned */
	page_cur_t	page_cur;	/*!< page cursor */
	purge_node_t*	purge_node;	/*!< purge node, for BTR_DELETE */
	buf_block_t*	left_block;	/*!< this field is used to store
670 671 672 673
					a pointer to the left neighbor
					page, in the cases
					BTR_SEARCH_PREV and
					BTR_MODIFY_PREV */
osku's avatar
osku committed
674
	/*------------------------------*/
675 676 677 678 679
	que_thr_t*	thr;		/*!< this field is only used
					when btr_cur_search_to_nth_level
					is called for an index entry
					insertion: the calling query
					thread is passed here to be
osku's avatar
osku committed
680 681
					used in the insert buffer */
	/*------------------------------*/
682 683 684 685 686
	/** The following fields are used in
	btr_cur_search_to_nth_level to pass information: */
	/* @{ */
	enum btr_cur_method	flag;	/*!< Search method used */
	ulint		tree_height;	/*!< Tree height if the search is done
osku's avatar
osku committed
687 688
					for a pessimistic insert or update
					operation */
689
	ulint		up_match;	/*!< If the search mode was PAGE_CUR_LE,
osku's avatar
osku committed
690 691 692
					the number of matched fields to the
					the first user record to the right of
					the cursor record after
693
					btr_cur_search_to_nth_level;
osku's avatar
osku committed
694 695 696 697 698 699 700 701 702
					for the mode PAGE_CUR_GE, the matched
					fields to the first user record AT THE
					CURSOR or to the right of it;
					NOTE that the up_match and low_match
					values may exceed the correct values
					for comparison to the adjacent user
					record if that record is on a
					different leaf page! (See the note in
					row_ins_duplicate_key.) */
703
	ulint		up_bytes;	/*!< number of matched bytes to the
osku's avatar
osku committed
704 705 706
					right at the time cursor positioned;
					only used internally in searches: not
					defined after the search */
707
	ulint		low_match;	/*!< if search mode was PAGE_CUR_LE,
osku's avatar
osku committed
708 709 710
					the number of matched fields to the
					first user record AT THE CURSOR or
					to the left of it after
711
					btr_cur_search_to_nth_level;
osku's avatar
osku committed
712 713 714
					NOT defined for PAGE_CUR_GE or any
					other search modes; see also the NOTE
					in up_match! */
715
	ulint		low_bytes;	/*!< number of matched bytes to the
osku's avatar
osku committed
716 717 718
					right at the time cursor positioned;
					only used internally in searches: not
					defined after the search */
719
	ulint		n_fields;	/*!< prefix length used in a hash
osku's avatar
osku committed
720
					search if hash_node != NULL */
721
	ulint		n_bytes;	/*!< hash prefix bytes if hash_node !=
osku's avatar
osku committed
722
					NULL */
723
	ulint		fold;		/*!< fold value used in the search if
osku's avatar
osku committed
724
					flag is BTR_CUR_HASH */
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
	/*----- Delete buffering -------*/
	ulint		ibuf_cnt;	/* in searches done on insert buffer
					trees, this contains the "counter"
					value (the first two bytes of the
					fourth field) extracted from the
					page above the leaf page, from the
					father node pointer that pointed to
					the leaf page. in other words, it
					contains the minimum counter value
					for records to be inserted on the
					chosen leaf page. If for some reason
					this can't be read, or if the search
					ended on the leftmost leaf page in
					the tree (in which case the father
					node pointer had the 'minimum
					record' flag set), this is
					ULINT_UNDEFINED. */
osku's avatar
osku committed
742
	/*------------------------------*/
743 744
	/* @} */
	btr_path_t*	path_arr;	/*!< in estimating the number of
osku's avatar
osku committed
745 746 747 748 749
					rows in range, we store in this array
					information of the path through
					the tree */
};

750 751 752
/** If pessimistic delete fails because of lack of file space, there
is still a good change of success a little later.  Try this many
times. */
osku's avatar
osku committed
753
#define BTR_CUR_RETRY_DELETE_N_TIMES	100
754 755 756
/** If pessimistic delete fails because of lack of file space, there
is still a good change of success a little later.  Sleep this many
microseconds between retries. */
osku's avatar
osku committed
757 758
#define BTR_CUR_RETRY_SLEEP_TIME	50000

759
/** The reference in a field for which data is stored on a different page.
osku's avatar
osku committed
760 761 762 763 764
The reference is at the end of the 'locally' stored part of the field.
'Locally' means storage in the index record.
We store locally a long enough prefix of each column so that we can determine
the ordering parts of each index record without looking into the externally
stored part. */
765 766 767 768
/*-------------------------------------- @{ */
#define BTR_EXTERN_SPACE_ID		0	/*!< space id where stored */
#define BTR_EXTERN_PAGE_NO		4	/*!< page no where stored */
#define BTR_EXTERN_OFFSET		8	/*!< offset of BLOB header
osku's avatar
osku committed
769
						on that page */
770
#define BTR_EXTERN_LEN			12	/*!< 8 bytes containing the
osku's avatar
osku committed
771 772 773 774
						length of the externally
						stored part of the BLOB.
						The 2 highest bits are
						reserved to the flags below. */
775
/*-------------------------------------- @} */
776
/* #define BTR_EXTERN_FIELD_REF_SIZE	20 // moved to btr0types.h */
osku's avatar
osku committed
777

778 779 780 781
/** The most significant bit of BTR_EXTERN_LEN (i.e., the most
significant bit of the byte at smallest address) is set to 1 if this
field does not 'own' the externally stored field; only the owner field
is allowed to free the field in purge! */
osku's avatar
osku committed
782
#define BTR_EXTERN_OWNER_FLAG		128
783 784 785 786 787
/** If the second most significant bit of BTR_EXTERN_LEN (i.e., the
second most significant bit of the byte at smallest address) is 1 then
it means that the externally stored field was inherited from an
earlier version of the row.  In rollback we are not allowed to free an
inherited external field. */
osku's avatar
osku committed
788 789
#define BTR_EXTERN_INHERITED_FLAG	64

790
/** Number of searches down the B-tree in btr_cur_search_to_nth_level(). */
osku's avatar
osku committed
791
extern ulint	btr_cur_n_non_sea;
792 793
/** Number of successful adaptive hash index lookups in
btr_cur_search_to_nth_level(). */
osku's avatar
osku committed
794
extern ulint	btr_cur_n_sea;
795 796 797
/** Old value of btr_cur_n_non_sea.  Copied by
srv_refresh_innodb_monitor_stats().  Referenced by
srv_printf_innodb_monitor(). */
osku's avatar
osku committed
798
extern ulint	btr_cur_n_non_sea_old;
799 800 801
/** Old value of btr_cur_n_sea.  Copied by
srv_refresh_innodb_monitor_stats().  Referenced by
srv_printf_innodb_monitor(). */
osku's avatar
osku committed
802
extern ulint	btr_cur_n_sea_old;
803
#endif /* !UNIV_HOTBACKUP */
osku's avatar
osku committed
804 805 806 807

#ifndef UNIV_NONINL
#include "btr0cur.ic"
#endif
808

osku's avatar
osku committed
809
#endif