mariadb/myisam/mi_create.c
unknown a8ea31fae6 Add support for up to VARCHAR (size up to 65535)
Renamed HA_VAR_LENGTH to HA_VAR_LENGTH_PART
Renamed in all files FIELD_TYPE_STRING and FIELD_TYPE_VAR_STRING to MYSQL_TYPE_STRING and MYSQL_TYPE_VAR_STRING to make it easy to catch all possible errors
Added support for VARCHAR KEYS to heap
Removed support for ISAM
Now only long VARCHAR columns are changed to TEXT on demand (not CHAR)
Internal temporary files can now use fixed length tables if the used VARCHAR columns are short


BitKeeper/deleted/.del-ha_isam.cc~4dce65904db2675e:
  Delete: sql/ha_isam.cc
BitKeeper/deleted/.del-_cache.c~b5d80b5c3ae233b1:
  Delete: isam/_cache.c
BitKeeper/deleted/.del-_dbug.c~88d7964ae5e3c9bd:
  Delete: isam/_dbug.c
BitKeeper/deleted/.del-_dynrec.c~48dd758f5a5450df:
  Delete: isam/_dynrec.c
BitKeeper/deleted/.del-_key.c~ce62d47a6c681084:
  Delete: isam/_key.c
BitKeeper/deleted/.del-_locking.c~dea4cdc6ea425c67:
  Delete: isam/_locking.c
BitKeeper/deleted/.del-_packrec.c~47ae1b16c007e9be:
  Delete: isam/_packrec.c
BitKeeper/deleted/.del-_page.c~148b1a613d052ee8:
  Delete: isam/_page.c
BitKeeper/deleted/.del-_search.c~f509292aa1ff18ff:
  Delete: isam/_search.c
BitKeeper/deleted/.del-_statrec.c~58d9263b3475d58b:
  Delete: isam/_statrec.c
BitKeeper/deleted/.del-changed.c~d075de80a314b02d:
  Delete: isam/changed.c
BitKeeper/deleted/.del-close.c~fd62629496ee5bcc:
  Delete: isam/close.c
BitKeeper/deleted/.del-create.c~96cecc433c0c2242:
  Delete: isam/create.c
BitKeeper/deleted/.del-delete.c~65ee8daaa75a14b6:
  Delete: isam/delete.c
BitKeeper/deleted/.del-extra.c~706f29d72beb2565:
  Delete: isam/extra.c
BitKeeper/deleted/.del-info.c~96cfb747af8da0d:
  Delete: isam/info.c
BitKeeper/deleted/.del-isamchk.c~c0f59c2687d2248f:
  Delete: isam/isamchk.c
BitKeeper/deleted/.del-isamlog.c~85b6b31c6e2b8519:
  Delete: isam/isamlog.c
BitKeeper/deleted/.del-log.c~55a973013d55cade:
  Delete: isam/log.c
BitKeeper/deleted/.del-open.c~95b3b75042fae00a:
  Delete: isam/open.c
BitKeeper/deleted/.del-pack_isam.c~43801f0df7504834:
  Delete: isam/pack_isam.c
BitKeeper/deleted/.del-panic.c~f7fd71605324f8f3:
  Delete: isam/panic.c
BitKeeper/deleted/.del-range.c~142f1f8ac4948082:
  Delete: isam/range.c
BitKeeper/deleted/.del-rfirst.c~66f494291dc005d3:
  Delete: isam/rfirst.c
BitKeeper/deleted/.del-rkey.c~cc54c6498352f999:
  Delete: isam/rkey.c
BitKeeper/deleted/.del-rlast.c~d1fe1866139e9866:
  Delete: isam/rlast.c
BitKeeper/deleted/.del-rnext.c~b308eaa1e11ea7de:
  Delete: isam/rnext.c
BitKeeper/deleted/.del-rprev.c~b359f71fdea4bbce:
  Delete: isam/rprev.c
BitKeeper/deleted/.del-rrnd.c~7fcfcce88d4a5200:
  Delete: isam/rrnd.c
BitKeeper/deleted/.del-rsame.c~75a62d5548103a15:
  Delete: isam/rsame.c
BitKeeper/deleted/.del-rsamepos.c~5b5652dd2cda6d5d:
  Delete: isam/rsamepos.c
BitKeeper/deleted/.del-sort.c~e2e56b5a37ce86f4:
  Delete: isam/sort.c
BitKeeper/deleted/.del-static.c~3a1354b84f4a9cc7:
  Delete: isam/static.c
BitKeeper/deleted/.del-test1.c~64d52e9412d457ed:
  Delete: isam/test1.c
BitKeeper/deleted/.del-test2.c~2f9a632cab572958:
  Delete: isam/test2.c
BitKeeper/deleted/.del-test3.c~e8a7a4afe8a087:
  Delete: isam/test3.c
BitKeeper/deleted/.del-isamdef.h~ac8d49e7e2201c66:
  Delete: isam/isamdef.h
BitKeeper/deleted/.del-update.c~670264f51dc44934:
  Delete: isam/update.c
BitKeeper/deleted/.del-write.c~8f1918b1f6770e54:
  Delete: isam/write.c
BitKeeper/deleted/.del-Makefile.am~6cfa0db5e7778d09:
  Delete: isam/Makefile.am
BitKeeper/deleted/.del-make-ccc~3ee55391eda0b0ab:
  Delete: isam/make-ccc
BitKeeper/deleted/.del-ChangeLog~208984fb7a51e568:
  Delete: isam/ChangeLog
BitKeeper/deleted/.del-test_all.res~c2aafb49a3a77db7:
  Delete: isam/test_all.res
BitKeeper/deleted/.del-test_all~93c701e44a9c5b65:
  Delete: isam/test_all
BitKeeper/deleted/.del-.cvsignore~54f6f0f2d5012561:
  Delete: isam/.cvsignore
BitKeeper/deleted/.del-ha_isammrg.cc~dc682e4755d77a2e:
  Delete: sql/ha_isammrg.cc
BitKeeper/deleted/.del-ha_isam.h~bf53d533be3d3927:
  Delete: sql/ha_isam.h
BitKeeper/deleted/.del-ha_isammrg.h~66fd2e5bfe7207dc:
  Delete: sql/ha_isammrg.h
acinclude.m4:
  Remove ISAM
client/mysqldump.c:
  FIELD_TYPE -> MYSQL_TYPE
client/mysqltest.c:
  Add missing DBUG_RETURN
configure.in:
  Remove ISAM
heap/heapdef.h:
  Add support for VARCHAR
heap/hp_create.c:
  Add support for VARCHAR
heap/hp_delete.c:
  Add support for VARCHAR
heap/hp_hash.c:
  Add support for VARCHAR
  (VARCHAR keys was not supported before)
heap/hp_rkey.c:
  Add support for VARCHAR
heap/hp_update.c:
  Add support for VARCHAR
heap/hp_write.c:
  Add support for VARCHAR
  (Added flag SEARCH_UPDATE to mark that this is an update)
include/decimal.h:
  Remove not needed my_global.h
include/m_ctype.h:
  Add support for VARCHAR
include/my_base.h:
  Add support for VARCHAR
include/my_handler.h:
  Moved general purpose macro from MyISAM code
include/mysql_com.h:
  Add support for VARCHAR
libmysql/libmysql.c:
  Add support for VARCHAR
libmysqld/Makefile.am:
  Removed ISAM
myisam/ft_static.c:
  Add support for VARCHAR
myisam/ft_test1.c:
  Add support for VARCHAR
myisam/ft_update.c:
  Add support for VARCHAR
myisam/mi_check.c:
  Add support for VARCHAR
myisam/mi_create.c:
  Add support for VARCHAR
  - VARCHAR key segments are marked with HA_VAR_LENGTH_PART
myisam/mi_key.c:
  Add support for VARCHAR
  Fixed bug in old VARCHAR code when reading index-only
myisam/mi_range.c:
  Fixed comment style
myisam/mi_rnext_same.c:
  Handle case where equal keys can be of different length
myisam/mi_search.c:
  Add support for VARCHAR
myisam/mi_test1.c:
  Add support for VARCHAR
myisam/mi_unique.c:
  Add support for VARCHAR
  (Some new code to handle keys that are equal but have different lengths)
myisam/mi_write.c:
  Fixed comment
myisam/myisamchk.c:
  Better infotext if wrong type
mysql-test/r/bdb.result:
  Updated old result and new results for VARCHAR
mysql-test/r/create.result:
  Updated old result and new results for VARCHAR
mysql-test/r/ctype_tis620.result:
  Updated old result and new results for VARCHAR
  (Old code sorted tis620 wrong)
mysql-test/r/ctype_ucs.result:
  Updated old result and new results for VARCHAR
mysql-test/r/endspace.result:
  Updated old result and new results for VARCHAR
mysql-test/r/func_like.result:
  Updated old result and new results for VARCHAR
mysql-test/r/heap.result:
  Updated old result and new results for VARCHAR
mysql-test/r/innodb.result:
  Updated old result. This will change a bit when also InnoDB supports VARCHAR
mysql-test/r/merge.result:
  Updated old result and new results for VARCHAR
mysql-test/r/myisam.result:
  Updated old result and new results for VARCHAR
mysql-test/r/mysqldump.result:
  Updated old result and new results for VARCHAR
mysql-test/r/order_by.result:
  Updated old result and new results for VARCHAR
  (Key length is different for VARCHAR)
mysql-test/r/ps.result:
  Updated old result and new results for VARCHAR
mysql-test/r/ps_1general.result:
  Updated results for new .frm version
  Don't print seconds in show full process list as this may change
mysql-test/r/ps_2myisam.result:
  Updated old result and new results for VARCHAR
mysql-test/r/ps_3innodb.result:
  Updated old result and new results for VARCHAR
mysql-test/r/ps_4heap.result:
  Updated old result and new results for VARCHAR
mysql-test/r/ps_5merge.result:
  Updated old result and new results for VARCHAR
mysql-test/r/ps_6bdb.result:
  Updated old result and new results for VARCHAR
mysql-test/r/select.result.es:
  Updated results by hand
mysql-test/r/select.result:
  Updated old result and new results for VARCHAR
mysql-test/r/select_found.result:
  Updated old result and new results for VARCHAR
mysql-test/r/show_check.result:
  Updated old result and new results for VARCHAR
mysql-test/r/strict.result:
  Updated old result and new results for VARCHAR
mysql-test/r/subselect.result:
  Updated old result and new results for VARCHAR
mysql-test/r/system_mysql_db.result:
  Updated old result and new results for VARCHAR
mysql-test/r/type_blob.result:
  Updated old result and new results for VARCHAR
mysql-test/r/type_ranges.result:
  Updated old result and new results for VARCHAR
mysql-test/r/type_ranges.result.es:
  Updated some results by hand
mysql-test/t/bdb.test:
  Test VARCHAR
mysql-test/t/ctype_ucs.test:
  Some fixes related to VARCHAR
mysql-test/t/endspace.test:
  Fixes to make it easier to compare columns with end space
mysql-test/t/heap.test:
  Test VARCHAR
mysql-test/t/innodb.test:
  Prepare for testing VARCHAR
mysql-test/t/myisam.test:
  Test VARCHAR
mysql-test/t/ps_1general.test:
  Don't show seconds for show processlist
mysql-test/t/ps_4heap.test:
  Update for VARCHAR
mysql-test/t/strict.test:
  Fix test for VARCHAR
mysql-test/t/type_blob.test:
  Update test for VARCHAR
  Note that now you can't store 'a' and 'a ' in an unique varchar/text index if the column is not binary
mysys/my_handler.c:
  Add support for VARCHAR
ndb/src/common/util/NdbSqlUtil.cpp:
  Fix for usage of strnncollsp
scripts/mysql_fix_privilege_tables.sh:
  Simple fix so that my_print_defaults works
sql/Makefile.am:
  Remove ISAM
sql/field.cc:
  Add support for VARCHAR
  Fixed the keys for blob's are compared with strnncollsp
  Ensure that old tables from MySQL 4.0 works as they did before.
  (Old VARCHAR will be converted to new VARCHAR on ALTER TABLE)
sql/field.h:
  Add support for VARCHAR
sql/field_conv.cc:
  Change FIELD_TYPE_VAR_STRING -> MYSQL_TYPE_VARCHAR
  Added usage of HA_KEY_BLOB_LENGTH
sql/ha_berkeley.cc:
  Add support for VARCHAR
  Added usage of table->insert_or_update if we would ever want to know in key_cmp if we are changing keys
sql/ha_heap.cc:
  Add support for VARCHAR
sql/ha_innodb.cc:
  Changed MYSQL_TYPE_VAR_STRING to MYSQL_TYPE_VARCHAR.
  Waiting for Heikki to add full VARCHAR support
sql/ha_innodb.h:
  InnoDB doesn't support full VARCHAR yet
sql/ha_myisam.cc:
  Add support for VARCHAR
sql/ha_ndbcluster.cc:
  Add support for VARCHAR
sql/handler.h:
  Added HA_NO_VARCHAR for table handler that doesn't support VARCHAR. In this case MySQL will create a normal CHAR instead
sql/item.cc:
  Fixed access of already freed memory
  Added support of VARCHAR
  - varchar length is now checked in mysql_prepare
sql/item_cmpfunc.cc:
  Added new parameter to strncollsp
sql/item_sum.cc:
  Added new parameter to strncollsp
  FIELD_TYPE -> MYSQL_TYPE
sql/key.cc:
  Add support for VARCHAR
sql/opt_range.cc:
  Remove character set parameter from set_key_image()
sql/opt_sum.cc:
  Remove character set parameter from set_key_image()
sql/protocol.cc:
  Return MYSQL_TYPE_VAR_STRING instead of MYSQL_TYPE_VARCHAR to clients (to not cause compatiblity problems)
sql/sql_acl.cc:
  Change key handling code so that we can use CHAR or VARCHAR for the user table columns
sql/sql_base.cc:
  Remove old, not used code
sql/sql_help.cc:
  Remove charset from get_key_image
sql/sql_parse.cc:
  Ensure that OPTION_TABLE_LOCK is cleared ASAP; This fixed a problem in BDB transaction code when one used LOCK TABLES on a BDB table
  Added support for VARCHAR
  Moved field length checking and VARCHAR -> TEXT convert to mysql_prepare (as we need the know the character set for the column)
sql/sql_select.cc:
  Added support of VARCHAR
  Added heuristic to use fixed size rows for tmp tables if we are using only a few short VARCHAR's
sql/sql_string.cc:
  Added extra argument to strnncollsp
sql/sql_table.cc:
  Add support for VARCHAR
  Automaticly convert (with warning) big VARCHAR (but not CHAR) to TEXT
  If handler doesn't support VARCHAR convert VARCHAR to CHAR
sql/sql_update.cc:
  Fixed compiler warning
sql/sql_yacc.yy:
  Add support for VARCHAR
sql/strfunc.cc:
  Fixed valgrind warning
sql/structs.h:
  Added 'table' to KEY structure to make life easier for some handler functions
sql/table.cc:
  Add support for VARCHAR
  - New .frm version
  - FIELD_TYPE -> MYSQL_TYPE
sql/table.h:
  Added insert_or_update; A bool flag a handler can set/reset if needed (for handler internal usage)
sql/unireg.h:
  Add support for VARCHAR
strings/ctype-big5.c:
  Added new argument to strnncollsp() to allow one to define if end space are significant or not
  Changed my_like_range... to correctly calculate min_length & max_length
strings/ctype-bin.c:
  Added new argument to strnncollsp() to allow one to define if end space are significant or not
strings/ctype-czech.c:
  Changed my_like_range... to correctly calculate min_length & max_length
strings/ctype-gbk.c:
  Added new argument to strnncollsp() to allow one to define if end space are significant or not
  Changed my_like_range... to correctly calculate min_length & max_length
strings/ctype-latin1.c:
  Added new argument to strnncollsp() to allow one to define if end space are significant or not
strings/ctype-mb.c:
  Added new argument to strnncollsp() to allow one to define if end space are significant or not
  Changed my_like_range... to correctly calculate min_length & max_length
strings/ctype-simple.c:
  Added new argument to strnncollsp() to allow one to define if end space are significant or not
  Changed my_like_range... to correctly calculate min_length & max_length
strings/ctype-sjis.c:
  Added new argument to strnncollsp() to allow one to define if end space are significant or not
  Changed my_like_range... to correctly calculate min_length & max_length
strings/ctype-tis620.c:
  Added new argument to strnncollsp() to allow one to define if end space are significant or not
  Changed my_like_range... to correctly calculate min_length & max_length
strings/ctype-uca.c:
  Added new argument to strnncollsp() to allow one to define if end space are significant or not
strings/ctype-ucs2.c:
  Changed my_like_range... to correctly calculate min_length & max_length
strings/ctype-utf8.c:
  Added new argument to strnncollsp() to allow one to define if end space are significant or not
strings/ctype-win1250ch.c:
  Changed my_like_range... to correctly calculate min_length & max_length
strings/decimal.c:
  Fixed include files usage
  Fixed some compiler warnings
tests/client_test.c:
  Ensure tests works with VARCHAR
2004-12-06 02:00:37 +02:00

733 lines
22 KiB
C
Raw Blame History

/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
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 */
/* Create a MyISAM table */
#include "fulltext.h"
#include "sp_defs.h"
#if defined(MSDOS) || defined(__WIN__)
#ifdef __WIN__
#include <fcntl.h>
#else
#include <process.h> /* Prototype for getpid */
#endif
#endif
#include <m_ctype.h>
/*
** Old options is used when recreating database, from isamchk
*/
int mi_create(const char *name,uint keys,MI_KEYDEF *keydefs,
uint columns, MI_COLUMNDEF *recinfo,
uint uniques, MI_UNIQUEDEF *uniquedefs,
MI_CREATE_INFO *ci,uint flags)
{
register uint i,j;
File dfile,file;
int errpos,save_errno;
myf create_flag;
uint fields,length,max_key_length,packed,pointer,
key_length,info_length,key_segs,options,min_key_length_skip,
base_pos,varchar_count,long_varchar_count,varchar_length,
max_key_block_length,unique_key_parts,fulltext_keys,offset;
ulong reclength, real_reclength,min_pack_length;
char filename[FN_REFLEN],linkname[FN_REFLEN], *linkname_ptr;
ulong pack_reclength;
ulonglong tot_length,max_rows, tmp;
enum en_fieldtype type;
MYISAM_SHARE share;
MI_KEYDEF *keydef,tmp_keydef;
MI_UNIQUEDEF *uniquedef;
HA_KEYSEG *keyseg,tmp_keyseg;
MI_COLUMNDEF *rec;
ulong *rec_per_key_part;
my_off_t key_root[MI_MAX_POSSIBLE_KEY],key_del[MI_MAX_KEY_BLOCK_SIZE];
MI_CREATE_INFO tmp_create_info;
DBUG_ENTER("mi_create");
if (!ci)
{
bzero((char*) &tmp_create_info,sizeof(tmp_create_info));
ci=&tmp_create_info;
}
if (keys + uniques > MI_MAX_KEY || columns == 0)
{
DBUG_RETURN(my_errno=HA_WRONG_CREATE_OPTION);
}
LINT_INIT(dfile);
LINT_INIT(file);
pthread_mutex_lock(&THR_LOCK_myisam);
errpos=0;
options=0;
bzero((byte*) &share,sizeof(share));
if (flags & HA_DONT_TOUCH_DATA)
{
if (!(ci->old_options & HA_OPTION_TEMP_COMPRESS_RECORD))
options=ci->old_options &
(HA_OPTION_COMPRESS_RECORD | HA_OPTION_PACK_RECORD |
HA_OPTION_READ_ONLY_DATA | HA_OPTION_CHECKSUM |
HA_OPTION_TMP_TABLE | HA_OPTION_DELAY_KEY_WRITE);
else
options=ci->old_options &
(HA_OPTION_CHECKSUM | HA_OPTION_TMP_TABLE | HA_OPTION_DELAY_KEY_WRITE);
}
if (ci->reloc_rows > ci->max_rows)
ci->reloc_rows=ci->max_rows; /* Check if wrong parameter */
if (!(rec_per_key_part=
(ulong*) my_malloc((keys + uniques)*MI_MAX_KEY_SEG*sizeof(long),
MYF(MY_WME | MY_ZEROFILL))))
DBUG_RETURN(my_errno);
/* Start by checking fields and field-types used */
reclength=varchar_count=varchar_length=long_varchar_count=packed=
min_pack_length=pack_reclength=0;
for (rec=recinfo, fields=0 ;
fields != columns ;
rec++,fields++)
{
reclength+=rec->length;
if ((type=(enum en_fieldtype) rec->type) != FIELD_NORMAL &&
type != FIELD_CHECK)
{
packed++;
if (type == FIELD_BLOB)
{
share.base.blobs++;
if (pack_reclength != INT_MAX32)
{
if (rec->length == 4+mi_portable_sizeof_char_ptr)
pack_reclength= INT_MAX32;
else
pack_reclength+=(1 << ((rec->length-mi_portable_sizeof_char_ptr)*8)); /* Max blob length */
}
}
else if (type == FIELD_SKIP_PRESPACE ||
type == FIELD_SKIP_ENDSPACE)
{
if (pack_reclength != INT_MAX32)
pack_reclength+= rec->length > 255 ? 2 : 1;
min_pack_length++;
}
else if (type == FIELD_VARCHAR)
{
varchar_count++;
varchar_length+=rec->length-2;
packed--;
pack_reclength+=1;
if (test(rec->length > 257))
{ /* May be packed on 3 bytes */
long_varchar_count++;
pack_reclength+=2;
}
}
else if (type != FIELD_SKIP_ZERO)
{
min_pack_length+=rec->length;
packed--; /* Not a pack record type */
}
}
else /* FIELD_NORMAL */
min_pack_length+=rec->length;
}
if ((packed & 7) == 1)
{ /* Bad packing, try to remove a zero-field */
while (rec != recinfo)
{
rec--;
if (rec->type == (int) FIELD_SKIP_ZERO && rec->length == 1)
{
rec->type=(int) FIELD_NORMAL;
packed--;
min_pack_length++;
break;
}
}
}
if (packed || (flags & HA_PACK_RECORD))
options|=HA_OPTION_PACK_RECORD; /* Must use packed records */
/* We can't use checksum with static length rows */
if (!(options & HA_OPTION_PACK_RECORD))
options&= ~HA_OPTION_CHECKSUM;
if (options & (HA_OPTION_PACK_RECORD | HA_OPTION_COMPRESS_RECORD))
min_pack_length+=varchar_count; /* Min length to pack */
else
{
min_pack_length+=varchar_length+2*varchar_count;
}
if (flags & HA_CREATE_TMP_TABLE)
options|= HA_OPTION_TMP_TABLE;
if (flags & HA_CREATE_CHECKSUM || (options & HA_OPTION_CHECKSUM))
{
options|= HA_OPTION_CHECKSUM;
min_pack_length++;
}
if (flags & HA_CREATE_DELAY_KEY_WRITE)
options|= HA_OPTION_DELAY_KEY_WRITE;
packed=(packed+7)/8;
if (pack_reclength != INT_MAX32)
pack_reclength+= reclength+packed +
test(test_all_bits(options, HA_OPTION_CHECKSUM | HA_PACK_RECORD));
min_pack_length+=packed;
if (!ci->data_file_length)
{
if (ci->max_rows == 0 || pack_reclength == INT_MAX32)
ci->data_file_length= INT_MAX32-1; /* Should be enough */
else if ((~(ulonglong) 0)/ci->max_rows < (ulonglong) pack_reclength)
ci->data_file_length= ~(ulonglong) 0;
else
ci->data_file_length=(ulonglong) ci->max_rows*pack_reclength;
}
else if (!ci->max_rows)
ci->max_rows=(ha_rows) (ci->data_file_length/(min_pack_length +
((options & HA_OPTION_PACK_RECORD) ?
3 : 0)));
if (options & (HA_OPTION_COMPRESS_RECORD | HA_OPTION_PACK_RECORD))
pointer=mi_get_pointer_length(ci->data_file_length,myisam_data_pointer_size);
else
pointer=mi_get_pointer_length(ci->max_rows,myisam_data_pointer_size);
if (!(max_rows=(ulonglong) ci->max_rows))
max_rows= ((((ulonglong) 1 << (pointer*8)) -1) / min_pack_length);
real_reclength=reclength;
if (!(options & (HA_OPTION_COMPRESS_RECORD | HA_OPTION_PACK_RECORD)))
{
if (reclength <= pointer)
reclength=pointer+1; /* reserve place for delete link */
}
else
reclength+=long_varchar_count; /* We need space for this! */
max_key_length=0; tot_length=0 ; key_segs=0;
fulltext_keys=0;
max_key_block_length=0;
share.state.rec_per_key_part=rec_per_key_part;
share.state.key_root=key_root;
share.state.key_del=key_del;
if (uniques)
{
max_key_block_length= myisam_block_size;
max_key_length= MI_UNIQUE_HASH_LENGTH + pointer;
}
for (i=0, keydef=keydefs ; i < keys ; i++ , keydef++)
{
share.state.key_root[i]= HA_OFFSET_ERROR;
min_key_length_skip=length=0;
key_length=pointer;
if (keydef->flag & HA_SPATIAL)
{
#ifdef HAVE_SPATIAL
/* BAR TODO to support 3D and more dimensions in the future */
uint sp_segs=SPDIMS*2;
keydef->flag=HA_SPATIAL;
if (flags & HA_DONT_TOUCH_DATA)
{
/*
called by myisamchk - i.e. table structure was taken from
MYI file and SPATIAL key *does have* additional sp_segs keysegs.
We'd better delete them now
*/
keydef->keysegs-=sp_segs;
}
for (j=0, keyseg=keydef->seg ; (int) j < keydef->keysegs ;
j++, keyseg++)
{
if (keyseg->type != HA_KEYTYPE_BINARY &&
keyseg->type != HA_KEYTYPE_VARBINARY)
{
my_errno=HA_WRONG_CREATE_OPTION;
goto err;
}
}
keydef->keysegs+=sp_segs;
key_length+=SPLEN*sp_segs;
length++; /* At least one length byte */
min_key_length_skip+=SPLEN*2*SPDIMS;
#else
my_errno= HA_ERR_UNSUPPORTED;
goto err;
#endif /*HAVE_SPATIAL*/
}
else if (keydef->flag & HA_FULLTEXT)
{
keydef->flag=HA_FULLTEXT | HA_PACK_KEY | HA_VAR_LENGTH_KEY;
options|=HA_OPTION_PACK_KEYS; /* Using packed keys */
for (j=0, keyseg=keydef->seg ; (int) j < keydef->keysegs ;
j++, keyseg++)
{
if (keyseg->type != HA_KEYTYPE_TEXT &&
keyseg->type != HA_KEYTYPE_VARTEXT)
{
my_errno=HA_WRONG_CREATE_OPTION;
goto err;
}
}
fulltext_keys++;
key_length+= HA_FT_MAXBYTELEN+HA_FT_WLEN;
length++; /* At least one length byte */
min_key_length_skip+=HA_FT_MAXBYTELEN;
}
else
{
/* Test if prefix compression */
if (keydef->flag & HA_PACK_KEY)
{
/* Can't use space_compression on number keys */
if ((keydef->seg[0].flag & HA_SPACE_PACK) &&
keydef->seg[0].type == (int) HA_KEYTYPE_NUM)
keydef->seg[0].flag&= ~HA_SPACE_PACK;
/* Only use HA_PACK_KEY when first segment is a variable length key */
if (!(keydef->seg[0].flag & (HA_SPACE_PACK | HA_BLOB_PART |
HA_VAR_LENGTH_PART)))
{
/* pack relative to previous key */
keydef->flag&= ~HA_PACK_KEY;
keydef->flag|= HA_BINARY_PACK_KEY | HA_VAR_LENGTH_KEY;
}
else
{
keydef->seg[0].flag|=HA_PACK_KEY; /* for easyer intern test */
keydef->flag|=HA_VAR_LENGTH_KEY;
options|=HA_OPTION_PACK_KEYS; /* Using packed keys */
}
}
if (keydef->flag & HA_BINARY_PACK_KEY)
options|=HA_OPTION_PACK_KEYS; /* Using packed keys */
if (keydef->flag & HA_AUTO_KEY && ci->with_auto_increment)
share.base.auto_key=i+1;
for (j=0, keyseg=keydef->seg ; j < keydef->keysegs ; j++, keyseg++)
{
/* numbers are stored with high by first to make compression easier */
switch (keyseg->type) {
case HA_KEYTYPE_SHORT_INT:
case HA_KEYTYPE_LONG_INT:
case HA_KEYTYPE_FLOAT:
case HA_KEYTYPE_DOUBLE:
case HA_KEYTYPE_USHORT_INT:
case HA_KEYTYPE_ULONG_INT:
case HA_KEYTYPE_LONGLONG:
case HA_KEYTYPE_ULONGLONG:
case HA_KEYTYPE_INT24:
case HA_KEYTYPE_UINT24:
case HA_KEYTYPE_INT8:
keyseg->flag|= HA_SWAP_KEY;
break;
case HA_KEYTYPE_VARTEXT:
case HA_KEYTYPE_VARBINARY:
if (!(keyseg->flag & HA_BLOB_PART))
keyseg->flag|= HA_VAR_LENGTH_PART;
break;
default:
break;
}
if (keyseg->flag & HA_SPACE_PACK)
{
DBUG_ASSERT(!(keyseg->flag & HA_VAR_LENGTH_PART));
keydef->flag |= HA_SPACE_PACK_USED | HA_VAR_LENGTH_KEY;
options|=HA_OPTION_PACK_KEYS; /* Using packed keys */
length++; /* At least one length byte */
min_key_length_skip+=keyseg->length;
if (keyseg->length >= 255)
{ /* prefix may be 3 bytes */
min_key_length_skip+=2;
length+=2;
}
}
if (keyseg->flag & (HA_VAR_LENGTH_PART | HA_BLOB_PART))
{
keydef->flag|=HA_VAR_LENGTH_KEY;
length++; /* At least one length byte */
options|=HA_OPTION_PACK_KEYS; /* Using packed keys */
min_key_length_skip+=keyseg->length;
if (keyseg->length >= 255)
{ /* prefix may be 3 bytes */
min_key_length_skip+=2;
length+=2;
}
}
key_length+= keyseg->length;
if (keyseg->null_bit)
{
key_length++;
options|=HA_OPTION_PACK_KEYS;
keyseg->flag|=HA_NULL_PART;
keydef->flag|=HA_VAR_LENGTH_KEY | HA_NULL_PART_KEY;
}
}
} /* if HA_FULLTEXT */
key_segs+=keydef->keysegs;
if (keydef->keysegs > MI_MAX_KEY_SEG)
{
my_errno=HA_WRONG_CREATE_OPTION;
goto err;
}
/*
key_segs may be 0 in the case when we only want to be able to
add on row into the table. This can happen with some DISTINCT queries
in MySQL
*/
if ((keydef->flag & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME &&
key_segs)
share.state.rec_per_key_part[key_segs-1]=1L;
length+=key_length;
keydef->block_length= MI_BLOCK_SIZE(length,pointer,MI_MAX_KEYPTR_SIZE);
if (keydef->block_length > MI_MAX_KEY_BLOCK_LENGTH ||
length >= MI_MAX_KEY_BUFF)
{
my_errno=HA_WRONG_CREATE_OPTION;
goto err;
}
set_if_bigger(max_key_block_length,keydef->block_length);
keydef->keylength= (uint16) key_length;
keydef->minlength= (uint16) (length-min_key_length_skip);
keydef->maxlength= (uint16) length;
if (length > max_key_length)
max_key_length= length;
tot_length+= (max_rows/(ulong) (((uint) keydef->block_length-5)/
(length*2)))*
(ulong) keydef->block_length;
}
for (i=max_key_block_length/MI_MIN_KEY_BLOCK_LENGTH ; i-- ; )
key_del[i]=HA_OFFSET_ERROR;
unique_key_parts=0;
offset=reclength-uniques*MI_UNIQUE_HASH_LENGTH;
for (i=0, uniquedef=uniquedefs ; i < uniques ; i++ , uniquedef++)
{
uniquedef->key=keys+i;
unique_key_parts+=uniquedef->keysegs;
share.state.key_root[keys+i]= HA_OFFSET_ERROR;
tot_length+= (max_rows/(ulong) (((uint) myisam_block_size-5)/
((MI_UNIQUE_HASH_LENGTH + pointer)*2)))*
(ulong) myisam_block_size;
}
keys+=uniques; /* Each unique has 1 key */
key_segs+=uniques; /* Each unique has 1 key seg */
base_pos=(MI_STATE_INFO_SIZE + keys * MI_STATE_KEY_SIZE +
max_key_block_length/MI_MIN_KEY_BLOCK_LENGTH*
MI_STATE_KEYBLOCK_SIZE+
key_segs*MI_STATE_KEYSEG_SIZE);
info_length=base_pos+(uint) (MI_BASE_INFO_SIZE+
keys * MI_KEYDEF_SIZE+
uniques * MI_UNIQUEDEF_SIZE +
(key_segs + unique_key_parts)*HA_KEYSEG_SIZE+
columns*MI_COLUMNDEF_SIZE);
bmove(share.state.header.file_version,(byte*) myisam_file_magic,4);
ci->old_options=options| (ci->old_options & HA_OPTION_TEMP_COMPRESS_RECORD ?
HA_OPTION_COMPRESS_RECORD |
HA_OPTION_TEMP_COMPRESS_RECORD: 0);
mi_int2store(share.state.header.options,ci->old_options);
mi_int2store(share.state.header.header_length,info_length);
mi_int2store(share.state.header.state_info_length,MI_STATE_INFO_SIZE);
mi_int2store(share.state.header.base_info_length,MI_BASE_INFO_SIZE);
mi_int2store(share.state.header.base_pos,base_pos);
share.state.header.language= (ci->language ?
ci->language : default_charset_info->number);
share.state.header.max_block_size=max_key_block_length/MI_MIN_KEY_BLOCK_LENGTH;
share.state.dellink = HA_OFFSET_ERROR;
share.state.process= (ulong) getpid();
share.state.unique= (ulong) 0;
share.state.update_count=(ulong) 0;
share.state.version= (ulong) time((time_t*) 0);
share.state.sortkey= (ushort) ~0;
share.state.auto_increment=ci->auto_increment;
share.options=options;
share.base.rec_reflength=pointer;
/* Get estimate for index file length (this may be wrong for FT keys) */
tmp= (tot_length + max_key_block_length * keys *
MI_INDEX_BLOCK_MARGIN) / MI_MIN_KEY_BLOCK_LENGTH;
/*
use maximum of key_file_length we calculated and key_file_length value we
got from MYI file header (see also myisampack.c:save_state)
*/
share.base.key_reflength=
mi_get_pointer_length(max(ci->key_file_length,tmp),3);
share.base.keys= share.state.header.keys= keys;
share.state.header.uniques= uniques;
share.state.header.fulltext_keys= fulltext_keys;
mi_int2store(share.state.header.key_parts,key_segs);
mi_int2store(share.state.header.unique_key_parts,unique_key_parts);
share.state.key_map = ((ulonglong) 1 << keys)-1;
share.base.keystart = share.state.state.key_file_length=
MY_ALIGN(info_length, myisam_block_size);
share.base.max_key_block_length=max_key_block_length;
share.base.max_key_length=ALIGN_SIZE(max_key_length+4);
share.base.records=ci->max_rows;
share.base.reloc= ci->reloc_rows;
share.base.reclength=real_reclength;
share.base.pack_reclength=reclength+ test(options & HA_OPTION_CHECKSUM);
share.base.max_pack_length=pack_reclength;
share.base.min_pack_length=min_pack_length;
share.base.pack_bits=packed;
share.base.fields=fields;
share.base.pack_fields=packed;
#ifdef USE_RAID
share.base.raid_type=ci->raid_type;
share.base.raid_chunks=ci->raid_chunks;
share.base.raid_chunksize=ci->raid_chunksize;
#endif
/* max_data_file_length and max_key_file_length are recalculated on open */
if (options & HA_OPTION_TMP_TABLE)
share.base.max_data_file_length=(my_off_t) ci->data_file_length;
share.base.min_block_length=
(share.base.pack_reclength+3 < MI_EXTEND_BLOCK_LENGTH &&
! share.base.blobs) ?
max(share.base.pack_reclength,MI_MIN_BLOCK_LENGTH) :
MI_EXTEND_BLOCK_LENGTH;
if (! (flags & HA_DONT_TOUCH_DATA))
share.state.create_time= (long) time((time_t*) 0);
if (ci->index_file_name)
{
fn_format(filename, ci->index_file_name,"",MI_NAME_IEXT,4);
fn_format(linkname,name, "",MI_NAME_IEXT,4);
linkname_ptr=linkname;
/*
Don't create the table if the link or file exists to ensure that one
doesn't accidently destroy another table.
*/
create_flag=0;
}
else
{
fn_format(filename,name,"",MI_NAME_IEXT,(4+ (flags & HA_DONT_TOUCH_DATA) ?
32 : 0));
linkname_ptr=0;
/* Replace the current file */
create_flag=MY_DELETE_OLD;
}
if ((file= my_create_with_symlink(linkname_ptr,
filename,
0, O_RDWR | O_TRUNC,
MYF(MY_WME | create_flag))) < 0)
goto err;
errpos=1;
if (!(flags & HA_DONT_TOUCH_DATA))
{
#ifdef USE_RAID
if (share.base.raid_type)
{
(void) fn_format(filename,name,"",MI_NAME_DEXT,2+4);
if ((dfile=my_raid_create(filename,0,O_RDWR | O_TRUNC,
share.base.raid_type,
share.base.raid_chunks,
share.base.raid_chunksize,
MYF(MY_WME | MY_RAID))) < 0)
goto err;
}
else
#endif
{
if (ci->data_file_name)
{
fn_format(filename, ci->data_file_name,"",MI_NAME_DEXT,4);
fn_format(linkname, name, "",MI_NAME_DEXT,4);
linkname_ptr=linkname;
create_flag=0;
}
else
{
fn_format(filename,name,"",MI_NAME_DEXT,4);
linkname_ptr=0;
create_flag=MY_DELETE_OLD;
}
if ((dfile=
my_create_with_symlink(linkname_ptr, filename,
0,O_RDWR | O_TRUNC,
MYF(MY_WME | create_flag))) < 0)
goto err;
}
errpos=3;
}
if (mi_state_info_write(file, &share.state, 2) ||
mi_base_info_write(file, &share.base))
goto err;
#ifndef DBUG_OFF
if ((uint) my_tell(file,MYF(0)) != base_pos+ MI_BASE_INFO_SIZE)
{
uint pos=(uint) my_tell(file,MYF(0));
DBUG_PRINT("warning",("base_length: %d != used_length: %d",
base_pos+ MI_BASE_INFO_SIZE, pos));
}
#endif
/* Write key and keyseg definitions */
for (i=0 ; i < share.base.keys - uniques; i++)
{
uint sp_segs=(keydefs[i].flag & HA_SPATIAL) ? 2*SPDIMS : 0;
if (mi_keydef_write(file, &keydefs[i]))
goto err;
for (j=0 ; j < keydefs[i].keysegs-sp_segs ; j++)
if (mi_keyseg_write(file, &keydefs[i].seg[j]))
goto err;
#ifdef HAVE_SPATIAL
for (j=0 ; j < sp_segs ; j++)
{
HA_KEYSEG sseg;
sseg.type=SPTYPE;
sseg.language= 7;
sseg.null_bit=0;
sseg.bit_start=0;
sseg.bit_end=0;
sseg.length=SPLEN;
sseg.null_pos=0;
sseg.start=j*SPLEN;
sseg.flag= HA_SWAP_KEY;
if (mi_keyseg_write(file, &sseg))
goto err;
}
#endif
}
/* Create extra keys for unique definitions */
offset=reclength-uniques*MI_UNIQUE_HASH_LENGTH;
bzero((char*) &tmp_keydef,sizeof(tmp_keydef));
bzero((char*) &tmp_keyseg,sizeof(tmp_keyseg));
for (i=0; i < uniques ; i++)
{
tmp_keydef.keysegs=1;
tmp_keydef.flag= HA_UNIQUE_CHECK;
tmp_keydef.block_length= myisam_block_size;
tmp_keydef.keylength= MI_UNIQUE_HASH_LENGTH + pointer;
tmp_keydef.minlength=tmp_keydef.maxlength=tmp_keydef.keylength;
tmp_keyseg.type= MI_UNIQUE_HASH_TYPE;
tmp_keyseg.length= MI_UNIQUE_HASH_LENGTH;
tmp_keyseg.start= offset;
offset+= MI_UNIQUE_HASH_LENGTH;
if (mi_keydef_write(file,&tmp_keydef) ||
mi_keyseg_write(file,(&tmp_keyseg)))
goto err;
}
/* Save unique definition */
for (i=0 ; i < share.state.header.uniques ; i++)
{
if (mi_uniquedef_write(file, &uniquedefs[i]))
goto err;
for (j=0 ; j < uniquedefs[i].keysegs ; j++)
{
if (mi_keyseg_write(file, &uniquedefs[i].seg[j]))
goto err;
}
}
for (i=0 ; i < share.base.fields ; i++)
if (mi_recinfo_write(file, &recinfo[i]))
goto err;
#ifndef DBUG_OFF
if ((uint) my_tell(file,MYF(0)) != info_length)
{
uint pos= (uint) my_tell(file,MYF(0));
DBUG_PRINT("warning",("info_length: %d != used_length: %d",
info_length, pos));
}
#endif
/* Enlarge files */
if (my_chsize(file,(ulong) share.base.keystart,0,MYF(0)))
goto err;
if (! (flags & HA_DONT_TOUCH_DATA))
{
#ifdef USE_RELOC
if (my_chsize(dfile,share.base.min_pack_length*ci->reloc_rows,0,MYF(0)))
goto err;
#endif
errpos=2;
if (my_close(dfile,MYF(0)))
goto err;
}
errpos=0;
pthread_mutex_unlock(&THR_LOCK_myisam);
if (my_close(file,MYF(0)))
goto err;
my_free((char*) rec_per_key_part,MYF(0));
DBUG_RETURN(0);
err:
pthread_mutex_unlock(&THR_LOCK_myisam);
save_errno=my_errno;
switch (errpos) {
case 3:
VOID(my_close(dfile,MYF(0)));
/* fall through */
case 2:
/* QQ: T<>nu should add a call to my_raid_delete() here */
if (! (flags & HA_DONT_TOUCH_DATA))
my_delete_with_symlink(fn_format(filename,name,"",MI_NAME_DEXT,2+4),
MYF(0));
/* fall through */
case 1:
VOID(my_close(file,MYF(0)));
if (! (flags & HA_DONT_TOUCH_DATA))
my_delete_with_symlink(fn_format(filename,name,"",MI_NAME_IEXT,2+4),
MYF(0));
}
my_free((char*) rec_per_key_part, MYF(0));
DBUG_RETURN(my_errno=save_errno); /* return the fatal errno */
}
uint mi_get_pointer_length(ulonglong file_length, uint def)
{
if (file_length) /* If not default */
{
if (file_length >= (longlong) 1 << 56)
def=8;
if (file_length >= (longlong) 1 << 48)
def=7;
if (file_length >= (longlong) 1 << 40)
def=6;
else if (file_length >= (longlong) 1 << 32)
def=5;
else if (file_length >= (1L << 24))
def=4;
else if (file_length >= (1L << 16))
def=3;
else
def=2;
}
return def;
}