mi_create.c 23.9 KB
Newer Older
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3 4 5 6
   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.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8 9 10 11
   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.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
12

bk@work.mysql.com's avatar
bk@work.mysql.com committed
13 14 15 16 17 18
   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 */

19
#include "ftdefs.h"
20 21
#include "sp_defs.h"

bk@work.mysql.com's avatar
bk@work.mysql.com committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#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;
43
  myf create_flag;
44
  uint fields,length,max_key_length,packed,pointer,real_length_diff,
45
       key_length,info_length,key_segs,options,min_key_length_skip,
46
       base_pos,long_varchar_count,varchar_length,
47
       max_key_block_length,unique_key_parts,fulltext_keys,offset;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
48
  ulong reclength, real_reclength,min_pack_length;
49
  char filename[FN_REFLEN],linkname[FN_REFLEN], *linkname_ptr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
50
  ulong pack_reclength;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
51
  ulonglong tot_length,max_rows, tmp;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
52 53 54 55
  enum en_fieldtype type;
  MYISAM_SHARE share;
  MI_KEYDEF *keydef,tmp_keydef;
  MI_UNIQUEDEF *uniquedef;
56
  HA_KEYSEG *keyseg,tmp_keyseg;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
57
  MI_COLUMNDEF *rec;
58
  ulong *rec_per_key_part;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
59 60 61 62 63 64 65 66 67 68
  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;
  }

69
  if (keys + uniques > MI_MAX_KEY || columns == 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  {
    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)
93 94 95 96 97 98
    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);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
99 100 101

	/* Start by checking fields and field-types used */

102
  reclength=varchar_length=long_varchar_count=packed=
bk@work.mysql.com's avatar
bk@work.mysql.com committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    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 */
	}
      }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
124 125
      else if (type == FIELD_SKIP_PRESPACE ||
	       type == FIELD_SKIP_ENDSPACE)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
126 127 128 129 130 131 132
      {
	if (pack_reclength != INT_MAX32)
	  pack_reclength+= rec->length > 255 ? 2 : 1;
	min_pack_length++;
      }
      else if (type == FIELD_VARCHAR)
      {
133
	varchar_length+= rec->length-1;          /* Used for min_pack_length */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
134
	packed--;
135 136 137 138 139
	pack_reclength++;
        min_pack_length++;
        /* We must test for 257 as length includes pack-length */
 	if (test(rec->length >= 257))
	{
bk@work.mysql.com's avatar
bk@work.mysql.com committed
140
	  long_varchar_count++;
141
	  pack_reclength+= 2;			/* May be packed on 3 bytes */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
142 143
	}
      }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
144
      else if (type != FIELD_SKIP_ZERO)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
145 146 147 148 149 150 151 152 153 154 155 156 157
      {
	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--;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
158
      if (rec->type == (int) FIELD_SKIP_ZERO && rec->length == 1)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
159 160 161 162 163 164 165 166 167 168 169
      {
	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 */
170 171 172
  /* We can't use checksum with static length rows */
  if (!(options & HA_OPTION_PACK_RECORD))
    options&= ~HA_OPTION_CHECKSUM;
173 174
  if (!(options & (HA_OPTION_PACK_RECORD | HA_OPTION_COMPRESS_RECORD)))
    min_pack_length+= varchar_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
  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))
serg@serg.mylan's avatar
serg@serg.mylan committed
206
    pointer=mi_get_pointer_length(ci->data_file_length,myisam_data_pointer_size);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
207
  else
serg@serg.mylan's avatar
serg@serg.mylan committed
208
    pointer=mi_get_pointer_length(ci->max_rows,myisam_data_pointer_size);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
209 210 211 212 213 214 215 216 217 218 219
  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
220
    reclength+= long_varchar_count;	/* We need space for varchar! */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
221 222

  max_key_length=0; tot_length=0 ; key_segs=0;
223
  fulltext_keys=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
224 225 226 227 228 229
  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)
  {
230
    max_key_block_length= myisam_block_size;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
231
    max_key_length=	  MI_UNIQUE_HASH_LENGTH + pointer;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
232 233 234 235 236
  }

  for (i=0, keydef=keydefs ; i < keys ; i++ , keydef++)
  {

237
    share.state.key_root[i]= HA_OFFSET_ERROR;
238
    min_key_length_skip=length=real_length_diff=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
239
    key_length=pointer;
240 241
    if (keydef->flag & HA_SPATIAL)
    {
242
#ifdef HAVE_SPATIAL
243
      /* BAR TODO to support 3D and more dimensions in the future */
244
      uint sp_segs=SPDIMS*2;
245
      keydef->flag=HA_SPATIAL;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
246

247 248
      if (flags & HA_DONT_TOUCH_DATA)
      {
249
        /*
250
           called by myisamchk - i.e. table structure was taken from
251
           MYI file and SPATIAL key *does have* additional sp_segs keysegs.
252 253 254 255 256 257 258 259 260
           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 &&
261 262
	    keyseg->type != HA_KEYTYPE_VARBINARY1 &&
            keyseg->type != HA_KEYTYPE_VARBINARY2)
263 264 265 266 267 268 269 270
        {
          my_errno=HA_WRONG_CREATE_OPTION;
          goto err;
        }
      }
      keydef->keysegs+=sp_segs;
      key_length+=SPLEN*sp_segs;
      length++;                              /* At least one length byte */
271
      min_key_length_skip+=SPLEN*2*SPDIMS;
272 273 274 275
#else
      my_errno= HA_ERR_UNSUPPORTED;
      goto err;
#endif /*HAVE_SPATIAL*/
276
    }
277
    else if (keydef->flag & HA_FULLTEXT)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
278 279 280 281 282 283 284 285
    {
      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 &&
286 287
	    keyseg->type != HA_KEYTYPE_VARTEXT1 &&
            keyseg->type != HA_KEYTYPE_VARTEXT2)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
288 289 290 291
        {
          my_errno=HA_WRONG_CREATE_OPTION;
          goto err;
        }
292 293 294 295 296 297 298 299 300 301
        if (!(keyseg->flag & HA_BLOB_PART) &&
	    (keyseg->type == HA_KEYTYPE_VARTEXT1 ||
             keyseg->type == HA_KEYTYPE_VARTEXT2))
        {
          /* Make a flag that this is a VARCHAR */
          keyseg->flag|= HA_VAR_LENGTH_PART;
          /* Store in bit_start number of bytes used to pack the length */
          keyseg->bit_start= ((keyseg->type == HA_KEYTYPE_VARTEXT1)?
                              1 : 2);
        }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
302 303
      }

304
      fulltext_keys++;
305
      key_length+= HA_FT_MAXBYTELEN+HA_FT_WLEN;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
306
      length++;                              /* At least one length byte */
307
      min_key_length_skip+=HA_FT_MAXBYTELEN;
308
      real_length_diff=HA_FT_MAXBYTELEN-FT_MAX_WORD_LEN_FOR_SORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
309 310 311
    }
    else
    {
312 313
      /* Test if prefix compression */
      if (keydef->flag & HA_PACK_KEY)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
314
      {
315 316 317 318 319
	/* 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;

320
	/* Only use HA_PACK_KEY when first segment is a variable length key */
321
	if (!(keydef->seg[0].flag & (HA_SPACE_PACK | HA_BLOB_PART |
322
				     HA_VAR_LENGTH_PART)))
323 324 325 326 327 328 329 330 331 332 333
	{
	  /* 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 */
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
334
      }
335
      if (keydef->flag & HA_BINARY_PACK_KEY)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
336 337
	options|=HA_OPTION_PACK_KEYS;		/* Using packed keys */

338
      if (keydef->flag & HA_AUTO_KEY && ci->with_auto_increment)
339 340
	share.base.auto_key=i+1;
      for (j=0, keyseg=keydef->seg ; j < keydef->keysegs ; j++, keyseg++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
341
      {
342 343 344 345 346 347 348 349 350 351 352 353 354 355
	/* 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;
356
          break;
357 358 359 360
        case HA_KEYTYPE_VARTEXT1:
        case HA_KEYTYPE_VARTEXT2:
        case HA_KEYTYPE_VARBINARY1:
        case HA_KEYTYPE_VARBINARY2:
361
          if (!(keyseg->flag & HA_BLOB_PART))
362 363
          {
            /* Make a flag that this is a VARCHAR */
364
            keyseg->flag|= HA_VAR_LENGTH_PART;
365 366 367 368 369
            /* Store in bit_start number of bytes used to pack the length */
            keyseg->bit_start= ((keyseg->type == HA_KEYTYPE_VARTEXT1 ||
                                 keyseg->type == HA_KEYTYPE_VARBINARY1) ?
                                1 : 2);
          }
370
          break;
371 372
	default:
	  break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
373
	}
374 375
	if (keyseg->flag & HA_SPACE_PACK)
	{
376
          DBUG_ASSERT(!(keyseg->flag & HA_VAR_LENGTH_PART));
377 378 379
	  keydef->flag |= HA_SPACE_PACK_USED | HA_VAR_LENGTH_KEY;
	  options|=HA_OPTION_PACK_KEYS;		/* Using packed keys */
	  length++;				/* At least one length byte */
380
	  min_key_length_skip+=keyseg->length;
381 382
	  if (keyseg->length >= 255)
	  {					/* prefix may be 3 bytes */
383
	    min_key_length_skip+=2;
384 385 386
	    length+=2;
	  }
	}
387
	if (keyseg->flag & (HA_VAR_LENGTH_PART | HA_BLOB_PART))
388
	{
389 390
          DBUG_ASSERT(!test_all_bits(keyseg->flag,
                                    (HA_VAR_LENGTH_PART | HA_BLOB_PART)));
391 392 393
	  keydef->flag|=HA_VAR_LENGTH_KEY;
	  length++;				/* At least one length byte */
	  options|=HA_OPTION_PACK_KEYS;		/* Using packed keys */
394
	  min_key_length_skip+=keyseg->length;
395 396
	  if (keyseg->length >= 255)
	  {					/* prefix may be 3 bytes */
397
	    min_key_length_skip+=2;
398 399 400 401 402 403 404 405 406 407
	    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;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
408 409 410 411 412 413 414 415 416
	}
      }
    } /* if HA_FULLTEXT */
    key_segs+=keydef->keysegs;
    if (keydef->keysegs > MI_MAX_KEY_SEG)
    {
      my_errno=HA_WRONG_CREATE_OPTION;
      goto err;
    }
417 418 419 420 421 422 423
    /*
      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)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
424 425
      share.state.rec_per_key_part[key_segs-1]=1L;
    length+=key_length;
426 427
    keydef->block_length= MI_BLOCK_SIZE(length-real_length_diff,
                                        pointer,MI_MAX_KEYPTR_SIZE);
428
    if (keydef->block_length > MI_MAX_KEY_BLOCK_LENGTH ||
429
        length >= MI_MAX_KEY_BUFF)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
430 431 432 433 434 435
    {
      my_errno=HA_WRONG_CREATE_OPTION;
      goto err;
    }
    set_if_bigger(max_key_block_length,keydef->block_length);
    keydef->keylength= (uint16) key_length;
436
    keydef->minlength= (uint16) (length-min_key_length_skip);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
437 438 439 440 441 442 443 444
    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;
  }
445
  for (i=max_key_block_length/MI_MIN_KEY_BLOCK_LENGTH ; i-- ; )
bk@work.mysql.com's avatar
bk@work.mysql.com committed
446 447 448 449 450 451 452 453 454
    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;
455 456 457
    tot_length+= (max_rows/(ulong) (((uint) myisam_block_size-5)/
                         ((MI_UNIQUE_HASH_LENGTH + pointer)*2)))*
                         (ulong) myisam_block_size;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
458 459 460 461 462
  }
  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 +
463 464
	    max_key_block_length/MI_MIN_KEY_BLOCK_LENGTH*
	    MI_STATE_KEYBLOCK_SIZE+
bk@work.mysql.com's avatar
bk@work.mysql.com committed
465 466 467 468
	    key_segs*MI_STATE_KEYSEG_SIZE);
  info_length=base_pos+(uint) (MI_BASE_INFO_SIZE+
			       keys * MI_KEYDEF_SIZE+
			       uniques * MI_UNIQUEDEF_SIZE +
469
			       (key_segs + unique_key_parts)*HA_KEYSEG_SIZE+
bk@work.mysql.com's avatar
bk@work.mysql.com committed
470 471 472 473 474 475 476 477 478 479 480 481
			       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 ?
482
				ci->language : default_charset_info->number);
483
  share.state.header.max_block_size=max_key_block_length/MI_MIN_KEY_BLOCK_LENGTH;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
484 485 486 487

  share.state.dellink = HA_OFFSET_ERROR;
  share.state.process=	(ulong) getpid();
  share.state.unique=	(ulong) 0;
488
  share.state.update_count=(ulong) 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
489 490 491 492 493
  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;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
494 495 496 497 498 499 500
  /* 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)
  */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
501
  share.base.key_reflength=
sergefp@mysql.com's avatar
sergefp@mysql.com committed
502
    mi_get_pointer_length(max(ci->key_file_length,tmp),3);
503
  share.base.keys= share.state.header.keys= keys;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
504
  share.state.header.uniques= uniques;
505
  share.state.header.fulltext_keys= fulltext_keys;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
506 507 508 509 510 511 512 513 514 515 516
  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;
517
  share.base.pack_reclength=reclength+ test(options & HA_OPTION_CHECKSUM);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
  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);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
540

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
  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;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
560

561 562
  if ((file= my_create_with_symlink(linkname_ptr,
				    filename,
563
				    0, O_RDWR | O_TRUNC,
564
				    MYF(MY_WME | create_flag))) < 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
565 566
    goto err;
  errpos=1;
567

bk@work.mysql.com's avatar
bk@work.mysql.com committed
568 569 570 571 572
  if (!(flags & HA_DONT_TOUCH_DATA))
  {
#ifdef USE_RAID
    if (share.base.raid_type)
    {
573 574
      (void) fn_format(filename,name,"",MI_NAME_DEXT,2+4);
      if ((dfile=my_raid_create(filename,0,O_RDWR | O_TRUNC,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
575 576 577 578 579 580 581 582
				share.base.raid_type,
				share.base.raid_chunks,
				share.base.raid_chunksize,
				MYF(MY_WME | MY_RAID))) < 0)
	goto err;
    }
    else
#endif
583
    {
584 585 586 587 588 589 590 591 592 593 594 595 596
      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;
      }
597
      if ((dfile=
598 599 600
	   my_create_with_symlink(linkname_ptr, filename,
				  0,O_RDWR | O_TRUNC,
				  MYF(MY_WME | create_flag))) < 0)
601 602
	goto err;
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
    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++)
  {
621
    uint sp_segs=(keydefs[i].flag & HA_SPATIAL) ? 2*SPDIMS : 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
622 623 624

    if (mi_keydef_write(file, &keydefs[i]))
      goto err;
625
    for (j=0 ; j < keydefs[i].keysegs-sp_segs ; j++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
626
      if (mi_keyseg_write(file, &keydefs[i].seg[j]))
627
       goto err;
628
#ifdef HAVE_SPATIAL
629 630
    for (j=0 ; j < sp_segs ; j++)
    {
631
      HA_KEYSEG sseg;
632
      sseg.type=SPTYPE;
633
      sseg.language= 7;                         /* Binary */
634 635 636
      sseg.null_bit=0;
      sseg.bit_start=0;
      sseg.bit_end=0;
637 638
      sseg.bit_length= 0;
      sseg.bit_pos= 0;
639 640 641 642 643 644 645
      sseg.length=SPLEN;
      sseg.null_pos=0;
      sseg.start=j*SPLEN;
      sseg.flag= HA_SWAP_KEY;
      if (mi_keyseg_write(file, &sseg))
        goto err;
    }
646
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
647 648 649 650 651 652 653 654 655
  }
  /* 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;
656
    tmp_keydef.block_length=	myisam_block_size;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
657 658
    tmp_keydef.keylength=	MI_UNIQUE_HASH_LENGTH + pointer;
    tmp_keydef.minlength=tmp_keydef.maxlength=tmp_keydef.keylength;
659 660
    tmp_keyseg.type=		MI_UNIQUE_HASH_TYPE;
    tmp_keyseg.length=		MI_UNIQUE_HASH_LENGTH;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
661 662 663 664 665 666 667 668 669 670
    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++)
  {
671 672
    HA_KEYSEG *keyseg_end;
    keyseg= uniquedefs[i].seg;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
673 674
    if (mi_uniquedef_write(file, &uniquedefs[i]))
      goto err;
675 676 677
    for (keyseg= uniquedefs[i].seg, keyseg_end= keyseg+ uniquedefs[i].keysegs;
         keyseg < keyseg_end;
         keyseg++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
678
    {
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
      switch (keyseg->type) {
      case HA_KEYTYPE_VARTEXT1:
      case HA_KEYTYPE_VARTEXT2:
      case HA_KEYTYPE_VARBINARY1:
      case HA_KEYTYPE_VARBINARY2:
        if (!(keyseg->flag & HA_BLOB_PART))
        {
          keyseg->flag|= HA_VAR_LENGTH_PART;
          keyseg->bit_start= ((keyseg->type == HA_KEYTYPE_VARTEXT1 ||
                               keyseg->type == HA_KEYTYPE_VARBINARY1) ?
                              1 : 2);
        }
        break;
      default:
        break;
      }
      if (mi_keyseg_write(file, keyseg))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
	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 */
713
  if (my_chsize(file,(ulong) share.base.keystart,0,MYF(0)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
714 715 716 717 718
    goto err;

  if (! (flags & HA_DONT_TOUCH_DATA))
  {
#ifdef USE_RELOC
719
    if (my_chsize(dfile,share.base.min_pack_length*ci->reloc_rows,0,MYF(0)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
720 721 722 723 724 725 726 727 728 729
      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;
730
  my_free((char*) rec_per_key_part,MYF(0));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
731 732 733 734 735 736 737 738 739 740 741
  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: Tnu should add a call to my_raid_delete() here */
742 743 744
  if (! (flags & HA_DONT_TOUCH_DATA))
    my_delete_with_symlink(fn_format(filename,name,"",MI_NAME_DEXT,2+4),
			   MYF(0));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
745 746 747 748
    /* fall through */
  case 1:
    VOID(my_close(file,MYF(0)));
    if (! (flags & HA_DONT_TOUCH_DATA))
749 750
      my_delete_with_symlink(fn_format(filename,name,"",MI_NAME_IEXT,2+4),
			     MYF(0));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
751
  }
752
  my_free((char*) rec_per_key_part, MYF(0));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
  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;
}