rem0rec.ic
Upload User: tsgydb
Upload Date: 2007-04-14
Package Size: 10674k
Code Size: 24k
Category:

MySQL

Development Platform:

Visual C++

  1. /************************************************************************
  2. Record manager
  3. (c) 1994-1996 Innobase Oy
  4. Created 5/30/1994 Heikki Tuuri
  5. *************************************************************************/
  6. #include "mach0data.h"
  7. #include "ut0byte.h"
  8. /* Offsets of the bit-fields in the record. NOTE! In the table the most 
  9. significant bytes and bits are written below less significant.
  10. (1) byte offset (2) bit usage within byte
  11. downward from
  12. origin -> 1 8 bits pointer to next record
  13. 2 8 bits pointer to next record
  14. 3   1 bit short flag
  15. 7 bits number of fields
  16. 4 3 bits number of fields
  17. 5 bits heap number
  18. 5 8 bits heap number
  19. 6 4 bits n_owned
  20. 4 bits info bits
  21. */
  22. /* Maximum lengths for the data in a physical record if the offsets
  23. are given as one byte (resp. two byte) format. */
  24. #define REC_1BYTE_OFFS_LIMIT 0x7F
  25. #define REC_2BYTE_OFFS_LIMIT 0x7FFF
  26. /* We list the byte offsets from the origin of the record, the mask,
  27. and the shift needed to obtain each bit-field of the record. */
  28. #define REC_NEXT 2
  29. #define REC_NEXT_MASK 0xFFFF
  30. #define REC_NEXT_SHIFT 0
  31. #define REC_SHORT 3 /* This is single byte bit-field */
  32. #define REC_SHORT_MASK 0x1
  33. #define REC_SHORT_SHIFT 0
  34. #define REC_N_FIELDS 4
  35. #define REC_N_FIELDS_MASK 0x7FE
  36. #define REC_N_FIELDS_SHIFT 1
  37. #define REC_HEAP_NO 5
  38. #define REC_HEAP_NO_MASK 0xFFF8
  39. #define REC_HEAP_NO_SHIFT 3
  40. #define REC_N_OWNED 6 /* This is single byte bit-field */
  41. #define REC_N_OWNED_MASK 0xF
  42. #define REC_N_OWNED_SHIFT 0
  43. #define REC_INFO_BITS_MASK 0xF0
  44. #define REC_INFO_BITS_SHIFT 0
  45. /* The deleted flag in info bits */
  46. #define REC_INFO_DELETED_FLAG  0x20 /* when bit is set to 1, it means the
  47. record has been delete marked */
  48. /* The following masks are used to filter the SQL null bit from
  49. one-byte and two-byte offsets */
  50. #define REC_1BYTE_SQL_NULL_MASK 0x80
  51. #define REC_2BYTE_SQL_NULL_MASK 0x8000
  52. /***************************************************************
  53. Sets the value of the ith field SQL null bit. */
  54. void
  55. rec_set_nth_field_null_bit(
  56. /*=======================*/
  57. rec_t* rec, /* in: record */
  58. ulint i, /* in: ith field */
  59. ibool val); /* in: value to set */
  60. /*************************************************************** 
  61. Sets a record field to SQL null. The physical size of the field is not
  62. changed. */
  63. void
  64. rec_set_nth_field_sql_null(
  65. /*=======================*/
  66. rec_t* rec,  /* in: record */
  67. ulint n); /* in: index of the field */
  68. /**********************************************************
  69. Gets a bit field from within 1 byte. */
  70. UNIV_INLINE
  71. ulint
  72. rec_get_bit_field_1(
  73. /*================*/
  74. rec_t* rec, /* in: pointer to record origin */
  75. ulint offs, /* in: offset from the origin down */
  76. ulint mask, /* in: mask used to filter bits */
  77. ulint shift) /* in: shift right applied after masking */
  78. {
  79. ut_ad(rec);
  80. return((mach_read_from_1(rec - offs) & mask) >> shift);
  81. }
  82. /**********************************************************
  83. Sets a bit field within 1 byte. */
  84. UNIV_INLINE
  85. void
  86. rec_set_bit_field_1(
  87. /*================*/
  88. rec_t* rec, /* in: pointer to record origin */
  89. ulint val, /* in: value to set */
  90. ulint offs, /* in: offset from the origin down */
  91. ulint mask, /* in: mask used to filter bits */
  92. ulint shift) /* in: shift right applied after masking */
  93. {
  94. ut_ad(rec);
  95. ut_ad(offs <= REC_N_EXTRA_BYTES);
  96. ut_ad(mask);
  97. ut_ad(mask <= 0xFF);
  98. ut_ad(((mask >> shift) << shift) == mask);
  99. ut_ad(((val << shift) & mask) == (val << shift));
  100. mach_write_to_1(rec - offs, 
  101. (mach_read_from_1(rec - offs) & ~mask) 
  102. | (val << shift));
  103. }
  104. /**********************************************************
  105. Gets a bit field from within 2 bytes. */
  106. UNIV_INLINE
  107. ulint
  108. rec_get_bit_field_2(
  109. /*================*/
  110. rec_t* rec, /* in: pointer to record origin */
  111. ulint offs, /* in: offset from the origin down */
  112. ulint mask, /* in: mask used to filter bits */
  113. ulint shift) /* in: shift right applied after masking */
  114. {
  115. ut_ad(rec);
  116. return((mach_read_from_2(rec - offs) & mask) >> shift);
  117. }
  118. /**********************************************************
  119. Sets a bit field within 2 bytes. */
  120. UNIV_INLINE
  121. void
  122. rec_set_bit_field_2(
  123. /*================*/
  124. rec_t* rec, /* in: pointer to record origin */
  125. ulint val, /* in: value to set */
  126. ulint offs, /* in: offset from the origin down */
  127. ulint mask, /* in: mask used to filter bits */
  128. ulint shift) /* in: shift right applied after masking */
  129. {
  130. ut_ad(rec);
  131. ut_ad(offs <= REC_N_EXTRA_BYTES);
  132. ut_ad(mask > 0xFF);
  133. ut_ad(mask <= 0xFFFF);
  134. ut_ad((mask >> shift) & 1);
  135. ut_ad(0 == ((mask >> shift) & ((mask >> shift) + 1)));
  136. ut_ad(((mask >> shift) << shift) == mask);
  137. ut_ad(((val << shift) & mask) == (val << shift));
  138. #ifdef UNIV_DEBUG
  139.       {
  140. ulint m;
  141. /* The following assertion checks that the masks of currently
  142. defined bit-fields in bytes 3-6 do not overlap. */
  143. m = (ulint)((REC_SHORT_MASK << (8 * (REC_SHORT - 3)))
  144.    + (REC_N_FIELDS_MASK << (8 * (REC_N_FIELDS - 4)))
  145.    + (REC_HEAP_NO_MASK << (8 * (REC_HEAP_NO - 4)))
  146.    + (REC_N_OWNED_MASK << (8 * (REC_N_OWNED - 3)))
  147.    + (REC_INFO_BITS_MASK << (8 * (REC_INFO_BITS - 3))));
  148. if (m != ut_dbg_zero + 0xFFFFFFFF) {
  149. printf("Sum of masks %lxn", m);
  150. ut_error;
  151. }
  152.       }
  153. #endif
  154. mach_write_to_2(rec - offs, 
  155. (mach_read_from_2(rec - offs) & ~mask) 
  156. | (val << shift));
  157. }
  158. /**********************************************************
  159. The following function is used to get the offset of the next chained record
  160. on the same page. */
  161. UNIV_INLINE
  162. ulint 
  163. rec_get_next_offs(
  164. /*==============*/
  165. /* out: the page offset of the next chained record */
  166. rec_t* rec) /* in: physical record */
  167. {
  168. ulint ret;
  169. ut_ad(rec);
  170. ret = rec_get_bit_field_2(rec, REC_NEXT, REC_NEXT_MASK,
  171. REC_NEXT_SHIFT);
  172. ut_ad(ret < UNIV_PAGE_SIZE);
  173. return(ret);
  174. }
  175. /**********************************************************
  176. The following function is used to set the next record offset field of the
  177. record. */
  178. UNIV_INLINE
  179. void
  180. rec_set_next_offs(
  181. /*==============*/
  182. rec_t* rec, /* in: physical record */
  183. ulint next) /* in: offset of the next record */
  184. {
  185. ut_ad(rec);
  186. ut_ad(UNIV_PAGE_SIZE > next);
  187. rec_set_bit_field_2(rec, next, REC_NEXT, REC_NEXT_MASK,
  188. REC_NEXT_SHIFT);
  189. }
  190. /**********************************************************
  191. The following function is used to get the number of fields in the record. */
  192. UNIV_INLINE
  193. ulint
  194. rec_get_n_fields(
  195. /*=============*/
  196. /* out: number of data fields */
  197. rec_t* rec) /* in: physical record */
  198. {
  199. ulint ret;
  200. ut_ad(rec);
  201. ret = rec_get_bit_field_2(rec, REC_N_FIELDS, REC_N_FIELDS_MASK,
  202. REC_N_FIELDS_SHIFT);
  203. ut_ad(ret <= REC_MAX_N_FIELDS);
  204. ut_ad(ret > 0);
  205. return(ret);
  206. }
  207. /**********************************************************
  208. The following function is used to set the number of fields field in the
  209. record. */
  210. UNIV_INLINE
  211. void
  212. rec_set_n_fields(
  213. /*=============*/
  214. rec_t* rec, /* in: physical record */
  215. ulint n_fields) /* in: the number of fields */
  216. {
  217. ut_ad(rec);
  218. ut_ad(n_fields <= REC_MAX_N_FIELDS);
  219. ut_ad(n_fields > 0);
  220. rec_set_bit_field_2(rec, n_fields, REC_N_FIELDS, REC_N_FIELDS_MASK,
  221. REC_N_FIELDS_SHIFT);
  222. }
  223. /**********************************************************
  224. The following function is used to get the number of records owned by the
  225. previous directory record. */
  226. UNIV_INLINE
  227. ulint
  228. rec_get_n_owned(
  229. /*============*/
  230. /* out: number of owned records */
  231. rec_t* rec) /* in: physical record */
  232. {
  233. ulint ret;
  234. ut_ad(rec);
  235. ret = rec_get_bit_field_1(rec, REC_N_OWNED, REC_N_OWNED_MASK,
  236. REC_N_OWNED_SHIFT);
  237. ut_ad(ret <= REC_MAX_N_OWNED); 
  238. return(ret);
  239. }
  240. /**********************************************************
  241. The following function is used to set the number of owned records. */
  242. UNIV_INLINE
  243. void
  244. rec_set_n_owned(
  245. /*============*/
  246. rec_t* rec, /* in: physical record */
  247. ulint n_owned) /* in: the number of owned */
  248. {
  249. ut_ad(rec);
  250. ut_ad(n_owned <= REC_MAX_N_OWNED);
  251. rec_set_bit_field_1(rec, n_owned, REC_N_OWNED, REC_N_OWNED_MASK,
  252. REC_N_OWNED_SHIFT);
  253. }
  254. /**********************************************************
  255. The following function is used to retrieve the info bits of a record. */
  256. UNIV_INLINE
  257. ulint
  258. rec_get_info_bits(
  259. /*==============*/
  260. /* out: info bits */
  261. rec_t* rec) /* in: physical record */
  262. {
  263. ulint ret;
  264. ut_ad(rec);
  265. ret = rec_get_bit_field_1(rec, REC_INFO_BITS, REC_INFO_BITS_MASK,
  266. REC_INFO_BITS_SHIFT);
  267. ut_ad((ret & ~REC_INFO_BITS_MASK) == 0);
  268. return(ret);
  269. }
  270. /**********************************************************
  271. The following function is used to set the info bits of a record. */
  272. UNIV_INLINE
  273. void
  274. rec_set_info_bits(
  275. /*==============*/
  276. rec_t* rec, /* in: physical record */
  277. ulint bits) /* in: info bits */
  278. {
  279. ut_ad(rec);
  280. ut_ad((bits & ~REC_INFO_BITS_MASK) == 0);
  281. rec_set_bit_field_1(rec, bits, REC_INFO_BITS, REC_INFO_BITS_MASK,
  282. REC_INFO_BITS_SHIFT);
  283. }
  284. /**********************************************************
  285. Gets the value of the deleted flag in info bits. */
  286. UNIV_INLINE
  287. ibool
  288. rec_info_bits_get_deleted_flag(
  289. /*===========================*/
  290. /* out: TRUE if deleted flag set */
  291. ulint info_bits) /* in: info bits from a record */
  292. {
  293. if (info_bits & REC_INFO_DELETED_FLAG) {
  294. return(TRUE);
  295. }
  296. return(FALSE);
  297. }
  298. /**********************************************************
  299. The following function tells if record is delete marked. */
  300. UNIV_INLINE
  301. ibool
  302. rec_get_deleted_flag(
  303. /*=================*/
  304. /* out: TRUE if delete marked */
  305. rec_t* rec) /* in: physical record */
  306. {
  307. if (REC_INFO_DELETED_FLAG & rec_get_info_bits(rec)) {
  308. return(TRUE);
  309. }
  310. return(FALSE);
  311. }
  312. /**********************************************************
  313. The following function is used to set the deleted bit. */
  314. UNIV_INLINE
  315. void
  316. rec_set_deleted_flag(
  317. /*=================*/
  318. rec_t* rec, /* in: physical record */
  319. ibool flag) /* in: TRUE if delete marked */
  320. {
  321. ulint old_val;
  322. ulint new_val;
  323. ut_ad(TRUE == 1);
  324. ut_ad(flag <= TRUE);
  325. old_val = rec_get_info_bits(rec);
  326. if (flag) {
  327. new_val = REC_INFO_DELETED_FLAG | old_val;
  328. } else {
  329. new_val = ~REC_INFO_DELETED_FLAG & old_val;
  330. }
  331. rec_set_info_bits(rec, new_val);
  332. }
  333. /**********************************************************
  334. The following function is used to get the order number of the record in the
  335. heap of the index page. */
  336. UNIV_INLINE
  337. ulint
  338. rec_get_heap_no(
  339. /*=============*/
  340. /* out: heap order number */
  341. rec_t* rec) /* in: physical record */
  342. {
  343. ulint ret;
  344. ut_ad(rec);
  345. ret = rec_get_bit_field_2(rec, REC_HEAP_NO, REC_HEAP_NO_MASK,
  346. REC_HEAP_NO_SHIFT);
  347. ut_ad(ret <= REC_MAX_HEAP_NO);
  348. return(ret);
  349. }
  350. /**********************************************************
  351. The following function is used to set the heap number field in the record. */
  352. UNIV_INLINE
  353. void
  354. rec_set_heap_no(
  355. /*=============*/
  356. rec_t* rec, /* in: physical record */
  357. ulint heap_no)/* in: the heap number */
  358. {
  359. ut_ad(heap_no <= REC_MAX_HEAP_NO);
  360. rec_set_bit_field_2(rec, heap_no, REC_HEAP_NO, REC_HEAP_NO_MASK,
  361. REC_HEAP_NO_SHIFT);
  362. }
  363. /**********************************************************
  364. The following function is used to test whether the data offsets in the record
  365. are stored in one-byte or two-byte format. */
  366. UNIV_INLINE
  367. ibool
  368. rec_get_1byte_offs_flag(
  369. /*====================*/
  370. /* out: TRUE if 1-byte form */
  371. rec_t* rec) /* in: physical record */
  372. {
  373. ut_ad(TRUE == 1);
  374. return(rec_get_bit_field_1(rec, REC_SHORT, REC_SHORT_MASK,
  375. REC_SHORT_SHIFT));
  376. }
  377. /**********************************************************
  378. The following function is used to set the 1-byte offsets flag. */
  379. UNIV_INLINE
  380. void
  381. rec_set_1byte_offs_flag(
  382. /*====================*/
  383. rec_t* rec, /* in: physical record */
  384. ibool flag) /* in: TRUE if 1byte form */
  385. {
  386. ut_ad(TRUE == 1);
  387. ut_ad(flag <= TRUE);
  388. rec_set_bit_field_1(rec, flag, REC_SHORT, REC_SHORT_MASK,
  389. REC_SHORT_SHIFT);
  390. }
  391. /**********************************************************
  392. Returns the offset of nth field end if the record is stored in the 1-byte
  393. offsets form. If the field is SQL null, the flag is ORed in the returned
  394. value. */
  395. UNIV_INLINE
  396. ulint
  397. rec_1_get_field_end_info(
  398. /*=====================*/
  399.   /* out: offset of the start of the field, SQL null
  400.   flag ORed */
  401.   rec_t* rec,  /* in: record */
  402.   ulint n) /* in: field index */
  403. {
  404. ut_ad(rec_get_1byte_offs_flag(rec));
  405. ut_ad(n < rec_get_n_fields(rec));
  406. return(mach_read_from_1(rec - (REC_N_EXTRA_BYTES + n + 1)));
  407. }
  408. /**********************************************************
  409. Returns the offset of nth field end if the record is stored in the 2-byte
  410. offsets form. If the field is SQL null, the flag is ORed in the returned
  411. value. */
  412. UNIV_INLINE
  413. ulint
  414. rec_2_get_field_end_info(
  415. /*=====================*/
  416.   /* out: offset of the start of the field, SQL null
  417.   flag ORed */
  418.   rec_t* rec,  /* in: record */
  419.   ulint n) /* in: field index */
  420. {
  421. ut_ad(!rec_get_1byte_offs_flag(rec));
  422. ut_ad(n < rec_get_n_fields(rec));
  423. return(mach_read_from_2(rec - (REC_N_EXTRA_BYTES + 2 * n + 2)));
  424. }
  425. /**********************************************************
  426. Returns the offset of n - 1th field end if the record is stored in the 1-byte
  427. offsets form. If the field is SQL null, the flag is ORed in the returned
  428. value. This function and the 2-byte counterpart are defined here because the
  429. C-compilerwas not able to sum negative and positive constant offsets, and
  430. warned of constant arithmetic overflow within the compiler. */
  431. UNIV_INLINE
  432. ulint
  433. rec_1_get_prev_field_end_info(
  434. /*==========================*/
  435.   /* out: offset of the start of the PREVIOUS field, SQL
  436. null flag ORed */
  437.   rec_t* rec,  /* in: record */
  438.   ulint n) /* in: field index */
  439. {
  440. ut_ad(rec_get_1byte_offs_flag(rec));
  441. ut_ad(n <= rec_get_n_fields(rec));
  442. return(mach_read_from_1(rec - (REC_N_EXTRA_BYTES + n)));
  443. }
  444. /**********************************************************
  445. Returns the offset of n - 1th field end if the record is stored in the 2-byte
  446. offsets form. If the field is SQL null, the flag is ORed in the returned
  447. value. */
  448. UNIV_INLINE
  449. ulint
  450. rec_2_get_prev_field_end_info(
  451. /*==========================*/
  452.   /* out: offset of the start of the PREVIOUS field, SQL
  453. null flag ORed */
  454.   rec_t* rec,  /* in: record */
  455.   ulint n) /* in: field index */
  456. {
  457. ut_ad(!rec_get_1byte_offs_flag(rec));
  458. ut_ad(n <= rec_get_n_fields(rec));
  459. return(mach_read_from_2(rec - (REC_N_EXTRA_BYTES + 2 * n)));
  460. }
  461. /**********************************************************
  462. Sets the field end info for the nth field if the record is stored in the
  463. 1-byte format. */
  464. UNIV_INLINE
  465. void
  466. rec_1_set_field_end_info(
  467. /*=====================*/
  468.   rec_t* rec,  /* in: record */
  469.   ulint n, /* in: field index */
  470.   ulint info) /* in: value to set */
  471. {
  472. ut_ad(rec_get_1byte_offs_flag(rec));
  473. ut_ad(n < rec_get_n_fields(rec));
  474. mach_write_to_1(rec - (REC_N_EXTRA_BYTES + n + 1), info);
  475. }
  476. /**********************************************************
  477. Sets the field end info for the nth field if the record is stored in the
  478. 2-byte format. */
  479. UNIV_INLINE
  480. void
  481. rec_2_set_field_end_info(
  482. /*=====================*/
  483.   rec_t* rec,  /* in: record */
  484.   ulint n, /* in: field index */
  485.   ulint info) /* in: value to set */
  486. {
  487. ut_ad(!rec_get_1byte_offs_flag(rec));
  488. ut_ad(n < rec_get_n_fields(rec));
  489. mach_write_to_2(rec - (REC_N_EXTRA_BYTES + 2 * n + 2), info);
  490. }
  491. /**********************************************************
  492. Returns the offset of nth field start if the record is stored in the 1-byte
  493. offsets form. */
  494. UNIV_INLINE
  495. ulint
  496. rec_1_get_field_start_offs(
  497. /*=======================*/
  498.   /* out: offset of the start of the field */
  499.   rec_t* rec,  /* in: record */
  500.   ulint n) /* in: field index */
  501. {
  502. ut_ad(rec_get_1byte_offs_flag(rec));
  503. ut_ad(n <= rec_get_n_fields(rec));
  504. if (n == 0) {
  505. return(0);
  506. }
  507. return(rec_1_get_prev_field_end_info(rec, n)
  508. & ~REC_1BYTE_SQL_NULL_MASK);
  509. }
  510. /**********************************************************
  511. Returns the offset of nth field start if the record is stored in the 2-byte
  512. offsets form. */
  513. UNIV_INLINE
  514. ulint
  515. rec_2_get_field_start_offs(
  516. /*=======================*/
  517.   /* out: offset of the start of the field */
  518.   rec_t* rec,  /* in: record */
  519.   ulint n) /* in: field index */
  520. {
  521. ut_ad(!rec_get_1byte_offs_flag(rec));
  522. ut_ad(n <= rec_get_n_fields(rec));
  523. if (n == 0) {
  524. return(0);
  525. }
  526. return(rec_2_get_prev_field_end_info(rec, n)
  527. & ~REC_2BYTE_SQL_NULL_MASK);
  528. }
  529. /**********************************************************
  530. The following function is used to read the offset of the start of a data field
  531. in the record. The start of an SQL null field is the end offset of the
  532. previous non-null field, or 0, if none exists. If n is the number of the last
  533. field + 1, then the end offset of the last field is returned. */
  534. UNIV_INLINE
  535. ulint
  536. rec_get_field_start_offs(
  537. /*=====================*/
  538.   /* out: offset of the start of the field */
  539.   rec_t* rec,  /* in: record */
  540.   ulint n) /* in: field index */
  541. {
  542. ut_ad(rec);
  543. ut_ad(n <= rec_get_n_fields(rec));
  544. if (n == 0) {
  545. return(0);
  546. }
  547. if (rec_get_1byte_offs_flag(rec)) {
  548. return(rec_1_get_field_start_offs(rec, n));
  549. }
  550. return(rec_2_get_field_start_offs(rec, n));
  551. }
  552. /****************************************************************
  553. Gets the physical size of a field. Also an SQL null may have a field of
  554. size > 0, if the data type is of a fixed size. */
  555. UNIV_INLINE
  556. ulint
  557. rec_get_nth_field_size(
  558. /*===================*/
  559. /* out: field size in bytes */
  560.   rec_t* rec,  /* in: record */
  561.   ulint n) /* in: index of the field */
  562. {
  563. ulint os;
  564. ulint next_os;
  565. os = rec_get_field_start_offs(rec, n);
  566. next_os = rec_get_field_start_offs(rec, n + 1);
  567. ut_ad(next_os - os < UNIV_PAGE_SIZE);
  568. return(next_os - os);
  569. }
  570. /****************************************************************
  571. The following function is used to get a copy of the nth data field in a
  572. record to a buffer. */
  573. UNIV_INLINE
  574. void
  575. rec_copy_nth_field(
  576. /*===============*/
  577.   void* buf, /* in: pointer to the buffer */
  578.   rec_t* rec,  /* in: record */
  579.   ulint n, /* in: index of the field */
  580. ulint* len) /* out: length of the field; UNIV_SQL_NULL if SQL 
  581. null */
  582. {
  583. byte* ptr;
  584. ut_ad(buf && rec && len);
  585. ptr = rec_get_nth_field(rec, n, len);
  586. if (*len == UNIV_SQL_NULL) {
  587. return;
  588. }
  589. ut_memcpy(buf, ptr, *len);
  590. }
  591. /*************************************************************** 
  592. This is used to modify the value of an already existing field in a record.
  593. The previous value must have exactly the same size as the new value. If len
  594. is UNIV_SQL_NULL then the field is treated as an SQL null. */
  595. UNIV_INLINE
  596. void
  597. rec_set_nth_field(
  598. /*==============*/
  599. rec_t* rec,  /* in: record */
  600. ulint n, /* in: index of the field */
  601. void* data, /* in: pointer to the data if not SQL null */
  602. ulint len) /* in: length of the data or UNIV_SQL_NULL */
  603. {
  604. byte* data2;
  605. ulint len2;
  606. ut_ad((len == UNIV_SQL_NULL)
  607. || (rec_get_nth_field_size(rec, n) == len));
  608. if (len == UNIV_SQL_NULL) {
  609. rec_set_nth_field_sql_null(rec, n);
  610. return;
  611. }
  612. data2 = rec_get_nth_field(rec, n, &len2);
  613. ut_memcpy(data2, data, len);
  614. if (len2 == UNIV_SQL_NULL) {
  615. rec_set_nth_field_null_bit(rec, n, FALSE);
  616. }
  617. }
  618. /************************************************************** 
  619. The following function returns the data size of a physical
  620. record, that is the sum of field lengths. SQL null fields
  621. are counted as length 0 fields. The value returned by the function
  622. is the distance from record origin to record end in bytes. */
  623. UNIV_INLINE
  624. ulint
  625. rec_get_data_size(
  626. /*==============*/
  627. /* out: size */
  628. rec_t* rec) /* in: physical record */
  629. {
  630. ut_ad(rec);
  631. return(rec_get_field_start_offs(rec, rec_get_n_fields(rec)));
  632. }
  633. /************************************************************** 
  634. Returns the total size of record minus data size of record. The value
  635. returned by the function is the distance from record start to record origin
  636. in bytes. */
  637. UNIV_INLINE
  638. ulint
  639. rec_get_extra_size(
  640. /*===============*/
  641. /* out: size */
  642. rec_t* rec) /* in: physical record */
  643. {
  644. ulint n_fields;
  645. ut_ad(rec);
  646. n_fields = rec_get_n_fields(rec);
  647. if (rec_get_1byte_offs_flag(rec)) {
  648. return(REC_N_EXTRA_BYTES + n_fields);
  649. }
  650. return(REC_N_EXTRA_BYTES + 2 * n_fields);
  651. }
  652. /************************************************************** 
  653. Returns the total size of a physical record.  */
  654. UNIV_INLINE
  655. ulint
  656. rec_get_size(
  657. /*=========*/
  658. /* out: size */
  659. rec_t* rec) /* in: physical record */
  660. {
  661. ulint n_fields;
  662. ut_ad(rec);
  663. n_fields = rec_get_n_fields(rec);
  664. if (rec_get_1byte_offs_flag(rec)) {
  665. return(REC_N_EXTRA_BYTES + n_fields
  666. + rec_1_get_field_start_offs(rec, n_fields));
  667. }
  668. return(REC_N_EXTRA_BYTES + 2 * n_fields
  669. + rec_2_get_field_start_offs(rec, n_fields));
  670. }
  671. /**************************************************************
  672. Returns a pointer to the end of the record. */
  673. UNIV_INLINE
  674. byte*
  675. rec_get_end(
  676. /*========*/
  677. /* out: pointer to end */
  678. rec_t* rec) /* in: pointer to record */
  679. {
  680. return(rec + rec_get_data_size(rec));
  681. }
  682. /**************************************************************
  683. Returns a pointer to the start of the record. */
  684. UNIV_INLINE
  685. byte*
  686. rec_get_start(
  687. /*==========*/
  688. /* out: pointer to start */
  689. rec_t* rec) /* in: pointer to record */
  690. {
  691. return(rec - rec_get_extra_size(rec));
  692. }
  693. /*******************************************************************
  694. Copies a physical record to a buffer. */
  695. UNIV_INLINE
  696. rec_t*
  697. rec_copy(
  698. /*=====*/
  699. /* out: pointer to the origin of the copied record */
  700. void* buf, /* in: buffer */
  701. rec_t* rec) /* in: physical record */
  702. {
  703. ulint extra_len;
  704. ulint data_len;
  705. ut_ad(rec && buf);
  706. ut_ad(rec_validate(rec));
  707. extra_len = rec_get_extra_size(rec);
  708. data_len = rec_get_data_size(rec);
  709. ut_memcpy(buf, rec - extra_len, extra_len + data_len);
  710. return((byte*)buf + extra_len);
  711. }
  712. /**************************************************************
  713. Returns the extra size of a physical record if we know its data size and
  714. the number of fields. */
  715. UNIV_INLINE
  716. ulint
  717. rec_get_converted_extra_size(
  718. /*=========================*/
  719. /* out: extra size */
  720. ulint data_size, /* in: data size */
  721. ulint n_fields) /* in: number of fields */
  722. {
  723. if (data_size <= REC_1BYTE_OFFS_LIMIT) {
  724. return(REC_N_EXTRA_BYTES + n_fields);
  725. }
  726. return(REC_N_EXTRA_BYTES + 2 * n_fields);
  727. }
  728. /**************************************************************
  729. The following function returns the size of a data tuple when converted to
  730. a physical record. */
  731. UNIV_INLINE
  732. ulint
  733. rec_get_converted_size(
  734. /*===================*/
  735. /* out: size */
  736. dtuple_t* dtuple) /* in: data tuple */
  737. {
  738. ulint data_size;
  739. ulint extra_size;
  740. ut_ad(dtuple);
  741. ut_ad(dtuple_check_typed(dtuple));
  742. data_size = dtuple_get_data_size(dtuple);
  743. extra_size = rec_get_converted_extra_size(
  744. data_size, dtuple_get_n_fields(dtuple));
  745. return(data_size + extra_size);
  746. }
  747. /****************************************************************
  748. Folds a prefix of a physical record to a ulint. */
  749. UNIV_INLINE
  750. ulint
  751. rec_fold(
  752. /*=====*/
  753. /* out: the folded value */
  754. rec_t* rec, /* in: the physical record */
  755. ulint n_fields, /* in: number of complete fields to fold */
  756. ulint n_bytes, /* in: number of bytes to fold in an
  757. incomplete last field */
  758. dulint tree_id) /* in: index tree id */
  759. {
  760. ulint i;
  761. byte* data;
  762. ulint len;
  763. ulint fold;
  764. ut_ad(rec_validate(rec));
  765. ut_ad(n_fields <= rec_get_n_fields(rec));
  766. ut_ad((n_fields < rec_get_n_fields(rec)) || (n_bytes == 0));
  767. ut_ad(n_fields + n_bytes > 0);
  768. /* Only the page supremum and infimum records have 1 field: */
  769. ut_ad(rec_get_n_fields(rec) > 1);
  770. fold = ut_fold_dulint(tree_id);
  771. for (i = 0; i < n_fields; i++) {
  772. data = rec_get_nth_field(rec, i, &len);
  773. if (len != UNIV_SQL_NULL) {
  774. fold = ut_fold_ulint_pair(fold,
  775.   ut_fold_binary(data, len));
  776. }
  777. }
  778. if (n_bytes > 0) {
  779. data = rec_get_nth_field(rec, i, &len);
  780. if (len != UNIV_SQL_NULL) {
  781. if (len > n_bytes) {
  782. len = n_bytes;
  783. }
  784. fold = ut_fold_ulint_pair(fold,
  785.   ut_fold_binary(data, len));
  786. }
  787. }
  788. return(fold);
  789. }
  790. /*************************************************************
  791. Builds a physical record out of a data tuple and stores it beginning from
  792. the address destination. */
  793. UNIV_INLINE
  794. rec_t* 
  795. rec_convert_dtuple_to_rec(
  796. /*======================*/
  797. /* out: pointer to the origin of physical
  798. record */
  799. byte* destination, /* in: start address of the physical record */
  800. dtuple_t* dtuple) /* in: data tuple */
  801. {
  802. return(rec_convert_dtuple_to_rec_low(destination, dtuple,
  803. dtuple_get_data_size(dtuple)));
  804. }