diff options
author | unknown <monty@donna.mysql.com> | 2001-02-17 14:19:19 +0200 |
---|---|---|
committer | unknown <monty@donna.mysql.com> | 2001-02-17 14:19:19 +0200 |
commit | 2662b59306ef0cd495fa6e2edf7129e58a11393a (patch) | |
tree | bfe39951a73e906579ab819bf5198ad8f3a64a36 /sql | |
parent | 66de55a56bdcf2f7a9c0c4f8e19b3e761475e202 (diff) | |
download | mariadb-git-2662b59306ef0cd495fa6e2edf7129e58a11393a.tar.gz |
Added Innobase to source distribution
Docs/manual.texi:
Added Innobase documentation
configure.in:
Incremented version
include/my_base.h:
Added option for Innobase
myisam/mi_check.c:
cleanup
mysql-test/t/bdb.test:
cleanup
mysql-test/t/innobase.test:
Extended with new tests from bdb.test
mysql-test/t/merge.test:
Added test of SHOW create
mysys/my_init.c:
Fix for UNIXWARE 7
scripts/mysql_install_db.sh:
Always write how to start mysqld
scripts/safe_mysqld.sh:
Fixed type
sql/ha_innobase.cc:
Update to new version
sql/ha_innobase.h:
Update to new version
sql/handler.h:
Added 'update_table_comment()' and 'append_create_info()'
sql/sql_delete.cc:
Fixes for Innobase
sql/sql_select.cc:
Fixes for Innobase
sql/sql_show.cc:
Append create information (for MERGE tables)
sql/sql_update.cc:
Fixes for Innobase
Diffstat (limited to 'sql')
-rw-r--r-- | sql/ha_innobase.cc | 1684 | ||||
-rw-r--r-- | sql/ha_innobase.h | 37 | ||||
-rw-r--r-- | sql/handler.h | 10 | ||||
-rw-r--r-- | sql/sql_delete.cc | 4 | ||||
-rw-r--r-- | sql/sql_select.cc | 5 | ||||
-rw-r--r-- | sql/sql_show.cc | 27 | ||||
-rw-r--r-- | sql/sql_update.cc | 65 |
7 files changed, 937 insertions, 895 deletions
diff --git a/sql/ha_innobase.cc b/sql/ha_innobase.cc index b86336ab3ac..e8f12928513 100644 --- a/sql/ha_innobase.cc +++ b/sql/ha_innobase.cc @@ -1,7 +1,5 @@ /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB & Innobase Oy - - - This file is modified from ha_berkeley.cpp of the MySQL distribution - This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -23,26 +21,12 @@ Innobase */ /* TODO list for the Innobase handler: - How to check for deadlocks if Innobase tables are used alongside other MySQL table types? Solution: we will use a timeout. - - MySQL parser should know SELECT FOR UPDATE and SELECT WITH SHARED LOCKS - for Innobase interface. We probably will make the non-locking - consistent read the default in Innobase like in Oracle. - - Dropping of table in Innobase fails if there is a lock set on it: - Innobase then gives an error number to MySQL but MySQL seems to drop - the table from its own data dictionary anyway, causing incoherence - between the two databases. Solution: sleep until locks have been - released. - Innobase currently includes the path to a table name: the path should actually be dropped off, because we may move a whole database to a new directory. - - We use memcpy to store float and double types to Innobase: this - makes database files not portable between big-endian and little-endian - machines. - - In mysql_delete, in sql_delete.cpp, we must be able to prevent - MySQL from using generate_table to do a delete: consistent read does - not allow this. Currently, MySQL uses generate_table in DELETE FROM ... - if autocommit is on. - - Make the SELECT in an update a locking read. - Add a deadlock error message to MySQL. + - Ask Monty if strings of different languages can exist in the same + database. Answer: in near future yes, but not yet. */ #ifdef __GNUC__ @@ -56,14 +40,15 @@ Innobase */ #include <hash.h> #include <myisampack.h> +#define MAX_ULONG_BIT ((ulong) 1 << (sizeof(ulong)*8-1)) + #include "ha_innobase.h" -/* We use the following define in univ.i to remove a conflicting definition -of type 'byte' in univ.i, different from MySQL definition */ -#define INSIDE_HA_INNOBASE_CC +/* Store MySQL definition of 'byte': in Linux it is char while Innobase +uses unsigned char */ +typedef byte mysql_byte; -/* NOTE! When we include univ.i below, bool will be defined in the Innobase -way as an unsigned long int! In MySQL source code bool may be char. */ +#define INSIDE_HA_INNOBASE_CC /* Include necessary Innobase headers */ extern "C" { @@ -77,28 +62,28 @@ extern "C" { #include "../innobase/include/row0sel.h" #include "../innobase/include/row0upd.h" #include "../innobase/include/log0log.h" +#include "../innobase/include/lock0lock.h" #include "../innobase/include/dict0crea.h" #include "../innobase/include/btr0cur.h" #include "../innobase/include/btr0btr.h" +#include "../innobase/include/fsp0fsp.h" } #define HA_INNOBASE_ROWS_IN_TABLE 10000 /* to get optimization right */ #define HA_INNOBASE_RANGE_COUNT 100 -const char* ha_innobase_ext = ".ib"; - -mysql_bool innobase_skip = 0; -uint innobase_init_flags = 0; -ulong innobase_cache_size = 0; +bool innobase_skip = 0; +uint innobase_init_flags = 0; +ulong innobase_cache_size = 0; long innobase_mirrored_log_groups, innobase_log_files_in_group, innobase_log_file_size, innobase_log_buffer_size, innobase_buffer_pool_size, innobase_additional_mem_pool_size, - innobase_file_io_threads; + innobase_file_io_threads, innobase_lock_wait_timeout; char *innobase_data_home_dir, *innobase_data_file_path; char *innobase_log_group_home_dir, *innobase_log_arch_dir; -mysql_bool innobase_flush_log_at_trx_commit, innobase_log_archive, +bool innobase_flush_log_at_trx_commit, innobase_log_archive, innobase_use_native_aio; /* innobase_data_file_path=ibdata:15,idata2:1,... */ @@ -108,9 +93,8 @@ about server activity: in selects it is not sensible to call srv_active_wake_master_thread after each fetch or search, we only do it every INNOBASE_WAKE_INTERVAL'th step. */ -ulong innobase_select_counter = 0; #define INNOBASE_WAKE_INTERVAL 32 - +ulong innobase_active_counter = 0; char* innobase_home = NULL; @@ -118,7 +102,7 @@ pthread_mutex_t innobase_mutex; static HASH innobase_open_tables; -static byte* innobase_get_key(INNOBASE_SHARE *share,uint *length, +static mysql_byte* innobase_get_key(INNOBASE_SHARE *share,uint *length, my_bool not_used __attribute__((unused))); static INNOBASE_SHARE *get_share(const char *table_name); static void free_share(INNOBASE_SHARE *share); @@ -127,6 +111,23 @@ static void innobase_print_error(const char* db_errpfx, char* buffer); /* General functions */ /************************************************************************ +Increments innobase_active_counter and every INNOBASE_WAKE_INTERVALth +time calls srv_active_wake_master_thread. This function should be used +when a single database operation may introduce a small need for +server utility activity, like checkpointing. */ +inline +void +innobase_active_small(void) +/*=======================*/ +{ + innobase_active_counter++; + + if ((innobase_active_counter % INNOBASE_WAKE_INTERVAL) == 0) { + srv_active_wake_master_thread(); + } +} + +/************************************************************************ Converts an Innobase error code to a MySQL error code. */ static int @@ -153,7 +154,7 @@ convert_error_code_to_mysql( } else if (error == (int) DB_DEADLOCK) { - return(HA_ERR_UNSUPPORTED); + return(1000000); } else if (error == (int) DB_OUT_OF_FILE_SPACE) { @@ -182,7 +183,7 @@ convert_error_code_to_mysql( Gets the Innobase transaction handle for a MySQL handler object, creates an Innobase transaction struct if the corresponding MySQL thread struct still lacks one. */ -inline +static trx_t* check_trx_exists( /*=============*/ @@ -191,11 +192,10 @@ check_trx_exists( { trx_t* trx; - assert(thd != NULL); - trx = (trx_t*) thd->transaction.all.innobase_tid; if (trx == NULL) { + assert(thd != NULL); trx = trx_allocate_for_mysql(); thd->transaction.all.innobase_tid = trx; @@ -216,21 +216,21 @@ check_trx_exists( Updates the user_thd field in a handle and also allocates a new Innobase transaction handle if needed, and updates the transaction fields in the prebuilt struct. */ - +inline int ha_innobase::update_thd( /*====================*/ /* out: 0 or error code */ THD* thd) /* in: thd to use the handle */ { - trx_t* trx; + row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt; + trx_t* trx; trx = check_trx_exists(thd); - if (innobase_prebuilt != NULL) { + if (prebuilt->trx != trx) { - row_update_prebuilt_trx((row_prebuilt_t*) innobase_prebuilt, - trx); + row_update_prebuilt_trx(prebuilt, trx); } user_thd = thd; @@ -242,11 +242,10 @@ ha_innobase::update_thd( Reads the data files and their sizes from a character string given in the .cnf file. */ static -mysql_bool +bool innobase_parse_data_file_paths_and_sizes(void) /*==========================================*/ - /* out: ((mysql_bool)TRUE) if ok, - ((mysql_bool)FALSE) if parsing + /* out: TRUE if ok, FALSE if parsing error */ { char* str; @@ -268,7 +267,7 @@ innobase_parse_data_file_paths_and_sizes(void) } if (*str == '\0') { - return(((mysql_bool)FALSE)); + return(FALSE); } str++; @@ -283,7 +282,7 @@ innobase_parse_data_file_paths_and_sizes(void) } if (size == 0) { - return(((mysql_bool)FALSE)); + return(FALSE); } i++; @@ -292,7 +291,7 @@ innobase_parse_data_file_paths_and_sizes(void) str++; } else if (*str != '\0') { - return(((mysql_bool)FALSE)); + return(FALSE); } } @@ -338,18 +337,17 @@ innobase_parse_data_file_paths_and_sizes(void) } } - return(((mysql_bool)TRUE)); + return(TRUE); } /************************************************************************* Reads log group home directories from a character string given in the .cnf file. */ static -mysql_bool +bool innobase_parse_log_group_home_dirs(void) /*====================================*/ - /* out: ((mysql_bool)TRUE) if ok, - ((mysql_bool)FALSE) if parsing + /* out: TRUE if ok, FALSE if parsing error */ { char* str; @@ -374,13 +372,13 @@ innobase_parse_log_group_home_dirs(void) str++; } else if (*str != '\0') { - return(((mysql_bool)FALSE)); + return(FALSE); } } if (i != (ulint) innobase_mirrored_log_groups) { - return(((mysql_bool)FALSE)); + return(FALSE); } srv_log_group_home_dirs = (char**) ut_malloc(i * sizeof(void*)); @@ -407,22 +405,28 @@ innobase_parse_log_group_home_dirs(void) i++; } - return(((mysql_bool)TRUE)); + return(TRUE); } /************************************************************************* Opens an Innobase database. */ -mysql_bool +bool innobase_init(void) /*===============*/ - /* out: ((mysql_bool)TRUE) if error */ + /* out: TRUE if error */ { int err; - mysql_bool ret; + bool ret; + ibool test_bool; DBUG_ENTER("innobase_init"); + test_bool = TRUE; + assert(test_bool == 1); + test_bool = FALSE; + assert(test_bool == 0); + /* Set Innobase initialization parameters according to the values read from MySQL .cnf file */ @@ -432,14 +436,14 @@ innobase_init(void) ret = innobase_parse_data_file_paths_and_sizes(); - if (ret == ((mysql_bool)FALSE)) { - return(((mysql_bool)TRUE)); + if (ret == FALSE) { + return(TRUE); } ret = innobase_parse_log_group_home_dirs(); - if (ret == ((mysql_bool)FALSE)) { - return(((mysql_bool)TRUE)); + if (ret == FALSE) { + return(TRUE); } srv_n_log_groups = (ulint) innobase_mirrored_log_groups; @@ -450,13 +454,15 @@ innobase_init(void) srv_log_buffer_size = (ulint) innobase_log_buffer_size; srv_flush_log_at_trx_commit = (ulint) innobase_flush_log_at_trx_commit; - srv_use_native_aio = (ulint) innobase_use_native_aio; + srv_use_native_aio = 0; srv_pool_size = (ulint) innobase_buffer_pool_size; srv_mem_pool_size = (ulint) innobase_additional_mem_pool_size; srv_n_file_io_threads = (ulint) innobase_file_io_threads; + srv_lock_wait_timeout = (ulint) innobase_lock_wait_timeout; + err = innobase_start_or_create_for_mysql(); if (err != DB_SUCCESS) { @@ -472,10 +478,10 @@ innobase_init(void) /*********************************************************************** Closes an Innobase database. */ -mysql_bool +bool innobase_end(void) /*==============*/ - /* out: ((mysql_bool)TRUE) if error */ + /* out: TRUE if error */ { int err; @@ -495,12 +501,12 @@ innobase_end(void) Flushes Innobase logs to disk and makes a checkpoint. Really, a commit flushes logs, and the name of this function should be innobase_checkpoint. */ -mysql_bool +bool innobase_flush_logs(void) /*=====================*/ - /* out: ((mysql_bool)TRUE) if error */ + /* out: TRUE if error */ { - mysql_bool result = 0; + bool result = 0; DBUG_ENTER("innobase_flush_logs"); @@ -509,6 +515,17 @@ innobase_flush_logs(void) DBUG_RETURN(result); } +/************************************************************************* +Gets the free space in an Innobase database: returned in units of kB. */ + +uint +innobase_get_free_space(void) +/*=========================*/ + /* out: free space in kB */ +{ + return((uint) fsp_get_available_space_in_free_extents(0)); +} + /********************************************************************* Commits a transaction in an Innobase database. */ @@ -523,18 +540,17 @@ innobase_commit( mark the start of a new statement with a savepoint */ { int error = 0; + trx_t* trx; DBUG_ENTER("innobase_commit"); DBUG_PRINT("trans", ("ending transaction")); - check_trx_exists(thd); + trx = check_trx_exists(thd); if (trx_handle) { - trx_commit_for_mysql( - (trx_t*) (thd->transaction.all.innobase_tid)); + trx_commit_for_mysql(trx); } else { - trx_mark_sql_stat_end( - (trx_t*) (thd->transaction.all.innobase_tid)); + trx_mark_sql_stat_end(trx); } #ifndef DBUG_OFF @@ -564,18 +580,17 @@ innobase_rollback( back */ { int error = 0; - + trx_t* trx; + DBUG_ENTER("innobase_rollback"); DBUG_PRINT("trans", ("aborting transaction")); - check_trx_exists(thd); + trx = check_trx_exists(thd); if (trx_handle) { - error = trx_rollback_for_mysql( - (trx_t*) (thd->transaction.all.innobase_tid)); + error = trx_rollback_for_mysql(trx); } else { - error = trx_rollback_last_sql_stat_for_mysql( - (trx_t*) (thd->transaction.all.innobase_tid)); + error = trx_rollback_last_sql_stat_for_mysql(trx); } DBUG_RETURN(convert_error_code_to_mysql(error)); @@ -619,18 +634,63 @@ innobase_print_error( *****************************************************************************/ /******************************************************************** -??????????????? */ +This function is not relevant since we store the tables and indexes +into our own tablespace, not as files, whose extension this function would +give. */ const char** ha_innobase::bas_ext() const /*========================*/ - /* out: ?????????? */ + /* out: file extension strings, currently not + used */ { - static const char* ext[] = {ha_innobase_ext, NullS}; + static const char* ext[] = {".not_used", NullS}; + return(ext); } /********************************************************************* +Normalizes a table name string. A normalized name consists of the +database name catenated to '/' and table name. An example: +test/mytable. */ +static +void +normalize_table_name( +/*=================*/ + char* norm_name, /* out: normalized name as a + null-terminated string */ + const char* name) /* in: table name string */ +{ + char* name_ptr; + char* db_ptr; + char* ptr; + + /* Scan name from the end */ + + ptr = (char*) name + strlen(name) - 1; + + while (ptr >= name && *ptr != '\\' && *ptr != '/') { + ptr--; + } + + name_ptr = ptr + 1; + + assert(ptr > name); + + ptr--; + + while (ptr >= name && *ptr != '\\' && *ptr != '/') { + ptr--; + } + + db_ptr = ptr + 1; + + memcpy(norm_name, db_ptr, strlen(name) + 1 - (db_ptr - name)); + + norm_name[name_ptr - db_ptr - 1] = '/'; +} + +/********************************************************************* Creates and opens a handle to a table which already exists in an Innnobase database. */ @@ -642,36 +702,36 @@ ha_innobase::open( int mode, /* in: not used */ uint test_if_locked) /* in: not used */ { - int error = 0; - uint buff_len; + dict_table_t* ib_table; + int error = 0; + uint buff_len; + char norm_name[1000]; DBUG_ENTER("ha_innobase::open"); UT_NOT_USED(mode); UT_NOT_USED(test_if_locked); + normalize_table_name(norm_name, name); + user_thd = NULL; if (!(share=get_share(name))) DBUG_RETURN(1); - /* Create buffers for packing the fields of a record; - Why table->reclength did not work here? - obviously, because char fields when packed actually became - 1 byte longer, when we also stored the string length as - the first byte. */ + /* Create buffers for packing the fields of a record. Why + table->reclength did not work here? Obviously, because char + fields when packed actually became 1 byte longer, when we also + stored the string length as the first byte. */ buff_len = table->reclength + table->max_key_length - + MAX_REF_PARTS * 2; - - if (!(byte*) my_multi_malloc(MYF(MY_WME), - &rec_buff, buff_len, + + MAX_REF_PARTS * 2; + if (!(mysql_byte*) my_multi_malloc(MYF(MY_WME), &upd_buff, buff_len, &key_val_buff, buff_len, - NullS)) - { - free_share(share); - DBUG_RETURN(1); + NullS)) { + free_share(share); + DBUG_RETURN(1); } /* MySQL allocates the buffer for ref */ @@ -680,38 +740,45 @@ ha_innobase::open( /* Get pointer to a table object in Innobase dictionary cache */ - if (NULL == (innobase_table_handle - = dict_table_get((char*)name, NULL))) { + if (NULL == (ib_table = dict_table_get(norm_name, NULL))) { free_share(share); - my_free((char*) rec_buff, MYF(0)); + my_free((char*) upd_buff, MYF(0)); my_errno = ENOENT; DBUG_RETURN(1); } - info(HA_STATUS_NO_LOCK | HA_STATUS_VARIABLE | HA_STATUS_CONST); + innobase_prebuilt = row_create_prebuilt(ib_table); + + ((row_prebuilt_t*)innobase_prebuilt)->mysql_row_len = table->reclength; - /* Allocate a persistent cursor struct */ - - innobase_prebuilt = row_create_prebuilt((dict_table_t*) - innobase_table_handle); - primary_key = 0; + primary_key = MAX_KEY; + + if (!row_table_got_default_clust_index(ib_table)) { - if (!row_table_got_default_clust_index((dict_table_t*) - innobase_table_handle)) { /* If we automatically created the clustered index, then MySQL does not know about it and it must not be aware of the index used on scan, to avoid checking if we update the column of the index. The column is the row id in the automatical case, and it will not be updated. */ - - key_used_on_scan = primary_key; + + ((row_prebuilt_t*)innobase_prebuilt) + ->clust_index_was_generated = FALSE; + + primary_key = 0; + key_used_on_scan = 0; } else { + ((row_prebuilt_t*)innobase_prebuilt) + ->clust_index_was_generated = TRUE; + assert(key_used_on_scan == MAX_KEY); } /* Init table lock structure */ thr_lock_data_init(&share->lock,&lock,(void*) 0); + + info(HA_STATUS_NO_LOCK | HA_STATUS_VARIABLE | HA_STATUS_CONST); + DBUG_RETURN(0); } @@ -736,7 +803,7 @@ ha_innobase::close(void) row_prebuilt_free((row_prebuilt_t*) innobase_prebuilt); - my_free((char*) rec_buff, MYF(0)); + my_free((char*) upd_buff, MYF(0)); free_share(share); /* Tell Innobase server that there might be work for @@ -825,8 +892,10 @@ reset_null_bits( extern "C" { /***************************************************************** -This function is used to compare two data fields for which the data type -is such that we must use MySQL code to compare them. */ +Innobase uses this function is to compare two data fields for which the +data type is such that we must use MySQL code to compare them. NOTE that the +prototype of this function is in rem0cmp.c in Innobase source code! +If you change this function, remember to update the prototype there! */ int innobase_mysql_cmp( @@ -841,11 +910,6 @@ innobase_mysql_cmp( unsigned int b_length) /* in: data field length, not UNIV_SQL_NULL */ { - float f_1; - float f_2; - double d_1; - double d_2; - int swap_flag = 1; enum_field_types mysql_tp; assert(a_length != UNIV_SQL_NULL); @@ -859,82 +923,6 @@ innobase_mysql_cmp( case FIELD_TYPE_VAR_STRING: return(my_sortncmp((const char*) a, a_length, (const char*) b, b_length)); - case FIELD_TYPE_FLOAT: - memcpy(&f_1, a, sizeof(float)); - memcpy(&f_2, b, sizeof(float)); - - if (f_1 > f_2) { - return(1); - } else if (f_2 > f_1) { - return(-1); - } - - return(0); - - case FIELD_TYPE_DOUBLE: - memcpy(&d_1, a, sizeof(double)); - memcpy(&d_2, b, sizeof(double)); - - if (d_1 > d_2) { - return(1); - } else if (d_2 > d_1) { - return(-1); - } - - return(0); - - case FIELD_TYPE_DECIMAL: - /* Remove preceding spaces */ - for (; a_length && *a == ' '; a++, a_length--); - for (; b_length && *b == ' '; b++, b_length--); - - if (*a == '-') { - if (*b != '-') { - return(-1); - } - - a++; b++; - a_length--; - b_length--; - - swap_flag = -1; - - } else if (*b == '-') { - - return(1); - } - - while (a_length > 0 && (*a == '+' || *a == '0')) { - a++; a_length--; - } - - while (b_length > 0 && (*b == '+' || *b == '0')) { - b++; b_length--; - } - - if (a_length != b_length) { - if (a_length < b_length) { - return(-swap_flag); - } - - return(swap_flag); - } - - while (a_length > 0 && *a == *b) { - - a++; b++; a_length--; - } - - if (a_length == 0) { - - return(0); - } - - if (*a > *b) { - return(swap_flag); - } - - return(-swap_flag); default: assert(0); } @@ -944,18 +932,17 @@ innobase_mysql_cmp( } /****************************************************************** -Decides of MySQL types whether Innobase can internally compare them -using its own comparison functions, or whether Innobase must call MySQL -cmp function to compare them. */ +Converts a MySQL type to an Innobase type. */ inline ulint -innobase_cmp_type( -/*==============*/ - /* out: DATA_BINARY, DATA_VARCHAR, or DATA_MYSQL */ +get_innobase_type_from_mysql_type( +/*==============================*/ + /* out: DATA_BINARY, DATA_VARCHAR, ... */ Field* field) /* in: MySQL field */ { /* The following asserts check that MySQL type code fits in - one byte: this is used in ibuf */ + 8 bits: this is used in ibuf and also when DATA_NOT_NULL is + ORed to the type */ assert((ulint)FIELD_TYPE_STRING < 256); assert((ulint)FIELD_TYPE_VAR_STRING < 256); @@ -964,8 +951,7 @@ innobase_cmp_type( assert((ulint)FIELD_TYPE_DECIMAL < 256); switch (field->type()) { - case FIELD_TYPE_VAR_STRING: - case FIELD_TYPE_STRING: if (field->flags & BINARY_FLAG) { + case FIELD_TYPE_VAR_STRING: if (field->flags & BINARY_FLAG) { return(DATA_BINARY); } else if (strcmp( @@ -973,6 +959,16 @@ innobase_cmp_type( "latin1") == 0) { return(DATA_VARCHAR); } else { + return(DATA_VARMYSQL); + } + case FIELD_TYPE_STRING: if (field->flags & BINARY_FLAG) { + + return(DATA_FIXBINARY); + } else if (strcmp( + default_charset_info->name, + "latin1") == 0) { + return(DATA_CHAR); + } else { return(DATA_MYSQL); } case FIELD_TYPE_LONG: @@ -986,11 +982,20 @@ innobase_cmp_type( case FIELD_TYPE_NEWDATE: case FIELD_TYPE_ENUM: case FIELD_TYPE_SET: - return(DATA_BINARY); + case FIELD_TYPE_TIME: + case FIELD_TYPE_TIMESTAMP: + return(DATA_INT); case FIELD_TYPE_FLOAT: + return(DATA_FLOAT); case FIELD_TYPE_DOUBLE: + return(DATA_DOUBLE); case FIELD_TYPE_DECIMAL: - return(DATA_MYSQL); + return(DATA_DECIMAL); + case FIELD_TYPE_TINY_BLOB: + case FIELD_TYPE_MEDIUM_BLOB: + case FIELD_TYPE_BLOB: + case FIELD_TYPE_LONG_BLOB: + return(DATA_BLOB); default: assert(0); } @@ -998,165 +1003,6 @@ innobase_cmp_type( return(0); } -/****************************************************************** -Packs a non-SQL-NULL field data for storage in Innobase. Usually this -'packing' is just memcpy, but for an integer it is also converted -to a big-endian, sign bit negated form. */ -inline -byte* -pack_for_ib( -/*========*/ - /* out: pointer to the end of the packed data - in the buffer */ - byte* buf, /* in/out: pointer to buffer where to pack */ - Field* field, /* in: MySQL field object */ - byte* data) /* in: data to pack */ -{ - uint len; - uint i; - - switch (field->type()) { - case FIELD_TYPE_LONG: - case FIELD_TYPE_TINY: - case FIELD_TYPE_SHORT: - case FIELD_TYPE_INT24: - case FIELD_TYPE_LONGLONG: - len = field->pack_length(); break; - case FIELD_TYPE_VAR_STRING: - len = field->field_length; - - /* Scan away trailing spaces */ - for (i = 0; i < len; i++) { - if (data[len - i - 1] != ' ') { - break; - } - } - - memcpy(buf, data, len - i); - return(buf + len - i); - case FIELD_TYPE_STRING: - /* We store character strings with no - conversion */ - len = field->field_length; - memcpy(buf, data, len); - return(buf + len); - case FIELD_TYPE_DOUBLE: - memcpy(buf, data, sizeof(double)); - - return(buf + sizeof(double)); - case FIELD_TYPE_FLOAT: - memcpy(buf, data, sizeof(float)); - - return(buf + sizeof(float)); - default: - return((byte*) field->pack((char*) buf, - (const char*) data)); - } - - /* Store integer data in Innobase in a big-endian format, sign - bit negated */ - - for (i = 0; i < len; i++) { - buf[len - 1 - i] = data[i]; - } - - buf[0] = buf[0] ^ 128; - - return(buf + len); -} - -/****************************************************************** -Packs a non-SQL-NULL field data in a key value for storage in Innobase. -TODO: find out what is the difference between keypack and pack. */ -inline -byte* -keypack_for_ib( -/*===========*/ - /* out: pointer to the end of the packed data - in the buffer */ - byte* buf, /* in/out: buffer where to pack */ - Field* field, /* in: field object */ - byte* data, /* in: data to pack */ - uint len) /* in: length of the data to pack */ -{ - switch (field->type()) { - case FIELD_TYPE_LONG: - case FIELD_TYPE_TINY: - case FIELD_TYPE_SHORT: - case FIELD_TYPE_INT24: - case FIELD_TYPE_LONGLONG: - case FIELD_TYPE_STRING: - case FIELD_TYPE_VAR_STRING: - case FIELD_TYPE_DOUBLE: - case FIELD_TYPE_FLOAT: - return(pack_for_ib(buf, field, data)); - default: - return((byte*) field->keypack((char*) buf, - (const char*) data, len)); - } -} - -/****************************************************************** -Unpacks a non-SQL-NULL field data stored in Innobase. */ -inline -void -unpack_for_ib( -/*==========*/ - byte* dest, /* in/out: buffer where to unpack */ - Field* field, /* in: field object */ - byte* ptr, /* in: data to unpack */ - uint data_len)/* in: length of the data */ -{ - uint len; - uint i; - - switch (field->type()) { - case FIELD_TYPE_LONG: - case FIELD_TYPE_TINY: - case FIELD_TYPE_SHORT: - case FIELD_TYPE_INT24: - case FIELD_TYPE_LONGLONG: - len = field->pack_length(); break; - case FIELD_TYPE_VAR_STRING: - len = field->field_length; - /* Pad trailing characters with spaces */ - for (i = data_len; i < len; i++) { - dest[i] = ' '; - } - memcpy(dest, ptr, data_len); - - return; - case FIELD_TYPE_STRING: - /* We store character strings with no - conversion */ - len = field->field_length; - memcpy(dest, ptr, len); - - return; - case FIELD_TYPE_DOUBLE: - memcpy(dest, ptr, sizeof(double)); - - return; - case FIELD_TYPE_FLOAT: - memcpy(dest, ptr, sizeof(float)); - - return; - default: - field->unpack((char*) dest, (const char*) ptr); - - return; - } - - /* Get integer data from Innobase in a little-endian format, sign - bit restored to normal */ - - for (i = 0; i < len; i++) { - dest[len - 1 - i] = ptr[i]; - } - - dest[len - 1] = dest[len - 1] ^ 128; -} - /*********************************************************************** Stores a key value for a row to a buffer. */ @@ -1167,7 +1013,7 @@ ha_innobase::store_key_val_for_row( uint keynr, /* in: key number */ char* buff, /* in/out: buffer for the key value (in MySQL format) */ - const byte* record) /* in: row in MySQL format */ + const mysql_byte* record)/* in: row in MySQL format */ { KEY* key_info = table->key_info + keynr; KEY_PART_INFO* key_part = key_info->key_part; @@ -1183,11 +1029,11 @@ ha_innobase::store_key_val_for_row( if (record[key_part->null_offset] & key_part->null_bit) { - *buff++ =0; + *buff++ = 1; continue; } - *buff++ = 1; + *buff++ = 0; } memcpy(buff, record + key_part->offset, key_part->length); @@ -1198,173 +1044,142 @@ ha_innobase::store_key_val_for_row( } /****************************************************************** -Convert a row in MySQL format to a row in Innobase format. Uses rec_buff -of the handle. */ +Builds a template to the prebuilt struct. */ static void -convert_row_to_innobase( -/*====================*/ - dtuple_t* row, /* in/out: row in Innobase format */ - char* record, /* in: row in MySQL format */ - byte* rec_buff,/* in: record buffer */ - struct st_table* table) /* in: table in MySQL data dictionary */ +build_template( +/*===========*/ + row_prebuilt_t* prebuilt, /* in: prebuilt struct */ + THD* thd, /* in: current user thread, used + only if templ_type is + ROW_MYSQL_REC_FIELDS */ + TABLE* table, /* in: MySQL table */ + ulint templ_type) /* in: ROW_MYSQL_WHOLE_ROW or + ROW_MYSQL_REC_FIELDS */ { + dict_index_t* index; + dict_index_t* clust_index; + mysql_row_templ_t* templ; Field* field; - dfield_t* dfield; - uint n_fields; - ulint len; - byte* old_ptr; - byte* ptr; - uint i; - - n_fields = table->fields; + ulint n_fields; + ulint n_requested_fields = 0; + ulint i; - ptr = rec_buff; + clust_index = dict_table_get_first_index_noninline(prebuilt->table); - for (i = 0; i < n_fields; i++) { - field = table->field[i]; - dfield = dtuple_get_nth_field_noninline(row, i); + if (!prebuilt->in_update_remember_pos) { + /* We are building a temporary table: fetch all columns */ + + templ_type = ROW_MYSQL_WHOLE_ROW; + } + + if (templ_type == ROW_MYSQL_REC_FIELDS) { - old_ptr = ptr; + if (prebuilt->select_lock_type != LOCK_NONE) { + /* Let index be the clustered index */ - if (field->null_ptr && field_in_record_is_null(table, - field, record)) { - len = UNIV_SQL_NULL; + index = clust_index; } else { - ptr = pack_for_ib(ptr, field, (byte*) record - + get_field_offset(table, - field)); - len = ptr - old_ptr; + index = prebuilt->index; } - - dfield_set_data_noninline(dfield, old_ptr, len); + } else { + index = clust_index; } -} - -/****************************************************************** -Convert a row in Innobase format to a row in MySQL format. */ -static -void -convert_row_to_mysql( -/*=================*/ - char* record, /* in/out: row in MySQL format */ - dtuple_t* row, /* in: row in Innobase format */ - struct st_table* table) /* in: table in MySQL data dictionary */ -{ - Field* field; - dfield_t* dfield; - byte* ptr; - uint n_fields; - uint len; - uint i; - reset_null_bits(table, record); + if (index == clust_index) { + prebuilt->need_to_access_clustered = TRUE; + } else { + prebuilt->need_to_access_clustered = FALSE; + /* Below we check column by column if we need to access + the clustered index */ + } - n_fields = table->fields; + n_fields = (ulint)table->fields; + + if (!prebuilt->mysql_template) { + prebuilt->mysql_template = (mysql_row_templ_t*) + mem_alloc_noninline( + n_fields * sizeof(mysql_row_templ_t)); + } + prebuilt->template_type = templ_type; + prebuilt->null_bitmap_len = table->null_bytes; + + prebuilt->templ_contains_blob = FALSE; + for (i = 0; i < n_fields; i++) { + templ = prebuilt->mysql_template + n_requested_fields; field = table->field[i]; - dfield = dtuple_get_nth_field_noninline(row, i); - len = dfield_get_len_noninline(dfield); - - if (len != UNIV_SQL_NULL) { + if (templ_type == ROW_MYSQL_REC_FIELDS + && thd->query_id != field->query_id + && thd->query_id != (field->query_id ^ MAX_ULONG_BIT) + && thd->query_id != + (field->query_id ^ (MAX_ULONG_BIT >> 1))) { - ptr = (byte*) dfield_get_data_noninline(dfield); + /* This field is not needed in the query, skip it */ + + goto skip_field; + } - unpack_for_ib((byte*) - record + get_field_offset(table, field), - field, ptr, len); + n_requested_fields++; + + templ->col_no = i; + + if (index == clust_index) { + templ->rec_field_no = (index->table->cols + i) + ->clust_pos; } else { - set_field_in_record_to_null(table, field, record); + templ->rec_field_no = dict_index_get_nth_col_pos( + index, i); } - } -} -/******************************************************************** -Converts a key value stored in MySQL format to an Innobase dtuple. -The last field of the key value may be just a prefix of a fixed length -field: hence the parameter key_len. */ -static -dtuple_t* -convert_key_to_innobase( -/*====================*/ - dtuple_t* tuple, /* in/out: an Innobase dtuple which - must contain enough fields to be - able to store the key value */ - byte* buf, /* in/out: buffer where to store converted - field data */ - dict_index_t* index, /* in: Innobase index handle */ - KEY* key, /* in: MySQL key definition */ - byte* key_ptr,/* in: MySQL key value */ - int key_len)/* in: MySQL key value length */ -{ - KEY_PART_INFO* key_part = key->key_part; - KEY_PART_INFO* end = key_part + key->key_parts; - uint offset; - dfield_t* dfield; - byte* old_buf; - ulint n_fields = 0; - - DBUG_ENTER("convert_key_to_innobase"); + if (templ->rec_field_no == ULINT_UNDEFINED) { + prebuilt->need_to_access_clustered = TRUE; + } - /* Permit us to access any field in the tuple (ULINT_MAX): */ - - dtuple_set_n_fields(tuple, ULINT_MAX); + if (field->null_ptr) { + templ->mysql_null_byte_offset = + (ulint) ((char*) field->null_ptr + - (char*) table->record[0]); - dfield = dtuple_get_nth_field_noninline(tuple, 0); - - for (; key_part != end && key_len > 0; key_part++) { - n_fields++; - - offset = 0; - - if (key_part->null_bit) { - offset = 1; - if (*key_ptr != 0) { - dfield_set_data_noninline(dfield, NULL, - UNIV_SQL_NULL); - goto next_part; - } + templ->mysql_null_bit_mask = (ulint) field->null_bit; + } else { + templ->mysql_null_bit_mask = 0; + } - /* Is there a bug in ha_berkeley.cpp here? There - key_ptr is advanced one byte here. ???????????? */ - } + templ->mysql_col_offset = (ulint) + get_field_offset(table, field); - old_buf = buf; - buf = keypack_for_ib(buf, key_part->field, key_ptr + offset, - key_part->length); - - dfield_set_data_noninline(dfield, old_buf, - (ulint) (buf - old_buf)); - next_part: - key_ptr += key_part->store_length; - key_len -= key_part->store_length; - - if (key_len < 0) { - /* The last field in key was not a complete - field but a prefix of it */ - - assert(dfield_get_len_noninline(dfield) - != UNIV_SQL_NULL); - assert((int)(buf - old_buf) + key_len >= 0); - - dfield_set_data_noninline(dfield, old_buf, - (ulint) ((buf - old_buf) + key_len)); + templ->mysql_col_len = (ulint) field->pack_length(); + templ->type = get_innobase_type_from_mysql_type(field); + templ->is_unsigned = (ulint) (field->flags & UNSIGNED_FLAG); + + if (templ->type == DATA_BLOB) { + prebuilt->templ_contains_blob = TRUE; } +skip_field: + ; + } - dfield++; - } + prebuilt->n_template = n_requested_fields; - dict_index_copy_types(tuple, index, n_fields); + if (prebuilt->need_to_access_clustered) { + /* Change rec_field_no's to correspond to the clustered index + record */ + for (i = 0; i < n_requested_fields; i++) { + templ = prebuilt->mysql_template + i; - /* We set the length of tuple to n_fields: we assume that - the memory area allocated for it is big enough (usually - bigger than n_fields). */ - - dtuple_set_n_fields(tuple, n_fields); + templ->rec_field_no = + (index->table->cols + templ->col_no)->clust_pos; + } + } + + if (templ_type == ROW_MYSQL_REC_FIELDS + && prebuilt->select_lock_type != LOCK_NONE) { - DBUG_RETURN(tuple); + prebuilt->need_to_access_clustered = TRUE; + } } /************************************************************************ @@ -1374,11 +1189,10 @@ handle. */ int ha_innobase::write_row( /*===================*/ - /* out: error code */ - byte* record) /* in: a row in MySQL format */ + /* out: error code */ + mysql_byte* record) /* in: a row in MySQL format */ { - trx_t* trx; - dtuple_t* row; + row_prebuilt_t* prebuilt = (row_prebuilt_t*)innobase_prebuilt; int error; DBUG_ENTER("write_row"); @@ -1388,33 +1202,73 @@ ha_innobase::write_row( if (table->time_stamp) { update_timestamp(record + table->time_stamp - 1); } + if (table->next_number_field && record == table->record[0]) { update_auto_increment(); } - assert(user_thd->transaction.all.innobase_tid); - trx = check_trx_exists(user_thd); - - /* Convert the MySQL row into an Innobase dtuple format */ + if (prebuilt->mysql_template == NULL + || prebuilt->template_type != ROW_MYSQL_WHOLE_ROW) { + /* Build the template used in converting quickly between + the two database formats */ - row = row_get_prebuilt_insert_row( - (row_prebuilt_t*) innobase_prebuilt, - (dict_table_t*) innobase_table_handle, trx); - - convert_row_to_innobase(row, (char*) record, rec_buff, table); + build_template(prebuilt, NULL, table, ROW_MYSQL_WHOLE_ROW); + } - error = row_insert_for_mysql((row_prebuilt_t*)innobase_prebuilt, trx); + error = row_insert_for_mysql((byte*) record, prebuilt); error = convert_error_code_to_mysql(error); /* Tell Innobase server that there might be work for utility threads: */ - srv_active_wake_master_thread(); + innobase_active_small(); DBUG_RETURN(error); } +/****************************************************************** +Converts field data for storage in an Innobase update vector. */ +inline +mysql_byte* +innobase_convert_and_store_changed_col( +/*===================================*/ + /* out: pointer to the end of the converted + data in the buffer */ + upd_field_t* ufield, /* in/out: field in the update vector */ + mysql_byte* buf, /* in: buffer we can use in conversion */ + mysql_byte* data, /* in: column data to store */ + ulint len, /* in: data len */ + ulint col_type,/* in: data type in Innobase type numbers */ + ulint is_unsigned)/* in: != 0 if an unsigned integer type */ +{ + uint i; + + if (len == UNIV_SQL_NULL) { + data = NULL; + } else if (col_type == DATA_INT) { + /* Store integer data in Innobase in a big-endian + format, sign bit negated, if signed */ + + for (i = 0; i < len; i++) { + buf[len - 1 - i] = data[i]; + } + + if (!is_unsigned) { + buf[0] = buf[0] ^ 128; + } + + data = buf; + + buf += len; + } + + ufield->new_val.data = data; + ufield->new_val.len = len; + + return(buf); +} + /************************************************************************** Checks which fields have changed in a row and stores information of them to an update vector. */ @@ -1422,45 +1276,72 @@ static int calc_row_difference( /*================*/ - /* out: error number or 0 */ - upd_t* uvect, /* in/out: update vector */ - byte* old_row, /* in: old row in MySQL format */ - byte* new_row, /* in: new row in MySQL format */ - struct st_table* table, /* in: table in MySQL data dictionary */ - byte* upd_buff, /* in: buffer to use */ - row_prebuilt_t* prebuilt,/* in: Innobase prebuilt struct */ - void* innobase_table_handle) /* in: Innobase table handle */ + /* out: error number or 0 */ + upd_t* uvect, /* in/out: update vector */ + mysql_byte* old_row, /* in: old row in MySQL format */ + mysql_byte* new_row, /* in: new row in MySQL format */ + struct st_table* table, /* in: table in MySQL data dictionary */ + mysql_byte* upd_buff, /* in: buffer to use */ + row_prebuilt_t* prebuilt, /* in: Innobase prebuilt struct */ + THD* thd) /* in: user thread */ { Field* field; uint n_fields; ulint o_len; ulint n_len; - byte* o_ptr; - byte* n_ptr; - byte* old_ptr; - byte* ptr; - uint i; + mysql_byte* o_ptr; + mysql_byte* n_ptr; + mysql_byte* buf; upd_field_t* ufield; + ulint col_type; + ulint is_unsigned; ulint n_changed = 0; + uint i; n_fields = table->fields; - /* We use upd_buff to pack changed fields */ - ptr = upd_buff; + /* We use upd_buff to convert changed fields */ + buf = upd_buff; for (i = 0; i < n_fields; i++) { field = table->field[i]; + if (thd->query_id != field->query_id) { + /* TODO: check that these fields cannot have + changed! */ + + goto skip_field; + } + o_ptr = old_row + get_field_offset(table, field); n_ptr = new_row + get_field_offset(table, field); o_len = field->pack_length(); n_len = field->pack_length(); + col_type = get_innobase_type_from_mysql_type(field); + is_unsigned = (ulint) (field->flags & UNSIGNED_FLAG); + + switch (col_type) { + + case DATA_BLOB: + o_ptr = row_mysql_read_blob_ref(&o_len, o_ptr, o_len); + n_ptr = row_mysql_read_blob_ref(&n_len, n_ptr, n_len); + break; + case DATA_VARCHAR: + case DATA_BINARY: + case DATA_VARMYSQL: + o_ptr = row_mysql_read_var_ref_noninline(&o_len, o_ptr); + n_ptr = row_mysql_read_var_ref_noninline(&n_len, n_ptr); + default: + ; + } + if (field->null_ptr) { if (field_in_record_is_null(table, field, (char*) old_row)) { o_len = UNIV_SQL_NULL; } + if (field_in_record_is_null(table, field, (char*) new_row)) { n_len = UNIV_SQL_NULL; @@ -1471,22 +1352,18 @@ calc_row_difference( 0 != memcmp(o_ptr, n_ptr, o_len))) { /* The field has changed */ - if (n_len != UNIV_SQL_NULL) { - old_ptr = ptr; - ptr = pack_for_ib(ptr, field, n_ptr); - n_len = ptr - old_ptr; - } - ufield = uvect->fields + n_changed; - dfield_set_data_noninline(&(ufield->new_val), old_ptr, - n_len); + buf = innobase_convert_and_store_changed_col(ufield, + buf, n_ptr, n_len, col_type, + is_unsigned); ufield->exp = NULL; - ufield->field_no = dict_table_get_nth_col_pos( - (dict_table_t*) - innobase_table_handle, i); + ufield->field_no = + (prebuilt->table->cols + i)->clust_pos; n_changed++; } +skip_field: + ; } uvect->n_fields = n_changed; @@ -1507,46 +1384,41 @@ int ha_innobase::update_row( /*====================*/ /* out: error number or 0 */ - const byte* old_row, /* in: old row in MySQL format */ - byte* new_row) /* in: new row in MySQL format */ + const mysql_byte* old_row,/* in: old row in MySQL format */ + mysql_byte* new_row)/* in: new row in MySQL format */ { row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt; - trx_t* trx; upd_t* uvect; int error = 0; DBUG_ENTER("update_row"); - assert(user_thd->transaction.all.innobase_tid); - trx = check_trx_exists(user_thd); - - uvect = row_get_prebuilt_update_vector( - prebuilt, - (dict_table_t*) innobase_table_handle, trx); - - /* Build old row in the Innobase format (uses rec_buff of the - handle) */ + if (prebuilt->upd_node) { + uvect = prebuilt->upd_node->update; + } else { + uvect = row_get_prebuilt_update_vector(prebuilt); + } - convert_row_to_innobase(prebuilt->row_tuple, (char*) old_row, - rec_buff, table); - /* Build an update vector from the modified fields in the rows (uses upd_buff of the handle) */ - calc_row_difference(uvect, (byte*) old_row, new_row, table, upd_buff, - prebuilt, innobase_table_handle); + calc_row_difference(uvect, (mysql_byte*) old_row, new_row, table, + upd_buff, prebuilt, user_thd); /* This is not a delete */ prebuilt->upd_node->is_delete = FALSE; - error = row_update_for_mysql((row_prebuilt_t*) innobase_prebuilt, - (dict_table_t*) innobase_table_handle, trx); + if (!prebuilt->in_update_remember_pos) { + assert(prebuilt->template_type == ROW_MYSQL_WHOLE_ROW); + } + + error = row_update_for_mysql((byte*) old_row, prebuilt); error = convert_error_code_to_mysql(error); /* Tell Innobase server that there might be work for utility threads: */ - srv_active_wake_master_thread(); + innobase_active_small(); DBUG_RETURN(error); } @@ -1557,41 +1429,31 @@ Deletes a row given as the parameter. */ int ha_innobase::delete_row( /*====================*/ - /* out: error number or 0 */ - const byte* record) /* in: a row in MySQL format */ + /* out: error number or 0 */ + const mysql_byte* record) /* in: a row in MySQL format */ { row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt; - trx_t* trx; - upd_t* uvect; int error = 0; DBUG_ENTER("update_row"); - assert(user_thd->transaction.all.innobase_tid); - trx = check_trx_exists(user_thd); - - uvect = row_get_prebuilt_update_vector( - prebuilt, - (dict_table_t*) innobase_table_handle, trx); - - /* Build old row in the Innobase format (uses rec_buff of the - handle) */ + if (!prebuilt->upd_node) { + row_get_prebuilt_update_vector(prebuilt); + } - convert_row_to_innobase(prebuilt->row_tuple, (char*) record, - rec_buff, table); /* This is a delete */ prebuilt->upd_node->is_delete = TRUE; - - error = row_update_for_mysql((row_prebuilt_t*) innobase_prebuilt, - (dict_table_t*) innobase_table_handle, trx); + prebuilt->in_update_remember_pos = TRUE; + + error = row_update_for_mysql((byte*) record, prebuilt); error = convert_error_code_to_mysql(error); /* Tell the Innobase server that there might be work for utility threads: */ - srv_active_wake_master_thread(); + innobase_active_small(); DBUG_RETURN(error); } @@ -1623,11 +1485,6 @@ ha_innobase::index_end(void) int error = 0; DBUG_ENTER("index_end"); - /* Tell Innobase server that there might be work for utility - threads: */ - - srv_active_wake_master_thread(); - DBUG_RETURN(error); } @@ -1667,44 +1524,42 @@ ha_innobase::index_read( /*====================*/ /* out: 0, HA_ERR_KEY_NOT_FOUND, or error number */ - byte* buf, /* in/out: buffer for the returned + mysql_byte* buf, /* in/out: buffer for the returned row */ - const byte* key_ptr, /* in: key value; if this is NULL + const mysql_byte* key_ptr,/* in: key value; if this is NULL we position the cursor at the start or end of index */ - uint key_len, /* in: key value length */ + uint key_len,/* in: key value length */ enum ha_rkey_function find_flag)/* in: search flags from my_base.h */ { row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt; ulint mode; dict_index_t* index; - btr_pcur_t* pcur; - KEY* key; ulint match_mode = 0; int error; ulint ret; - trx_t* trx; - mtr_t mtr; DBUG_ENTER("index_read"); statistic_increment(ha_read_key_count, &LOCK_status); - - /* TODO: currently we assume all reads perform consistent read! */ - /* prebuilt->consistent_read = TRUE; */ - - assert(user_thd->transaction.all.innobase_tid); - trx = check_trx_exists(user_thd); - pcur = prebuilt->pcur; + index = prebuilt->index; - key = table->key_info + active_index; + /* Note that if the select is used for an update, we always + fetch the clustered index record: therefore the index for which the + template is built is not necessarily prebuilt->index, but can also + be the clustered index */ - index = prebuilt->index; + if (prebuilt->sql_stat_start) { + build_template(prebuilt, user_thd, table, + ROW_MYSQL_REC_FIELDS); + } if (key_ptr) { - convert_key_to_innobase(prebuilt->search_tuple, key_val_buff, - index, key, (byte*) key_ptr, - (int) key_len); + row_sel_convert_mysql_key_to_innobase(prebuilt->search_tuple, + (byte*) key_val_buff, + index, + (byte*) key_ptr, + (ulint) key_len); } else { /* We position the cursor to the last or the first entry in the index */ @@ -1726,17 +1581,9 @@ ha_innobase::index_read( last_match_mode = match_mode; - /* Start an Innobase mini-transaction, which carries the - latch information of the read operation */ - - mtr_start_noninline(&mtr); - - ret = row_search_for_mysql(prebuilt->row_tuple, - mode, prebuilt, match_mode, - trx, &mtr, 0); + ret = row_search_for_mysql(buf, mode, prebuilt, match_mode, 0); if (ret == DB_SUCCESS) { - convert_row_to_mysql((char*) buf, prebuilt->row_tuple, table); error = 0; table->status = 0; @@ -1751,15 +1598,7 @@ ha_innobase::index_read( error = convert_error_code_to_mysql(ret); table->status = STATUS_NOT_FOUND; } - - mtr_commit(&mtr); - innobase_select_counter++; - - if (innobase_select_counter % INNOBASE_WAKE_INTERVAL == 0) { - srv_active_wake_master_thread(); - } - DBUG_RETURN(error); } @@ -1769,8 +1608,10 @@ Changes the active index of a handle. */ int ha_innobase::change_active_index( /*=============================*/ - /* out: 0 or error code */ - uint keynr) /* in: use this index */ + /* out: 0 or error code */ + uint keynr) /* in: use this index; MAX_KEY means always clustered + index, even if it was internally generated by + Innobase */ { row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt; KEY* key; @@ -1781,20 +1622,28 @@ ha_innobase::change_active_index( active_index = keynr; - if (table->keys > 0) { + if (keynr != MAX_KEY && table->keys > 0) { key = table->key_info + active_index; prebuilt->index = dict_table_get_index_noninline( - (dict_table_t*) innobase_table_handle, - key->name); + prebuilt->table, key->name); } else { - assert(keynr == 0); prebuilt->index = dict_table_get_first_index_noninline( - (dict_table_t*) innobase_table_handle); + prebuilt->table); } + dtuple_set_n_fields(prebuilt->search_tuple, prebuilt->index->n_fields); + + dict_index_copy_types(prebuilt->search_tuple, prebuilt->index, + prebuilt->index->n_fields); assert(prebuilt->index); + + /* Maybe MySQL changes the active index for a handle also + during some queries, we do not know: then it is safest to build + the template such that all columns will be fetched */ + build_template(prebuilt, user_thd, table, ROW_MYSQL_WHOLE_ROW); + return(0); } @@ -1807,10 +1656,10 @@ int ha_innobase::index_read_idx( /*========================*/ /* out: error number or 0 */ - byte* buf, /* in/out: buffer for the returned + mysql_byte* buf, /* in/out: buffer for the returned row */ uint keynr, /* in: use this index */ - const byte* key, /* in: key value; if this is NULL + const mysql_byte* key, /* in: key value; if this is NULL we position the cursor at the start or end of index */ uint key_len, /* in: key value length */ @@ -1830,7 +1679,7 @@ ha_innobase::general_fetch( /*=======================*/ /* out: 0, HA_ERR_END_OF_FILE, or error number */ - byte* buf, /* in/out: buffer for next row in MySQL + mysql_byte* buf, /* in/out: buffer for next row in MySQL format */ uint direction, /* in: ROW_SEL_NEXT or ROW_SEL_PREV */ uint match_mode) /* in: 0, ROW_SEL_EXACT, or @@ -1838,21 +1687,13 @@ ha_innobase::general_fetch( { row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt; ulint ret; - trx_t* trx; int error = 0; - mtr_t mtr; DBUG_ENTER("general_fetch"); - statistic_increment(ha_read_next_count, &LOCK_status); - - trx = check_trx_exists(user_thd); - mtr_start_noninline(&mtr); + ret = row_search_for_mysql(buf, 0, prebuilt, match_mode, direction); - ret = row_search_for_mysql(prebuilt->row_tuple, 0, prebuilt, - match_mode, trx, &mtr, direction); if (ret == DB_SUCCESS) { - convert_row_to_mysql((char*) buf, prebuilt->row_tuple, table); error = 0; table->status = 0; @@ -1867,15 +1708,7 @@ ha_innobase::general_fetch( error = convert_error_code_to_mysql(ret); table->status = STATUS_NOT_FOUND; } - - mtr_commit(&mtr); - innobase_select_counter++; - - if (innobase_select_counter % INNOBASE_WAKE_INTERVAL == 0) { - srv_active_wake_master_thread(); - } - DBUG_RETURN(error); } @@ -1888,9 +1721,11 @@ ha_innobase::index_next( /*====================*/ /* out: 0, HA_ERR_END_OF_FILE, or error number */ - byte* buf) /* in/out: buffer for next row in MySQL + mysql_byte* buf) /* in/out: buffer for next row in MySQL format */ { + statistic_increment(ha_read_next_count, &LOCK_status); + return(general_fetch(buf, ROW_SEL_NEXT, 0)); } @@ -1902,11 +1737,11 @@ ha_innobase::index_next_same( /*=========================*/ /* out: 0, HA_ERR_END_OF_FILE, or error number */ - byte* buf, /* in/out: buffer for the row */ - const byte* key, /* in: key value */ + mysql_byte* buf, /* in/out: buffer for the row */ + const mysql_byte* key, /* in: key value */ uint keylen) /* in: key value length */ { - assert(last_match_mode != 0); + statistic_increment(ha_read_next_count, &LOCK_status); return(general_fetch(buf, ROW_SEL_NEXT, last_match_mode)); } @@ -1920,7 +1755,7 @@ ha_innobase::index_prev( /*====================*/ /* out: 0, HA_ERR_END_OF_FILE, or error number */ - byte* buf) /* in/out: buffer for previous row in MySQL + mysql_byte* buf) /* in/out: buffer for previous row in MySQL format */ { return(general_fetch(buf, ROW_SEL_PREV, 0)); @@ -1933,8 +1768,9 @@ corresponding row to buf. */ int ha_innobase::index_first( /*=====================*/ - /* out: 0, HA_ERR_KEY_NOT_FOUND, or error code */ - byte* buf) /* in/out: buffer for the row */ + /* out: 0, HA_ERR_KEY_NOT_FOUND, + or error code */ + mysql_byte* buf) /* in/out: buffer for the row */ { int error; @@ -1953,13 +1789,13 @@ corresponding row to buf. */ int ha_innobase::index_last( /*====================*/ - /* out: 0, HA_ERR_END_OF_FILE, or error code */ - byte* buf) /* in/out: buffer for the row */ + /* out: 0, HA_ERR_END_OF_FILE, or error code */ + mysql_byte* buf) /* in/out: buffer for the row */ { int error; DBUG_ENTER("index_first"); - statistic_increment(ha_read_first_count, &LOCK_status); + statistic_increment(ha_read_last_count, &LOCK_status); error = index_read(buf, NULL, 0, HA_READ_BEFORE_KEY); @@ -1979,13 +1815,17 @@ int ha_innobase::rnd_init( /*==================*/ /* out: 0 or error number */ - mysql_bool scan) /* in: ???????? */ + bool scan) /* in: ???????? */ { - row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt; + row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt; - change_active_index(primary_key); + if (prebuilt->clust_index_was_generated) { + change_active_index(MAX_KEY); + } else { + change_active_index(primary_key); + } - prebuilt->start_of_scan = TRUE; + start_of_scan = 1; return(0); } @@ -2009,23 +1849,22 @@ int ha_innobase::rnd_next( /*==================*/ /* out: 0, HA_ERR_END_OF_FILE, or error number */ - byte* buf) /* in/out: returns the row in this buffer, + mysql_byte* buf)/* in/out: returns the row in this buffer, in MySQL format */ { - row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt; - int error; + int error; DBUG_ENTER("rnd_next"); statistic_increment(ha_read_rnd_next_count, &LOCK_status); - if (prebuilt->start_of_scan) { + if (start_of_scan) { error = index_first(buf); if (error == HA_ERR_KEY_NOT_FOUND) { error = HA_ERR_END_OF_FILE; } - prebuilt->start_of_scan = FALSE; + start_of_scan = 0; } else { - error = index_next(buf); + error = general_fetch(buf, ROW_SEL_NEXT, 0); } DBUG_RETURN(error); @@ -2038,79 +1877,96 @@ Fetches a row from the table based on a reference. TODO: currently we use int ha_innobase::rnd_pos( /*=================*/ - /* out: 0, HA_ERR_KEY_NOT_FOUND, or error code */ - byte* buf, /* in/out: buffer for the row */ - byte* pos) /* in: primary key value in MySQL format */ + /* out: 0, HA_ERR_KEY_NOT_FOUND, + or error code */ + mysql_byte* buf, /* in/out: buffer for the row */ + mysql_byte* pos) /* in: primary key value in MySQL format */ { - int error; - + row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt; + int error; + uint keynr = active_index; + DBUG_ENTER("rnd_pos"); statistic_increment(ha_read_rnd_count, &LOCK_status); - - assert(table->keys > 0); - - /* The following assert states that the cursor is positioned - to the primary index in this function: this cannot be used to - position the cursor to a secondary index! */ - assert(active_index == primary_key); + if (prebuilt->clust_index_was_generated) { + /* No primary key was defined for the table and we + generated the clustered index from the row id: the + row reference is the row id, not any key value + that MySQL knows */ + change_active_index(MAX_KEY); + } else { + change_active_index(primary_key); + } + error = index_read(buf, pos, ref_stored_len, HA_READ_KEY_EXACT); + change_active_index(keynr); + DBUG_RETURN(error); } /************************************************************************* -Stores a reference to a given row to 'ref' field of the handle. */ +Stores a reference to the current row to 'ref' field of the handle. Note +that the function parameter is illogical: we must assume that 'record' +is the current 'position' of the handle, because if row ref is actually +the row id internally generated in Innobase, then 'record' does not contain +it. We just guess that the row id must be for the record where the handle +was positioned the last time. */ void ha_innobase::position( /*==================*/ - const byte* record) /* in: row in MySQL format */ + const mysql_byte* record) /* in: row in MySQL format */ { - uint len; - - assert(table->keys > 0); + row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt; + uint len; - len = store_key_val_for_row(primary_key, (char*) ref, record); + if (prebuilt->clust_index_was_generated) { + /* No primary key was defined for the table and we + generated the clustered index from row id: the + row reference will be the row id, not any key value + that MySQL knows */ + + len = DATA_ROW_ID_LEN; + + memcpy(ref, prebuilt->row_id, len); + } else { + len = store_key_val_for_row(primary_key, (char*) ref, record); + } assert(len <= ref_length); ref_stored_len = len; } -/************************************************************************* -Returns various information to MySQL interpreter, in various fields -of the handle object. */ +/*********************************************************************** +Tells something additional to the handler about how to do things. */ -void -ha_innobase::info( -/*==============*/ - uint flag) /* in: what information MySQL requests */ +int +ha_innobase::extra( +/*===============*/ + /* out: 0 or error number */ + enum ha_extra_function operation) + /* in: HA_EXTRA_DONT_USE_CURSOR_TO_UPDATE */ { - DBUG_ENTER("info"); - - if (flag & HA_STATUS_VARIABLE) { - records = HA_INNOBASE_ROWS_IN_TABLE; // Just to get optimisations right - deleted = 0; - - } else if (flag & HA_STATUS_ERRKEY) { - - errkey = (unsigned int)-1; /* TODO: get the key number from - Innobase */ - } + row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt; - DBUG_VOID_RETURN; -} + switch (operation) { + case HA_EXTRA_DONT_USE_CURSOR_TO_UPDATE: + prebuilt->in_update_remember_pos = FALSE; + break; + default: /* Do nothing */ + ; + } -int ha_innobase::extra(enum ha_extra_function operation) -{ - return 0; + return(0); } int ha_innobase::reset(void) { - return 0; + return(0); } /********************************************************************** @@ -2134,10 +1990,17 @@ ha_innobase::external_lock( DBUG_ENTER("ha_innobase::external_lock"); update_thd(thd); + + trx = prebuilt->trx; prebuilt->sql_stat_start = TRUE; - - trx = check_trx_exists(thd); + prebuilt->in_update_remember_pos = TRUE; + + if (lock_type == F_WRLCK) { + /* If this is a SELECT, then it is in UPDATE TABLE ... + or SELECT ... FOR UPDATE */ + prebuilt->select_lock_type = LOCK_X; + } if (lock_type != F_UNLCK) { if (trx->n_mysql_tables_in_use == 0) { @@ -2153,42 +2016,6 @@ ha_innobase::external_lock( } /********************************************************************* -Stores a MySQL lock into a 'lock' field in a handle. */ - -THR_LOCK_DATA** -ha_innobase::store_lock( -/*====================*/ - /* out: pointer to the next - element in the 'to' array */ - THD* thd, /* in: user thread handle */ - THR_LOCK_DATA** to, /* in: pointer to an array - of pointers to lock structs; - pointer to the 'lock' field - of current handle is stored - next to this array */ - enum thr_lock_type lock_type) /* in: lock type to store in - 'lock' */ -{ - if (lock_type != TL_IGNORE && lock.type == TL_UNLOCK) { - - /* If we are not doing a LOCK TABLE, then allow multiple - writers */ - - if ((lock_type >= TL_WRITE_CONCURRENT_INSERT && - lock_type <= TL_WRITE) && !thd->in_lock_tables) { - - lock_type = TL_WRITE_ALLOW_WRITE; - } - - lock.type=lock_type; - } - - *to++= &lock; - - return(to); -} - -/********************************************************************* Creates a table definition to an Innobase database. */ static int @@ -2204,8 +2031,10 @@ create_table_def( ulint n_cols; int error; ulint col_type; + ulint nulls_allowed; + ulint unsigned_type; ulint i; - + DBUG_ENTER("create_table_def"); DBUG_PRINT("enter", ("table_name: %s", table_name)); @@ -2219,10 +2048,22 @@ create_table_def( for (i = 0; i < n_cols; i++) { field = form->field[i]; - col_type = innobase_cmp_type(field); + col_type = get_innobase_type_from_mysql_type(field); + if (field->null_ptr) { + nulls_allowed = 0; + } else { + nulls_allowed = DATA_NOT_NULL; + } + + if (field->flags & UNSIGNED_FLAG) { + unsigned_type = DATA_UNSIGNED; + } else { + unsigned_type = 0; + } dict_mem_table_add_col(table, (char*) field->field_name, - col_type, (ulint)field->type(), + col_type, (ulint)field->type() + | nulls_allowed | unsigned_type, field->pack_length(), 0); } @@ -2237,8 +2078,8 @@ create_table_def( Creates an index in an Innobase database. */ static int -create_sub_table( -/*=============*/ +create_index( +/*=========*/ trx_t* trx, /* in: Innobase transaction handle */ TABLE* form, /* in: information on table columns and indexes */ @@ -2253,7 +2094,7 @@ create_sub_table( ulint ind_type; ulint i; - DBUG_ENTER("create_sub_table"); + DBUG_ENTER("create_index"); key = form->key_info + key_num; @@ -2261,9 +2102,7 @@ create_sub_table( ind_type = 0; - if (key_num == 0) { - /* We assume that the clustered index is always - created first: */ + if (strcmp(key->name, "PRIMARY") == 0) { ind_type = ind_type | DICT_CLUSTERED; } @@ -2294,18 +2133,16 @@ create_sub_table( /********************************************************************* Creates an index to an Innobase table when the user has defined no -index. */ +primary index. */ static int -create_index_when_no_index( -/*=======================*/ +create_clustered_index_when_no_primary( +/*===================================*/ trx_t* trx, /* in: Innobase transaction handle */ const char* table_name) /* in: table name */ { dict_index_t* index; int error; - - DBUG_ENTER("create_index_when_no_index"); /* The first '0' below specifies that everything in Innobase is currently created in file space 0 */ @@ -2316,7 +2153,7 @@ create_index_when_no_index( error = convert_error_code_to_mysql(error); - DBUG_RETURN(error); + return(error); } /********************************************************************* @@ -2335,7 +2172,11 @@ ha_innobase::create( dict_table_t* innobase_table; uint name_len; trx_t* trx; + int primary_key_no = -1; + KEY* key; + uint i; char name2[1000]; + char norm_name[1000]; DBUG_ENTER("ha_innobase::create"); @@ -2351,10 +2192,12 @@ ha_innobase::create( /* Erase the .frm end from table name: */ name2[name_len - 4] = '\0'; + + normalize_table_name(norm_name, name2); /* Create the table definition in Innobase */ - if (error = create_table_def(trx, form, name2)) { + if (error = create_table_def(trx, form, norm_name)) { trx_commit_for_mysql(trx); @@ -2363,15 +2206,25 @@ ha_innobase::create( DBUG_RETURN(error); } + /* Look for a primary key */ + + for (i = 0; i < form->keys; i++) { + key = form->key_info + i; + + if (strcmp(key->name, "PRIMARY") == 0) { + primary_key_no = (int) i; + } + } + /* Create the keys */ - if (form->keys == 0) { - /* Create a single index which is used as the clustered - index; order the rows by their row id generated internally + if (form->keys == 0 || primary_key_no == -1) { + /* Create an index which is used as the clustered index; + order the rows by their row id which is internally generated by Innobase */ - error = create_index_when_no_index(trx, name2); - + error = create_clustered_index_when_no_primary(trx, + norm_name); if (error) { trx_commit_for_mysql(trx); @@ -2379,23 +2232,39 @@ ha_innobase::create( DBUG_RETURN(error); } - } else { - for (uint i = 0; i < form->keys; i++) { + } + + if (primary_key_no != -1) { + /* In Innobase the clustered index must always be created + first */ + if (error = create_index(trx, form, norm_name, + (uint) primary_key_no)) { + trx_commit_for_mysql(trx); + + trx_free_for_mysql(trx); + + DBUG_RETURN(error); + } + } + + for (i = 0; i < form->keys; i++) { + + if (i != (uint) primary_key_no) { + + if (error = create_index(trx, form, norm_name, i)) { - if (error = create_sub_table(trx, form, name2, i)) { - trx_commit_for_mysql(trx); trx_free_for_mysql(trx); DBUG_RETURN(error); } - } + } } - + trx_commit_for_mysql(trx); - innobase_table = dict_table_get((char*)name2, NULL); + innobase_table = dict_table_get(norm_name, NULL); assert(innobase_table); @@ -2425,6 +2294,7 @@ ha_innobase::delete_table( ulint name_len; int error; trx_t* trx; + char norm_name[1000]; DBUG_ENTER("ha_innobase::delete_table"); @@ -2433,14 +2303,15 @@ ha_innobase::delete_table( name_len = strlen(name); assert(name_len < 1000); - assert(name_len > 4); - + /* Strangely, MySQL passes the table name without the '.frm' extension, in contrast to ::create */ + normalize_table_name(norm_name, name); + /* Drop the table in Innobase */ - error = row_drop_table_for_mysql((char*) name, trx, FALSE); + error = row_drop_table_for_mysql(norm_name, trx, FALSE); /* Tell the Innobase server that there might be work for utility threads: */ @@ -2468,6 +2339,8 @@ ha_innobase::rename_table( ulint name_len2; int error; trx_t* trx; + char norm_from[1000]; + char norm_to[1000]; DBUG_ENTER("ha_innobase::rename_table"); @@ -2477,15 +2350,14 @@ ha_innobase::rename_table( name_len2 = strlen(to); assert(name_len1 < 1000); - assert(name_len1 > 4); assert(name_len2 < 1000); - assert(name_len2 > 4); - - /* TODO: what name extensions MySQL passes here? */ + normalize_table_name(norm_from, from); + normalize_table_name(norm_to, to); + /* Rename the table in Innobase */ - error = row_rename_table_for_mysql((char*) from, (char*) to, trx); + error = row_rename_table_for_mysql(norm_from, norm_to, trx); /* Tell the Innobase server that there might be work for utility threads: */ @@ -2500,26 +2372,6 @@ ha_innobase::rename_table( } /************************************************************************* -How many seeks it will take to read through the table. This is to be -comparable to the number returned by records_in_range so that we can -decide if we should scan the table or use keys. */ - -double -ha_innobase::scan_time() -/*====================*/ - /* out: estimated time measured in disk seeks */ -{ - dict_table_t* table = (dict_table_t*) innobase_table_handle; - - /* In the following formula we assume that scanning 5 pages - takes the same time as a disk seek: */ - - return((double) (btr_get_size( - dict_table_get_first_index_noninline(table), - BTR_N_LEAF_PAGES) / 5)); -} - -/************************************************************************* Estimates the number of index records in a range. */ ha_rows @@ -2528,13 +2380,13 @@ ha_innobase::records_in_range( /* out: estimated number of rows, currently 32-bit int or uint */ int keynr, /* in: index number */ - const byte* start_key, /* in: start key value of the + const mysql_byte* start_key, /* in: start key value of the range, may also be empty */ uint start_key_len, /* in: start key val len, may also be 0 */ enum ha_rkey_function start_search_flag,/* in: start search condition e.g., 'greater than' */ - const byte* end_key, /* in: range end key val, may + const mysql_byte* end_key, /* in: range end key val, may also be empty */ uint end_key_len, /* in: range end key val len, may also be 0 */ @@ -2543,13 +2395,16 @@ ha_innobase::records_in_range( row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt; KEY* key; dict_index_t* index; - byte* key_val_buff2 = (byte*) my_malloc(table->reclength, + mysql_byte* key_val_buff2 = (mysql_byte*) my_malloc( + table->reclength, MYF(MY_WME)); + dtuple_t* range_start; dtuple_t* range_end; ulint n_rows; ulint mode1; ulint mode2; - void* heap; + void* heap1; + void* heap2; DBUG_ENTER("records_in_range"); @@ -2557,44 +2412,163 @@ ha_innobase::records_in_range( key = table->key_info + active_index; - index = dict_table_get_index_noninline( - (dict_table_t*) innobase_table_handle, - key->name); - - /* In converting the first key value we make use of the buffers - in our handle: */ + index = dict_table_get_index_noninline(prebuilt->table, key->name); - convert_key_to_innobase(prebuilt->search_tuple, key_val_buff, index, - key, (byte*) start_key, (int) start_key_len); - - /* For the second key value we have to use allocated buffers: */ + range_start = dtuple_create_for_mysql(&heap1, key->key_parts); + dict_index_copy_types(range_start, index, index->n_fields); - range_end = dtuple_create_for_mysql(&heap, key->key_parts); + range_end = dtuple_create_for_mysql(&heap2, key->key_parts); + dict_index_copy_types(range_end, index, index->n_fields); - convert_key_to_innobase(range_end, key_val_buff2, index, - key, (byte*) end_key, (int) end_key_len); + row_sel_convert_mysql_key_to_innobase( + range_start, (byte*) key_val_buff, index, + (byte*) start_key, + (ulint) start_key_len); + row_sel_convert_mysql_key_to_innobase( + range_end, (byte*) key_val_buff2, index, + (byte*) end_key, + (ulint) end_key_len); + mode1 = convert_search_mode_to_innobase(start_search_flag); mode2 = convert_search_mode_to_innobase(end_search_flag); - n_rows = btr_estimate_n_rows_in_range(index, prebuilt->search_tuple, + n_rows = btr_estimate_n_rows_in_range(index, range_start, mode1, range_end, mode2); - dtuple_free_for_mysql(heap); + dtuple_free_for_mysql(heap1); + dtuple_free_for_mysql(heap2); + my_free((char*) key_val_buff2, MYF(0)); DBUG_RETURN((ha_rows) n_rows); } +/************************************************************************* +How many seeks it will take to read through the table. This is to be +comparable to the number returned by records_in_range so that we can +decide if we should scan the table or use keys. */ + +double +ha_innobase::scan_time() +/*====================*/ + /* out: estimated time measured in disk seeks */ +{ + row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt; + + /* In the following formula we assume that scanning 5 pages + takes the same time as a disk seek: */ + + return((double) (1 + prebuilt->table->stat_clustered_index_size / 5)); +} + +/************************************************************************* +Returns statistics information of the table to the MySQL interpreter, +in various fields of the handle object. */ + +void +ha_innobase::info( +/*==============*/ + uint flag) /* in: what information MySQL requests */ +{ + row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt; + dict_table_t* ib_table; + dict_index_t* index; + uint rec_per_key; + uint i; + + DBUG_ENTER("info"); + + ib_table = prebuilt->table; + + if (flag & HA_STATUS_TIME) { + /* In sql_show we call with this flag: update then statistics + so that they are up-to-date */ + + dict_update_statistics(ib_table); + } + + if (flag & HA_STATUS_VARIABLE) { + records = ib_table->stat_n_rows; + deleted = 0; + data_file_length = ((ulonglong) + ib_table->stat_clustered_index_size) + * UNIV_PAGE_SIZE; + index_file_length = ((ulonglong) + ib_table->stat_sum_of_other_index_sizes) + * UNIV_PAGE_SIZE; + delete_length = 0; + check_time = 0; + + if (records == 0) { + mean_rec_length = 0; + } else { + mean_rec_length = (ulong) data_file_length / records; + } + } + + if (flag & HA_STATUS_CONST) { + index = dict_table_get_first_index_noninline(ib_table); + + if (prebuilt->clust_index_was_generated) { + index = dict_table_get_next_index_noninline(index); + } + + for (i = 0; i < table->keys; i++) { + if (index->stat_n_diff_key_vals == 0) { + rec_per_key = records; + } else { + rec_per_key = records / + index->stat_n_diff_key_vals; + } + + table->key_info[i].rec_per_key[ + table->key_info[i].key_parts - 1] + = rec_per_key; + index = dict_table_get_next_index_noninline(index); + } + } + + if (flag & HA_STATUS_ERRKEY) { + + errkey = (unsigned int)-1; /* TODO: get the key number from + Innobase */ + } + + DBUG_VOID_RETURN; +} + +/***************************************************************** +Adds information about free space in the Innobase tablespace to a +table comment which is printed out when a user calls SHOW TABLE STATUS. */ + +char* +ha_innobase::update_table_comment( +/*==============================*/ + const char* comment) +{ + uint length=strlen(comment); + + char *str=my_malloc(length + 50,MYF(0)); + + if (!str) + return comment; + + sprintf(str,"%s Innobase free: %lu kB", comment,innobase_get_free_space()); + + return str; +} + + /**************************************************************************** Handling the shared INNOBASE_SHARE structure that is needed to provide table locking. ****************************************************************************/ -static byte* innobase_get_key(INNOBASE_SHARE *share,uint *length, +static mysql_byte* innobase_get_key(INNOBASE_SHARE *share,uint *length, my_bool not_used __attribute__((unused))) { *length=share->table_name_length; - return (byte*) share->table_name; + return (mysql_byte*) share->table_name; } static INNOBASE_SHARE *get_share(const char *table_name) @@ -2603,7 +2577,7 @@ static INNOBASE_SHARE *get_share(const char *table_name) pthread_mutex_lock(&innobase_mutex); uint length=(uint) strlen(table_name); if (!(share=(INNOBASE_SHARE*) hash_search(&innobase_open_tables, - (byte*) table_name, + (mysql_byte*) table_name, length))) { if ((share=(INNOBASE_SHARE *) my_malloc(sizeof(*share)+length+1, @@ -2612,7 +2586,7 @@ static INNOBASE_SHARE *get_share(const char *table_name) share->table_name_length=length; share->table_name=(char*) (share+1); strmov(share->table_name,table_name); - if (hash_insert(&innobase_open_tables, (byte*) share)) + if (hash_insert(&innobase_open_tables, (mysql_byte*) share)) { pthread_mutex_unlock(&innobase_mutex); my_free((gptr) share,0); @@ -2632,11 +2606,59 @@ static void free_share(INNOBASE_SHARE *share) pthread_mutex_lock(&innobase_mutex); if (!--share->use_count) { - hash_delete(&innobase_open_tables, (byte*) share); + hash_delete(&innobase_open_tables, (mysql_byte*) share); thr_lock_delete(&share->lock); pthread_mutex_destroy(&share->mutex); my_free((gptr) share, MYF(0)); } pthread_mutex_unlock(&innobase_mutex); } + +/********************************************************************* +Stores a MySQL lock into a 'lock' field in a handle. */ + +THR_LOCK_DATA** +ha_innobase::store_lock( +/*====================*/ + /* out: pointer to the next + element in the 'to' array */ + THD* thd, /* in: user thread handle */ + THR_LOCK_DATA** to, /* in: pointer to an array + of pointers to lock structs; + pointer to the 'lock' field + of current handle is stored + next to this array */ + enum thr_lock_type lock_type) /* in: lock type to store in + 'lock' */ +{ + row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt; + + if (lock_type == TL_READ_WITH_SHARED_LOCKS) { + /* This is a SELECT ... IN SHARE MODE */ + prebuilt->select_lock_type = LOCK_S; + } else { + /* We set possible LOCK_X value in external_lock, not yet + here even if this would be SELECT ... FOR UPDATE */ + prebuilt->select_lock_type = LOCK_NONE; + } + + if (lock_type != TL_IGNORE && lock.type == TL_UNLOCK) { + + /* If we are not doing a LOCK TABLE, then allow multiple + writers */ + + if ((lock_type >= TL_WRITE_CONCURRENT_INSERT && + lock_type <= TL_WRITE) && !thd->in_lock_tables) { + + lock_type = TL_WRITE_ALLOW_WRITE; + } + + lock.type=lock_type; + } + + *to++= &lock; + + return(to); +} + #endif /* HAVE_INNOBASE_DB */ diff --git a/sql/ha_innobase.h b/sql/ha_innobase.h index fc9d4892c9b..43dee4b1aae 100644 --- a/sql/ha_innobase.h +++ b/sql/ha_innobase.h @@ -1,18 +1,18 @@ /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB && Innobase Oy - + -This file is modified from ha_berkeley.h of MySQL distribution- This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ @@ -21,10 +21,6 @@ #pragma interface /* gcc class implementation */ #endif -/* Store the MySQL bool type definition to this defined type: -inside ha_innobase we use the Innobase definition of the bool type! */ -typedef bool mysql_bool; - /* This file defines the Innobase handler: the interface between MySQL and Innobase */ @@ -39,10 +35,8 @@ typedef struct st_innobase_share { /* The class defining a handle to an Innobase table */ class ha_innobase: public handler { - void* innobase_table_handle; /* (dict_table_t*) pointer to a table - in Innobase data dictionary cache */ void* innobase_prebuilt; /* (row_prebuilt_t*) prebuilt - structs in Innobase, used to save + struct in Innobase, used to save CPU */ THD* user_thd; /* the thread handle of the user currently using the handle; this is @@ -51,9 +45,6 @@ class ha_innobase: public handler INNOBASE_SHARE *share; gptr alloc_ptr; - byte* rec_buff; /* buffer used in converting - rows from MySQL format - to Innobase format */ byte* upd_buff; /* buffer used in updates */ byte* key_val_buff; /* buffer used in converting search key values from MySQL format @@ -62,12 +53,15 @@ class ha_innobase: public handler 'ref' buffer of the handle, if any */ ulong int_option_flag; uint primary_key; + ulong start_of_scan; /* this is set to 1 when we are + starting a table scan but have not + yet fetched any row, else 0 */ uint last_dup_key; uint last_match_mode;/* match mode of the latest search: - ROW_SEL_EXACT or - ROW_SEL_EXACT_PREFIX or undefined */ - + ROW_SEL_EXACT, ROW_SEL_EXACT_PREFIX, + or undefined */ + ulong max_row_length(const byte *buf); uint store_key_val_for_row(uint keynr, char* buff, const byte* record); @@ -78,16 +72,16 @@ class ha_innobase: public handler /* Init values for the class: */ public: ha_innobase(TABLE *table): handler(table), - rec_buff(0), int_option_flag(HA_READ_NEXT | HA_READ_PREV | HA_READ_ORDER | HA_REC_NOT_IN_SEQ | HA_KEYPOS_TO_RNDPOS | HA_LASTKEY_ORDER | HA_HAVE_KEY_READ_ONLY | HA_READ_NOT_EXACT_KEY | - HA_LONGLONG_KEYS | HA_NULL_KEY | HA_NO_BLOBS | + HA_LONGLONG_KEYS | HA_NULL_KEY | HA_NOT_EXACT_COUNT | HA_NO_WRITE_DELAYED | HA_PRIMARY_KEY_IN_READ_INDEX | HA_DROP_BEFORE_CREATE), - last_dup_key((uint) -1) + last_dup_key((uint) -1), + start_of_scan(0) { } ~ha_innobase() {} @@ -145,6 +139,8 @@ class ha_innobase: public handler int delete_table(const char *name); int rename_table(const char* from, const char* to); + char* update_table_comment(const char* comment); + THR_LOCK_DATA **store_lock(THD *thd, THR_LOCK_DATA **to, enum thr_lock_type lock_type); }; @@ -157,7 +153,7 @@ extern long innobase_lock_scan_time; extern long innobase_mirrored_log_groups, innobase_log_files_in_group; extern long innobase_log_file_size, innobase_log_buffer_size; extern long innobase_buffer_pool_size, innobase_additional_mem_pool_size; -extern long innobase_file_io_threads; +extern long innobase_file_io_threads, innobase_lock_wait_timeout; extern char *innobase_data_home_dir, *innobase_data_file_path; extern char *innobase_log_group_home_dir, *innobase_log_arch_dir; extern bool innobase_flush_log_at_trx_commit, innobase_log_archive, @@ -168,6 +164,7 @@ extern TYPELIB innobase_lock_typelib; bool innobase_init(void); bool innobase_end(void); bool innobase_flush_logs(void); +uint innobase_get_free_space(void); int innobase_commit(THD *thd, void* trx_handle); int innobase_rollback(THD *thd, void* trx_handle); diff --git a/sql/handler.h b/sql/handler.h index a4ebe9b51ff..ed3683acbd2 100644 --- a/sql/handler.h +++ b/sql/handler.h @@ -234,9 +234,10 @@ public: virtual int index_last(byte * buf)=0; virtual int index_next_same(byte *buf, const byte *key, uint keylen); virtual int ft_init() - { return -1; } - virtual void *ft_init_ext(uint inx,const byte *key, uint keylen, bool presort) - { return (void *)NULL; } + { return -1; } + virtual void *ft_init_ext(uint inx,const byte *key, uint keylen, + bool presort) + { return (void *)NULL; } virtual int ft_read(byte *buf) { return -1; } virtual int rnd_init(bool scan=1)=0; virtual int rnd_end() { return 0; } @@ -275,6 +276,9 @@ public: // not implemented by default virtual int net_read_dump(NET* net) { return ER_DUMP_NOT_IMPLEMENTED; } + virtual char *update_table_comment(const char * comment) + { return (char*) comment;} + virtual void append_create_info(String *packet) {} /* The following can be called without an open handler */ virtual const char *table_type() const =0; diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc index ca33e9b2367..bb8f8591577 100644 --- a/sql/sql_delete.cc +++ b/sql/sql_delete.cc @@ -142,6 +142,10 @@ int mysql_delete(THD *thd,TABLE_LIST *table_list,COND *conds,ha_rows limit, (SPECIAL_NO_NEW_FUNC | SPECIAL_SAFE_MODE)) && !(thd->options & (OPTION_NOT_AUTO_COMMIT | OPTION_BEGIN))); + /* We need to add code to not generate table based on the table type */ +#ifdef HAVE_INNOBASE_DB + use_generate_table=0; +#endif if (use_generate_table && ! thd->open_tables) { error=generate_table(thd,table_list,(TABLE*) 0); diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 5a2f672368b..af851733af9 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -487,10 +487,9 @@ mysql_select(THD *thd,TABLE_LIST *tables,List<Item> &fields,COND *conds, { for (uint i_h = join.const_tables; i_h < join.tables; i_h++) { - JOIN_TAB* tab_h = join.join_tab + i_h; - TABLE* table_h = tab_h->table; + TABLE* table_h = join.join_tab[i_h].table; if (table_h->db_type == DB_TYPE_INNOBASE) - table_h->file->extra(HA_EXTRA_RESTORE_POS); + table_h->file->extra(HA_EXTRA_DONT_USE_CURSOR_TO_UPDATE); } } #endif diff --git a/sql/sql_show.cc b/sql/sql_show.cc index eda6731307b..9f82c9f15c3 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -393,8 +393,12 @@ int mysqld_extend_show_tables(THD *thd,const char *db,const char *wild) net_store_data(packet, option_buff+1, (ptr == option_buff ? 0 : (uint) (ptr-option_buff)-1)); } - net_store_data(packet, table->comment); - + { + char *comment=table->file->update_table_comment(table->comment); + net_store_data(packet, comment); + if (comment != table->comment) + my_free(comment,MYF(0)); + } close_thread_tables(thd,0); } if (my_net_write(&thd->net,(char*) packet->ptr(), @@ -784,7 +788,7 @@ store_create_info(THD *thd, TABLE *table, String *packet) Field **ptr,*field; for (ptr=table->field ; (field= *ptr); ptr++) { - if(ptr != table->field) + if (ptr != table->field) packet->append(",\n", 2); uint flags = field->flags; @@ -792,7 +796,7 @@ store_create_info(THD *thd, TABLE *table, String *packet) append_identifier(thd,packet,field->field_name); packet->append(' '); // check for surprises from the previous call to Field::sql_type() - if(type.ptr() != tmp) + if (type.ptr() != tmp) type.set(tmp, sizeof(tmp)); field->sql_type(type); @@ -812,7 +816,7 @@ store_create_info(THD *thd, TABLE *table, String *packet) type.set(tmp,sizeof(tmp)); field->val_str(&type,&type); packet->append('\''); - if(type.length()) + if (type.length()) append_unescaped(packet, type.c_ptr()); packet->append('\''); } @@ -835,15 +839,15 @@ store_create_info(THD *thd, TABLE *table, String *packet) packet->append(",\n ", 4); KEY_PART_INFO *key_part= key_info->key_part; - if(i == primary_key) + if (i == primary_key) packet->append("PRIMARY ", 8); - else if(key_info->flags & HA_NOSAME) + else if (key_info->flags & HA_NOSAME) packet->append("UNIQUE ", 7); - else if(key_info->flags & HA_FULLTEXT) + else if (key_info->flags & HA_FULLTEXT) packet->append("FULLTEXT ", 9); packet->append("KEY ", 4); - if(i != primary_key) + if (i != primary_key) append_identifier(thd,packet,key_info->name); packet->append(" (", 2); @@ -877,14 +881,14 @@ store_create_info(THD *thd, TABLE *table, String *packet) char buff[128]; char* p; - if(table->min_rows) + if (table->min_rows) { packet->append(" MIN_ROWS="); p = longlong10_to_str(table->min_rows, buff, 10); packet->append(buff, (uint) (p - buff)); } - if(table->max_rows) + if (table->max_rows) { packet->append(" MAX_ROWS="); p = longlong10_to_str(table->max_rows, buff, 10); @@ -899,6 +903,7 @@ store_create_info(THD *thd, TABLE *table, String *packet) packet->append(" CHECKSUM=1", 11); if (table->db_create_options & HA_OPTION_DELAY_KEY_WRITE) packet->append(" DELAY_KEY_WRITE=1",18); + table->file->append_create_info(packet); if (table->comment && table->comment[0]) { packet->append(" COMMENT='", 10); diff --git a/sql/sql_update.cc b/sql/sql_update.cc index b7b9b23d79d..d17b5768440 100644 --- a/sql/sql_update.cc +++ b/sql/sql_update.cc @@ -20,23 +20,21 @@ #include "mysql_priv.h" #include "sql_acl.h" -#define MAX_ULONG_BIT ((ulong) 1 << (sizeof(ulong)*8-1)); +#define MAX_ULONG_BIT ((ulong) 1 << (sizeof(ulong)*8-1)) /* Return 0 if row hasn't changed */ -static bool compare_record(TABLE *table) +static bool compare_record(TABLE *table, ulong query_id) { if (!table->blob_fields) return cmp_record(table,1); - ulong current_query_id=current_thd->query_id; - if (memcmp(table->null_flags, table->null_flags+table->rec_buff_length, table->null_bytes)) return 1; // Diff in NULL value for (Field **ptr=table->field ; *ptr ; ptr++) { - if ((*ptr)->query_id == current_query_id && + if ((*ptr)->query_id == query_id && (*ptr)->cmp_binary_offset(table->rec_buff_length)) return 1; } @@ -53,7 +51,8 @@ int mysql_update(THD *thd,TABLE_LIST *table_list,List<Item> &fields, bool using_limit=limit != HA_POS_ERROR; bool used_key_is_modified, using_transactions; int error=0; - uint save_time_stamp, used_index; + uint save_time_stamp, used_index, want_privilege; + ulong query_id=thd->query_id, timestamp_query_id; key_map old_used_keys; TABLE *table; SQL_SELECT *select; @@ -67,35 +66,45 @@ int mysql_update(THD *thd,TABLE_LIST *table_list,List<Item> &fields, table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK); thd->proc_info="init"; + /* Calculate "table->used_keys" based on the WHERE */ + table->used_keys=table->keys_in_use; + table->quick_keys=0; + want_privilege=table->grant.want_privilege; + table->grant.want_privilege=(SELECT_ACL & ~table->grant.privilege); + if (setup_conds(thd,table_list,&conds)) + DBUG_RETURN(-1); /* purecov: inspected */ + old_used_keys=table->used_keys; // Keys used in WHERE + /* - ** Find the offsets of the given fields and condition + Change the query_id for the timestamp column so that we can + check if this is modified directly */ + if (table->timestamp_field) + { + timestamp_query_id=table->timestamp_field->query_id; + table->timestamp_field->query_id=thd->query_id-1; + } + /* Check the fields we are going to modify */ + table->grant.want_privilege=want_privilege; if (setup_fields(thd,table_list,fields,1,0)) - DBUG_RETURN(-1); - table->grant.want_privilege=(SELECT_ACL & ~table->grant.privilege); - if (table->timestamp_field && // Don't set timestamp if used - table->timestamp_field->query_id == thd->query_id) - table->time_stamp=0; + DBUG_RETURN(-1); /* purecov: inspected */ + if (table->timestamp_field) + { + // Don't set timestamp column if this is modified + if (table->timestamp_field->query_id == thd->query_id) + table->time_stamp=0; + else + table->timestamp_field->query_id=timestamp_query_id; + } - /* Change query_id so that ->used_keys is based on the WHERE */ - table->used_keys=table->keys_in_use; - table->quick_keys=0; - ulong query_id=thd->query_id; - thd->query_id^= MAX_ULONG_BIT; - if (setup_fields(thd,table_list,values,0,0) || - setup_conds(thd,table_list,&conds)) + /* Check values */ + table->grant.want_privilege=(SELECT_ACL & ~table->grant.privilege); + if (setup_fields(thd,table_list,values,0,0)) { table->time_stamp=save_time_stamp; // Restore timestamp pointer DBUG_RETURN(-1); /* purecov: inspected */ } - old_used_keys=table->used_keys; - /* Restore query_id for compare_record */ - thd->query_id=query_id; - List_iterator<Item> it(fields); - Item *item; - while ((item=it++)) - ((Item_field*) item)->field->query_id=query_id; // Don't count on usage of 'only index' when calculating which key to use table->used_keys=0; @@ -141,6 +150,7 @@ int mysql_update(THD *thd,TABLE_LIST *table_list,List<Item> &fields, ** We can't update table directly; We must first search after all ** matching rows before updating the table! */ + table->file->extra(HA_EXTRA_DONT_USE_CURSOR_TO_UPDATE); IO_CACHE tempfile; if (open_cached_file(&tempfile, mysql_tmpdir,TEMP_PREFIX, DISK_BUFFER_SIZE, MYF(MY_WME))) @@ -218,6 +228,7 @@ int mysql_update(THD *thd,TABLE_LIST *table_list,List<Item> &fields, thd->count_cuted_fields=1; /* calc cuted fields */ thd->cuted_fields=0L; thd->proc_info="updating"; + query_id=thd->query_id; while (!(error=info.read_record(&info)) && !thd->killed) { @@ -227,7 +238,7 @@ int mysql_update(THD *thd,TABLE_LIST *table_list,List<Item> &fields, if (fill_record(fields,values)) break; found++; - if (compare_record(table)) + if (compare_record(table, query_id)) { if (!(error=table->file->update_row((byte*) table->record[1], (byte*) table->record[0]))) |