page0zip.c 128 KB
Newer Older
marko's avatar
marko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/******************************************************
Compressed page interface

(c) 2005 Innobase Oy

Created June 2005 by Marko Makela
*******************************************************/

#define THIS_MODULE
#include "page0zip.h"
#ifdef UNIV_NONINL
# include "page0zip.ic"
#endif
#undef THIS_MODULE
#include "page0page.h"
#include "mtr0log.h"
17
#include "ut0sort.h"
18
#include "dict0boot.h"
19
#include "dict0dict.h"
20
#include "btr0sea.h"
21 22
#include "btr0cur.h"
#include "page0types.h"
23
#include "lock0lock.h"
24
#include "log0recv.h"
marko's avatar
marko committed
25
#include "zlib.h"
26
#include "buf0lru.h"
marko's avatar
marko committed
27

28 29
/** Statistics on compression, indexed by page_zip_des_t::ssize - 1 */
UNIV_INTERN page_zip_stat_t page_zip_stat[PAGE_ZIP_NUM_SSIZE - 1];
30

31 32 33
/* Please refer to ../include/page0zip.ic for a description of the
compressed page format. */

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/* The infimum and supremum records are omitted from the compressed page.
On compress, we compare that the records are there, and on uncompress we
restore the records. */
static const byte infimum_extra[] = {
	0x01,			/* info_bits=0, n_owned=1 */
	0x00, 0x02		/* heap_no=0, status=2 */
	/* ?, ?	*/		/* next=(first user rec, or supremum) */
};
static const byte infimum_data[] = {
	0x69, 0x6e, 0x66, 0x69,
	0x6d, 0x75, 0x6d, 0x00	/* "infimum\0" */
};
static const byte supremum_extra_data[] = {
	/* 0x0?, */		/* info_bits=0, n_owned=1..8 */
	0x00, 0x0b,		/* heap_no=1, status=3 */
	0x00, 0x00,		/* next=0 */
	0x73, 0x75, 0x70, 0x72,
	0x65, 0x6d, 0x75, 0x6d	/* "supremum" */
};

vasil's avatar
vasil committed
54 55
/** Assert that a block of memory is filled with zero bytes.
Compare at most sizeof(field_ref_zero) bytes. */
56 57 58 59 60
#define ASSERT_ZERO(b, s) \
	ut_ad(!memcmp(b, field_ref_zero, ut_min(s, sizeof field_ref_zero)))
/** Assert that a BLOB pointer is filled with zero bytes. */
#define ASSERT_ZERO_BLOB(b) \
	ut_ad(!memcmp(b, field_ref_zero, sizeof field_ref_zero))
marko's avatar
marko committed
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
/* Enable some extra debugging output.  This code can be enabled
independently of any UNIV_ debugging conditions. */
#if defined UNIV_DEBUG || defined UNIV_ZIP_DEBUG
# include <stdarg.h>
__attribute__((format (printf, 1, 2)))
/**************************************************************************
Report a failure to decompress or compress. */
static
int
page_zip_fail_func(
/*===============*/
				/* out: number of characters printed */
	const char*	fmt,	/* in: printf(3) format string */
	...)			/* in: arguments corresponding to fmt */
{
	int	res;
	va_list	ap;

80 81
	ut_print_timestamp(stderr);
	fputs("  InnoDB: ", stderr);
82 83 84 85 86 87 88 89 90 91 92
	va_start(ap, fmt);
	res = vfprintf(stderr, fmt, ap);
	va_end(ap);

	return(res);
}
# define page_zip_fail(fmt_args) page_zip_fail_func fmt_args
#else /* UNIV_DEBUG || UNIV_ZIP_DEBUG */
# define page_zip_fail(fmt_args) /* empty */
#endif /* UNIV_DEBUG || UNIV_ZIP_DEBUG */

93 94
/**************************************************************************
Determine the guaranteed free space on an empty page. */
95
UNIV_INTERN
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
ulint
page_zip_empty_size(
/*================*/
				/* out: minimum payload size on the page */
	ulint	n_fields,	/* in: number of columns in the index */
	ulint	zip_size)	/* in: compressed page size in bytes */
{
	lint	size = zip_size
		/* subtract the page header and the longest
		uncompressed data needed for one record */
		- (PAGE_DATA
		   + PAGE_ZIP_DIR_SLOT_SIZE
		   + DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN
		   + 1/* encoded heap_no==2 in page_zip_write_rec() */
		   + 1/* end of modification log */
		   - REC_N_NEW_EXTRA_BYTES/* omitted bytes */)
		/* subtract the space for page_zip_fields_encode() */
		- compressBound(2 * (n_fields + 1));
	return(size > 0 ? (ulint) size : 0);
}

117 118 119 120 121 122 123 124 125 126 127 128 129
/*****************************************************************
Gets the size of the compressed page trailer (the dense page directory),
including deleted records (the free list). */
UNIV_INLINE
ulint
page_zip_dir_size(
/*==============*/
						/* out: length of dense page
						directory, in bytes */
	const page_zip_des_t*	page_zip)	/* in: compressed page */
{
	/* Exclude the page infimum and supremum from the record count. */
	ulint	size = PAGE_ZIP_DIR_SLOT_SIZE
130 131
		* (page_dir_get_n_heap(page_zip->data)
		   - PAGE_HEAP_NO_USER_LOW);
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	return(size);
}

/*****************************************************************
Gets the size of the compressed page trailer (the dense page directory),
only including user records (excluding the free list). */
UNIV_INLINE
ulint
page_zip_dir_user_size(
/*===================*/
						/* out: length of dense page
						directory comprising existing
						records, in bytes */
	const page_zip_des_t*	page_zip)	/* in: compressed page */
{
	ulint	size = PAGE_ZIP_DIR_SLOT_SIZE
148
		* page_get_n_recs(page_zip->data);
149
	ut_ad(size <= page_zip_dir_size(page_zip));
150 151 152 153
	return(size);
}

/*****************************************************************
154
Find the slot of the given record in the dense page directory. */
155 156
UNIV_INLINE
byte*
157 158 159 160 161 162 163
page_zip_dir_find_low(
/*==================*/
					/* out: dense directory slot,
					or NULL if record not found */
	byte*	slot,			/* in: start of records */
	byte*	end,			/* in: end of records */
	ulint	offset)			/* in: offset of user record */
164
{
165
	ut_ad(slot <= end);
166 167 168

	for (; slot < end; slot += PAGE_ZIP_DIR_SLOT_SIZE) {
		if ((mach_read_from_2(slot) & PAGE_ZIP_DIR_SLOT_MASK)
169
		    == offset) {
170 171 172 173 174 175 176 177
			return(slot);
		}
	}

	return(NULL);
}

/*****************************************************************
178
Find the slot of the given non-free record in the dense page directory. */
179 180
UNIV_INLINE
byte*
181 182 183 184 185 186
page_zip_dir_find(
/*==============*/
						/* out: dense directory slot,
						or NULL if record not found */
	page_zip_des_t*	page_zip,		/* in: compressed page */
	ulint		offset)			/* in: offset of user record */
187
{
188
	byte*	end	= page_zip->data + page_zip_get_size(page_zip);
189

190
	ut_ad(page_zip_simple_validate(page_zip));
191

192 193 194
	return(page_zip_dir_find_low(end - page_zip_dir_user_size(page_zip),
				     end,
				     offset));
195 196
}

197 198 199 200 201 202 203 204 205 206 207
/*****************************************************************
Find the slot of the given free record in the dense page directory. */
UNIV_INLINE
byte*
page_zip_dir_find_free(
/*===================*/
						/* out: dense directory slot,
						or NULL if record not found */
	page_zip_des_t*	page_zip,		/* in: compressed page */
	ulint		offset)			/* in: offset of user record */
{
208
	byte*	end	= page_zip->data + page_zip_get_size(page_zip);
209 210 211

	ut_ad(page_zip_simple_validate(page_zip));

212 213 214
	return(page_zip_dir_find_low(end - page_zip_dir_size(page_zip),
				     end - page_zip_dir_user_size(page_zip),
				     offset));
215 216
}

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
/*****************************************************************
Read a given slot in the dense page directory. */
UNIV_INLINE
ulint
page_zip_dir_get(
/*=============*/
						/* out: record offset
						on the uncompressed page,
						possibly ORed with
						PAGE_ZIP_DIR_SLOT_DEL or
						PAGE_ZIP_DIR_SLOT_OWNED */
	const page_zip_des_t*	page_zip,	/* in: compressed page */
	ulint			slot)		/* in: slot
						(0=first user record) */
{
	ut_ad(page_zip_simple_validate(page_zip));
	ut_ad(slot < page_zip_dir_size(page_zip) / PAGE_ZIP_DIR_SLOT_SIZE);
234
	return(mach_read_from_2(page_zip->data + page_zip_get_size(page_zip)
235
				- PAGE_ZIP_DIR_SLOT_SIZE * (slot + 1)));
236 237
}

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
/**************************************************************************
Write a log record of compressing an index page. */
static
void
page_zip_compress_write_log(
/*========================*/
	const page_zip_des_t*	page_zip,/* in: compressed page */
	const page_t*		page,	/* in: uncompressed page */
	dict_index_t*		index,	/* in: index of the B-tree node */
	mtr_t*			mtr)	/* in: mini-transaction */
{
	byte*	log_ptr;
	ulint	trailer_size;

	log_ptr = mlog_open(mtr, 11 + 2 + 2);

	if (!log_ptr) {

		return;
	}

259 260 261
	/* Read the number of user records. */
	trailer_size = page_dir_get_n_heap(page_zip->data)
		- PAGE_HEAP_NO_USER_LOW;
262
	/* Multiply by uncompressed of size stored per record */
263
	if (!page_is_leaf(page)) {
264
		trailer_size *= PAGE_ZIP_DIR_SLOT_SIZE + REC_NODE_PTR_SIZE;
265 266
	} else if (dict_index_is_clust(index)) {
		trailer_size *= PAGE_ZIP_DIR_SLOT_SIZE
267
			+ DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN;
268 269
	} else {
		trailer_size *= PAGE_ZIP_DIR_SLOT_SIZE;
270 271 272 273 274 275 276
	}
	/* Add the space occupied by BLOB pointers. */
	trailer_size += page_zip->n_blobs * BTR_EXTERN_FIELD_REF_SIZE;
	ut_a(page_zip->m_end > PAGE_DATA);
#if FIL_PAGE_DATA > PAGE_DATA
# error "FIL_PAGE_DATA > PAGE_DATA"
#endif
277
	ut_a(page_zip->m_end + trailer_size <= page_zip_get_size(page_zip));
278

279 280 281
	log_ptr = mlog_write_initial_log_record_fast((page_t*) page,
						     MLOG_ZIP_PAGE_COMPRESS,
						     log_ptr, mtr);
282 283 284 285 286 287 288 289 290 291 292 293
	mach_write_to_2(log_ptr, page_zip->m_end - FIL_PAGE_TYPE);
	log_ptr += 2;
	mach_write_to_2(log_ptr, trailer_size);
	log_ptr += 2;
	mlog_close(mtr, log_ptr);

	/* Write FIL_PAGE_PREV and FIL_PAGE_NEXT */
	mlog_catenate_string(mtr, page_zip->data + FIL_PAGE_PREV, 4);
	mlog_catenate_string(mtr, page_zip->data + FIL_PAGE_NEXT, 4);
	/* Write most of the page header, the compressed stream and
	the modification log. */
	mlog_catenate_string(mtr, page_zip->data + FIL_PAGE_TYPE,
294
			     page_zip->m_end - FIL_PAGE_TYPE);
295
	/* Write the uncompressed trailer of the compressed page. */
296
	mlog_catenate_string(mtr, page_zip->data + page_zip_get_size(page_zip)
297
			     - trailer_size, trailer_size);
298 299
}

300 301
/**********************************************************
Determine how many externally stored columns are contained
302
in existing records with smaller heap_no than rec. */
303
static
304 305 306
ulint
page_zip_get_n_prev_extern(
/*=======================*/
307 308 309 310 311
	const page_zip_des_t*	page_zip,/* in: dense page directory on
					compressed page */
	const rec_t*		rec,	/* in: compact physical record
					on a B-tree leaf page */
	dict_index_t*		index)	/* in: record descriptor */
312
{
313
	const page_t*	page	= page_align(rec);
314 315 316 317 318
	ulint		n_ext	= 0;
	ulint		i;
	ulint		left;
	ulint		heap_no;
	ulint		n_recs	= page_get_n_recs(page_zip->data);
319 320 321 322

	ut_ad(page_is_leaf(page));
	ut_ad(page_is_comp(page));
	ut_ad(dict_table_is_comp(index->table));
323
	ut_ad(dict_index_is_clust(index));
324

325
	heap_no = rec_get_heap_no_new(rec);
326 327
	ut_ad(heap_no >= PAGE_HEAP_NO_USER_LOW);
	left = heap_no - PAGE_HEAP_NO_USER_LOW;
328 329 330
	if (UNIV_UNLIKELY(!left)) {
		return(0);
	}
331

332
	for (i = 0; i < n_recs; i++) {
333 334
		const rec_t*	r	= page + (page_zip_dir_get(page_zip, i)
						  & PAGE_ZIP_DIR_SLOT_MASK);
335

336
		if (rec_get_heap_no_new(r) < heap_no) {
337 338
			n_ext += rec_get_n_extern_new(r, index,
						      ULINT_UNDEFINED);
339 340 341
			if (!--left) {
				break;
			}
342
		}
343
	}
344

345
	return(n_ext);
346 347
}

348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
/**************************************************************************
Encode the length of a fixed-length column. */
static
byte*
page_zip_fixed_field_encode(
/*========================*/
			/* out: buf + length of encoded val */
	byte*	buf,	/* in: pointer to buffer where to write */
	ulint	val)	/* in: value to write */
{
	ut_ad(val >= 2);

	if (UNIV_LIKELY(val < 126)) {
		/*
		0 = nullable variable field of at most 255 bytes length;
		1 = not null variable field of at most 255 bytes length;
		126 = nullable variable field with maximum length >255;
		127 = not null variable field with maximum length >255
		*/
367
		*buf++ = (byte) val;
368
	} else {
369 370
		*buf++ = (byte) (0x80 | val >> 8);
		*buf++ = (byte) val;
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
	}

	return(buf);
}

/**************************************************************************
Write the index information for the compressed page. */
static
ulint
page_zip_fields_encode(
/*===================*/
				/* out: used size of buf */
	ulint		n,	/* in: number of fields to compress */
	dict_index_t*	index,	/* in: index comprising at least n fields */
	ulint		trx_id_pos,/* in: position of the trx_id column
				in the index, or ULINT_UNDEFINED if
				this is a non-leaf page */
	byte*		buf)	/* out: buffer of (n + 1) * 2 bytes */
{
	const byte*	buf_start	= buf;
	ulint		i;
	ulint		col;
	ulint		trx_id_col	= 0;
	/* sum of lengths of preceding non-nullable fixed fields, or 0 */
	ulint		fixed_sum	= 0;

	ut_ad(trx_id_pos == ULINT_UNDEFINED || trx_id_pos < n);

	for (i = col = 0; i < n; i++) {
		dict_field_t*	field = dict_index_get_nth_field(index, i);
		ulint		val;

403
		if (dict_field_get_col(field)->prtype & DATA_NOT_NULL) {
404 405 406 407 408 409 410
			val = 1; /* set the "not nullable" flag */
		} else {
			val = 0; /* nullable field */
		}

		if (!field->fixed_len) {
			/* variable-length field */
411 412
			const dict_col_t*	column
				= dict_field_get_col(field);
413

414 415
			if (UNIV_UNLIKELY(column->len > 255)
			    || UNIV_UNLIKELY(column->mtype == DATA_BLOB)) {
416 417 418 419 420 421
				val |= 0x7e; /* max > 255 bytes */
			}

			if (fixed_sum) {
				/* write out the length of any
				preceding non-nullable fields */
422 423
				buf = page_zip_fixed_field_encode(
					buf, fixed_sum << 1 | 1);
424 425 426 427
				fixed_sum = 0;
				col++;
			}

428
			*buf++ = (byte) val;
429 430 431
			col++;
		} else if (val) {
			/* fixed-length non-nullable field */
432

433 434 435
			if (fixed_sum && UNIV_UNLIKELY
			    (fixed_sum + field->fixed_len
			     > DICT_MAX_INDEX_COL_LEN)) {
436 437 438 439
				/* Write out the length of the
				preceding non-nullable fields,
				to avoid exceeding the maximum
				length of a fixed-length column. */
440 441
				buf = page_zip_fixed_field_encode(
					buf, fixed_sum << 1 | 1);
442 443 444 445
				fixed_sum = 0;
				col++;
			}

446 447 448 449 450
			if (i && UNIV_UNLIKELY(i == trx_id_pos)) {
				if (fixed_sum) {
					/* Write out the length of any
					preceding non-nullable fields,
					and start a new trx_id column. */
451 452
					buf = page_zip_fixed_field_encode(
						buf, fixed_sum << 1 | 1);
453
					col++;
454 455
				}

456
				trx_id_col = col;
457 458 459 460 461 462 463 464 465 466 467
				fixed_sum = field->fixed_len;
			} else {
				/* add to the sum */
				fixed_sum += field->fixed_len;
			}
		} else {
			/* fixed-length nullable field */

			if (fixed_sum) {
				/* write out the length of any
				preceding non-nullable fields */
468 469
				buf = page_zip_fixed_field_encode(
					buf, fixed_sum << 1 | 1);
470 471 472 473
				fixed_sum = 0;
				col++;
			}

474 475
			buf = page_zip_fixed_field_encode(
				buf, field->fixed_len << 1);
476 477 478 479 480 481 482 483 484 485 486
			col++;
		}
	}

	if (fixed_sum) {
		/* Write out the lengths of last fixed-length columns. */
		buf = page_zip_fixed_field_encode(buf, fixed_sum << 1 | 1);
	}

	if (trx_id_pos != ULINT_UNDEFINED) {
		/* Write out the position of the trx_id column */
marko's avatar
marko committed
487 488 489 490
		i = trx_id_col;
	} else {
		/* Write out the number of nullable fields */
		i = index->n_nullable;
491 492
	}

marko's avatar
marko committed
493
	if (i < 128) {
494
		*buf++ = (byte) i;
marko's avatar
marko committed
495
	} else {
496 497
		*buf++ = (byte) (0x80 | i >> 8);
		*buf++ = (byte) i;
marko's avatar
marko committed
498 499 500
	}

	ut_ad((ulint) (buf - buf_start) <= (n + 2) * 2);
501 502 503
	return((ulint) (buf - buf_start));
}

504 505 506 507 508 509 510
/**************************************************************************
Populate the dense page directory from the sparse directory. */
static
void
page_zip_dir_encode(
/*================*/
	const page_t*	page,	/* in: compact page */
511 512 513
	byte*		buf,	/* in: pointer to dense page directory[-1];
				out: dense directory on compressed page */
	const rec_t**	recs)	/* in: pointer to an array of 0, or NULL;
514 515
				out: dense page directory sorted by ascending
				address (and heap_no) */
516
{
517 518 519 520 521 522 523
	const byte*	rec;
	ulint		status;
	ulint		min_mark;
	ulint		heap_no;
	ulint		i;
	ulint		n_heap;
	ulint		offs;
524 525 526

	min_mark = 0;

527 528 529
	if (page_is_leaf(page)) {
		status = REC_STATUS_ORDINARY;
	} else {
530
		status = REC_STATUS_NODE_PTR;
531
		if (UNIV_UNLIKELY
532
		    (mach_read_from_4(page + FIL_PAGE_PREV) == FIL_NULL)) {
533 534 535 536
			min_mark = REC_INFO_MIN_REC_FLAG;
		}
	}

537
	n_heap = page_dir_get_n_heap(page);
538 539 540 541

	/* Traverse the list of stored records in the collation order,
	starting from the first user record. */

542
	rec = page + PAGE_NEW_INFIMUM, TRUE;
543 544 545 546 547 548 549 550 551

	i = 0;

	for (;;) {
		ulint	info_bits;
		offs = rec_get_next_offs(rec, TRUE);
		if (UNIV_UNLIKELY(offs == PAGE_NEW_SUPREMUM)) {
			break;
		}
552
		rec = page + offs;
553
		heap_no = rec_get_heap_no_new(rec);
554
		ut_a(heap_no >= PAGE_HEAP_NO_USER_LOW);
555
		ut_a(heap_no < n_heap);
556 557
		ut_a(offs < UNIV_PAGE_SIZE - PAGE_DIR);
		ut_a(offs >= PAGE_ZIP_START);
558 559 560 561 562
#if PAGE_ZIP_DIR_SLOT_MASK & (PAGE_ZIP_DIR_SLOT_MASK + 1)
# error "PAGE_ZIP_DIR_SLOT_MASK is not 1 less than a power of 2"
#endif
#if PAGE_ZIP_DIR_SLOT_MASK < UNIV_PAGE_SIZE - 1
# error "PAGE_ZIP_DIR_SLOT_MASK < UNIV_PAGE_SIZE - 1"
563
#endif
564 565 566 567 568 569 570 571 572 573 574 575 576 577
		if (UNIV_UNLIKELY(rec_get_n_owned_new(rec))) {
			offs |= PAGE_ZIP_DIR_SLOT_OWNED;
		}

		info_bits = rec_get_info_bits(rec, TRUE);
		if (UNIV_UNLIKELY(info_bits & REC_INFO_DELETED_FLAG)) {
			info_bits &= ~REC_INFO_DELETED_FLAG;
			offs |= PAGE_ZIP_DIR_SLOT_DEL;
		}
		ut_a(info_bits == min_mark);
		/* Only the smallest user record can have
		REC_INFO_MIN_REC_FLAG set. */
		min_mark = 0;

578
		mach_write_to_2(buf - PAGE_ZIP_DIR_SLOT_SIZE * ++i, offs);
579

580 581
		if (UNIV_LIKELY_NULL(recs)) {
			/* Ensure that each heap_no occurs at most once. */
582
			ut_a(!recs[heap_no - PAGE_HEAP_NO_USER_LOW]);
583
			/* exclude infimum and supremum */
584
			recs[heap_no - PAGE_HEAP_NO_USER_LOW] = rec;
585
		}
586 587 588 589

		ut_a(rec_get_status(rec) == status);
	}

590
	offs = page_header_get_field(page, PAGE_FREE);
591 592 593 594

	/* Traverse the free list (of deleted records). */
	while (offs) {
		ut_ad(!(offs & ~PAGE_ZIP_DIR_SLOT_MASK));
595
		rec = page + offs;
596 597

		heap_no = rec_get_heap_no_new(rec);
598
		ut_a(heap_no >= PAGE_HEAP_NO_USER_LOW);
599 600 601 602 603
		ut_a(heap_no < n_heap);

		ut_a(!rec[-REC_N_NEW_EXTRA_BYTES]); /* info_bits and n_owned */
		ut_a(rec_get_status(rec) == status);

604
		mach_write_to_2(buf - PAGE_ZIP_DIR_SLOT_SIZE * ++i, offs);
605

606 607
		if (UNIV_LIKELY_NULL(recs)) {
			/* Ensure that each heap_no occurs at most once. */
608
			ut_a(!recs[heap_no - PAGE_HEAP_NO_USER_LOW]);
609
			/* exclude infimum and supremum */
610
			recs[heap_no - PAGE_HEAP_NO_USER_LOW] = rec;
611
		}
612 613 614 615 616

		offs = rec_get_next_offs(rec, TRUE);
	}

	/* Ensure that each heap no occurs at least once. */
617
	ut_a(i + PAGE_HEAP_NO_USER_LOW == n_heap);
618 619
}

620 621 622 623
/**************************************************************************
Allocate memory for zlib. */
static
void*
624 625
page_zip_malloc(
/*============*/
626
	void*	opaque,
627 628 629
	uInt	items,
	uInt	size)
{
630
	return(mem_heap_alloc(opaque, items * size));
631 632 633 634 635 636 637 638
}

/**************************************************************************
Deallocate memory for zlib. */
static
void
page_zip_free(
/*==========*/
639 640
	void*	opaque __attribute__((unused)),
	void*	address __attribute__((unused)))
641 642 643
{
}

644 645
/**************************************************************************
Configure the zlib allocator to use the given memory heap. */
646
UNIV_INTERN
647 648 649 650 651 652 653 654 655 656 657 658 659
void
page_zip_set_alloc(
/*===============*/
	void*		stream,		/* in/out: zlib stream */
	mem_heap_t*	heap)		/* in: memory heap to use */
{
	z_stream*	strm = stream;

	strm->zalloc = page_zip_malloc;
	strm->zfree = page_zip_free;
	strm->opaque = heap;
}

660 661 662 663 664
#if 0 || defined UNIV_DEBUG || defined UNIV_ZIP_DEBUG
# define PAGE_ZIP_COMPRESS_DBG
#endif

#ifdef PAGE_ZIP_COMPRESS_DBG
665 666
/* Set this variable in a debugger to enable
excessive logging in page_zip_compress(). */
667
UNIV_INTERN ibool	page_zip_compress_dbg;
668 669 670 671 672
/* Set this variable in a debugger to enable
binary logging of the data passed to deflate().
When this variable is nonzero, it will act
as a log file name generator. */
UNIV_INTERN unsigned	page_zip_compress_log;
673 674 675 676 677 678 679

/**************************************************************************
Wrapper for deflate().  Log the operation if page_zip_compress_dbg is set. */
static
ibool
page_zip_compress_deflate(
/*======================*/
680
	FILE*		logfile,/* in: log file, or NULL */
681 682 683 684 685 686 687
	z_streamp	strm,	/* in/out: compressed stream for deflate() */
	int		flush)	/* in: deflate() flushing method */
{
	int	status;
	if (UNIV_UNLIKELY(page_zip_compress_dbg)) {
		ut_print_buf(stderr, strm->next_in, strm->avail_in);
	}
688 689 690
	if (UNIV_LIKELY_NULL(logfile)) {
		fwrite(strm->next_in, 1, strm->avail_in, logfile);
	}
691 692 693 694 695 696 697 698 699
	status = deflate(strm, flush);
	if (UNIV_UNLIKELY(page_zip_compress_dbg)) {
		fprintf(stderr, " -> %d\n", status);
	}
	return(status);
}

/* Redefine deflate(). */
# undef deflate
700 701 702 703 704 705 706
# define deflate(strm, flush) page_zip_compress_deflate(logfile, strm, flush)
# define FILE_LOGFILE FILE* logfile,
# define LOGFILE logfile,
#else /* PAGE_ZIP_COMPRESS_DBG */
# define FILE_LOGFILE
# define LOGFILE
#endif /* PAGE_ZIP_COMPRESS_DBG */
707

708 709 710 711 712 713 714
/**************************************************************************
Compress the records of a node pointer page. */
static
int
page_zip_compress_node_ptrs(
/*========================*/
					/* out: Z_OK, or a zlib error code */
715
	FILE_LOGFILE
716 717 718 719 720 721 722 723 724 725 726 727
	z_stream*	c_stream,	/* in/out: compressed page stream */
	const rec_t**	recs,		/* in: dense page directory
					sorted by address */
	ulint		n_dense,	/* in: size of recs[] */
	dict_index_t*	index,		/* in: the index of the page */
	byte*		storage,	/* in: end of dense page directory */
	mem_heap_t*	heap)		/* in: temporary memory heap */
{
	int	err	= Z_OK;
	ulint*	offsets = NULL;

	do {
728
		const rec_t*	rec = *recs++;
729 730 731 732 733 734

		offsets = rec_get_offsets(rec, index, offsets,
					  ULINT_UNDEFINED, &heap);
		/* Only leaf nodes may contain externally stored columns. */
		ut_ad(!rec_offs_any_extern(offsets));

735 736 737
		UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets));
		UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets),
				   rec_offs_extra_size(offsets));
738

739 740 741 742 743 744 745 746 747 748 749 750 751
		/* Compress the extra bytes. */
		c_stream->avail_in = rec - REC_N_NEW_EXTRA_BYTES
			- c_stream->next_in;

		if (c_stream->avail_in) {
			err = deflate(c_stream, Z_NO_FLUSH);
			if (UNIV_UNLIKELY(err != Z_OK)) {
				break;
			}
		}
		ut_ad(!c_stream->avail_in);

		/* Compress the data bytes, except node_ptr. */
752
		c_stream->next_in = (byte*) rec;
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
		c_stream->avail_in = rec_offs_data_size(offsets)
			- REC_NODE_PTR_SIZE;
		ut_ad(c_stream->avail_in);

		err = deflate(c_stream, Z_NO_FLUSH);
		if (UNIV_UNLIKELY(err != Z_OK)) {
			break;
		}

		ut_ad(!c_stream->avail_in);

		memcpy(storage - REC_NODE_PTR_SIZE
		       * (rec_get_heap_no_new(rec) - 1),
		       c_stream->next_in, REC_NODE_PTR_SIZE);
		c_stream->next_in += REC_NODE_PTR_SIZE;
	} while (--n_dense);

	return(err);
}

/**************************************************************************
Compress the records of a leaf node of a secondary index. */
static
int
page_zip_compress_sec(
/*==================*/
					/* out: Z_OK, or a zlib error code */
780
	FILE_LOGFILE
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
	z_stream*	c_stream,	/* in/out: compressed page stream */
	const rec_t**	recs,		/* in: dense page directory
					sorted by address */
	ulint		n_dense)	/* in: size of recs[] */
{
	int		err	= Z_OK;

	ut_ad(n_dense > 0);

	do {
		const rec_t*	rec = *recs++;

		/* Compress everything up to this record. */
		c_stream->avail_in = rec - REC_N_NEW_EXTRA_BYTES
			- c_stream->next_in;

		if (UNIV_LIKELY(c_stream->avail_in)) {
798 799
			UNIV_MEM_ASSERT_RW(c_stream->next_in,
					   c_stream->avail_in);
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
			err = deflate(c_stream, Z_NO_FLUSH);
			if (UNIV_UNLIKELY(err != Z_OK)) {
				break;
			}
		}

		ut_ad(!c_stream->avail_in);
		ut_ad(c_stream->next_in == rec - REC_N_NEW_EXTRA_BYTES);

		/* Skip the REC_N_NEW_EXTRA_BYTES. */

		c_stream->next_in = (byte*) rec;
	} while (--n_dense);

	return(err);
}

817 818 819 820 821 822 823 824
/**************************************************************************
Compress a record of a leaf node of a clustered index that contains
externally stored columns. */
static
int
page_zip_compress_clust_ext(
/*========================*/
					/* out: Z_OK, or a zlib error code */
825
	FILE_LOGFILE
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
	z_stream*	c_stream,	/* in/out: compressed page stream */
	const rec_t*	rec,		/* in: record */
	const ulint*	offsets,	/* in: rec_get_offsets(rec) */
	ulint		trx_id_col,	/* in: position of of DB_TRX_ID */
	byte*		deleted,	/* in: dense directory entry pointing
					to the head of the free list */
	byte*		storage,	/* in: end of dense page directory */
	byte**		externs,	/* in/out: pointer to the next
					available BLOB pointer */
	ulint*		n_blobs)	/* in/out: number of
					externally stored columns */
{
	int	err;
	ulint	i;

841 842 843
	UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets));
	UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets),
			   rec_offs_extra_size(offsets));
844

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
	for (i = 0; i < rec_offs_n_fields(offsets); i++) {
		ulint		len;
		const byte*	src;

		if (UNIV_UNLIKELY(i == trx_id_col)) {
			ut_ad(!rec_offs_nth_extern(offsets, i));
			/* Store trx_id and roll_ptr
			in uncompressed form. */
			src = rec_get_nth_field(rec, offsets, i, &len);
			ut_ad(src + DATA_TRX_ID_LEN
			      == rec_get_nth_field(rec, offsets,
						   i + 1, &len));
			ut_ad(len == DATA_ROLL_PTR_LEN);

			/* Compress any preceding bytes. */
			c_stream->avail_in
				= src - c_stream->next_in;

			if (c_stream->avail_in) {
				err = deflate(c_stream, Z_NO_FLUSH);
				if (UNIV_UNLIKELY(err != Z_OK)) {

					return(err);
				}
			}

			ut_ad(!c_stream->avail_in);
			ut_ad(c_stream->next_in == src);

			memcpy(storage
			       - (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN)
			       * (rec_get_heap_no_new(rec) - 1),
			       c_stream->next_in,
			       DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);

			c_stream->next_in
				+= DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN;

			/* Skip also roll_ptr */
			i++;
		} else if (rec_offs_nth_extern(offsets, i)) {
			src = rec_get_nth_field(rec, offsets, i, &len);
			ut_ad(len >= BTR_EXTERN_FIELD_REF_SIZE);
			src += len - BTR_EXTERN_FIELD_REF_SIZE;

			c_stream->avail_in = src
				- c_stream->next_in;
			if (UNIV_LIKELY(c_stream->avail_in)) {
				err = deflate(c_stream, Z_NO_FLUSH);
				if (UNIV_UNLIKELY(err != Z_OK)) {

					return(err);
				}
			}

			ut_ad(!c_stream->avail_in);
			ut_ad(c_stream->next_in == src);

			/* Reserve space for the data at
			the end of the space reserved for
			the compressed data and the page
			modification log. */

			if (UNIV_UNLIKELY
			    (c_stream->avail_out
			     <= BTR_EXTERN_FIELD_REF_SIZE)) {
				/* out of space */
				return(Z_BUF_ERROR);
			}

			ut_ad(*externs == c_stream->next_out
			      + c_stream->avail_out
			      + 1/* end of modif. log */);

			c_stream->next_in
				+= BTR_EXTERN_FIELD_REF_SIZE;

			/* Skip deleted records. */
			if (UNIV_LIKELY_NULL
			    (page_zip_dir_find_low(
				    storage, deleted,
				    page_offset(rec)))) {
				continue;
			}

			(*n_blobs)++;
			c_stream->avail_out
				-= BTR_EXTERN_FIELD_REF_SIZE;
			*externs -= BTR_EXTERN_FIELD_REF_SIZE;

			/* Copy the BLOB pointer */
			memcpy(*externs, c_stream->next_in
			       - BTR_EXTERN_FIELD_REF_SIZE,
			       BTR_EXTERN_FIELD_REF_SIZE);
		}
	}

	return(Z_OK);
}

945 946 947 948 949 950 951
/**************************************************************************
Compress the records of a leaf node of a clustered index. */
static
int
page_zip_compress_clust(
/*====================*/
					/* out: Z_OK, or a zlib error code */
952
	FILE_LOGFILE
953 954 955 956 957
	z_stream*	c_stream,	/* in/out: compressed page stream */
	const rec_t**	recs,		/* in: dense page directory
					sorted by address */
	ulint		n_dense,	/* in: size of recs[] */
	dict_index_t*	index,		/* in: the index of the page */
958
	ulint*		n_blobs,	/* in: 0; out: number of
959 960 961 962 963 964 965 966 967 968 969 970
					externally stored columns */
	ulint		trx_id_col,	/* index of the trx_id column */
	byte*		deleted,	/* in: dense directory entry pointing
					to the head of the free list */
	byte*		storage,	/* in: end of dense page directory */
	mem_heap_t*	heap)		/* in: temporary memory heap */
{
	int	err		= Z_OK;
	ulint*	offsets		= NULL;
	/* BTR_EXTERN_FIELD_REF storage */
	byte*	externs		= storage - n_dense
		* (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
971 972

	ut_ad(*n_blobs == 0);
973 974

	do {
975
		const rec_t*	rec = *recs++;
976 977 978

		offsets = rec_get_offsets(rec, index, offsets,
					  ULINT_UNDEFINED, &heap);
979 980
		ut_ad(rec_offs_n_fields(offsets)
		      == dict_index_get_n_fields(index));
981 982 983
		UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets));
		UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets),
				   rec_offs_extra_size(offsets));
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000

		/* Compress the extra bytes. */
		c_stream->avail_in = rec - REC_N_NEW_EXTRA_BYTES
			- c_stream->next_in;

		if (c_stream->avail_in) {
			err = deflate(c_stream, Z_NO_FLUSH);
			if (UNIV_UNLIKELY(err != Z_OK)) {

				goto func_exit;
			}
		}
		ut_ad(!c_stream->avail_in);
		ut_ad(c_stream->next_in == rec - REC_N_NEW_EXTRA_BYTES);

		/* Compress the data bytes. */

1001
		c_stream->next_in = (byte*) rec;
1002 1003 1004 1005

		/* Check if there are any externally stored columns.
		For each externally stored column, store the
		BTR_EXTERN_FIELD_REF separately. */
1006 1007
		if (UNIV_UNLIKELY(rec_offs_any_extern(offsets))) {
			ut_ad(dict_index_is_clust(index));
1008

1009
			err = page_zip_compress_clust_ext(
1010
				LOGFILE
1011 1012
				c_stream, rec, offsets, trx_id_col,
				deleted, storage, &externs, n_blobs);
1013

1014
			if (UNIV_UNLIKELY(err != Z_OK)) {
1015

1016 1017 1018 1019 1020
				goto func_exit;
			}
		} else {
			ulint		len;
			const byte*	src;
1021

1022 1023 1024 1025 1026 1027 1028
			/* Store trx_id and roll_ptr in uncompressed form. */
			src = rec_get_nth_field(rec, offsets,
						trx_id_col, &len);
			ut_ad(src + DATA_TRX_ID_LEN
			      == rec_get_nth_field(rec, offsets,
						   trx_id_col + 1, &len));
			ut_ad(len == DATA_ROLL_PTR_LEN);
1029 1030 1031
			UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets));
			UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets),
					   rec_offs_extra_size(offsets));
1032

1033 1034
			/* Compress any preceding bytes. */
			c_stream->avail_in = src - c_stream->next_in;
1035

1036 1037 1038
			if (c_stream->avail_in) {
				err = deflate(c_stream, Z_NO_FLUSH);
				if (UNIV_UNLIKELY(err != Z_OK)) {
1039

1040
					return(err);
1041
				}
1042
			}
1043

1044 1045
			ut_ad(!c_stream->avail_in);
			ut_ad(c_stream->next_in == src);
1046

1047 1048 1049 1050 1051
			memcpy(storage
			       - (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN)
			       * (rec_get_heap_no_new(rec) - 1),
			       c_stream->next_in,
			       DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
1052

1053 1054
			c_stream->next_in
				+= DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN;
1055

1056 1057
			/* Skip also roll_ptr */
			ut_ad(trx_id_col + 1 < rec_offs_n_fields(offsets));
1058 1059 1060
		}

		/* Compress the last bytes of the record. */
1061
		c_stream->avail_in = rec + rec_offs_data_size(offsets)
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
			- c_stream->next_in;

		if (c_stream->avail_in) {
			err = deflate(c_stream, Z_NO_FLUSH);
			if (UNIV_UNLIKELY(err != Z_OK)) {

				goto func_exit;
			}
		}
		ut_ad(!c_stream->avail_in);
	} while (--n_dense);

func_exit:
	return(err);
}

marko's avatar
marko committed
1078 1079
/**************************************************************************
Compress a page. */
1080
UNIV_INTERN
marko's avatar
marko committed
1081 1082 1083 1084 1085
ibool
page_zip_compress(
/*==============*/
				/* out: TRUE on success, FALSE on failure;
				page_zip will be left intact on failure. */
1086
	page_zip_des_t*	page_zip,/* in: size; out: data, n_blobs,
1087
				m_start, m_end, m_nonempty */
1088
	const page_t*	page,	/* in: uncompressed page */
1089 1090
	dict_index_t*	index,	/* in: index of the B-tree node */
	mtr_t*		mtr)	/* in: mini-transaction, or NULL */
marko's avatar
marko committed
1091 1092 1093
{
	z_stream	c_stream;
	int		err;
1094 1095 1096
	ulint		n_fields;/* number of index fields needed */
	byte*		fields;	/* index field information */
	byte*		buf;	/* compressed payload of the page */
1097
	byte*		buf_end;/* end of buf */
1098
	ulint		n_dense;
1099
	ulint		slot_size;/* amount of uncompressed bytes per record */
1100
	const rec_t**	recs;	/* dense page directory, sorted by address */
1101
	mem_heap_t*	heap;
1102 1103 1104 1105
	ulint		trx_id_col;
	ulint*		offsets	= NULL;
	ulint		n_blobs	= 0;
	byte*		storage;/* storage of uncompressed columns */
1106
	ullint		usec = ut_time_us(NULL);
1107 1108 1109
#ifdef PAGE_ZIP_COMPRESS_DBG
	FILE*		logfile = NULL;
#endif
1110

1111 1112
	ut_a(page_is_comp(page));
	ut_a(fil_page_get_type(page) == FIL_PAGE_INDEX);
1113
	ut_ad(page_simple_validate_new((page_t*) page));
1114
	ut_ad(page_zip_simple_validate(page_zip));
1115

1116 1117
	UNIV_MEM_ASSERT_RW(page, UNIV_PAGE_SIZE);

1118 1119 1120 1121 1122 1123
	/* Check the data that will be omitted. */
	ut_a(!memcmp(page + (PAGE_NEW_INFIMUM - REC_N_NEW_EXTRA_BYTES),
		     infimum_extra, sizeof infimum_extra));
	ut_a(!memcmp(page + PAGE_NEW_INFIMUM,
		     infimum_data, sizeof infimum_data));
	ut_a(page[PAGE_NEW_SUPREMUM - REC_N_NEW_EXTRA_BYTES]
1124 1125
	     /* info_bits == 0, n_owned <= max */
	     <= PAGE_DIR_SLOT_MAX_N_OWNED);
1126 1127
	ut_a(!memcmp(page + (PAGE_NEW_SUPREMUM - REC_N_NEW_EXTRA_BYTES + 1),
		     supremum_extra_data, sizeof supremum_extra_data));
1128

1129 1130
	if (UNIV_UNLIKELY(!page_get_n_recs(page))) {
		ut_a(rec_get_next_offs(page + PAGE_NEW_INFIMUM, TRUE)
1131
		     == PAGE_NEW_SUPREMUM);
1132
	}
1133

1134 1135 1136 1137 1138 1139
	if (page_is_leaf(page)) {
		n_fields = dict_index_get_n_fields(index);
	} else {
		n_fields = dict_index_get_n_unique_in_tree(index);
	}

1140
	/* The dense directory excludes the infimum and supremum records. */
1141
	n_dense = page_dir_get_n_heap(page) - PAGE_HEAP_NO_USER_LOW;
1142
#ifdef PAGE_ZIP_COMPRESS_DBG
1143 1144 1145 1146 1147 1148
	if (UNIV_UNLIKELY(page_zip_compress_dbg)) {
		fprintf(stderr, "compress %p %p %lu %lu %lu\n",
			(void*) page_zip, (void*) page,
			page_is_leaf(page),
			n_fields, n_dense);
	}
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
	if (UNIV_UNLIKELY(page_zip_compress_log)) {
		/* Create a log file for every compression attempt. */
		char	logfilename[9];
		ut_snprintf(logfilename, sizeof logfilename,
			    "%08x", page_zip_compress_log++);
		logfile = fopen(logfilename, "wb");

		if (logfile) {
			/* Write the uncompressed page to the log. */
			fwrite(page, 1, UNIV_PAGE_SIZE, logfile);
			/* Record the compressed size as zero.
			This will be overwritten at successful exit. */
			putc(0, logfile);
			putc(0, logfile);
			putc(0, logfile);
			putc(0, logfile);
		}
	}
#endif /* PAGE_ZIP_COMPRESS_DBG */
1168
	page_zip_stat[page_zip->ssize - 1].compressed++;
1169

1170
	if (UNIV_UNLIKELY(n_dense * PAGE_ZIP_DIR_SLOT_SIZE
1171
			  >= page_zip_get_size(page_zip))) {
1172 1173

		goto err_exit;
1174
	}
1175

1176
	heap = mem_heap_create(page_zip_get_size(page_zip)
1177 1178
			       + n_fields * (2 + sizeof *offsets)
			       + n_dense * ((sizeof *recs)
1179 1180 1181
					    - PAGE_ZIP_DIR_SLOT_SIZE)
			       + UNIV_PAGE_SIZE * 4
			       + (512 << MAX_MEM_LEVEL));
1182

1183
	recs = mem_heap_zalloc(heap, n_dense * sizeof *recs);
1184

1185 1186
	fields = mem_heap_alloc(heap, (n_fields + 1) * 2);

1187 1188
	buf = mem_heap_alloc(heap, page_zip_get_size(page_zip) - PAGE_DATA);
	buf_end = buf + page_zip_get_size(page_zip) - PAGE_DATA;
1189

marko's avatar
marko committed
1190
	/* Compress the data payload. */
1191
	page_zip_set_alloc(&c_stream, heap);
marko's avatar
marko committed
1192

1193 1194 1195
	err = deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION,
			   Z_DEFLATED, UNIV_PAGE_SIZE_SHIFT,
			   MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
marko's avatar
marko committed
1196 1197 1198
	ut_a(err == Z_OK);

	c_stream.next_out = buf;
1199
	/* Subtract the space reserved for uncompressed data. */
1200 1201
	/* Page header and the end marker of the modification log */
	c_stream.avail_out = buf_end - buf - 1;
1202 1203
	/* Dense page directory and uncompressed columns, if any */
	if (page_is_leaf(page)) {
1204
		if (dict_index_is_clust(index)) {
1205 1206
			trx_id_col = dict_index_get_sys_col_pos(
				index, DATA_TRX_ID);
1207 1208 1209
			ut_ad(trx_id_col > 0);
			ut_ad(trx_id_col != ULINT_UNDEFINED);

1210
			slot_size = PAGE_ZIP_DIR_SLOT_SIZE
1211
				+ DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN;
1212
		} else {
1213 1214
			/* Signal the absence of trx_id
			in page_zip_fields_encode() */
1215 1216
			ut_ad(dict_index_get_sys_col_pos(index, DATA_TRX_ID)
			      == ULINT_UNDEFINED);
1217
			trx_id_col = 0;
1218
			slot_size = PAGE_ZIP_DIR_SLOT_SIZE;
1219
		}
1220
	} else {
1221
		slot_size = PAGE_ZIP_DIR_SLOT_SIZE + REC_NODE_PTR_SIZE;
1222
		trx_id_col = ULINT_UNDEFINED;
1223 1224
	}

1225 1226
	if (UNIV_UNLIKELY(c_stream.avail_out <= n_dense * slot_size
			  + 6/* sizeof(zlib header and footer) */)) {
1227 1228 1229 1230
		goto zlib_error;
	}

	c_stream.avail_out -= n_dense * slot_size;
1231 1232
	c_stream.avail_in = page_zip_fields_encode(n_fields, index,
						   trx_id_col, fields);
1233
	c_stream.next_in = fields;
1234
	if (UNIV_LIKELY(!trx_id_col)) {
1235 1236
		trx_id_col = ULINT_UNDEFINED;
	}
1237

1238
	UNIV_MEM_ASSERT_RW(c_stream.next_in, c_stream.avail_in);
1239 1240 1241 1242 1243
	err = deflate(&c_stream, Z_FULL_FLUSH);
	if (err != Z_OK) {
		goto zlib_error;
	}

1244 1245
	ut_ad(!c_stream.avail_in);

1246
	page_zip_dir_encode(page, buf_end, recs);
1247 1248 1249

	c_stream.next_in = (byte*) page + PAGE_ZIP_START;

1250
	storage = buf_end - n_dense * PAGE_ZIP_DIR_SLOT_SIZE;
1251

1252
	/* Compress the records in heap_no order. */
1253
	if (UNIV_UNLIKELY(!n_dense)) {
1254 1255
	} else if (!page_is_leaf(page)) {
		/* This is a node pointer page. */
1256 1257
		err = page_zip_compress_node_ptrs(LOGFILE
						  &c_stream, recs, n_dense,
1258 1259 1260 1261
						  index, storage, heap);
		if (UNIV_UNLIKELY(err != Z_OK)) {
			goto zlib_error;
		}
1262
	} else if (UNIV_LIKELY(trx_id_col == ULINT_UNDEFINED)) {
1263
		/* This is a leaf page in a secondary index. */
1264 1265
		err = page_zip_compress_sec(LOGFILE
					    &c_stream, recs, n_dense);
1266 1267 1268
		if (UNIV_UNLIKELY(err != Z_OK)) {
			goto zlib_error;
		}
1269 1270
	} else {
		/* This is a leaf page in a clustered index. */
1271 1272
		err = page_zip_compress_clust(LOGFILE
					      &c_stream, recs, n_dense,
1273
					      index, &n_blobs, trx_id_col,
1274
					      buf_end - PAGE_ZIP_DIR_SLOT_SIZE
1275
					      * page_get_n_recs(page),
1276 1277 1278 1279
					      storage, heap);
		if (UNIV_UNLIKELY(err != Z_OK)) {
			goto zlib_error;
		}
1280 1281
	}

1282 1283
	/* Finish the compression. */
	ut_ad(!c_stream.avail_in);
1284
	/* Compress any trailing garbage, in case the last record was
1285 1286
	allocated from an originally longer space on the free list,
	or the data of the last record from page_zip_compress_sec(). */
1287
	c_stream.avail_in
1288
		= page_header_get_field(page, PAGE_HEAP_TOP)
1289
		- (c_stream.next_in - page);
1290
	ut_a(c_stream.avail_in <= UNIV_PAGE_SIZE - PAGE_ZIP_START - PAGE_DIR);
marko's avatar
marko committed
1291

1292
	UNIV_MEM_ASSERT_RW(c_stream.next_in, c_stream.avail_in);
marko's avatar
marko committed
1293
	err = deflate(&c_stream, Z_FINISH);
1294

1295
	if (UNIV_UNLIKELY(err != Z_STREAM_END)) {
1296
zlib_error:
marko's avatar
marko committed
1297
		deflateEnd(&c_stream);
1298
		mem_heap_free(heap);
1299
err_exit:
1300 1301 1302 1303 1304
#ifdef PAGE_ZIP_COMPRESS_DBG
		if (logfile) {
			fclose(logfile);
		}
#endif /* PAGE_ZIP_COMPRESS_DBG */
1305
		page_zip_stat[page_zip->ssize - 1].compressed_usec
1306
			+= ut_time_us(NULL) - usec;
marko's avatar
marko committed
1307 1308 1309 1310 1311 1312
		return(FALSE);
	}

	err = deflateEnd(&c_stream);
	ut_a(err == Z_OK);

1313
	ut_ad(buf + c_stream.total_out == c_stream.next_out);
1314 1315
	ut_ad((ulint) (storage - c_stream.next_out) >= c_stream.avail_out);

1316 1317 1318 1319
	/* Valgrind believes that zlib does not initialize some bits
	in the last 7 or 8 bytes of the stream.  Make Valgrind happy. */
	UNIV_MEM_VALID(buf, c_stream.total_out);

1320 1321 1322 1323
	/* Zero out the area reserved for the modification log.
	Space for the end marker of the modification log is not
	included in avail_out. */
	memset(c_stream.next_out, 0, c_stream.avail_out + 1/* end marker */);
1324

1325 1326 1327 1328 1329
#ifdef UNIV_DEBUG
	page_zip->m_start =
#endif /* UNIV_DEBUG */
		page_zip->m_end = PAGE_DATA + c_stream.total_out;
	page_zip->m_nonempty = FALSE;
1330
	page_zip->n_blobs = n_blobs;
1331 1332 1333
	/* Copy those header fields that will not be written
	in buf_flush_init_for_writing() */
	memcpy(page_zip->data + FIL_PAGE_PREV, page + FIL_PAGE_PREV,
1334
	       FIL_PAGE_LSN - FIL_PAGE_PREV);
1335 1336
	memcpy(page_zip->data + FIL_PAGE_TYPE, page + FIL_PAGE_TYPE, 2);
	memcpy(page_zip->data + FIL_PAGE_DATA, page + FIL_PAGE_DATA,
1337
	       PAGE_DATA - FIL_PAGE_DATA);
1338
	/* Copy the rest of the compressed page */
1339 1340
	memcpy(page_zip->data + PAGE_DATA, buf,
	       page_zip_get_size(page_zip) - PAGE_DATA);
1341
	mem_heap_free(heap);
1342
#ifdef UNIV_ZIP_DEBUG
1343
	ut_a(page_zip_validate(page_zip, page));
1344
#endif /* UNIV_ZIP_DEBUG */
marko's avatar
marko committed
1345

1346 1347 1348 1349
	if (mtr) {
		page_zip_compress_write_log(page_zip, page, index, mtr);
	}

1350
	UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip));
1351

1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
#ifdef PAGE_ZIP_COMPRESS_DBG
	if (logfile) {
		/* Record the compressed size of the block. */
		byte sz[4];
		mach_write_to_4(sz, c_stream.total_out);
		fseek(logfile, UNIV_PAGE_SIZE, SEEK_SET);
		fwrite(sz, 1, sizeof sz, logfile);
		fclose(logfile);
	}
#endif /* PAGE_ZIP_COMPRESS_DBG */
1362 1363 1364 1365 1366 1367 1368
	{
		page_zip_stat_t*	zip_stat
			= &page_zip_stat[page_zip->ssize - 1];
		zip_stat->compressed_ok++;
		zip_stat->compressed_usec += ut_time_us(NULL) - usec;
	}

1369
	return(TRUE);
marko's avatar
marko committed
1370 1371
}

1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
/**************************************************************************
Compare two page directory entries. */
UNIV_INLINE
ibool
page_zip_dir_cmp(
/*=============*/
				/* out: positive if rec1 > rec2 */
	const rec_t*	rec1,	/* in: rec1 */
	const rec_t*	rec2)	/* in: rec2 */
{
	return(rec1 > rec2);
}

/**************************************************************************
Sort the dense page directory by address (heap_no). */
static
void
page_zip_dir_sort(
/*==============*/
	rec_t**	arr,	/* in/out: dense page directory */
	rec_t**	aux_arr,/* in/out: work area */
1393 1394
	ulint	low,	/* in: lower bound of the sorting area, inclusive */
	ulint	high)	/* in: upper bound of the sorting area, exclusive */
1395
{
1396 1397
	UT_SORT_FUNCTION_BODY(page_zip_dir_sort, arr, aux_arr, low, high,
			      page_zip_dir_cmp);
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
}

/**************************************************************************
Deallocate the index information initialized by page_zip_fields_decode(). */
static
void
page_zip_fields_free(
/*=================*/
	dict_index_t*	index)	/* in: dummy index to be freed */
{
	if (index) {
		dict_table_t*	table = index->table;
		mem_heap_free(index->heap);
		mutex_free(&(table->autoinc_mutex));
		mem_heap_free(table->heap);
	}
}

/**************************************************************************
Read the index information for the compressed page. */
static
dict_index_t*
page_zip_fields_decode(
/*===================*/
				/* out,own: dummy index describing the page,
				or NULL on error */
	const byte*	buf,	/* in: index information */
	const byte*	end,	/* in: end of buf */
	ulint*		trx_id_col)/* in: NULL for non-leaf pages;
				for leaf pages, pointer to where to store
				the position of the trx_id column */
{
	const byte*	b;
	ulint		n;
	ulint		i;
marko's avatar
marko committed
1433
	ulint		val;
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
	dict_table_t*	table;
	dict_index_t*	index;

	/* Determine the number of fields. */
	for (b = buf, n = 0; b < end; n++) {
		if (*b++ & 0x80) {
			b++; /* skip the second byte */
		}
	}

marko's avatar
marko committed
1444 1445
	n--; /* n_nullable or trx_id */

1446
	if (UNIV_UNLIKELY(n > REC_MAX_N_FIELDS)) {
1447

1448 1449 1450 1451 1452 1453 1454 1455 1456
		page_zip_fail(("page_zip_fields_decode: n = %lu\n",
			       (ulong) n));
		return(NULL);
	}

	if (UNIV_UNLIKELY(b > end)) {

		page_zip_fail(("page_zip_fields_decode: %p > %p\n",
			       (const void*) b, (const void*) end));
1457 1458 1459
		return(NULL);
	}

1460
	table = dict_mem_table_create("ZIP_DUMMY", DICT_HDR_SPACE, n,
1461
				      DICT_TF_COMPACT);
1462
	index = dict_mem_index_create("ZIP_DUMMY", "ZIP_DUMMY",
1463
				      DICT_HDR_SPACE, 0, n);
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
	index->table = table;
	index->n_uniq = n;
	/* avoid ut_ad(index->cached) in dict_index_get_n_unique_in_tree */
	index->cached = TRUE;

	/* Initialize the fields. */
	for (b = buf, i = 0; i < n; i++) {
		ulint	mtype;
		ulint	len;

marko's avatar
marko committed
1474 1475
		val = *b++;

1476
		if (UNIV_UNLIKELY(val & 0x80)) {
1477
			/* fixed length > 62 bytes */
1478
			val = (val & 0x7f) << 8 | *b++;
1479 1480 1481 1482
			len = val >> 1;
			mtype = DATA_FIXBINARY;
		} else if (UNIV_UNLIKELY(val >= 126)) {
			/* variable length with max > 255 bytes */
1483 1484
			len = 0x7fff;
			mtype = DATA_BINARY;
1485 1486 1487 1488 1489 1490 1491
		} else if (val <= 1) {
			/* variable length with max <= 255 bytes */
			len = 0;
			mtype = DATA_BINARY;
		} else {
			/* fixed length < 62 bytes */
			len = val >> 1;
1492 1493
			mtype = DATA_FIXBINARY;
		}
1494

1495
		dict_mem_table_add_col(table, NULL, NULL, mtype,
1496
				       val & 1 ? DATA_NOT_NULL : 0, len);
1497
		dict_index_add_col(index, table,
1498
				   dict_table_get_nth_col(table, i), 0);
1499 1500
	}

marko's avatar
marko committed
1501 1502 1503 1504 1505
	val = *b++;
	if (UNIV_UNLIKELY(val & 0x80)) {
		val = (val & 0x7f) << 8 | *b++;
	}

1506 1507 1508 1509
	/* Decode the position of the trx_id column. */
	if (trx_id_col) {
		if (!val) {
			val = ULINT_UNDEFINED;
1510 1511 1512
		} else if (UNIV_UNLIKELY(val >= n)) {
			page_zip_fields_free(index);
			index = NULL;
1513 1514
		} else {
			index->type = DICT_CLUSTERED;
1515 1516 1517
		}

		*trx_id_col = val;
marko's avatar
marko committed
1518 1519 1520 1521 1522 1523 1524 1525
	} else {
		/* Decode the number of nullable fields. */
		if (UNIV_UNLIKELY(index->n_nullable > val)) {
			page_zip_fields_free(index);
			index = NULL;
		} else {
			index->n_nullable = val;
		}
1526 1527 1528 1529 1530
	}

	ut_ad(b == end);

	return(index);
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
}

/**************************************************************************
Populate the sparse page directory from the dense directory. */
static
ibool
page_zip_dir_decode(
/*================*/
					/* out: TRUE on success,
					FALSE on failure */
	const page_zip_des_t*	page_zip,/* in: dense page directory on
					compressed page */
	page_t*			page,	/* in: compact page with valid header;
					out: trailer and sparse page directory
					filled in */
	rec_t**			recs,	/* out: dense page directory sorted by
					ascending address (and heap_no) */
	rec_t**			recs_aux,/* in/out: scratch area */
1549
	ulint			n_dense)/* in: number of user records, and
1550 1551 1552 1553 1554 1555 1556 1557
					size of recs[] and recs_aux[] */
{
	ulint	i;
	ulint	n_recs;
	byte*	slot;

	n_recs = page_get_n_recs(page);

1558
	if (UNIV_UNLIKELY(n_recs > n_dense)) {
1559 1560
		page_zip_fail(("page_zip_dir_decode 1: %lu > %lu\n",
			       (ulong) n_recs, (ulong) n_dense));
1561 1562 1563
		return(FALSE);
	}

1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
	/* Traverse the list of stored records in the sorting order,
	starting from the first user record. */

	slot = page + (UNIV_PAGE_SIZE - PAGE_DIR - PAGE_DIR_SLOT_SIZE);
	UNIV_PREFETCH_RW(slot);

	/* Zero out the page trailer. */
	memset(slot + PAGE_DIR_SLOT_SIZE, 0, PAGE_DIR);

	mach_write_to_2(slot, PAGE_NEW_INFIMUM);
	slot -= PAGE_DIR_SLOT_SIZE;
	UNIV_PREFETCH_RW(slot);

	/* Initialize the sparse directory and copy the dense directory. */
	for (i = 0; i < n_recs; i++) {
		ulint	offs = page_zip_dir_get(page_zip, i);

		if (offs & PAGE_ZIP_DIR_SLOT_OWNED) {
			mach_write_to_2(slot, offs & PAGE_ZIP_DIR_SLOT_MASK);
			slot -= PAGE_DIR_SLOT_SIZE;
			UNIV_PREFETCH_RW(slot);
		}

1587
		if (UNIV_UNLIKELY((offs & PAGE_ZIP_DIR_SLOT_MASK)
1588
				  < PAGE_ZIP_START + REC_N_NEW_EXTRA_BYTES)) {
1589 1590 1591
			page_zip_fail(("page_zip_dir_decode 2: %u %u %lx\n",
				       (unsigned) i, (unsigned) n_recs,
				       (ulong) offs));
1592 1593
			return(FALSE);
		}
1594

1595 1596 1597 1598
		recs[i] = page + (offs & PAGE_ZIP_DIR_SLOT_MASK);
	}

	mach_write_to_2(slot, PAGE_NEW_SUPREMUM);
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
	{
		const page_dir_slot_t*	last_slot = page_dir_get_nth_slot(
			page, page_dir_get_n_slots(page) - 1);

		if (UNIV_UNLIKELY(slot != last_slot)) {
			page_zip_fail(("page_zip_dir_decode 3: %p != %p\n",
				       (const void*) slot,
				       (const void*) last_slot));
			return(FALSE);
		}
1609 1610 1611
	}

	/* Copy the rest of the dense directory. */
1612
	for (; i < n_dense; i++) {
1613 1614 1615
		ulint	offs = page_zip_dir_get(page_zip, i);

		if (UNIV_UNLIKELY(offs & ~PAGE_ZIP_DIR_SLOT_MASK)) {
1616 1617 1618
			page_zip_fail(("page_zip_dir_decode 4: %u %u %lx\n",
				       (unsigned) i, (unsigned) n_dense,
				       (ulong) offs));
1619 1620 1621 1622 1623 1624
			return(FALSE);
		}

		recs[i] = page + offs;
	}

1625
	if (UNIV_LIKELY(n_dense > 1)) {
1626
		page_zip_dir_sort(recs, recs_aux, 0, n_dense);
1627 1628 1629 1630
	}
	return(TRUE);
}

1631 1632
/**************************************************************************
Initialize the REC_N_NEW_EXTRA_BYTES of each record. */
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
static
ibool
page_zip_set_extra_bytes(
/*=====================*/
					/* out: TRUE on success,
					FALSE on failure */
	const page_zip_des_t*	page_zip,/* in: compressed page */
	page_t*			page,	/* in/out: uncompressed page */
	ulint			info_bits)/* in: REC_INFO_MIN_REC_FLAG or 0 */
{
	ulint	n;
	ulint	i;
	ulint	n_owned = 1;
	ulint	offs;
	rec_t*	rec;

	n = page_get_n_recs(page);
	rec = page + PAGE_NEW_INFIMUM;

	for (i = 0; i < n; i++) {
		offs = page_zip_dir_get(page_zip, i);

		if (UNIV_UNLIKELY(offs & PAGE_ZIP_DIR_SLOT_DEL)) {
			info_bits |= REC_INFO_DELETED_FLAG;
		}
		if (UNIV_UNLIKELY(offs & PAGE_ZIP_DIR_SLOT_OWNED)) {
			info_bits |= n_owned;
			n_owned = 1;
		} else {
			n_owned++;
		}
		offs &= PAGE_ZIP_DIR_SLOT_MASK;
1665
		if (UNIV_UNLIKELY(offs < PAGE_ZIP_START
1666
				  + REC_N_NEW_EXTRA_BYTES)) {
1667 1668 1669 1670
			page_zip_fail(("page_zip_set_extra_bytes 1:"
				       " %u %u %lx\n",
				       (unsigned) i, (unsigned) n,
				       (ulong) offs));
1671 1672
			return(FALSE);
		}
1673

1674
		rec_set_next_offs_new(rec, offs);
1675
		rec = page + offs;
1676
		rec[-REC_N_NEW_EXTRA_BYTES] = (byte) info_bits;
1677
		info_bits = 0;
1678 1679 1680
	}

	/* Set the next pointer of the last user record. */
1681
	rec_set_next_offs_new(rec, PAGE_NEW_SUPREMUM);
1682 1683

	/* Set n_owned of the supremum record. */
1684
	page[PAGE_NEW_SUPREMUM - REC_N_NEW_EXTRA_BYTES] = (byte) n_owned;
1685

1686
	/* The dense directory excludes the infimum and supremum records. */
1687
	n = page_dir_get_n_heap(page) - PAGE_HEAP_NO_USER_LOW;
1688 1689

	if (i >= n) {
1690 1691 1692
		if (UNIV_LIKELY(i == n)) {
			return(TRUE);
		}
1693

1694 1695 1696
		page_zip_fail(("page_zip_set_extra_bytes 2: %u != %u\n",
			       (unsigned) i, (unsigned) n));
		return(FALSE);
1697 1698 1699 1700 1701 1702 1703 1704
	}

	offs = page_zip_dir_get(page_zip, i);

	/* Set the extra bytes of deleted records on the free list. */
	for (;;) {
		if (UNIV_UNLIKELY(!offs)
		    || UNIV_UNLIKELY(offs & ~PAGE_ZIP_DIR_SLOT_MASK)) {
1705 1706 1707

			page_zip_fail(("page_zip_set_extra_bytes 3: %lx\n",
				       (ulong) offs));
1708 1709 1710 1711 1712 1713
			return(FALSE);
		}

		rec = page + offs;
		rec[-REC_N_NEW_EXTRA_BYTES] = 0; /* info_bits and n_owned */

1714 1715 1716 1717 1718
		if (++i == n) {
			break;
		}

		offs = page_zip_dir_get(page_zip, i);
1719
		rec_set_next_offs_new(rec, offs);
1720 1721 1722 1723
	}

	/* Terminate the free list. */
	rec[-REC_N_NEW_EXTRA_BYTES] = 0; /* info_bits and n_owned */
1724
	rec_set_next_offs_new(rec, 0);
1725

1726
	return(TRUE);
1727 1728
}

1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
/**************************************************************************
Apply the modification log to a record containing externally stored
columns.  Do not copy the fields that are stored separately. */
static
const byte*
page_zip_apply_log_ext(
/*===================*/
					/* out: pointer to modification log,
					or NULL on failure */
	rec_t*		rec,		/* in/out: record */
	const ulint*	offsets,	/* in: rec_get_offsets(rec) */
	ulint		trx_id_col,	/* in: position of of DB_TRX_ID */
	const byte*	data,		/* in: modification log */
	const byte*	end)		/* in: end of modification log */
{
	ulint	i;
	ulint	len;
	byte*	next_out = rec;

	/* Check if there are any externally stored columns.
	For each externally stored column, skip the
	BTR_EXTERN_FIELD_REF. */

	for (i = 0; i < rec_offs_n_fields(offsets); i++) {
		byte*	dst;

		if (UNIV_UNLIKELY(i == trx_id_col)) {
			/* Skip trx_id and roll_ptr */
			dst = rec_get_nth_field(rec, offsets,
						i, &len);
1759
			if (UNIV_UNLIKELY(dst - next_out >= end - data)
1760
			    || UNIV_UNLIKELY
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
			    (len < (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN))
			    || rec_offs_nth_extern(offsets, i)) {
				page_zip_fail(("page_zip_apply_log_ext:"
					       " trx_id len %lu,"
					       " %p - %p >= %p - %p\n",
					       (ulong) len,
					       (const void*) dst,
					       (const void*) next_out,
					       (const void*) end,
					       (const void*) data));
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
				return(NULL);
			}

			memcpy(next_out, data, dst - next_out);
			data += dst - next_out;
			next_out = dst + (DATA_TRX_ID_LEN
					  + DATA_ROLL_PTR_LEN);
		} else if (rec_offs_nth_extern(offsets, i)) {
			dst = rec_get_nth_field(rec, offsets,
						i, &len);
			ut_ad(len
			      >= BTR_EXTERN_FIELD_REF_SIZE);

			len += dst - next_out
				- BTR_EXTERN_FIELD_REF_SIZE;

			if (UNIV_UNLIKELY(data + len >= end)) {
1788 1789 1790 1791 1792
				page_zip_fail(("page_zip_apply_log_ext: "
					       "ext %p+%lu >= %p\n",
					       (const void*) data,
					       (ulong) len,
					       (const void*) end));
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
				return(NULL);
			}

			memcpy(next_out, data, len);
			data += len;
			next_out += len
				+ BTR_EXTERN_FIELD_REF_SIZE;
		}
	}

	/* Copy the last bytes of the record. */
	len = rec_get_end(rec, offsets) - next_out;
	if (UNIV_UNLIKELY(data + len >= end)) {
1806 1807 1808 1809 1810
		page_zip_fail(("page_zip_apply_log_ext: "
			       "last %p+%lu >= %p\n",
			       (const void*) data,
			       (ulong) len,
			       (const void*) end));
1811 1812 1813 1814 1815 1816 1817 1818
		return(NULL);
	}
	memcpy(next_out, data, len);
	data += len;

	return(data);
}

1819
/**************************************************************************
1820 1821
Apply the modification log to an uncompressed page.
Do not copy the fields that are stored separately. */
1822 1823 1824 1825 1826 1827 1828
static
const byte*
page_zip_apply_log(
/*===============*/
				/* out: pointer to end of modification log,
				or NULL on failure */
	const byte*	data,	/* in: modification log */
1829
	ulint		size,	/* in: maximum length of the log, in bytes */
1830
	rec_t**		recs,	/* in: dense page directory,
1831 1832
				sorted by address (indexed by
				heap_no - PAGE_HEAP_NO_USER_LOW) */
1833
	ulint		n_dense,/* in: size of recs[] */
1834 1835
	ulint		trx_id_col,/* in: column number of trx_id in the index,
				or ULINT_UNDEFINED if none */
1836 1837 1838 1839 1840 1841
	ulint		heap_status,
				/* in: heap_no and status bits for
				the next record to uncompress */
	dict_index_t*	index,	/* in: index of the page */
	ulint*		offsets)/* in/out: work area for
				rec_get_offsets_reverse() */
1842
{
1843 1844
	const byte* const end = data + size;

1845
	for (;;) {
1846
		ulint	val;
1847 1848 1849 1850
		rec_t*	rec;
		ulint	len;
		ulint	hs;

1851 1852
		val = *data++;
		if (UNIV_UNLIKELY(!val)) {
1853
			return(data - 1);
1854
		}
1855
		if (val & 0x80) {
1856
			val = (val & 0x7f) << 8 | *data++;
1857
			if (UNIV_UNLIKELY(!val)) {
1858 1859 1860
				page_zip_fail(("page_zip_apply_log:"
					       " invalid val %x%x\n",
					       data[-2], data[-1]));
1861 1862
				return(NULL);
			}
1863 1864
		}
		if (UNIV_UNLIKELY(data >= end)) {
1865 1866 1867
			page_zip_fail(("page_zip_apply_log: %p >= %p\n",
				       (const void*) data,
				       (const void*) end));
1868 1869
			return(NULL);
		}
1870
		if (UNIV_UNLIKELY((val >> 1) > n_dense)) {
1871 1872
			page_zip_fail(("page_zip_apply_log: %lu>>1 > %lu\n",
				       (ulong) val, (ulong) n_dense));
1873 1874
			return(NULL);
		}
1875 1876

		/* Determine the heap number and status bits of the record. */
1877 1878 1879
		rec = recs[(val >> 1) - 1];

		hs = ((val >> 1) + 1) << REC_HEAP_NO_SHIFT;
1880 1881 1882 1883 1884 1885 1886
		hs |= heap_status & ((1 << REC_HEAP_NO_SHIFT) - 1);

		/* This may either be an old record that is being
		overwritten (updated in place, or allocated from
		the free list), or a new record, with the next
		available_heap_no. */
		if (UNIV_UNLIKELY(hs > heap_status)) {
1887 1888
			page_zip_fail(("page_zip_apply_log: %lu > %lu\n",
				       (ulong) hs, (ulong) heap_status));
1889 1890 1891
			return(NULL);
		} else if (hs == heap_status) {
			/* A new record was allocated from the heap. */
1892 1893
			if (UNIV_UNLIKELY(val & 1)) {
				/* Only existing records may be cleared. */
1894 1895 1896 1897
				page_zip_fail(("page_zip_apply_log:"
					       " attempting to create"
					       " deleted rec %lu\n",
					       (ulong) hs));
1898 1899
				return(NULL);
			}
marko's avatar
marko committed
1900
			heap_status += 1 << REC_HEAP_NO_SHIFT;
1901 1902
		}

marko's avatar
marko committed
1903
		mach_write_to_2(rec - REC_NEW_HEAP_NO, hs);
1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918

		if (val & 1) {
			/* Clear the data bytes of the record. */
			mem_heap_t*	heap	= NULL;
			ulint*		offs;
			offs = rec_get_offsets(rec, index, offsets,
					       ULINT_UNDEFINED, &heap);
			memset(rec, 0, rec_offs_data_size(offs));

			if (UNIV_LIKELY_NULL(heap)) {
				mem_heap_free(heap);
			}
			continue;
		}

marko's avatar
marko committed
1919 1920 1921
#if REC_STATUS_NODE_PTR != TRUE
# error "REC_STATUS_NODE_PTR != TRUE"
#endif
1922
		rec_get_offsets_reverse(data, index,
1923 1924
					hs & REC_STATUS_NODE_PTR,
					offsets);
marko's avatar
marko committed
1925
		rec_offs_make_valid(rec, index, offsets);
1926 1927 1928

		/* Copy the extra bytes (backwards). */
		{
1929 1930 1931 1932
			byte*	start	= rec_get_start(rec, offsets);
			byte*	b	= rec - REC_N_NEW_EXTRA_BYTES;
			while (b != start) {
				*--b = *data++;
1933 1934 1935 1936
			}
		}

		/* Copy the data bytes. */
1937
		if (UNIV_UNLIKELY(rec_offs_any_extern(offsets))) {
1938 1939
			/* Non-leaf nodes should not contain any
			externally stored columns. */
1940
			if (UNIV_UNLIKELY(hs & REC_STATUS_NODE_PTR)) {
1941 1942 1943
				page_zip_fail(("page_zip_apply_log: "
					       "%lu&REC_STATUS_NODE_PTR\n",
					       (ulong) hs));
1944 1945
				return(NULL);
			}
1946

1947 1948 1949 1950 1951 1952 1953
			data = page_zip_apply_log_ext(
				rec, offsets, trx_id_col, data, end);

			if (UNIV_UNLIKELY(!data)) {
				return(NULL);
			}
		} else if (UNIV_UNLIKELY(hs & REC_STATUS_NODE_PTR)) {
1954
			len = rec_offs_data_size(offsets)
1955
				- REC_NODE_PTR_SIZE;
1956 1957
			/* Copy the data bytes, except node_ptr. */
			if (UNIV_UNLIKELY(data + len >= end)) {
1958
				page_zip_fail(("page_zip_apply_log: "
1959
					       "node_ptr %p+%lu >= %p\n",
1960 1961 1962
					       (const void*) data,
					       (ulong) len,
					       (const void*) end));
1963 1964 1965 1966
				return(NULL);
			}
			memcpy(rec, data, len);
			data += len;
1967 1968
		} else if (UNIV_LIKELY(trx_id_col == ULINT_UNDEFINED)) {
			len = rec_offs_data_size(offsets);
1969

1970 1971 1972
			/* Copy all data bytes of
			a record in a secondary index. */
			if (UNIV_UNLIKELY(data + len >= end)) {
1973
				page_zip_fail(("page_zip_apply_log: "
1974
					       "sec %p+%lu >= %p\n",
1975 1976 1977
					       (const void*) data,
					       (ulong) len,
					       (const void*) end));
1978 1979
				return(NULL);
			}
1980

1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
			memcpy(rec, data, len);
			data += len;
		} else {
			/* Skip DB_TRX_ID and DB_ROLL_PTR. */
			ulint	l = rec_get_nth_field_offs(offsets,
							   trx_id_col, &len);
			byte*	b;

			if (UNIV_UNLIKELY(data + l >= end)
			    || UNIV_UNLIKELY(len < (DATA_TRX_ID_LEN
						    + DATA_ROLL_PTR_LEN))) {
1992
				page_zip_fail(("page_zip_apply_log: "
1993
					       "trx_id %p+%lu >= %p\n",
1994 1995 1996
					       (const void*) data,
					       (ulong) l,
					       (const void*) end));
1997
				return(NULL);
1998 1999
			}

2000 2001 2002 2003 2004 2005 2006
			/* Copy any preceding data bytes. */
			memcpy(rec, data, l);
			data += l;

			/* Copy any bytes following DB_TRX_ID, DB_ROLL_PTR. */
			b = rec + l + (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
			len = rec_get_end(rec, offsets) - b;
2007
			if (UNIV_UNLIKELY(data + len >= end)) {
2008
				page_zip_fail(("page_zip_apply_log: "
2009
					       "clust %p+%lu >= %p\n",
2010 2011 2012
					       (const void*) data,
					       (ulong) len,
					       (const void*) end));
2013 2014
				return(NULL);
			}
2015
			memcpy(b, data, len);
2016 2017
			data += len;
		}
2018 2019 2020
	}
}

marko's avatar
marko committed
2021
/**************************************************************************
2022 2023
Decompress the records of a node pointer page. */
static
marko's avatar
marko committed
2024
ibool
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
page_zip_decompress_node_ptrs(
/*==========================*/
					/* out: TRUE on success,
					FALSE on failure */
	page_zip_des_t*	page_zip,	/* in/out: compressed page */
	z_stream*	d_stream,	/* in/out: compressed page stream */
	rec_t**		recs,		/* in: dense page directory
					sorted by address */
	ulint		n_dense,	/* in: size of recs[] */
	dict_index_t*	index,		/* in: the index of the page */
	ulint*		offsets,	/* in/out: temporary offsets */
	mem_heap_t*	heap)		/* in: temporary memory heap */
marko's avatar
marko committed
2037
{
2038
	ulint		heap_status = REC_STATUS_NODE_PTR
2039
		| PAGE_HEAP_NO_USER_LOW << REC_HEAP_NO_SHIFT;
2040
	ulint		slot;
2041
	const byte*	storage;
marko's avatar
marko committed
2042

2043 2044 2045
	/* Subtract the space reserved for uncompressed data. */
	d_stream->avail_in -= n_dense
		* (PAGE_ZIP_DIR_SLOT_SIZE + REC_NODE_PTR_SIZE);
marko's avatar
marko committed
2046

2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
	/* Decompress the records in heap_no order. */
	for (slot = 0; slot < n_dense; slot++) {
		rec_t*	rec = recs[slot];

		d_stream->avail_out = rec - REC_N_NEW_EXTRA_BYTES
			- d_stream->next_out;

		ut_ad(d_stream->avail_out < UNIV_PAGE_SIZE
		      - PAGE_ZIP_START - PAGE_DIR);
		switch (inflate(d_stream, Z_SYNC_FLUSH)) {
		case Z_STREAM_END:
			/* Apparently, n_dense has grown
			since the time the page was last compressed. */
			goto zlib_done;
		case Z_OK:
		case Z_BUF_ERROR:
			if (!d_stream->avail_out) {
				break;
			}
			/* fall through */
		default:
2068 2069 2070
			page_zip_fail(("page_zip_decompress_node_ptrs:"
				       " 1 inflate(Z_SYNC_FLUSH)=%s\n",
				       d_stream->msg));
2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
			goto zlib_error;
		}

		ut_ad(d_stream->next_out == rec - REC_N_NEW_EXTRA_BYTES);
		/* Prepare to decompress the data bytes. */
		d_stream->next_out = rec;
		/* Set heap_no and the status bits. */
		mach_write_to_2(rec - REC_NEW_HEAP_NO, heap_status);
		heap_status += 1 << REC_HEAP_NO_SHIFT;

		/* Read the offsets. The status bits are needed here. */
		offsets = rec_get_offsets(rec, index, offsets,
					  ULINT_UNDEFINED, &heap);

		/* Non-leaf nodes should not have any externally
		stored columns. */
		ut_ad(!rec_offs_any_extern(offsets));

		/* Decompress the data bytes, except node_ptr. */
		d_stream->avail_out = rec_offs_data_size(offsets)
			- REC_NODE_PTR_SIZE;

		switch (inflate(d_stream, Z_SYNC_FLUSH)) {
		case Z_STREAM_END:
			goto zlib_done;
		case Z_OK:
		case Z_BUF_ERROR:
			if (!d_stream->avail_out) {
				break;
			}
			/* fall through */
		default:
2103 2104 2105
			page_zip_fail(("page_zip_decompress_node_ptrs:"
				       " 2 inflate(Z_SYNC_FLUSH)=%s\n",
				       d_stream->msg));
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
			goto zlib_error;
		}

		/* Clear the node pointer in case the record
		will be deleted and the space will be reallocated
		to a smaller record. */
		memset(d_stream->next_out, 0, REC_NODE_PTR_SIZE);
		d_stream->next_out += REC_NODE_PTR_SIZE;

		ut_ad(d_stream->next_out == rec_get_end(rec, offsets));
2116
	}
2117

2118 2119 2120 2121 2122 2123 2124
	/* Decompress any trailing garbage, in case the last record was
	allocated from an originally longer space on the free list. */
	d_stream->avail_out = page_header_get_field(page_zip->data,
						    PAGE_HEAP_TOP)
		- page_offset(d_stream->next_out);
	if (UNIV_UNLIKELY(d_stream->avail_out > UNIV_PAGE_SIZE
			  - PAGE_ZIP_START - PAGE_DIR)) {
2125

2126 2127 2128
		page_zip_fail(("page_zip_decompress_node_ptrs:"
			       " avail_out = %u\n",
			       d_stream->avail_out));
2129 2130
		goto zlib_error;
	}
2131

2132
	if (UNIV_UNLIKELY(inflate(d_stream, Z_FINISH) != Z_STREAM_END)) {
2133 2134 2135
		page_zip_fail(("page_zip_decompress_node_ptrs:"
			       " inflate(Z_FINISH)=%s\n",
			       d_stream->msg));
2136 2137
zlib_error:
		inflateEnd(d_stream);
2138 2139 2140
		return(FALSE);
	}

2141 2142 2143 2144 2145 2146
	/* Note that d_stream->avail_out > 0 may hold here
	if the modification log is nonempty. */

zlib_done:
	if (UNIV_UNLIKELY(inflateEnd(d_stream) != Z_OK)) {
		ut_error;
2147 2148
	}

2149 2150
	{
		page_t*	page = page_align(d_stream->next_out);
marko's avatar
marko committed
2151

2152 2153 2154 2155 2156
		/* Clear the unused heap space on the uncompressed page. */
		memset(d_stream->next_out, 0,
		       page_dir_get_nth_slot(page,
					     page_dir_get_n_slots(page) - 1)
		       - d_stream->next_out);
marko's avatar
marko committed
2157
	}
marko's avatar
marko committed
2158

2159
#ifdef UNIV_DEBUG
2160
	page_zip->m_start = PAGE_DATA + d_stream->total_in;
2161
#endif /* UNIV_DEBUG */
2162

2163 2164 2165
	/* Apply the modification log. */
	{
		const byte*	mod_log_ptr;
2166
		mod_log_ptr = page_zip_apply_log(d_stream->next_in,
2167 2168 2169 2170
						 d_stream->avail_in + 1,
						 recs, n_dense,
						 ULINT_UNDEFINED, heap_status,
						 index, offsets);
2171

2172 2173 2174 2175
		if (UNIV_UNLIKELY(!mod_log_ptr)) {
			return(FALSE);
		}
		page_zip->m_end = mod_log_ptr - page_zip->data;
2176
		page_zip->m_nonempty = mod_log_ptr != d_stream->next_in;
2177 2178 2179 2180 2181 2182 2183 2184 2185
	}

	if (UNIV_UNLIKELY
	    (page_zip_get_trailer_len(page_zip,
				      dict_index_is_clust(index), NULL)
	     + page_zip->m_end >= page_zip_get_size(page_zip))) {
		page_zip_fail(("page_zip_decompress_node_ptrs:"
			       " %lu + %lu >= %lu, %lu\n",
			       (ulong) page_zip_get_trailer_len(
2186 2187
				       page_zip, dict_index_is_clust(index),
				       NULL),
2188 2189
			       (ulong) page_zip->m_end,
			       (ulong) page_zip_get_size(page_zip),
2190
			       (ulong) dict_index_is_clust(index)));
2191
		return(FALSE);
2192
	}
2193

2194
	/* Restore the uncompressed columns in heap_no order. */
2195
	storage	= page_zip->data + page_zip_get_size(page_zip)
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209
		- n_dense * PAGE_ZIP_DIR_SLOT_SIZE;

	for (slot = 0; slot < n_dense; slot++) {
		rec_t*		rec	= recs[slot];

		offsets = rec_get_offsets(rec, index, offsets,
					  ULINT_UNDEFINED, &heap);
		/* Non-leaf nodes should not have any externally
		stored columns. */
		ut_ad(!rec_offs_any_extern(offsets));
		storage -= REC_NODE_PTR_SIZE;

		memcpy(rec_get_end(rec, offsets) - REC_NODE_PTR_SIZE,
		       storage, REC_NODE_PTR_SIZE);
2210 2211
	}

2212 2213
	return(TRUE);
}
2214

2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
/**************************************************************************
Decompress the records of a leaf node of a secondary index. */
static
ibool
page_zip_decompress_sec(
/*====================*/
					/* out: TRUE on success,
					FALSE on failure */
	page_zip_des_t*	page_zip,	/* in/out: compressed page */
	z_stream*	d_stream,	/* in/out: compressed page stream */
	rec_t**		recs,		/* in: dense page directory
					sorted by address */
	ulint		n_dense,	/* in: size of recs[] */
	dict_index_t*	index,		/* in: the index of the page */
	ulint*		offsets)	/* in/out: temporary offsets */
{
2231 2232
	ulint	heap_status	= REC_STATUS_ORDINARY
		| PAGE_HEAP_NO_USER_LOW << REC_HEAP_NO_SHIFT;
2233 2234
	ulint	slot;

2235 2236
	ut_a(!dict_index_is_clust(index));

2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259
	/* Subtract the space reserved for uncompressed data. */
	d_stream->avail_in -= n_dense * PAGE_ZIP_DIR_SLOT_SIZE;

	for (slot = 0; slot < n_dense; slot++) {
		rec_t*	rec = recs[slot];

		/* Decompress everything up to this record. */
		d_stream->avail_out = rec - REC_N_NEW_EXTRA_BYTES
			- d_stream->next_out;

		if (UNIV_LIKELY(d_stream->avail_out)) {
			switch (inflate(d_stream, Z_SYNC_FLUSH)) {
			case Z_STREAM_END:
				/* Apparently, n_dense has grown
				since the time the page was last compressed. */
				goto zlib_done;
			case Z_OK:
			case Z_BUF_ERROR:
				if (!d_stream->avail_out) {
					break;
				}
				/* fall through */
			default:
2260 2261 2262
				page_zip_fail(("page_zip_decompress_sec:"
					       " inflate(Z_SYNC_FLUSH)=%s\n",
					       d_stream->msg));
2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285
				goto zlib_error;
			}
		}

		ut_ad(d_stream->next_out == rec - REC_N_NEW_EXTRA_BYTES);

		/* Skip the REC_N_NEW_EXTRA_BYTES. */

		d_stream->next_out = rec;

		/* Set heap_no and the status bits. */
		mach_write_to_2(rec - REC_NEW_HEAP_NO, heap_status);
		heap_status += 1 << REC_HEAP_NO_SHIFT;
	}

	/* Decompress the data of the last record and any trailing garbage,
	in case the last record was allocated from an originally longer space
	on the free list. */
	d_stream->avail_out = page_header_get_field(page_zip->data,
						    PAGE_HEAP_TOP)
		- page_offset(d_stream->next_out);
	if (UNIV_UNLIKELY(d_stream->avail_out > UNIV_PAGE_SIZE
			  - PAGE_ZIP_START - PAGE_DIR)) {
2286

2287 2288 2289
		page_zip_fail(("page_zip_decompress_sec:"
			       " avail_out = %u\n",
			       d_stream->avail_out));
2290 2291 2292
		goto zlib_error;
	}

2293
	if (UNIV_UNLIKELY(inflate(d_stream, Z_FINISH) != Z_STREAM_END)) {
2294 2295 2296
		page_zip_fail(("page_zip_decompress_sec:"
			       " inflate(Z_FINISH)=%s\n",
			       d_stream->msg));
2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
zlib_error:
		inflateEnd(d_stream);
		return(FALSE);
	}

	/* Note that d_stream->avail_out > 0 may hold here
	if the modification log is nonempty. */

zlib_done:
	if (UNIV_UNLIKELY(inflateEnd(d_stream) != Z_OK)) {
		ut_error;
	}
2309 2310

	{
2311
		page_t*	page = page_align(d_stream->next_out);
2312

2313 2314 2315 2316 2317 2318
		/* Clear the unused heap space on the uncompressed page. */
		memset(d_stream->next_out, 0,
		       page_dir_get_nth_slot(page,
					     page_dir_get_n_slots(page) - 1)
		       - d_stream->next_out);
	}
2319

2320
#ifdef UNIV_DEBUG
2321
	page_zip->m_start = PAGE_DATA + d_stream->total_in;
2322
#endif /* UNIV_DEBUG */
2323 2324 2325 2326

	/* Apply the modification log. */
	{
		const byte*	mod_log_ptr;
2327
		mod_log_ptr = page_zip_apply_log(d_stream->next_in,
2328 2329 2330 2331 2332 2333 2334 2335 2336
						 d_stream->avail_in + 1,
						 recs, n_dense,
						 ULINT_UNDEFINED, heap_status,
						 index, offsets);

		if (UNIV_UNLIKELY(!mod_log_ptr)) {
			return(FALSE);
		}
		page_zip->m_end = mod_log_ptr - page_zip->data;
2337
		page_zip->m_nonempty = mod_log_ptr != d_stream->next_in;
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
	}

	if (UNIV_UNLIKELY(page_zip_get_trailer_len(page_zip, FALSE, NULL)
			  + page_zip->m_end >= page_zip_get_size(page_zip))) {

		page_zip_fail(("page_zip_decompress_sec: %lu + %lu >= %lu\n",
			       (ulong) page_zip_get_trailer_len(
				       page_zip, FALSE, NULL),
			       (ulong) page_zip->m_end,
			       (ulong) page_zip_get_size(page_zip)));
		return(FALSE);
2349 2350
	}

2351 2352 2353 2354 2355 2356
	/* There are no uncompressed columns on leaf pages of
	secondary indexes. */

	return(TRUE);
}

2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379
/**************************************************************************
Decompress a record of a leaf node of a clustered index that contains
externally stored columns. */
static
ibool
page_zip_decompress_clust_ext(
/*==========================*/
					/* out: TRUE on success */
	z_stream*	d_stream,	/* in/out: compressed page stream */
	rec_t*		rec,		/* in/out: record */
	const ulint*	offsets,	/* in: rec_get_offsets(rec) */
	ulint		trx_id_col)	/* in: position of of DB_TRX_ID */
{
	ulint	i;

	for (i = 0; i < rec_offs_n_fields(offsets); i++) {
		ulint	len;
		byte*	dst;

		if (UNIV_UNLIKELY(i == trx_id_col)) {
			/* Skip trx_id and roll_ptr */
			dst = rec_get_nth_field(rec, offsets, i, &len);
			if (UNIV_UNLIKELY(len < DATA_TRX_ID_LEN
2380
					  + DATA_ROLL_PTR_LEN)) {
2381

2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
				page_zip_fail(("page_zip_decompress_clust_ext:"
					       " len[%lu] = %lu\n",
					       (ulong) i, (ulong) len));
				return(FALSE);
			}

			if (rec_offs_nth_extern(offsets, i)) {

				page_zip_fail(("page_zip_decompress_clust_ext:"
					       " DB_TRX_ID at %lu is ext\n",
					       (ulong) i));
2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406
				return(FALSE);
			}

			d_stream->avail_out = dst - d_stream->next_out;

			switch (inflate(d_stream, Z_SYNC_FLUSH)) {
			case Z_STREAM_END:
			case Z_OK:
			case Z_BUF_ERROR:
				if (!d_stream->avail_out) {
					break;
				}
				/* fall through */
			default:
2407 2408 2409
				page_zip_fail(("page_zip_decompress_clust_ext:"
					       " 1 inflate(Z_SYNC_FLUSH)=%s\n",
					       d_stream->msg));
2410 2411 2412 2413 2414
				return(FALSE);
			}

			ut_ad(d_stream->next_out == dst);

2415 2416 2417 2418 2419
			/* Clear DB_TRX_ID and DB_ROLL_PTR in order to
			avoid uninitialized bytes in case the record
			is affected by page_zip_apply_log(). */
			memset(dst, 0, DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);

2420 2421 2422 2423 2424 2425 2426 2427
			d_stream->next_out += DATA_TRX_ID_LEN
				+ DATA_ROLL_PTR_LEN;
		} else if (rec_offs_nth_extern(offsets, i)) {
			dst = rec_get_nth_field(rec, offsets, i, &len);
			ut_ad(len >= BTR_EXTERN_FIELD_REF_SIZE);
			dst += len - BTR_EXTERN_FIELD_REF_SIZE;

			d_stream->avail_out = dst - d_stream->next_out;
2428
			switch (inflate(d_stream, Z_SYNC_FLUSH)) {
2429 2430 2431 2432 2433 2434 2435 2436
			case Z_STREAM_END:
			case Z_OK:
			case Z_BUF_ERROR:
				if (!d_stream->avail_out) {
					break;
				}
				/* fall through */
			default:
2437 2438 2439
				page_zip_fail(("page_zip_decompress_clust_ext:"
					       " 2 inflate(Z_SYNC_FLUSH)=%s\n",
					       d_stream->msg));
2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465
				return(FALSE);
			}

			ut_ad(d_stream->next_out == dst);

			/* Clear the BLOB pointer in case
			the record will be deleted and the
			space will not be reused.  Note that
			the final initialization of the BLOB
			pointers (copying from "externs"
			or clearing) will have to take place
			only after the page modification log
			has been applied.  Otherwise, we
			could end up with an uninitialized
			BLOB pointer when a record is deleted,
			reallocated and deleted. */
			memset(d_stream->next_out, 0,
			       BTR_EXTERN_FIELD_REF_SIZE);
			d_stream->next_out
				+= BTR_EXTERN_FIELD_REF_SIZE;
		}
	}

	return(TRUE);
}

2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486
/**************************************************************************
Compress the records of a leaf node of a clustered index. */
static
ibool
page_zip_decompress_clust(
/*======================*/
					/* out: TRUE on success,
					FALSE on failure */
	page_zip_des_t*	page_zip,	/* in/out: compressed page */
	z_stream*	d_stream,	/* in/out: compressed page stream */
	rec_t**		recs,		/* in: dense page directory
					sorted by address */
	ulint		n_dense,	/* in: size of recs[] */
	dict_index_t*	index,		/* in: the index of the page */
	ulint		trx_id_col,	/* index of the trx_id column */
	ulint*		offsets,	/* in/out: temporary offsets */
	mem_heap_t*	heap)		/* in: temporary memory heap */
{
	int		err;
	ulint		slot;
	ulint		heap_status	= REC_STATUS_ORDINARY
2487
		| PAGE_HEAP_NO_USER_LOW << REC_HEAP_NO_SHIFT;
2488 2489 2490
	const byte*	storage;
	const byte*	externs;

2491 2492
	ut_a(dict_index_is_clust(index));

2493 2494 2495 2496 2497
	/* Subtract the space reserved for uncompressed data. */
	d_stream->avail_in -= n_dense * (PAGE_ZIP_DIR_SLOT_SIZE
					 + DATA_TRX_ID_LEN
					 + DATA_ROLL_PTR_LEN);

2498
	/* Decompress the records in heap_no order. */
2499
	for (slot = 0; slot < n_dense; slot++) {
2500
		rec_t*	rec	= recs[slot];
2501

2502 2503
		d_stream->avail_out = rec - REC_N_NEW_EXTRA_BYTES
			- d_stream->next_out;
2504

2505
		ut_ad(d_stream->avail_out < UNIV_PAGE_SIZE
2506
		      - PAGE_ZIP_START - PAGE_DIR);
2507 2508
		err = inflate(d_stream, Z_SYNC_FLUSH);
		switch (err) {
2509 2510
		case Z_STREAM_END:
			/* Apparently, n_dense has grown
2511
			since the time the page was last compressed. */
2512
			goto zlib_done;
2513
		case Z_OK:
2514
		case Z_BUF_ERROR:
2515
			if (UNIV_LIKELY(!d_stream->avail_out)) {
2516 2517
				break;
			}
2518
			/* fall through */
2519
		default:
2520 2521 2522
			page_zip_fail(("page_zip_decompress_clust:"
				       " 1 inflate(Z_SYNC_FLUSH)=%s\n",
				       d_stream->msg));
2523 2524 2525
			goto zlib_error;
		}

2526
		ut_ad(d_stream->next_out == rec - REC_N_NEW_EXTRA_BYTES);
2527
		/* Prepare to decompress the data bytes. */
2528
		d_stream->next_out = rec;
2529 2530 2531
		/* Set heap_no and the status bits. */
		mach_write_to_2(rec - REC_NEW_HEAP_NO, heap_status);
		heap_status += 1 << REC_HEAP_NO_SHIFT;
2532

2533 2534
		/* Read the offsets. The status bits are needed here. */
		offsets = rec_get_offsets(rec, index, offsets,
2535
					  ULINT_UNDEFINED, &heap);
2536

2537
		/* This is a leaf page in a clustered index. */
2538

2539 2540 2541
		/* Check if there are any externally stored columns.
		For each externally stored column, restore the
		BTR_EXTERN_FIELD_REF separately. */
2542

2543 2544 2545 2546
		if (UNIV_UNLIKELY(rec_offs_any_extern(offsets))) {
			if (UNIV_UNLIKELY
			    (!page_zip_decompress_clust_ext(
				    d_stream, rec, offsets, trx_id_col))) {
2547

2548 2549 2550 2551 2552 2553 2554 2555 2556
				goto zlib_error;
			}
		} else {
			/* Skip trx_id and roll_ptr */
			ulint	len;
			byte*	dst = rec_get_nth_field(rec, offsets,
							trx_id_col, &len);
			if (UNIV_UNLIKELY(len < DATA_TRX_ID_LEN
					  + DATA_ROLL_PTR_LEN)) {
2557

2558 2559
				page_zip_fail(("page_zip_decompress_clust:"
					       " len = %lu\n", (ulong) len));
2560 2561
				goto zlib_error;
			}
2562

2563
			d_stream->avail_out = dst - d_stream->next_out;
2564

2565 2566 2567 2568 2569 2570
			switch (inflate(d_stream, Z_SYNC_FLUSH)) {
			case Z_STREAM_END:
			case Z_OK:
			case Z_BUF_ERROR:
				if (!d_stream->avail_out) {
					break;
2571
				}
2572 2573
				/* fall through */
			default:
2574 2575 2576
				page_zip_fail(("page_zip_decompress_clust:"
					       " 2 inflate(Z_SYNC_FLUSH)=%s\n",
					       d_stream->msg));
2577 2578
				goto zlib_error;
			}
2579

2580
			ut_ad(d_stream->next_out == dst);
2581

2582 2583 2584 2585 2586
			/* Clear DB_TRX_ID and DB_ROLL_PTR in order to
			avoid uninitialized bytes in case the record
			is affected by page_zip_apply_log(). */
			memset(dst, 0, DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);

2587 2588
			d_stream->next_out += DATA_TRX_ID_LEN
				+ DATA_ROLL_PTR_LEN;
2589
		}
marko's avatar
marko committed
2590

2591 2592 2593
		/* Decompress the last bytes of the record. */
		d_stream->avail_out = rec_get_end(rec, offsets)
			- d_stream->next_out;
2594

2595 2596 2597 2598 2599 2600
		switch (inflate(d_stream, Z_SYNC_FLUSH)) {
		case Z_STREAM_END:
		case Z_OK:
		case Z_BUF_ERROR:
			if (!d_stream->avail_out) {
				break;
2601
			}
2602 2603
			/* fall through */
		default:
2604 2605 2606
			page_zip_fail(("page_zip_decompress_clust:"
				       " 3 inflate(Z_SYNC_FLUSH)=%s\n",
				       d_stream->msg));
2607
			goto zlib_error;
2608
		}
2609 2610
	}

2611 2612
	/* Decompress any trailing garbage, in case the last record was
	allocated from an originally longer space on the free list. */
2613 2614 2615 2616
	d_stream->avail_out = page_header_get_field(page_zip->data,
						    PAGE_HEAP_TOP)
		- page_offset(d_stream->next_out);
	if (UNIV_UNLIKELY(d_stream->avail_out > UNIV_PAGE_SIZE
2617
			  - PAGE_ZIP_START - PAGE_DIR)) {
2618

2619 2620 2621
		page_zip_fail(("page_zip_decompress_clust:"
			       " avail_out = %u\n",
			       d_stream->avail_out));
2622 2623 2624
		goto zlib_error;
	}

2625
	if (UNIV_UNLIKELY(inflate(d_stream, Z_FINISH) != Z_STREAM_END)) {
2626 2627 2628
		page_zip_fail(("page_zip_decompress_clust:"
			       " inflate(Z_FINISH)=%s\n",
			       d_stream->msg));
2629
zlib_error:
2630 2631
		inflateEnd(d_stream);
		return(FALSE);
2632 2633
	}

2634
	/* Note that d_stream->avail_out > 0 may hold here
2635
	if the modification log is nonempty. */
2636 2637

zlib_done:
2638
	if (UNIV_UNLIKELY(inflateEnd(d_stream) != Z_OK)) {
marko's avatar
marko committed
2639 2640
		ut_error;
	}
2641

2642 2643
	{
		page_t*	page = page_align(d_stream->next_out);
2644

2645 2646 2647 2648 2649 2650
		/* Clear the unused heap space on the uncompressed page. */
		memset(d_stream->next_out, 0,
		       page_dir_get_nth_slot(page,
					     page_dir_get_n_slots(page) - 1)
		       - d_stream->next_out);
	}
2651

2652
#ifdef UNIV_DEBUG
2653
	page_zip->m_start = PAGE_DATA + d_stream->total_in;
2654
#endif /* UNIV_DEBUG */
marko's avatar
marko committed
2655 2656

	/* Apply the modification log. */
2657 2658
	{
		const byte*	mod_log_ptr;
2659
		mod_log_ptr = page_zip_apply_log(d_stream->next_in,
2660
						 d_stream->avail_in + 1,
2661 2662 2663
						 recs, n_dense,
						 trx_id_col, heap_status,
						 index, offsets);
2664

2665
		if (UNIV_UNLIKELY(!mod_log_ptr)) {
2666
			return(FALSE);
marko's avatar
marko committed
2667
		}
2668
		page_zip->m_end = mod_log_ptr - page_zip->data;
2669
		page_zip->m_nonempty = mod_log_ptr != d_stream->next_in;
2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680
	}

	if (UNIV_UNLIKELY(page_zip_get_trailer_len(page_zip, TRUE, NULL)
			  + page_zip->m_end >= page_zip_get_size(page_zip))) {

		page_zip_fail(("page_zip_decompress_clust: %lu + %lu >= %lu\n",
			       (ulong) page_zip_get_trailer_len(
				       page_zip, TRUE, NULL),
			       (ulong) page_zip->m_end,
			       (ulong) page_zip_get_size(page_zip)));
		return(FALSE);
marko's avatar
marko committed
2681 2682
	}

2683
	storage = page_zip->data + page_zip_get_size(page_zip)
2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711
		- n_dense * PAGE_ZIP_DIR_SLOT_SIZE;

	externs = storage - n_dense
		* (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);

	/* Restore the uncompressed columns in heap_no order. */

	for (slot = 0; slot < n_dense; slot++) {
		ulint	i;
		ulint	len;
		byte*	dst;
		rec_t*	rec	= recs[slot];
		ibool	exists	= !page_zip_dir_find_free(
			page_zip, page_offset(rec));
		offsets = rec_get_offsets(rec, index, offsets,
					  ULINT_UNDEFINED, &heap);

		dst = rec_get_nth_field(rec, offsets,
					trx_id_col, &len);
		ut_ad(len >= DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
		storage -= DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN;
		memcpy(dst, storage,
		       DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);

		/* Check if there are any externally stored
		columns in this record.  For each externally
		stored column, restore or clear the
		BTR_EXTERN_FIELD_REF. */
2712 2713 2714
		if (!rec_offs_any_extern(offsets)) {
			continue;
		}
2715 2716 2717 2718 2719 2720

		for (i = 0; i < rec_offs_n_fields(offsets); i++) {
			if (!rec_offs_nth_extern(offsets, i)) {
				continue;
			}
			dst = rec_get_nth_field(rec, offsets, i, &len);
2721 2722 2723 2724 2725 2726 2727 2728

			if (UNIV_UNLIKELY(len < BTR_EXTERN_FIELD_REF_SIZE)) {
				page_zip_fail(("page_zip_decompress_clust:"
					       " %lu < 20\n",
					       (ulong) len));
				return(FALSE);
			}

2729 2730 2731 2732 2733 2734 2735
			dst += len - BTR_EXTERN_FIELD_REF_SIZE;

			if (UNIV_LIKELY(exists)) {
				/* Existing record:
				restore the BLOB pointer */
				externs -= BTR_EXTERN_FIELD_REF_SIZE;

2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749
				if (UNIV_UNLIKELY
				    (externs < page_zip->data
				     + page_zip->m_end)) {
					page_zip_fail(("page_zip_"
						       "decompress_clust: "
						       "%p < %p + %lu\n",
						       (const void*) externs,
						       (const void*)
						       page_zip->data,
						       (ulong)
						       page_zip->m_end));
					return(FALSE);
				}

2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769
				memcpy(dst, externs,
				       BTR_EXTERN_FIELD_REF_SIZE);

				page_zip->n_blobs++;
			} else {
				/* Deleted record:
				clear the BLOB pointer */
				memset(dst, 0,
				       BTR_EXTERN_FIELD_REF_SIZE);
			}
		}
	}

	return(TRUE);
}

/**************************************************************************
Decompress a page.  This function should tolerate errors on the compressed
page.  Instead of letting assertions fail, it will return FALSE if an
inconsistency is detected. */
2770
UNIV_INTERN
2771 2772 2773 2774
ibool
page_zip_decompress(
/*================*/
				/* out: TRUE on success, FALSE on failure */
2775
	page_zip_des_t*	page_zip,/* in: data, ssize;
2776
				out: m_start, m_end, m_nonempty, n_blobs */
2777 2778 2779 2780 2781 2782 2783 2784 2785
	page_t*		page)	/* out: uncompressed page, may be trashed */
{
	z_stream	d_stream;
	dict_index_t*	index	= NULL;
	rec_t**		recs;	/* dense page directory, sorted by address */
	ulint		n_dense;/* number of user records on the page */
	ulint		trx_id_col = ULINT_UNDEFINED;
	mem_heap_t*	heap;
	ulint*		offsets;
2786
	ullint		usec = ut_time_us(NULL);
2787 2788

	ut_ad(page_zip_simple_validate(page_zip));
2789 2790
	UNIV_MEM_ASSERT_W(page, UNIV_PAGE_SIZE);
	UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip));
2791 2792

	/* The dense directory excludes the infimum and supremum records. */
2793
	n_dense = page_dir_get_n_heap(page_zip->data) - PAGE_HEAP_NO_USER_LOW;
2794
	if (UNIV_UNLIKELY(n_dense * PAGE_ZIP_DIR_SLOT_SIZE
2795
			  >= page_zip_get_size(page_zip))) {
2796 2797 2798
		page_zip_fail(("page_zip_decompress 1: %lu %lu\n",
			       (ulong) n_dense,
			       (ulong) page_zip_get_size(page_zip)));
2799 2800 2801
		return(FALSE);
	}

2802
	heap = mem_heap_create(n_dense * (3 * sizeof *recs) + UNIV_PAGE_SIZE);
2803 2804 2805
	recs = mem_heap_alloc(heap, n_dense * (2 * sizeof *recs));

#ifdef UNIV_ZIP_DEBUG
2806 2807
	/* Clear the page. */
	memset(page, 0x55, UNIV_PAGE_SIZE);
2808
#endif /* UNIV_ZIP_DEBUG */
2809
	UNIV_MEM_INVALID(page, UNIV_PAGE_SIZE);
2810 2811 2812 2813 2814 2815 2816
	/* Copy the page header. */
	memcpy(page, page_zip->data, PAGE_DATA);

	/* Copy the page directory. */
	if (UNIV_UNLIKELY(!page_zip_dir_decode(page_zip, page, recs,
					       recs + n_dense, n_dense))) {
zlib_error:
2817 2818 2819 2820
		mem_heap_free(heap);
		return(FALSE);
	}

2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834
	/* Copy the infimum and supremum records. */
	memcpy(page + (PAGE_NEW_INFIMUM - REC_N_NEW_EXTRA_BYTES),
	       infimum_extra, sizeof infimum_extra);
	if (UNIV_UNLIKELY(!page_get_n_recs(page))) {
		rec_set_next_offs_new(page + PAGE_NEW_INFIMUM,
				      PAGE_NEW_SUPREMUM);
	} else {
		rec_set_next_offs_new(page + PAGE_NEW_INFIMUM,
				      page_zip_dir_get(page_zip, 0)
				      & PAGE_ZIP_DIR_SLOT_MASK);
	}
	memcpy(page + PAGE_NEW_INFIMUM, infimum_data, sizeof infimum_data);
	memcpy(page + (PAGE_NEW_SUPREMUM - REC_N_NEW_EXTRA_BYTES + 1),
	       supremum_extra_data, sizeof supremum_extra_data);
2835

2836
	page_zip_set_alloc(&d_stream, heap);
2837

2838 2839
	if (UNIV_UNLIKELY(inflateInit2(&d_stream, UNIV_PAGE_SIZE_SHIFT)
			  != Z_OK)) {
2840
		ut_error;
2841
	}
2842

2843 2844 2845
	d_stream.next_in = page_zip->data + PAGE_DATA;
	/* Subtract the space reserved for
	the page header and the end marker of the modification log. */
2846
	d_stream.avail_in = page_zip_get_size(page_zip) - (PAGE_DATA + 1);
2847

2848 2849
	d_stream.next_out = page + PAGE_ZIP_START;
	d_stream.avail_out = UNIV_PAGE_SIZE - PAGE_ZIP_START;
2850

2851
	/* Decode the zlib header and the index information. */
2852 2853 2854 2855 2856 2857 2858 2859
	if (UNIV_UNLIKELY(inflate(&d_stream, Z_BLOCK) != Z_OK)) {

		page_zip_fail(("page_zip_decompress:"
			       " 1 inflate(Z_BLOCK)=%s\n", d_stream.msg));
		goto zlib_error;
	}

	if (UNIV_UNLIKELY(inflate(&d_stream, Z_BLOCK) != Z_OK)) {
2860

2861 2862
		page_zip_fail(("page_zip_decompress:"
			       " 2 inflate(Z_BLOCK)=%s\n", d_stream.msg));
2863 2864
		goto zlib_error;
	}
2865

2866 2867 2868
	index = page_zip_fields_decode(
		page + PAGE_ZIP_START, d_stream.next_out,
		page_is_leaf(page) ? &trx_id_col : NULL);
2869

2870
	if (UNIV_UNLIKELY(!index)) {
2871

2872 2873
		goto zlib_error;
	}
2874

2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912
	/* Decompress the user records. */
	page_zip->n_blobs = 0;
	d_stream.next_out = page + PAGE_ZIP_START;

	{
		/* Pre-allocate the offsets for rec_get_offsets_reverse(). */
		ulint	n = 1 + 1/* node ptr */ + REC_OFFS_HEADER_SIZE
			+ dict_index_get_n_fields(index);
		offsets = mem_heap_alloc(heap, n * sizeof(ulint));
		*offsets = n;
	}

	/* Decompress the records in heap_no order. */
	if (!page_is_leaf(page)) {
		/* This is a node pointer page. */
		ulint	info_bits;

		if (UNIV_UNLIKELY
		    (!page_zip_decompress_node_ptrs(page_zip, &d_stream,
						    recs, n_dense, index,
						    offsets, heap))) {
			goto err_exit;
		}

		info_bits = mach_read_from_4(page + FIL_PAGE_PREV) == FIL_NULL
			? REC_INFO_MIN_REC_FLAG : 0;

		if (UNIV_UNLIKELY(!page_zip_set_extra_bytes(page_zip, page,
							    info_bits))) {
			goto err_exit;
		}
	} else if (UNIV_LIKELY(trx_id_col == ULINT_UNDEFINED)) {
		/* This is a leaf page in a secondary index. */
		if (UNIV_UNLIKELY(!page_zip_decompress_sec(page_zip, &d_stream,
							   recs, n_dense,
							   index, offsets))) {
			goto err_exit;
		}
2913

2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929
		if (UNIV_UNLIKELY(!page_zip_set_extra_bytes(page_zip,
							    page, 0))) {
err_exit:
			page_zip_fields_free(index);
			mem_heap_free(heap);
			return(FALSE);
		}
	} else {
		/* This is a leaf page in a clustered index. */
		if (UNIV_UNLIKELY(!page_zip_decompress_clust(page_zip,
							     &d_stream, recs,
							     n_dense, index,
							     trx_id_col,
							     offsets, heap))) {
			goto err_exit;
		}
2930

2931 2932 2933 2934
		if (UNIV_UNLIKELY(!page_zip_set_extra_bytes(page_zip,
							    page, 0))) {
			goto err_exit;
		}
2935 2936
	}

marko's avatar
marko committed
2937
	ut_a(page_is_comp(page));
2938
	UNIV_MEM_ASSERT_RW(page, UNIV_PAGE_SIZE);
marko's avatar
marko committed
2939

2940 2941
	page_zip_fields_free(index);
	mem_heap_free(heap);
2942 2943 2944 2945 2946 2947
	{
		page_zip_stat_t*	zip_stat
			= &page_zip_stat[page_zip->ssize - 1];
		zip_stat->decompressed++;
		zip_stat->decompressed_usec += ut_time_us(NULL) - usec;
	}
2948

2949 2950 2951
	/* Update the stat counter for LRU policy. */
	buf_LRU_stat_inc_unzip();

marko's avatar
marko committed
2952 2953 2954
	return(TRUE);
}

2955
#ifdef UNIV_ZIP_DEBUG
2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988
/**************************************************************************
Dump a block of memory on the standard error stream. */
static
void
page_zip_hexdump_func(
/*==================*/
	const char*	name,	/* in: name of the data structure */
	const void*	buf,	/* in: data */
	ulint		size)	/* in: length of the data, in bytes */
{
	const byte*	s	= buf;
	ulint		addr;
	const ulint	width	= 32; /* bytes per line */

	fprintf(stderr, "%s:\n", name);

	for (addr = 0; addr < size; addr += width) {
		ulint	i;

		fprintf(stderr, "%04lx ", (ulong) addr);

		i = ut_min(width, size - addr);

		while (i--) {
			fprintf(stderr, "%02x", *s++);
		}

		putc('\n', stderr);
	}
}

#define page_zip_hexdump(buf, size) page_zip_hexdump_func(#buf, buf, size)

2989
/* Flag: make page_zip_validate() compare page headers only */
2990
UNIV_INTERN ibool	page_zip_validate_header_only = FALSE;
2991

marko's avatar
marko committed
2992 2993
/**************************************************************************
Check that the compressed and decompressed pages match. */
2994
UNIV_INTERN
marko's avatar
marko committed
2995
ibool
2996 2997
page_zip_validate_low(
/*==================*/
2998
					/* out: TRUE if valid, FALSE if not */
2999
	const page_zip_des_t*	page_zip,/* in: compressed page */
3000 3001 3002
	const page_t*		page,	/* in: uncompressed page */
	ibool			sloppy)	/* in: FALSE=strict,
					TRUE=ignore the MIN_REC_FLAG */
marko's avatar
marko committed
3003
{
3004
	page_zip_des_t	temp_page_zip;
3005
	byte*		temp_page_buf;
3006
	page_t*		temp_page;
3007
	ibool		valid;
marko's avatar
marko committed
3008

3009
	if (memcmp(page_zip->data + FIL_PAGE_PREV, page + FIL_PAGE_PREV,
3010
		   FIL_PAGE_LSN - FIL_PAGE_PREV)
3011 3012
	    || memcmp(page_zip->data + FIL_PAGE_TYPE, page + FIL_PAGE_TYPE, 2)
	    || memcmp(page_zip->data + FIL_PAGE_DATA, page + FIL_PAGE_DATA,
3013
		      PAGE_DATA - FIL_PAGE_DATA)) {
3014
		page_zip_fail(("page_zip_validate: page header\n"));
3015 3016 3017
		page_zip_hexdump(page_zip, sizeof *page_zip);
		page_zip_hexdump(page_zip->data, page_zip_get_size(page_zip));
		page_zip_hexdump(page, UNIV_PAGE_SIZE);
3018 3019 3020
		return(FALSE);
	}

3021 3022
	ut_a(page_is_comp(page));

3023 3024 3025 3026
	if (page_zip_validate_header_only) {
		return(TRUE);
	}

3027 3028 3029 3030
	/* page_zip_decompress() expects the uncompressed page to be
	UNIV_PAGE_SIZE aligned. */
	temp_page_buf = ut_malloc(2 * UNIV_PAGE_SIZE);
	temp_page = ut_align(temp_page_buf, UNIV_PAGE_SIZE);
3031

3032
#ifdef UNIV_DEBUG_VALGRIND
3033
	/* Get detailed information on the valid bits in case the
3034 3035 3036
	UNIV_MEM_ASSERT_RW() checks fail.  The v-bits of page[],
	page_zip->data[] or page_zip could be viewed at temp_page[] or
	temp_page_zip in a debugger when running valgrind --db-attach. */
3037 3038
	VALGRIND_GET_VBITS(page, temp_page, UNIV_PAGE_SIZE);
	UNIV_MEM_ASSERT_RW(page, UNIV_PAGE_SIZE);
3039 3040 3041 3042
	VALGRIND_GET_VBITS(page_zip, &temp_page_zip, sizeof temp_page_zip);
	UNIV_MEM_ASSERT_RW(page_zip, sizeof *page_zip);
	VALGRIND_GET_VBITS(page_zip->data, temp_page,
			   page_zip_get_size(page_zip));
3043
	UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip));
3044
#endif /* UNIV_DEBUG_VALGRIND */
3045 3046

	temp_page_zip = *page_zip;
3047
	valid = page_zip_decompress(&temp_page_zip, temp_page);
3048 3049 3050 3051 3052
	if (!valid) {
		fputs("page_zip_validate(): failed to decompress\n", stderr);
		goto func_exit;
	}
	if (page_zip->n_blobs != temp_page_zip.n_blobs) {
3053 3054
		page_zip_fail(("page_zip_validate: n_blobs: %u!=%u\n",
			       page_zip->n_blobs, temp_page_zip.n_blobs));
3055 3056
		valid = FALSE;
	}
3057
#ifdef UNIV_DEBUG
3058
	if (page_zip->m_start != temp_page_zip.m_start) {
3059 3060
		page_zip_fail(("page_zip_validate: m_start: %u!=%u\n",
			       page_zip->m_start, temp_page_zip.m_start));
3061 3062
		valid = FALSE;
	}
3063
#endif /* UNIV_DEBUG */
3064
	if (page_zip->m_end != temp_page_zip.m_end) {
3065 3066
		page_zip_fail(("page_zip_validate: m_end: %u!=%u\n",
			       page_zip->m_end, temp_page_zip.m_end));
3067 3068
		valid = FALSE;
	}
3069
	if (page_zip->m_nonempty != temp_page_zip.m_nonempty) {
3070 3071 3072
		page_zip_fail(("page_zip_validate(): m_nonempty: %u!=%u\n",
			       page_zip->m_nonempty,
			       temp_page_zip.m_nonempty));
3073 3074
		valid = FALSE;
	}
3075
	if (memcmp(page + PAGE_HEADER, temp_page + PAGE_HEADER,
3076
		   UNIV_PAGE_SIZE - PAGE_HEADER - FIL_PAGE_DATA_END)) {
3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113

		/* In crash recovery, the "minimum record" flag may be
		set incorrectly until the mini-transaction is
		committed.  Let us tolerate that difference when we
		are performing a sloppy validation. */

		if (sloppy) {
			byte	info_bits_diff;
			ulint	offset
				= rec_get_next_offs(page + PAGE_NEW_INFIMUM,
						    TRUE);
			ut_a(offset >= PAGE_NEW_SUPREMUM);
			offset -= 5 /* REC_NEW_INFO_BITS */;

			info_bits_diff = page[offset] ^ temp_page[offset];

			if (info_bits_diff == REC_INFO_MIN_REC_FLAG) {
				temp_page[offset] = page[offset];

				if (!memcmp(page + PAGE_HEADER,
					    temp_page + PAGE_HEADER,
					    UNIV_PAGE_SIZE - PAGE_HEADER
					    - FIL_PAGE_DATA_END)) {

					/* Only the minimum record flag
					differed.  Let us ignore it. */
					page_zip_fail(("page_zip_validate: "
						       "min_rec_flag "
						       "(ignored, "
						       "%lu,%lu,0x%02lx)\n",
						       page_get_space_id(page),
						       page_get_page_no(page),
						       (ulong) page[offset]));
					goto func_exit;
				}
			}
		}
3114
		page_zip_fail(("page_zip_validate: content\n"));
3115 3116 3117 3118
		valid = FALSE;
	}

func_exit:
3119 3120 3121 3122 3123 3124
	if (!valid) {
		page_zip_hexdump(page_zip, sizeof *page_zip);
		page_zip_hexdump(page_zip->data, page_zip_get_size(page_zip));
		page_zip_hexdump(page, UNIV_PAGE_SIZE);
		page_zip_hexdump(temp_page, UNIV_PAGE_SIZE);
	}
3125
	ut_free(temp_page_buf);
3126
	return(valid);
marko's avatar
marko committed
3127
}
3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141

/**************************************************************************
Check that the compressed and decompressed pages match. */
UNIV_INTERN
ibool
page_zip_validate(
/*==============*/
					/* out: TRUE if valid, FALSE if not */
	const page_zip_des_t*	page_zip,/* in: compressed page */
	const page_t*		page)	/* in: uncompressed page */
{
	return(page_zip_validate_low(page_zip, page,
				     recv_recovery_is_on()));
}
3142
#endif /* UNIV_ZIP_DEBUG */
marko's avatar
marko committed
3143

3144 3145 3146 3147 3148 3149 3150 3151 3152 3153
#ifdef UNIV_DEBUG
static
ibool
page_zip_header_cmp(
/*================*/
					/* out: TRUE */
	const page_zip_des_t*	page_zip,/* in: compressed page */
	const byte*		page)	/* in: uncompressed page */
{
	ut_ad(!memcmp(page_zip->data + FIL_PAGE_PREV, page + FIL_PAGE_PREV,
3154
		      FIL_PAGE_LSN - FIL_PAGE_PREV));
3155
	ut_ad(!memcmp(page_zip->data + FIL_PAGE_TYPE, page + FIL_PAGE_TYPE,
3156
		      2));
3157
	ut_ad(!memcmp(page_zip->data + FIL_PAGE_DATA, page + FIL_PAGE_DATA,
3158
		      PAGE_DATA - FIL_PAGE_DATA));
3159 3160 3161 3162 3163

	return(TRUE);
}
#endif /* UNIV_DEBUG */

3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189
/**************************************************************************
Write a record on the compressed page that contains externally stored
columns.  The data must already have been written to the uncompressed page. */
static
byte*
page_zip_write_rec_ext(
/*===================*/
					/* out: end of modification log */
	page_zip_des_t*	page_zip,	/* in/out: compressed page */
	const page_t*	page,		/* in: page containing rec */
	const byte*	rec,		/* in: record being written */
	dict_index_t*	index,		/* in: record descriptor */
	const ulint*	offsets,	/* in: rec_get_offsets(rec, index) */
	ulint		create,		/* in: nonzero=insert, zero=update */
	ulint		trx_id_col,	/* in: position of DB_TRX_ID */
	ulint		heap_no,	/* in: heap number of rec */
	byte*		storage,	/* in: end of dense page directory */
	byte*		data)		/* in: end of modification log */
{
	const byte*	start	= rec;
	ulint		i;
	ulint		len;
	byte*		externs	= storage;
	ulint		n_ext	= rec_offs_n_extern(offsets);

	ut_ad(rec_offs_validate(rec, index, offsets));
3190 3191 3192
	UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets));
	UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets),
			   rec_offs_extra_size(offsets));
3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213

	externs -= (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN)
		* (page_dir_get_n_heap(page) - PAGE_HEAP_NO_USER_LOW);

	/* Note that this will not take into account
	the BLOB columns of rec if create==TRUE. */
	ut_ad(data + rec_offs_data_size(offsets)
	      - (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN)
	      - n_ext * BTR_EXTERN_FIELD_REF_SIZE
	      < externs - BTR_EXTERN_FIELD_REF_SIZE * page_zip->n_blobs);

	{
		ulint	blob_no = page_zip_get_n_prev_extern(
			page_zip, rec, index);
		byte*	ext_end = externs - page_zip->n_blobs
			* BTR_EXTERN_FIELD_REF_SIZE;
		ut_ad(blob_no <= page_zip->n_blobs);
		externs -= blob_no * BTR_EXTERN_FIELD_REF_SIZE;

		if (create) {
			page_zip->n_blobs += n_ext;
3214 3215
			ASSERT_ZERO_BLOB(ext_end - n_ext
					 * BTR_EXTERN_FIELD_REF_SIZE);
3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243
			memmove(ext_end - n_ext
				* BTR_EXTERN_FIELD_REF_SIZE,
				ext_end,
				externs - ext_end);
		}

		ut_a(blob_no + n_ext <= page_zip->n_blobs);
	}

	for (i = 0; i < rec_offs_n_fields(offsets); i++) {
		const byte*	src;

		if (UNIV_UNLIKELY(i == trx_id_col)) {
			ut_ad(!rec_offs_nth_extern(offsets,
						   i));
			ut_ad(!rec_offs_nth_extern(offsets,
						   i + 1));
			/* Locate trx_id and roll_ptr. */
			src = rec_get_nth_field(rec, offsets,
						i, &len);
			ut_ad(len == DATA_TRX_ID_LEN);
			ut_ad(src + DATA_TRX_ID_LEN
			      == rec_get_nth_field(
				      rec, offsets,
				      i + 1, &len));
			ut_ad(len == DATA_ROLL_PTR_LEN);

			/* Log the preceding fields. */
3244
			ASSERT_ZERO(data, src - start);
3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263
			memcpy(data, start, src - start);
			data += src - start;
			start = src + (DATA_TRX_ID_LEN
				       + DATA_ROLL_PTR_LEN);

			/* Store trx_id and roll_ptr. */
			memcpy(storage - (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN)
			       * (heap_no - 1),
			       src, DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
			i++; /* skip also roll_ptr */
		} else if (rec_offs_nth_extern(offsets, i)) {
			src = rec_get_nth_field(rec, offsets,
						i, &len);

			ut_ad(dict_index_is_clust(index));
			ut_ad(len
			      >= BTR_EXTERN_FIELD_REF_SIZE);
			src += len - BTR_EXTERN_FIELD_REF_SIZE;

3264
			ASSERT_ZERO(data, src - start);
3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278
			memcpy(data, start, src - start);
			data += src - start;
			start = src + BTR_EXTERN_FIELD_REF_SIZE;

			/* Store the BLOB pointer. */
			externs -= BTR_EXTERN_FIELD_REF_SIZE;
			ut_ad(data < externs);
			memcpy(externs, src, BTR_EXTERN_FIELD_REF_SIZE);
		}
	}

	/* Log the last bytes of the record. */
	len = rec_offs_data_size(offsets) - (start - rec);

3279
	ASSERT_ZERO(data, len);
3280 3281 3282 3283 3284 3285
	memcpy(data, start, len);
	data += len;

	return(data);
}

marko's avatar
marko committed
3286
/**************************************************************************
3287
Write an entire record on the compressed page.  The data must already
marko's avatar
marko committed
3288
have been written to the uncompressed page. */
3289
UNIV_INTERN
marko's avatar
marko committed
3290
void
3291 3292
page_zip_write_rec(
/*===============*/
marko's avatar
marko committed
3293
	page_zip_des_t*	page_zip,/* in/out: compressed page */
3294
	const byte*	rec,	/* in: record being written */
3295
	dict_index_t*	index,	/* in: the index the record belongs to */
3296 3297
	const ulint*	offsets,/* in: rec_get_offsets(rec, index) */
	ulint		create)	/* in: nonzero=insert, zero=update */
marko's avatar
marko committed
3298
{
3299 3300 3301 3302 3303
	const page_t*	page;
	byte*		data;
	byte*		storage;
	ulint		heap_no;
	byte*		slot;
marko's avatar
marko committed
3304

3305
	ut_ad(buf_frame_get_page_zip(rec) == page_zip);
marko's avatar
marko committed
3306
	ut_ad(page_zip_simple_validate(page_zip));
3307 3308
	ut_ad(page_zip_get_size(page_zip)
	      > PAGE_DATA + page_zip_dir_size(page_zip));
3309
	ut_ad(rec_offs_comp(offsets));
3310
	ut_ad(rec_offs_validate(rec, index, offsets));
marko's avatar
marko committed
3311

3312
	ut_ad(page_zip->m_start >= PAGE_DATA);
marko's avatar
marko committed
3313

3314
	page = page_align(rec);
3315 3316

	ut_ad(page_zip_header_cmp(page_zip, page));
3317
	ut_ad(page_simple_validate_new((page_t*) page));
3318

3319
	UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip));
3320 3321 3322
	UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets));
	UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets),
			   rec_offs_extra_size(offsets));
3323

3324
	slot = page_zip_dir_find(page_zip, page_offset(rec));
marko's avatar
marko committed
3325
	ut_a(slot);
3326
	/* Copy the delete mark. */
3327
	if (rec_get_deleted_flag(rec, TRUE)) {
3328 3329 3330 3331
		*slot |= PAGE_ZIP_DIR_SLOT_DEL >> 8;
	} else {
		*slot &= ~(PAGE_ZIP_DIR_SLOT_DEL >> 8);
	}
marko's avatar
marko committed
3332

3333 3334
	ut_ad(rec_get_start((rec_t*) rec, offsets) >= page + PAGE_ZIP_START);
	ut_ad(rec_get_end((rec_t*) rec, offsets) <= page + UNIV_PAGE_SIZE
3335 3336
	      - PAGE_DIR - PAGE_DIR_SLOT_SIZE
	      * page_dir_get_n_slots(page));
marko's avatar
marko committed
3337

3338
	heap_no = rec_get_heap_no_new(rec);
3339
	ut_ad(heap_no >= PAGE_HEAP_NO_USER_LOW); /* not infimum or supremum */
3340 3341
	ut_ad(heap_no < page_dir_get_n_heap(page));

marko's avatar
marko committed
3342
	/* Append to the modification log. */
3343
	data = page_zip->data + page_zip->m_end;
3344 3345 3346 3347 3348
	ut_ad(!*data);

	/* Identify the record by writing its heap number - 1.
	0 is reserved to indicate the end of the modification log. */

3349
	if (UNIV_UNLIKELY(heap_no - 1 >= 64)) {
3350
		*data++ = (byte) (0x80 | (heap_no - 1) >> 7);
marko's avatar
marko committed
3351
		ut_ad(!*data);
3352
	}
3353
	*data++ = (byte) ((heap_no - 1) << 1);
marko's avatar
marko committed
3354
	ut_ad(!*data);
3355 3356

	{
3357
		const byte*	start	= rec - rec_offs_extra_size(offsets);
3358 3359 3360 3361 3362 3363 3364 3365 3366
		const byte*	b	= rec - REC_N_NEW_EXTRA_BYTES;

		/* Write the extra bytes backwards, so that
		rec_offs_extra_size() can be easily computed in
		page_zip_apply_log() by invoking
		rec_get_offsets_reverse(). */

		while (b != start) {
			*data++ = *--b;
marko's avatar
marko committed
3367
			ut_ad(!*data);
3368 3369 3370 3371
		}
	}

	/* Write the data bytes.  Store the uncompressed bytes separately. */
3372
	storage = page_zip->data + page_zip_get_size(page_zip)
3373
		- (page_dir_get_n_heap(page) - PAGE_HEAP_NO_USER_LOW)
3374
		* PAGE_ZIP_DIR_SLOT_SIZE;
3375 3376 3377

	if (page_is_leaf(page)) {
		ulint		len;
3378 3379

		if (dict_index_is_clust(index)) {
3380 3381
			ulint		trx_id_col;

3382 3383
			trx_id_col = dict_index_get_sys_col_pos(index,
								DATA_TRX_ID);
3384
			ut_ad(trx_id_col != ULINT_UNDEFINED);
3385

3386 3387
			/* Store separately trx_id, roll_ptr and
			the BTR_EXTERN_FIELD_REF of each BLOB column. */
3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405
			if (rec_offs_any_extern(offsets)) {
				data = page_zip_write_rec_ext(
					page_zip, page,
					rec, index, offsets, create,
					trx_id_col, heap_no, storage, data);
			} else {
				/* Locate trx_id and roll_ptr. */
				const byte*	src
					= rec_get_nth_field(rec, offsets,
							    trx_id_col, &len);
				ut_ad(len == DATA_TRX_ID_LEN);
				ut_ad(src + DATA_TRX_ID_LEN
				      == rec_get_nth_field(
					      rec, offsets,
					      trx_id_col + 1, &len));
				ut_ad(len == DATA_ROLL_PTR_LEN);

				/* Log the preceding fields. */
3406
				ASSERT_ZERO(data, src - rec);
3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422
				memcpy(data, rec, src - rec);
				data += src - rec;

				/* Store trx_id and roll_ptr. */
				memcpy(storage
				       - (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN)
				       * (heap_no - 1),
				       src,
				       DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);

				src += DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN;

				/* Log the last bytes of the record. */
				len = rec_offs_data_size(offsets)
					- (src - rec);

3423
				ASSERT_ZERO(data, len);
3424 3425
				memcpy(data, src, len);
				data += len;
3426
			}
3427 3428 3429 3430
		} else {
			/* Leaf page of a secondary index:
			no externally stored columns */
			ut_ad(dict_index_get_sys_col_pos(index, DATA_TRX_ID)
3431
			      == ULINT_UNDEFINED);
3432
			ut_ad(!rec_offs_any_extern(offsets));
3433

3434 3435
			/* Log the entire record. */
			len = rec_offs_data_size(offsets);
3436

3437
			ASSERT_ZERO(data, len);
3438 3439 3440
			memcpy(data, rec, len);
			data += len;
		}
3441 3442 3443 3444 3445 3446 3447 3448 3449 3450
	} else {
		/* This is a node pointer page. */
		ulint	len;

		/* Non-leaf nodes should not have any externally
		stored columns. */
		ut_ad(!rec_offs_any_extern(offsets));

		/* Copy the data bytes, except node_ptr. */
		len = rec_offs_data_size(offsets) - REC_NODE_PTR_SIZE;
3451
		ut_ad(data + len < storage - REC_NODE_PTR_SIZE
3452
		      * (page_dir_get_n_heap(page) - PAGE_HEAP_NO_USER_LOW));
3453
		ASSERT_ZERO(data, len);
3454 3455 3456 3457 3458
		memcpy(data, rec, len);
		data += len;

		/* Copy the node pointer to the uncompressed area. */
		memcpy(storage - REC_NODE_PTR_SIZE
3459 3460 3461
		       * (heap_no - 1),
		       rec + len,
		       REC_NODE_PTR_SIZE);
3462 3463
	}

3464
	ut_a(!*data);
3465
	ut_ad((ulint) (data - page_zip->data) < page_zip_get_size(page_zip));
3466
	page_zip->m_end = data - page_zip->data;
3467
	page_zip->m_nonempty = TRUE;
3468

3469
#ifdef UNIV_ZIP_DEBUG
3470
	ut_a(page_zip_validate(page_zip, page_align(rec)));
3471
#endif /* UNIV_ZIP_DEBUG */
3472 3473
}

3474 3475
/***************************************************************
Parses a log record of writing a BLOB pointer of a record. */
3476
UNIV_INTERN
3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490
byte*
page_zip_parse_write_blob_ptr(
/*==========================*/
				/* out: end of log record or NULL */
	byte*		ptr,	/* in: redo log buffer */
	byte*		end_ptr,/* in: redo log buffer end */
	page_t*		page,	/* in/out: uncompressed page */
	page_zip_des_t*	page_zip)/* in/out: compressed page */
{
	ulint	offset;
	ulint	z_offset;

	ut_ad(!page == !page_zip);

3491 3492
	if (UNIV_UNLIKELY
	    (end_ptr < ptr + (2 + 2 + BTR_EXTERN_FIELD_REF_SIZE))) {
3493 3494 3495 3496 3497 3498 3499 3500

		return(NULL);
	}

	offset = mach_read_from_2(ptr);
	z_offset = mach_read_from_2(ptr + 2);

	if (UNIV_UNLIKELY(offset < PAGE_ZIP_START)
3501 3502
	    || UNIV_UNLIKELY(offset >= UNIV_PAGE_SIZE)
	    || UNIV_UNLIKELY(z_offset >= UNIV_PAGE_SIZE)) {
3503 3504 3505 3506 3507 3508 3509
corrupt:
		recv_sys->found_corrupt_log = TRUE;

		return(NULL);
	}

	if (page) {
3510
		if (UNIV_UNLIKELY(!page_zip)
3511
		    || UNIV_UNLIKELY(!page_is_leaf(page))) {
3512 3513 3514 3515

			goto corrupt;
		}

3516
#ifdef UNIV_ZIP_DEBUG
3517
		ut_a(page_zip_validate(page_zip, page));
3518
#endif /* UNIV_ZIP_DEBUG */
3519

3520
		memcpy(page + offset,
3521
		       ptr + 4, BTR_EXTERN_FIELD_REF_SIZE);
3522
		memcpy(page_zip->data + z_offset,
3523
		       ptr + 4, BTR_EXTERN_FIELD_REF_SIZE);
3524

3525
#ifdef UNIV_ZIP_DEBUG
3526
		ut_a(page_zip_validate(page_zip, page));
3527
#endif /* UNIV_ZIP_DEBUG */
3528 3529 3530 3531 3532
	}

	return(ptr + (2 + 2 + BTR_EXTERN_FIELD_REF_SIZE));
}

3533
/**************************************************************************
3534
Write a BLOB pointer of a record on the leaf page of a clustered index.
3535
The information must already have been updated on the uncompressed page. */
3536
UNIV_INTERN
3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548
void
page_zip_write_blob_ptr(
/*====================*/
	page_zip_des_t*	page_zip,/* in/out: compressed page */
	const byte*	rec,	/* in/out: record whose data is being
				written */
	dict_index_t*	index,	/* in: index of the page */
	const ulint*	offsets,/* in: rec_get_offsets(rec, index) */
	ulint		n,	/* in: column index */
	mtr_t*		mtr)	/* in: mini-transaction handle,
				or NULL if no logging is needed */
{
3549 3550
	const byte*	field;
	byte*		externs;
3551
	const page_t*	page	= page_align(rec);
3552 3553
	ulint		blob_no;
	ulint		len;
3554

3555 3556
	ut_ad(buf_frame_get_page_zip(rec) == page_zip);
	ut_ad(page_simple_validate_new((page_t*) page));
3557
	ut_ad(page_zip_simple_validate(page_zip));
3558 3559
	ut_ad(page_zip_get_size(page_zip)
	      > PAGE_DATA + page_zip_dir_size(page_zip));
3560
	ut_ad(rec_offs_comp(offsets));
3561
	ut_ad(rec_offs_validate(rec, NULL, offsets));
3562
	ut_ad(rec_offs_any_extern(offsets));
3563 3564 3565
	ut_ad(rec_offs_nth_extern(offsets, n));

	ut_ad(page_zip->m_start >= PAGE_DATA);
3566
	ut_ad(page_zip_header_cmp(page_zip, page));
3567 3568

	ut_ad(page_is_leaf(page));
3569
	ut_ad(dict_index_is_clust(index));
3570

3571
	UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip));
3572 3573 3574
	UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets));
	UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets),
			   rec_offs_extra_size(offsets));
3575

3576
	blob_no = page_zip_get_n_prev_extern(page_zip, rec, index)
3577
		+ rec_get_n_extern_new(rec, index, n);
3578
	ut_a(blob_no < page_zip->n_blobs);
3579

3580
	externs = page_zip->data + page_zip_get_size(page_zip)
3581
		- (page_dir_get_n_heap(page) - PAGE_HEAP_NO_USER_LOW)
3582 3583
		* (PAGE_ZIP_DIR_SLOT_SIZE
		   + DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
3584

3585
	field = rec_get_nth_field(rec, offsets, n, &len);
3586

3587 3588 3589 3590
	externs -= (blob_no + 1) * BTR_EXTERN_FIELD_REF_SIZE;
	field += len - BTR_EXTERN_FIELD_REF_SIZE;

	memcpy(externs, field, BTR_EXTERN_FIELD_REF_SIZE);
3591

3592
#ifdef UNIV_ZIP_DEBUG
3593
	ut_a(page_zip_validate(page_zip, page));
3594
#endif /* UNIV_ZIP_DEBUG */
3595

3596
	if (mtr) {
3597 3598
		byte*	log_ptr	= mlog_open(
			mtr, 11 + 2 + 2 + BTR_EXTERN_FIELD_REF_SIZE);
3599 3600 3601 3602
		if (UNIV_UNLIKELY(!log_ptr)) {
			return;
		}

3603 3604 3605
		log_ptr = mlog_write_initial_log_record_fast(
			(byte*) field, MLOG_ZIP_WRITE_BLOB_PTR, log_ptr, mtr);
		mach_write_to_2(log_ptr, page_offset(field));
3606
		log_ptr += 2;
3607 3608 3609 3610 3611
		mach_write_to_2(log_ptr, externs - page_zip->data);
		log_ptr += 2;
		memcpy(log_ptr, externs, BTR_EXTERN_FIELD_REF_SIZE);
		log_ptr += BTR_EXTERN_FIELD_REF_SIZE;
		mlog_close(mtr, log_ptr);
3612 3613 3614
	}
}

3615 3616
/***************************************************************
Parses a log record of writing the node pointer of a record. */
3617
UNIV_INTERN
3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631
byte*
page_zip_parse_write_node_ptr(
/*==========================*/
				/* out: end of log record or NULL */
	byte*		ptr,	/* in: redo log buffer */
	byte*		end_ptr,/* in: redo log buffer end */
	page_t*		page,	/* in/out: uncompressed page */
	page_zip_des_t*	page_zip)/* in/out: compressed page */
{
	ulint	offset;
	ulint	z_offset;

	ut_ad(!page == !page_zip);

3632
	if (UNIV_UNLIKELY(end_ptr < ptr + (2 + 2 + REC_NODE_PTR_SIZE))) {
3633 3634 3635 3636 3637 3638 3639 3640

		return(NULL);
	}

	offset = mach_read_from_2(ptr);
	z_offset = mach_read_from_2(ptr + 2);

	if (UNIV_UNLIKELY(offset < PAGE_ZIP_START)
3641 3642
	    || UNIV_UNLIKELY(offset >= UNIV_PAGE_SIZE)
	    || UNIV_UNLIKELY(z_offset >= UNIV_PAGE_SIZE)) {
3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654
corrupt:
		recv_sys->found_corrupt_log = TRUE;

		return(NULL);
	}

	if (page) {
		byte*	storage_end;
		byte*	field;
		byte*	storage;
		ulint	heap_no;

3655
		if (UNIV_UNLIKELY(!page_zip)
3656
		    || UNIV_UNLIKELY(page_is_leaf(page))) {
3657 3658 3659 3660

			goto corrupt;
		}

3661
#ifdef UNIV_ZIP_DEBUG
3662
		ut_a(page_zip_validate(page_zip, page));
3663
#endif /* UNIV_ZIP_DEBUG */
3664

3665 3666 3667
		field = page + offset;
		storage = page_zip->data + z_offset;

3668
		storage_end = page_zip->data + page_zip_get_size(page_zip)
3669
			- (page_dir_get_n_heap(page) - PAGE_HEAP_NO_USER_LOW)
3670 3671 3672 3673 3674
			* PAGE_ZIP_DIR_SLOT_SIZE;

		heap_no = 1 + (storage_end - storage) / REC_NODE_PTR_SIZE;

		if (UNIV_UNLIKELY((storage_end - storage) % REC_NODE_PTR_SIZE)
3675
		    || UNIV_UNLIKELY(heap_no < PAGE_HEAP_NO_USER_LOW)
3676 3677 3678 3679 3680 3681 3682 3683
		    || UNIV_UNLIKELY(heap_no >= page_dir_get_n_heap(page))) {

			goto corrupt;
		}

		memcpy(field, ptr + 4, REC_NODE_PTR_SIZE);
		memcpy(storage, ptr + 4, REC_NODE_PTR_SIZE);

3684
#ifdef UNIV_ZIP_DEBUG
3685
		ut_a(page_zip_validate(page_zip, page));
3686
#endif /* UNIV_ZIP_DEBUG */
3687 3688
	}

3689
	return(ptr + (2 + 2 + REC_NODE_PTR_SIZE));
3690 3691
}

3692 3693
/**************************************************************************
Write the node pointer of a record on a non-leaf compressed page. */
3694
UNIV_INTERN
3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705
void
page_zip_write_node_ptr(
/*====================*/
	page_zip_des_t*	page_zip,/* in/out: compressed page */
	byte*		rec,	/* in/out: record */
	ulint		size,	/* in: data size of rec */
	ulint		ptr,	/* in: node pointer */
	mtr_t*		mtr)	/* in: mini-transaction, or NULL */
{
	byte*	field;
	byte*	storage;
3706
	page_t*	page	= page_align(rec);
3707

3708
	ut_ad(buf_frame_get_page_zip(rec) == page_zip);
3709
	ut_ad(page_simple_validate_new(page));
3710
	ut_ad(page_zip_simple_validate(page_zip));
3711 3712
	ut_ad(page_zip_get_size(page_zip)
	      > PAGE_DATA + page_zip_dir_size(page_zip));
3713 3714 3715
	ut_ad(page_rec_is_comp(rec));

	ut_ad(page_zip->m_start >= PAGE_DATA);
3716
	ut_ad(page_zip_header_cmp(page_zip, page));
3717 3718 3719

	ut_ad(!page_is_leaf(page));

3720 3721 3722
	UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip));
	UNIV_MEM_ASSERT_RW(rec, size);

3723
	storage = page_zip->data + page_zip_get_size(page_zip)
3724
		- (page_dir_get_n_heap(page) - PAGE_HEAP_NO_USER_LOW)
3725 3726
		* PAGE_ZIP_DIR_SLOT_SIZE
		- (rec_get_heap_no_new(rec) - 1) * REC_NODE_PTR_SIZE;
3727 3728
	field = rec + size - REC_NODE_PTR_SIZE;

3729
#if defined UNIV_DEBUG || defined UNIV_ZIP_DEBUG
3730
	ut_a(!memcmp(storage, field, REC_NODE_PTR_SIZE));
3731
#endif /* UNIV_DEBUG || UNIV_ZIP_DEBUG */
3732 3733 3734 3735 3736 3737 3738
#if REC_NODE_PTR_SIZE != 4
# error "REC_NODE_PTR_SIZE != 4"
#endif
	mach_write_to_4(field, ptr);
	memcpy(storage, field, REC_NODE_PTR_SIZE);

	if (mtr) {
3739
		byte*	log_ptr	= mlog_open(mtr,
3740
					    11 + 2 + 2 + REC_NODE_PTR_SIZE);
3741 3742 3743 3744
		if (UNIV_UNLIKELY(!log_ptr)) {
			return;
		}

3745 3746 3747
		log_ptr = mlog_write_initial_log_record_fast(
			field, MLOG_ZIP_WRITE_NODE_PTR, log_ptr, mtr);
		mach_write_to_2(log_ptr, page_offset(field));
3748
		log_ptr += 2;
3749 3750
		mach_write_to_2(log_ptr, storage - page_zip->data);
		log_ptr += 2;
3751
		memcpy(log_ptr, field, REC_NODE_PTR_SIZE);
3752 3753
		log_ptr += REC_NODE_PTR_SIZE;
		mlog_close(mtr, log_ptr);
3754
	}
marko's avatar
marko committed
3755 3756 3757
}

/**************************************************************************
3758
Write the trx_id and roll_ptr of a record on a B-tree leaf node page. */
3759
UNIV_INTERN
3760
void
3761 3762
page_zip_write_trx_id_and_roll_ptr(
/*===============================*/
3763 3764
	page_zip_des_t*	page_zip,/* in/out: compressed page */
	byte*		rec,	/* in/out: record */
3765 3766
	const ulint*	offsets,/* in: rec_get_offsets(rec, index) */
	ulint		trx_id_col,/* in: column number of TRX_ID in rec */
3767
	dulint		trx_id,	/* in: transaction identifier */
3768
	dulint		roll_ptr)/* in: roll_ptr */
marko's avatar
marko committed
3769
{
3770 3771
	byte*	field;
	byte*	storage;
3772
	page_t*	page	= page_align(rec);
3773
	ulint	len;
3774

3775
	ut_ad(buf_frame_get_page_zip(rec) == page_zip);
3776
	ut_ad(page_simple_validate_new(page));
3777
	ut_ad(page_zip_simple_validate(page_zip));
3778 3779
	ut_ad(page_zip_get_size(page_zip)
	      > PAGE_DATA + page_zip_dir_size(page_zip));
3780 3781
	ut_ad(rec_offs_validate(rec, NULL, offsets));
	ut_ad(rec_offs_comp(offsets));
3782 3783

	ut_ad(page_zip->m_start >= PAGE_DATA);
3784
	ut_ad(page_zip_header_cmp(page_zip, page));
3785 3786 3787

	ut_ad(page_is_leaf(page));

3788 3789
	UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip));

3790
	storage = page_zip->data + page_zip_get_size(page_zip)
3791
		- (page_dir_get_n_heap(page) - PAGE_HEAP_NO_USER_LOW)
3792 3793 3794
		* PAGE_ZIP_DIR_SLOT_SIZE
		- (rec_get_heap_no_new(rec) - 1)
		* (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
3795 3796 3797 3798 3799 3800 3801

#if DATA_TRX_ID + 1 != DATA_ROLL_PTR
# error "DATA_TRX_ID + 1 != DATA_ROLL_PTR"
#endif
	field = rec_get_nth_field(rec, offsets, trx_id_col, &len);
	ut_ad(len == DATA_TRX_ID_LEN);
	ut_ad(field + DATA_TRX_ID_LEN
3802
	      == rec_get_nth_field(rec, offsets, trx_id_col + 1, &len));
3803
	ut_ad(len == DATA_ROLL_PTR_LEN);
3804 3805 3806 3807 3808 3809 3810 3811 3812 3813
#if defined UNIV_DEBUG || defined UNIV_ZIP_DEBUG
	ut_a(!memcmp(storage, field, DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN));
#endif /* UNIV_DEBUG || UNIV_ZIP_DEBUG */
#if DATA_TRX_ID_LEN != 6
# error "DATA_TRX_ID_LEN != 6"
#endif
	mach_write_to_6(field, trx_id);
#if DATA_ROLL_PTR_LEN != 7
# error "DATA_ROLL_PTR_LEN != 7"
#endif
marko's avatar
marko committed
3814
	mach_write_to_7(field + DATA_TRX_ID_LEN, roll_ptr);
3815
	memcpy(storage, field, DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
3816

3817 3818 3819
	UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets));
	UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets),
			   rec_offs_extra_size(offsets));
3820
	UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip));
3821 3822
}

3823 3824 3825
#ifdef UNIV_ZIP_DEBUG
/* Set this variable in a debugger to disable page_zip_clear_rec().
The only observable effect should be the compression ratio due to
3826 3827 3828
deleted records not being zeroed out.  In rare cases, there can be
page_zip_validate() failures on the node_ptr, trx_id and roll_ptr
columns if the space is reallocated for a smaller record. */
3829
UNIV_INTERN ibool	page_zip_clear_rec_disable;
3830 3831
#endif /* UNIV_ZIP_DEBUG */

3832 3833
/**************************************************************************
Clear an area on the uncompressed and compressed page, if possible. */
3834
static
3835 3836 3837 3838 3839 3840
void
page_zip_clear_rec(
/*===============*/
	page_zip_des_t*	page_zip,/* in/out: compressed page */
	byte*		rec,	/* in: record to clear */
	dict_index_t*	index,	/* in: index of rec */
3841
	const ulint*	offsets)/* in: rec_get_offsets(rec, index) */
3842
{
3843
	ulint	heap_no;
3844
	page_t*	page	= page_align(rec);
3845 3846
	/* page_zip_validate() would fail here if a record
	containing externally stored columns is being deleted. */
3847
	ut_ad(rec_offs_validate(rec, index, offsets));
3848 3849
	ut_ad(!page_zip_dir_find(page_zip, page_offset(rec)));
	ut_ad(page_zip_dir_find_free(page_zip, page_offset(rec)));
3850
	ut_ad(page_zip_header_cmp(page_zip, page));
3851

3852
	heap_no = rec_get_heap_no_new(rec);
3853
	ut_ad(heap_no >= PAGE_HEAP_NO_USER_LOW);
3854

3855
	UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip));
3856 3857 3858
	UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets));
	UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets),
			   rec_offs_extra_size(offsets));
3859

3860 3861
	if (
#ifdef UNIV_ZIP_DEBUG
3862
	    !page_zip_clear_rec_disable &&
3863
#endif /* UNIV_ZIP_DEBUG */
3864 3865
	    page_zip->m_end
	    + 1 + ((heap_no - 1) >= 64)/* size of the log entry */
3866 3867
	    + page_zip_get_trailer_len(page_zip,
				       dict_index_is_clust(index), NULL)
3868
	    < page_zip_get_size(page_zip)) {
3869
		byte*	data;
3870 3871 3872

		/* Clear only the data bytes, because the allocator and
		the decompressor depend on the extra bytes. */
3873 3874
		memset(rec, 0, rec_offs_data_size(offsets));

marko's avatar
marko committed
3875 3876
		if (!page_is_leaf(page)) {
			/* Clear node_ptr on the compressed page. */
3877 3878
			byte*	storage	= page_zip->data
				+ page_zip_get_size(page_zip)
3879 3880
				- (page_dir_get_n_heap(page)
				   - PAGE_HEAP_NO_USER_LOW)
3881
				* PAGE_ZIP_DIR_SLOT_SIZE;
marko's avatar
marko committed
3882 3883

			memset(storage - (heap_no - 1) * REC_NODE_PTR_SIZE,
3884
			       0, REC_NODE_PTR_SIZE);
3885
		} else if (dict_index_is_clust(index)) {
3886
			/* Clear trx_id and roll_ptr on the compressed page. */
3887 3888
			byte*	storage	= page_zip->data
				+ page_zip_get_size(page_zip)
3889 3890
				- (page_dir_get_n_heap(page)
				   - PAGE_HEAP_NO_USER_LOW)
3891
				* PAGE_ZIP_DIR_SLOT_SIZE;
3892 3893

			memset(storage - (heap_no - 1)
3894 3895
			       * (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN),
			       0, DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
3896 3897
		}

3898
		/* Log that the data was zeroed out. */
3899 3900 3901
		data = page_zip->data + page_zip->m_end;
		ut_ad(!*data);
		if (UNIV_UNLIKELY(heap_no - 1 >= 64)) {
3902
			*data++ = (byte) (0x80 | (heap_no - 1) >> 7);
marko's avatar
marko committed
3903
			ut_ad(!*data);
3904
		}
3905
		*data++ = (byte) ((heap_no - 1) << 1 | 1);
3906
		ut_ad(!*data);
3907 3908
		ut_ad((ulint) (data - page_zip->data)
		      < page_zip_get_size(page_zip));
3909
		page_zip->m_end = data - page_zip->data;
3910
		page_zip->m_nonempty = TRUE;
3911
	} else if (page_is_leaf(page) && dict_index_is_clust(index)) {
marko's avatar
marko committed
3912 3913 3914
		/* Do not clear the record, because there is not enough space
		to log the operation. */

3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928
		if (rec_offs_any_extern(offsets)) {
			ulint	i;

			for (i = rec_offs_n_fields(offsets); i--; ) {
				/* Clear all BLOB pointers in order to make
				page_zip_validate() pass. */
				if (rec_offs_nth_extern(offsets, i)) {
					ulint	len;
					byte*	field = rec_get_nth_field(
						rec, offsets, i, &len);
					memset(field + len
					       - BTR_EXTERN_FIELD_REF_SIZE,
					       0, BTR_EXTERN_FIELD_REF_SIZE);
				}
marko's avatar
marko committed
3929
			}
3930 3931
		}
	}
marko's avatar
marko committed
3932 3933 3934 3935

#ifdef UNIV_ZIP_DEBUG
	ut_a(page_zip_validate(page_zip, page));
#endif /* UNIV_ZIP_DEBUG */
3936 3937 3938 3939 3940
}

/**************************************************************************
Write the "deleted" flag of a record on a compressed page.  The flag must
already have been written on the uncompressed page. */
3941
UNIV_INTERN
3942 3943 3944 3945 3946 3947 3948
void
page_zip_rec_set_deleted(
/*=====================*/
	page_zip_des_t*	page_zip,/* in/out: compressed page */
	const byte*	rec,	/* in: record on the uncompressed page */
	ulint		flag)	/* in: the deleted flag (nonzero=TRUE) */
{
3949
	byte*	slot = page_zip_dir_find(page_zip, page_offset(rec));
3950
	ut_a(slot);
3951
	UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip));
3952 3953 3954 3955 3956
	if (flag) {
		*slot |= (PAGE_ZIP_DIR_SLOT_DEL >> 8);
	} else {
		*slot &= ~(PAGE_ZIP_DIR_SLOT_DEL >> 8);
	}
marko's avatar
marko committed
3957 3958 3959
#ifdef UNIV_ZIP_DEBUG
	ut_a(page_zip_validate(page_zip, page_align(rec)));
#endif /* UNIV_ZIP_DEBUG */
3960 3961 3962 3963 3964
}

/**************************************************************************
Write the "owned" flag of a record on a compressed page.  The n_owned field
must already have been written on the uncompressed page. */
3965
UNIV_INTERN
3966 3967 3968 3969 3970 3971 3972
void
page_zip_rec_set_owned(
/*===================*/
	page_zip_des_t*	page_zip,/* in/out: compressed page */
	const byte*	rec,	/* in: record on the uncompressed page */
	ulint		flag)	/* in: the owned flag (nonzero=TRUE) */
{
3973
	byte*	slot = page_zip_dir_find(page_zip, page_offset(rec));
3974
	ut_a(slot);
3975
	UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip));
3976 3977 3978 3979 3980 3981 3982
	if (flag) {
		*slot |= (PAGE_ZIP_DIR_SLOT_OWNED >> 8);
	} else {
		*slot &= ~(PAGE_ZIP_DIR_SLOT_OWNED >> 8);
	}
}

3983 3984
/**************************************************************************
Insert a record to the dense page directory. */
3985
UNIV_INTERN
3986 3987 3988 3989 3990 3991 3992
void
page_zip_dir_insert(
/*================*/
	page_zip_des_t*	page_zip,/* in/out: compressed page */
	const byte*	prev_rec,/* in: record after which to insert */
	const byte*	free_rec,/* in: record from which rec was
				allocated, or NULL */
3993
	byte*		rec)	/* in: record to insert */
3994 3995 3996 3997 3998 3999 4000
{
	ulint	n_dense;
	byte*	slot_rec;
	byte*	slot_free;

	ut_ad(prev_rec != rec);
	ut_ad(page_rec_get_next((rec_t*) prev_rec) == rec);
4001
	ut_ad(page_zip_simple_validate(page_zip));
4002

4003 4004
	UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip));

4005 4006
	if (page_rec_is_infimum(prev_rec)) {
		/* Use the first slot. */
4007
		slot_rec = page_zip->data + page_zip_get_size(page_zip);
4008
	} else {
4009
		byte*	end	= page_zip->data + page_zip_get_size(page_zip);
4010 4011 4012 4013
		byte*	start	= end - page_zip_dir_user_size(page_zip);

		if (UNIV_LIKELY(!free_rec)) {
			/* PAGE_N_RECS was already incremented
4014
			in page_cur_insert_rec_zip(), but the
4015 4016 4017 4018 4019
			dense directory slot at that position
			contains garbage.  Skip it. */
			start += PAGE_ZIP_DIR_SLOT_SIZE;
		}

4020 4021
		slot_rec = page_zip_dir_find_low(start, end,
						 page_offset(prev_rec));
4022 4023 4024
		ut_a(slot_rec);
	}

4025 4026 4027
	/* Read the old n_dense (n_heap may have been incremented). */
	n_dense = page_dir_get_n_heap(page_zip->data)
		- (PAGE_HEAP_NO_USER_LOW + 1);
4028 4029 4030 4031 4032

	if (UNIV_LIKELY_NULL(free_rec)) {
		/* The record was allocated from the free list.
		Shift the dense directory only up to that slot.
		Note that in this case, n_dense is actually
4033
		off by one, because page_cur_insert_rec_zip()
4034 4035
		did not increment n_heap. */
		ut_ad(rec_get_heap_no_new(rec) < n_dense + 1
4036
		      + PAGE_HEAP_NO_USER_LOW);
4037
		ut_ad(rec >= free_rec);
4038
		slot_free = page_zip_dir_find(page_zip, page_offset(free_rec));
4039 4040 4041 4042 4043 4044
		ut_ad(slot_free);
		slot_free += PAGE_ZIP_DIR_SLOT_SIZE;
	} else {
		/* The record was allocated from the heap.
		Shift the entire dense directory. */
		ut_ad(rec_get_heap_no_new(rec) == n_dense
4045
		      + PAGE_HEAP_NO_USER_LOW);
4046 4047

		/* Shift to the end of the dense page directory. */
4048
		slot_free = page_zip->data + page_zip_get_size(page_zip)
4049
			- PAGE_ZIP_DIR_SLOT_SIZE * n_dense;
4050 4051 4052 4053
	}

	/* Shift the dense directory to allocate place for rec. */
	memmove(slot_free - PAGE_ZIP_DIR_SLOT_SIZE, slot_free,
4054
		slot_rec - slot_free);
4055 4056 4057

	/* Write the entry for the inserted record.
	The "owned" and "deleted" flags must be zero. */
4058
	mach_write_to_2(slot_rec - PAGE_ZIP_DIR_SLOT_SIZE, page_offset(rec));
4059 4060
}

4061
/**************************************************************************
4062 4063
Shift the dense page directory and the array of BLOB pointers
when a record is deleted. */
4064
UNIV_INTERN
4065 4066 4067 4068
void
page_zip_dir_delete(
/*================*/
	page_zip_des_t*	page_zip,/* in/out: compressed page */
4069 4070 4071
	byte*		rec,	/* in: record to delete */
	dict_index_t*	index,	/* in: index of rec */
	const ulint*	offsets,/* in: rec_get_offsets(rec) */
4072 4073 4074 4075
	const byte*	free)	/* in: previous start of the free list */
{
	byte*	slot_rec;
	byte*	slot_free;
4076
	ulint	n_ext;
4077
	page_t*	page	= page_align(rec);
4078

4079 4080
	ut_ad(rec_offs_validate(rec, index, offsets));
	ut_ad(rec_offs_comp(offsets));
marko's avatar
marko committed
4081

4082
	UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip));
4083 4084 4085
	UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets));
	UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets),
			   rec_offs_extra_size(offsets));
4086

4087
	slot_rec = page_zip_dir_find(page_zip, page_offset(rec));
4088 4089 4090

	ut_a(slot_rec);

4091 4092
	/* This could not be done before page_zip_dir_find(). */
	page_header_set_field(page, page_zip, PAGE_N_RECS,
4093
			      (ulint)(page_get_n_recs(page) - 1));
4094

marko's avatar
marko committed
4095
	if (UNIV_UNLIKELY(!free)) {
4096
		/* Make the last slot the start of the free list. */
4097
		slot_free = page_zip->data + page_zip_get_size(page_zip)
4098
			- PAGE_ZIP_DIR_SLOT_SIZE
4099 4100
			* (page_dir_get_n_heap(page_zip->data)
			   - PAGE_HEAP_NO_USER_LOW);
4101
	} else {
4102 4103
		slot_free = page_zip_dir_find_free(page_zip,
						   page_offset(free));
4104 4105 4106 4107 4108
		ut_a(slot_free < slot_rec);
		/* Grow the free list by one slot by moving the start. */
		slot_free += PAGE_ZIP_DIR_SLOT_SIZE;
	}

marko's avatar
marko committed
4109
	if (UNIV_LIKELY(slot_rec > slot_free)) {
4110 4111
		memmove(slot_free + PAGE_ZIP_DIR_SLOT_SIZE,
			slot_free,
marko's avatar
marko committed
4112
			slot_rec - slot_free);
4113 4114 4115 4116
	}

	/* Write the entry for the deleted record.
	The "owned" and "deleted" flags will be cleared. */
4117
	mach_write_to_2(slot_free, page_offset(rec));
4118

4119
	if (!page_is_leaf(page) || !dict_index_is_clust(index)) {
4120
		ut_ad(!rec_offs_any_extern(offsets));
4121
		goto skip_blobs;
4122 4123
	}

4124 4125 4126 4127 4128 4129 4130 4131 4132 4133
	n_ext = rec_offs_n_extern(offsets);
	if (UNIV_UNLIKELY(n_ext)) {
		/* Shift and zero fill the array of BLOB pointers. */
		ulint	blob_no;
		byte*	externs;
		byte*	ext_end;

		blob_no = page_zip_get_n_prev_extern(page_zip, rec, index);
		ut_a(blob_no + n_ext <= page_zip->n_blobs);

4134
		externs = page_zip->data + page_zip_get_size(page_zip)
4135
			- (page_dir_get_n_heap(page) - PAGE_HEAP_NO_USER_LOW)
4136 4137
			* (PAGE_ZIP_DIR_SLOT_SIZE
			   + DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
4138 4139

		ext_end = externs - page_zip->n_blobs
4140
			* BTR_EXTERN_FIELD_REF_SIZE;
4141 4142 4143 4144 4145
		externs -= blob_no * BTR_EXTERN_FIELD_REF_SIZE;

		page_zip->n_blobs -= n_ext;
		/* Shift and zero fill the array. */
		memmove(ext_end + n_ext * BTR_EXTERN_FIELD_REF_SIZE, ext_end,
4146 4147
			(page_zip->n_blobs - blob_no)
			* BTR_EXTERN_FIELD_REF_SIZE);
4148 4149 4150
		memset(ext_end, 0, n_ext * BTR_EXTERN_FIELD_REF_SIZE);
	}

4151 4152 4153 4154 4155
skip_blobs:
	/* The compression algorithm expects info_bits and n_owned
	to be 0 for deleted records. */
	rec[-REC_N_NEW_EXTRA_BYTES] = 0; /* info_bits and n_owned */

4156
	page_zip_clear_rec(page_zip, rec, index, offsets);
marko's avatar
marko committed
4157
}
4158

4159 4160
/**************************************************************************
Add a slot to the dense page directory. */
4161
UNIV_INTERN
4162 4163 4164
void
page_zip_dir_add_slot(
/*==================*/
4165 4166 4167
	page_zip_des_t*	page_zip,	/* in/out: compressed page */
	ulint		is_clustered)	/* in: nonzero for clustered index,
					zero for others */
4168 4169 4170 4171 4172
{
	ulint	n_dense;
	byte*	dir;
	byte*	stored;

4173
	ut_ad(page_is_comp(page_zip->data));
4174
	UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip));
4175

4176 4177 4178
	/* Read the old n_dense (n_heap has already been incremented). */
	n_dense = page_dir_get_n_heap(page_zip->data)
		- (PAGE_HEAP_NO_USER_LOW + 1);
4179

4180
	dir = page_zip->data + page_zip_get_size(page_zip)
4181
		- PAGE_ZIP_DIR_SLOT_SIZE * n_dense;
4182

4183 4184 4185 4186
	if (!page_is_leaf(page_zip->data)) {
		ut_ad(!page_zip->n_blobs);
		stored = dir - n_dense * REC_NODE_PTR_SIZE;
	} else if (UNIV_UNLIKELY(is_clustered)) {
4187 4188 4189 4190
		/* Move the BLOB pointer array backwards to make space for the
		roll_ptr and trx_id columns and the dense directory slot. */
		byte*	externs;

4191
		stored = dir - n_dense
4192 4193
			* (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
		externs = stored
4194
			- page_zip->n_blobs * BTR_EXTERN_FIELD_REF_SIZE;
4195 4196 4197 4198 4199
		ASSERT_ZERO(externs
			    - (PAGE_ZIP_DIR_SLOT_SIZE
			       + DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN),
			    PAGE_ZIP_DIR_SLOT_SIZE
			    + DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
4200
		memmove(externs - (PAGE_ZIP_DIR_SLOT_SIZE
4201
				   + DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN),
4202
			externs, stored - externs);
4203
	} else {
4204 4205
		stored = dir
			- page_zip->n_blobs * BTR_EXTERN_FIELD_REF_SIZE;
4206 4207
		ASSERT_ZERO(stored - PAGE_ZIP_DIR_SLOT_SIZE,
			    PAGE_ZIP_DIR_SLOT_SIZE);
4208 4209
	}

4210 4211 4212
	/* Move the uncompressed area backwards to make space
	for one directory slot. */
	memmove(stored - PAGE_ZIP_DIR_SLOT_SIZE, stored, dir - stored);
4213 4214
}

4215 4216
/***************************************************************
Parses a log record of writing to the header of a page. */
4217
UNIV_INTERN
4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229
byte*
page_zip_parse_write_header(
/*========================*/
				/* out: end of log record or NULL */
	byte*		ptr,	/* in: redo log buffer */
	byte*		end_ptr,/* in: redo log buffer end */
	page_t*		page,	/* in/out: uncompressed page */
	page_zip_des_t*	page_zip)/* in/out: compressed page */
{
	ulint	offset;
	ulint	len;

4230
	ut_ad(ptr && end_ptr);
4231 4232
	ut_ad(!page == !page_zip);

4233
	if (UNIV_UNLIKELY(end_ptr < ptr + (1 + 1))) {
4234 4235 4236 4237

		return(NULL);
	}

4238
	offset = (ulint) *ptr++;
4239 4240
	len = (ulint) *ptr++;

4241 4242
	if (UNIV_UNLIKELY(!len) || UNIV_UNLIKELY(offset + len >= PAGE_DATA)) {
corrupt:
4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253
		recv_sys->found_corrupt_log = TRUE;

		return(NULL);
	}

	if (UNIV_UNLIKELY(end_ptr < ptr + len)) {

		return(NULL);
	}

	if (page) {
4254 4255 4256 4257
		if (UNIV_UNLIKELY(!page_zip)) {

			goto corrupt;
		}
4258
#ifdef UNIV_ZIP_DEBUG
4259
		ut_a(page_zip_validate(page_zip, page));
4260
#endif /* UNIV_ZIP_DEBUG */
4261 4262 4263 4264

		memcpy(page + offset, ptr, len);
		memcpy(page_zip->data + offset, ptr, len);

4265
#ifdef UNIV_ZIP_DEBUG
4266
		ut_a(page_zip_validate(page_zip, page));
4267
#endif /* UNIV_ZIP_DEBUG */
4268 4269 4270 4271 4272
	}

	return(ptr + len);
}

4273 4274
/**************************************************************************
Write a log record of writing to the uncompressed header portion of a page. */
4275
UNIV_INTERN
4276 4277 4278
void
page_zip_write_header_log(
/*======================*/
4279 4280 4281
	const byte*	data,	/* in: data on the uncompressed page */
	ulint		length,	/* in: length of the data */
	mtr_t*		mtr)	/* in: mini-transaction */
4282
{
4283
	byte*	log_ptr	= mlog_open(mtr, 11 + 1 + 1);
4284
	ulint	offset	= page_offset(data);
4285

4286 4287 4288 4289 4290 4291 4292 4293
	ut_ad(offset < PAGE_DATA);
	ut_ad(offset + length < PAGE_DATA);
#if PAGE_DATA > 255
# error "PAGE_DATA > 255"
#endif
	ut_ad(length < 256);

	/* If no logging is requested, we may return now */
4294
	if (UNIV_UNLIKELY(!log_ptr)) {
4295 4296 4297 4298

		return;
	}

4299 4300
	log_ptr = mlog_write_initial_log_record_fast(
		(byte*) data, MLOG_ZIP_WRITE_HEADER, log_ptr, mtr);
4301
	*log_ptr++ = (byte) offset;
4302
	*log_ptr++ = (byte) length;
4303 4304
	mlog_close(mtr, log_ptr);

4305
	mlog_catenate_string(mtr, data, length);
4306
}
4307

4308 4309 4310
/**************************************************************************
Reorganize and compress a page.  This is a low-level operation for
compressed pages, to be used when page_zip_compress() fails.
4311
On success, a redo log entry MLOG_ZIP_PAGE_COMPRESS will be written.
4312 4313 4314 4315 4316
The function btr_page_reorganize() should be preferred whenever possible.
IMPORTANT: if page_zip_reorganize() is invoked on a leaf page of a
non-clustered index, the caller must update the insert buffer free
bits in the same mini-transaction in such a way that the modification
will be redo-logged. */
4317
UNIV_INTERN
4318 4319 4320 4321 4322 4323
ibool
page_zip_reorganize(
/*================*/
				/* out: TRUE on success, FALSE on failure;
				page and page_zip will be left intact
				on failure. */
4324 4325
	buf_block_t*	block,	/* in/out: page with compressed page;
				on the compressed page, in: size;
4326 4327
				out: data, n_blobs,
				m_start, m_end, m_nonempty */
4328 4329 4330
	dict_index_t*	index,	/* in: index of the B-tree node */
	mtr_t*		mtr)	/* in: mini-transaction */
{
4331 4332
	page_zip_des_t*	page_zip	= buf_block_get_page_zip(block);
	page_t*		page		= buf_block_get_frame(block);
4333
	buf_block_t*	temp_block;
4334 4335
	page_t*		temp_page;
	ulint		log_mode;
4336

marko's avatar
marko committed
4337
	ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
4338 4339
	ut_ad(page_is_comp(page));
	/* Note that page_zip_validate(page_zip, page) may fail here. */
4340 4341
	UNIV_MEM_ASSERT_RW(page, UNIV_PAGE_SIZE);
	UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip));
4342 4343 4344 4345

	/* Disable logging */
	log_mode = mtr_set_log_mode(mtr, MTR_LOG_NONE);

4346 4347
	temp_block = buf_block_alloc(0);
	temp_page = temp_block->frame;
4348

4349 4350
	btr_search_drop_page_hash_index(block);

4351 4352 4353 4354 4355 4356
	/* Copy the old page to temporary space */
	buf_frame_copy(temp_page, page);

	/* Recreate the page: note that global data on page (possible
	segment headers, next page-field, etc.) is preserved intact */

4357
	page_create(block, mtr, TRUE);
4358
	block->check_index_page_at_flush = TRUE;
4359 4360 4361 4362

	/* Copy the records from the temporary space to the recreated page;
	do not copy the lock bits yet */

4363 4364
	page_copy_rec_list_end_no_locks(block, temp_block,
					page_get_infimum_rec(temp_page),
4365
					index, mtr);
4366
	/* Copy max trx id to recreated page */
marko's avatar
marko committed
4367
	page_set_max_trx_id(block, NULL, page_get_max_trx_id(temp_page));
4368

4369 4370 4371
	/* Restore logging. */
	mtr_set_log_mode(mtr, log_mode);

4372 4373 4374 4375 4376
	if (UNIV_UNLIKELY(!page_zip_compress(page_zip, page, index, mtr))) {

		/* Restore the old page and exit. */
		buf_frame_copy(page, temp_page);

4377
		buf_block_free(temp_block);
4378 4379 4380
		return(FALSE);
	}

4381
	lock_move_reorganize_page(block, temp_block);
4382

4383
	buf_block_free(temp_block);
4384 4385 4386
	return(TRUE);
}

4387
/**************************************************************************
4388 4389
Copy the records of a page byte for byte.  Do not copy the page header
or trailer, except those B-tree header fields that are directly
4390 4391
related to the storage of records.  Also copy PAGE_MAX_TRX_ID.
NOTE: The caller must update the lock table and the adaptive hash index. */
4392
UNIV_INTERN
4393
void
4394 4395
page_zip_copy_recs(
/*===============*/
4396 4397
	page_zip_des_t*		page_zip,	/* out: copy of src_zip
						(n_blobs, m_start, m_end,
4398
						m_nonempty, data[0..size-1]) */
4399 4400 4401
	page_t*			page,		/* out: copy of src */
	const page_zip_des_t*	src_zip,	/* in: compressed page */
	const page_t*		src,		/* in: page */
4402
	dict_index_t*		index,		/* in: index of the B-tree */
4403 4404
	mtr_t*			mtr)		/* in: mini-transaction */
{
4405 4406
	ut_ad(mtr_memo_contains_page(mtr, page, MTR_MEMO_PAGE_X_FIX));
	ut_ad(mtr_memo_contains_page(mtr, (page_t*) src, MTR_MEMO_PAGE_X_FIX));
4407
#ifdef UNIV_ZIP_DEBUG
4408 4409 4410 4411 4412
	/* The B-tree operations that call this function may set
	FIL_PAGE_PREV or PAGE_LEVEL, causing a temporary min_rec_flag
	mismatch.  A strict page_zip_validate() will be executed later
	during the B-tree operations. */
	ut_a(page_zip_validate_low(src_zip, src, TRUE));
4413
#endif /* UNIV_ZIP_DEBUG */
4414
	ut_a(page_zip_get_size(page_zip) == page_zip_get_size(src_zip));
4415
	if (UNIV_UNLIKELY(src_zip->n_blobs)) {
4416
		ut_a(page_is_leaf(src));
4417 4418
		ut_a(dict_index_is_clust(index));
	}
4419

4420 4421 4422 4423 4424
	UNIV_MEM_ASSERT_W(page, UNIV_PAGE_SIZE);
	UNIV_MEM_ASSERT_W(page_zip->data, page_zip_get_size(page_zip));
	UNIV_MEM_ASSERT_RW(src, UNIV_PAGE_SIZE);
	UNIV_MEM_ASSERT_RW(src_zip->data, page_zip_get_size(page_zip));

4425
	/* Copy those B-tree page header fields that are related to
4426
	the records stored in the page.  Also copy the field
4427 4428 4429 4430 4431 4432
	PAGE_MAX_TRX_ID.  Skip the rest of the page header and
	trailer.  On the compressed page, there is no trailer. */
#if PAGE_MAX_TRX_ID + 8 != PAGE_HEADER_PRIV_END
# error "PAGE_MAX_TRX_ID + 8 != PAGE_HEADER_PRIV_END"
#endif
	memcpy(PAGE_HEADER + page, PAGE_HEADER + src,
4433
	       PAGE_HEADER_PRIV_END);
4434 4435 4436
	memcpy(PAGE_DATA + page, PAGE_DATA + src,
	       UNIV_PAGE_SIZE - PAGE_DATA - FIL_PAGE_DATA_END);
	memcpy(PAGE_HEADER + page_zip->data, PAGE_HEADER + src_zip->data,
4437
	       PAGE_HEADER_PRIV_END);
4438 4439
	memcpy(PAGE_DATA + page_zip->data, PAGE_DATA + src_zip->data,
	       page_zip_get_size(page_zip) - PAGE_DATA);
4440

4441 4442
	/* Copy all fields of src_zip to page_zip, except the pointer
	to the compressed data page. */
4443 4444 4445 4446 4447
	{
		page_zip_t*	data = page_zip->data;
		memcpy(page_zip, src_zip, sizeof *page_zip);
		page_zip->data = data;
	}
4448 4449
	ut_ad(page_zip_get_trailer_len(page_zip,
				       dict_index_is_clust(index), NULL)
4450
	      + page_zip->m_end < page_zip_get_size(page_zip));
4451

4452
	if (!page_is_leaf(src)
4453
	    && UNIV_UNLIKELY(mach_read_from_4(src + FIL_PAGE_PREV) == FIL_NULL)
4454 4455
	    && UNIV_LIKELY(mach_read_from_4(page
					    + FIL_PAGE_PREV) != FIL_NULL)) {
4456
		/* Clear the REC_INFO_MIN_REC_FLAG of the first user record. */
4457 4458
		ulint	offs = rec_get_next_offs(page + PAGE_NEW_INFIMUM,
						 TRUE);
4459 4460 4461
		if (UNIV_LIKELY(offs != PAGE_NEW_SUPREMUM)) {
			rec_t*	rec = page + offs;
			ut_a(rec[-REC_N_NEW_EXTRA_BYTES]
4462
			     & REC_INFO_MIN_REC_FLAG);
4463 4464 4465 4466
			rec[-REC_N_NEW_EXTRA_BYTES] &= ~ REC_INFO_MIN_REC_FLAG;
		}
	}

4467
#ifdef UNIV_ZIP_DEBUG
4468
	ut_a(page_zip_validate(page_zip, page));
4469
#endif /* UNIV_ZIP_DEBUG */
4470

4471
	page_zip_compress_write_log(page_zip, page, index, mtr);
4472 4473
}

4474 4475
/**************************************************************************
Parses a log record of compressing an index page. */
4476
UNIV_INTERN
4477 4478 4479 4480 4481 4482 4483 4484 4485 4486
byte*
page_zip_parse_compress(
/*====================*/
				/* out: end of log record or NULL */
	byte*		ptr,	/* in: buffer */
	byte*		end_ptr,/* in: buffer end */
	page_t*		page,	/* out: uncompressed page */
	page_zip_des_t*	page_zip)/* out: compressed page */
{
	ulint	size;
4487
	ulint	trailer_size;
4488 4489

	ut_ad(ptr && end_ptr);
4490
	ut_ad(!page == !page_zip);
4491

4492
	if (UNIV_UNLIKELY(ptr + (2 + 2) > end_ptr)) {
4493 4494 4495 4496 4497 4498

		return(NULL);
	}

	size = mach_read_from_2(ptr);
	ptr += 2;
4499 4500
	trailer_size = mach_read_from_2(ptr);
	ptr += 2;
4501

4502
	if (UNIV_UNLIKELY(ptr + 8 + size + trailer_size > end_ptr)) {
4503 4504 4505 4506 4507 4508

		return(NULL);
	}

	if (page) {
		if (UNIV_UNLIKELY(!page_zip)
4509
		    || UNIV_UNLIKELY(page_zip_get_size(page_zip) < size)) {
4510 4511 4512 4513 4514 4515
corrupt:
			recv_sys->found_corrupt_log = TRUE;

			return(NULL);
		}

4516 4517 4518 4519
		memcpy(page_zip->data + FIL_PAGE_PREV, ptr, 4);
		memcpy(page_zip->data + FIL_PAGE_NEXT, ptr + 4, 4);
		memcpy(page_zip->data + FIL_PAGE_TYPE, ptr + 8, size);
		memset(page_zip->data + FIL_PAGE_TYPE + size, 0,
4520
		       page_zip_get_size(page_zip) - trailer_size
4521
		       - (FIL_PAGE_TYPE + size));
4522 4523
		memcpy(page_zip->data + page_zip_get_size(page_zip)
		       - trailer_size, ptr + 8 + size, trailer_size);
4524

4525 4526 4527 4528 4529 4530
		if (UNIV_UNLIKELY(!page_zip_decompress(page_zip, page))) {

			goto corrupt;
		}
	}

4531
	return(ptr + 8 + size + trailer_size);
4532
}
4533 4534 4535

/**************************************************************************
Calculate the compressed page checksum. */
4536
UNIV_INTERN
4537 4538 4539 4540 4541 4542 4543
ulint
page_zip_calc_checksum(
/*===================*/
				/* out: page checksum */
	const void*	data,	/* in: compressed page */
	ulint		size)	/* in: size of compressed page */
{
4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558
	/* Exclude FIL_PAGE_SPACE_OR_CHKSUM, FIL_PAGE_LSN,
	and FIL_PAGE_FILE_FLUSH_LSN from the checksum. */

	const Bytef*	s	= data;
	uLong		adler;

	ut_ad(size > FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);

	adler = adler32(0L, s + FIL_PAGE_OFFSET,
			FIL_PAGE_LSN - FIL_PAGE_OFFSET);
	adler = adler32(adler, s + FIL_PAGE_TYPE, 2);
	adler = adler32(adler, s + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID,
			size - FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);

	return((ulint) adler);
4559
}