You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4855 lines
134 KiB

20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
branches/zip: Initialize dfield_t::ext as soon as possible. This should fix the bugs introduced in r1591. row_rec_to_index_entry_low(): Clear "n_ext". Do not allow it to be NULL. Add const qualifier to dict_index_t*. row_rec_to_index_entry(): Add the parameters "offsets" and "n_ext". btr_cur_optimistic_update(): Add an assertion that there are no externally stored columns. Remove the unreachable call to btr_cur_unmark_extern_fields() and the preceding unnecessary call to rec_get_offsets(). btr_push_update_extern_fields(): Remove the parameters index, offsets. Only report the additional externally stored columns of the update vector. row_build(), trx_undo_rec_get_partial_row(): Flag externally stored columns also with dfield_set_ext(). rec_copy_prefix_to_dtuple(): Assert that there are no externally stored columns in the prefix. row_build_row_ref(): Note and assert that the index is a secondary index, and assert that there are no externally stored columns. row_build_row_ref_fast(): Assert that there are no externally stored columns. rec_offs_get_n_alloc(): Expose the function. row_build_row_ref_in_tuple(): Assert that there are no externally stored columns in a record of a secondary index. row_build_row_ref_from_row(): Assert that there are no externally stored columns. row_upd_check_references_constraints(): Add the parameter offsets, to avoid a redundant call to rec_get_offsets(). row_upd_del_mark_clust_rec(): Add the parameter offsets. Remove duplicated code. row_ins_index_entry_set_vals(): Copy the external storage flag. sel_pop_prefetched_row(): Assert that there are no externally stored columns. row_scan_and_check_index(): Copy offsets to a temporary heap across the invocation of row_rec_to_index_entry().
18 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
branches/zip: Initialize dfield_t::ext as soon as possible. This should fix the bugs introduced in r1591. row_rec_to_index_entry_low(): Clear "n_ext". Do not allow it to be NULL. Add const qualifier to dict_index_t*. row_rec_to_index_entry(): Add the parameters "offsets" and "n_ext". btr_cur_optimistic_update(): Add an assertion that there are no externally stored columns. Remove the unreachable call to btr_cur_unmark_extern_fields() and the preceding unnecessary call to rec_get_offsets(). btr_push_update_extern_fields(): Remove the parameters index, offsets. Only report the additional externally stored columns of the update vector. row_build(), trx_undo_rec_get_partial_row(): Flag externally stored columns also with dfield_set_ext(). rec_copy_prefix_to_dtuple(): Assert that there are no externally stored columns in the prefix. row_build_row_ref(): Note and assert that the index is a secondary index, and assert that there are no externally stored columns. row_build_row_ref_fast(): Assert that there are no externally stored columns. rec_offs_get_n_alloc(): Expose the function. row_build_row_ref_in_tuple(): Assert that there are no externally stored columns in a record of a secondary index. row_build_row_ref_from_row(): Assert that there are no externally stored columns. row_upd_check_references_constraints(): Add the parameter offsets, to avoid a redundant call to rec_get_offsets(). row_upd_del_mark_clust_rec(): Add the parameter offsets. Remove duplicated code. row_ins_index_entry_set_vals(): Copy the external storage flag. sel_pop_prefetched_row(): Assert that there are no externally stored columns. row_scan_and_check_index(): Copy offsets to a temporary heap across the invocation of row_rec_to_index_entry().
18 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
branches/zip: Initialize dfield_t::ext as soon as possible. This should fix the bugs introduced in r1591. row_rec_to_index_entry_low(): Clear "n_ext". Do not allow it to be NULL. Add const qualifier to dict_index_t*. row_rec_to_index_entry(): Add the parameters "offsets" and "n_ext". btr_cur_optimistic_update(): Add an assertion that there are no externally stored columns. Remove the unreachable call to btr_cur_unmark_extern_fields() and the preceding unnecessary call to rec_get_offsets(). btr_push_update_extern_fields(): Remove the parameters index, offsets. Only report the additional externally stored columns of the update vector. row_build(), trx_undo_rec_get_partial_row(): Flag externally stored columns also with dfield_set_ext(). rec_copy_prefix_to_dtuple(): Assert that there are no externally stored columns in the prefix. row_build_row_ref(): Note and assert that the index is a secondary index, and assert that there are no externally stored columns. row_build_row_ref_fast(): Assert that there are no externally stored columns. rec_offs_get_n_alloc(): Expose the function. row_build_row_ref_in_tuple(): Assert that there are no externally stored columns in a record of a secondary index. row_build_row_ref_from_row(): Assert that there are no externally stored columns. row_upd_check_references_constraints(): Add the parameter offsets, to avoid a redundant call to rec_get_offsets(). row_upd_del_mark_clust_rec(): Add the parameter offsets. Remove duplicated code. row_ins_index_entry_set_vals(): Copy the external storage flag. sel_pop_prefetched_row(): Assert that there are no externally stored columns. row_scan_and_check_index(): Copy offsets to a temporary heap across the invocation of row_rec_to_index_entry().
18 years ago
20 years ago
20 years ago
20 years ago
20 years ago
branches/zip: Initialize dfield_t::ext as soon as possible. This should fix the bugs introduced in r1591. row_rec_to_index_entry_low(): Clear "n_ext". Do not allow it to be NULL. Add const qualifier to dict_index_t*. row_rec_to_index_entry(): Add the parameters "offsets" and "n_ext". btr_cur_optimistic_update(): Add an assertion that there are no externally stored columns. Remove the unreachable call to btr_cur_unmark_extern_fields() and the preceding unnecessary call to rec_get_offsets(). btr_push_update_extern_fields(): Remove the parameters index, offsets. Only report the additional externally stored columns of the update vector. row_build(), trx_undo_rec_get_partial_row(): Flag externally stored columns also with dfield_set_ext(). rec_copy_prefix_to_dtuple(): Assert that there are no externally stored columns in the prefix. row_build_row_ref(): Note and assert that the index is a secondary index, and assert that there are no externally stored columns. row_build_row_ref_fast(): Assert that there are no externally stored columns. rec_offs_get_n_alloc(): Expose the function. row_build_row_ref_in_tuple(): Assert that there are no externally stored columns in a record of a secondary index. row_build_row_ref_from_row(): Assert that there are no externally stored columns. row_upd_check_references_constraints(): Add the parameter offsets, to avoid a redundant call to rec_get_offsets(). row_upd_del_mark_clust_rec(): Add the parameter offsets. Remove duplicated code. row_ins_index_entry_set_vals(): Copy the external storage flag. sel_pop_prefetched_row(): Assert that there are no externally stored columns. row_scan_and_check_index(): Copy offsets to a temporary heap across the invocation of row_rec_to_index_entry().
18 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
branches/zip: Initialize dfield_t::ext as soon as possible. This should fix the bugs introduced in r1591. row_rec_to_index_entry_low(): Clear "n_ext". Do not allow it to be NULL. Add const qualifier to dict_index_t*. row_rec_to_index_entry(): Add the parameters "offsets" and "n_ext". btr_cur_optimistic_update(): Add an assertion that there are no externally stored columns. Remove the unreachable call to btr_cur_unmark_extern_fields() and the preceding unnecessary call to rec_get_offsets(). btr_push_update_extern_fields(): Remove the parameters index, offsets. Only report the additional externally stored columns of the update vector. row_build(), trx_undo_rec_get_partial_row(): Flag externally stored columns also with dfield_set_ext(). rec_copy_prefix_to_dtuple(): Assert that there are no externally stored columns in the prefix. row_build_row_ref(): Note and assert that the index is a secondary index, and assert that there are no externally stored columns. row_build_row_ref_fast(): Assert that there are no externally stored columns. rec_offs_get_n_alloc(): Expose the function. row_build_row_ref_in_tuple(): Assert that there are no externally stored columns in a record of a secondary index. row_build_row_ref_from_row(): Assert that there are no externally stored columns. row_upd_check_references_constraints(): Add the parameter offsets, to avoid a redundant call to rec_get_offsets(). row_upd_del_mark_clust_rec(): Add the parameter offsets. Remove duplicated code. row_ins_index_entry_set_vals(): Copy the external storage flag. sel_pop_prefetched_row(): Assert that there are no externally stored columns. row_scan_and_check_index(): Copy offsets to a temporary heap across the invocation of row_rec_to_index_entry().
18 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
branches/zip: dtuple_convert_big_rec(): Do not store anything locally of externally stored columns, and fix bugs introduced in r873. (Bug #22496) btr_page_get_sure_split_rec(), btr_page_insert_fits(), rec_get_converted_size(), rec_convert_dtuple_to_rec(), rec_convert_dtuple_to_rec_old(), rec_convert_dtuple_to_rec_new(): Add parameters ext and n_ext. Flag external fields during the conversion. rec_set_field_extern_bits(), rec_set_field_extern_bits_new(), rec_offs_set_nth_extern(), rec_set_nth_field_extern_bit_old(): Remove. The bits are set by rec_convert_dtuple_to_rec(). page_cur_insert_rec_low(): Remove the parameters ext and n_ext. btr_cur_add_ext(): New utility function for updating and sorting ext[]. Low-level functions now expect the array to be in ascending order for performance reasons. Used in btr_cur_optimistic_insert(), btr_cur_pessimistic_insert(), and btr_cur_pessimistic_update(). btr_cur_optimistic_insert(): Remove some defensive code, because we cannot compute the added parameters of rec_get_converted_size(). btr_push_update_extern_fields(): Sort the array. Require the array to be twice the maximum usage, so that ut_ulint_sort() can be used. dtuple_convert_big_rec(): Allocate new space for the BLOB pointer, to avoid overwriting prefix indexes to the same column. Adapt dtuple_convert_back_big_rec(). row_build_index_entry(): Fetch the columns also for prefix indexes of the clustered index. page_zip_apply_log(), page_zip_decompress_clust(): Allow externally stored fields to lack a locally stored part.
19 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
branches/zip: Try to synchronize the updates of uncompressed and compressed pages. btr_root_raise_and_insert(): Distinguish root_page_zip and new_page_zip. btr_cur_set_ownership_of_extern_field(): Do not log the write on the uncompressed page if it will be logged for page_zip. lock_rec_insert_check_and_lock(), lock_sec_rec_modify_check_and_lock(): Update the max_trx_id field also on the compressed page. mlog_write_ulint(): Add UNIV_UNLIKELY hints. Remove trailing white space. mlog_log_string(): Remove trailing white space. rec_set_field_extern_bits(): Remove parameter mtr, as the write will either occur in the heap, or it will be logged at a higher level. recv_parse_or_apply_log_rec_body(), page_zip_write_header(): Add log record type MLOG_ZIP_WRITE_HEADER. page_header_set_field(): Pass mtr=NULL to page_zip_write_header(). page_header_reset_last_insert(): Pass mtr to page_zip_write_header(). btr_page_set_index_id(), btr_page_set_level(), btr_page_set_next(), btr_page_set_prev(): Pass mtr to page_zip_write_header(). row_upd_rec_sys_fields(): Pass mtr=NULL to page_zip_write_trx_id() and page_zip_write_roll_ptr(), since the write will be logged at a higher level. page_zip_write_header(): Add parameter mtr. page_zip_write_header_log(): New function. Remove rec_set_nth_field_extern_bit(). Make rec_set_nth_field_extern_bit_old() static. Rename rec_set_nth_field_extern_bit_new() to rec_set_field_extern_bits_new() and make it static. row_ins_index_entry_low(): Remove bogus TODO comment.
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
branches/zip: Initialize dfield_t::ext as soon as possible. This should fix the bugs introduced in r1591. row_rec_to_index_entry_low(): Clear "n_ext". Do not allow it to be NULL. Add const qualifier to dict_index_t*. row_rec_to_index_entry(): Add the parameters "offsets" and "n_ext". btr_cur_optimistic_update(): Add an assertion that there are no externally stored columns. Remove the unreachable call to btr_cur_unmark_extern_fields() and the preceding unnecessary call to rec_get_offsets(). btr_push_update_extern_fields(): Remove the parameters index, offsets. Only report the additional externally stored columns of the update vector. row_build(), trx_undo_rec_get_partial_row(): Flag externally stored columns also with dfield_set_ext(). rec_copy_prefix_to_dtuple(): Assert that there are no externally stored columns in the prefix. row_build_row_ref(): Note and assert that the index is a secondary index, and assert that there are no externally stored columns. row_build_row_ref_fast(): Assert that there are no externally stored columns. rec_offs_get_n_alloc(): Expose the function. row_build_row_ref_in_tuple(): Assert that there are no externally stored columns in a record of a secondary index. row_build_row_ref_from_row(): Assert that there are no externally stored columns. row_upd_check_references_constraints(): Add the parameter offsets, to avoid a redundant call to rec_get_offsets(). row_upd_del_mark_clust_rec(): Add the parameter offsets. Remove duplicated code. row_ins_index_entry_set_vals(): Copy the external storage flag. sel_pop_prefetched_row(): Assert that there are no externally stored columns. row_scan_and_check_index(): Copy offsets to a temporary heap across the invocation of row_rec_to_index_entry().
18 years ago
20 years ago
20 years ago
branches/zip: Initialize dfield_t::ext as soon as possible. This should fix the bugs introduced in r1591. row_rec_to_index_entry_low(): Clear "n_ext". Do not allow it to be NULL. Add const qualifier to dict_index_t*. row_rec_to_index_entry(): Add the parameters "offsets" and "n_ext". btr_cur_optimistic_update(): Add an assertion that there are no externally stored columns. Remove the unreachable call to btr_cur_unmark_extern_fields() and the preceding unnecessary call to rec_get_offsets(). btr_push_update_extern_fields(): Remove the parameters index, offsets. Only report the additional externally stored columns of the update vector. row_build(), trx_undo_rec_get_partial_row(): Flag externally stored columns also with dfield_set_ext(). rec_copy_prefix_to_dtuple(): Assert that there are no externally stored columns in the prefix. row_build_row_ref(): Note and assert that the index is a secondary index, and assert that there are no externally stored columns. row_build_row_ref_fast(): Assert that there are no externally stored columns. rec_offs_get_n_alloc(): Expose the function. row_build_row_ref_in_tuple(): Assert that there are no externally stored columns in a record of a secondary index. row_build_row_ref_from_row(): Assert that there are no externally stored columns. row_upd_check_references_constraints(): Add the parameter offsets, to avoid a redundant call to rec_get_offsets(). row_upd_del_mark_clust_rec(): Add the parameter offsets. Remove duplicated code. row_ins_index_entry_set_vals(): Copy the external storage flag. sel_pop_prefetched_row(): Assert that there are no externally stored columns. row_scan_and_check_index(): Copy offsets to a temporary heap across the invocation of row_rec_to_index_entry().
18 years ago
20 years ago
branches/zip: Initialize dfield_t::ext as soon as possible. This should fix the bugs introduced in r1591. row_rec_to_index_entry_low(): Clear "n_ext". Do not allow it to be NULL. Add const qualifier to dict_index_t*. row_rec_to_index_entry(): Add the parameters "offsets" and "n_ext". btr_cur_optimistic_update(): Add an assertion that there are no externally stored columns. Remove the unreachable call to btr_cur_unmark_extern_fields() and the preceding unnecessary call to rec_get_offsets(). btr_push_update_extern_fields(): Remove the parameters index, offsets. Only report the additional externally stored columns of the update vector. row_build(), trx_undo_rec_get_partial_row(): Flag externally stored columns also with dfield_set_ext(). rec_copy_prefix_to_dtuple(): Assert that there are no externally stored columns in the prefix. row_build_row_ref(): Note and assert that the index is a secondary index, and assert that there are no externally stored columns. row_build_row_ref_fast(): Assert that there are no externally stored columns. rec_offs_get_n_alloc(): Expose the function. row_build_row_ref_in_tuple(): Assert that there are no externally stored columns in a record of a secondary index. row_build_row_ref_from_row(): Assert that there are no externally stored columns. row_upd_check_references_constraints(): Add the parameter offsets, to avoid a redundant call to rec_get_offsets(). row_upd_del_mark_clust_rec(): Add the parameter offsets. Remove duplicated code. row_ins_index_entry_set_vals(): Copy the external storage flag. sel_pop_prefetched_row(): Assert that there are no externally stored columns. row_scan_and_check_index(): Copy offsets to a temporary heap across the invocation of row_rec_to_index_entry().
18 years ago
20 years ago
branches/zip: Initialize dfield_t::ext as soon as possible. This should fix the bugs introduced in r1591. row_rec_to_index_entry_low(): Clear "n_ext". Do not allow it to be NULL. Add const qualifier to dict_index_t*. row_rec_to_index_entry(): Add the parameters "offsets" and "n_ext". btr_cur_optimistic_update(): Add an assertion that there are no externally stored columns. Remove the unreachable call to btr_cur_unmark_extern_fields() and the preceding unnecessary call to rec_get_offsets(). btr_push_update_extern_fields(): Remove the parameters index, offsets. Only report the additional externally stored columns of the update vector. row_build(), trx_undo_rec_get_partial_row(): Flag externally stored columns also with dfield_set_ext(). rec_copy_prefix_to_dtuple(): Assert that there are no externally stored columns in the prefix. row_build_row_ref(): Note and assert that the index is a secondary index, and assert that there are no externally stored columns. row_build_row_ref_fast(): Assert that there are no externally stored columns. rec_offs_get_n_alloc(): Expose the function. row_build_row_ref_in_tuple(): Assert that there are no externally stored columns in a record of a secondary index. row_build_row_ref_from_row(): Assert that there are no externally stored columns. row_upd_check_references_constraints(): Add the parameter offsets, to avoid a redundant call to rec_get_offsets(). row_upd_del_mark_clust_rec(): Add the parameter offsets. Remove duplicated code. row_ins_index_entry_set_vals(): Copy the external storage flag. sel_pop_prefetched_row(): Assert that there are no externally stored columns. row_scan_and_check_index(): Copy offsets to a temporary heap across the invocation of row_rec_to_index_entry().
18 years ago
branches/zip: Initialize dfield_t::ext as soon as possible. This should fix the bugs introduced in r1591. row_rec_to_index_entry_low(): Clear "n_ext". Do not allow it to be NULL. Add const qualifier to dict_index_t*. row_rec_to_index_entry(): Add the parameters "offsets" and "n_ext". btr_cur_optimistic_update(): Add an assertion that there are no externally stored columns. Remove the unreachable call to btr_cur_unmark_extern_fields() and the preceding unnecessary call to rec_get_offsets(). btr_push_update_extern_fields(): Remove the parameters index, offsets. Only report the additional externally stored columns of the update vector. row_build(), trx_undo_rec_get_partial_row(): Flag externally stored columns also with dfield_set_ext(). rec_copy_prefix_to_dtuple(): Assert that there are no externally stored columns in the prefix. row_build_row_ref(): Note and assert that the index is a secondary index, and assert that there are no externally stored columns. row_build_row_ref_fast(): Assert that there are no externally stored columns. rec_offs_get_n_alloc(): Expose the function. row_build_row_ref_in_tuple(): Assert that there are no externally stored columns in a record of a secondary index. row_build_row_ref_from_row(): Assert that there are no externally stored columns. row_upd_check_references_constraints(): Add the parameter offsets, to avoid a redundant call to rec_get_offsets(). row_upd_del_mark_clust_rec(): Add the parameter offsets. Remove duplicated code. row_ins_index_entry_set_vals(): Copy the external storage flag. sel_pop_prefetched_row(): Assert that there are no externally stored columns. row_scan_and_check_index(): Copy offsets to a temporary heap across the invocation of row_rec_to_index_entry().
18 years ago
20 years ago
branches/zip: Initialize dfield_t::ext as soon as possible. This should fix the bugs introduced in r1591. row_rec_to_index_entry_low(): Clear "n_ext". Do not allow it to be NULL. Add const qualifier to dict_index_t*. row_rec_to_index_entry(): Add the parameters "offsets" and "n_ext". btr_cur_optimistic_update(): Add an assertion that there are no externally stored columns. Remove the unreachable call to btr_cur_unmark_extern_fields() and the preceding unnecessary call to rec_get_offsets(). btr_push_update_extern_fields(): Remove the parameters index, offsets. Only report the additional externally stored columns of the update vector. row_build(), trx_undo_rec_get_partial_row(): Flag externally stored columns also with dfield_set_ext(). rec_copy_prefix_to_dtuple(): Assert that there are no externally stored columns in the prefix. row_build_row_ref(): Note and assert that the index is a secondary index, and assert that there are no externally stored columns. row_build_row_ref_fast(): Assert that there are no externally stored columns. rec_offs_get_n_alloc(): Expose the function. row_build_row_ref_in_tuple(): Assert that there are no externally stored columns in a record of a secondary index. row_build_row_ref_from_row(): Assert that there are no externally stored columns. row_upd_check_references_constraints(): Add the parameter offsets, to avoid a redundant call to rec_get_offsets(). row_upd_del_mark_clust_rec(): Add the parameter offsets. Remove duplicated code. row_ins_index_entry_set_vals(): Copy the external storage flag. sel_pop_prefetched_row(): Assert that there are no externally stored columns. row_scan_and_check_index(): Copy offsets to a temporary heap across the invocation of row_rec_to_index_entry().
18 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
branches/zip: Improve the LRU algorithm with a separate unzip_LRU list of blocks that contains uncompressed and compressed frames. This patch was designed by Heikki and Inaam, implemented by Inaam, and refined and reviewed by Marko and Sunny. buf_buddy_n_frames, buf_buddy_min_n_frames, buf_buddy_max_n_frames: Remove. buf_page_belongs_to_unzip_LRU(): New predicate: bpage->zip.data && buf_page_get_state(bpage) == BUF_BLOCK_FILE_PAGE. buf_pool_t, buf_block_t: Add the linked list unzip_LRU. A block in the regular LRU list is in unzip_LRU iff buf_page_belongs_to_unzip_LRU() holds. buf_LRU_free_block(): Add a third return value to refine the case "cannot free the block". buf_LRU_search_and_free_block(): Update the documentation to reflect the implementation. buf_LRU_stat_t, buf_LRU_stat_cur, buf_LRU_stat_sum, buf_LRU_stat_arr[]: Statistics for the unzip_LRU algorithm. buf_LRU_stat_update(): New function: Update the statistics. Called once per second by srv_error_monitor_thread(). buf_LRU_validate(): Validate the unzip_LRU list as well. buf_LRU_evict_from_unzip_LRU(): New predicate: Use the unzip_LRU before falling back to the regular LRU? buf_LRU_free_from_unzip_LRU_list(), buf_LRU_free_from_common_LRU_list(): Subfunctions of buf_LRU_search_and_free_block(). buf_LRU_search_and_free_block(): Reimplement. Try to evict an uncompressed page from the unzip_LRU list before falling back to evicting an entire block from the common LRU list. buf_unzip_LRU_remove_block_if_needed(): New function. buf_unzip_LRU_add_block(): New function: Add a block to the unzip_LRU list.
18 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1994, 2010, Innobase Oy. All Rights Reserved.
  3. Copyright (c) 2008, Google Inc.
  4. Portions of this file contain modifications contributed and copyrighted by
  5. Google, Inc. Those modifications are gratefully acknowledged and are described
  6. briefly in the InnoDB documentation. The contributions by Google are
  7. incorporated with their permission, and subject to the conditions contained in
  8. the file COPYING.Google.
  9. This program is free software; you can redistribute it and/or modify it under
  10. the terms of the GNU General Public License as published by the Free Software
  11. Foundation; version 2 of the License.
  12. This program is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  14. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License along with
  16. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  17. Place, Suite 330, Boston, MA 02111-1307 USA
  18. *****************************************************************************/
  19. /**************************************************//**
  20. @file btr/btr0cur.c
  21. The index tree cursor
  22. All changes that row operations make to a B-tree or the records
  23. there must go through this module! Undo log records are written here
  24. of every modify or insert of a clustered index record.
  25. NOTE!!!
  26. To make sure we do not run out of disk space during a pessimistic
  27. insert or update, we have to reserve 2 x the height of the index tree
  28. many pages in the tablespace before we start the operation, because
  29. if leaf splitting has been started, it is difficult to undo, except
  30. by crashing the database and doing a roll-forward.
  31. Created 10/16/1994 Heikki Tuuri
  32. *******************************************************/
  33. #include "btr0cur.h"
  34. #ifdef UNIV_NONINL
  35. #include "btr0cur.ic"
  36. #endif
  37. #include "row0upd.h"
  38. #ifndef UNIV_HOTBACKUP
  39. #include "mtr0log.h"
  40. #include "page0page.h"
  41. #include "page0zip.h"
  42. #include "rem0rec.h"
  43. #include "rem0cmp.h"
  44. #include "buf0lru.h"
  45. #include "btr0btr.h"
  46. #include "btr0sea.h"
  47. #include "trx0rec.h"
  48. #include "trx0roll.h" /* trx_is_recv() */
  49. #include "que0que.h"
  50. #include "row0row.h"
  51. #include "srv0srv.h"
  52. #include "ibuf0ibuf.h"
  53. #include "lock0lock.h"
  54. #include "zlib.h"
  55. #ifdef UNIV_DEBUG
  56. /** If the following is set to TRUE, this module prints a lot of
  57. trace information of individual record operations */
  58. UNIV_INTERN ibool btr_cur_print_record_ops = FALSE;
  59. #endif /* UNIV_DEBUG */
  60. /** Number of searches down the B-tree in btr_cur_search_to_nth_level(). */
  61. UNIV_INTERN ulint btr_cur_n_non_sea = 0;
  62. /** Number of successful adaptive hash index lookups in
  63. btr_cur_search_to_nth_level(). */
  64. UNIV_INTERN ulint btr_cur_n_sea = 0;
  65. /** Old value of btr_cur_n_non_sea. Copied by
  66. srv_refresh_innodb_monitor_stats(). Referenced by
  67. srv_printf_innodb_monitor(). */
  68. UNIV_INTERN ulint btr_cur_n_non_sea_old = 0;
  69. /** Old value of btr_cur_n_sea. Copied by
  70. srv_refresh_innodb_monitor_stats(). Referenced by
  71. srv_printf_innodb_monitor(). */
  72. UNIV_INTERN ulint btr_cur_n_sea_old = 0;
  73. /** In the optimistic insert, if the insert does not fit, but this much space
  74. can be released by page reorganize, then it is reorganized */
  75. #define BTR_CUR_PAGE_REORGANIZE_LIMIT (UNIV_PAGE_SIZE / 32)
  76. /** The structure of a BLOB part header */
  77. /* @{ */
  78. /*--------------------------------------*/
  79. #define BTR_BLOB_HDR_PART_LEN 0 /*!< BLOB part len on this
  80. page */
  81. #define BTR_BLOB_HDR_NEXT_PAGE_NO 4 /*!< next BLOB part page no,
  82. FIL_NULL if none */
  83. /*--------------------------------------*/
  84. #define BTR_BLOB_HDR_SIZE 8 /*!< Size of a BLOB
  85. part header, in bytes */
  86. /* @} */
  87. #endif /* !UNIV_HOTBACKUP */
  88. /** A BLOB field reference full of zero, for use in assertions and tests.
  89. Initially, BLOB field references are set to zero, in
  90. dtuple_convert_big_rec(). */
  91. UNIV_INTERN const byte field_ref_zero[BTR_EXTERN_FIELD_REF_SIZE];
  92. #ifndef UNIV_HOTBACKUP
  93. /*******************************************************************//**
  94. Marks all extern fields in a record as owned by the record. This function
  95. should be called if the delete mark of a record is removed: a not delete
  96. marked record always owns all its extern fields. */
  97. static
  98. void
  99. btr_cur_unmark_extern_fields(
  100. /*=========================*/
  101. page_zip_des_t* page_zip,/*!< in/out: compressed page whose uncompressed
  102. part will be updated, or NULL */
  103. rec_t* rec, /*!< in/out: record in a clustered index */
  104. dict_index_t* index, /*!< in: index of the page */
  105. const ulint* offsets,/*!< in: array returned by rec_get_offsets() */
  106. mtr_t* mtr); /*!< in: mtr, or NULL if not logged */
  107. /*******************************************************************//**
  108. Adds path information to the cursor for the current page, for which
  109. the binary search has been performed. */
  110. static
  111. void
  112. btr_cur_add_path_info(
  113. /*==================*/
  114. btr_cur_t* cursor, /*!< in: cursor positioned on a page */
  115. ulint height, /*!< in: height of the page in tree;
  116. 0 means leaf node */
  117. ulint root_height); /*!< in: root node height in tree */
  118. /***********************************************************//**
  119. Frees the externally stored fields for a record, if the field is mentioned
  120. in the update vector. */
  121. static
  122. void
  123. btr_rec_free_updated_extern_fields(
  124. /*===============================*/
  125. dict_index_t* index, /*!< in: index of rec; the index tree MUST be
  126. X-latched */
  127. rec_t* rec, /*!< in: record */
  128. page_zip_des_t* page_zip,/*!< in: compressed page whose uncompressed
  129. part will be updated, or NULL */
  130. const ulint* offsets,/*!< in: rec_get_offsets(rec, index) */
  131. const upd_t* update, /*!< in: update vector */
  132. enum trx_rb_ctx rb_ctx, /*!< in: rollback context */
  133. mtr_t* mtr); /*!< in: mini-transaction handle which contains
  134. an X-latch to record page and to the tree */
  135. /***********************************************************//**
  136. Frees the externally stored fields for a record. */
  137. static
  138. void
  139. btr_rec_free_externally_stored_fields(
  140. /*==================================*/
  141. dict_index_t* index, /*!< in: index of the data, the index
  142. tree MUST be X-latched */
  143. rec_t* rec, /*!< in: record */
  144. const ulint* offsets,/*!< in: rec_get_offsets(rec, index) */
  145. page_zip_des_t* page_zip,/*!< in: compressed page whose uncompressed
  146. part will be updated, or NULL */
  147. enum trx_rb_ctx rb_ctx, /*!< in: rollback context */
  148. mtr_t* mtr); /*!< in: mini-transaction handle which contains
  149. an X-latch to record page and to the index
  150. tree */
  151. /***********************************************************//**
  152. Gets the externally stored size of a record, in units of a database page.
  153. @return externally stored part, in units of a database page */
  154. static
  155. ulint
  156. btr_rec_get_externally_stored_len(
  157. /*==============================*/
  158. rec_t* rec, /*!< in: record */
  159. const ulint* offsets);/*!< in: array returned by rec_get_offsets() */
  160. #endif /* !UNIV_HOTBACKUP */
  161. /******************************************************//**
  162. The following function is used to set the deleted bit of a record. */
  163. UNIV_INLINE
  164. void
  165. btr_rec_set_deleted_flag(
  166. /*=====================*/
  167. rec_t* rec, /*!< in/out: physical record */
  168. page_zip_des_t* page_zip,/*!< in/out: compressed page (or NULL) */
  169. ulint flag) /*!< in: nonzero if delete marked */
  170. {
  171. if (page_rec_is_comp(rec)) {
  172. rec_set_deleted_flag_new(rec, page_zip, flag);
  173. } else {
  174. ut_ad(!page_zip);
  175. rec_set_deleted_flag_old(rec, flag);
  176. }
  177. }
  178. #ifndef UNIV_HOTBACKUP
  179. /*==================== B-TREE SEARCH =========================*/
  180. /********************************************************************//**
  181. Latches the leaf page or pages requested. */
  182. static
  183. void
  184. btr_cur_latch_leaves(
  185. /*=================*/
  186. page_t* page, /*!< in: leaf page where the search
  187. converged */
  188. ulint space, /*!< in: space id */
  189. ulint zip_size, /*!< in: compressed page size in bytes
  190. or 0 for uncompressed pages */
  191. ulint page_no, /*!< in: page number of the leaf */
  192. ulint latch_mode, /*!< in: BTR_SEARCH_LEAF, ... */
  193. btr_cur_t* cursor, /*!< in: cursor */
  194. mtr_t* mtr) /*!< in: mtr */
  195. {
  196. ulint mode;
  197. ulint left_page_no;
  198. ulint right_page_no;
  199. buf_block_t* get_block;
  200. ut_ad(page && mtr);
  201. switch (latch_mode) {
  202. case BTR_SEARCH_LEAF:
  203. case BTR_MODIFY_LEAF:
  204. mode = latch_mode == BTR_SEARCH_LEAF ? RW_S_LATCH : RW_X_LATCH;
  205. get_block = btr_block_get(space, zip_size, page_no, mode, mtr);
  206. #ifdef UNIV_BTR_DEBUG
  207. ut_a(page_is_comp(get_block->frame) == page_is_comp(page));
  208. #endif /* UNIV_BTR_DEBUG */
  209. get_block->check_index_page_at_flush = TRUE;
  210. return;
  211. case BTR_MODIFY_TREE:
  212. /* x-latch also brothers from left to right */
  213. left_page_no = btr_page_get_prev(page, mtr);
  214. if (left_page_no != FIL_NULL) {
  215. get_block = btr_block_get(space, zip_size,
  216. left_page_no,
  217. RW_X_LATCH, mtr);
  218. #ifdef UNIV_BTR_DEBUG
  219. ut_a(page_is_comp(get_block->frame)
  220. == page_is_comp(page));
  221. ut_a(btr_page_get_next(get_block->frame, mtr)
  222. == page_get_page_no(page));
  223. #endif /* UNIV_BTR_DEBUG */
  224. get_block->check_index_page_at_flush = TRUE;
  225. }
  226. get_block = btr_block_get(space, zip_size, page_no,
  227. RW_X_LATCH, mtr);
  228. #ifdef UNIV_BTR_DEBUG
  229. ut_a(page_is_comp(get_block->frame) == page_is_comp(page));
  230. #endif /* UNIV_BTR_DEBUG */
  231. get_block->check_index_page_at_flush = TRUE;
  232. right_page_no = btr_page_get_next(page, mtr);
  233. if (right_page_no != FIL_NULL) {
  234. get_block = btr_block_get(space, zip_size,
  235. right_page_no,
  236. RW_X_LATCH, mtr);
  237. #ifdef UNIV_BTR_DEBUG
  238. ut_a(page_is_comp(get_block->frame)
  239. == page_is_comp(page));
  240. ut_a(btr_page_get_prev(get_block->frame, mtr)
  241. == page_get_page_no(page));
  242. #endif /* UNIV_BTR_DEBUG */
  243. get_block->check_index_page_at_flush = TRUE;
  244. }
  245. return;
  246. case BTR_SEARCH_PREV:
  247. case BTR_MODIFY_PREV:
  248. mode = latch_mode == BTR_SEARCH_PREV ? RW_S_LATCH : RW_X_LATCH;
  249. /* latch also left brother */
  250. left_page_no = btr_page_get_prev(page, mtr);
  251. if (left_page_no != FIL_NULL) {
  252. get_block = btr_block_get(space, zip_size,
  253. left_page_no, mode, mtr);
  254. cursor->left_block = get_block;
  255. #ifdef UNIV_BTR_DEBUG
  256. ut_a(page_is_comp(get_block->frame)
  257. == page_is_comp(page));
  258. ut_a(btr_page_get_next(get_block->frame, mtr)
  259. == page_get_page_no(page));
  260. #endif /* UNIV_BTR_DEBUG */
  261. get_block->check_index_page_at_flush = TRUE;
  262. }
  263. get_block = btr_block_get(space, zip_size, page_no, mode, mtr);
  264. #ifdef UNIV_BTR_DEBUG
  265. ut_a(page_is_comp(get_block->frame) == page_is_comp(page));
  266. #endif /* UNIV_BTR_DEBUG */
  267. get_block->check_index_page_at_flush = TRUE;
  268. return;
  269. }
  270. ut_error;
  271. }
  272. /********************************************************************//**
  273. Searches an index tree and positions a tree cursor on a given level.
  274. NOTE: n_fields_cmp in tuple must be set so that it cannot be compared
  275. to node pointer page number fields on the upper levels of the tree!
  276. Note that if mode is PAGE_CUR_LE, which is used in inserts, then
  277. cursor->up_match and cursor->low_match both will have sensible values.
  278. If mode is PAGE_CUR_GE, then up_match will a have a sensible value.
  279. If mode is PAGE_CUR_LE , cursor is left at the place where an insert of the
  280. search tuple should be performed in the B-tree. InnoDB does an insert
  281. immediately after the cursor. Thus, the cursor may end up on a user record,
  282. or on a page infimum record. */
  283. UNIV_INTERN
  284. void
  285. btr_cur_search_to_nth_level(
  286. /*========================*/
  287. dict_index_t* index, /*!< in: index */
  288. ulint level, /*!< in: the tree level of search */
  289. const dtuple_t* tuple, /*!< in: data tuple; NOTE: n_fields_cmp in
  290. tuple must be set so that it cannot get
  291. compared to the node ptr page number field! */
  292. ulint mode, /*!< in: PAGE_CUR_L, ...;
  293. Inserts should always be made using
  294. PAGE_CUR_LE to search the position! */
  295. ulint latch_mode, /*!< in: BTR_SEARCH_LEAF, ..., ORed with
  296. BTR_INSERT and BTR_ESTIMATE;
  297. cursor->left_block is used to store a pointer
  298. to the left neighbor page, in the cases
  299. BTR_SEARCH_PREV and BTR_MODIFY_PREV;
  300. NOTE that if has_search_latch
  301. is != 0, we maybe do not have a latch set
  302. on the cursor page, we assume
  303. the caller uses his search latch
  304. to protect the record! */
  305. btr_cur_t* cursor, /*!< in/out: tree cursor; the cursor page is
  306. s- or x-latched, but see also above! */
  307. ulint has_search_latch,/*!< in: info on the latch mode the
  308. caller currently has on btr_search_latch:
  309. RW_S_LATCH, or 0 */
  310. const char* file, /*!< in: file name */
  311. ulint line, /*!< in: line where called */
  312. mtr_t* mtr) /*!< in: mtr */
  313. {
  314. page_cur_t* page_cursor;
  315. page_t* page;
  316. buf_block_t* guess;
  317. rec_t* node_ptr;
  318. ulint page_no;
  319. ulint space;
  320. ulint up_match;
  321. ulint up_bytes;
  322. ulint low_match;
  323. ulint low_bytes;
  324. ulint height;
  325. ulint savepoint;
  326. ulint page_mode;
  327. ulint insert_planned;
  328. ulint estimate;
  329. ulint ignore_sec_unique;
  330. ulint root_height = 0; /* remove warning */
  331. #ifdef BTR_CUR_ADAPT
  332. btr_search_t* info;
  333. #endif
  334. mem_heap_t* heap = NULL;
  335. ulint offsets_[REC_OFFS_NORMAL_SIZE];
  336. ulint* offsets = offsets_;
  337. rec_offs_init(offsets_);
  338. /* Currently, PAGE_CUR_LE is the only search mode used for searches
  339. ending to upper levels */
  340. ut_ad(level == 0 || mode == PAGE_CUR_LE);
  341. ut_ad(dict_index_check_search_tuple(index, tuple));
  342. ut_ad(!dict_index_is_ibuf(index) || ibuf_inside());
  343. ut_ad(dtuple_check_typed(tuple));
  344. #ifdef UNIV_DEBUG
  345. cursor->up_match = ULINT_UNDEFINED;
  346. cursor->low_match = ULINT_UNDEFINED;
  347. #endif
  348. insert_planned = latch_mode & BTR_INSERT;
  349. estimate = latch_mode & BTR_ESTIMATE;
  350. ignore_sec_unique = latch_mode & BTR_IGNORE_SEC_UNIQUE;
  351. latch_mode = latch_mode & ~(BTR_INSERT | BTR_ESTIMATE
  352. | BTR_IGNORE_SEC_UNIQUE);
  353. ut_ad(!insert_planned || (mode == PAGE_CUR_LE));
  354. cursor->flag = BTR_CUR_BINARY;
  355. cursor->index = index;
  356. #ifndef BTR_CUR_ADAPT
  357. guess = NULL;
  358. #else
  359. info = btr_search_get_info(index);
  360. guess = info->root_guess;
  361. #ifdef BTR_CUR_HASH_ADAPT
  362. #ifdef UNIV_SEARCH_PERF_STAT
  363. info->n_searches++;
  364. #endif
  365. if (rw_lock_get_writer(&btr_search_latch) == RW_LOCK_NOT_LOCKED
  366. && latch_mode <= BTR_MODIFY_LEAF && info->last_hash_succ
  367. && !estimate
  368. #ifdef PAGE_CUR_LE_OR_EXTENDS
  369. && mode != PAGE_CUR_LE_OR_EXTENDS
  370. #endif /* PAGE_CUR_LE_OR_EXTENDS */
  371. /* If !has_search_latch, we do a dirty read of
  372. btr_search_enabled below, and btr_search_guess_on_hash()
  373. will have to check it again. */
  374. && UNIV_LIKELY(btr_search_enabled)
  375. && btr_search_guess_on_hash(index, info, tuple, mode,
  376. latch_mode, cursor,
  377. has_search_latch, mtr)) {
  378. /* Search using the hash index succeeded */
  379. ut_ad(cursor->up_match != ULINT_UNDEFINED
  380. || mode != PAGE_CUR_GE);
  381. ut_ad(cursor->up_match != ULINT_UNDEFINED
  382. || mode != PAGE_CUR_LE);
  383. ut_ad(cursor->low_match != ULINT_UNDEFINED
  384. || mode != PAGE_CUR_LE);
  385. btr_cur_n_sea++;
  386. return;
  387. }
  388. #endif /* BTR_CUR_HASH_ADAPT */
  389. #endif /* BTR_CUR_ADAPT */
  390. btr_cur_n_non_sea++;
  391. /* If the hash search did not succeed, do binary search down the
  392. tree */
  393. if (has_search_latch) {
  394. /* Release possible search latch to obey latching order */
  395. rw_lock_s_unlock(&btr_search_latch);
  396. }
  397. /* Store the position of the tree latch we push to mtr so that we
  398. know how to release it when we have latched leaf node(s) */
  399. savepoint = mtr_set_savepoint(mtr);
  400. if (latch_mode == BTR_MODIFY_TREE) {
  401. mtr_x_lock(dict_index_get_lock(index), mtr);
  402. } else if (latch_mode == BTR_CONT_MODIFY_TREE) {
  403. /* Do nothing */
  404. ut_ad(mtr_memo_contains(mtr, dict_index_get_lock(index),
  405. MTR_MEMO_X_LOCK));
  406. } else {
  407. mtr_s_lock(dict_index_get_lock(index), mtr);
  408. }
  409. page_cursor = btr_cur_get_page_cur(cursor);
  410. space = dict_index_get_space(index);
  411. page_no = dict_index_get_page(index);
  412. up_match = 0;
  413. up_bytes = 0;
  414. low_match = 0;
  415. low_bytes = 0;
  416. height = ULINT_UNDEFINED;
  417. /* We use these modified search modes on non-leaf levels of the
  418. B-tree. These let us end up in the right B-tree leaf. In that leaf
  419. we use the original search mode. */
  420. switch (mode) {
  421. case PAGE_CUR_GE:
  422. page_mode = PAGE_CUR_L;
  423. break;
  424. case PAGE_CUR_G:
  425. page_mode = PAGE_CUR_LE;
  426. break;
  427. default:
  428. #ifdef PAGE_CUR_LE_OR_EXTENDS
  429. ut_ad(mode == PAGE_CUR_L || mode == PAGE_CUR_LE
  430. || mode == PAGE_CUR_LE_OR_EXTENDS);
  431. #else /* PAGE_CUR_LE_OR_EXTENDS */
  432. ut_ad(mode == PAGE_CUR_L || mode == PAGE_CUR_LE);
  433. #endif /* PAGE_CUR_LE_OR_EXTENDS */
  434. page_mode = mode;
  435. break;
  436. }
  437. /* Loop and search until we arrive at the desired level */
  438. for (;;) {
  439. ulint zip_size;
  440. buf_block_t* block;
  441. ulint rw_latch;
  442. ulint buf_mode;
  443. zip_size = dict_table_zip_size(index->table);
  444. rw_latch = RW_NO_LATCH;
  445. buf_mode = BUF_GET;
  446. if (height == 0 && latch_mode <= BTR_MODIFY_LEAF) {
  447. rw_latch = latch_mode;
  448. if (insert_planned
  449. && ibuf_should_try(index, ignore_sec_unique)) {
  450. /* Try insert to the insert buffer if the
  451. page is not in the buffer pool */
  452. buf_mode = BUF_GET_IF_IN_POOL;
  453. }
  454. }
  455. retry_page_get:
  456. block = buf_page_get_gen(space, zip_size, page_no,
  457. rw_latch, guess, buf_mode,
  458. file, line, mtr);
  459. if (block == NULL) {
  460. /* This must be a search to perform an insert;
  461. try insert to the insert buffer */
  462. ut_ad(buf_mode == BUF_GET_IF_IN_POOL);
  463. ut_ad(insert_planned);
  464. ut_ad(cursor->thr);
  465. if (ibuf_insert(tuple, index, space, zip_size,
  466. page_no, cursor->thr)) {
  467. /* Insertion to the insert buffer succeeded */
  468. cursor->flag = BTR_CUR_INSERT_TO_IBUF;
  469. if (UNIV_LIKELY_NULL(heap)) {
  470. mem_heap_free(heap);
  471. }
  472. goto func_exit;
  473. }
  474. /* Insert to the insert buffer did not succeed:
  475. retry page get */
  476. buf_mode = BUF_GET;
  477. goto retry_page_get;
  478. }
  479. page = buf_block_get_frame(block);
  480. block->check_index_page_at_flush = TRUE;
  481. if (rw_latch != RW_NO_LATCH) {
  482. #ifdef UNIV_ZIP_DEBUG
  483. const page_zip_des_t* page_zip
  484. = buf_block_get_page_zip(block);
  485. ut_a(!page_zip || page_zip_validate(page_zip, page));
  486. #endif /* UNIV_ZIP_DEBUG */
  487. buf_block_dbg_add_level(block, SYNC_TREE_NODE);
  488. }
  489. ut_ad(0 == ut_dulint_cmp(index->id,
  490. btr_page_get_index_id(page)));
  491. if (UNIV_UNLIKELY(height == ULINT_UNDEFINED)) {
  492. /* We are in the root node */
  493. height = btr_page_get_level(page, mtr);
  494. root_height = height;
  495. cursor->tree_height = root_height + 1;
  496. #ifdef BTR_CUR_ADAPT
  497. if (block != guess) {
  498. info->root_guess = block;
  499. }
  500. #endif
  501. }
  502. if (height == 0) {
  503. if (rw_latch == RW_NO_LATCH) {
  504. btr_cur_latch_leaves(page, space, zip_size,
  505. page_no, latch_mode,
  506. cursor, mtr);
  507. }
  508. if ((latch_mode != BTR_MODIFY_TREE)
  509. && (latch_mode != BTR_CONT_MODIFY_TREE)) {
  510. /* Release the tree s-latch */
  511. mtr_release_s_latch_at_savepoint(
  512. mtr, savepoint,
  513. dict_index_get_lock(index));
  514. }
  515. page_mode = mode;
  516. }
  517. page_cur_search_with_match(block, index, tuple, page_mode,
  518. &up_match, &up_bytes,
  519. &low_match, &low_bytes,
  520. page_cursor);
  521. if (estimate) {
  522. btr_cur_add_path_info(cursor, height, root_height);
  523. }
  524. /* If this is the desired level, leave the loop */
  525. ut_ad(height == btr_page_get_level(
  526. page_cur_get_page(page_cursor), mtr));
  527. if (level == height) {
  528. if (level > 0) {
  529. /* x-latch the page */
  530. page = btr_page_get(space, zip_size,
  531. page_no, RW_X_LATCH, mtr);
  532. ut_a((ibool)!!page_is_comp(page)
  533. == dict_table_is_comp(index->table));
  534. }
  535. break;
  536. }
  537. ut_ad(height > 0);
  538. height--;
  539. guess = NULL;
  540. node_ptr = page_cur_get_rec(page_cursor);
  541. offsets = rec_get_offsets(node_ptr, cursor->index, offsets,
  542. ULINT_UNDEFINED, &heap);
  543. /* Go to the child node */
  544. page_no = btr_node_ptr_get_child_page_no(node_ptr, offsets);
  545. }
  546. if (UNIV_LIKELY_NULL(heap)) {
  547. mem_heap_free(heap);
  548. }
  549. if (level == 0) {
  550. cursor->low_match = low_match;
  551. cursor->low_bytes = low_bytes;
  552. cursor->up_match = up_match;
  553. cursor->up_bytes = up_bytes;
  554. #ifdef BTR_CUR_ADAPT
  555. /* We do a dirty read of btr_search_enabled here. We
  556. will properly check btr_search_enabled again in
  557. btr_search_build_page_hash_index() before building a
  558. page hash index, while holding btr_search_latch. */
  559. if (UNIV_LIKELY(btr_search_enabled)) {
  560. btr_search_info_update(index, cursor);
  561. }
  562. #endif
  563. ut_ad(cursor->up_match != ULINT_UNDEFINED
  564. || mode != PAGE_CUR_GE);
  565. ut_ad(cursor->up_match != ULINT_UNDEFINED
  566. || mode != PAGE_CUR_LE);
  567. ut_ad(cursor->low_match != ULINT_UNDEFINED
  568. || mode != PAGE_CUR_LE);
  569. }
  570. func_exit:
  571. if (has_search_latch) {
  572. rw_lock_s_lock(&btr_search_latch);
  573. }
  574. }
  575. /*****************************************************************//**
  576. Opens a cursor at either end of an index. */
  577. UNIV_INTERN
  578. void
  579. btr_cur_open_at_index_side_func(
  580. /*============================*/
  581. ibool from_left, /*!< in: TRUE if open to the low end,
  582. FALSE if to the high end */
  583. dict_index_t* index, /*!< in: index */
  584. ulint latch_mode, /*!< in: latch mode */
  585. btr_cur_t* cursor, /*!< in: cursor */
  586. const char* file, /*!< in: file name */
  587. ulint line, /*!< in: line where called */
  588. mtr_t* mtr) /*!< in: mtr */
  589. {
  590. page_cur_t* page_cursor;
  591. ulint page_no;
  592. ulint space;
  593. ulint zip_size;
  594. ulint height;
  595. ulint root_height = 0; /* remove warning */
  596. rec_t* node_ptr;
  597. ulint estimate;
  598. ulint savepoint;
  599. mem_heap_t* heap = NULL;
  600. ulint offsets_[REC_OFFS_NORMAL_SIZE];
  601. ulint* offsets = offsets_;
  602. rec_offs_init(offsets_);
  603. estimate = latch_mode & BTR_ESTIMATE;
  604. latch_mode = latch_mode & ~BTR_ESTIMATE;
  605. /* Store the position of the tree latch we push to mtr so that we
  606. know how to release it when we have latched the leaf node */
  607. savepoint = mtr_set_savepoint(mtr);
  608. if (latch_mode == BTR_MODIFY_TREE) {
  609. mtr_x_lock(dict_index_get_lock(index), mtr);
  610. } else {
  611. mtr_s_lock(dict_index_get_lock(index), mtr);
  612. }
  613. page_cursor = btr_cur_get_page_cur(cursor);
  614. cursor->index = index;
  615. space = dict_index_get_space(index);
  616. zip_size = dict_table_zip_size(index->table);
  617. page_no = dict_index_get_page(index);
  618. height = ULINT_UNDEFINED;
  619. for (;;) {
  620. buf_block_t* block;
  621. page_t* page;
  622. block = buf_page_get_gen(space, zip_size, page_no,
  623. RW_NO_LATCH, NULL, BUF_GET,
  624. file, line, mtr);
  625. page = buf_block_get_frame(block);
  626. ut_ad(0 == ut_dulint_cmp(index->id,
  627. btr_page_get_index_id(page)));
  628. block->check_index_page_at_flush = TRUE;
  629. if (height == ULINT_UNDEFINED) {
  630. /* We are in the root node */
  631. height = btr_page_get_level(page, mtr);
  632. root_height = height;
  633. }
  634. if (height == 0) {
  635. btr_cur_latch_leaves(page, space, zip_size, page_no,
  636. latch_mode, cursor, mtr);
  637. /* In versions <= 3.23.52 we had forgotten to
  638. release the tree latch here. If in an index scan
  639. we had to scan far to find a record visible to the
  640. current transaction, that could starve others
  641. waiting for the tree latch. */
  642. if ((latch_mode != BTR_MODIFY_TREE)
  643. && (latch_mode != BTR_CONT_MODIFY_TREE)) {
  644. /* Release the tree s-latch */
  645. mtr_release_s_latch_at_savepoint(
  646. mtr, savepoint,
  647. dict_index_get_lock(index));
  648. }
  649. }
  650. if (from_left) {
  651. page_cur_set_before_first(block, page_cursor);
  652. } else {
  653. page_cur_set_after_last(block, page_cursor);
  654. }
  655. if (height == 0) {
  656. if (estimate) {
  657. btr_cur_add_path_info(cursor, height,
  658. root_height);
  659. }
  660. break;
  661. }
  662. ut_ad(height > 0);
  663. if (from_left) {
  664. page_cur_move_to_next(page_cursor);
  665. } else {
  666. page_cur_move_to_prev(page_cursor);
  667. }
  668. if (estimate) {
  669. btr_cur_add_path_info(cursor, height, root_height);
  670. }
  671. height--;
  672. node_ptr = page_cur_get_rec(page_cursor);
  673. offsets = rec_get_offsets(node_ptr, cursor->index, offsets,
  674. ULINT_UNDEFINED, &heap);
  675. /* Go to the child node */
  676. page_no = btr_node_ptr_get_child_page_no(node_ptr, offsets);
  677. }
  678. if (UNIV_LIKELY_NULL(heap)) {
  679. mem_heap_free(heap);
  680. }
  681. }
  682. /**********************************************************************//**
  683. Positions a cursor at a randomly chosen position within a B-tree. */
  684. UNIV_INTERN
  685. void
  686. btr_cur_open_at_rnd_pos_func(
  687. /*=========================*/
  688. dict_index_t* index, /*!< in: index */
  689. ulint latch_mode, /*!< in: BTR_SEARCH_LEAF, ... */
  690. btr_cur_t* cursor, /*!< in/out: B-tree cursor */
  691. const char* file, /*!< in: file name */
  692. ulint line, /*!< in: line where called */
  693. mtr_t* mtr) /*!< in: mtr */
  694. {
  695. page_cur_t* page_cursor;
  696. ulint page_no;
  697. ulint space;
  698. ulint zip_size;
  699. ulint height;
  700. rec_t* node_ptr;
  701. mem_heap_t* heap = NULL;
  702. ulint offsets_[REC_OFFS_NORMAL_SIZE];
  703. ulint* offsets = offsets_;
  704. rec_offs_init(offsets_);
  705. if (latch_mode == BTR_MODIFY_TREE) {
  706. mtr_x_lock(dict_index_get_lock(index), mtr);
  707. } else {
  708. mtr_s_lock(dict_index_get_lock(index), mtr);
  709. }
  710. page_cursor = btr_cur_get_page_cur(cursor);
  711. cursor->index = index;
  712. space = dict_index_get_space(index);
  713. zip_size = dict_table_zip_size(index->table);
  714. page_no = dict_index_get_page(index);
  715. height = ULINT_UNDEFINED;
  716. for (;;) {
  717. buf_block_t* block;
  718. page_t* page;
  719. block = buf_page_get_gen(space, zip_size, page_no,
  720. RW_NO_LATCH, NULL, BUF_GET,
  721. file, line, mtr);
  722. page = buf_block_get_frame(block);
  723. ut_ad(0 == ut_dulint_cmp(index->id,
  724. btr_page_get_index_id(page)));
  725. if (height == ULINT_UNDEFINED) {
  726. /* We are in the root node */
  727. height = btr_page_get_level(page, mtr);
  728. }
  729. if (height == 0) {
  730. btr_cur_latch_leaves(page, space, zip_size, page_no,
  731. latch_mode, cursor, mtr);
  732. }
  733. page_cur_open_on_rnd_user_rec(block, page_cursor);
  734. if (height == 0) {
  735. break;
  736. }
  737. ut_ad(height > 0);
  738. height--;
  739. node_ptr = page_cur_get_rec(page_cursor);
  740. offsets = rec_get_offsets(node_ptr, cursor->index, offsets,
  741. ULINT_UNDEFINED, &heap);
  742. /* Go to the child node */
  743. page_no = btr_node_ptr_get_child_page_no(node_ptr, offsets);
  744. }
  745. if (UNIV_LIKELY_NULL(heap)) {
  746. mem_heap_free(heap);
  747. }
  748. }
  749. /*==================== B-TREE INSERT =========================*/
  750. /*************************************************************//**
  751. Inserts a record if there is enough space, or if enough space can
  752. be freed by reorganizing. Differs from btr_cur_optimistic_insert because
  753. no heuristics is applied to whether it pays to use CPU time for
  754. reorganizing the page or not.
  755. @return pointer to inserted record if succeed, else NULL */
  756. static
  757. rec_t*
  758. btr_cur_insert_if_possible(
  759. /*=======================*/
  760. btr_cur_t* cursor, /*!< in: cursor on page after which to insert;
  761. cursor stays valid */
  762. const dtuple_t* tuple, /*!< in: tuple to insert; the size info need not
  763. have been stored to tuple */
  764. ulint n_ext, /*!< in: number of externally stored columns */
  765. mtr_t* mtr) /*!< in: mtr */
  766. {
  767. page_cur_t* page_cursor;
  768. buf_block_t* block;
  769. rec_t* rec;
  770. ut_ad(dtuple_check_typed(tuple));
  771. block = btr_cur_get_block(cursor);
  772. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  773. page_cursor = btr_cur_get_page_cur(cursor);
  774. /* Now, try the insert */
  775. rec = page_cur_tuple_insert(page_cursor, tuple,
  776. cursor->index, n_ext, mtr);
  777. if (UNIV_UNLIKELY(!rec)) {
  778. /* If record did not fit, reorganize */
  779. if (btr_page_reorganize(block, cursor->index, mtr)) {
  780. page_cur_search(block, cursor->index, tuple,
  781. PAGE_CUR_LE, page_cursor);
  782. rec = page_cur_tuple_insert(page_cursor, tuple,
  783. cursor->index, n_ext, mtr);
  784. }
  785. }
  786. return(rec);
  787. }
  788. /*************************************************************//**
  789. For an insert, checks the locks and does the undo logging if desired.
  790. @return DB_SUCCESS, DB_WAIT_LOCK, DB_FAIL, or error number */
  791. UNIV_INLINE
  792. ulint
  793. btr_cur_ins_lock_and_undo(
  794. /*======================*/
  795. ulint flags, /*!< in: undo logging and locking flags: if
  796. not zero, the parameters index and thr
  797. should be specified */
  798. btr_cur_t* cursor, /*!< in: cursor on page after which to insert */
  799. const dtuple_t* entry, /*!< in: entry to insert */
  800. que_thr_t* thr, /*!< in: query thread or NULL */
  801. mtr_t* mtr, /*!< in/out: mini-transaction */
  802. ibool* inherit)/*!< out: TRUE if the inserted new record maybe
  803. should inherit LOCK_GAP type locks from the
  804. successor record */
  805. {
  806. dict_index_t* index;
  807. ulint err;
  808. rec_t* rec;
  809. roll_ptr_t roll_ptr;
  810. /* Check if we have to wait for a lock: enqueue an explicit lock
  811. request if yes */
  812. rec = btr_cur_get_rec(cursor);
  813. index = cursor->index;
  814. err = lock_rec_insert_check_and_lock(flags, rec,
  815. btr_cur_get_block(cursor),
  816. index, thr, mtr, inherit);
  817. if (err != DB_SUCCESS) {
  818. return(err);
  819. }
  820. if (dict_index_is_clust(index) && !dict_index_is_ibuf(index)) {
  821. err = trx_undo_report_row_operation(flags, TRX_UNDO_INSERT_OP,
  822. thr, index, entry,
  823. NULL, 0, NULL,
  824. &roll_ptr);
  825. if (err != DB_SUCCESS) {
  826. return(err);
  827. }
  828. /* Now we can fill in the roll ptr field in entry */
  829. if (!(flags & BTR_KEEP_SYS_FLAG)) {
  830. row_upd_index_entry_sys_field(entry, index,
  831. DATA_ROLL_PTR, roll_ptr);
  832. }
  833. }
  834. return(DB_SUCCESS);
  835. }
  836. #ifdef UNIV_DEBUG
  837. /*************************************************************//**
  838. Report information about a transaction. */
  839. static
  840. void
  841. btr_cur_trx_report(
  842. /*===============*/
  843. trx_t* trx, /*!< in: transaction */
  844. const dict_index_t* index, /*!< in: index */
  845. const char* op) /*!< in: operation */
  846. {
  847. fprintf(stderr, "Trx with id " TRX_ID_FMT " going to ",
  848. TRX_ID_PREP_PRINTF(trx->id));
  849. fputs(op, stderr);
  850. dict_index_name_print(stderr, trx, index);
  851. putc('\n', stderr);
  852. }
  853. #endif /* UNIV_DEBUG */
  854. /*************************************************************//**
  855. Tries to perform an insert to a page in an index tree, next to cursor.
  856. It is assumed that mtr holds an x-latch on the page. The operation does
  857. not succeed if there is too little space on the page. If there is just
  858. one record on the page, the insert will always succeed; this is to
  859. prevent trying to split a page with just one record.
  860. @return DB_SUCCESS, DB_WAIT_LOCK, DB_FAIL, or error number */
  861. UNIV_INTERN
  862. ulint
  863. btr_cur_optimistic_insert(
  864. /*======================*/
  865. ulint flags, /*!< in: undo logging and locking flags: if not
  866. zero, the parameters index and thr should be
  867. specified */
  868. btr_cur_t* cursor, /*!< in: cursor on page after which to insert;
  869. cursor stays valid */
  870. dtuple_t* entry, /*!< in/out: entry to insert */
  871. rec_t** rec, /*!< out: pointer to inserted record if
  872. succeed */
  873. big_rec_t** big_rec,/*!< out: big rec vector whose fields have to
  874. be stored externally by the caller, or
  875. NULL */
  876. ulint n_ext, /*!< in: number of externally stored columns */
  877. que_thr_t* thr, /*!< in: query thread or NULL */
  878. mtr_t* mtr) /*!< in: mtr; if this function returns
  879. DB_SUCCESS on a leaf page of a secondary
  880. index in a compressed tablespace, the
  881. mtr must be committed before latching
  882. any further pages */
  883. {
  884. big_rec_t* big_rec_vec = NULL;
  885. dict_index_t* index;
  886. page_cur_t* page_cursor;
  887. buf_block_t* block;
  888. page_t* page;
  889. ulint max_size;
  890. rec_t* dummy_rec;
  891. ibool leaf;
  892. ibool reorg;
  893. ibool inherit;
  894. ulint zip_size;
  895. ulint rec_size;
  896. mem_heap_t* heap = NULL;
  897. ulint err;
  898. *big_rec = NULL;
  899. block = btr_cur_get_block(cursor);
  900. page = buf_block_get_frame(block);
  901. index = cursor->index;
  902. zip_size = buf_block_get_zip_size(block);
  903. #ifdef UNIV_DEBUG_VALGRIND
  904. if (zip_size) {
  905. UNIV_MEM_ASSERT_RW(page, UNIV_PAGE_SIZE);
  906. UNIV_MEM_ASSERT_RW(block->page.zip.data, zip_size);
  907. }
  908. #endif /* UNIV_DEBUG_VALGRIND */
  909. if (!dtuple_check_typed_no_assert(entry)) {
  910. fputs("InnoDB: Error in a tuple to insert into ", stderr);
  911. dict_index_name_print(stderr, thr_get_trx(thr), index);
  912. }
  913. #ifdef UNIV_DEBUG
  914. if (btr_cur_print_record_ops && thr) {
  915. btr_cur_trx_report(thr_get_trx(thr), index, "insert into ");
  916. dtuple_print(stderr, entry);
  917. }
  918. #endif /* UNIV_DEBUG */
  919. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  920. max_size = page_get_max_insert_size_after_reorganize(page, 1);
  921. leaf = page_is_leaf(page);
  922. /* Calculate the record size when entry is converted to a record */
  923. rec_size = rec_get_converted_size(index, entry, n_ext);
  924. if (page_zip_rec_needs_ext(rec_size, page_is_comp(page),
  925. dtuple_get_n_fields(entry), zip_size)) {
  926. /* The record is so big that we have to store some fields
  927. externally on separate database pages */
  928. big_rec_vec = dtuple_convert_big_rec(index, entry, &n_ext);
  929. if (UNIV_UNLIKELY(big_rec_vec == NULL)) {
  930. return(DB_TOO_BIG_RECORD);
  931. }
  932. rec_size = rec_get_converted_size(index, entry, n_ext);
  933. }
  934. if (UNIV_UNLIKELY(zip_size)) {
  935. /* Estimate the free space of an empty compressed page.
  936. Subtract one byte for the encoded heap_no in the
  937. modification log. */
  938. ulint free_space_zip = page_zip_empty_size(
  939. cursor->index->n_fields, zip_size) - 1;
  940. ulint n_uniq = dict_index_get_n_unique_in_tree(index);
  941. ut_ad(dict_table_is_comp(index->table));
  942. /* There should be enough room for two node pointer
  943. records on an empty non-leaf page. This prevents
  944. infinite page splits. */
  945. if (UNIV_LIKELY(entry->n_fields >= n_uniq)
  946. && UNIV_UNLIKELY(REC_NODE_PTR_SIZE
  947. + rec_get_converted_size_comp_prefix(
  948. index, entry->fields, n_uniq,
  949. NULL)
  950. /* On a compressed page, there is
  951. a two-byte entry in the dense
  952. page directory for every record.
  953. But there is no record header. */
  954. - (REC_N_NEW_EXTRA_BYTES - 2)
  955. > free_space_zip / 2)) {
  956. if (big_rec_vec) {
  957. dtuple_convert_back_big_rec(
  958. index, entry, big_rec_vec);
  959. }
  960. if (heap) {
  961. mem_heap_free(heap);
  962. }
  963. return(DB_TOO_BIG_RECORD);
  964. }
  965. }
  966. /* If there have been many consecutive inserts, and we are on the leaf
  967. level, check if we have to split the page to reserve enough free space
  968. for future updates of records. */
  969. if (dict_index_is_clust(index)
  970. && (page_get_n_recs(page) >= 2)
  971. && UNIV_LIKELY(leaf)
  972. && (dict_index_get_space_reserve() + rec_size > max_size)
  973. && (btr_page_get_split_rec_to_right(cursor, &dummy_rec)
  974. || btr_page_get_split_rec_to_left(cursor, &dummy_rec))) {
  975. fail:
  976. err = DB_FAIL;
  977. fail_err:
  978. if (big_rec_vec) {
  979. dtuple_convert_back_big_rec(index, entry, big_rec_vec);
  980. }
  981. if (UNIV_LIKELY_NULL(heap)) {
  982. mem_heap_free(heap);
  983. }
  984. return(err);
  985. }
  986. if (UNIV_UNLIKELY(max_size < BTR_CUR_PAGE_REORGANIZE_LIMIT
  987. || max_size < rec_size)
  988. && UNIV_LIKELY(page_get_n_recs(page) > 1)
  989. && page_get_max_insert_size(page, 1) < rec_size) {
  990. goto fail;
  991. }
  992. /* Check locks and write to the undo log, if specified */
  993. err = btr_cur_ins_lock_and_undo(flags, cursor, entry,
  994. thr, mtr, &inherit);
  995. if (UNIV_UNLIKELY(err != DB_SUCCESS)) {
  996. goto fail_err;
  997. }
  998. page_cursor = btr_cur_get_page_cur(cursor);
  999. /* Now, try the insert */
  1000. {
  1001. const rec_t* page_cursor_rec = page_cur_get_rec(page_cursor);
  1002. *rec = page_cur_tuple_insert(page_cursor, entry, index,
  1003. n_ext, mtr);
  1004. reorg = page_cursor_rec != page_cur_get_rec(page_cursor);
  1005. if (UNIV_UNLIKELY(reorg)) {
  1006. ut_a(zip_size);
  1007. ut_a(*rec);
  1008. }
  1009. }
  1010. if (UNIV_UNLIKELY(!*rec) && UNIV_LIKELY(!reorg)) {
  1011. /* If the record did not fit, reorganize */
  1012. if (UNIV_UNLIKELY(!btr_page_reorganize(block, index, mtr))) {
  1013. ut_a(zip_size);
  1014. goto fail;
  1015. }
  1016. ut_ad(zip_size
  1017. || page_get_max_insert_size(page, 1) == max_size);
  1018. reorg = TRUE;
  1019. page_cur_search(block, index, entry, PAGE_CUR_LE, page_cursor);
  1020. *rec = page_cur_tuple_insert(page_cursor, entry, index,
  1021. n_ext, mtr);
  1022. if (UNIV_UNLIKELY(!*rec)) {
  1023. if (UNIV_LIKELY(zip_size != 0)) {
  1024. goto fail;
  1025. }
  1026. fputs("InnoDB: Error: cannot insert tuple ", stderr);
  1027. dtuple_print(stderr, entry);
  1028. fputs(" into ", stderr);
  1029. dict_index_name_print(stderr, thr_get_trx(thr), index);
  1030. fprintf(stderr, "\nInnoDB: max insert size %lu\n",
  1031. (ulong) max_size);
  1032. ut_error;
  1033. }
  1034. }
  1035. if (UNIV_LIKELY_NULL(heap)) {
  1036. mem_heap_free(heap);
  1037. }
  1038. #ifdef BTR_CUR_HASH_ADAPT
  1039. if (!reorg && leaf && (cursor->flag == BTR_CUR_HASH)) {
  1040. btr_search_update_hash_node_on_insert(cursor);
  1041. } else {
  1042. btr_search_update_hash_on_insert(cursor);
  1043. }
  1044. #endif
  1045. if (!(flags & BTR_NO_LOCKING_FLAG) && inherit) {
  1046. lock_update_insert(block, *rec);
  1047. }
  1048. #if 0
  1049. fprintf(stderr, "Insert into page %lu, max ins size %lu,"
  1050. " rec %lu ind type %lu\n",
  1051. buf_block_get_page_no(block), max_size,
  1052. rec_size + PAGE_DIR_SLOT_SIZE, index->type);
  1053. #endif
  1054. if (leaf && !dict_index_is_clust(index)) {
  1055. /* Update the free bits of the B-tree page in the
  1056. insert buffer bitmap. */
  1057. /* The free bits in the insert buffer bitmap must
  1058. never exceed the free space on a page. It is safe to
  1059. decrement or reset the bits in the bitmap in a
  1060. mini-transaction that is committed before the
  1061. mini-transaction that affects the free space. */
  1062. /* It is unsafe to increment the bits in a separately
  1063. committed mini-transaction, because in crash recovery,
  1064. the free bits could momentarily be set too high. */
  1065. if (zip_size) {
  1066. /* Update the bits in the same mini-transaction. */
  1067. ibuf_update_free_bits_zip(block, mtr);
  1068. } else {
  1069. /* Decrement the bits in a separate
  1070. mini-transaction. */
  1071. ibuf_update_free_bits_if_full(
  1072. block, max_size,
  1073. rec_size + PAGE_DIR_SLOT_SIZE);
  1074. }
  1075. }
  1076. *big_rec = big_rec_vec;
  1077. return(DB_SUCCESS);
  1078. }
  1079. /*************************************************************//**
  1080. Performs an insert on a page of an index tree. It is assumed that mtr
  1081. holds an x-latch on the tree and on the cursor page. If the insert is
  1082. made on the leaf level, to avoid deadlocks, mtr must also own x-latches
  1083. to brothers of page, if those brothers exist.
  1084. @return DB_SUCCESS or error number */
  1085. UNIV_INTERN
  1086. ulint
  1087. btr_cur_pessimistic_insert(
  1088. /*=======================*/
  1089. ulint flags, /*!< in: undo logging and locking flags: if not
  1090. zero, the parameter thr should be
  1091. specified; if no undo logging is specified,
  1092. then the caller must have reserved enough
  1093. free extents in the file space so that the
  1094. insertion will certainly succeed */
  1095. btr_cur_t* cursor, /*!< in: cursor after which to insert;
  1096. cursor stays valid */
  1097. dtuple_t* entry, /*!< in/out: entry to insert */
  1098. rec_t** rec, /*!< out: pointer to inserted record if
  1099. succeed */
  1100. big_rec_t** big_rec,/*!< out: big rec vector whose fields have to
  1101. be stored externally by the caller, or
  1102. NULL */
  1103. ulint n_ext, /*!< in: number of externally stored columns */
  1104. que_thr_t* thr, /*!< in: query thread or NULL */
  1105. mtr_t* mtr) /*!< in: mtr */
  1106. {
  1107. dict_index_t* index = cursor->index;
  1108. ulint zip_size = dict_table_zip_size(index->table);
  1109. big_rec_t* big_rec_vec = NULL;
  1110. mem_heap_t* heap = NULL;
  1111. ulint err;
  1112. ibool dummy_inh;
  1113. ibool success;
  1114. ulint n_extents = 0;
  1115. ulint n_reserved;
  1116. ut_ad(dtuple_check_typed(entry));
  1117. *big_rec = NULL;
  1118. ut_ad(mtr_memo_contains(mtr,
  1119. dict_index_get_lock(btr_cur_get_index(cursor)),
  1120. MTR_MEMO_X_LOCK));
  1121. ut_ad(mtr_memo_contains(mtr, btr_cur_get_block(cursor),
  1122. MTR_MEMO_PAGE_X_FIX));
  1123. /* Try first an optimistic insert; reset the cursor flag: we do not
  1124. assume anything of how it was positioned */
  1125. cursor->flag = BTR_CUR_BINARY;
  1126. err = btr_cur_optimistic_insert(flags, cursor, entry, rec,
  1127. big_rec, n_ext, thr, mtr);
  1128. if (err != DB_FAIL) {
  1129. return(err);
  1130. }
  1131. /* Retry with a pessimistic insert. Check locks and write to undo log,
  1132. if specified */
  1133. err = btr_cur_ins_lock_and_undo(flags, cursor, entry,
  1134. thr, mtr, &dummy_inh);
  1135. if (err != DB_SUCCESS) {
  1136. return(err);
  1137. }
  1138. if (!(flags & BTR_NO_UNDO_LOG_FLAG)) {
  1139. /* First reserve enough free space for the file segments
  1140. of the index tree, so that the insert will not fail because
  1141. of lack of space */
  1142. n_extents = cursor->tree_height / 16 + 3;
  1143. success = fsp_reserve_free_extents(&n_reserved, index->space,
  1144. n_extents, FSP_NORMAL, mtr);
  1145. if (!success) {
  1146. return(DB_OUT_OF_FILE_SPACE);
  1147. }
  1148. }
  1149. if (page_zip_rec_needs_ext(rec_get_converted_size(index, entry, n_ext),
  1150. dict_table_is_comp(index->table),
  1151. dict_index_get_n_fields(index),
  1152. zip_size)) {
  1153. /* The record is so big that we have to store some fields
  1154. externally on separate database pages */
  1155. if (UNIV_LIKELY_NULL(big_rec_vec)) {
  1156. /* This should never happen, but we handle
  1157. the situation in a robust manner. */
  1158. ut_ad(0);
  1159. dtuple_convert_back_big_rec(index, entry, big_rec_vec);
  1160. }
  1161. big_rec_vec = dtuple_convert_big_rec(index, entry, &n_ext);
  1162. if (big_rec_vec == NULL) {
  1163. if (n_extents > 0) {
  1164. fil_space_release_free_extents(index->space,
  1165. n_reserved);
  1166. }
  1167. return(DB_TOO_BIG_RECORD);
  1168. }
  1169. }
  1170. if (dict_index_get_page(index)
  1171. == buf_block_get_page_no(btr_cur_get_block(cursor))) {
  1172. /* The page is the root page */
  1173. *rec = btr_root_raise_and_insert(cursor, entry, n_ext, mtr);
  1174. } else {
  1175. *rec = btr_page_split_and_insert(cursor, entry, n_ext, mtr);
  1176. }
  1177. if (UNIV_LIKELY_NULL(heap)) {
  1178. mem_heap_free(heap);
  1179. }
  1180. ut_ad(page_rec_get_next(btr_cur_get_rec(cursor)) == *rec);
  1181. #ifdef BTR_CUR_ADAPT
  1182. btr_search_update_hash_on_insert(cursor);
  1183. #endif
  1184. if (!(flags & BTR_NO_LOCKING_FLAG)) {
  1185. lock_update_insert(btr_cur_get_block(cursor), *rec);
  1186. }
  1187. if (n_extents > 0) {
  1188. fil_space_release_free_extents(index->space, n_reserved);
  1189. }
  1190. *big_rec = big_rec_vec;
  1191. return(DB_SUCCESS);
  1192. }
  1193. /*==================== B-TREE UPDATE =========================*/
  1194. /*************************************************************//**
  1195. For an update, checks the locks and does the undo logging.
  1196. @return DB_SUCCESS, DB_WAIT_LOCK, or error number */
  1197. UNIV_INLINE
  1198. ulint
  1199. btr_cur_upd_lock_and_undo(
  1200. /*======================*/
  1201. ulint flags, /*!< in: undo logging and locking flags */
  1202. btr_cur_t* cursor, /*!< in: cursor on record to update */
  1203. const upd_t* update, /*!< in: update vector */
  1204. ulint cmpl_info,/*!< in: compiler info on secondary index
  1205. updates */
  1206. que_thr_t* thr, /*!< in: query thread */
  1207. mtr_t* mtr, /*!< in/out: mini-transaction */
  1208. roll_ptr_t* roll_ptr)/*!< out: roll pointer */
  1209. {
  1210. dict_index_t* index;
  1211. rec_t* rec;
  1212. ulint err;
  1213. ut_ad(cursor && update && thr && roll_ptr);
  1214. rec = btr_cur_get_rec(cursor);
  1215. index = cursor->index;
  1216. if (!dict_index_is_clust(index)) {
  1217. /* We do undo logging only when we update a clustered index
  1218. record */
  1219. return(lock_sec_rec_modify_check_and_lock(
  1220. flags, btr_cur_get_block(cursor), rec,
  1221. index, thr, mtr));
  1222. }
  1223. /* Check if we have to wait for a lock: enqueue an explicit lock
  1224. request if yes */
  1225. err = DB_SUCCESS;
  1226. if (!(flags & BTR_NO_LOCKING_FLAG)) {
  1227. mem_heap_t* heap = NULL;
  1228. ulint offsets_[REC_OFFS_NORMAL_SIZE];
  1229. rec_offs_init(offsets_);
  1230. err = lock_clust_rec_modify_check_and_lock(
  1231. flags, btr_cur_get_block(cursor), rec, index,
  1232. rec_get_offsets(rec, index, offsets_,
  1233. ULINT_UNDEFINED, &heap), thr);
  1234. if (UNIV_LIKELY_NULL(heap)) {
  1235. mem_heap_free(heap);
  1236. }
  1237. if (err != DB_SUCCESS) {
  1238. return(err);
  1239. }
  1240. }
  1241. /* Append the info about the update in the undo log */
  1242. err = trx_undo_report_row_operation(flags, TRX_UNDO_MODIFY_OP, thr,
  1243. index, NULL, update,
  1244. cmpl_info, rec, roll_ptr);
  1245. return(err);
  1246. }
  1247. /***********************************************************//**
  1248. Writes a redo log record of updating a record in-place. */
  1249. UNIV_INLINE
  1250. void
  1251. btr_cur_update_in_place_log(
  1252. /*========================*/
  1253. ulint flags, /*!< in: flags */
  1254. rec_t* rec, /*!< in: record */
  1255. dict_index_t* index, /*!< in: index where cursor positioned */
  1256. const upd_t* update, /*!< in: update vector */
  1257. trx_t* trx, /*!< in: transaction */
  1258. roll_ptr_t roll_ptr, /*!< in: roll ptr */
  1259. mtr_t* mtr) /*!< in: mtr */
  1260. {
  1261. byte* log_ptr;
  1262. page_t* page = page_align(rec);
  1263. ut_ad(flags < 256);
  1264. ut_ad(!!page_is_comp(page) == dict_table_is_comp(index->table));
  1265. log_ptr = mlog_open_and_write_index(mtr, rec, index, page_is_comp(page)
  1266. ? MLOG_COMP_REC_UPDATE_IN_PLACE
  1267. : MLOG_REC_UPDATE_IN_PLACE,
  1268. 1 + DATA_ROLL_PTR_LEN + 14 + 2
  1269. + MLOG_BUF_MARGIN);
  1270. if (!log_ptr) {
  1271. /* Logging in mtr is switched off during crash recovery */
  1272. return;
  1273. }
  1274. /* The code below assumes index is a clustered index: change index to
  1275. the clustered index if we are updating a secondary index record (or we
  1276. could as well skip writing the sys col values to the log in this case
  1277. because they are not needed for a secondary index record update) */
  1278. index = dict_table_get_first_index(index->table);
  1279. mach_write_to_1(log_ptr, flags);
  1280. log_ptr++;
  1281. log_ptr = row_upd_write_sys_vals_to_log(index, trx, roll_ptr, log_ptr,
  1282. mtr);
  1283. mach_write_to_2(log_ptr, page_offset(rec));
  1284. log_ptr += 2;
  1285. row_upd_index_write_log(update, log_ptr, mtr);
  1286. }
  1287. #endif /* UNIV_HOTBACKUP */
  1288. /***********************************************************//**
  1289. Parses a redo log record of updating a record in-place.
  1290. @return end of log record or NULL */
  1291. UNIV_INTERN
  1292. byte*
  1293. btr_cur_parse_update_in_place(
  1294. /*==========================*/
  1295. byte* ptr, /*!< in: buffer */
  1296. byte* end_ptr,/*!< in: buffer end */
  1297. page_t* page, /*!< in/out: page or NULL */
  1298. page_zip_des_t* page_zip,/*!< in/out: compressed page, or NULL */
  1299. dict_index_t* index) /*!< in: index corresponding to page */
  1300. {
  1301. ulint flags;
  1302. rec_t* rec;
  1303. upd_t* update;
  1304. ulint pos;
  1305. trx_id_t trx_id;
  1306. roll_ptr_t roll_ptr;
  1307. ulint rec_offset;
  1308. mem_heap_t* heap;
  1309. ulint* offsets;
  1310. if (end_ptr < ptr + 1) {
  1311. return(NULL);
  1312. }
  1313. flags = mach_read_from_1(ptr);
  1314. ptr++;
  1315. ptr = row_upd_parse_sys_vals(ptr, end_ptr, &pos, &trx_id, &roll_ptr);
  1316. if (ptr == NULL) {
  1317. return(NULL);
  1318. }
  1319. if (end_ptr < ptr + 2) {
  1320. return(NULL);
  1321. }
  1322. rec_offset = mach_read_from_2(ptr);
  1323. ptr += 2;
  1324. ut_a(rec_offset <= UNIV_PAGE_SIZE);
  1325. heap = mem_heap_create(256);
  1326. ptr = row_upd_index_parse(ptr, end_ptr, heap, &update);
  1327. if (!ptr || !page) {
  1328. goto func_exit;
  1329. }
  1330. ut_a((ibool)!!page_is_comp(page) == dict_table_is_comp(index->table));
  1331. rec = page + rec_offset;
  1332. /* We do not need to reserve btr_search_latch, as the page is only
  1333. being recovered, and there cannot be a hash index to it. */
  1334. offsets = rec_get_offsets(rec, index, NULL, ULINT_UNDEFINED, &heap);
  1335. if (!(flags & BTR_KEEP_SYS_FLAG)) {
  1336. row_upd_rec_sys_fields_in_recovery(rec, page_zip, offsets,
  1337. pos, trx_id, roll_ptr);
  1338. }
  1339. row_upd_rec_in_place(rec, index, offsets, update, page_zip);
  1340. func_exit:
  1341. mem_heap_free(heap);
  1342. return(ptr);
  1343. }
  1344. #ifndef UNIV_HOTBACKUP
  1345. /*************************************************************//**
  1346. See if there is enough place in the page modification log to log
  1347. an update-in-place.
  1348. @return TRUE if enough place */
  1349. static
  1350. ibool
  1351. btr_cur_update_alloc_zip(
  1352. /*=====================*/
  1353. page_zip_des_t* page_zip,/*!< in/out: compressed page */
  1354. buf_block_t* block, /*!< in/out: buffer page */
  1355. dict_index_t* index, /*!< in: the index corresponding to the block */
  1356. ulint length, /*!< in: size needed */
  1357. ibool create, /*!< in: TRUE=delete-and-insert,
  1358. FALSE=update-in-place */
  1359. mtr_t* mtr) /*!< in: mini-transaction */
  1360. {
  1361. ut_a(page_zip == buf_block_get_page_zip(block));
  1362. ut_ad(page_zip);
  1363. ut_ad(!dict_index_is_ibuf(index));
  1364. if (page_zip_available(page_zip, dict_index_is_clust(index),
  1365. length, create)) {
  1366. return(TRUE);
  1367. }
  1368. if (!page_zip->m_nonempty) {
  1369. /* The page has been freshly compressed, so
  1370. recompressing it will not help. */
  1371. return(FALSE);
  1372. }
  1373. if (!page_zip_compress(page_zip, buf_block_get_frame(block),
  1374. index, mtr)) {
  1375. /* Unable to compress the page */
  1376. return(FALSE);
  1377. }
  1378. /* After recompressing a page, we must make sure that the free
  1379. bits in the insert buffer bitmap will not exceed the free
  1380. space on the page. Because this function will not attempt
  1381. recompression unless page_zip_available() fails above, it is
  1382. safe to reset the free bits if page_zip_available() fails
  1383. again, below. The free bits can safely be reset in a separate
  1384. mini-transaction. If page_zip_available() succeeds below, we
  1385. can be sure that the page_zip_compress() above did not reduce
  1386. the free space available on the page. */
  1387. if (!page_zip_available(page_zip, dict_index_is_clust(index),
  1388. length, create)) {
  1389. /* Out of space: reset the free bits. */
  1390. if (!dict_index_is_clust(index)
  1391. && page_is_leaf(buf_block_get_frame(block))) {
  1392. ibuf_reset_free_bits(block);
  1393. }
  1394. return(FALSE);
  1395. }
  1396. return(TRUE);
  1397. }
  1398. /*************************************************************//**
  1399. Updates a record when the update causes no size changes in its fields.
  1400. We assume here that the ordering fields of the record do not change.
  1401. @return DB_SUCCESS or error number */
  1402. UNIV_INTERN
  1403. ulint
  1404. btr_cur_update_in_place(
  1405. /*====================*/
  1406. ulint flags, /*!< in: undo logging and locking flags */
  1407. btr_cur_t* cursor, /*!< in: cursor on the record to update;
  1408. cursor stays valid and positioned on the
  1409. same record */
  1410. const upd_t* update, /*!< in: update vector */
  1411. ulint cmpl_info,/*!< in: compiler info on secondary index
  1412. updates */
  1413. que_thr_t* thr, /*!< in: query thread */
  1414. mtr_t* mtr) /*!< in: mtr; must be committed before
  1415. latching any further pages */
  1416. {
  1417. dict_index_t* index;
  1418. buf_block_t* block;
  1419. page_zip_des_t* page_zip;
  1420. ulint err;
  1421. rec_t* rec;
  1422. roll_ptr_t roll_ptr = ut_dulint_zero;
  1423. trx_t* trx;
  1424. ulint was_delete_marked;
  1425. mem_heap_t* heap = NULL;
  1426. ulint offsets_[REC_OFFS_NORMAL_SIZE];
  1427. ulint* offsets = offsets_;
  1428. rec_offs_init(offsets_);
  1429. rec = btr_cur_get_rec(cursor);
  1430. index = cursor->index;
  1431. ut_ad(!!page_rec_is_comp(rec) == dict_table_is_comp(index->table));
  1432. /* The insert buffer tree should never be updated in place. */
  1433. ut_ad(!dict_index_is_ibuf(index));
  1434. trx = thr_get_trx(thr);
  1435. offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, &heap);
  1436. #ifdef UNIV_DEBUG
  1437. if (btr_cur_print_record_ops && thr) {
  1438. btr_cur_trx_report(trx, index, "update ");
  1439. rec_print_new(stderr, rec, offsets);
  1440. }
  1441. #endif /* UNIV_DEBUG */
  1442. block = btr_cur_get_block(cursor);
  1443. page_zip = buf_block_get_page_zip(block);
  1444. /* Check that enough space is available on the compressed page. */
  1445. if (UNIV_LIKELY_NULL(page_zip)
  1446. && !btr_cur_update_alloc_zip(page_zip, block, index,
  1447. rec_offs_size(offsets), FALSE, mtr)) {
  1448. return(DB_ZIP_OVERFLOW);
  1449. }
  1450. /* Do lock checking and undo logging */
  1451. err = btr_cur_upd_lock_and_undo(flags, cursor, update, cmpl_info,
  1452. thr, mtr, &roll_ptr);
  1453. if (UNIV_UNLIKELY(err != DB_SUCCESS)) {
  1454. if (UNIV_LIKELY_NULL(heap)) {
  1455. mem_heap_free(heap);
  1456. }
  1457. return(err);
  1458. }
  1459. if (block->is_hashed) {
  1460. /* The function row_upd_changes_ord_field_binary works only
  1461. if the update vector was built for a clustered index, we must
  1462. NOT call it if index is secondary */
  1463. if (!dict_index_is_clust(index)
  1464. || row_upd_changes_ord_field_binary(NULL, index, update)) {
  1465. /* Remove possible hash index pointer to this record */
  1466. btr_search_update_hash_on_delete(cursor);
  1467. }
  1468. rw_lock_x_lock(&btr_search_latch);
  1469. }
  1470. if (!(flags & BTR_KEEP_SYS_FLAG)) {
  1471. row_upd_rec_sys_fields(rec, NULL,
  1472. index, offsets, trx, roll_ptr);
  1473. }
  1474. was_delete_marked = rec_get_deleted_flag(
  1475. rec, page_is_comp(buf_block_get_frame(block)));
  1476. row_upd_rec_in_place(rec, index, offsets, update, page_zip);
  1477. if (block->is_hashed) {
  1478. rw_lock_x_unlock(&btr_search_latch);
  1479. }
  1480. if (page_zip && !dict_index_is_clust(index)
  1481. && page_is_leaf(buf_block_get_frame(block))) {
  1482. /* Update the free bits in the insert buffer. */
  1483. ibuf_update_free_bits_zip(block, mtr);
  1484. }
  1485. btr_cur_update_in_place_log(flags, rec, index, update,
  1486. trx, roll_ptr, mtr);
  1487. if (was_delete_marked
  1488. && !rec_get_deleted_flag(rec, page_is_comp(
  1489. buf_block_get_frame(block)))) {
  1490. /* The new updated record owns its possible externally
  1491. stored fields */
  1492. btr_cur_unmark_extern_fields(page_zip,
  1493. rec, index, offsets, mtr);
  1494. }
  1495. if (UNIV_LIKELY_NULL(heap)) {
  1496. mem_heap_free(heap);
  1497. }
  1498. return(DB_SUCCESS);
  1499. }
  1500. /*************************************************************//**
  1501. Tries to update a record on a page in an index tree. It is assumed that mtr
  1502. holds an x-latch on the page. The operation does not succeed if there is too
  1503. little space on the page or if the update would result in too empty a page,
  1504. so that tree compression is recommended. We assume here that the ordering
  1505. fields of the record do not change.
  1506. @return DB_SUCCESS, or DB_OVERFLOW if the updated record does not fit,
  1507. DB_UNDERFLOW if the page would become too empty, or DB_ZIP_OVERFLOW if
  1508. there is not enough space left on the compressed page */
  1509. UNIV_INTERN
  1510. ulint
  1511. btr_cur_optimistic_update(
  1512. /*======================*/
  1513. ulint flags, /*!< in: undo logging and locking flags */
  1514. btr_cur_t* cursor, /*!< in: cursor on the record to update;
  1515. cursor stays valid and positioned on the
  1516. same record */
  1517. const upd_t* update, /*!< in: update vector; this must also
  1518. contain trx id and roll ptr fields */
  1519. ulint cmpl_info,/*!< in: compiler info on secondary index
  1520. updates */
  1521. que_thr_t* thr, /*!< in: query thread */
  1522. mtr_t* mtr) /*!< in: mtr; must be committed before
  1523. latching any further pages */
  1524. {
  1525. dict_index_t* index;
  1526. page_cur_t* page_cursor;
  1527. ulint err;
  1528. buf_block_t* block;
  1529. page_t* page;
  1530. page_zip_des_t* page_zip;
  1531. rec_t* rec;
  1532. rec_t* orig_rec;
  1533. ulint max_size;
  1534. ulint new_rec_size;
  1535. ulint old_rec_size;
  1536. dtuple_t* new_entry;
  1537. roll_ptr_t roll_ptr;
  1538. trx_t* trx;
  1539. mem_heap_t* heap;
  1540. ulint i;
  1541. ulint n_ext;
  1542. ulint* offsets;
  1543. block = btr_cur_get_block(cursor);
  1544. page = buf_block_get_frame(block);
  1545. orig_rec = rec = btr_cur_get_rec(cursor);
  1546. index = cursor->index;
  1547. ut_ad(!!page_rec_is_comp(rec) == dict_table_is_comp(index->table));
  1548. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  1549. /* The insert buffer tree should never be updated in place. */
  1550. ut_ad(!dict_index_is_ibuf(index));
  1551. heap = mem_heap_create(1024);
  1552. offsets = rec_get_offsets(rec, index, NULL, ULINT_UNDEFINED, &heap);
  1553. #ifdef UNIV_DEBUG
  1554. if (btr_cur_print_record_ops && thr) {
  1555. btr_cur_trx_report(thr_get_trx(thr), index, "update ");
  1556. rec_print_new(stderr, rec, offsets);
  1557. }
  1558. #endif /* UNIV_DEBUG */
  1559. if (!row_upd_changes_field_size_or_external(index, offsets, update)) {
  1560. /* The simplest and the most common case: the update does not
  1561. change the size of any field and none of the updated fields is
  1562. externally stored in rec or update, and there is enough space
  1563. on the compressed page to log the update. */
  1564. mem_heap_free(heap);
  1565. return(btr_cur_update_in_place(flags, cursor, update,
  1566. cmpl_info, thr, mtr));
  1567. }
  1568. if (rec_offs_any_extern(offsets)) {
  1569. any_extern:
  1570. /* Externally stored fields are treated in pessimistic
  1571. update */
  1572. mem_heap_free(heap);
  1573. return(DB_OVERFLOW);
  1574. }
  1575. for (i = 0; i < upd_get_n_fields(update); i++) {
  1576. if (dfield_is_ext(&upd_get_nth_field(update, i)->new_val)) {
  1577. goto any_extern;
  1578. }
  1579. }
  1580. page_cursor = btr_cur_get_page_cur(cursor);
  1581. new_entry = row_rec_to_index_entry(ROW_COPY_DATA, rec, index, offsets,
  1582. &n_ext, heap);
  1583. /* We checked above that there are no externally stored fields. */
  1584. ut_a(!n_ext);
  1585. /* The page containing the clustered index record
  1586. corresponding to new_entry is latched in mtr.
  1587. Thus the following call is safe. */
  1588. row_upd_index_replace_new_col_vals_index_pos(new_entry, index, update,
  1589. FALSE, heap);
  1590. old_rec_size = rec_offs_size(offsets);
  1591. new_rec_size = rec_get_converted_size(index, new_entry, 0);
  1592. page_zip = buf_block_get_page_zip(block);
  1593. #ifdef UNIV_ZIP_DEBUG
  1594. ut_a(!page_zip || page_zip_validate(page_zip, page));
  1595. #endif /* UNIV_ZIP_DEBUG */
  1596. if (UNIV_LIKELY_NULL(page_zip)
  1597. && !btr_cur_update_alloc_zip(page_zip, block, index,
  1598. new_rec_size, TRUE, mtr)) {
  1599. err = DB_ZIP_OVERFLOW;
  1600. goto err_exit;
  1601. }
  1602. if (UNIV_UNLIKELY(new_rec_size
  1603. >= (page_get_free_space_of_empty(page_is_comp(page))
  1604. / 2))) {
  1605. err = DB_OVERFLOW;
  1606. goto err_exit;
  1607. }
  1608. if (UNIV_UNLIKELY(page_get_data_size(page)
  1609. - old_rec_size + new_rec_size
  1610. < BTR_CUR_PAGE_COMPRESS_LIMIT)) {
  1611. /* The page would become too empty */
  1612. err = DB_UNDERFLOW;
  1613. goto err_exit;
  1614. }
  1615. max_size = old_rec_size
  1616. + page_get_max_insert_size_after_reorganize(page, 1);
  1617. if (!(((max_size >= BTR_CUR_PAGE_REORGANIZE_LIMIT)
  1618. && (max_size >= new_rec_size))
  1619. || (page_get_n_recs(page) <= 1))) {
  1620. /* There was not enough space, or it did not pay to
  1621. reorganize: for simplicity, we decide what to do assuming a
  1622. reorganization is needed, though it might not be necessary */
  1623. err = DB_OVERFLOW;
  1624. goto err_exit;
  1625. }
  1626. /* Do lock checking and undo logging */
  1627. err = btr_cur_upd_lock_and_undo(flags, cursor, update, cmpl_info,
  1628. thr, mtr, &roll_ptr);
  1629. if (err != DB_SUCCESS) {
  1630. err_exit:
  1631. mem_heap_free(heap);
  1632. return(err);
  1633. }
  1634. /* Ok, we may do the replacement. Store on the page infimum the
  1635. explicit locks on rec, before deleting rec (see the comment in
  1636. btr_cur_pessimistic_update). */
  1637. lock_rec_store_on_page_infimum(block, rec);
  1638. btr_search_update_hash_on_delete(cursor);
  1639. /* The call to row_rec_to_index_entry(ROW_COPY_DATA, ...) above
  1640. invokes rec_offs_make_valid() to point to the copied record that
  1641. the fields of new_entry point to. We have to undo it here. */
  1642. ut_ad(rec_offs_validate(NULL, index, offsets));
  1643. rec_offs_make_valid(page_cur_get_rec(page_cursor), index, offsets);
  1644. page_cur_delete_rec(page_cursor, index, offsets, mtr);
  1645. page_cur_move_to_prev(page_cursor);
  1646. trx = thr_get_trx(thr);
  1647. if (!(flags & BTR_KEEP_SYS_FLAG)) {
  1648. row_upd_index_entry_sys_field(new_entry, index, DATA_ROLL_PTR,
  1649. roll_ptr);
  1650. row_upd_index_entry_sys_field(new_entry, index, DATA_TRX_ID,
  1651. trx->id);
  1652. }
  1653. /* There are no externally stored columns in new_entry */
  1654. rec = btr_cur_insert_if_possible(cursor, new_entry, 0/*n_ext*/, mtr);
  1655. ut_a(rec); /* <- We calculated above the insert would fit */
  1656. if (page_zip && !dict_index_is_clust(index)
  1657. && page_is_leaf(page)) {
  1658. /* Update the free bits in the insert buffer. */
  1659. ibuf_update_free_bits_zip(block, mtr);
  1660. }
  1661. /* Restore the old explicit lock state on the record */
  1662. lock_rec_restore_from_page_infimum(block, rec, block);
  1663. page_cur_move_to_next(page_cursor);
  1664. mem_heap_free(heap);
  1665. return(DB_SUCCESS);
  1666. }
  1667. /*************************************************************//**
  1668. If, in a split, a new supremum record was created as the predecessor of the
  1669. updated record, the supremum record must inherit exactly the locks on the
  1670. updated record. In the split it may have inherited locks from the successor
  1671. of the updated record, which is not correct. This function restores the
  1672. right locks for the new supremum. */
  1673. static
  1674. void
  1675. btr_cur_pess_upd_restore_supremum(
  1676. /*==============================*/
  1677. buf_block_t* block, /*!< in: buffer block of rec */
  1678. const rec_t* rec, /*!< in: updated record */
  1679. mtr_t* mtr) /*!< in: mtr */
  1680. {
  1681. page_t* page;
  1682. buf_block_t* prev_block;
  1683. ulint space;
  1684. ulint zip_size;
  1685. ulint prev_page_no;
  1686. page = buf_block_get_frame(block);
  1687. if (page_rec_get_next(page_get_infimum_rec(page)) != rec) {
  1688. /* Updated record is not the first user record on its page */
  1689. return;
  1690. }
  1691. space = buf_block_get_space(block);
  1692. zip_size = buf_block_get_zip_size(block);
  1693. prev_page_no = btr_page_get_prev(page, mtr);
  1694. ut_ad(prev_page_no != FIL_NULL);
  1695. prev_block = buf_page_get_with_no_latch(space, zip_size,
  1696. prev_page_no, mtr);
  1697. #ifdef UNIV_BTR_DEBUG
  1698. ut_a(btr_page_get_next(prev_block->frame, mtr)
  1699. == page_get_page_no(page));
  1700. #endif /* UNIV_BTR_DEBUG */
  1701. /* We must already have an x-latch on prev_block! */
  1702. ut_ad(mtr_memo_contains(mtr, prev_block, MTR_MEMO_PAGE_X_FIX));
  1703. lock_rec_reset_and_inherit_gap_locks(prev_block, block,
  1704. PAGE_HEAP_NO_SUPREMUM,
  1705. page_rec_get_heap_no(rec));
  1706. }
  1707. /*************************************************************//**
  1708. Performs an update of a record on a page of a tree. It is assumed
  1709. that mtr holds an x-latch on the tree and on the cursor page. If the
  1710. update is made on the leaf level, to avoid deadlocks, mtr must also
  1711. own x-latches to brothers of page, if those brothers exist. We assume
  1712. here that the ordering fields of the record do not change.
  1713. @return DB_SUCCESS or error code */
  1714. UNIV_INTERN
  1715. ulint
  1716. btr_cur_pessimistic_update(
  1717. /*=======================*/
  1718. ulint flags, /*!< in: undo logging, locking, and rollback
  1719. flags */
  1720. btr_cur_t* cursor, /*!< in: cursor on the record to update */
  1721. mem_heap_t** heap, /*!< in/out: pointer to memory heap, or NULL */
  1722. big_rec_t** big_rec,/*!< out: big rec vector whose fields have to
  1723. be stored externally by the caller, or NULL */
  1724. const upd_t* update, /*!< in: update vector; this is allowed also
  1725. contain trx id and roll ptr fields, but
  1726. the values in update vector have no effect */
  1727. ulint cmpl_info,/*!< in: compiler info on secondary index
  1728. updates */
  1729. que_thr_t* thr, /*!< in: query thread */
  1730. mtr_t* mtr) /*!< in: mtr; must be committed before
  1731. latching any further pages */
  1732. {
  1733. big_rec_t* big_rec_vec = NULL;
  1734. big_rec_t* dummy_big_rec;
  1735. dict_index_t* index;
  1736. buf_block_t* block;
  1737. page_t* page;
  1738. page_zip_des_t* page_zip;
  1739. rec_t* rec;
  1740. page_cur_t* page_cursor;
  1741. dtuple_t* new_entry;
  1742. ulint err;
  1743. ulint optim_err;
  1744. roll_ptr_t roll_ptr;
  1745. trx_t* trx;
  1746. ibool was_first;
  1747. ulint n_extents = 0;
  1748. ulint n_reserved;
  1749. ulint n_ext;
  1750. ulint* offsets = NULL;
  1751. *big_rec = NULL;
  1752. block = btr_cur_get_block(cursor);
  1753. page = buf_block_get_frame(block);
  1754. page_zip = buf_block_get_page_zip(block);
  1755. rec = btr_cur_get_rec(cursor);
  1756. index = cursor->index;
  1757. ut_ad(mtr_memo_contains(mtr, dict_index_get_lock(index),
  1758. MTR_MEMO_X_LOCK));
  1759. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  1760. #ifdef UNIV_ZIP_DEBUG
  1761. ut_a(!page_zip || page_zip_validate(page_zip, page));
  1762. #endif /* UNIV_ZIP_DEBUG */
  1763. /* The insert buffer tree should never be updated in place. */
  1764. ut_ad(!dict_index_is_ibuf(index));
  1765. optim_err = btr_cur_optimistic_update(flags, cursor, update,
  1766. cmpl_info, thr, mtr);
  1767. switch (optim_err) {
  1768. case DB_UNDERFLOW:
  1769. case DB_OVERFLOW:
  1770. case DB_ZIP_OVERFLOW:
  1771. break;
  1772. default:
  1773. return(optim_err);
  1774. }
  1775. /* Do lock checking and undo logging */
  1776. err = btr_cur_upd_lock_and_undo(flags, cursor, update, cmpl_info,
  1777. thr, mtr, &roll_ptr);
  1778. if (err != DB_SUCCESS) {
  1779. return(err);
  1780. }
  1781. if (optim_err == DB_OVERFLOW) {
  1782. ulint reserve_flag;
  1783. /* First reserve enough free space for the file segments
  1784. of the index tree, so that the update will not fail because
  1785. of lack of space */
  1786. n_extents = cursor->tree_height / 16 + 3;
  1787. if (flags & BTR_NO_UNDO_LOG_FLAG) {
  1788. reserve_flag = FSP_CLEANING;
  1789. } else {
  1790. reserve_flag = FSP_NORMAL;
  1791. }
  1792. if (!fsp_reserve_free_extents(&n_reserved, index->space,
  1793. n_extents, reserve_flag, mtr)) {
  1794. return(DB_OUT_OF_FILE_SPACE);
  1795. }
  1796. }
  1797. if (!*heap) {
  1798. *heap = mem_heap_create(1024);
  1799. }
  1800. offsets = rec_get_offsets(rec, index, NULL, ULINT_UNDEFINED, heap);
  1801. trx = thr_get_trx(thr);
  1802. new_entry = row_rec_to_index_entry(ROW_COPY_DATA, rec, index, offsets,
  1803. &n_ext, *heap);
  1804. /* The call to row_rec_to_index_entry(ROW_COPY_DATA, ...) above
  1805. invokes rec_offs_make_valid() to point to the copied record that
  1806. the fields of new_entry point to. We have to undo it here. */
  1807. ut_ad(rec_offs_validate(NULL, index, offsets));
  1808. rec_offs_make_valid(rec, index, offsets);
  1809. /* The page containing the clustered index record
  1810. corresponding to new_entry is latched in mtr. If the
  1811. clustered index record is delete-marked, then its externally
  1812. stored fields cannot have been purged yet, because then the
  1813. purge would also have removed the clustered index record
  1814. itself. Thus the following call is safe. */
  1815. row_upd_index_replace_new_col_vals_index_pos(new_entry, index, update,
  1816. FALSE, *heap);
  1817. if (!(flags & BTR_KEEP_SYS_FLAG)) {
  1818. row_upd_index_entry_sys_field(new_entry, index, DATA_ROLL_PTR,
  1819. roll_ptr);
  1820. row_upd_index_entry_sys_field(new_entry, index, DATA_TRX_ID,
  1821. trx->id);
  1822. }
  1823. if ((flags & BTR_NO_UNDO_LOG_FLAG) && rec_offs_any_extern(offsets)) {
  1824. /* We are in a transaction rollback undoing a row
  1825. update: we must free possible externally stored fields
  1826. which got new values in the update, if they are not
  1827. inherited values. They can be inherited if we have
  1828. updated the primary key to another value, and then
  1829. update it back again. */
  1830. ut_ad(big_rec_vec == NULL);
  1831. btr_rec_free_updated_extern_fields(
  1832. index, rec, page_zip, offsets, update,
  1833. trx_is_recv(trx) ? RB_RECOVERY : RB_NORMAL, mtr);
  1834. }
  1835. /* We have to set appropriate extern storage bits in the new
  1836. record to be inserted: we have to remember which fields were such */
  1837. ut_ad(!page_is_comp(page) || !rec_get_node_ptr_flag(rec));
  1838. offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, heap);
  1839. n_ext += btr_push_update_extern_fields(new_entry, update, *heap);
  1840. if (UNIV_LIKELY_NULL(page_zip)) {
  1841. ut_ad(page_is_comp(page));
  1842. if (page_zip_rec_needs_ext(
  1843. rec_get_converted_size(index, new_entry, n_ext),
  1844. TRUE,
  1845. dict_index_get_n_fields(index),
  1846. page_zip_get_size(page_zip))) {
  1847. goto make_external;
  1848. }
  1849. } else if (page_zip_rec_needs_ext(
  1850. rec_get_converted_size(index, new_entry, n_ext),
  1851. page_is_comp(page), 0, 0)) {
  1852. make_external:
  1853. big_rec_vec = dtuple_convert_big_rec(index, new_entry, &n_ext);
  1854. if (UNIV_UNLIKELY(big_rec_vec == NULL)) {
  1855. err = DB_TOO_BIG_RECORD;
  1856. goto return_after_reservations;
  1857. }
  1858. }
  1859. /* Store state of explicit locks on rec on the page infimum record,
  1860. before deleting rec. The page infimum acts as a dummy carrier of the
  1861. locks, taking care also of lock releases, before we can move the locks
  1862. back on the actual record. There is a special case: if we are
  1863. inserting on the root page and the insert causes a call of
  1864. btr_root_raise_and_insert. Therefore we cannot in the lock system
  1865. delete the lock structs set on the root page even if the root
  1866. page carries just node pointers. */
  1867. lock_rec_store_on_page_infimum(block, rec);
  1868. btr_search_update_hash_on_delete(cursor);
  1869. #ifdef UNIV_ZIP_DEBUG
  1870. ut_a(!page_zip || page_zip_validate(page_zip, page));
  1871. #endif /* UNIV_ZIP_DEBUG */
  1872. page_cursor = btr_cur_get_page_cur(cursor);
  1873. page_cur_delete_rec(page_cursor, index, offsets, mtr);
  1874. page_cur_move_to_prev(page_cursor);
  1875. rec = btr_cur_insert_if_possible(cursor, new_entry, n_ext, mtr);
  1876. if (rec) {
  1877. lock_rec_restore_from_page_infimum(btr_cur_get_block(cursor),
  1878. rec, block);
  1879. offsets = rec_get_offsets(rec, index, offsets,
  1880. ULINT_UNDEFINED, heap);
  1881. if (!rec_get_deleted_flag(rec, rec_offs_comp(offsets))) {
  1882. /* The new inserted record owns its possible externally
  1883. stored fields */
  1884. btr_cur_unmark_extern_fields(page_zip,
  1885. rec, index, offsets, mtr);
  1886. }
  1887. btr_cur_compress_if_useful(cursor, mtr);
  1888. if (page_zip && !dict_index_is_clust(index)
  1889. && page_is_leaf(page)) {
  1890. /* Update the free bits in the insert buffer. */
  1891. ibuf_update_free_bits_zip(block, mtr);
  1892. }
  1893. err = DB_SUCCESS;
  1894. goto return_after_reservations;
  1895. } else {
  1896. ut_a(optim_err != DB_UNDERFLOW);
  1897. /* Out of space: reset the free bits. */
  1898. if (!dict_index_is_clust(index)
  1899. && page_is_leaf(page)) {
  1900. ibuf_reset_free_bits(block);
  1901. }
  1902. }
  1903. /* Was the record to be updated positioned as the first user
  1904. record on its page? */
  1905. was_first = page_cur_is_before_first(page_cursor);
  1906. /* The first parameter means that no lock checking and undo logging
  1907. is made in the insert */
  1908. err = btr_cur_pessimistic_insert(BTR_NO_UNDO_LOG_FLAG
  1909. | BTR_NO_LOCKING_FLAG
  1910. | BTR_KEEP_SYS_FLAG,
  1911. cursor, new_entry, &rec,
  1912. &dummy_big_rec, n_ext, NULL, mtr);
  1913. ut_a(rec);
  1914. ut_a(err == DB_SUCCESS);
  1915. ut_a(dummy_big_rec == NULL);
  1916. if (dict_index_is_sec_or_ibuf(index)) {
  1917. /* Update PAGE_MAX_TRX_ID in the index page header.
  1918. It was not updated by btr_cur_pessimistic_insert()
  1919. because of BTR_NO_LOCKING_FLAG. */
  1920. buf_block_t* rec_block;
  1921. rec_block = btr_cur_get_block(cursor);
  1922. page_update_max_trx_id(rec_block,
  1923. buf_block_get_page_zip(rec_block),
  1924. trx->id, mtr);
  1925. }
  1926. if (!rec_get_deleted_flag(rec, rec_offs_comp(offsets))) {
  1927. /* The new inserted record owns its possible externally
  1928. stored fields */
  1929. buf_block_t* rec_block = btr_cur_get_block(cursor);
  1930. #ifdef UNIV_ZIP_DEBUG
  1931. ut_a(!page_zip || page_zip_validate(page_zip, page));
  1932. page = buf_block_get_frame(rec_block);
  1933. #endif /* UNIV_ZIP_DEBUG */
  1934. page_zip = buf_block_get_page_zip(rec_block);
  1935. offsets = rec_get_offsets(rec, index, offsets,
  1936. ULINT_UNDEFINED, heap);
  1937. btr_cur_unmark_extern_fields(page_zip,
  1938. rec, index, offsets, mtr);
  1939. }
  1940. lock_rec_restore_from_page_infimum(btr_cur_get_block(cursor),
  1941. rec, block);
  1942. /* If necessary, restore also the correct lock state for a new,
  1943. preceding supremum record created in a page split. While the old
  1944. record was nonexistent, the supremum might have inherited its locks
  1945. from a wrong record. */
  1946. if (!was_first) {
  1947. btr_cur_pess_upd_restore_supremum(btr_cur_get_block(cursor),
  1948. rec, mtr);
  1949. }
  1950. return_after_reservations:
  1951. #ifdef UNIV_ZIP_DEBUG
  1952. ut_a(!page_zip || page_zip_validate(page_zip, page));
  1953. #endif /* UNIV_ZIP_DEBUG */
  1954. if (n_extents > 0) {
  1955. fil_space_release_free_extents(index->space, n_reserved);
  1956. }
  1957. *big_rec = big_rec_vec;
  1958. return(err);
  1959. }
  1960. /*==================== B-TREE DELETE MARK AND UNMARK ===============*/
  1961. /****************************************************************//**
  1962. Writes the redo log record for delete marking or unmarking of an index
  1963. record. */
  1964. UNIV_INLINE
  1965. void
  1966. btr_cur_del_mark_set_clust_rec_log(
  1967. /*===============================*/
  1968. ulint flags, /*!< in: flags */
  1969. rec_t* rec, /*!< in: record */
  1970. dict_index_t* index, /*!< in: index of the record */
  1971. ibool val, /*!< in: value to set */
  1972. trx_t* trx, /*!< in: deleting transaction */
  1973. roll_ptr_t roll_ptr,/*!< in: roll ptr to the undo log record */
  1974. mtr_t* mtr) /*!< in: mtr */
  1975. {
  1976. byte* log_ptr;
  1977. ut_ad(flags < 256);
  1978. ut_ad(val <= 1);
  1979. ut_ad(!!page_rec_is_comp(rec) == dict_table_is_comp(index->table));
  1980. log_ptr = mlog_open_and_write_index(mtr, rec, index,
  1981. page_rec_is_comp(rec)
  1982. ? MLOG_COMP_REC_CLUST_DELETE_MARK
  1983. : MLOG_REC_CLUST_DELETE_MARK,
  1984. 1 + 1 + DATA_ROLL_PTR_LEN
  1985. + 14 + 2);
  1986. if (!log_ptr) {
  1987. /* Logging in mtr is switched off during crash recovery */
  1988. return;
  1989. }
  1990. mach_write_to_1(log_ptr, flags);
  1991. log_ptr++;
  1992. mach_write_to_1(log_ptr, val);
  1993. log_ptr++;
  1994. log_ptr = row_upd_write_sys_vals_to_log(index, trx, roll_ptr, log_ptr,
  1995. mtr);
  1996. mach_write_to_2(log_ptr, page_offset(rec));
  1997. log_ptr += 2;
  1998. mlog_close(mtr, log_ptr);
  1999. }
  2000. #endif /* !UNIV_HOTBACKUP */
  2001. /****************************************************************//**
  2002. Parses the redo log record for delete marking or unmarking of a clustered
  2003. index record.
  2004. @return end of log record or NULL */
  2005. UNIV_INTERN
  2006. byte*
  2007. btr_cur_parse_del_mark_set_clust_rec(
  2008. /*=================================*/
  2009. byte* ptr, /*!< in: buffer */
  2010. byte* end_ptr,/*!< in: buffer end */
  2011. page_t* page, /*!< in/out: page or NULL */
  2012. page_zip_des_t* page_zip,/*!< in/out: compressed page, or NULL */
  2013. dict_index_t* index) /*!< in: index corresponding to page */
  2014. {
  2015. ulint flags;
  2016. ulint val;
  2017. ulint pos;
  2018. trx_id_t trx_id;
  2019. roll_ptr_t roll_ptr;
  2020. ulint offset;
  2021. rec_t* rec;
  2022. ut_ad(!page
  2023. || !!page_is_comp(page) == dict_table_is_comp(index->table));
  2024. if (end_ptr < ptr + 2) {
  2025. return(NULL);
  2026. }
  2027. flags = mach_read_from_1(ptr);
  2028. ptr++;
  2029. val = mach_read_from_1(ptr);
  2030. ptr++;
  2031. ptr = row_upd_parse_sys_vals(ptr, end_ptr, &pos, &trx_id, &roll_ptr);
  2032. if (ptr == NULL) {
  2033. return(NULL);
  2034. }
  2035. if (end_ptr < ptr + 2) {
  2036. return(NULL);
  2037. }
  2038. offset = mach_read_from_2(ptr);
  2039. ptr += 2;
  2040. ut_a(offset <= UNIV_PAGE_SIZE);
  2041. if (page) {
  2042. rec = page + offset;
  2043. /* We do not need to reserve btr_search_latch, as the page
  2044. is only being recovered, and there cannot be a hash index to
  2045. it. */
  2046. btr_rec_set_deleted_flag(rec, page_zip, val);
  2047. if (!(flags & BTR_KEEP_SYS_FLAG)) {
  2048. mem_heap_t* heap = NULL;
  2049. ulint offsets_[REC_OFFS_NORMAL_SIZE];
  2050. rec_offs_init(offsets_);
  2051. row_upd_rec_sys_fields_in_recovery(
  2052. rec, page_zip,
  2053. rec_get_offsets(rec, index, offsets_,
  2054. ULINT_UNDEFINED, &heap),
  2055. pos, trx_id, roll_ptr);
  2056. if (UNIV_LIKELY_NULL(heap)) {
  2057. mem_heap_free(heap);
  2058. }
  2059. }
  2060. }
  2061. return(ptr);
  2062. }
  2063. #ifndef UNIV_HOTBACKUP
  2064. /***********************************************************//**
  2065. Marks a clustered index record deleted. Writes an undo log record to
  2066. undo log on this delete marking. Writes in the trx id field the id
  2067. of the deleting transaction, and in the roll ptr field pointer to the
  2068. undo log record created.
  2069. @return DB_SUCCESS, DB_LOCK_WAIT, or error number */
  2070. UNIV_INTERN
  2071. ulint
  2072. btr_cur_del_mark_set_clust_rec(
  2073. /*===========================*/
  2074. ulint flags, /*!< in: undo logging and locking flags */
  2075. btr_cur_t* cursor, /*!< in: cursor */
  2076. ibool val, /*!< in: value to set */
  2077. que_thr_t* thr, /*!< in: query thread */
  2078. mtr_t* mtr) /*!< in: mtr */
  2079. {
  2080. dict_index_t* index;
  2081. buf_block_t* block;
  2082. roll_ptr_t roll_ptr;
  2083. ulint err;
  2084. rec_t* rec;
  2085. page_zip_des_t* page_zip;
  2086. trx_t* trx;
  2087. mem_heap_t* heap = NULL;
  2088. ulint offsets_[REC_OFFS_NORMAL_SIZE];
  2089. ulint* offsets = offsets_;
  2090. rec_offs_init(offsets_);
  2091. rec = btr_cur_get_rec(cursor);
  2092. index = cursor->index;
  2093. ut_ad(!!page_rec_is_comp(rec) == dict_table_is_comp(index->table));
  2094. offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, &heap);
  2095. #ifdef UNIV_DEBUG
  2096. if (btr_cur_print_record_ops && thr) {
  2097. btr_cur_trx_report(thr_get_trx(thr), index, "del mark ");
  2098. rec_print_new(stderr, rec, offsets);
  2099. }
  2100. #endif /* UNIV_DEBUG */
  2101. ut_ad(dict_index_is_clust(index));
  2102. ut_ad(!rec_get_deleted_flag(rec, rec_offs_comp(offsets)));
  2103. err = lock_clust_rec_modify_check_and_lock(flags,
  2104. btr_cur_get_block(cursor),
  2105. rec, index, offsets, thr);
  2106. if (err != DB_SUCCESS) {
  2107. goto func_exit;
  2108. }
  2109. err = trx_undo_report_row_operation(flags, TRX_UNDO_MODIFY_OP, thr,
  2110. index, NULL, NULL, 0, rec,
  2111. &roll_ptr);
  2112. if (err != DB_SUCCESS) {
  2113. goto func_exit;
  2114. }
  2115. block = btr_cur_get_block(cursor);
  2116. if (block->is_hashed) {
  2117. rw_lock_x_lock(&btr_search_latch);
  2118. }
  2119. page_zip = buf_block_get_page_zip(block);
  2120. btr_rec_set_deleted_flag(rec, page_zip, val);
  2121. trx = thr_get_trx(thr);
  2122. if (!(flags & BTR_KEEP_SYS_FLAG)) {
  2123. row_upd_rec_sys_fields(rec, page_zip,
  2124. index, offsets, trx, roll_ptr);
  2125. }
  2126. if (block->is_hashed) {
  2127. rw_lock_x_unlock(&btr_search_latch);
  2128. }
  2129. btr_cur_del_mark_set_clust_rec_log(flags, rec, index, val, trx,
  2130. roll_ptr, mtr);
  2131. func_exit:
  2132. if (UNIV_LIKELY_NULL(heap)) {
  2133. mem_heap_free(heap);
  2134. }
  2135. return(err);
  2136. }
  2137. /****************************************************************//**
  2138. Writes the redo log record for a delete mark setting of a secondary
  2139. index record. */
  2140. UNIV_INLINE
  2141. void
  2142. btr_cur_del_mark_set_sec_rec_log(
  2143. /*=============================*/
  2144. rec_t* rec, /*!< in: record */
  2145. ibool val, /*!< in: value to set */
  2146. mtr_t* mtr) /*!< in: mtr */
  2147. {
  2148. byte* log_ptr;
  2149. ut_ad(val <= 1);
  2150. log_ptr = mlog_open(mtr, 11 + 1 + 2);
  2151. if (!log_ptr) {
  2152. /* Logging in mtr is switched off during crash recovery:
  2153. in that case mlog_open returns NULL */
  2154. return;
  2155. }
  2156. log_ptr = mlog_write_initial_log_record_fast(
  2157. rec, MLOG_REC_SEC_DELETE_MARK, log_ptr, mtr);
  2158. mach_write_to_1(log_ptr, val);
  2159. log_ptr++;
  2160. mach_write_to_2(log_ptr, page_offset(rec));
  2161. log_ptr += 2;
  2162. mlog_close(mtr, log_ptr);
  2163. }
  2164. #endif /* !UNIV_HOTBACKUP */
  2165. /****************************************************************//**
  2166. Parses the redo log record for delete marking or unmarking of a secondary
  2167. index record.
  2168. @return end of log record or NULL */
  2169. UNIV_INTERN
  2170. byte*
  2171. btr_cur_parse_del_mark_set_sec_rec(
  2172. /*===============================*/
  2173. byte* ptr, /*!< in: buffer */
  2174. byte* end_ptr,/*!< in: buffer end */
  2175. page_t* page, /*!< in/out: page or NULL */
  2176. page_zip_des_t* page_zip)/*!< in/out: compressed page, or NULL */
  2177. {
  2178. ulint val;
  2179. ulint offset;
  2180. rec_t* rec;
  2181. if (end_ptr < ptr + 3) {
  2182. return(NULL);
  2183. }
  2184. val = mach_read_from_1(ptr);
  2185. ptr++;
  2186. offset = mach_read_from_2(ptr);
  2187. ptr += 2;
  2188. ut_a(offset <= UNIV_PAGE_SIZE);
  2189. if (page) {
  2190. rec = page + offset;
  2191. /* We do not need to reserve btr_search_latch, as the page
  2192. is only being recovered, and there cannot be a hash index to
  2193. it. */
  2194. btr_rec_set_deleted_flag(rec, page_zip, val);
  2195. }
  2196. return(ptr);
  2197. }
  2198. #ifndef UNIV_HOTBACKUP
  2199. /***********************************************************//**
  2200. Sets a secondary index record delete mark to TRUE or FALSE.
  2201. @return DB_SUCCESS, DB_LOCK_WAIT, or error number */
  2202. UNIV_INTERN
  2203. ulint
  2204. btr_cur_del_mark_set_sec_rec(
  2205. /*=========================*/
  2206. ulint flags, /*!< in: locking flag */
  2207. btr_cur_t* cursor, /*!< in: cursor */
  2208. ibool val, /*!< in: value to set */
  2209. que_thr_t* thr, /*!< in: query thread */
  2210. mtr_t* mtr) /*!< in: mtr */
  2211. {
  2212. buf_block_t* block;
  2213. rec_t* rec;
  2214. ulint err;
  2215. block = btr_cur_get_block(cursor);
  2216. rec = btr_cur_get_rec(cursor);
  2217. #ifdef UNIV_DEBUG
  2218. if (btr_cur_print_record_ops && thr) {
  2219. btr_cur_trx_report(thr_get_trx(thr), cursor->index,
  2220. "del mark ");
  2221. rec_print(stderr, rec, cursor->index);
  2222. }
  2223. #endif /* UNIV_DEBUG */
  2224. err = lock_sec_rec_modify_check_and_lock(flags,
  2225. btr_cur_get_block(cursor),
  2226. rec, cursor->index, thr, mtr);
  2227. if (err != DB_SUCCESS) {
  2228. return(err);
  2229. }
  2230. ut_ad(!!page_rec_is_comp(rec)
  2231. == dict_table_is_comp(cursor->index->table));
  2232. if (block->is_hashed) {
  2233. rw_lock_x_lock(&btr_search_latch);
  2234. }
  2235. btr_rec_set_deleted_flag(rec, buf_block_get_page_zip(block), val);
  2236. if (block->is_hashed) {
  2237. rw_lock_x_unlock(&btr_search_latch);
  2238. }
  2239. btr_cur_del_mark_set_sec_rec_log(rec, val, mtr);
  2240. return(DB_SUCCESS);
  2241. }
  2242. /***********************************************************//**
  2243. Clear a secondary index record's delete mark. This function is only
  2244. used by the insert buffer insert merge mechanism. */
  2245. UNIV_INTERN
  2246. void
  2247. btr_cur_del_unmark_for_ibuf(
  2248. /*========================*/
  2249. rec_t* rec, /*!< in/out: record to delete unmark */
  2250. page_zip_des_t* page_zip, /*!< in/out: compressed page
  2251. corresponding to rec, or NULL
  2252. when the tablespace is
  2253. uncompressed */
  2254. mtr_t* mtr) /*!< in: mtr */
  2255. {
  2256. /* We do not need to reserve btr_search_latch, as the page has just
  2257. been read to the buffer pool and there cannot be a hash index to it. */
  2258. btr_rec_set_deleted_flag(rec, page_zip, FALSE);
  2259. btr_cur_del_mark_set_sec_rec_log(rec, FALSE, mtr);
  2260. }
  2261. /*==================== B-TREE RECORD REMOVE =========================*/
  2262. /*************************************************************//**
  2263. Tries to compress a page of the tree if it seems useful. It is assumed
  2264. that mtr holds an x-latch on the tree and on the cursor page. To avoid
  2265. deadlocks, mtr must also own x-latches to brothers of page, if those
  2266. brothers exist. NOTE: it is assumed that the caller has reserved enough
  2267. free extents so that the compression will always succeed if done!
  2268. @return TRUE if compression occurred */
  2269. UNIV_INTERN
  2270. ibool
  2271. btr_cur_compress_if_useful(
  2272. /*=======================*/
  2273. btr_cur_t* cursor, /*!< in: cursor on the page to compress;
  2274. cursor does not stay valid if compression
  2275. occurs */
  2276. mtr_t* mtr) /*!< in: mtr */
  2277. {
  2278. ut_ad(mtr_memo_contains(mtr,
  2279. dict_index_get_lock(btr_cur_get_index(cursor)),
  2280. MTR_MEMO_X_LOCK));
  2281. ut_ad(mtr_memo_contains(mtr, btr_cur_get_block(cursor),
  2282. MTR_MEMO_PAGE_X_FIX));
  2283. return(btr_cur_compress_recommendation(cursor, mtr)
  2284. && btr_compress(cursor, mtr));
  2285. }
  2286. /*******************************************************//**
  2287. Removes the record on which the tree cursor is positioned on a leaf page.
  2288. It is assumed that the mtr has an x-latch on the page where the cursor is
  2289. positioned, but no latch on the whole tree.
  2290. @return TRUE if success, i.e., the page did not become too empty */
  2291. UNIV_INTERN
  2292. ibool
  2293. btr_cur_optimistic_delete(
  2294. /*======================*/
  2295. btr_cur_t* cursor, /*!< in: cursor on leaf page, on the record to
  2296. delete; cursor stays valid: if deletion
  2297. succeeds, on function exit it points to the
  2298. successor of the deleted record */
  2299. mtr_t* mtr) /*!< in: mtr; if this function returns
  2300. TRUE on a leaf page of a secondary
  2301. index, the mtr must be committed
  2302. before latching any further pages */
  2303. {
  2304. buf_block_t* block;
  2305. rec_t* rec;
  2306. mem_heap_t* heap = NULL;
  2307. ulint offsets_[REC_OFFS_NORMAL_SIZE];
  2308. ulint* offsets = offsets_;
  2309. ibool no_compress_needed;
  2310. rec_offs_init(offsets_);
  2311. ut_ad(mtr_memo_contains(mtr, btr_cur_get_block(cursor),
  2312. MTR_MEMO_PAGE_X_FIX));
  2313. /* This is intended only for leaf page deletions */
  2314. block = btr_cur_get_block(cursor);
  2315. ut_ad(page_is_leaf(buf_block_get_frame(block)));
  2316. rec = btr_cur_get_rec(cursor);
  2317. offsets = rec_get_offsets(rec, cursor->index, offsets,
  2318. ULINT_UNDEFINED, &heap);
  2319. no_compress_needed = !rec_offs_any_extern(offsets)
  2320. && btr_cur_can_delete_without_compress(
  2321. cursor, rec_offs_size(offsets), mtr);
  2322. if (no_compress_needed) {
  2323. page_t* page = buf_block_get_frame(block);
  2324. page_zip_des_t* page_zip= buf_block_get_page_zip(block);
  2325. ulint max_ins = 0;
  2326. lock_update_delete(block, rec);
  2327. btr_search_update_hash_on_delete(cursor);
  2328. if (!page_zip) {
  2329. max_ins = page_get_max_insert_size_after_reorganize(
  2330. page, 1);
  2331. }
  2332. #ifdef UNIV_ZIP_DEBUG
  2333. ut_a(!page_zip || page_zip_validate(page_zip, page));
  2334. #endif /* UNIV_ZIP_DEBUG */
  2335. page_cur_delete_rec(btr_cur_get_page_cur(cursor),
  2336. cursor->index, offsets, mtr);
  2337. #ifdef UNIV_ZIP_DEBUG
  2338. ut_a(!page_zip || page_zip_validate(page_zip, page));
  2339. #endif /* UNIV_ZIP_DEBUG */
  2340. if (dict_index_is_clust(cursor->index)
  2341. || dict_index_is_ibuf(cursor->index)
  2342. || !page_is_leaf(page)) {
  2343. /* The insert buffer does not handle
  2344. inserts to clustered indexes, to
  2345. non-leaf pages of secondary index B-trees,
  2346. or to the insert buffer. */
  2347. } else if (page_zip) {
  2348. ibuf_update_free_bits_zip(block, mtr);
  2349. } else {
  2350. ibuf_update_free_bits_low(block, max_ins, mtr);
  2351. }
  2352. }
  2353. if (UNIV_LIKELY_NULL(heap)) {
  2354. mem_heap_free(heap);
  2355. }
  2356. return(no_compress_needed);
  2357. }
  2358. /*************************************************************//**
  2359. Removes the record on which the tree cursor is positioned. Tries
  2360. to compress the page if its fillfactor drops below a threshold
  2361. or if it is the only page on the level. It is assumed that mtr holds
  2362. an x-latch on the tree and on the cursor page. To avoid deadlocks,
  2363. mtr must also own x-latches to brothers of page, if those brothers
  2364. exist.
  2365. @return TRUE if compression occurred */
  2366. UNIV_INTERN
  2367. ibool
  2368. btr_cur_pessimistic_delete(
  2369. /*=======================*/
  2370. ulint* err, /*!< out: DB_SUCCESS or DB_OUT_OF_FILE_SPACE;
  2371. the latter may occur because we may have
  2372. to update node pointers on upper levels,
  2373. and in the case of variable length keys
  2374. these may actually grow in size */
  2375. ibool has_reserved_extents, /*!< in: TRUE if the
  2376. caller has already reserved enough free
  2377. extents so that he knows that the operation
  2378. will succeed */
  2379. btr_cur_t* cursor, /*!< in: cursor on the record to delete;
  2380. if compression does not occur, the cursor
  2381. stays valid: it points to successor of
  2382. deleted record on function exit */
  2383. enum trx_rb_ctx rb_ctx, /*!< in: rollback context */
  2384. mtr_t* mtr) /*!< in: mtr */
  2385. {
  2386. buf_block_t* block;
  2387. page_t* page;
  2388. page_zip_des_t* page_zip;
  2389. dict_index_t* index;
  2390. rec_t* rec;
  2391. dtuple_t* node_ptr;
  2392. ulint n_extents = 0;
  2393. ulint n_reserved;
  2394. ibool success;
  2395. ibool ret = FALSE;
  2396. ulint level;
  2397. mem_heap_t* heap;
  2398. ulint* offsets;
  2399. block = btr_cur_get_block(cursor);
  2400. page = buf_block_get_frame(block);
  2401. index = btr_cur_get_index(cursor);
  2402. ut_ad(mtr_memo_contains(mtr, dict_index_get_lock(index),
  2403. MTR_MEMO_X_LOCK));
  2404. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  2405. if (!has_reserved_extents) {
  2406. /* First reserve enough free space for the file segments
  2407. of the index tree, so that the node pointer updates will
  2408. not fail because of lack of space */
  2409. n_extents = cursor->tree_height / 32 + 1;
  2410. success = fsp_reserve_free_extents(&n_reserved,
  2411. index->space,
  2412. n_extents,
  2413. FSP_CLEANING, mtr);
  2414. if (!success) {
  2415. *err = DB_OUT_OF_FILE_SPACE;
  2416. return(FALSE);
  2417. }
  2418. }
  2419. heap = mem_heap_create(1024);
  2420. rec = btr_cur_get_rec(cursor);
  2421. page_zip = buf_block_get_page_zip(block);
  2422. #ifdef UNIV_ZIP_DEBUG
  2423. ut_a(!page_zip || page_zip_validate(page_zip, page));
  2424. #endif /* UNIV_ZIP_DEBUG */
  2425. offsets = rec_get_offsets(rec, index, NULL, ULINT_UNDEFINED, &heap);
  2426. if (rec_offs_any_extern(offsets)) {
  2427. btr_rec_free_externally_stored_fields(index,
  2428. rec, offsets, page_zip,
  2429. rb_ctx, mtr);
  2430. #ifdef UNIV_ZIP_DEBUG
  2431. ut_a(!page_zip || page_zip_validate(page_zip, page));
  2432. #endif /* UNIV_ZIP_DEBUG */
  2433. }
  2434. if (UNIV_UNLIKELY(page_get_n_recs(page) < 2)
  2435. && UNIV_UNLIKELY(dict_index_get_page(index)
  2436. != buf_block_get_page_no(block))) {
  2437. /* If there is only one record, drop the whole page in
  2438. btr_discard_page, if this is not the root page */
  2439. btr_discard_page(cursor, mtr);
  2440. *err = DB_SUCCESS;
  2441. ret = TRUE;
  2442. goto return_after_reservations;
  2443. }
  2444. lock_update_delete(block, rec);
  2445. level = btr_page_get_level(page, mtr);
  2446. if (level > 0
  2447. && UNIV_UNLIKELY(rec == page_rec_get_next(
  2448. page_get_infimum_rec(page)))) {
  2449. rec_t* next_rec = page_rec_get_next(rec);
  2450. if (btr_page_get_prev(page, mtr) == FIL_NULL) {
  2451. /* If we delete the leftmost node pointer on a
  2452. non-leaf level, we must mark the new leftmost node
  2453. pointer as the predefined minimum record */
  2454. /* This will make page_zip_validate() fail until
  2455. page_cur_delete_rec() completes. This is harmless,
  2456. because everything will take place within a single
  2457. mini-transaction and because writing to the redo log
  2458. is an atomic operation (performed by mtr_commit()). */
  2459. btr_set_min_rec_mark(next_rec, mtr);
  2460. } else {
  2461. /* Otherwise, if we delete the leftmost node pointer
  2462. on a page, we have to change the father node pointer
  2463. so that it is equal to the new leftmost node pointer
  2464. on the page */
  2465. btr_node_ptr_delete(index, block, mtr);
  2466. node_ptr = dict_index_build_node_ptr(
  2467. index, next_rec, buf_block_get_page_no(block),
  2468. heap, level);
  2469. btr_insert_on_non_leaf_level(index,
  2470. level + 1, node_ptr, mtr);
  2471. }
  2472. }
  2473. btr_search_update_hash_on_delete(cursor);
  2474. page_cur_delete_rec(btr_cur_get_page_cur(cursor), index, offsets, mtr);
  2475. #ifdef UNIV_ZIP_DEBUG
  2476. ut_a(!page_zip || page_zip_validate(page_zip, page));
  2477. #endif /* UNIV_ZIP_DEBUG */
  2478. ut_ad(btr_check_node_ptr(index, block, mtr));
  2479. *err = DB_SUCCESS;
  2480. return_after_reservations:
  2481. mem_heap_free(heap);
  2482. if (ret == FALSE) {
  2483. ret = btr_cur_compress_if_useful(cursor, mtr);
  2484. }
  2485. if (n_extents > 0) {
  2486. fil_space_release_free_extents(index->space, n_reserved);
  2487. }
  2488. return(ret);
  2489. }
  2490. /*******************************************************************//**
  2491. Adds path information to the cursor for the current page, for which
  2492. the binary search has been performed. */
  2493. static
  2494. void
  2495. btr_cur_add_path_info(
  2496. /*==================*/
  2497. btr_cur_t* cursor, /*!< in: cursor positioned on a page */
  2498. ulint height, /*!< in: height of the page in tree;
  2499. 0 means leaf node */
  2500. ulint root_height) /*!< in: root node height in tree */
  2501. {
  2502. btr_path_t* slot;
  2503. rec_t* rec;
  2504. ut_a(cursor->path_arr);
  2505. if (root_height >= BTR_PATH_ARRAY_N_SLOTS - 1) {
  2506. /* Do nothing; return empty path */
  2507. slot = cursor->path_arr;
  2508. slot->nth_rec = ULINT_UNDEFINED;
  2509. return;
  2510. }
  2511. if (height == 0) {
  2512. /* Mark end of slots for path */
  2513. slot = cursor->path_arr + root_height + 1;
  2514. slot->nth_rec = ULINT_UNDEFINED;
  2515. }
  2516. rec = btr_cur_get_rec(cursor);
  2517. slot = cursor->path_arr + (root_height - height);
  2518. slot->nth_rec = page_rec_get_n_recs_before(rec);
  2519. slot->n_recs = page_get_n_recs(page_align(rec));
  2520. }
  2521. /*******************************************************************//**
  2522. Estimates the number of rows in a given index range.
  2523. @return estimated number of rows */
  2524. UNIV_INTERN
  2525. ib_int64_t
  2526. btr_estimate_n_rows_in_range(
  2527. /*=========================*/
  2528. dict_index_t* index, /*!< in: index */
  2529. const dtuple_t* tuple1, /*!< in: range start, may also be empty tuple */
  2530. ulint mode1, /*!< in: search mode for range start */
  2531. const dtuple_t* tuple2, /*!< in: range end, may also be empty tuple */
  2532. ulint mode2) /*!< in: search mode for range end */
  2533. {
  2534. btr_path_t path1[BTR_PATH_ARRAY_N_SLOTS];
  2535. btr_path_t path2[BTR_PATH_ARRAY_N_SLOTS];
  2536. btr_cur_t cursor;
  2537. btr_path_t* slot1;
  2538. btr_path_t* slot2;
  2539. ibool diverged;
  2540. ibool diverged_lot;
  2541. ulint divergence_level;
  2542. ib_int64_t n_rows;
  2543. ulint i;
  2544. mtr_t mtr;
  2545. mtr_start(&mtr);
  2546. cursor.path_arr = path1;
  2547. if (dtuple_get_n_fields(tuple1) > 0) {
  2548. btr_cur_search_to_nth_level(index, 0, tuple1, mode1,
  2549. BTR_SEARCH_LEAF | BTR_ESTIMATE,
  2550. &cursor, 0,
  2551. __FILE__, __LINE__, &mtr);
  2552. } else {
  2553. btr_cur_open_at_index_side(TRUE, index,
  2554. BTR_SEARCH_LEAF | BTR_ESTIMATE,
  2555. &cursor, &mtr);
  2556. }
  2557. mtr_commit(&mtr);
  2558. mtr_start(&mtr);
  2559. cursor.path_arr = path2;
  2560. if (dtuple_get_n_fields(tuple2) > 0) {
  2561. btr_cur_search_to_nth_level(index, 0, tuple2, mode2,
  2562. BTR_SEARCH_LEAF | BTR_ESTIMATE,
  2563. &cursor, 0,
  2564. __FILE__, __LINE__, &mtr);
  2565. } else {
  2566. btr_cur_open_at_index_side(FALSE, index,
  2567. BTR_SEARCH_LEAF | BTR_ESTIMATE,
  2568. &cursor, &mtr);
  2569. }
  2570. mtr_commit(&mtr);
  2571. /* We have the path information for the range in path1 and path2 */
  2572. n_rows = 1;
  2573. diverged = FALSE; /* This becomes true when the path is not
  2574. the same any more */
  2575. diverged_lot = FALSE; /* This becomes true when the paths are
  2576. not the same or adjacent any more */
  2577. divergence_level = 1000000; /* This is the level where paths diverged
  2578. a lot */
  2579. for (i = 0; ; i++) {
  2580. ut_ad(i < BTR_PATH_ARRAY_N_SLOTS);
  2581. slot1 = path1 + i;
  2582. slot2 = path2 + i;
  2583. if (slot1->nth_rec == ULINT_UNDEFINED
  2584. || slot2->nth_rec == ULINT_UNDEFINED) {
  2585. if (i > divergence_level + 1) {
  2586. /* In trees whose height is > 1 our algorithm
  2587. tends to underestimate: multiply the estimate
  2588. by 2: */
  2589. n_rows = n_rows * 2;
  2590. }
  2591. /* Do not estimate the number of rows in the range
  2592. to over 1 / 2 of the estimated rows in the whole
  2593. table */
  2594. if (n_rows > index->table->stat_n_rows / 2) {
  2595. n_rows = index->table->stat_n_rows / 2;
  2596. /* If there are just 0 or 1 rows in the table,
  2597. then we estimate all rows are in the range */
  2598. if (n_rows == 0) {
  2599. n_rows = index->table->stat_n_rows;
  2600. }
  2601. }
  2602. return(n_rows);
  2603. }
  2604. if (!diverged && slot1->nth_rec != slot2->nth_rec) {
  2605. diverged = TRUE;
  2606. if (slot1->nth_rec < slot2->nth_rec) {
  2607. n_rows = slot2->nth_rec - slot1->nth_rec;
  2608. if (n_rows > 1) {
  2609. diverged_lot = TRUE;
  2610. divergence_level = i;
  2611. }
  2612. } else {
  2613. /* Maybe the tree has changed between
  2614. searches */
  2615. return(10);
  2616. }
  2617. } else if (diverged && !diverged_lot) {
  2618. if (slot1->nth_rec < slot1->n_recs
  2619. || slot2->nth_rec > 1) {
  2620. diverged_lot = TRUE;
  2621. divergence_level = i;
  2622. n_rows = 0;
  2623. if (slot1->nth_rec < slot1->n_recs) {
  2624. n_rows += slot1->n_recs
  2625. - slot1->nth_rec;
  2626. }
  2627. if (slot2->nth_rec > 1) {
  2628. n_rows += slot2->nth_rec - 1;
  2629. }
  2630. }
  2631. } else if (diverged_lot) {
  2632. n_rows = (n_rows * (slot1->n_recs + slot2->n_recs))
  2633. / 2;
  2634. }
  2635. }
  2636. }
  2637. /*******************************************************************//**
  2638. Estimates the number of different key values in a given index, for
  2639. each n-column prefix of the index where n <= dict_index_get_n_unique(index).
  2640. The estimates are stored in the array index->stat_n_diff_key_vals. */
  2641. UNIV_INTERN
  2642. void
  2643. btr_estimate_number_of_different_key_vals(
  2644. /*======================================*/
  2645. dict_index_t* index) /*!< in: index */
  2646. {
  2647. btr_cur_t cursor;
  2648. page_t* page;
  2649. rec_t* rec;
  2650. ulint n_cols;
  2651. ulint matched_fields;
  2652. ulint matched_bytes;
  2653. ib_int64_t* n_diff;
  2654. ullint n_sample_pages; /* number of pages to sample */
  2655. ulint not_empty_flag = 0;
  2656. ulint total_external_size = 0;
  2657. ulint i;
  2658. ulint j;
  2659. ullint add_on;
  2660. mtr_t mtr;
  2661. mem_heap_t* heap = NULL;
  2662. ulint offsets_rec_[REC_OFFS_NORMAL_SIZE];
  2663. ulint offsets_next_rec_[REC_OFFS_NORMAL_SIZE];
  2664. ulint* offsets_rec = offsets_rec_;
  2665. ulint* offsets_next_rec= offsets_next_rec_;
  2666. rec_offs_init(offsets_rec_);
  2667. rec_offs_init(offsets_next_rec_);
  2668. n_cols = dict_index_get_n_unique(index);
  2669. n_diff = mem_zalloc((n_cols + 1) * sizeof(ib_int64_t));
  2670. /* It makes no sense to test more pages than are contained
  2671. in the index, thus we lower the number if it is too high */
  2672. if (srv_stats_sample_pages > index->stat_index_size) {
  2673. if (index->stat_index_size > 0) {
  2674. n_sample_pages = index->stat_index_size;
  2675. } else {
  2676. n_sample_pages = 1;
  2677. }
  2678. } else {
  2679. n_sample_pages = srv_stats_sample_pages;
  2680. }
  2681. /* We sample some pages in the index to get an estimate */
  2682. for (i = 0; i < n_sample_pages; i++) {
  2683. rec_t* supremum;
  2684. mtr_start(&mtr);
  2685. btr_cur_open_at_rnd_pos(index, BTR_SEARCH_LEAF, &cursor, &mtr);
  2686. /* Count the number of different key values for each prefix of
  2687. the key on this index page. If the prefix does not determine
  2688. the index record uniquely in the B-tree, then we subtract one
  2689. because otherwise our algorithm would give a wrong estimate
  2690. for an index where there is just one key value. */
  2691. page = btr_cur_get_page(&cursor);
  2692. supremum = page_get_supremum_rec(page);
  2693. rec = page_rec_get_next(page_get_infimum_rec(page));
  2694. if (rec != supremum) {
  2695. not_empty_flag = 1;
  2696. offsets_rec = rec_get_offsets(rec, index, offsets_rec,
  2697. ULINT_UNDEFINED, &heap);
  2698. }
  2699. while (rec != supremum) {
  2700. rec_t* next_rec = page_rec_get_next(rec);
  2701. if (next_rec == supremum) {
  2702. break;
  2703. }
  2704. matched_fields = 0;
  2705. matched_bytes = 0;
  2706. offsets_next_rec = rec_get_offsets(next_rec, index,
  2707. offsets_next_rec,
  2708. n_cols, &heap);
  2709. cmp_rec_rec_with_match(rec, next_rec,
  2710. offsets_rec, offsets_next_rec,
  2711. index, &matched_fields,
  2712. &matched_bytes);
  2713. for (j = matched_fields + 1; j <= n_cols; j++) {
  2714. /* We add one if this index record has
  2715. a different prefix from the previous */
  2716. n_diff[j]++;
  2717. }
  2718. total_external_size
  2719. += btr_rec_get_externally_stored_len(
  2720. rec, offsets_rec);
  2721. rec = next_rec;
  2722. /* Initialize offsets_rec for the next round
  2723. and assign the old offsets_rec buffer to
  2724. offsets_next_rec. */
  2725. {
  2726. ulint* offsets_tmp = offsets_rec;
  2727. offsets_rec = offsets_next_rec;
  2728. offsets_next_rec = offsets_tmp;
  2729. }
  2730. }
  2731. if (n_cols == dict_index_get_n_unique_in_tree(index)) {
  2732. /* If there is more than one leaf page in the tree,
  2733. we add one because we know that the first record
  2734. on the page certainly had a different prefix than the
  2735. last record on the previous index page in the
  2736. alphabetical order. Before this fix, if there was
  2737. just one big record on each clustered index page, the
  2738. algorithm grossly underestimated the number of rows
  2739. in the table. */
  2740. if (btr_page_get_prev(page, &mtr) != FIL_NULL
  2741. || btr_page_get_next(page, &mtr) != FIL_NULL) {
  2742. n_diff[n_cols]++;
  2743. }
  2744. }
  2745. offsets_rec = rec_get_offsets(rec, index, offsets_rec,
  2746. ULINT_UNDEFINED, &heap);
  2747. total_external_size += btr_rec_get_externally_stored_len(
  2748. rec, offsets_rec);
  2749. mtr_commit(&mtr);
  2750. }
  2751. /* If we saw k borders between different key values on
  2752. n_sample_pages leaf pages, we can estimate how many
  2753. there will be in index->stat_n_leaf_pages */
  2754. /* We must take into account that our sample actually represents
  2755. also the pages used for external storage of fields (those pages are
  2756. included in index->stat_n_leaf_pages) */
  2757. for (j = 0; j <= n_cols; j++) {
  2758. index->stat_n_diff_key_vals[j]
  2759. = ((n_diff[j]
  2760. * (ib_int64_t)index->stat_n_leaf_pages
  2761. + n_sample_pages - 1
  2762. + total_external_size
  2763. + not_empty_flag)
  2764. / (n_sample_pages
  2765. + total_external_size));
  2766. /* If the tree is small, smaller than
  2767. 10 * n_sample_pages + total_external_size, then
  2768. the above estimate is ok. For bigger trees it is common that we
  2769. do not see any borders between key values in the few pages
  2770. we pick. But still there may be n_sample_pages
  2771. different key values, or even more. Let us try to approximate
  2772. that: */
  2773. add_on = index->stat_n_leaf_pages
  2774. / (10 * (n_sample_pages
  2775. + total_external_size));
  2776. if (add_on > n_sample_pages) {
  2777. add_on = n_sample_pages;
  2778. }
  2779. index->stat_n_diff_key_vals[j] += add_on;
  2780. }
  2781. mem_free(n_diff);
  2782. if (UNIV_LIKELY_NULL(heap)) {
  2783. mem_heap_free(heap);
  2784. }
  2785. }
  2786. /*================== EXTERNAL STORAGE OF BIG FIELDS ===================*/
  2787. /***********************************************************//**
  2788. Gets the externally stored size of a record, in units of a database page.
  2789. @return externally stored part, in units of a database page */
  2790. static
  2791. ulint
  2792. btr_rec_get_externally_stored_len(
  2793. /*==============================*/
  2794. rec_t* rec, /*!< in: record */
  2795. const ulint* offsets)/*!< in: array returned by rec_get_offsets() */
  2796. {
  2797. ulint n_fields;
  2798. byte* data;
  2799. ulint local_len;
  2800. ulint extern_len;
  2801. ulint total_extern_len = 0;
  2802. ulint i;
  2803. ut_ad(!rec_offs_comp(offsets) || !rec_get_node_ptr_flag(rec));
  2804. n_fields = rec_offs_n_fields(offsets);
  2805. for (i = 0; i < n_fields; i++) {
  2806. if (rec_offs_nth_extern(offsets, i)) {
  2807. data = rec_get_nth_field(rec, offsets, i, &local_len);
  2808. local_len -= BTR_EXTERN_FIELD_REF_SIZE;
  2809. extern_len = mach_read_from_4(data + local_len
  2810. + BTR_EXTERN_LEN + 4);
  2811. total_extern_len += ut_calc_align(extern_len,
  2812. UNIV_PAGE_SIZE);
  2813. }
  2814. }
  2815. return(total_extern_len / UNIV_PAGE_SIZE);
  2816. }
  2817. /*******************************************************************//**
  2818. Sets the ownership bit of an externally stored field in a record. */
  2819. static
  2820. void
  2821. btr_cur_set_ownership_of_extern_field(
  2822. /*==================================*/
  2823. page_zip_des_t* page_zip,/*!< in/out: compressed page whose uncompressed
  2824. part will be updated, or NULL */
  2825. rec_t* rec, /*!< in/out: clustered index record */
  2826. dict_index_t* index, /*!< in: index of the page */
  2827. const ulint* offsets,/*!< in: array returned by rec_get_offsets() */
  2828. ulint i, /*!< in: field number */
  2829. ibool val, /*!< in: value to set */
  2830. mtr_t* mtr) /*!< in: mtr, or NULL if not logged */
  2831. {
  2832. byte* data;
  2833. ulint local_len;
  2834. ulint byte_val;
  2835. data = rec_get_nth_field(rec, offsets, i, &local_len);
  2836. ut_a(local_len >= BTR_EXTERN_FIELD_REF_SIZE);
  2837. local_len -= BTR_EXTERN_FIELD_REF_SIZE;
  2838. byte_val = mach_read_from_1(data + local_len + BTR_EXTERN_LEN);
  2839. if (val) {
  2840. byte_val = byte_val & (~BTR_EXTERN_OWNER_FLAG);
  2841. } else {
  2842. byte_val = byte_val | BTR_EXTERN_OWNER_FLAG;
  2843. }
  2844. if (UNIV_LIKELY_NULL(page_zip)) {
  2845. mach_write_to_1(data + local_len + BTR_EXTERN_LEN, byte_val);
  2846. page_zip_write_blob_ptr(page_zip, rec, index, offsets, i, mtr);
  2847. } else if (UNIV_LIKELY(mtr != NULL)) {
  2848. mlog_write_ulint(data + local_len + BTR_EXTERN_LEN, byte_val,
  2849. MLOG_1BYTE, mtr);
  2850. } else {
  2851. mach_write_to_1(data + local_len + BTR_EXTERN_LEN, byte_val);
  2852. }
  2853. }
  2854. /*******************************************************************//**
  2855. Marks not updated extern fields as not-owned by this record. The ownership
  2856. is transferred to the updated record which is inserted elsewhere in the
  2857. index tree. In purge only the owner of externally stored field is allowed
  2858. to free the field. */
  2859. UNIV_INTERN
  2860. void
  2861. btr_cur_mark_extern_inherited_fields(
  2862. /*=================================*/
  2863. page_zip_des_t* page_zip,/*!< in/out: compressed page whose uncompressed
  2864. part will be updated, or NULL */
  2865. rec_t* rec, /*!< in/out: record in a clustered index */
  2866. dict_index_t* index, /*!< in: index of the page */
  2867. const ulint* offsets,/*!< in: array returned by rec_get_offsets() */
  2868. const upd_t* update, /*!< in: update vector */
  2869. mtr_t* mtr) /*!< in: mtr, or NULL if not logged */
  2870. {
  2871. ulint n;
  2872. ulint j;
  2873. ulint i;
  2874. ut_ad(rec_offs_validate(rec, NULL, offsets));
  2875. ut_ad(!rec_offs_comp(offsets) || !rec_get_node_ptr_flag(rec));
  2876. if (!rec_offs_any_extern(offsets)) {
  2877. return;
  2878. }
  2879. n = rec_offs_n_fields(offsets);
  2880. for (i = 0; i < n; i++) {
  2881. if (rec_offs_nth_extern(offsets, i)) {
  2882. /* Check it is not in updated fields */
  2883. if (update) {
  2884. for (j = 0; j < upd_get_n_fields(update);
  2885. j++) {
  2886. if (upd_get_nth_field(update, j)
  2887. ->field_no == i) {
  2888. goto updated;
  2889. }
  2890. }
  2891. }
  2892. btr_cur_set_ownership_of_extern_field(
  2893. page_zip, rec, index, offsets, i, FALSE, mtr);
  2894. updated:
  2895. ;
  2896. }
  2897. }
  2898. }
  2899. /*******************************************************************//**
  2900. The complement of the previous function: in an update entry may inherit
  2901. some externally stored fields from a record. We must mark them as inherited
  2902. in entry, so that they are not freed in a rollback. */
  2903. UNIV_INTERN
  2904. void
  2905. btr_cur_mark_dtuple_inherited_extern(
  2906. /*=================================*/
  2907. dtuple_t* entry, /*!< in/out: updated entry to be
  2908. inserted to clustered index */
  2909. const upd_t* update) /*!< in: update vector */
  2910. {
  2911. ulint i;
  2912. for (i = 0; i < dtuple_get_n_fields(entry); i++) {
  2913. dfield_t* dfield = dtuple_get_nth_field(entry, i);
  2914. byte* data;
  2915. ulint len;
  2916. ulint j;
  2917. if (!dfield_is_ext(dfield)) {
  2918. continue;
  2919. }
  2920. /* Check if it is in updated fields */
  2921. for (j = 0; j < upd_get_n_fields(update); j++) {
  2922. if (upd_get_nth_field(update, j)->field_no == i) {
  2923. goto is_updated;
  2924. }
  2925. }
  2926. data = dfield_get_data(dfield);
  2927. len = dfield_get_len(dfield);
  2928. data[len - BTR_EXTERN_FIELD_REF_SIZE + BTR_EXTERN_LEN]
  2929. |= BTR_EXTERN_INHERITED_FLAG;
  2930. is_updated:
  2931. ;
  2932. }
  2933. }
  2934. /*******************************************************************//**
  2935. Marks all extern fields in a record as owned by the record. This function
  2936. should be called if the delete mark of a record is removed: a not delete
  2937. marked record always owns all its extern fields. */
  2938. static
  2939. void
  2940. btr_cur_unmark_extern_fields(
  2941. /*=========================*/
  2942. page_zip_des_t* page_zip,/*!< in/out: compressed page whose uncompressed
  2943. part will be updated, or NULL */
  2944. rec_t* rec, /*!< in/out: record in a clustered index */
  2945. dict_index_t* index, /*!< in: index of the page */
  2946. const ulint* offsets,/*!< in: array returned by rec_get_offsets() */
  2947. mtr_t* mtr) /*!< in: mtr, or NULL if not logged */
  2948. {
  2949. ulint n;
  2950. ulint i;
  2951. ut_ad(!rec_offs_comp(offsets) || !rec_get_node_ptr_flag(rec));
  2952. n = rec_offs_n_fields(offsets);
  2953. if (!rec_offs_any_extern(offsets)) {
  2954. return;
  2955. }
  2956. for (i = 0; i < n; i++) {
  2957. if (rec_offs_nth_extern(offsets, i)) {
  2958. btr_cur_set_ownership_of_extern_field(
  2959. page_zip, rec, index, offsets, i, TRUE, mtr);
  2960. }
  2961. }
  2962. }
  2963. /*******************************************************************//**
  2964. Marks all extern fields in a dtuple as owned by the record. */
  2965. UNIV_INTERN
  2966. void
  2967. btr_cur_unmark_dtuple_extern_fields(
  2968. /*================================*/
  2969. dtuple_t* entry) /*!< in/out: clustered index entry */
  2970. {
  2971. ulint i;
  2972. for (i = 0; i < dtuple_get_n_fields(entry); i++) {
  2973. dfield_t* dfield = dtuple_get_nth_field(entry, i);
  2974. if (dfield_is_ext(dfield)) {
  2975. byte* data = dfield_get_data(dfield);
  2976. ulint len = dfield_get_len(dfield);
  2977. data[len - BTR_EXTERN_FIELD_REF_SIZE + BTR_EXTERN_LEN]
  2978. &= ~BTR_EXTERN_OWNER_FLAG;
  2979. }
  2980. }
  2981. }
  2982. /*******************************************************************//**
  2983. Flags the data tuple fields that are marked as extern storage in the
  2984. update vector. We use this function to remember which fields we must
  2985. mark as extern storage in a record inserted for an update.
  2986. @return number of flagged external columns */
  2987. UNIV_INTERN
  2988. ulint
  2989. btr_push_update_extern_fields(
  2990. /*==========================*/
  2991. dtuple_t* tuple, /*!< in/out: data tuple */
  2992. const upd_t* update, /*!< in: update vector */
  2993. mem_heap_t* heap) /*!< in: memory heap */
  2994. {
  2995. ulint n_pushed = 0;
  2996. ulint n;
  2997. const upd_field_t* uf;
  2998. ut_ad(tuple);
  2999. ut_ad(update);
  3000. uf = update->fields;
  3001. n = upd_get_n_fields(update);
  3002. for (; n--; uf++) {
  3003. if (dfield_is_ext(&uf->new_val)) {
  3004. dfield_t* field
  3005. = dtuple_get_nth_field(tuple, uf->field_no);
  3006. if (!dfield_is_ext(field)) {
  3007. dfield_set_ext(field);
  3008. n_pushed++;
  3009. }
  3010. switch (uf->orig_len) {
  3011. byte* data;
  3012. ulint len;
  3013. byte* buf;
  3014. case 0:
  3015. break;
  3016. case BTR_EXTERN_FIELD_REF_SIZE:
  3017. /* Restore the original locally stored
  3018. part of the column. In the undo log,
  3019. InnoDB writes a longer prefix of externally
  3020. stored columns, so that column prefixes
  3021. in secondary indexes can be reconstructed. */
  3022. dfield_set_data(field, (byte*) dfield_get_data(field)
  3023. + dfield_get_len(field)
  3024. - BTR_EXTERN_FIELD_REF_SIZE,
  3025. BTR_EXTERN_FIELD_REF_SIZE);
  3026. dfield_set_ext(field);
  3027. break;
  3028. default:
  3029. /* Reconstruct the original locally
  3030. stored part of the column. The data
  3031. will have to be copied. */
  3032. ut_a(uf->orig_len > BTR_EXTERN_FIELD_REF_SIZE);
  3033. data = dfield_get_data(field);
  3034. len = dfield_get_len(field);
  3035. buf = mem_heap_alloc(heap, uf->orig_len);
  3036. /* Copy the locally stored prefix. */
  3037. memcpy(buf, data,
  3038. uf->orig_len
  3039. - BTR_EXTERN_FIELD_REF_SIZE);
  3040. /* Copy the BLOB pointer. */
  3041. memcpy(buf + uf->orig_len
  3042. - BTR_EXTERN_FIELD_REF_SIZE,
  3043. data + len - BTR_EXTERN_FIELD_REF_SIZE,
  3044. BTR_EXTERN_FIELD_REF_SIZE);
  3045. dfield_set_data(field, buf, uf->orig_len);
  3046. dfield_set_ext(field);
  3047. }
  3048. }
  3049. }
  3050. return(n_pushed);
  3051. }
  3052. /*******************************************************************//**
  3053. Returns the length of a BLOB part stored on the header page.
  3054. @return part length */
  3055. static
  3056. ulint
  3057. btr_blob_get_part_len(
  3058. /*==================*/
  3059. const byte* blob_header) /*!< in: blob header */
  3060. {
  3061. return(mach_read_from_4(blob_header + BTR_BLOB_HDR_PART_LEN));
  3062. }
  3063. /*******************************************************************//**
  3064. Returns the page number where the next BLOB part is stored.
  3065. @return page number or FIL_NULL if no more pages */
  3066. static
  3067. ulint
  3068. btr_blob_get_next_page_no(
  3069. /*======================*/
  3070. const byte* blob_header) /*!< in: blob header */
  3071. {
  3072. return(mach_read_from_4(blob_header + BTR_BLOB_HDR_NEXT_PAGE_NO));
  3073. }
  3074. /*******************************************************************//**
  3075. Deallocate a buffer block that was reserved for a BLOB part. */
  3076. static
  3077. void
  3078. btr_blob_free(
  3079. /*==========*/
  3080. buf_block_t* block, /*!< in: buffer block */
  3081. ibool all, /*!< in: TRUE=remove also the compressed page
  3082. if there is one */
  3083. mtr_t* mtr) /*!< in: mini-transaction to commit */
  3084. {
  3085. ulint space = buf_block_get_space(block);
  3086. ulint page_no = buf_block_get_page_no(block);
  3087. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  3088. mtr_commit(mtr);
  3089. buf_pool_mutex_enter();
  3090. mutex_enter(&block->mutex);
  3091. /* Only free the block if it is still allocated to
  3092. the same file page. */
  3093. if (buf_block_get_state(block)
  3094. == BUF_BLOCK_FILE_PAGE
  3095. && buf_block_get_space(block) == space
  3096. && buf_block_get_page_no(block) == page_no) {
  3097. if (buf_LRU_free_block(&block->page, all, NULL)
  3098. != BUF_LRU_FREED
  3099. && all && block->page.zip.data) {
  3100. /* Attempt to deallocate the uncompressed page
  3101. if the whole block cannot be deallocted. */
  3102. buf_LRU_free_block(&block->page, FALSE, NULL);
  3103. }
  3104. }
  3105. buf_pool_mutex_exit();
  3106. mutex_exit(&block->mutex);
  3107. }
  3108. /*******************************************************************//**
  3109. Stores the fields in big_rec_vec to the tablespace and puts pointers to
  3110. them in rec. The extern flags in rec will have to be set beforehand.
  3111. The fields are stored on pages allocated from leaf node
  3112. file segment of the index tree.
  3113. @return DB_SUCCESS or error */
  3114. UNIV_INTERN
  3115. ulint
  3116. btr_store_big_rec_extern_fields(
  3117. /*============================*/
  3118. dict_index_t* index, /*!< in: index of rec; the index tree
  3119. MUST be X-latched */
  3120. buf_block_t* rec_block, /*!< in/out: block containing rec */
  3121. rec_t* rec, /*!< in/out: record */
  3122. const ulint* offsets, /*!< in: rec_get_offsets(rec, index);
  3123. the "external storage" flags in offsets
  3124. will not correspond to rec when
  3125. this function returns */
  3126. big_rec_t* big_rec_vec, /*!< in: vector containing fields
  3127. to be stored externally */
  3128. mtr_t* local_mtr __attribute__((unused))) /*!< in: mtr
  3129. containing the latch to rec and to the
  3130. tree */
  3131. {
  3132. ulint rec_page_no;
  3133. byte* field_ref;
  3134. ulint extern_len;
  3135. ulint store_len;
  3136. ulint page_no;
  3137. ulint space_id;
  3138. ulint zip_size;
  3139. ulint prev_page_no;
  3140. ulint hint_page_no;
  3141. ulint i;
  3142. mtr_t mtr;
  3143. mem_heap_t* heap = NULL;
  3144. page_zip_des_t* page_zip;
  3145. z_stream c_stream;
  3146. ut_ad(rec_offs_validate(rec, index, offsets));
  3147. ut_ad(mtr_memo_contains(local_mtr, dict_index_get_lock(index),
  3148. MTR_MEMO_X_LOCK));
  3149. ut_ad(mtr_memo_contains(local_mtr, rec_block, MTR_MEMO_PAGE_X_FIX));
  3150. ut_ad(buf_block_get_frame(rec_block) == page_align(rec));
  3151. ut_a(dict_index_is_clust(index));
  3152. page_zip = buf_block_get_page_zip(rec_block);
  3153. ut_a(dict_table_zip_size(index->table)
  3154. == buf_block_get_zip_size(rec_block));
  3155. space_id = buf_block_get_space(rec_block);
  3156. zip_size = buf_block_get_zip_size(rec_block);
  3157. rec_page_no = buf_block_get_page_no(rec_block);
  3158. ut_a(fil_page_get_type(page_align(rec)) == FIL_PAGE_INDEX);
  3159. if (UNIV_LIKELY_NULL(page_zip)) {
  3160. int err;
  3161. /* Zlib deflate needs 128 kilobytes for the default
  3162. window size, plus 512 << memLevel, plus a few
  3163. kilobytes for small objects. We use reduced memLevel
  3164. to limit the memory consumption, and preallocate the
  3165. heap, hoping to avoid memory fragmentation. */
  3166. heap = mem_heap_create(250000);
  3167. page_zip_set_alloc(&c_stream, heap);
  3168. err = deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION,
  3169. Z_DEFLATED, 15, 7, Z_DEFAULT_STRATEGY);
  3170. ut_a(err == Z_OK);
  3171. }
  3172. /* We have to create a file segment to the tablespace
  3173. for each field and put the pointer to the field in rec */
  3174. for (i = 0; i < big_rec_vec->n_fields; i++) {
  3175. ut_ad(rec_offs_nth_extern(offsets,
  3176. big_rec_vec->fields[i].field_no));
  3177. {
  3178. ulint local_len;
  3179. field_ref = rec_get_nth_field(
  3180. rec, offsets, big_rec_vec->fields[i].field_no,
  3181. &local_len);
  3182. ut_a(local_len >= BTR_EXTERN_FIELD_REF_SIZE);
  3183. local_len -= BTR_EXTERN_FIELD_REF_SIZE;
  3184. field_ref += local_len;
  3185. }
  3186. extern_len = big_rec_vec->fields[i].len;
  3187. ut_a(extern_len > 0);
  3188. prev_page_no = FIL_NULL;
  3189. if (UNIV_LIKELY_NULL(page_zip)) {
  3190. int err = deflateReset(&c_stream);
  3191. ut_a(err == Z_OK);
  3192. c_stream.next_in = (void*) big_rec_vec->fields[i].data;
  3193. c_stream.avail_in = extern_len;
  3194. }
  3195. for (;;) {
  3196. buf_block_t* block;
  3197. page_t* page;
  3198. mtr_start(&mtr);
  3199. if (prev_page_no == FIL_NULL) {
  3200. hint_page_no = 1 + rec_page_no;
  3201. } else {
  3202. hint_page_no = prev_page_no + 1;
  3203. }
  3204. block = btr_page_alloc(index, hint_page_no,
  3205. FSP_NO_DIR, 0, &mtr);
  3206. if (UNIV_UNLIKELY(block == NULL)) {
  3207. mtr_commit(&mtr);
  3208. if (UNIV_LIKELY_NULL(page_zip)) {
  3209. deflateEnd(&c_stream);
  3210. mem_heap_free(heap);
  3211. }
  3212. return(DB_OUT_OF_FILE_SPACE);
  3213. }
  3214. page_no = buf_block_get_page_no(block);
  3215. page = buf_block_get_frame(block);
  3216. if (prev_page_no != FIL_NULL) {
  3217. buf_block_t* prev_block;
  3218. page_t* prev_page;
  3219. prev_block = buf_page_get(space_id, zip_size,
  3220. prev_page_no,
  3221. RW_X_LATCH, &mtr);
  3222. buf_block_dbg_add_level(prev_block,
  3223. SYNC_EXTERN_STORAGE);
  3224. prev_page = buf_block_get_frame(prev_block);
  3225. if (UNIV_LIKELY_NULL(page_zip)) {
  3226. mlog_write_ulint(
  3227. prev_page + FIL_PAGE_NEXT,
  3228. page_no, MLOG_4BYTES, &mtr);
  3229. memcpy(buf_block_get_page_zip(
  3230. prev_block)
  3231. ->data + FIL_PAGE_NEXT,
  3232. prev_page + FIL_PAGE_NEXT, 4);
  3233. } else {
  3234. mlog_write_ulint(
  3235. prev_page + FIL_PAGE_DATA
  3236. + BTR_BLOB_HDR_NEXT_PAGE_NO,
  3237. page_no, MLOG_4BYTES, &mtr);
  3238. }
  3239. }
  3240. if (UNIV_LIKELY_NULL(page_zip)) {
  3241. int err;
  3242. page_zip_des_t* blob_page_zip;
  3243. /* Write FIL_PAGE_TYPE to the redo log
  3244. separately, before logging any other
  3245. changes to the page, so that the debug
  3246. assertions in
  3247. recv_parse_or_apply_log_rec_body() can
  3248. be made simpler. Before InnoDB Plugin
  3249. 1.0.4, the initialization of
  3250. FIL_PAGE_TYPE was logged as part of
  3251. the mlog_log_string() below. */
  3252. mlog_write_ulint(page + FIL_PAGE_TYPE,
  3253. prev_page_no == FIL_NULL
  3254. ? FIL_PAGE_TYPE_ZBLOB
  3255. : FIL_PAGE_TYPE_ZBLOB2,
  3256. MLOG_2BYTES, &mtr);
  3257. c_stream.next_out = page
  3258. + FIL_PAGE_DATA;
  3259. c_stream.avail_out
  3260. = page_zip_get_size(page_zip)
  3261. - FIL_PAGE_DATA;
  3262. err = deflate(&c_stream, Z_FINISH);
  3263. ut_a(err == Z_OK || err == Z_STREAM_END);
  3264. ut_a(err == Z_STREAM_END
  3265. || c_stream.avail_out == 0);
  3266. /* Write the "next BLOB page" pointer */
  3267. mlog_write_ulint(page + FIL_PAGE_NEXT,
  3268. FIL_NULL, MLOG_4BYTES, &mtr);
  3269. /* Initialize the unused "prev page" pointer */
  3270. mlog_write_ulint(page + FIL_PAGE_PREV,
  3271. FIL_NULL, MLOG_4BYTES, &mtr);
  3272. /* Write a back pointer to the record
  3273. into the otherwise unused area. This
  3274. information could be useful in
  3275. debugging. Later, we might want to
  3276. implement the possibility to relocate
  3277. BLOB pages. Then, we would need to be
  3278. able to adjust the BLOB pointer in the
  3279. record. We do not store the heap
  3280. number of the record, because it can
  3281. change in page_zip_reorganize() or
  3282. btr_page_reorganize(). However, also
  3283. the page number of the record may
  3284. change when B-tree nodes are split or
  3285. merged. */
  3286. mlog_write_ulint(page
  3287. + FIL_PAGE_FILE_FLUSH_LSN,
  3288. space_id,
  3289. MLOG_4BYTES, &mtr);
  3290. mlog_write_ulint(page
  3291. + FIL_PAGE_FILE_FLUSH_LSN + 4,
  3292. rec_page_no,
  3293. MLOG_4BYTES, &mtr);
  3294. /* Zero out the unused part of the page. */
  3295. memset(page + page_zip_get_size(page_zip)
  3296. - c_stream.avail_out,
  3297. 0, c_stream.avail_out);
  3298. mlog_log_string(page + FIL_PAGE_FILE_FLUSH_LSN,
  3299. page_zip_get_size(page_zip)
  3300. - FIL_PAGE_FILE_FLUSH_LSN,
  3301. &mtr);
  3302. /* Copy the page to compressed storage,
  3303. because it will be flushed to disk
  3304. from there. */
  3305. blob_page_zip = buf_block_get_page_zip(block);
  3306. ut_ad(blob_page_zip);
  3307. ut_ad(page_zip_get_size(blob_page_zip)
  3308. == page_zip_get_size(page_zip));
  3309. memcpy(blob_page_zip->data, page,
  3310. page_zip_get_size(page_zip));
  3311. if (err == Z_OK && prev_page_no != FIL_NULL) {
  3312. goto next_zip_page;
  3313. }
  3314. rec_block = buf_page_get(space_id, zip_size,
  3315. rec_page_no,
  3316. RW_X_LATCH, &mtr);
  3317. buf_block_dbg_add_level(rec_block,
  3318. SYNC_NO_ORDER_CHECK);
  3319. if (err == Z_STREAM_END) {
  3320. mach_write_to_4(field_ref
  3321. + BTR_EXTERN_LEN, 0);
  3322. mach_write_to_4(field_ref
  3323. + BTR_EXTERN_LEN + 4,
  3324. c_stream.total_in);
  3325. } else {
  3326. memset(field_ref + BTR_EXTERN_LEN,
  3327. 0, 8);
  3328. }
  3329. if (prev_page_no == FIL_NULL) {
  3330. mach_write_to_4(field_ref
  3331. + BTR_EXTERN_SPACE_ID,
  3332. space_id);
  3333. mach_write_to_4(field_ref
  3334. + BTR_EXTERN_PAGE_NO,
  3335. page_no);
  3336. mach_write_to_4(field_ref
  3337. + BTR_EXTERN_OFFSET,
  3338. FIL_PAGE_NEXT);
  3339. }
  3340. page_zip_write_blob_ptr(
  3341. page_zip, rec, index, offsets,
  3342. big_rec_vec->fields[i].field_no, &mtr);
  3343. next_zip_page:
  3344. prev_page_no = page_no;
  3345. /* Commit mtr and release the
  3346. uncompressed page frame to save memory. */
  3347. btr_blob_free(block, FALSE, &mtr);
  3348. if (err == Z_STREAM_END) {
  3349. break;
  3350. }
  3351. } else {
  3352. mlog_write_ulint(page + FIL_PAGE_TYPE,
  3353. FIL_PAGE_TYPE_BLOB,
  3354. MLOG_2BYTES, &mtr);
  3355. if (extern_len > (UNIV_PAGE_SIZE
  3356. - FIL_PAGE_DATA
  3357. - BTR_BLOB_HDR_SIZE
  3358. - FIL_PAGE_DATA_END)) {
  3359. store_len = UNIV_PAGE_SIZE
  3360. - FIL_PAGE_DATA
  3361. - BTR_BLOB_HDR_SIZE
  3362. - FIL_PAGE_DATA_END;
  3363. } else {
  3364. store_len = extern_len;
  3365. }
  3366. mlog_write_string(page + FIL_PAGE_DATA
  3367. + BTR_BLOB_HDR_SIZE,
  3368. (const byte*)
  3369. big_rec_vec->fields[i].data
  3370. + big_rec_vec->fields[i].len
  3371. - extern_len,
  3372. store_len, &mtr);
  3373. mlog_write_ulint(page + FIL_PAGE_DATA
  3374. + BTR_BLOB_HDR_PART_LEN,
  3375. store_len, MLOG_4BYTES, &mtr);
  3376. mlog_write_ulint(page + FIL_PAGE_DATA
  3377. + BTR_BLOB_HDR_NEXT_PAGE_NO,
  3378. FIL_NULL, MLOG_4BYTES, &mtr);
  3379. extern_len -= store_len;
  3380. rec_block = buf_page_get(space_id, zip_size,
  3381. rec_page_no,
  3382. RW_X_LATCH, &mtr);
  3383. buf_block_dbg_add_level(rec_block,
  3384. SYNC_NO_ORDER_CHECK);
  3385. mlog_write_ulint(field_ref + BTR_EXTERN_LEN, 0,
  3386. MLOG_4BYTES, &mtr);
  3387. mlog_write_ulint(field_ref
  3388. + BTR_EXTERN_LEN + 4,
  3389. big_rec_vec->fields[i].len
  3390. - extern_len,
  3391. MLOG_4BYTES, &mtr);
  3392. if (prev_page_no == FIL_NULL) {
  3393. mlog_write_ulint(field_ref
  3394. + BTR_EXTERN_SPACE_ID,
  3395. space_id,
  3396. MLOG_4BYTES, &mtr);
  3397. mlog_write_ulint(field_ref
  3398. + BTR_EXTERN_PAGE_NO,
  3399. page_no,
  3400. MLOG_4BYTES, &mtr);
  3401. mlog_write_ulint(field_ref
  3402. + BTR_EXTERN_OFFSET,
  3403. FIL_PAGE_DATA,
  3404. MLOG_4BYTES, &mtr);
  3405. }
  3406. prev_page_no = page_no;
  3407. mtr_commit(&mtr);
  3408. if (extern_len == 0) {
  3409. break;
  3410. }
  3411. }
  3412. }
  3413. }
  3414. if (UNIV_LIKELY_NULL(page_zip)) {
  3415. deflateEnd(&c_stream);
  3416. mem_heap_free(heap);
  3417. }
  3418. return(DB_SUCCESS);
  3419. }
  3420. /*******************************************************************//**
  3421. Check the FIL_PAGE_TYPE on an uncompressed BLOB page. */
  3422. static
  3423. void
  3424. btr_check_blob_fil_page_type(
  3425. /*=========================*/
  3426. ulint space_id, /*!< in: space id */
  3427. ulint page_no, /*!< in: page number */
  3428. const page_t* page, /*!< in: page */
  3429. ibool read) /*!< in: TRUE=read, FALSE=purge */
  3430. {
  3431. ulint type = fil_page_get_type(page);
  3432. ut_a(space_id == page_get_space_id(page));
  3433. ut_a(page_no == page_get_page_no(page));
  3434. if (UNIV_UNLIKELY(type != FIL_PAGE_TYPE_BLOB)) {
  3435. ulint flags = fil_space_get_flags(space_id);
  3436. if (UNIV_LIKELY
  3437. ((flags & DICT_TF_FORMAT_MASK) == DICT_TF_FORMAT_51)) {
  3438. /* Old versions of InnoDB did not initialize
  3439. FIL_PAGE_TYPE on BLOB pages. Do not print
  3440. anything about the type mismatch when reading
  3441. a BLOB page that is in Antelope format.*/
  3442. return;
  3443. }
  3444. ut_print_timestamp(stderr);
  3445. fprintf(stderr,
  3446. " InnoDB: FIL_PAGE_TYPE=%lu"
  3447. " on BLOB %s space %lu page %lu flags %lx\n",
  3448. (ulong) type, read ? "read" : "purge",
  3449. (ulong) space_id, (ulong) page_no, (ulong) flags);
  3450. ut_error;
  3451. }
  3452. }
  3453. /*******************************************************************//**
  3454. Frees the space in an externally stored field to the file space
  3455. management if the field in data is owned by the externally stored field,
  3456. in a rollback we may have the additional condition that the field must
  3457. not be inherited. */
  3458. UNIV_INTERN
  3459. void
  3460. btr_free_externally_stored_field(
  3461. /*=============================*/
  3462. dict_index_t* index, /*!< in: index of the data, the index
  3463. tree MUST be X-latched; if the tree
  3464. height is 1, then also the root page
  3465. must be X-latched! (this is relevant
  3466. in the case this function is called
  3467. from purge where 'data' is located on
  3468. an undo log page, not an index
  3469. page) */
  3470. byte* field_ref, /*!< in/out: field reference */
  3471. const rec_t* rec, /*!< in: record containing field_ref, for
  3472. page_zip_write_blob_ptr(), or NULL */
  3473. const ulint* offsets, /*!< in: rec_get_offsets(rec, index),
  3474. or NULL */
  3475. page_zip_des_t* page_zip, /*!< in: compressed page corresponding
  3476. to rec, or NULL if rec == NULL */
  3477. ulint i, /*!< in: field number of field_ref;
  3478. ignored if rec == NULL */
  3479. enum trx_rb_ctx rb_ctx, /*!< in: rollback context */
  3480. mtr_t* local_mtr __attribute__((unused))) /*!< in: mtr
  3481. containing the latch to data an an
  3482. X-latch to the index tree */
  3483. {
  3484. page_t* page;
  3485. ulint space_id;
  3486. ulint rec_zip_size = dict_table_zip_size(index->table);
  3487. ulint ext_zip_size;
  3488. ulint page_no;
  3489. ulint next_page_no;
  3490. mtr_t mtr;
  3491. #ifdef UNIV_DEBUG
  3492. ut_ad(mtr_memo_contains(local_mtr, dict_index_get_lock(index),
  3493. MTR_MEMO_X_LOCK));
  3494. ut_ad(mtr_memo_contains_page(local_mtr, field_ref,
  3495. MTR_MEMO_PAGE_X_FIX));
  3496. ut_ad(!rec || rec_offs_validate(rec, index, offsets));
  3497. if (rec) {
  3498. ulint local_len;
  3499. const byte* f = rec_get_nth_field(rec, offsets,
  3500. i, &local_len);
  3501. ut_a(local_len >= BTR_EXTERN_FIELD_REF_SIZE);
  3502. local_len -= BTR_EXTERN_FIELD_REF_SIZE;
  3503. f += local_len;
  3504. ut_ad(f == field_ref);
  3505. }
  3506. #endif /* UNIV_DEBUG */
  3507. if (UNIV_UNLIKELY(!memcmp(field_ref, field_ref_zero,
  3508. BTR_EXTERN_FIELD_REF_SIZE))) {
  3509. /* In the rollback of uncommitted transactions, we may
  3510. encounter a clustered index record whose BLOBs have
  3511. not been written. There is nothing to free then. */
  3512. ut_a(rb_ctx == RB_RECOVERY || rb_ctx == RB_RECOVERY_PURGE_REC);
  3513. return;
  3514. }
  3515. space_id = mach_read_from_4(field_ref + BTR_EXTERN_SPACE_ID);
  3516. if (UNIV_UNLIKELY(space_id != dict_index_get_space(index))) {
  3517. ext_zip_size = fil_space_get_zip_size(space_id);
  3518. /* This must be an undo log record in the system tablespace,
  3519. that is, in row_purge_upd_exist_or_extern().
  3520. Currently, externally stored records are stored in the
  3521. same tablespace as the referring records. */
  3522. ut_ad(!page_get_space_id(page_align(field_ref)));
  3523. ut_ad(!rec);
  3524. ut_ad(!page_zip);
  3525. } else {
  3526. ext_zip_size = rec_zip_size;
  3527. }
  3528. if (!rec) {
  3529. /* This is a call from row_purge_upd_exist_or_extern(). */
  3530. ut_ad(!page_zip);
  3531. rec_zip_size = 0;
  3532. }
  3533. for (;;) {
  3534. buf_block_t* rec_block;
  3535. buf_block_t* ext_block;
  3536. mtr_start(&mtr);
  3537. rec_block = buf_page_get(page_get_space_id(
  3538. page_align(field_ref)),
  3539. rec_zip_size,
  3540. page_get_page_no(
  3541. page_align(field_ref)),
  3542. RW_X_LATCH, &mtr);
  3543. buf_block_dbg_add_level(rec_block, SYNC_NO_ORDER_CHECK);
  3544. page_no = mach_read_from_4(field_ref + BTR_EXTERN_PAGE_NO);
  3545. if (/* There is no external storage data */
  3546. page_no == FIL_NULL
  3547. /* This field does not own the externally stored field */
  3548. || (mach_read_from_1(field_ref + BTR_EXTERN_LEN)
  3549. & BTR_EXTERN_OWNER_FLAG)
  3550. /* Rollback and inherited field */
  3551. || ((rb_ctx == RB_NORMAL || rb_ctx == RB_RECOVERY)
  3552. && (mach_read_from_1(field_ref + BTR_EXTERN_LEN)
  3553. & BTR_EXTERN_INHERITED_FLAG))) {
  3554. /* Do not free */
  3555. mtr_commit(&mtr);
  3556. return;
  3557. }
  3558. ext_block = buf_page_get(space_id, ext_zip_size, page_no,
  3559. RW_X_LATCH, &mtr);
  3560. buf_block_dbg_add_level(ext_block, SYNC_EXTERN_STORAGE);
  3561. page = buf_block_get_frame(ext_block);
  3562. if (ext_zip_size) {
  3563. /* Note that page_zip will be NULL
  3564. in row_purge_upd_exist_or_extern(). */
  3565. switch (fil_page_get_type(page)) {
  3566. case FIL_PAGE_TYPE_ZBLOB:
  3567. case FIL_PAGE_TYPE_ZBLOB2:
  3568. break;
  3569. default:
  3570. ut_error;
  3571. }
  3572. next_page_no = mach_read_from_4(page + FIL_PAGE_NEXT);
  3573. btr_page_free_low(index, ext_block, 0, &mtr);
  3574. if (UNIV_LIKELY(page_zip != NULL)) {
  3575. mach_write_to_4(field_ref + BTR_EXTERN_PAGE_NO,
  3576. next_page_no);
  3577. mach_write_to_4(field_ref + BTR_EXTERN_LEN + 4,
  3578. 0);
  3579. page_zip_write_blob_ptr(page_zip, rec, index,
  3580. offsets, i, &mtr);
  3581. } else {
  3582. mlog_write_ulint(field_ref
  3583. + BTR_EXTERN_PAGE_NO,
  3584. next_page_no,
  3585. MLOG_4BYTES, &mtr);
  3586. mlog_write_ulint(field_ref
  3587. + BTR_EXTERN_LEN + 4, 0,
  3588. MLOG_4BYTES, &mtr);
  3589. }
  3590. } else {
  3591. ut_a(!page_zip);
  3592. btr_check_blob_fil_page_type(space_id, page_no, page,
  3593. FALSE);
  3594. next_page_no = mach_read_from_4(
  3595. page + FIL_PAGE_DATA
  3596. + BTR_BLOB_HDR_NEXT_PAGE_NO);
  3597. /* We must supply the page level (= 0) as an argument
  3598. because we did not store it on the page (we save the
  3599. space overhead from an index page header. */
  3600. btr_page_free_low(index, ext_block, 0, &mtr);
  3601. mlog_write_ulint(field_ref + BTR_EXTERN_PAGE_NO,
  3602. next_page_no,
  3603. MLOG_4BYTES, &mtr);
  3604. /* Zero out the BLOB length. If the server
  3605. crashes during the execution of this function,
  3606. trx_rollback_or_clean_all_recovered() could
  3607. dereference the half-deleted BLOB, fetching a
  3608. wrong prefix for the BLOB. */
  3609. mlog_write_ulint(field_ref + BTR_EXTERN_LEN + 4,
  3610. 0,
  3611. MLOG_4BYTES, &mtr);
  3612. }
  3613. /* Commit mtr and release the BLOB block to save memory. */
  3614. btr_blob_free(ext_block, TRUE, &mtr);
  3615. }
  3616. }
  3617. /***********************************************************//**
  3618. Frees the externally stored fields for a record. */
  3619. static
  3620. void
  3621. btr_rec_free_externally_stored_fields(
  3622. /*==================================*/
  3623. dict_index_t* index, /*!< in: index of the data, the index
  3624. tree MUST be X-latched */
  3625. rec_t* rec, /*!< in/out: record */
  3626. const ulint* offsets,/*!< in: rec_get_offsets(rec, index) */
  3627. page_zip_des_t* page_zip,/*!< in: compressed page whose uncompressed
  3628. part will be updated, or NULL */
  3629. enum trx_rb_ctx rb_ctx, /*!< in: rollback context */
  3630. mtr_t* mtr) /*!< in: mini-transaction handle which contains
  3631. an X-latch to record page and to the index
  3632. tree */
  3633. {
  3634. ulint n_fields;
  3635. ulint i;
  3636. ut_ad(rec_offs_validate(rec, index, offsets));
  3637. ut_ad(mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_X_FIX));
  3638. /* Free possible externally stored fields in the record */
  3639. ut_ad(dict_table_is_comp(index->table) == !!rec_offs_comp(offsets));
  3640. n_fields = rec_offs_n_fields(offsets);
  3641. for (i = 0; i < n_fields; i++) {
  3642. if (rec_offs_nth_extern(offsets, i)) {
  3643. ulint len;
  3644. byte* data
  3645. = rec_get_nth_field(rec, offsets, i, &len);
  3646. ut_a(len >= BTR_EXTERN_FIELD_REF_SIZE);
  3647. btr_free_externally_stored_field(
  3648. index, data + len - BTR_EXTERN_FIELD_REF_SIZE,
  3649. rec, offsets, page_zip, i, rb_ctx, mtr);
  3650. }
  3651. }
  3652. }
  3653. /***********************************************************//**
  3654. Frees the externally stored fields for a record, if the field is mentioned
  3655. in the update vector. */
  3656. static
  3657. void
  3658. btr_rec_free_updated_extern_fields(
  3659. /*===============================*/
  3660. dict_index_t* index, /*!< in: index of rec; the index tree MUST be
  3661. X-latched */
  3662. rec_t* rec, /*!< in/out: record */
  3663. page_zip_des_t* page_zip,/*!< in: compressed page whose uncompressed
  3664. part will be updated, or NULL */
  3665. const ulint* offsets,/*!< in: rec_get_offsets(rec, index) */
  3666. const upd_t* update, /*!< in: update vector */
  3667. enum trx_rb_ctx rb_ctx, /*!< in: rollback context */
  3668. mtr_t* mtr) /*!< in: mini-transaction handle which contains
  3669. an X-latch to record page and to the tree */
  3670. {
  3671. ulint n_fields;
  3672. ulint i;
  3673. ut_ad(rec_offs_validate(rec, index, offsets));
  3674. ut_ad(mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_X_FIX));
  3675. /* Free possible externally stored fields in the record */
  3676. n_fields = upd_get_n_fields(update);
  3677. for (i = 0; i < n_fields; i++) {
  3678. const upd_field_t* ufield = upd_get_nth_field(update, i);
  3679. if (rec_offs_nth_extern(offsets, ufield->field_no)) {
  3680. ulint len;
  3681. byte* data = rec_get_nth_field(
  3682. rec, offsets, ufield->field_no, &len);
  3683. ut_a(len >= BTR_EXTERN_FIELD_REF_SIZE);
  3684. btr_free_externally_stored_field(
  3685. index, data + len - BTR_EXTERN_FIELD_REF_SIZE,
  3686. rec, offsets, page_zip,
  3687. ufield->field_no, rb_ctx, mtr);
  3688. }
  3689. }
  3690. }
  3691. /*******************************************************************//**
  3692. Copies the prefix of an uncompressed BLOB. The clustered index record
  3693. that points to this BLOB must be protected by a lock or a page latch.
  3694. @return number of bytes written to buf */
  3695. static
  3696. ulint
  3697. btr_copy_blob_prefix(
  3698. /*=================*/
  3699. byte* buf, /*!< out: the externally stored part of
  3700. the field, or a prefix of it */
  3701. ulint len, /*!< in: length of buf, in bytes */
  3702. ulint space_id,/*!< in: space id of the BLOB pages */
  3703. ulint page_no,/*!< in: page number of the first BLOB page */
  3704. ulint offset) /*!< in: offset on the first BLOB page */
  3705. {
  3706. ulint copied_len = 0;
  3707. for (;;) {
  3708. mtr_t mtr;
  3709. buf_block_t* block;
  3710. const page_t* page;
  3711. const byte* blob_header;
  3712. ulint part_len;
  3713. ulint copy_len;
  3714. mtr_start(&mtr);
  3715. block = buf_page_get(space_id, 0, page_no, RW_S_LATCH, &mtr);
  3716. buf_block_dbg_add_level(block, SYNC_EXTERN_STORAGE);
  3717. page = buf_block_get_frame(block);
  3718. btr_check_blob_fil_page_type(space_id, page_no, page, TRUE);
  3719. blob_header = page + offset;
  3720. part_len = btr_blob_get_part_len(blob_header);
  3721. copy_len = ut_min(part_len, len - copied_len);
  3722. memcpy(buf + copied_len,
  3723. blob_header + BTR_BLOB_HDR_SIZE, copy_len);
  3724. copied_len += copy_len;
  3725. page_no = btr_blob_get_next_page_no(blob_header);
  3726. mtr_commit(&mtr);
  3727. if (page_no == FIL_NULL || copy_len != part_len) {
  3728. return(copied_len);
  3729. }
  3730. /* On other BLOB pages except the first the BLOB header
  3731. always is at the page data start: */
  3732. offset = FIL_PAGE_DATA;
  3733. ut_ad(copied_len <= len);
  3734. }
  3735. }
  3736. /*******************************************************************//**
  3737. Copies the prefix of a compressed BLOB. The clustered index record
  3738. that points to this BLOB must be protected by a lock or a page latch. */
  3739. static
  3740. void
  3741. btr_copy_zblob_prefix(
  3742. /*==================*/
  3743. z_stream* d_stream,/*!< in/out: the decompressing stream */
  3744. ulint zip_size,/*!< in: compressed BLOB page size */
  3745. ulint space_id,/*!< in: space id of the BLOB pages */
  3746. ulint page_no,/*!< in: page number of the first BLOB page */
  3747. ulint offset) /*!< in: offset on the first BLOB page */
  3748. {
  3749. ulint page_type = FIL_PAGE_TYPE_ZBLOB;
  3750. ut_ad(ut_is_2pow(zip_size));
  3751. ut_ad(zip_size >= PAGE_ZIP_MIN_SIZE);
  3752. ut_ad(zip_size <= UNIV_PAGE_SIZE);
  3753. ut_ad(space_id);
  3754. for (;;) {
  3755. buf_page_t* bpage;
  3756. int err;
  3757. ulint next_page_no;
  3758. /* There is no latch on bpage directly. Instead,
  3759. bpage is protected by the B-tree page latch that
  3760. is being held on the clustered index record, or,
  3761. in row_merge_copy_blobs(), by an exclusive table lock. */
  3762. bpage = buf_page_get_zip(space_id, zip_size, page_no);
  3763. if (UNIV_UNLIKELY(!bpage)) {
  3764. ut_print_timestamp(stderr);
  3765. fprintf(stderr,
  3766. " InnoDB: Cannot load"
  3767. " compressed BLOB"
  3768. " page %lu space %lu\n",
  3769. (ulong) page_no, (ulong) space_id);
  3770. return;
  3771. }
  3772. if (UNIV_UNLIKELY
  3773. (fil_page_get_type(bpage->zip.data) != page_type)) {
  3774. ut_print_timestamp(stderr);
  3775. fprintf(stderr,
  3776. " InnoDB: Unexpected type %lu of"
  3777. " compressed BLOB"
  3778. " page %lu space %lu\n",
  3779. (ulong) fil_page_get_type(bpage->zip.data),
  3780. (ulong) page_no, (ulong) space_id);
  3781. goto end_of_blob;
  3782. }
  3783. next_page_no = mach_read_from_4(bpage->zip.data + offset);
  3784. if (UNIV_LIKELY(offset == FIL_PAGE_NEXT)) {
  3785. /* When the BLOB begins at page header,
  3786. the compressed data payload does not
  3787. immediately follow the next page pointer. */
  3788. offset = FIL_PAGE_DATA;
  3789. } else {
  3790. offset += 4;
  3791. }
  3792. d_stream->next_in = bpage->zip.data + offset;
  3793. d_stream->avail_in = zip_size - offset;
  3794. err = inflate(d_stream, Z_NO_FLUSH);
  3795. switch (err) {
  3796. case Z_OK:
  3797. if (!d_stream->avail_out) {
  3798. goto end_of_blob;
  3799. }
  3800. break;
  3801. case Z_STREAM_END:
  3802. if (next_page_no == FIL_NULL) {
  3803. goto end_of_blob;
  3804. }
  3805. /* fall through */
  3806. default:
  3807. inflate_error:
  3808. ut_print_timestamp(stderr);
  3809. fprintf(stderr,
  3810. " InnoDB: inflate() of"
  3811. " compressed BLOB"
  3812. " page %lu space %lu returned %d (%s)\n",
  3813. (ulong) page_no, (ulong) space_id,
  3814. err, d_stream->msg);
  3815. case Z_BUF_ERROR:
  3816. goto end_of_blob;
  3817. }
  3818. if (next_page_no == FIL_NULL) {
  3819. if (!d_stream->avail_in) {
  3820. ut_print_timestamp(stderr);
  3821. fprintf(stderr,
  3822. " InnoDB: unexpected end of"
  3823. " compressed BLOB"
  3824. " page %lu space %lu\n",
  3825. (ulong) page_no,
  3826. (ulong) space_id);
  3827. } else {
  3828. err = inflate(d_stream, Z_FINISH);
  3829. switch (err) {
  3830. case Z_STREAM_END:
  3831. case Z_BUF_ERROR:
  3832. break;
  3833. default:
  3834. goto inflate_error;
  3835. }
  3836. }
  3837. end_of_blob:
  3838. buf_page_release_zip(bpage);
  3839. return;
  3840. }
  3841. buf_page_release_zip(bpage);
  3842. /* On other BLOB pages except the first
  3843. the BLOB header always is at the page header: */
  3844. page_no = next_page_no;
  3845. offset = FIL_PAGE_NEXT;
  3846. page_type = FIL_PAGE_TYPE_ZBLOB2;
  3847. }
  3848. }
  3849. /*******************************************************************//**
  3850. Copies the prefix of an externally stored field of a record. The
  3851. clustered index record that points to this BLOB must be protected by a
  3852. lock or a page latch.
  3853. @return number of bytes written to buf */
  3854. static
  3855. ulint
  3856. btr_copy_externally_stored_field_prefix_low(
  3857. /*========================================*/
  3858. byte* buf, /*!< out: the externally stored part of
  3859. the field, or a prefix of it */
  3860. ulint len, /*!< in: length of buf, in bytes */
  3861. ulint zip_size,/*!< in: nonzero=compressed BLOB page size,
  3862. zero for uncompressed BLOBs */
  3863. ulint space_id,/*!< in: space id of the first BLOB page */
  3864. ulint page_no,/*!< in: page number of the first BLOB page */
  3865. ulint offset) /*!< in: offset on the first BLOB page */
  3866. {
  3867. if (UNIV_UNLIKELY(len == 0)) {
  3868. return(0);
  3869. }
  3870. if (UNIV_UNLIKELY(zip_size)) {
  3871. int err;
  3872. z_stream d_stream;
  3873. mem_heap_t* heap;
  3874. /* Zlib inflate needs 32 kilobytes for the default
  3875. window size, plus a few kilobytes for small objects. */
  3876. heap = mem_heap_create(40000);
  3877. page_zip_set_alloc(&d_stream, heap);
  3878. err = inflateInit(&d_stream);
  3879. ut_a(err == Z_OK);
  3880. d_stream.next_out = buf;
  3881. d_stream.avail_out = len;
  3882. d_stream.avail_in = 0;
  3883. btr_copy_zblob_prefix(&d_stream, zip_size,
  3884. space_id, page_no, offset);
  3885. inflateEnd(&d_stream);
  3886. mem_heap_free(heap);
  3887. return(d_stream.total_out);
  3888. } else {
  3889. return(btr_copy_blob_prefix(buf, len, space_id,
  3890. page_no, offset));
  3891. }
  3892. }
  3893. /*******************************************************************//**
  3894. Copies the prefix of an externally stored field of a record. The
  3895. clustered index record must be protected by a lock or a page latch.
  3896. @return the length of the copied field, or 0 if the column was being
  3897. or has been deleted */
  3898. UNIV_INTERN
  3899. ulint
  3900. btr_copy_externally_stored_field_prefix(
  3901. /*====================================*/
  3902. byte* buf, /*!< out: the field, or a prefix of it */
  3903. ulint len, /*!< in: length of buf, in bytes */
  3904. ulint zip_size,/*!< in: nonzero=compressed BLOB page size,
  3905. zero for uncompressed BLOBs */
  3906. const byte* data, /*!< in: 'internally' stored part of the
  3907. field containing also the reference to
  3908. the external part; must be protected by
  3909. a lock or a page latch */
  3910. ulint local_len)/*!< in: length of data, in bytes */
  3911. {
  3912. ulint space_id;
  3913. ulint page_no;
  3914. ulint offset;
  3915. ut_a(local_len >= BTR_EXTERN_FIELD_REF_SIZE);
  3916. local_len -= BTR_EXTERN_FIELD_REF_SIZE;
  3917. if (UNIV_UNLIKELY(local_len >= len)) {
  3918. memcpy(buf, data, len);
  3919. return(len);
  3920. }
  3921. memcpy(buf, data, local_len);
  3922. data += local_len;
  3923. ut_a(memcmp(data, field_ref_zero, BTR_EXTERN_FIELD_REF_SIZE));
  3924. if (!mach_read_from_4(data + BTR_EXTERN_LEN + 4)) {
  3925. /* The externally stored part of the column has been
  3926. (partially) deleted. Signal the half-deleted BLOB
  3927. to the caller. */
  3928. return(0);
  3929. }
  3930. space_id = mach_read_from_4(data + BTR_EXTERN_SPACE_ID);
  3931. page_no = mach_read_from_4(data + BTR_EXTERN_PAGE_NO);
  3932. offset = mach_read_from_4(data + BTR_EXTERN_OFFSET);
  3933. return(local_len
  3934. + btr_copy_externally_stored_field_prefix_low(buf + local_len,
  3935. len - local_len,
  3936. zip_size,
  3937. space_id, page_no,
  3938. offset));
  3939. }
  3940. /*******************************************************************//**
  3941. Copies an externally stored field of a record to mem heap. The
  3942. clustered index record must be protected by a lock or a page latch.
  3943. @return the whole field copied to heap */
  3944. static
  3945. byte*
  3946. btr_copy_externally_stored_field(
  3947. /*=============================*/
  3948. ulint* len, /*!< out: length of the whole field */
  3949. const byte* data, /*!< in: 'internally' stored part of the
  3950. field containing also the reference to
  3951. the external part; must be protected by
  3952. a lock or a page latch */
  3953. ulint zip_size,/*!< in: nonzero=compressed BLOB page size,
  3954. zero for uncompressed BLOBs */
  3955. ulint local_len,/*!< in: length of data */
  3956. mem_heap_t* heap) /*!< in: mem heap */
  3957. {
  3958. ulint space_id;
  3959. ulint page_no;
  3960. ulint offset;
  3961. ulint extern_len;
  3962. byte* buf;
  3963. ut_a(local_len >= BTR_EXTERN_FIELD_REF_SIZE);
  3964. local_len -= BTR_EXTERN_FIELD_REF_SIZE;
  3965. space_id = mach_read_from_4(data + local_len + BTR_EXTERN_SPACE_ID);
  3966. page_no = mach_read_from_4(data + local_len + BTR_EXTERN_PAGE_NO);
  3967. offset = mach_read_from_4(data + local_len + BTR_EXTERN_OFFSET);
  3968. /* Currently a BLOB cannot be bigger than 4 GB; we
  3969. leave the 4 upper bytes in the length field unused */
  3970. extern_len = mach_read_from_4(data + local_len + BTR_EXTERN_LEN + 4);
  3971. buf = mem_heap_alloc(heap, local_len + extern_len);
  3972. memcpy(buf, data, local_len);
  3973. *len = local_len
  3974. + btr_copy_externally_stored_field_prefix_low(buf + local_len,
  3975. extern_len,
  3976. zip_size,
  3977. space_id,
  3978. page_no, offset);
  3979. return(buf);
  3980. }
  3981. /*******************************************************************//**
  3982. Copies an externally stored field of a record to mem heap.
  3983. @return the field copied to heap */
  3984. UNIV_INTERN
  3985. byte*
  3986. btr_rec_copy_externally_stored_field(
  3987. /*=================================*/
  3988. const rec_t* rec, /*!< in: record in a clustered index;
  3989. must be protected by a lock or a page latch */
  3990. const ulint* offsets,/*!< in: array returned by rec_get_offsets() */
  3991. ulint zip_size,/*!< in: nonzero=compressed BLOB page size,
  3992. zero for uncompressed BLOBs */
  3993. ulint no, /*!< in: field number */
  3994. ulint* len, /*!< out: length of the field */
  3995. mem_heap_t* heap) /*!< in: mem heap */
  3996. {
  3997. ulint local_len;
  3998. const byte* data;
  3999. ut_a(rec_offs_nth_extern(offsets, no));
  4000. /* An externally stored field can contain some initial
  4001. data from the field, and in the last 20 bytes it has the
  4002. space id, page number, and offset where the rest of the
  4003. field data is stored, and the data length in addition to
  4004. the data stored locally. We may need to store some data
  4005. locally to get the local record length above the 128 byte
  4006. limit so that field offsets are stored in two bytes, and
  4007. the extern bit is available in those two bytes. */
  4008. data = rec_get_nth_field(rec, offsets, no, &local_len);
  4009. return(btr_copy_externally_stored_field(len, data,
  4010. zip_size, local_len, heap));
  4011. }
  4012. #endif /* !UNIV_HOTBACKUP */