ha_myisammrg.cc 11.3 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
2

unknown's avatar
unknown 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.
7

unknown's avatar
unknown 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.
12

unknown's avatar
unknown committed
13 14 15 16 17 18 19 20 21 22 23 24 25
   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 */


#ifdef __GNUC__
#pragma implementation				// gcc: Class implementation
#endif

#include "mysql_priv.h"
#include <m_ctype.h>
#include "ha_myisammrg.h"
#ifndef MASTER
26
#include "../srclib/myisammrg/myrg_def.h"
unknown's avatar
unknown committed
27
#else
28
#include "../myisammrg/myrg_def.h"
unknown's avatar
unknown committed
29 30 31 32 33 34 35 36 37
#endif

/*****************************************************************************
** MyISAM MERGE tables
*****************************************************************************/

const char **ha_myisammrg::bas_ext() const
{ static const char *ext[]= { ".MRG", NullS }; return ext; }

38
int ha_myisammrg::open(const char *name, int mode, uint test_if_locked)
unknown's avatar
unknown committed
39 40
{
  char name_buff[FN_REFLEN];
unknown's avatar
unknown committed
41
  DBUG_PRINT("info", ("ha_myisammrg::open"));
unknown's avatar
unknown committed
42 43
  if (!(file=myrg_open(fn_format(name_buff,name,"","",2 | 4), mode,
		       test_if_locked)))
unknown's avatar
unknown committed
44 45
  {
    DBUG_PRINT("info", ("ha_myisammrg::open exit %d", my_errno));
unknown's avatar
unknown committed
46
    return (my_errno ? my_errno : -1);
unknown's avatar
unknown committed
47 48
  }
  DBUG_PRINT("info", ("ha_myisammrg::open myrg_extrafunc..."))
unknown's avatar
unknown committed
49
  myrg_extrafunc(file, query_cache_invalidate_by_MyISAM_filename_ref);
unknown's avatar
unknown committed
50 51
  if (!(test_if_locked == HA_OPEN_WAIT_IF_LOCKED ||
	test_if_locked == HA_OPEN_ABORT_IF_LOCKED))
unknown's avatar
unknown committed
52
    myrg_extra(file,HA_EXTRA_NO_WAIT_LOCK,0);
unknown's avatar
unknown committed
53 54
  info(HA_STATUS_NO_LOCK | HA_STATUS_VARIABLE | HA_STATUS_CONST);
  if (!(test_if_locked & HA_OPEN_WAIT_IF_LOCKED))
unknown's avatar
unknown committed
55
    myrg_extra(file,HA_EXTRA_WAIT_LOCK,0);
56

unknown's avatar
unknown committed
57
  if (table->reclength != mean_rec_length && mean_rec_length)
unknown's avatar
unknown committed
58 59 60
  {
    DBUG_PRINT("error",("reclength: %d  mean_rec_length: %d",
			table->reclength, mean_rec_length));
61
    goto err;
unknown's avatar
unknown committed
62
  }
63 64 65 66 67
#if !defined(BIG_TABLES) || SIZEOF_OFF_T == 4
  /* Merge table has more than 2G rows */
  if (table->crashed)
    goto err;
#endif
unknown's avatar
unknown committed
68
  return (0);
69 70 71
err:
  myrg_close(file);
  file=0;
unknown's avatar
unknown committed
72
  return (my_errno= HA_ERR_WRONG_TABLE_DEF);
unknown's avatar
unknown committed
73 74 75 76
}

int ha_myisammrg::close(void)
{
unknown's avatar
unknown committed
77
  return myrg_close(file);
unknown's avatar
unknown committed
78 79 80 81
}

int ha_myisammrg::write_row(byte * buf)
{
82 83 84 85
  statistic_increment(ha_write_count,&LOCK_status);
  if (table->time_stamp)
    update_timestamp(buf+table->time_stamp-1);
  if (table->next_number_field && buf == table->record[0])
unknown's avatar
unknown committed
86
      update_auto_increment();
unknown's avatar
unknown committed
87
  return myrg_write(file,buf);
unknown's avatar
unknown committed
88 89 90 91 92 93 94
}

int ha_myisammrg::update_row(const byte * old_data, byte * new_data)
{
  statistic_increment(ha_update_count,&LOCK_status);
  if (table->time_stamp)
    update_timestamp(new_data+table->time_stamp-1);
unknown's avatar
unknown committed
95
  return myrg_update(file,old_data,new_data);
unknown's avatar
unknown committed
96 97 98 99 100
}

int ha_myisammrg::delete_row(const byte * buf)
{
  statistic_increment(ha_delete_count,&LOCK_status);
unknown's avatar
unknown committed
101
  return myrg_delete(file,buf);
unknown's avatar
unknown committed
102 103 104 105 106
}

int ha_myisammrg::index_read(byte * buf, const byte * key,
			  uint key_len, enum ha_rkey_function find_flag)
{
107 108 109
  statistic_increment(ha_read_key_count,&LOCK_status);
  int error=myrg_rkey(file,buf,active_index, key, key_len, find_flag);
  table->status=error ? STATUS_NOT_FOUND: 0;
unknown's avatar
unknown committed
110
  return error;
unknown's avatar
unknown committed
111 112 113 114 115
}

int ha_myisammrg::index_read_idx(byte * buf, uint index, const byte * key,
				 uint key_len, enum ha_rkey_function find_flag)
{
116 117 118
  statistic_increment(ha_read_key_count,&LOCK_status);
  int error=myrg_rkey(file,buf,index, key, key_len, find_flag);
  table->status=error ? STATUS_NOT_FOUND: 0;
unknown's avatar
unknown committed
119
  return error;
unknown's avatar
unknown committed
120 121
}

122 123 124 125 126 127
int ha_myisammrg::index_read_last(byte * buf, const byte * key, uint key_len)
{
  statistic_increment(ha_read_key_count,&LOCK_status);
  int error=myrg_rkey(file,buf,active_index, key, key_len,
		      HA_READ_PREFIX_LAST);
  table->status=error ? STATUS_NOT_FOUND: 0;
unknown's avatar
unknown committed
128
  return error;
129 130
}

unknown's avatar
unknown committed
131 132
int ha_myisammrg::index_next(byte * buf)
{
133 134 135
  statistic_increment(ha_read_next_count,&LOCK_status);
  int error=myrg_rnext(file,buf,active_index);
  table->status=error ? STATUS_NOT_FOUND: 0;
unknown's avatar
unknown committed
136
  return error;
unknown's avatar
unknown committed
137 138 139 140
}

int ha_myisammrg::index_prev(byte * buf)
{
141 142 143
  statistic_increment(ha_read_prev_count,&LOCK_status);
  int error=myrg_rprev(file,buf, active_index);
  table->status=error ? STATUS_NOT_FOUND: 0;
unknown's avatar
unknown committed
144
  return error;
unknown's avatar
unknown committed
145
}
146

unknown's avatar
unknown committed
147 148
int ha_myisammrg::index_first(byte * buf)
{
149 150 151
  statistic_increment(ha_read_first_count,&LOCK_status);
  int error=myrg_rfirst(file, buf, active_index);
  table->status=error ? STATUS_NOT_FOUND: 0;
unknown's avatar
unknown committed
152
  return error;
unknown's avatar
unknown committed
153 154 155 156
}

int ha_myisammrg::index_last(byte * buf)
{
157 158 159
  statistic_increment(ha_read_last_count,&LOCK_status);
  int error=myrg_rlast(file, buf, active_index);
  table->status=error ? STATUS_NOT_FOUND: 0;
unknown's avatar
unknown committed
160
  return error;
unknown's avatar
unknown committed
161 162 163 164
}

int ha_myisammrg::rnd_init(bool scan)
{
unknown's avatar
unknown committed
165
  return myrg_extra(file,HA_EXTRA_RESET,0);
unknown's avatar
unknown committed
166 167 168 169 170 171 172
}

int ha_myisammrg::rnd_next(byte *buf)
{
  statistic_increment(ha_read_rnd_next_count,&LOCK_status);
  int error=myrg_rrnd(file, buf, HA_OFFSET_ERROR);
  table->status=error ? STATUS_NOT_FOUND: 0;
unknown's avatar
unknown committed
173
  return error;
unknown's avatar
unknown committed
174 175 176 177 178 179 180
}

int ha_myisammrg::rnd_pos(byte * buf, byte *pos)
{
  statistic_increment(ha_read_rnd_count,&LOCK_status);
  int error=myrg_rrnd(file, buf, ha_get_ptr(pos,ref_length));
  table->status=error ? STATUS_NOT_FOUND: 0;
unknown's avatar
unknown committed
181
  return error;
unknown's avatar
unknown committed
182 183 184 185 186 187 188 189
}

void ha_myisammrg::position(const byte *record)
{
  ulonglong position= myrg_position(file);
  ha_store_ptr(ref, ref_length, (my_off_t) position);
}

unknown's avatar
unknown committed
190 191 192 193 194 195 196 197 198 199 200 201 202
ha_rows ha_myisammrg::records_in_range(int inx,
				    const byte *start_key,uint start_key_len,
				    enum ha_rkey_function start_search_flag,
				    const byte *end_key,uint end_key_len,
				    enum ha_rkey_function end_search_flag)
{
  return (ha_rows) myrg_records_in_range(file,
				       inx,
				       start_key,start_key_len,
				       start_search_flag,
				       end_key,end_key_len,
				       end_search_flag);
}
unknown's avatar
unknown committed
203 204 205 206 207

void ha_myisammrg::info(uint flag)
{
  MYMERGE_INFO info;
  (void) myrg_status(file,&info,flag);
208 209 210 211 212 213 214 215 216 217 218
  /*
    The following fails if one has not compiled MySQL with -DBIG_TABLES
    and one has more than 2^32 rows in the merge tables.
  */
  records = (ha_rows) info.records;
  deleted = (ha_rows) info.deleted;
#if !defined(BIG_TABLES) || SIZEOF_OFF_T == 4
  if ((info.records >= (ulonglong) 1 << 32) ||
      (info.deleted >= (ulonglong) 1 << 32))
    table->crashed=1;
#endif
unknown's avatar
unknown committed
219 220
  data_file_length=info.data_file_length;
  errkey  = info.errkey;
unknown's avatar
unknown committed
221
  table->keys_in_use= set_bits(key_map, table->keys);
unknown's avatar
unknown committed
222
  table->db_options_in_use    = info.options;
unknown's avatar
unknown committed
223
  table->is_view=1;
unknown's avatar
unknown committed
224 225 226
  mean_rec_length=info.reclength;
  block_size=0;
  update_time=0;
227
#if SIZEOF_OFF_T > 4
unknown's avatar
unknown committed
228
  ref_length=6;					// Should be big enough
229 230 231
#else
  ref_length=4;					// Can't be > than my_off_t
#endif
unknown's avatar
unknown committed
232 233 234 235 236 237 238
  if (flag & HA_STATUS_CONST)
  {
    if (table->key_parts)
      memcpy((char*) table->key_info[0].rec_per_key,
	     (char*) info.rec_per_key,
	     sizeof(table->key_info[0].rec_per_key)*table->key_parts);
  }
unknown's avatar
unknown committed
239 240 241 242 243
}


int ha_myisammrg::extra(enum ha_extra_function operation)
{
244 245 246 247
  /* As this is just a mapping, we don't have to force the underlying
     tables to be closed */
  if (operation == HA_EXTRA_FORCE_REOPEN ||
      operation == HA_EXTRA_PREPARE_FOR_DELETE)
unknown's avatar
unknown committed
248 249
    return 0;
  return myrg_extra(file,operation,0);
unknown's avatar
unknown committed
250 251
}

unknown's avatar
unknown committed
252 253 254 255 256

/* To be used with WRITE_CACHE, EXTRA_CACHE and BULK_INSERT_BEGIN */

int ha_myisammrg::extra_opt(enum ha_extra_function operation, ulong cache_size)
{
unknown's avatar
unknown committed
257
  if ((specialflag & SPECIAL_SAFE_MODE) && operation == HA_EXTRA_WRITE_CACHE)
unknown's avatar
unknown committed
258 259
    return 0;
  return myrg_extra(file, operation, (void*) &cache_size);
unknown's avatar
unknown committed
260 261 262
}


unknown's avatar
unknown committed
263 264
int ha_myisammrg::reset(void)
{
unknown's avatar
unknown committed
265
  return myrg_extra(file,HA_EXTRA_RESET,0);
unknown's avatar
unknown committed
266 267 268 269
}

int ha_myisammrg::external_lock(THD *thd, int lock_type)
{
unknown's avatar
unknown committed
270
  return myrg_lock_database(file,lock_type);
271
}
unknown's avatar
unknown committed
272 273 274

uint ha_myisammrg::lock_count(void) const
{
unknown's avatar
unknown committed
275
  return file->tables;
unknown's avatar
unknown committed
276 277 278 279 280 281 282
}


THR_LOCK_DATA **ha_myisammrg::store_lock(THD *thd,
					 THR_LOCK_DATA **to,
					 enum thr_lock_type lock_type)
{
283
  MYRG_TABLE *open_table;
unknown's avatar
unknown committed
284

285 286 287
  for (open_table=file->open_tables ;
       open_table != file->end_table ;
       open_table++)
unknown's avatar
unknown committed
288
  {
289 290 291
    *(to++)= &open_table->table->lock;
    if (lock_type != TL_IGNORE && open_table->table->lock.type == TL_UNLOCK)
      open_table->table->lock.type=lock_type;
unknown's avatar
unknown committed
292
  }
unknown's avatar
unknown committed
293
  return to;
unknown's avatar
unknown committed
294 295
}

296 297
void ha_myisammrg::update_create_info(HA_CREATE_INFO *create_info)
{
298
  // [phi] auto_increment stuff is missing (but currently not needed)
unknown's avatar
unknown committed
299
  DBUG_ENTER("ha_myisammrg::update_create_info");
300 301
  if (!(create_info->used_fields & HA_CREATE_USED_UNION))
  {
302
    MYRG_TABLE *open_table;
303 304
    THD *thd=current_thd;
    create_info->merge_list.next= &create_info->merge_list.first;
305
    create_info->merge_list.elements=0;
306

307 308 309
    for (open_table=file->open_tables ;
	 open_table != file->end_table ;
	 open_table++)
310
    {
311
      char *name=open_table->table->filename;
312 313 314 315 316 317 318
      char buff[FN_REFLEN];
      TABLE_LIST *ptr;
      if (!(ptr = (TABLE_LIST *) thd->calloc(sizeof(TABLE_LIST))))
	goto err;
      fn_format(buff,name,"","",3);
      if (!(ptr->real_name=thd->strdup(buff)))
	goto err;
319
      create_info->merge_list.elements++;
320 321 322 323 324
      (*create_info->merge_list.next) = (byte*) ptr;
      create_info->merge_list.next= (byte**) &ptr->next;
    }
    *create_info->merge_list.next=0;
  }
325 326 327 328
  if (!(create_info->used_fields & HA_CREATE_USED_INSERT_METHOD))
  {
    create_info->merge_insert_method = file->merge_insert_method;
  }
329 330 331 332 333 334 335
  DBUG_VOID_RETURN;

err:
  create_info->merge_list.elements=0;
  create_info->merge_list.first=0;
  DBUG_VOID_RETURN;
}
unknown's avatar
unknown committed
336 337 338 339

int ha_myisammrg::create(const char *name, register TABLE *form,
			 HA_CREATE_INFO *create_info)
{
340 341 342 343 344 345 346 347
  char buff[FN_REFLEN],**table_names,**pos;
  TABLE_LIST *tables= (TABLE_LIST*) create_info->merge_list.first;
  DBUG_ENTER("ha_myisammrg::create");

  if (!(table_names= (char**) sql_alloc((create_info->merge_list.elements+1)*
					sizeof(char*))))
    DBUG_RETURN(1);
  for (pos=table_names ; tables ; tables=tables->next)
unknown's avatar
unknown committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
  {
    char *table_name;
    if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
    {
      TABLE **tbl=find_temporary_table(current_thd,
          tables->db, tables->real_name);
      if (!tbl)
      {
        table_name=sql_alloc(1+
            my_snprintf(buff,FN_REFLEN,"%s/%s/%s",mysql_real_data_home,
                        tables->db, tables->real_name));
        if (!table_name)
          DBUG_RETURN(1);
        strcpy(table_name, buff);
      }
      else
        table_name=(*tbl)->path;
    }
    else
      table_name=tables->real_name;
    *pos++= table_name;
  }
370 371
  *pos=0;
  DBUG_RETURN(myrg_create(fn_format(buff,name,"","",2+4+16),
372 373 374
			  (const char **) table_names,
                          create_info->merge_insert_method,
                          (my_bool) 0));
unknown's avatar
unknown committed
375
}
unknown's avatar
unknown committed
376 377 378 379

void ha_myisammrg::append_create_info(String *packet)
{
  char buff[FN_REFLEN];
380 381 382
  if (file->merge_insert_method != MERGE_INSERT_DISABLED)
  {
    packet->append(" INSERT_METHOD=",15);
383
    packet->append(get_type(&merge_insert_method,file->merge_insert_method-1));
384
  }
unknown's avatar
unknown committed
385
  packet->append(" UNION=(",8);
386
  MYRG_TABLE *open_table,*first;
unknown's avatar
unknown committed
387

388 389 390
  for (first=open_table=file->open_tables ;
       open_table != file->end_table ;
       open_table++)
unknown's avatar
unknown committed
391
  {
392
    char *name= open_table->table->filename;
unknown's avatar
unknown committed
393
    fn_format(buff,name,"","",3);
394
    if (open_table != first)
unknown's avatar
unknown committed
395 396 397 398 399
      packet->append(',');
    packet->append(buff,(uint) strlen(buff));
  }
  packet->append(')');
}