summaryrefslogtreecommitdiff
path: root/innobase/include/rem0rec.ic
blob: 9dfd4faeec87e0527708d0d965070c472439adf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
/************************************************************************
Record manager

(c) 1994-1996 Innobase Oy

Created 5/30/1994 Heikki Tuuri
*************************************************************************/

#include "mach0data.h"
#include "ut0byte.h"

/* Offsets of the bit-fields in the record. NOTE! In the table the most 
significant bytes and bits are written below less significant.

	(1) byte offset		(2) bit usage within byte
	downward from
	origin ->	1	8 bits pointer to next record
			2	8 bits pointer to next record
			3  	1 bit short flag
				7 bits number of fields
			4	3 bits number of fields
				5 bits heap number
			5	8 bits heap number
			6	4 bits n_owned
				4 bits info bits
*/

/* We list the byte offsets from the origin of the record, the mask,
and the shift needed to obtain each bit-field of the record. */

#define REC_NEXT		2
#define REC_NEXT_MASK		0xFFFF
#define REC_NEXT_SHIFT		0

#define REC_SHORT		3	/* This is single byte bit-field */
#define	REC_SHORT_MASK		0x1
#define REC_SHORT_SHIFT		0

#define	REC_N_FIELDS		4
#define REC_N_FIELDS_MASK	0x7FE
#define	REC_N_FIELDS_SHIFT	1

#define	REC_HEAP_NO		5
#define REC_HEAP_NO_MASK	0xFFF8
#define	REC_HEAP_NO_SHIFT	3

#define REC_N_OWNED		6	/* This is single byte bit-field */
#define	REC_N_OWNED_MASK	0xF
#define REC_N_OWNED_SHIFT	0

#define	REC_INFO_BITS_MASK	0xF0
#define REC_INFO_BITS_SHIFT	0

/* The deleted flag in info bits */
#define REC_INFO_DELETED_FLAG 	0x20	/* when bit is set to 1, it means the
					record has been delete marked */
/* The following masks are used to filter the SQL null bit from
one-byte and two-byte offsets */

#define REC_1BYTE_SQL_NULL_MASK	0x80
#define REC_2BYTE_SQL_NULL_MASK	0x8000

/* In a 2-byte offset the second most significant bit denotes
a field stored to another page: */

#define REC_2BYTE_EXTERN_MASK	0x4000

/****************************************************************
Return field length or UNIV_SQL_NULL. */
UNIV_INLINE
ulint
rec_get_nth_field_len(
/*==================*/
 			/* out: length of the field; UNIV_SQL_NULL if SQL
			null */
 	rec_t*	rec, 	/* in: record */
 	ulint	n)	/* in: index of the field */
{
	ulint	len;

	rec_get_nth_field(rec, n, &len);

	return(len);
}

/***************************************************************
Sets the value of the ith field SQL null bit. */

void
rec_set_nth_field_null_bit(
/*=======================*/
	rec_t*	rec,	/* in: record */
	ulint	i,	/* in: ith field */
	ibool	val);	/* in: value to set */
/*************************************************************** 
Sets a record field to SQL null. The physical size of the field is not
changed. */

void
rec_set_nth_field_sql_null(
/*=======================*/
	rec_t*	rec, 	/* in: record */
	ulint	n);	/* in: index of the field */

/**********************************************************
Gets a bit field from within 1 byte. */
UNIV_INLINE
ulint
rec_get_bit_field_1(
/*================*/
	rec_t*	rec,	/* in: pointer to record origin */
	ulint	offs,	/* in: offset from the origin down */
	ulint	mask,	/* in: mask used to filter bits */
	ulint	shift)	/* in: shift right applied after masking */
{
	ut_ad(rec);

	return((mach_read_from_1(rec - offs) & mask) >> shift);
}

/**********************************************************
Sets a bit field within 1 byte. */
UNIV_INLINE
void
rec_set_bit_field_1(
/*================*/
	rec_t*	rec,	/* in: pointer to record origin */
	ulint	val,	/* in: value to set */
	ulint	offs,	/* in: offset from the origin down */
	ulint	mask,	/* in: mask used to filter bits */
	ulint	shift)	/* in: shift right applied after masking */
{
	ut_ad(rec);
	ut_ad(offs <= REC_N_EXTRA_BYTES);
	ut_ad(mask);
	ut_ad(mask <= 0xFF);
	ut_ad(((mask >> shift) << shift) == mask);
	ut_ad(((val << shift) & mask) == (val << shift));
	
	mach_write_to_1(rec - offs, 
			(mach_read_from_1(rec - offs) & ~mask) 
			| (val << shift));
}

/**********************************************************
Gets a bit field from within 2 bytes. */
UNIV_INLINE
ulint
rec_get_bit_field_2(
/*================*/
	rec_t*	rec,	/* in: pointer to record origin */
	ulint	offs,	/* in: offset from the origin down */
	ulint	mask,	/* in: mask used to filter bits */
	ulint	shift)	/* in: shift right applied after masking */
{
	ut_ad(rec);

	return((mach_read_from_2(rec - offs) & mask) >> shift);
}

/**********************************************************
Sets a bit field within 2 bytes. */
UNIV_INLINE
void
rec_set_bit_field_2(
/*================*/
	rec_t*	rec,	/* in: pointer to record origin */
	ulint	val,	/* in: value to set */
	ulint	offs,	/* in: offset from the origin down */
	ulint	mask,	/* in: mask used to filter bits */
	ulint	shift)	/* in: shift right applied after masking */
{
	ut_ad(rec);
	ut_ad(offs <= REC_N_EXTRA_BYTES);
	ut_ad(mask > 0xFF);
	ut_ad(mask <= 0xFFFF);
	ut_ad((mask >> shift) & 1);
	ut_ad(0 == ((mask >> shift) & ((mask >> shift) + 1)));
	ut_ad(((mask >> shift) << shift) == mask);
	ut_ad(((val << shift) & mask) == (val << shift));
#ifdef UNIV_DEBUG
      {
	ulint	m;

	/* The following assertion checks that the masks of currently
	defined bit-fields in bytes 3-6 do not overlap. */
	m = (ulint)((REC_SHORT_MASK << (8 * (REC_SHORT - 3)))
			   + (REC_N_FIELDS_MASK << (8 * (REC_N_FIELDS - 4)))
			   + (REC_HEAP_NO_MASK << (8 * (REC_HEAP_NO - 4)))
			   + (REC_N_OWNED_MASK << (8 * (REC_N_OWNED - 3)))
			   + (REC_INFO_BITS_MASK << (8 * (REC_INFO_BITS - 3))));
	if (m != ut_dbg_zero + 0xFFFFFFFF) {
		printf("Sum of masks %lx\n", m);
		ut_error;
	}
      }
#endif	
	mach_write_to_2(rec - offs, 
			(mach_read_from_2(rec - offs) & ~mask) 
			| (val << shift));
}

/**********************************************************
The following function is used to get the offset of the next chained record
on the same page. */
UNIV_INLINE
ulint 
rec_get_next_offs(
/*==============*/
			/* out: the page offset of the next chained record */
	rec_t*	rec)	/* in: physical record */
{	
	ulint	ret;

	ut_ad(rec);

	ret = rec_get_bit_field_2(rec, REC_NEXT, REC_NEXT_MASK,
							REC_NEXT_SHIFT);
	ut_ad(ret < UNIV_PAGE_SIZE);

	return(ret);
}

/**********************************************************
The following function is used to set the next record offset field of the
record. */
UNIV_INLINE
void
rec_set_next_offs(
/*==============*/
	rec_t*	rec,	/* in: physical record */
	ulint	next)	/* in: offset of the next record */
{
	ut_ad(rec);
	ut_ad(UNIV_PAGE_SIZE > next);

	rec_set_bit_field_2(rec, next, REC_NEXT, REC_NEXT_MASK,
							REC_NEXT_SHIFT);
}

/**********************************************************
The following function is used to get the number of fields in the record. */
UNIV_INLINE
ulint
rec_get_n_fields(
/*=============*/
			/* out: number of data fields */
	rec_t*	rec)	/* in: physical record */
{
	ulint	ret;

	ut_ad(rec);

	ret = rec_get_bit_field_2(rec, REC_N_FIELDS, REC_N_FIELDS_MASK,
							REC_N_FIELDS_SHIFT);
	ut_ad(ret <= REC_MAX_N_FIELDS);
	ut_ad(ret > 0);

	return(ret);
}	

/**********************************************************
The following function is used to set the number of fields field in the
record. */
UNIV_INLINE
void
rec_set_n_fields(
/*=============*/
	rec_t*	rec,		/* in: physical record */
	ulint	n_fields)	/* in: the number of fields */
{
	ut_ad(rec);
	ut_ad(n_fields <= REC_MAX_N_FIELDS);
	ut_ad(n_fields > 0);

	rec_set_bit_field_2(rec, n_fields, REC_N_FIELDS, REC_N_FIELDS_MASK,
							REC_N_FIELDS_SHIFT);
}

/**********************************************************
The following function is used to get the number of records owned by the
previous directory record. */
UNIV_INLINE
ulint
rec_get_n_owned(
/*============*/
			/* out: number of owned records */
	rec_t*	rec)	/* in: physical record */
{
	ulint	ret;

	ut_ad(rec);

	ret = rec_get_bit_field_1(rec, REC_N_OWNED, REC_N_OWNED_MASK,
							REC_N_OWNED_SHIFT);
	ut_ad(ret <= REC_MAX_N_OWNED); 

	return(ret);
}	

/**********************************************************
The following function is used to set the number of owned records. */
UNIV_INLINE
void
rec_set_n_owned(
/*============*/
	rec_t*	rec,		/* in: physical record */
	ulint	n_owned)	/* in: the number of owned */
{
	ut_ad(rec);
	ut_ad(n_owned <= REC_MAX_N_OWNED);

	rec_set_bit_field_1(rec, n_owned, REC_N_OWNED, REC_N_OWNED_MASK,
							REC_N_OWNED_SHIFT);
}

/**********************************************************
The following function is used to retrieve the info bits of a record. */
UNIV_INLINE
ulint
rec_get_info_bits(
/*==============*/
			/* out: info bits */
	rec_t*	rec)	/* in: physical record */
{
	ulint	ret;

	ut_ad(rec);

	ret = rec_get_bit_field_1(rec, REC_INFO_BITS, REC_INFO_BITS_MASK,
							REC_INFO_BITS_SHIFT);
	ut_ad((ret & ~REC_INFO_BITS_MASK) == 0);

	return(ret);
}	

/**********************************************************
The following function is used to set the info bits of a record. */
UNIV_INLINE
void
rec_set_info_bits(
/*==============*/
	rec_t*	rec,	/* in: physical record */
	ulint	bits)	/* in: info bits */
{
	ut_ad(rec);
	ut_ad((bits & ~REC_INFO_BITS_MASK) == 0);

	rec_set_bit_field_1(rec, bits, REC_INFO_BITS, REC_INFO_BITS_MASK,
							REC_INFO_BITS_SHIFT);
}

/**********************************************************
Gets the value of the deleted flag in info bits. */
UNIV_INLINE
ibool
rec_info_bits_get_deleted_flag(
/*===========================*/
				/* out: TRUE if deleted flag set */
	ulint	info_bits)	/* in: info bits from a record */
{
	if (info_bits & REC_INFO_DELETED_FLAG) {

		return(TRUE);
	}

	return(FALSE);
}

/**********************************************************
The following function tells if record is delete marked. */
UNIV_INLINE
ibool
rec_get_deleted_flag(
/*=================*/
			/* out: TRUE if delete marked */
	rec_t*	rec)	/* in: physical record */
{
	if (REC_INFO_DELETED_FLAG & rec_get_info_bits(rec)) {

		return(TRUE);
	}

	return(FALSE);
}

/**********************************************************
The following function is used to set the deleted bit. */
UNIV_INLINE
void
rec_set_deleted_flag(
/*=================*/
	rec_t*	rec,	/* in: physical record */
	ibool	flag)	/* in: TRUE if delete marked */
{
	ulint	old_val;
	ulint	new_val;

	ut_ad(TRUE == 1);
	ut_ad(flag <= TRUE);

	old_val = rec_get_info_bits(rec);
	
	if (flag) {
		new_val = REC_INFO_DELETED_FLAG | old_val;
	} else {
		new_val = ~REC_INFO_DELETED_FLAG & old_val;
	}

	rec_set_info_bits(rec, new_val);
}

/**********************************************************
The following function is used to get the order number of the record in the
heap of the index page. */
UNIV_INLINE
ulint
rec_get_heap_no(
/*=============*/
			/* out: heap order number */
	rec_t*	rec)	/* in: physical record */
{
	ulint	ret;

	ut_ad(rec);

	ret = rec_get_bit_field_2(rec, REC_HEAP_NO, REC_HEAP_NO_MASK,
							REC_HEAP_NO_SHIFT);
	ut_ad(ret <= REC_MAX_HEAP_NO);

	return(ret);
}	

/**********************************************************
The following function is used to set the heap number field in the record. */
UNIV_INLINE
void
rec_set_heap_no(
/*=============*/
	rec_t*	rec,	/* in: physical record */
	ulint	heap_no)/* in: the heap number */
{
	ut_ad(heap_no <= REC_MAX_HEAP_NO);

	rec_set_bit_field_2(rec, heap_no, REC_HEAP_NO, REC_HEAP_NO_MASK,
							REC_HEAP_NO_SHIFT);
}

/**********************************************************
The following function is used to test whether the data offsets in the record
are stored in one-byte or two-byte format. */
UNIV_INLINE
ibool
rec_get_1byte_offs_flag(
/*====================*/
			/* out: TRUE if 1-byte form */
	rec_t*	rec)	/* in: physical record */
{
	ut_ad(TRUE == 1);

	return(rec_get_bit_field_1(rec, REC_SHORT, REC_SHORT_MASK,
							REC_SHORT_SHIFT));
}

/**********************************************************
The following function is used to set the 1-byte offsets flag. */
UNIV_INLINE
void
rec_set_1byte_offs_flag(
/*====================*/
	rec_t*	rec,	/* in: physical record */
	ibool	flag)	/* in: TRUE if 1byte form */
{
	ut_ad(TRUE == 1);
	ut_ad(flag <= TRUE);

	rec_set_bit_field_1(rec, flag, REC_SHORT, REC_SHORT_MASK,
							REC_SHORT_SHIFT);
}

/**********************************************************
Returns the offset of nth field end if the record is stored in the 1-byte
offsets form. If the field is SQL null, the flag is ORed in the returned
value. */
UNIV_INLINE
ulint
rec_1_get_field_end_info(
/*=====================*/
 			/* out: offset of the start of the field, SQL null
 			flag ORed */
 	rec_t*	rec, 	/* in: record */
 	ulint	n)	/* in: field index */
{
	ut_ad(rec_get_1byte_offs_flag(rec));
	ut_ad(n < rec_get_n_fields(rec));

	return(mach_read_from_1(rec - (REC_N_EXTRA_BYTES + n + 1)));
}
						
/**********************************************************
Returns the offset of nth field end if the record is stored in the 2-byte
offsets form. If the field is SQL null, the flag is ORed in the returned
value. */
UNIV_INLINE
ulint
rec_2_get_field_end_info(
/*=====================*/
 			/* out: offset of the start of the field, SQL null
 			flag and extern storage flag ORed */
 	rec_t*	rec, 	/* in: record */
 	ulint	n)	/* in: field index */
{
	ut_ad(!rec_get_1byte_offs_flag(rec));
	ut_ad(n < rec_get_n_fields(rec));

	return(mach_read_from_2(rec - (REC_N_EXTRA_BYTES + 2 * n + 2)));
}

/***************************************************************
Gets the value of the ith field extern storage bit. If it is TRUE
it means that the field is stored on another page. */
UNIV_INLINE
ibool
rec_get_nth_field_extern_bit(
/*=========================*/
			/* in: TRUE or FALSE */
	rec_t*	rec,	/* in: record */
	ulint	i)	/* in: ith field */
{
	ulint	info;

	if (rec_get_1byte_offs_flag(rec)) {

		return(FALSE);
	}

	info = rec_2_get_field_end_info(rec, i);

	if (info & REC_2BYTE_EXTERN_MASK) {
		return(TRUE);
	}

	return(FALSE);
}

/**********************************************************
Returns TRUE if the extern bit is set in any of the fields
of rec. */
UNIV_INLINE
ibool
rec_contains_externally_stored_field(
/*=================================*/
			/* out: TRUE if a field is stored externally */
	rec_t*	rec)	/* in: record */
{
	ulint	n;
	ulint	i;
	
	if (rec_get_1byte_offs_flag(rec)) {

		return(FALSE);
	}

	n = rec_get_n_fields(rec);

	for (i = 0; i < n; i++) {
		if (rec_get_nth_field_extern_bit(rec, i)) {

			return(TRUE);
		}
	}

	return(FALSE);
}

/**********************************************************
Returns the offset of n - 1th field end if the record is stored in the 1-byte
offsets form. If the field is SQL null, the flag is ORed in the returned
value. This function and the 2-byte counterpart are defined here because the
C-compilerwas not able to sum negative and positive constant offsets, and
warned of constant arithmetic overflow within the compiler. */
UNIV_INLINE
ulint
rec_1_get_prev_field_end_info(
/*==========================*/
 			/* out: offset of the start of the PREVIOUS field, SQL
			null flag ORed */
 	rec_t*	rec, 	/* in: record */
 	ulint	n)	/* in: field index */
{
	ut_ad(rec_get_1byte_offs_flag(rec));
	ut_ad(n <= rec_get_n_fields(rec));

	return(mach_read_from_1(rec - (REC_N_EXTRA_BYTES + n)));
}
						
/**********************************************************
Returns the offset of n - 1th field end if the record is stored in the 2-byte
offsets form. If the field is SQL null, the flag is ORed in the returned
value. */
UNIV_INLINE
ulint
rec_2_get_prev_field_end_info(
/*==========================*/
 			/* out: offset of the start of the PREVIOUS field, SQL
			null flag ORed */
 	rec_t*	rec, 	/* in: record */
 	ulint	n)	/* in: field index */
{
	ut_ad(!rec_get_1byte_offs_flag(rec));
	ut_ad(n <= rec_get_n_fields(rec));

	return(mach_read_from_2(rec - (REC_N_EXTRA_BYTES + 2 * n)));
}

/**********************************************************
Sets the field end info for the nth field if the record is stored in the
1-byte format. */
UNIV_INLINE
void
rec_1_set_field_end_info(
/*=====================*/
 	rec_t*	rec, 	/* in: record */
 	ulint	n,	/* in: field index */
 	ulint	info)	/* in: value to set */
{
	ut_ad(rec_get_1byte_offs_flag(rec));
	ut_ad(n < rec_get_n_fields(rec));

	mach_write_to_1(rec - (REC_N_EXTRA_BYTES + n + 1), info);
}

/**********************************************************
Sets the field end info for the nth field if the record is stored in the
2-byte format. */
UNIV_INLINE
void
rec_2_set_field_end_info(
/*=====================*/
 	rec_t*	rec, 	/* in: record */
 	ulint	n,	/* in: field index */
 	ulint	info)	/* in: value to set */
{
	ut_ad(!rec_get_1byte_offs_flag(rec));
	ut_ad(n < rec_get_n_fields(rec));

	mach_write_to_2(rec - (REC_N_EXTRA_BYTES + 2 * n + 2), info);
}

/**********************************************************
Returns the offset of nth field start if the record is stored in the 1-byte
offsets form. */
UNIV_INLINE
ulint
rec_1_get_field_start_offs(
/*=======================*/
 			/* out: offset of the start of the field */
 	rec_t*	rec, 	/* in: record */
 	ulint	n)	/* in: field index */
{
	ut_ad(rec_get_1byte_offs_flag(rec));
	ut_ad(n <= rec_get_n_fields(rec));

	if (n == 0) {

		return(0);
	}

	return(rec_1_get_prev_field_end_info(rec, n)
						& ~REC_1BYTE_SQL_NULL_MASK);
}
						
/**********************************************************
Returns the offset of nth field start if the record is stored in the 2-byte
offsets form. */
UNIV_INLINE
ulint
rec_2_get_field_start_offs(
/*=======================*/
 			/* out: offset of the start of the field */
 	rec_t*	rec, 	/* in: record */
 	ulint	n)	/* in: field index */
{
	ut_ad(!rec_get_1byte_offs_flag(rec));
	ut_ad(n <= rec_get_n_fields(rec));

	if (n == 0) {

		return(0);
	}

	return(rec_2_get_prev_field_end_info(rec, n)
		& ~(REC_2BYTE_SQL_NULL_MASK | REC_2BYTE_EXTERN_MASK));
}
						
/**********************************************************
The following function is used to read the offset of the start of a data field
in the record. The start of an SQL null field is the end offset of the
previous non-null field, or 0, if none exists. If n is the number of the last
field + 1, then the end offset of the last field is returned. */
UNIV_INLINE
ulint
rec_get_field_start_offs(
/*=====================*/
 			/* out: offset of the start of the field */
 	rec_t*	rec, 	/* in: record */
 	ulint	n)	/* in: field index */
{
	ut_ad(rec);
	ut_ad(n <= rec_get_n_fields(rec));

	if (n == 0) {

		return(0);
	}

	if (rec_get_1byte_offs_flag(rec)) {

		return(rec_1_get_field_start_offs(rec, n));
	}

	return(rec_2_get_field_start_offs(rec, n));
}

/****************************************************************
Gets the physical size of a field. Also an SQL null may have a field of
size > 0, if the data type is of a fixed size. */
UNIV_INLINE
ulint
rec_get_nth_field_size(
/*===================*/
			/* out: field size in bytes */
 	rec_t*	rec, 	/* in: record */
 	ulint	n)	/* in: index of the field */
{
	ulint	os;
	ulint	next_os;

	os = rec_get_field_start_offs(rec, n);
	next_os = rec_get_field_start_offs(rec, n + 1);

	ut_ad(next_os - os < UNIV_PAGE_SIZE);

	return(next_os - os);
}

/****************************************************************
The following function is used to get a copy of the nth data field in a
record to a buffer. */
UNIV_INLINE
void
rec_copy_nth_field(
/*===============*/
 	void*	buf,	/* in: pointer to the buffer */
 	rec_t*	rec, 	/* in: record */
 	ulint	n,	/* in: index of the field */
	ulint*	len)	/* out: length of the field; UNIV_SQL_NULL if SQL 
			null */
{
	byte*	ptr;
	
	ut_ad(buf && rec && len);

	ptr = rec_get_nth_field(rec, n, len);

	if (*len == UNIV_SQL_NULL) {

		return;
	}

	ut_memcpy(buf, ptr, *len);
}

/*************************************************************** 
This is used to modify the value of an already existing field in a record.
The previous value must have exactly the same size as the new value. If len
is UNIV_SQL_NULL then the field is treated as an SQL null. */
UNIV_INLINE
void
rec_set_nth_field(
/*==============*/
	rec_t*	rec, 	/* in: record */
	ulint	n,	/* in: index of the field */
	void*	data,	/* in: pointer to the data if not SQL null */
	ulint	len)	/* in: length of the data or UNIV_SQL_NULL */
{
	byte*	data2;
	ulint	len2;

	ut_ad((len == UNIV_SQL_NULL)
				|| (rec_get_nth_field_size(rec, n) == len));
	
	if (len == UNIV_SQL_NULL) {
		rec_set_nth_field_sql_null(rec, n);

		return;
	}

	data2 = rec_get_nth_field(rec, n, &len2);

	ut_memcpy(data2, data, len);

	if (len2 == UNIV_SQL_NULL) {
	
		rec_set_nth_field_null_bit(rec, n, FALSE);
	}
}

/************************************************************** 
The following function returns the data size of a physical
record, that is the sum of field lengths. SQL null fields
are counted as length 0 fields. The value returned by the function
is the distance from record origin to record end in bytes. */
UNIV_INLINE
ulint
rec_get_data_size(
/*==============*/
			/* out: size */
	rec_t*	rec)	/* in: physical record */
{
	ut_ad(rec);

	return(rec_get_field_start_offs(rec, rec_get_n_fields(rec)));
}

/************************************************************** 
Returns the total size of record minus data size of record. The value
returned by the function is the distance from record start to record origin
in bytes. */
UNIV_INLINE
ulint
rec_get_extra_size(
/*===============*/
			/* out: size */
	rec_t*	rec)	/* in: physical record */
{
	ulint	n_fields;
	
	ut_ad(rec);

	n_fields = rec_get_n_fields(rec);

	if (rec_get_1byte_offs_flag(rec)) {

		return(REC_N_EXTRA_BYTES + n_fields);
	}

	return(REC_N_EXTRA_BYTES + 2 * n_fields);
}

/************************************************************** 
Returns the total size of a physical record.  */
UNIV_INLINE
ulint
rec_get_size(
/*=========*/
			/* out: size */
	rec_t*	rec)	/* in: physical record */
{
	ulint	n_fields;
	
	ut_ad(rec);

	n_fields = rec_get_n_fields(rec);

	if (rec_get_1byte_offs_flag(rec)) {

		return(REC_N_EXTRA_BYTES + n_fields
				+ rec_1_get_field_start_offs(rec, n_fields));
	}
	
	return(REC_N_EXTRA_BYTES + 2 * n_fields
				+ rec_2_get_field_start_offs(rec, n_fields));
}
	
/**************************************************************
Returns a pointer to the end of the record. */
UNIV_INLINE
byte*
rec_get_end(
/*========*/
			/* out: pointer to end */
	rec_t*	rec)	/* in: pointer to record */
{
	return(rec + rec_get_data_size(rec));
}

/**************************************************************
Returns a pointer to the start of the record. */
UNIV_INLINE
byte*
rec_get_start(
/*==========*/
			/* out: pointer to start */
	rec_t*	rec)	/* in: pointer to record */
{
	return(rec - rec_get_extra_size(rec));
}

/*******************************************************************
Copies a physical record to a buffer. */
UNIV_INLINE
rec_t*
rec_copy(
/*=====*/
			/* out: pointer to the origin of the copied record */
	void*	buf,	/* in: buffer */
	rec_t*	rec)	/* in: physical record */
{
	ulint	extra_len;
	ulint	data_len;
	
	ut_ad(rec && buf);
	ut_ad(rec_validate(rec));

	extra_len = rec_get_extra_size(rec);
	data_len = rec_get_data_size(rec);

	ut_memcpy(buf, rec - extra_len, extra_len + data_len);

	return((byte*)buf + extra_len);
}

/**************************************************************
Returns the extra size of a physical record if we know its data size and
the number of fields. */
UNIV_INLINE
ulint
rec_get_converted_extra_size(
/*=========================*/
				/* out: extra size */
	ulint	data_size,	/* in: data size */
	ulint	n_fields)	/* in: number of fields */
{
	if (data_size <= REC_1BYTE_OFFS_LIMIT) {

		return(REC_N_EXTRA_BYTES + n_fields);
	}

	return(REC_N_EXTRA_BYTES + 2 * n_fields);
}

/**************************************************************
The following function returns the size of a data tuple when converted to
a physical record. */
UNIV_INLINE
ulint
rec_get_converted_size(
/*===================*/
				/* out: size */
	dtuple_t*	dtuple)	/* in: data tuple */
{
	ulint	data_size;
	ulint	extra_size;
	
	ut_ad(dtuple);
	ut_ad(dtuple_check_typed(dtuple));

	data_size = dtuple_get_data_size(dtuple);

	extra_size = rec_get_converted_extra_size(
				data_size, dtuple_get_n_fields(dtuple));

	return(data_size + extra_size);
}

/****************************************************************
Folds a prefix of a physical record to a ulint. Folds only existing fields,
that is, checks that we do not run out of the record. */
UNIV_INLINE
ulint
rec_fold(
/*=====*/
				/* out: the folded value */
	rec_t*	rec,		/* in: the physical record */
	ulint	n_fields,	/* in: number of complete fields to fold */
	ulint	n_bytes,	/* in: number of bytes to fold in an
				incomplete last field */
	dulint	tree_id)	/* in: index tree id */
{
	ulint	i;
	byte*	data;
	ulint	len;
	ulint	fold;
	ulint	n_fields_rec;

	ut_ad(rec_validate(rec));
	ut_ad(n_fields <= rec_get_n_fields(rec));
	ut_ad((n_fields < rec_get_n_fields(rec)) || (n_bytes == 0));
	ut_ad(n_fields + n_bytes > 0);
	
	n_fields_rec = rec_get_n_fields(rec);

	if (n_fields > n_fields_rec) {
	        n_fields = n_fields_rec;
	}

	if (n_fields == n_fields_rec) {
	        n_bytes = 0;
	}

	fold = ut_fold_dulint(tree_id);
	
	for (i = 0; i < n_fields; i++) {
		data = rec_get_nth_field(rec, i, &len);

		if (len != UNIV_SQL_NULL) {
			fold = ut_fold_ulint_pair(fold,
						  ut_fold_binary(data, len));
		}
	}

	if (n_bytes > 0) {
		data = rec_get_nth_field(rec, i, &len);

		if (len != UNIV_SQL_NULL) {
			if (len > n_bytes) {
				len = n_bytes;
			}
		
			fold = ut_fold_ulint_pair(fold,
						  ut_fold_binary(data, len));
		}
	}

	return(fold);
}

/*************************************************************
Builds a physical record out of a data tuple and stores it beginning from
the address destination. */
UNIV_INLINE
rec_t* 	
rec_convert_dtuple_to_rec(
/*======================*/			
				/* out: pointer to the origin of physical
				record */
	byte*	destination,	/* in: start address of the physical record */
	dtuple_t* dtuple)	/* in: data tuple */
{
	return(rec_convert_dtuple_to_rec_low(destination, dtuple,
						dtuple_get_data_size(dtuple)));
}