ha_myisammrg.cc 9.39 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 52 53 54 55
  if (!(test_if_locked == HA_OPEN_WAIT_IF_LOCKED ||
	test_if_locked == HA_OPEN_ABORT_IF_LOCKED))
    myrg_extra(file,HA_EXTRA_NO_WAIT_LOCK);
  info(HA_STATUS_NO_LOCK | HA_STATUS_VARIABLE | HA_STATUS_CONST);
  if (!(test_if_locked & HA_OPEN_WAIT_IF_LOCKED))
    myrg_extra(file,HA_EXTRA_WAIT_LOCK);
unknown's avatar
unknown committed
56
  if (table->reclength != mean_rec_length && mean_rec_length)
unknown's avatar
unknown committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  {
    DBUG_PRINT("error",("reclength: %d  mean_rec_length: %d",
			table->reclength, mean_rec_length));
    myrg_close(file);
    file=0;
    return my_errno=HA_ERR_WRONG_TABLE_DEF;
  }
  return (0);
}

int ha_myisammrg::close(void)
{
  return myrg_close(file);
}

int ha_myisammrg::write_row(byte * buf)
{
74 75 76 77 78 79 80
  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])
      return (my_errno=HA_ERR_WRONG_COMMAND);
  //  update_auto_increment(); - [phi] have to check this before allowing it
  return myrg_write(file,buf);
unknown's avatar
unknown committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
}

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);
  return myrg_update(file,old_data,new_data);
}

int ha_myisammrg::delete_row(const byte * buf)
{
  statistic_increment(ha_delete_count,&LOCK_status);
  return myrg_delete(file,buf);
}

int ha_myisammrg::index_read(byte * buf, const byte * key,
			  uint key_len, enum ha_rkey_function find_flag)
{
100 101 102 103
  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;
  return error;
unknown's avatar
unknown committed
104 105 106 107 108
}

int ha_myisammrg::index_read_idx(byte * buf, uint index, const byte * key,
				 uint key_len, enum ha_rkey_function find_flag)
{
109 110 111 112
  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;
  return error;
unknown's avatar
unknown committed
113 114
}

115 116 117 118 119 120 121 122 123
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;
  return error;
}

unknown's avatar
unknown committed
124 125
int ha_myisammrg::index_next(byte * buf)
{
126 127 128 129
  statistic_increment(ha_read_next_count,&LOCK_status);
  int error=myrg_rnext(file,buf,active_index);
  table->status=error ? STATUS_NOT_FOUND: 0;
  return error;
unknown's avatar
unknown committed
130 131 132 133
}

int ha_myisammrg::index_prev(byte * buf)
{
134 135 136 137
  statistic_increment(ha_read_prev_count,&LOCK_status);
  int error=myrg_rprev(file,buf, active_index);
  table->status=error ? STATUS_NOT_FOUND: 0;
  return error;
unknown's avatar
unknown committed
138
}
139

unknown's avatar
unknown committed
140 141
int ha_myisammrg::index_first(byte * buf)
{
142 143 144 145
  statistic_increment(ha_read_first_count,&LOCK_status);
  int error=myrg_rfirst(file, buf, active_index);
  table->status=error ? STATUS_NOT_FOUND: 0;
  return error;
unknown's avatar
unknown committed
146 147 148 149
}

int ha_myisammrg::index_last(byte * buf)
{
150 151 152 153
  statistic_increment(ha_read_last_count,&LOCK_status);
  int error=myrg_rlast(file, buf, active_index);
  table->status=error ? STATUS_NOT_FOUND: 0;
  return error;
unknown's avatar
unknown committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
}

int ha_myisammrg::rnd_init(bool scan)
{
  return myrg_extra(file,HA_EXTRA_RESET);
}

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;
  return error;
}

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;
  return error;
}

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


void ha_myisammrg::info(uint flag)
{
  MYMERGE_INFO info;
  (void) myrg_status(file,&info,flag);
  records = (ha_rows) info.records;
  deleted = (ha_rows) info.deleted;
  data_file_length=info.data_file_length;
  errkey  = info.errkey;
unknown's avatar
unknown committed
192
  table->keys_in_use= set_bits(key_map, table->keys);
unknown's avatar
unknown committed
193
  table->db_options_in_use    = info.options;
unknown's avatar
unknown committed
194
  table->is_view=1;
unknown's avatar
unknown committed
195 196 197
  mean_rec_length=info.reclength;
  block_size=0;
  update_time=0;
198
#if SIZEOF_OFF_T > 4
unknown's avatar
unknown committed
199
  ref_length=6;					// Should be big enough
200 201 202
#else
  ref_length=4;					// Can't be > than my_off_t
#endif
unknown's avatar
unknown committed
203 204 205 206 207
}


int ha_myisammrg::extra(enum ha_extra_function operation)
{
208 209 210 211 212
  /* 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)
    return 0;
unknown's avatar
unknown committed
213 214 215 216 217 218 219 220 221 222 223
  return myrg_extra(file,operation);
}

int ha_myisammrg::reset(void)
{
  return myrg_extra(file,HA_EXTRA_RESET);
}

int ha_myisammrg::external_lock(THD *thd, int lock_type)
{
  return myrg_lock_database(file,lock_type);
224
}
unknown's avatar
unknown committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

uint ha_myisammrg::lock_count(void) const
{
  return file->tables;
}


THR_LOCK_DATA **ha_myisammrg::store_lock(THD *thd,
					 THR_LOCK_DATA **to,
					 enum thr_lock_type lock_type)
{
  MYRG_TABLE *table;

  for (table=file->open_tables ; table != file->end_table ; table++)
  {
    *(to++)= &table->table->lock;
    if (lock_type != TL_IGNORE && table->table->lock.type == TL_UNLOCK)
      table->table->lock.type=lock_type;
  }
  return to;
}

247 248
void ha_myisammrg::update_create_info(HA_CREATE_INFO *create_info)
{
249
  // [phi] auto_increment stuff is missing (but currently not needed)
250 251 252 253 254 255
  DBUG_ENTER("ha_myisammrg::update_create_info");
  if (!(create_info->used_fields & HA_CREATE_USED_UNION))
  {
    MYRG_TABLE *table;
    THD *thd=current_thd;
    create_info->merge_list.next= &create_info->merge_list.first;
256
    create_info->merge_list.elements=0;
257 258 259

    for (table=file->open_tables ; table != file->end_table ; table++)
    {
260
      char *name=table->table->filename;
261 262 263 264 265 266 267
      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;
268
      create_info->merge_list.elements++;
269 270 271 272 273
      (*create_info->merge_list.next) = (byte*) ptr;
      create_info->merge_list.next= (byte**) &ptr->next;
    }
    *create_info->merge_list.next=0;
  }
274 275 276 277
  if (!(create_info->used_fields & HA_CREATE_USED_INSERT_METHOD))
  {
    create_info->merge_insert_method = file->merge_insert_method;
  }
278 279 280 281 282 283 284
  DBUG_VOID_RETURN;

err:
  create_info->merge_list.elements=0;
  create_info->merge_list.first=0;
  DBUG_VOID_RETURN;
}
unknown's avatar
unknown committed
285 286 287 288

int ha_myisammrg::create(const char *name, register TABLE *form,
			 HA_CREATE_INFO *create_info)
{
289 290 291 292 293 294 295 296 297 298 299
  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)
    *pos++= tables->real_name;
  *pos=0;
  DBUG_RETURN(myrg_create(fn_format(buff,name,"","",2+4+16),
300 301 302
			  (const char **) table_names,
                          create_info->merge_insert_method,
                          (my_bool) 0));
unknown's avatar
unknown committed
303
}
unknown's avatar
unknown committed
304 305 306 307

void ha_myisammrg::append_create_info(String *packet)
{
  char buff[FN_REFLEN];
308 309 310
  if (file->merge_insert_method != MERGE_INSERT_DISABLED)
  {
    packet->append(" INSERT_METHOD=",15);
311
    packet->append(get_type(&merge_insert_method,file->merge_insert_method-1));
312
  }
unknown's avatar
unknown committed
313 314 315 316 317
  packet->append(" UNION=(",8);
  MYRG_TABLE *table,*first;

  for (first=table=file->open_tables ; table != file->end_table ; table++)
  {
318
    char *name=table->table->filename;
unknown's avatar
unknown committed
319 320 321 322 323 324 325
    fn_format(buff,name,"","",3);
    if (table != first)
      packet->append(',');
    packet->append(buff,(uint) strlen(buff));
  }
  packet->append(')');
}