rem0rec.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:12k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /************************************************************************
  2. Record manager
  3. (c) 1994-1996 Innobase Oy
  4. Created 5/30/1994 Heikki Tuuri
  5. *************************************************************************/
  6. #include "rem0rec.h"
  7. #ifdef UNIV_NONINL
  8. #include "rem0rec.ic"
  9. #endif
  10. /* PHYSICAL RECORD
  11. ===============
  12. The physical record, which is the data type of all the records
  13. found in index pages of the database, has the following format
  14. (lower addresses and more significant bits inside a byte are below
  15. represented on a higher text line):
  16. | offset of the end of the last field of data, the most significant
  17.   bit is set to 1 if and only if the field is SQL-null | 
  18. ... 
  19. | offset of the end of the first field of data + the SQL-null bit |
  20. | 4 bits used to delete mark a record, and mark a predefined
  21.   minimum record in alphabetical order |
  22. | 4 bits giving the number of records owned by this record
  23.   (this term is explained in page0page.h) |
  24. | 13 bits giving the order number of this record in the
  25.   heap of the index page |
  26. | 10 bits giving the number of fields in this record |
  27. | 1 bit which is set to 1 if the offsets above are given in
  28.   one byte format, 0 if in two byte format |
  29. | two bytes giving the pointer to the next record in the page | 
  30. ORIGIN of the record
  31. | first field of data | 
  32. ... 
  33. | last field of data |
  34. The origin of the record is the start address of the first field 
  35. of data. The offsets are given relative to the origin. 
  36. The offsets of the data fields are stored in an inverted
  37. order because then the offset of the first fields are near the 
  38. origin, giving maybe a better processor cache hit rate in searches.
  39. The offsets of the data fields are given as one-byte 
  40. (if there are less than 127 bytes of data in the record) 
  41. or two-byte unsigned integers. The most significant bit
  42. is not part of the offset, instead it indicates the SQL-null
  43. if the bit is set to 1.
  44. CANONICAL COORDINATES. A record can be seen as a single
  45. string of 'characters' in the following way: catenate the bytes
  46. in each field, in the order of fields. An SQL-null field
  47. is taken to be an empty sequence of bytes. Then after
  48. the position of each field insert in the string 
  49. the 'character' <FIELD-END>, except that after an SQL-null field
  50. insert <NULL-FIELD-END>. Now the ordinal position of each
  51. byte in this canonical string is its canonical coordinate.
  52. So, for the record ("AA", SQL-NULL, "BB", ""), the canonical
  53. string is "AA<FIELD_END><NULL-FIELD-END>BB<FIELD-END><FIELD-END>".
  54. We identify prefixes (= initial segments) of a record
  55. with prefixes of the canonical string. The canonical
  56. length of the prefix is the length of the corresponding
  57. prefix of the canonical string. The canonical length of
  58. a record is the length of its canonical string.
  59. For example, the maximal common prefix of records
  60. ("AA", SQL-NULL, "BB", "C") and ("AA", SQL-NULL, "B", "C")
  61. is "AA<FIELD-END><NULL-FIELD-END>B", and its canonical
  62. length is 5.
  63. A complete-field prefix of a record is a prefix which ends at the
  64. end of some field (containing also <FIELD-END>).
  65. A record is a complete-field prefix of another record, if
  66. the corresponding canonical strings have the same property. */
  67. ulint rec_dummy; /* this is used to fool compiler in
  68. rec_validate */
  69. /****************************************************************
  70. The following function is used to get a pointer to the nth data field in a
  71. record. */
  72. byte*
  73. rec_get_nth_field(
  74. /*==============*/
  75.   /* out: pointer to the field */
  76.   rec_t* rec,  /* in: record */
  77.   ulint n, /* in: index of the field */
  78. ulint* len) /* out: length of the field; UNIV_SQL_NULL if SQL
  79. null */
  80. {
  81. ulint os;
  82. ulint next_os;
  83. ut_ad(rec && len);
  84. ut_ad(n < rec_get_n_fields(rec));
  85. if (rec_get_1byte_offs_flag(rec)) {
  86. os = rec_1_get_field_start_offs(rec, n);
  87. next_os = rec_1_get_field_end_info(rec, n);
  88. if (next_os & REC_1BYTE_SQL_NULL_MASK) {
  89. *len = UNIV_SQL_NULL;
  90. return(rec + os);
  91. }
  92. next_os = next_os & ~REC_1BYTE_SQL_NULL_MASK;
  93. } else {
  94. os = rec_2_get_field_start_offs(rec, n);
  95. next_os = rec_2_get_field_end_info(rec, n);
  96. if (next_os & REC_2BYTE_SQL_NULL_MASK) {
  97. *len = UNIV_SQL_NULL;
  98. return(rec + os);
  99. }
  100. next_os = next_os & ~REC_2BYTE_SQL_NULL_MASK;
  101. }
  102. *len = next_os - os;
  103. ut_ad(*len < UNIV_PAGE_SIZE);
  104. return(rec + os);
  105. }
  106. /***************************************************************
  107. Sets the value of the ith field SQL null bit. */
  108. void
  109. rec_set_nth_field_null_bit(
  110. /*=======================*/
  111. rec_t* rec, /* in: record */
  112. ulint i, /* in: ith field */
  113. ibool val) /* in: value to set */
  114. {
  115. ulint info;
  116. if (rec_get_1byte_offs_flag(rec)) {
  117. info = rec_1_get_field_end_info(rec, i);
  118. if (val) {
  119. info = info | REC_1BYTE_SQL_NULL_MASK;
  120. } else {
  121. info = info & ~REC_1BYTE_SQL_NULL_MASK;
  122. }
  123. rec_1_set_field_end_info(rec, i, info);
  124. return;
  125. }
  126. info = rec_2_get_field_end_info(rec, i);
  127. if (val) {
  128. info = info | REC_2BYTE_SQL_NULL_MASK;
  129. } else {
  130. info = info & ~REC_2BYTE_SQL_NULL_MASK;
  131. }
  132. rec_2_set_field_end_info(rec, i, info);
  133. }
  134. /*************************************************************** 
  135. Sets a record field to SQL null. The physical size of the field is not
  136. changed. */
  137. void
  138. rec_set_nth_field_sql_null(
  139. /*=======================*/
  140. rec_t* rec,  /* in: record */
  141. ulint n) /* in: index of the field */
  142. {
  143. ulint offset;
  144. offset = rec_get_field_start_offs(rec, n);
  145. data_write_sql_null(rec + offset, rec_get_nth_field_size(rec, n));
  146. rec_set_nth_field_null_bit(rec, n, TRUE);
  147. }
  148. /*************************************************************
  149. Builds a physical record out of a data tuple and stores it beginning from
  150. address destination. */
  151. rec_t* 
  152. rec_convert_dtuple_to_rec_low(
  153. /*==========================*/
  154. /* out: pointer to the origin of physical
  155. record */
  156. byte* destination, /* in: start address of the physical record */
  157. dtuple_t* dtuple, /* in: data tuple */
  158. ulint data_size) /* in: data size of dtuple */
  159. {
  160. dfield_t*  field;
  161. ulint n_fields;
  162. rec_t*  rec;
  163. ulint end_offset;
  164. ulint ored_offset;
  165. byte* data;
  166. ulint len;
  167. ulint i;
  168. ut_ad(destination && dtuple);
  169. ut_ad(dtuple_validate(dtuple));
  170. ut_ad(dtuple_check_typed(dtuple));
  171. ut_ad(dtuple_get_data_size(dtuple) == data_size);
  172. n_fields = dtuple_get_n_fields(dtuple);
  173. ut_ad(n_fields > 0);
  174. /* Calculate the offset of the origin in the physical record */
  175. rec = destination + rec_get_converted_extra_size(data_size, n_fields);
  176. /* Store the number of fields */
  177. rec_set_n_fields(rec, n_fields);
  178. /* Set the info bits of the record */
  179. rec_set_info_bits(rec, dtuple_get_info_bits(dtuple));
  180. /* Store the data and the offsets */
  181. end_offset = 0;
  182. if (data_size <= REC_1BYTE_OFFS_LIMIT) {
  183.     rec_set_1byte_offs_flag(rec, TRUE);
  184.     for (i = 0; i < n_fields; i++) {
  185. field = dtuple_get_nth_field(dtuple, i);
  186. data = dfield_get_data(field);
  187. len = dfield_get_len(field);
  188. if (len == UNIV_SQL_NULL) {
  189. len = dtype_get_sql_null_size(dfield_get_type(field));
  190. data_write_sql_null(rec + end_offset, len);
  191. end_offset += len;
  192. ored_offset = end_offset | REC_1BYTE_SQL_NULL_MASK;
  193. } else {
  194. /* If the data is not SQL null, store it */
  195. ut_memcpy(rec + end_offset, data, len);
  196. end_offset += len;
  197. ored_offset = end_offset;
  198. }
  199. rec_1_set_field_end_info(rec, i, ored_offset);
  200.     }
  201. } else {
  202.     rec_set_1byte_offs_flag(rec, FALSE);
  203.     for (i = 0; i < n_fields; i++) {
  204. field = dtuple_get_nth_field(dtuple, i);
  205. data = dfield_get_data(field);
  206. len = dfield_get_len(field);
  207. if (len == UNIV_SQL_NULL) {
  208. len = dtype_get_sql_null_size(dfield_get_type(field));
  209. data_write_sql_null(rec + end_offset, len);
  210. end_offset += len;
  211. ored_offset = end_offset | REC_2BYTE_SQL_NULL_MASK;
  212. } else {
  213. /* If the data is not SQL null, store it */
  214. ut_memcpy(rec + end_offset, data, len);
  215. end_offset += len;
  216. ored_offset = end_offset;
  217. }
  218. rec_2_set_field_end_info(rec, i, ored_offset);
  219.     }
  220. }
  221. ut_ad(rec_validate(rec));
  222. return(rec);
  223. }
  224. /******************************************************************
  225. Copies the first n fields of a physical record to a data tuple. The fields
  226. are copied to the memory heap. */
  227. void
  228. rec_copy_prefix_to_dtuple(
  229. /*======================*/
  230. dtuple_t* tuple, /* in: data tuple */
  231. rec_t* rec, /* in: physical record */
  232. ulint n_fields, /* in: number of fields to copy */
  233. mem_heap_t* heap) /* in: memory heap */
  234. {
  235. dfield_t* field;
  236. byte* data;
  237. ulint len;
  238. byte* buf = NULL;
  239. ulint i;
  240. ut_ad(rec_validate(rec));
  241. ut_ad(dtuple_check_typed(tuple));
  242. dtuple_set_info_bits(tuple, rec_get_info_bits(rec));
  243. for (i = 0; i < n_fields; i++) {
  244. field = dtuple_get_nth_field(tuple, i);
  245. data = rec_get_nth_field(rec, i, &len);
  246. if (len != UNIV_SQL_NULL) {
  247. buf = mem_heap_alloc(heap, len);
  248. ut_memcpy(buf, data, len);
  249. }
  250. dfield_set_data(field, buf, len);
  251. }
  252. }
  253. /******************************************************************
  254. Copies the first n fields of a physical record to a new physical record in
  255. a buffer. */
  256. rec_t*
  257. rec_copy_prefix_to_buf(
  258. /*===================*/
  259. /* out, own: copied record */
  260. rec_t* rec, /* in: physical record */
  261. ulint n_fields, /* in: number of fields to copy */
  262. byte** buf, /* in/out: memory buffer for the copied prefix,
  263. or NULL */
  264. ulint* buf_size) /* in/out: buffer size */
  265. {
  266. rec_t* copy_rec;
  267. ulint area_start;
  268. ulint area_end;
  269. ulint prefix_len;
  270. ut_ad(rec_validate(rec));
  271. area_end = rec_get_field_start_offs(rec, n_fields);
  272. if (rec_get_1byte_offs_flag(rec)) {
  273. area_start = REC_N_EXTRA_BYTES + n_fields;
  274. } else {
  275. area_start = REC_N_EXTRA_BYTES + 2 * n_fields;
  276. }
  277. prefix_len = area_start + area_end;
  278. if ((*buf == NULL) || (*buf_size < prefix_len)) {
  279. if (*buf != NULL) {
  280. mem_free(*buf);
  281. }
  282. *buf = mem_alloc(prefix_len);
  283. *buf_size = prefix_len;
  284. }
  285. ut_memcpy(*buf, rec - area_start, prefix_len);
  286. copy_rec = *buf + area_start;
  287. rec_set_n_fields(copy_rec, n_fields);
  288. return(copy_rec);
  289. }
  290. /*******************************************************************
  291. Validates the consistency of a physical record. */
  292. ibool
  293. rec_validate(
  294. /*=========*/
  295. /* out: TRUE if ok */
  296. rec_t* rec) /* in: physical record */
  297. {
  298. ulint i;
  299. byte* data;
  300. ulint len;
  301. ulint n_fields;
  302. ulint len_sum = 0;
  303. ulint sum = 0;
  304. ut_a(rec);
  305. n_fields = rec_get_n_fields(rec);
  306. if ((n_fields == 0) || (n_fields > REC_MAX_N_FIELDS)) {
  307. ut_a(0);
  308. }
  309. for (i = 0; i < n_fields; i++) {
  310. data = rec_get_nth_field(rec, i, &len);
  311. ut_a((len < UNIV_PAGE_SIZE) || (len == UNIV_SQL_NULL));
  312. if (len != UNIV_SQL_NULL) {
  313. len_sum += len;
  314. sum += *(data + len -1); /* dereference the
  315. end of the field to
  316. cause a memory trap
  317. if possible */
  318. } else {
  319. len_sum += rec_get_nth_field_size(rec, i);
  320. }
  321. }
  322. ut_a(len_sum == (ulint)(rec_get_end(rec) - rec));
  323. rec_dummy = sum; /* This is here only to fool the compiler */
  324. return(TRUE);
  325. }
  326. /*******************************************************************
  327. Prints a physical record. */
  328. void
  329. rec_print(
  330. /*======*/
  331. rec_t* rec) /* in: physical record */
  332. {
  333. byte* data;
  334. ulint len;
  335. char* offs;
  336. ulint n;
  337. ulint i;
  338. ut_ad(rec);
  339. if (rec_get_1byte_offs_flag(rec)) {
  340. offs = "TRUE";
  341. } else {
  342. offs = "FALSE";
  343. }
  344. n = rec_get_n_fields(rec);
  345. printf(
  346.     "PHYSICAL RECORD: n_fields %lu; 1-byte offs %s; info bits %lun",
  347. n, offs, rec_get_info_bits(rec));
  348. for (i = 0; i < n; i++) {
  349. data = rec_get_nth_field(rec, i, &len);
  350. printf(" %lu:", i);
  351. if (len != UNIV_SQL_NULL) {
  352. if (len <= 30) {
  353. ut_print_buf(data, len);
  354. } else {
  355. ut_print_buf(data, 30);
  356. printf("...(truncated)");
  357. }
  358. } else {
  359. printf(" SQL NULL, size %lu ",
  360. rec_get_nth_field_size(rec, i));
  361. }
  362. printf(";");
  363. }
  364. printf("n");
  365. rec_validate(rec);
  366. }
  367. /*******************************************************************
  368. Prints a physical record to a buffer. */
  369. ulint
  370. rec_sprintf(
  371. /*========*/
  372. /* out: printed length in bytes */
  373. char* buf, /* in: buffer to print to */
  374. ulint buf_len,/* in: buffer length */
  375. rec_t* rec) /* in: physical record */
  376. {
  377. byte* data;
  378. ulint len;
  379. ulint k;
  380. ulint n;
  381. ulint i;
  382. ut_ad(rec);
  383. n = rec_get_n_fields(rec);
  384. k = 0;
  385. if (k + 30 > buf_len) {
  386. return(k);
  387. }
  388. k += sprintf(buf + k, "RECORD: info bits %lu", rec_get_info_bits(rec));
  389. for (i = 0; i < n; i++) {
  390. if (k + 30 > buf_len) {
  391. return(k);
  392. }
  393. data = rec_get_nth_field(rec, i, &len);
  394. k += sprintf(buf + k, " %lu:", i);
  395. if (len != UNIV_SQL_NULL) {
  396. if (k + 30 + 5 * len > buf_len) {
  397. return(k);
  398. }
  399. k += ut_sprintf_buf(buf + k, data, len);
  400. } else {
  401. k += sprintf(buf + k, " SQL NULL");
  402. }
  403. k += sprintf(buf + k, ";");
  404. }
  405. return(k);
  406. }