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

MySQL数据库

开发平台:

Visual C++

  1. /******************************************************
  2. Insert into a table
  3. (c) 1996 Innobase Oy
  4. Created 4/20/1996 Heikki Tuuri
  5. *******************************************************/
  6. #include "row0ins.h"
  7. #ifdef UNIV_NONINL
  8. #include "row0ins.ic"
  9. #endif
  10. #include "dict0dict.h"
  11. #include "dict0boot.h"
  12. #include "trx0undo.h"
  13. #include "btr0btr.h"
  14. #include "btr0cur.h"
  15. #include "mach0data.h"
  16. #include "que0que.h"
  17. #include "row0upd.h"
  18. #include "row0sel.h"
  19. #include "row0row.h"
  20. #include "rem0cmp.h"
  21. #include "lock0lock.h"
  22. #include "log0log.h"
  23. #include "eval0eval.h"
  24. #include "data0data.h"
  25. #include "usr0sess.h"
  26. #define ROW_INS_PREV 1
  27. #define ROW_INS_NEXT 2
  28. /*************************************************************************
  29. Creates an insert node struct. */
  30. ins_node_t*
  31. ins_node_create(
  32. /*============*/
  33. /* out, own: insert node struct */
  34. ulint ins_type, /* in: INS_VALUES, ... */
  35. dict_table_t* table,  /* in: table where to insert */
  36. mem_heap_t* heap) /* in: mem heap where created */
  37. {
  38. ins_node_t* node;
  39. node = mem_heap_alloc(heap, sizeof(ins_node_t));
  40. node->common.type = QUE_NODE_INSERT;
  41. node->ins_type = ins_type;
  42. node->state = INS_NODE_SET_IX_LOCK;
  43. node->table = table;
  44. node->index = NULL;
  45. node->entry = NULL;
  46. node->select = NULL;
  47. node->trx_id = ut_dulint_zero;
  48. node->entry_sys_heap = mem_heap_create(128);
  49. node->magic_n = INS_NODE_MAGIC_N;
  50. return(node);
  51. }
  52. /***************************************************************
  53. Creates an entry template for each index of a table. */
  54. static
  55. void
  56. ins_node_create_entry_list(
  57. /*=======================*/
  58. ins_node_t* node) /* in: row insert node */
  59. {
  60. dict_index_t* index;
  61. dtuple_t* entry;
  62. ut_ad(node->entry_sys_heap);
  63. UT_LIST_INIT(node->entry_list);
  64. index = dict_table_get_first_index(node->table);
  65. while (index != NULL) {
  66. entry = row_build_index_entry(node->row, index,
  67. node->entry_sys_heap);
  68. UT_LIST_ADD_LAST(tuple_list, node->entry_list, entry);
  69. index = dict_table_get_next_index(index);
  70. }
  71. }
  72. /*********************************************************************
  73. Adds system field buffers to a row. */
  74. static
  75. void
  76. row_ins_alloc_sys_fields(
  77. /*=====================*/
  78. ins_node_t* node) /* in: insert node */
  79. {
  80. dtuple_t* row;
  81. dict_table_t* table;
  82. mem_heap_t* heap;
  83. dict_col_t* col;
  84. dfield_t* dfield;
  85. ulint len;
  86. byte* ptr;
  87. row = node->row;
  88. table = node->table;
  89. heap = node->entry_sys_heap;
  90. ut_ad(row && table && heap);
  91. ut_ad(dtuple_get_n_fields(row) == dict_table_get_n_cols(table));
  92. /* 1. Allocate buffer for row id */
  93. col = dict_table_get_sys_col(table, DATA_ROW_ID);
  94. dfield = dtuple_get_nth_field(row, dict_col_get_no(col));
  95. ptr = mem_heap_alloc(heap, DATA_ROW_ID_LEN);
  96. dfield_set_data(dfield, ptr, DATA_ROW_ID_LEN);
  97. node->row_id_buf = ptr;
  98. if (table->type == DICT_TABLE_CLUSTER_MEMBER) {
  99. /* 2. Fill in the dfield for mix id */
  100. col = dict_table_get_sys_col(table, DATA_MIX_ID);
  101. dfield = dtuple_get_nth_field(row, dict_col_get_no(col));
  102. len = mach_dulint_get_compressed_size(table->mix_id);
  103. ptr = mem_heap_alloc(heap, DATA_MIX_ID_LEN);
  104. mach_dulint_write_compressed(ptr, table->mix_id);
  105. dfield_set_data(dfield, ptr, len);
  106. }
  107. /* 3. Allocate buffer for trx id */
  108. col = dict_table_get_sys_col(table, DATA_TRX_ID);
  109. dfield = dtuple_get_nth_field(row, dict_col_get_no(col));
  110. ptr = mem_heap_alloc(heap, DATA_TRX_ID_LEN);
  111. dfield_set_data(dfield, ptr, DATA_TRX_ID_LEN);
  112. node->trx_id_buf = ptr;
  113. /* 4. Allocate buffer for roll ptr */
  114. col = dict_table_get_sys_col(table, DATA_ROLL_PTR);
  115. dfield = dtuple_get_nth_field(row, dict_col_get_no(col));
  116. ptr = mem_heap_alloc(heap, DATA_ROLL_PTR_LEN);
  117. dfield_set_data(dfield, ptr, DATA_ROLL_PTR_LEN);
  118. }
  119. /*************************************************************************
  120. Sets a new row to insert for an INS_DIRECT node. This function is only used
  121. if we have constructed the row separately, which is a rare case; this
  122. function is quite slow. */
  123. void
  124. ins_node_set_new_row(
  125. /*=================*/
  126. ins_node_t* node, /* in: insert node */
  127. dtuple_t* row) /* in: new row (or first row) for the node */
  128. {
  129. node->state = INS_NODE_SET_IX_LOCK;
  130. node->index = NULL;
  131. node->entry = NULL;
  132. node->row = row;
  133. mem_heap_empty(node->entry_sys_heap);
  134. /* Create templates for index entries */
  135. ins_node_create_entry_list(node);
  136. /* Allocate from entry_sys_heap buffers for sys fields */
  137. row_ins_alloc_sys_fields(node);
  138. /* As we allocated a new trx id buf, the trx id should be written
  139. there again: */
  140. node->trx_id = ut_dulint_zero;
  141. }
  142. /***********************************************************************
  143. Does an insert operation by updating a delete marked existing record
  144. in the index. This situation can occur if the delete marked record is
  145. kept in the index for consistent reads. */
  146. static
  147. ulint
  148. row_ins_sec_index_entry_by_modify(
  149. /*==============================*/
  150. /* out: DB_SUCCESS or error code */
  151. btr_cur_t* cursor, /* in: B-tree cursor */
  152. que_thr_t* thr, /* in: query thread */
  153. mtr_t* mtr) /* in: mtr */
  154. {
  155. ulint err;
  156. ut_ad(((cursor->index)->type & DICT_CLUSTERED) == 0);
  157. ut_ad(rec_get_deleted_flag(btr_cur_get_rec(cursor)));
  158. /* We just remove the delete mark from the secondary index record */
  159. err = btr_cur_del_mark_set_sec_rec(0, cursor, FALSE, thr, mtr);
  160. return(err);
  161. }
  162. /***********************************************************************
  163. Does an insert operation by delete unmarking and updating a delete marked
  164. existing record in the index. This situation can occur if the delete marked
  165. record is kept in the index for consistent reads. */
  166. static
  167. ulint
  168. row_ins_clust_index_entry_by_modify(
  169. /*================================*/
  170. /* out: DB_SUCCESS, DB_FAIL, or error code */
  171. ulint mode, /* in: BTR_MODIFY_LEAF or BTR_MODIFY_TREE,
  172. depending on whether mtr holds just a leaf
  173. latch or also a tree latch */
  174. btr_cur_t* cursor, /* in: B-tree cursor */
  175. dtuple_t* entry, /* in: index entry to insert */
  176. que_thr_t* thr, /* in: query thread */
  177. mtr_t* mtr) /* in: mtr */
  178. {
  179. mem_heap_t* heap;
  180. rec_t* rec;
  181. upd_t* update;
  182. ulint err;
  183. ut_ad((cursor->index)->type & DICT_CLUSTERED);
  184. rec = btr_cur_get_rec(cursor);
  185. ut_ad(rec_get_deleted_flag(rec));
  186. heap = mem_heap_create(1024);
  187. /* Build an update vector containing all the fields to be modified;
  188. NOTE that this vector may contain also system columns! */
  189. update = row_upd_build_difference(cursor->index, entry, rec, heap); 
  190. if (mode == BTR_MODIFY_LEAF) {
  191. /* Try optimistic updating of the record, keeping changes
  192. within the page */
  193. err = btr_cur_optimistic_update(0, cursor, update, 0, thr,
  194. mtr);
  195. if ((err == DB_OVERFLOW) || (err == DB_UNDERFLOW)) {
  196. err = DB_FAIL;
  197. }
  198. } else  {
  199. ut_ad(mode == BTR_MODIFY_TREE);
  200. err = btr_cur_pessimistic_update(0, cursor, update, 0, thr,
  201. mtr);
  202. }
  203. mem_heap_free(heap);
  204. return(err);
  205. }
  206. /*******************************************************************
  207. Checks if a unique key violation to rec would occur at the index entry
  208. insert. */
  209. static
  210. ibool
  211. row_ins_dupl_error_with_rec(
  212. /*========================*/
  213. /* out: TRUE if error */
  214. rec_t* rec, /* in: user record */
  215. dtuple_t* entry, /* in: entry to insert */
  216. dict_index_t* index, /* in: index */
  217. trx_t* trx) /* in: inserting transaction */
  218. {
  219. ulint matched_fields;
  220. ulint matched_bytes;
  221. ulint n_unique;
  222. trx_t* impl_trx;
  223. n_unique = dict_index_get_n_unique(index);
  224. matched_fields = 0;
  225. matched_bytes = 0;
  226. cmp_dtuple_rec_with_match(entry, rec, &matched_fields, &matched_bytes);
  227. if (matched_fields < n_unique) {
  228. return(FALSE);
  229. }
  230. if (!rec_get_deleted_flag(rec)) {
  231. return(TRUE);
  232. }
  233. /* If we get here, the record has its delete mark set. It is still
  234. a unique key violation if the transaction which set the delete mark
  235. is currently active and is not trx itself. We check if some
  236. transaction has an implicit x-lock on the record. */
  237. mutex_enter(&kernel_mutex);
  238. if (index->type & DICT_CLUSTERED) {
  239. impl_trx = lock_clust_rec_some_has_impl(rec, index);
  240. } else {
  241. impl_trx = lock_sec_rec_some_has_impl_off_kernel(rec, index);
  242. }
  243. mutex_exit(&kernel_mutex);
  244. if (impl_trx && impl_trx != trx) {
  245. return(TRUE);
  246. }
  247. return(FALSE);
  248. }
  249. /*******************************************************************
  250. Scans a unique non-clustered index at a given index entry to determine
  251. whether a uniqueness violation has occurred for the key value of the entry. */
  252. static
  253. ulint
  254. row_ins_scan_sec_index_for_duplicate(
  255. /*=================================*/
  256. /* out: DB_SUCCESS or DB_DUPLICATE_KEY */
  257. dict_index_t* index, /* in: non-clustered unique index */
  258. dtuple_t* entry, /* in: index entry */
  259. trx_t* trx) /* in: inserting transaction */
  260. {
  261. ulint dupl_count = 0;
  262. int cmp;
  263. ulint n_fields_cmp;
  264. rec_t* rec;
  265. btr_pcur_t pcur;
  266. mtr_t mtr;
  267. mtr_start(&mtr);
  268. /* Store old value on n_fields_cmp */
  269. n_fields_cmp = dtuple_get_n_fields_cmp(entry);
  270. dtuple_set_n_fields_cmp(entry, dict_index_get_n_unique(index));
  271. btr_pcur_open_on_user_rec(index, entry, PAGE_CUR_GE,
  272. BTR_SEARCH_LEAF, &pcur, &mtr);
  273. /* Scan index records and check that there are no duplicates */
  274. for (;;) {
  275. if (btr_pcur_is_after_last_in_tree(&pcur, &mtr)) {
  276. break;
  277. }
  278. rec = btr_pcur_get_rec(&pcur);
  279. cmp = cmp_dtuple_rec(entry, rec);
  280. if (cmp == 0) {
  281. if (row_ins_dupl_error_with_rec(rec, entry, index,
  282. trx)) {
  283. dupl_count++;
  284. if (dupl_count > 1) {
  285. /* printf(
  286. "Duplicate key in index %sn",
  287.       index->name);
  288. dtuple_print(entry); */
  289. }
  290. }
  291. }
  292. if (cmp < 0) {
  293. break;
  294. }
  295. ut_a(cmp == 0);
  296. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  297. }
  298. mtr_commit(&mtr);
  299. /* Restore old value */
  300. dtuple_set_n_fields_cmp(entry, n_fields_cmp);
  301. ut_a(dupl_count >= 1);
  302. if (dupl_count > 1) {
  303. trx->error_info = index;
  304. return(DB_DUPLICATE_KEY);
  305. }
  306. return(DB_SUCCESS);
  307. }
  308. /*******************************************************************
  309. Tries to check if a unique key violation error would occur at an index entry
  310. insert. */
  311. static
  312. ulint
  313. row_ins_duplicate_error(
  314. /*====================*/
  315. /* out: DB_SUCCESS if no error
  316. DB_DUPLICATE_KEY if error,
  317. DB_STRONG_FAIL if this is a non-clustered
  318. index record and we cannot determine yet
  319. if there will be an error: in this last
  320. case we must call
  321. row_ins_scan_sec_index_for_duplicate
  322. AFTER the insertion of the record! */
  323. btr_cur_t* cursor, /* in: B-tree cursor */
  324. dtuple_t* entry, /* in: entry to insert */
  325. trx_t* trx, /* in: inserting transaction */
  326. mtr_t* mtr, /* in: mtr */
  327. rec_t** dupl_rec)/* out: record with which duplicate error */
  328. {
  329. rec_t* rec;
  330. page_t* page;
  331. ulint n_unique;
  332. UT_NOT_USED(mtr);
  333. ut_ad(cursor->index->type & DICT_UNIQUE);
  334. /* NOTE: For unique non-clustered indexes there may be any number
  335. of delete marked records with the same value for the non-clustered
  336. index key (remember multiversioning), and which differ only in
  337. the row refererence part of the index record, containing the
  338. clustered index key fields. For such a secondary index record,
  339. to avoid race condition, we must FIRST do the insertion and after
  340. that check that the uniqueness condition is not breached! */
  341. /* NOTE: A problem is that in the B-tree node pointers on an
  342. upper level may match more to the entry than the actual existing
  343. user records on the leaf level. So, even if low_match would suggest
  344. that a duplicate key violation may occur, this may not be the case. */
  345. n_unique = dict_index_get_n_unique(cursor->index);
  346. if (cursor->low_match >= n_unique) {
  347. rec = btr_cur_get_rec(cursor);
  348. page = buf_frame_align(rec);
  349. if (rec != page_get_infimum_rec(page)) {
  350. if (row_ins_dupl_error_with_rec(rec, entry,
  351. cursor->index, trx)) {
  352. *dupl_rec = rec;
  353. trx->error_info = cursor->index;
  354. return(DB_DUPLICATE_KEY);
  355. }
  356. }
  357. }
  358. if (cursor->up_match >= n_unique) {
  359. rec = page_rec_get_next(btr_cur_get_rec(cursor));
  360. page = buf_frame_align(rec);
  361. if (rec != page_get_supremum_rec(page)) {
  362. if (row_ins_dupl_error_with_rec(rec, entry,
  363. cursor->index, trx)) {
  364. *dupl_rec = rec;
  365. trx->error_info = cursor->index;
  366. return(DB_DUPLICATE_KEY);
  367. }
  368. }
  369. ut_a(!(cursor->index->type & DICT_CLUSTERED));
  370. /* This should never happen */
  371. }
  372. if (cursor->index->type & DICT_CLUSTERED) {
  373. return(DB_SUCCESS);
  374. }
  375. /* It was a non-clustered index: we must scan the index after the
  376. insertion to be sure if there will be duplicate key error */
  377. return(DB_STRONG_FAIL);
  378. }
  379. /*******************************************************************
  380. Checks if an index entry has long enough common prefix with an existing
  381. record so that the intended insert of the entry must be changed to a modify of
  382. the existing record. In the case of a clustered index, the prefix must be
  383. n_unique fields long, and in the case of a secondary index, all fields must be
  384. equal. */
  385. UNIV_INLINE
  386. ulint
  387. row_ins_must_modify(
  388. /*================*/
  389. /* out: 0 if no update, ROW_INS_PREV if
  390. previous should be updated; currently we
  391. do the search so that only the low_match
  392. record can match enough to the search tuple,
  393. not the next record */
  394. btr_cur_t* cursor) /* in: B-tree cursor */
  395. {
  396. ulint enough_match;
  397. rec_t* rec;
  398. page_t* page;
  399. /* NOTE: (compare to the note in row_ins_duplicate_error) Because node
  400. pointers on upper levels of the B-tree may match more to entry than
  401. to actual user records on the leaf level, we have to check if the
  402. candidate record is actually a user record. In a clustered index
  403. node pointers contain index->n_unique first fields, and in the case
  404. of a secondary index, all fields of the index. */
  405. enough_match = dict_index_get_n_unique_in_tree(cursor->index);
  406. if (cursor->low_match >= enough_match) {
  407. rec = btr_cur_get_rec(cursor);
  408. page = buf_frame_align(rec);
  409. if (rec != page_get_infimum_rec(page)) {
  410. return(ROW_INS_PREV);
  411. }
  412. }
  413. return(0);
  414. }
  415. /*******************************************************************
  416. Tries to insert an index entry to an index. If the index is clustered
  417. and a record with the same unique key is found, the other record is
  418. necessarily marked deleted by a committed transaction, or a unique key
  419. violation error occurs. The delete marked record is then updated to an
  420. existing record, and we must write an undo log record on the delete
  421. marked record. If the index is secondary, and a record with exactly the
  422. same fields is found, the other record is necessarily marked deleted.
  423. It is then unmarked. Otherwise, the entry is just inserted to the index. */
  424. ulint
  425. row_ins_index_entry_low(
  426. /*====================*/
  427. /* out: DB_SUCCESS, DB_LOCK_WAIT, DB_FAIL
  428. if pessimistic retry needed, or error code */
  429. ulint mode, /* in: BTR_MODIFY_LEAF or BTR_MODIFY_TREE,
  430. depending on whether we wish optimistic or
  431. pessimistic descent down the index tree */
  432. dict_index_t* index, /* in: index */
  433. dtuple_t* entry, /* in: index entry to insert */
  434. que_thr_t* thr) /* in: query thread */
  435. {
  436. btr_cur_t cursor;
  437. ulint dupl = DB_SUCCESS;
  438. ulint modify;
  439. rec_t* dummy_rec;
  440. rec_t* rec;
  441. rec_t* dupl_rec; /* Note that this may be undefined
  442. for a non-clustered index even if
  443. there is a duplicate key */
  444. ulint err;
  445. ulint n_unique;
  446. mtr_t mtr;
  447. log_free_check();
  448. mtr_start(&mtr);
  449. cursor.thr = thr;
  450. /* Note that we use PAGE_CUR_LE as the search mode, because then
  451. the function will return in both low_match and up_match of the
  452. cursor sensible values */
  453. btr_cur_search_to_nth_level(index, 0, entry, PAGE_CUR_LE,
  454. mode | BTR_INSERT, &cursor, 0, &mtr);
  455. if (cursor.flag == BTR_CUR_INSERT_TO_IBUF) {
  456. /* The insertion was made to the insert buffer already during
  457. the search: we are done */
  458. err = DB_SUCCESS;
  459. goto function_exit;
  460. }
  461. n_unique = dict_index_get_n_unique(index);
  462. if (index->type & DICT_UNIQUE && (cursor.up_match >= n_unique
  463.  || cursor.low_match >= n_unique)) {
  464. dupl = row_ins_duplicate_error(&cursor, entry,
  465. thr_get_trx(thr), &mtr, &dupl_rec);
  466. if (dupl == DB_DUPLICATE_KEY) {
  467. /* printf("Duplicate key in index %s lm %lun",
  468.      cursor->index->name, cursor->low_match);
  469. rec_print(rec);
  470. dtuple_print(entry); */
  471. err = dupl;
  472. goto function_exit;
  473. }
  474. }
  475. modify = row_ins_must_modify(&cursor);
  476. if (modify != 0) {
  477. /* There is already an index entry with a long enough common
  478. prefix, we must convert the insert into a modify of an
  479. existing record */
  480. if (modify == ROW_INS_NEXT) {
  481. rec = page_rec_get_next(btr_cur_get_rec(&cursor));
  482. btr_cur_position(index, rec, &cursor);
  483. }
  484. if (index->type & DICT_CLUSTERED) {
  485. err = row_ins_clust_index_entry_by_modify(mode,
  486. &cursor, entry,
  487. thr, &mtr);
  488. } else {
  489. err = row_ins_sec_index_entry_by_modify(&cursor,
  490. thr, &mtr);
  491. }
  492. } else if (mode == BTR_MODIFY_LEAF) {
  493. err = btr_cur_optimistic_insert(0, &cursor, entry,
  494. &dummy_rec, thr, &mtr);
  495. } else {
  496. ut_ad(mode == BTR_MODIFY_TREE);
  497. err = btr_cur_pessimistic_insert(0, &cursor, entry,
  498. &dummy_rec, thr, &mtr);
  499. }
  500. function_exit:
  501. mtr_commit(&mtr);
  502. if (err == DB_SUCCESS && dupl == DB_STRONG_FAIL) {
  503. /* We were not able to determine before the insertion
  504. whether there will be a duplicate key error: do the check
  505. now */
  506. err = row_ins_scan_sec_index_for_duplicate(index, entry,
  507. thr_get_trx(thr));
  508. }
  509. ut_ad(err != DB_DUPLICATE_KEY || index->type & DICT_CLUSTERED
  510. || DB_DUPLICATE_KEY ==
  511. row_ins_scan_sec_index_for_duplicate(index, entry,
  512. thr_get_trx(thr)));
  513. return(err);
  514. }
  515. /*******************************************************************
  516. Inserts an index entry to index. Tries first optimistic, then pessimistic
  517. descent down the tree. If the entry matches enough to a delete marked record,
  518. performs the insert by updating or delete unmarking the delete marked
  519. record. */
  520. ulint
  521. row_ins_index_entry(
  522. /*================*/
  523. /* out: DB_SUCCESS, DB_LOCK_WAIT,
  524. DB_DUPLICATE_KEY, or some other error code */
  525. dict_index_t* index, /* in: index */
  526. dtuple_t* entry, /* in: index entry to insert */
  527. que_thr_t* thr) /* in: query thread */
  528. {
  529. ulint err;
  530. /* Try first optimistic descent to the B-tree */
  531. err = row_ins_index_entry_low(BTR_MODIFY_LEAF, index, entry, thr);
  532. if (err != DB_FAIL) {
  533. return(err);
  534. }
  535. /* Try then pessimistic descent to the B-tree */
  536. err = row_ins_index_entry_low(BTR_MODIFY_TREE, index, entry, thr);
  537. return(err);
  538. }
  539. /***************************************************************
  540. Sets the values of the dtuple fields in entry from the values of appropriate
  541. columns in row. */
  542. UNIV_INLINE
  543. void
  544. row_ins_index_entry_set_vals(
  545. /*=========================*/
  546. dtuple_t* entry, /* in: index entry to make */
  547. dtuple_t* row) /* in: row */
  548. {
  549. dfield_t* field;
  550. dfield_t* row_field;
  551. ulint n_fields;
  552. ulint i;
  553. ut_ad(entry && row);
  554. n_fields = dtuple_get_n_fields(entry);
  555. for (i = 0; i < n_fields; i++) {
  556. field = dtuple_get_nth_field(entry, i);
  557. row_field = dtuple_get_nth_field(row, field->col_no);
  558. field->data = row_field->data;
  559. field->len = row_field->len;
  560. }
  561. }
  562. /***************************************************************
  563. Inserts a single index entry to the table. */
  564. UNIV_INLINE
  565. ulint
  566. row_ins_index_entry_step(
  567. /*=====================*/
  568. /* out: DB_SUCCESS if operation successfully
  569. completed, else error code or DB_LOCK_WAIT */
  570. ins_node_t* node, /* in: row insert node */
  571. que_thr_t* thr) /* in: query thread */
  572. {
  573. ulint err;
  574. ut_ad(dtuple_check_typed(node->row));
  575. row_ins_index_entry_set_vals(node->entry, node->row);
  576. ut_ad(dtuple_check_typed(node->entry));
  577. err = row_ins_index_entry(node->index, node->entry, thr);
  578. return(err);
  579. }
  580. /***************************************************************
  581. Allocates a row id for row and inits the node->index field. */
  582. UNIV_INLINE
  583. void
  584. row_ins_alloc_row_id_step(
  585. /*======================*/
  586. ins_node_t* node) /* in: row insert node */
  587. {
  588. dulint row_id;
  589. ut_ad(node->state == INS_NODE_ALLOC_ROW_ID);
  590. if (dict_table_get_first_index(node->table)->type & DICT_UNIQUE) {
  591. /* No row id is stored if the clustered index is unique */
  592. return;
  593. }
  594. /* Fill in row id value to row */
  595. row_id = dict_sys_get_new_row_id();
  596. dict_sys_write_row_id(node->row_id_buf, row_id);
  597. }
  598. /***************************************************************
  599. Gets a row to insert from the values list. */
  600. UNIV_INLINE
  601. void
  602. row_ins_get_row_from_values(
  603. /*========================*/
  604. ins_node_t* node) /* in: row insert node */
  605. {
  606. que_node_t* list_node;
  607. dfield_t* dfield;
  608. dtuple_t* row;
  609. ulint i;
  610. /* The field values are copied in the buffers of the select node and
  611. it is safe to use them until we fetch from select again: therefore
  612. we can just copy the pointers */
  613. row = node->row; 
  614. i = 0;
  615. list_node = node->values_list;
  616. while (list_node) {
  617. eval_exp(list_node);
  618. dfield = dtuple_get_nth_field(row, i);
  619. dfield_copy_data(dfield, que_node_get_val(list_node));
  620. i++;
  621. list_node = que_node_get_next(list_node);
  622. }
  623. }
  624. /***************************************************************
  625. Gets a row to insert from the select list. */
  626. UNIV_INLINE
  627. void
  628. row_ins_get_row_from_select(
  629. /*========================*/
  630. ins_node_t* node) /* in: row insert node */
  631. {
  632. que_node_t* list_node;
  633. dfield_t* dfield;
  634. dtuple_t* row;
  635. ulint i;
  636. /* The field values are copied in the buffers of the select node and
  637. it is safe to use them until we fetch from select again: therefore
  638. we can just copy the pointers */
  639. row = node->row; 
  640. i = 0;
  641. list_node = node->select->select_list;
  642. while (list_node) {
  643. dfield = dtuple_get_nth_field(row, i);
  644. dfield_copy_data(dfield, que_node_get_val(list_node));
  645. i++;
  646. list_node = que_node_get_next(list_node);
  647. }
  648. }
  649. /***************************************************************
  650. Inserts a row to a table. */
  651. ulint
  652. row_ins(
  653. /*====*/
  654. /* out: DB_SUCCESS if operation successfully
  655. completed, else error code or DB_LOCK_WAIT */
  656. ins_node_t* node, /* in: row insert node */
  657. que_thr_t* thr) /* in: query thread */
  658. {
  659. ulint err;
  660. ut_ad(node && thr);
  661. if (node->state == INS_NODE_ALLOC_ROW_ID) {
  662. row_ins_alloc_row_id_step(node);
  663. node->index = dict_table_get_first_index(node->table);
  664. node->entry = UT_LIST_GET_FIRST(node->entry_list);
  665. if (node->ins_type == INS_SEARCHED) {
  666. row_ins_get_row_from_select(node);
  667. } else if (node->ins_type == INS_VALUES) {
  668. row_ins_get_row_from_values(node);
  669. }
  670. node->state = INS_NODE_INSERT_ENTRIES;
  671. }
  672. ut_ad(node->state == INS_NODE_INSERT_ENTRIES);
  673. while (node->index != NULL) {
  674. err = row_ins_index_entry_step(node, thr);
  675. if (err != DB_SUCCESS) {
  676. return(err);
  677. }
  678. node->index = dict_table_get_next_index(node->index);
  679. node->entry = UT_LIST_GET_NEXT(tuple_list, node->entry);
  680. }
  681. ut_ad(node->entry == NULL);
  682. node->state = INS_NODE_ALLOC_ROW_ID;
  683. return(DB_SUCCESS);
  684. }
  685. /***************************************************************
  686. Inserts a row to a table. This is a high-level function used in SQL execution
  687. graphs. */
  688. que_thr_t*
  689. row_ins_step(
  690. /*=========*/
  691. /* out: query thread to run next or NULL */
  692. que_thr_t* thr) /* in: query thread */
  693. {
  694. ins_node_t* node;
  695. que_node_t* parent;
  696. sel_node_t* sel_node;
  697. trx_t* trx;
  698. ulint err;
  699. ut_ad(thr);
  700. trx = thr_get_trx(thr);
  701. node = thr->run_node;
  702. ut_ad(que_node_get_type(node) == QUE_NODE_INSERT);
  703. parent = que_node_get_parent(node);
  704. sel_node = node->select;
  705. if (thr->prev_node == parent) {
  706. node->state = INS_NODE_SET_IX_LOCK;
  707. }
  708. /* If this is the first time this node is executed (or when
  709. execution resumes after wait for the table IX lock), set an
  710. IX lock on the table and reset the possible select node. */
  711. if (node->state == INS_NODE_SET_IX_LOCK) {
  712. /* It may be that the current session has not yet started
  713. its transaction, or it has been committed: */
  714. trx_start_if_not_started(trx);
  715. if (UT_DULINT_EQ(trx->id, node->trx_id)) {
  716. /* No need to do IX-locking or write trx id to buf */
  717. goto same_trx;
  718. }
  719. trx_write_trx_id(node->trx_id_buf, trx->id);
  720. err = lock_table(0, node->table, LOCK_IX, thr);
  721. if (err != DB_SUCCESS) {
  722. goto error_handling;
  723. }
  724. node->trx_id = trx->id;
  725. same_trx:
  726. node->state = INS_NODE_ALLOC_ROW_ID;
  727. if (node->ins_type == INS_SEARCHED) {
  728. /* Reset the cursor */
  729. sel_node->state = SEL_NODE_OPEN;
  730.  
  731. /* Fetch a row to insert */
  732. thr->run_node = sel_node;
  733. return(thr);
  734. }
  735. }
  736. if ((node->ins_type == INS_SEARCHED)
  737. && (sel_node->state != SEL_NODE_FETCH)) {
  738. ut_ad(sel_node->state == SEL_NODE_NO_MORE_ROWS);
  739. /* No more rows to insert */
  740. thr->run_node = parent;
  741. return(thr);
  742. }
  743. /* DO THE CHECKS OF THE CONSISTENCY CONSTRAINTS HERE */
  744. err = row_ins(node, thr);
  745. error_handling:
  746. trx->error_state = err;
  747. if (err == DB_SUCCESS) {
  748. /* Ok: do nothing */
  749. } else if (err == DB_LOCK_WAIT) {
  750. return(NULL);
  751. } else {
  752. /* SQL error detected */
  753. return(NULL);
  754. }
  755. /* DO THE TRIGGER ACTIONS HERE */
  756. if (node->ins_type == INS_SEARCHED) {
  757. /* Fetch a row to insert */
  758. thr->run_node = sel_node;
  759. } else {
  760. thr->run_node = que_node_get_parent(node);
  761. }
  762. return(thr);