handler.cc 57.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 19 20 21 22 23 24 25 26 27
   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 */


/* Handler-calling-functions */

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

#include "mysql_priv.h"
#include "ha_heap.h"
#include "ha_myisam.h"
#include "ha_myisammrg.h"
28
#ifdef HAVE_ISAM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
29 30 31 32 33 34
#include "ha_isam.h"
#include "ha_isammrg.h"
#endif
#ifdef HAVE_BERKELEY_DB
#include "ha_berkeley.h"
#endif
35 36 37
#ifdef HAVE_EXAMPLE_DB
#include "examples/ha_example.h"
#endif
38 39 40
#ifdef HAVE_ARCHIVE_DB
#include "examples/ha_archive.h"
#endif
41 42 43
#ifdef HAVE_CSV_DB
#include "examples/ha_tina.h"
#endif
44
#ifdef HAVE_INNOBASE_DB
45
#include "ha_innodb.h"
46
#endif
47 48 49
#ifdef HAVE_NDBCLUSTER_DB
#include "ha_ndbcluster.h"
#endif
50 51 52
#ifdef HAVE_FEDERATED_DB
#include "ha_federated.h"
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
53 54 55 56 57 58 59
#include <myisampack.h>
#include <errno.h>

	/* static functions defined in this file */

static int NEAR_F delete_file(const char *name,const char *ext,int extflag);

60
ulong ha_read_count, ha_discover_count;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
61

62
static SHOW_COMP_OPTION have_yes= SHOW_OPTION_YES;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
63

64
struct show_table_type_st sys_table_types[]=
65
{
66
  {"MyISAM",	&have_yes,
paul@ice.snake.net's avatar
paul@ice.snake.net committed
67
   "Default engine as of MySQL 3.23 with great performance", DB_TYPE_MYISAM},
68
  {"HEAP",	&have_yes,
paul@ice.snake.net's avatar
paul@ice.snake.net committed
69
   "Alias for MEMORY", DB_TYPE_HEAP},
70
  {"MEMORY",	&have_yes,
paul@ice.snake.net's avatar
paul@ice.snake.net committed
71
   "Hash based, stored in memory, useful for temporary tables", DB_TYPE_HEAP},
72 73 74 75 76
  {"MERGE",	&have_yes,
   "Collection of identical MyISAM tables", DB_TYPE_MRG_MYISAM},
  {"MRG_MYISAM",&have_yes,
   "Alias for MERGE", DB_TYPE_MRG_MYISAM},
  {"ISAM",	&have_isam,
paul@ice.snake.net's avatar
paul@ice.snake.net committed
77
   "Obsolete storage engine, now replaced by MyISAM", DB_TYPE_ISAM},
78
  {"MRG_ISAM",  &have_isam,
paul@ice.snake.net's avatar
paul@ice.snake.net committed
79
   "Obsolete storage engine, now replaced by MERGE", DB_TYPE_MRG_ISAM},
80
  {"InnoDB",	&have_innodb,
paul@ice.snake.net's avatar
paul@ice.snake.net committed
81
   "Supports transactions, row-level locking, and foreign keys", DB_TYPE_INNODB},
82 83 84 85 86 87
  {"INNOBASE",	&have_innodb,
   "Alias for INNODB", DB_TYPE_INNODB},
  {"BDB",	&have_berkeley_db,
   "Supports transactions and page-level locking", DB_TYPE_BERKELEY_DB},
  {"BERKELEYDB",&have_berkeley_db,
   "Alias for BDB", DB_TYPE_BERKELEY_DB},
88
  {"NDBCLUSTER", &have_ndbcluster,
paul@ice.snake.net's avatar
paul@ice.snake.net committed
89
   "Clustered, fault-tolerant, memory-based tables", DB_TYPE_NDBCLUSTER},
90 91
  {"NDB", &have_ndbcluster,
   "Alias for NDBCLUSTER", DB_TYPE_NDBCLUSTER},
92 93
  {"EXAMPLE",&have_example_db,
   "Example storage engine", DB_TYPE_EXAMPLE_DB},
94 95
  {"ARCHIVE",&have_archive_db,
   "Archive storage engine", DB_TYPE_ARCHIVE_DB},
96 97
  {"CSV",&have_csv_db,
   "CSV storage engine", DB_TYPE_CSV_DB},
98 99
  {"FEDERATED",&have_federated_db,
   "Federated MySQL storage engine", DB_TYPE_FEDERATED_DB},
100
  {NullS, NULL, NullS, DB_TYPE_UNKNOWN}
101
};
102

bk@work.mysql.com's avatar
bk@work.mysql.com committed
103
const char *ha_row_type[] = {
104
  "", "FIXED", "DYNAMIC", "COMPRESSED", "REDUNDANT", "COMPACT", "?","?","?"
bk@work.mysql.com's avatar
bk@work.mysql.com committed
105 106
};

monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
107
const char *tx_isolation_names[] =
108 109 110
{ "READ-UNCOMMITTED", "READ-COMMITTED", "REPEATABLE-READ", "SERIALIZABLE",
  NullS};
TYPELIB tx_isolation_typelib= {array_elements(tx_isolation_names)-1,"",
111
			       tx_isolation_names, NULL};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
112

113
static TYPELIB known_extensions= {0,"known_exts", NULL, NULL};
114
uint known_extensions_id= 0;
115

116 117
enum db_type ha_resolve_by_name(const char *name, uint namelen)
{
118
  THD *thd= current_thd;
119 120
  if (thd && !my_strcasecmp(&my_charset_latin1, name, "DEFAULT")) {
    return (enum db_type) thd->variables.table_type;
121 122 123 124 125 126
  }
  
  show_table_type_st *types;
  for (types= sys_table_types; types->type; types++)
  {
    if (!my_strcasecmp(&my_charset_latin1, name, types->type))
127
      return (enum db_type) types->db_type;
128 129 130 131
  }
  return DB_TYPE_UNKNOWN;
}

132
const char *ha_get_storage_engine(enum db_type db_type)
133 134 135 136 137 138 139 140 141 142 143
{
  show_table_type_st *types;
  for (types= sys_table_types; types->type; types++)
  {
    if (db_type == types->db_type)
      return types->type;
  }
  
  return "none";
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
144 145 146 147
	/* Use other database handler if databasehandler is not incompiled */

enum db_type ha_checktype(enum db_type database_type)
{
148
  show_table_type_st *types;
149
  THD *thd= current_thd;
150 151 152
  for (types= sys_table_types; types->type; types++)
  {
    if ((database_type == types->db_type) && 
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
153
	(*types->value == SHOW_OPTION_YES))
154 155 156
      return database_type;
  }

bk@work.mysql.com's avatar
bk@work.mysql.com committed
157 158 159
  switch (database_type) {
#ifndef NO_HASH
  case DB_TYPE_HASH:
160
    return (database_type);
161
#endif
162 163
  case DB_TYPE_MRG_ISAM:
    return (DB_TYPE_MRG_MYISAM);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
164 165 166
  default:
    break;
  }
167
  
168 169 170 171 172
  return ((enum db_type) thd->variables.table_type != DB_TYPE_UNKNOWN ?
          (enum db_type) thd->variables.table_type :
          (enum db_type) global_system_variables.table_type !=
          DB_TYPE_UNKNOWN ?
          (enum db_type) global_system_variables.table_type : DB_TYPE_MYISAM);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
173 174 175 176 177 178 179
} /* ha_checktype */


handler *get_new_handler(TABLE *table, enum db_type db_type)
{
  switch (db_type) {
#ifndef NO_HASH
180 181
  case DB_TYPE_HASH:
    return new ha_hash(table);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
182
#endif
183
#ifdef HAVE_ISAM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
184 185 186 187
  case DB_TYPE_MRG_ISAM:
    return new ha_isammrg(table);
  case DB_TYPE_ISAM:
    return new ha_isam(table);
188 189 190
#else
  case DB_TYPE_MRG_ISAM:
    return new ha_myisammrg(table);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
191 192 193 194
#endif
#ifdef HAVE_BERKELEY_DB
  case DB_TYPE_BERKELEY_DB:
    return new ha_berkeley(table);
195 196
#endif
#ifdef HAVE_INNOBASE_DB
197
  case DB_TYPE_INNODB:
198
    return new ha_innobase(table);
199 200 201 202
#endif
#ifdef HAVE_EXAMPLE_DB
  case DB_TYPE_EXAMPLE_DB:
    return new ha_example(table);
203
#endif
204 205 206
#ifdef HAVE_ARCHIVE_DB
  case DB_TYPE_ARCHIVE_DB:
    return new ha_archive(table);
brian@brian-akers-computer.local's avatar
Merge  
brian@brian-akers-computer.local committed
207
#endif
208 209 210 211
#ifdef HAVE_FEDERATED_DB
  case DB_TYPE_FEDERATED_DB:
    return new ha_federated(table);
#endif
212 213 214 215
#ifdef HAVE_CSV_DB
  case DB_TYPE_CSV_DB:
    return new ha_tina(table);
#endif
216 217 218
#ifdef HAVE_NDBCLUSTER_DB
  case DB_TYPE_NDBCLUSTER:
    return new ha_ndbcluster(table);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
219 220 221 222
#endif
  case DB_TYPE_HEAP:
    return new ha_heap(table);
  default:					// should never happen
223 224 225 226 227 228 229 230
  {
    enum db_type def=(enum db_type) current_thd->variables.table_type;
    /* Try first with 'default table type' */
    if (db_type != def)
      return get_new_handler(table, def);
  }
  /* Fall back to MyISAM */
  case DB_TYPE_MYISAM:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
231 232 233 234 235 236
    return new ha_myisam(table);
  case DB_TYPE_MRG_MYISAM:
    return new ha_myisammrg(table);
  }
}

237 238 239 240 241
bool ha_caching_allowed(THD* thd, char* table_key,
                        uint key_length, uint8 cache_type)
{
#ifdef HAVE_INNOBASE_DB
  if (cache_type == HA_CACHE_TBL_ASKTRANSACT)
242
    return innobase_query_caching_of_table_permitted(thd, table_key, key_length);
243
#endif
244
  return 1;
245 246
}

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

/*
  Register handler error messages for use with my_error().

  SYNOPSIS
    ha_init_errors()

  RETURN
    0           OK
    != 0        Error
*/

static int ha_init_errors(void)
{
#define SETMSG(nr, msg) errmsgs[(nr) - HA_ERR_FIRST]= (msg)
  const char    **errmsgs;

  /* Allocate a pointer array for the error message strings. */
  /* Zerofill it to avoid uninitialized gaps. */
  if (! (errmsgs= (const char**) my_malloc(HA_ERR_ERRORS * sizeof(char*),
                                           MYF(MY_WME | MY_ZEROFILL))))
    return 1;

  /* Set the dedicated error messages. */
  SETMSG(HA_ERR_KEY_NOT_FOUND,          ER(ER_KEY_NOT_FOUND));
  SETMSG(HA_ERR_FOUND_DUPP_KEY,         ER(ER_DUP_KEY));
  SETMSG(HA_ERR_RECORD_CHANGED,         "Update wich is recoverable");
  SETMSG(HA_ERR_WRONG_INDEX,            "Wrong index given to function");
  SETMSG(HA_ERR_CRASHED,                ER(ER_NOT_KEYFILE));
  SETMSG(HA_ERR_WRONG_IN_RECORD,        ER(ER_CRASHED_ON_USAGE));
  SETMSG(HA_ERR_OUT_OF_MEM,             "Table handler out of memory");
  SETMSG(HA_ERR_NOT_A_TABLE,            "Incorrect file format '%.64s'");
  SETMSG(HA_ERR_WRONG_COMMAND,          "Command not supported");
  SETMSG(HA_ERR_OLD_FILE,               ER(ER_OLD_KEYFILE));
  SETMSG(HA_ERR_NO_ACTIVE_RECORD,       "No record read in update");
  SETMSG(HA_ERR_RECORD_DELETED,         "Intern record deleted");
  SETMSG(HA_ERR_RECORD_FILE_FULL,       ER(ER_RECORD_FILE_FULL));
  SETMSG(HA_ERR_INDEX_FILE_FULL,        "No more room in index file '%.64s'");
  SETMSG(HA_ERR_END_OF_FILE,            "End in next/prev/first/last");
  SETMSG(HA_ERR_UNSUPPORTED,            ER(ER_ILLEGAL_HA));
  SETMSG(HA_ERR_TO_BIG_ROW,             "Too big row");
  SETMSG(HA_WRONG_CREATE_OPTION,        "Wrong create option");
  SETMSG(HA_ERR_FOUND_DUPP_UNIQUE,      ER(ER_DUP_UNIQUE));
  SETMSG(HA_ERR_UNKNOWN_CHARSET,        "Can't open charset");
  SETMSG(HA_ERR_WRONG_MRG_TABLE_DEF,    ER(ER_WRONG_MRG_TABLE));
  SETMSG(HA_ERR_CRASHED_ON_REPAIR,      ER(ER_CRASHED_ON_REPAIR));
  SETMSG(HA_ERR_CRASHED_ON_USAGE,       ER(ER_CRASHED_ON_USAGE));
  SETMSG(HA_ERR_LOCK_WAIT_TIMEOUT,      ER(ER_LOCK_WAIT_TIMEOUT));
  SETMSG(HA_ERR_LOCK_TABLE_FULL,        ER(ER_LOCK_TABLE_FULL));
  SETMSG(HA_ERR_READ_ONLY_TRANSACTION,  ER(ER_READ_ONLY_TRANSACTION));
  SETMSG(HA_ERR_LOCK_DEADLOCK,          ER(ER_LOCK_DEADLOCK));
  SETMSG(HA_ERR_CANNOT_ADD_FOREIGN,     ER(ER_CANNOT_ADD_FOREIGN));
  SETMSG(HA_ERR_NO_REFERENCED_ROW,      ER(ER_NO_REFERENCED_ROW));
  SETMSG(HA_ERR_ROW_IS_REFERENCED,      ER(ER_ROW_IS_REFERENCED));
  SETMSG(HA_ERR_NO_SAVEPOINT,           "No savepoint with that name");
  SETMSG(HA_ERR_NON_UNIQUE_BLOCK_SIZE,  "Non unique key block size");
  SETMSG(HA_ERR_NO_SUCH_TABLE,          "No such table: '%.64s'");
  SETMSG(HA_ERR_TABLE_EXIST,            ER(ER_TABLE_EXISTS_ERROR));
  SETMSG(HA_ERR_NO_CONNECTION,          "Could not connect to storage engine");

  /* Register the error messages for use with my_error(). */
  return my_error_register(errmsgs, HA_ERR_FIRST, HA_ERR_LAST);
}


/*
  Unregister handler error messages.

  SYNOPSIS
    ha_finish_errors()

  RETURN
    0           OK
    != 0        Error
*/

static int ha_finish_errors(void)
{
  const char    **errmsgs;

  /* Allocate a pointer array for the error message strings. */
  if (! (errmsgs= my_error_unregister(HA_ERR_FIRST, HA_ERR_LAST)))
    return 1;
  my_free((gptr) errmsgs, MYF(0));
  return 0;
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
335 336
int ha_init()
{
337
  int error= 0;
338 339
  if (ha_init_errors())
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
340
#ifdef HAVE_BERKELEY_DB
341
  if (have_berkeley_db == SHOW_OPTION_YES)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
342
  {
343 344 345 346 347
    if (berkeley_init())
    {
      have_berkeley_db= SHOW_OPTION_DISABLED;	// If we couldn't use handler
      error= 1;
    }
348
    else
349
      opt_using_transactions=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
350
  }
351 352
#endif
#ifdef HAVE_INNOBASE_DB
353
  if (have_innodb == SHOW_OPTION_YES)
354
  {
monty@donna.mysql.fi's avatar
monty@donna.mysql.fi committed
355
    if (innobase_init())
356 357 358 359
    {
      have_innodb= SHOW_OPTION_DISABLED;	// If we couldn't use handler
      error= 1;
    }
360
    else
361
      opt_using_transactions=1;
362
  }
363 364 365 366 367 368 369 370 371 372 373 374
#endif
#ifdef HAVE_NDBCLUSTER_DB
  if (have_ndbcluster == SHOW_OPTION_YES)
  {
    if (ndbcluster_init())
    {
      have_ndbcluster= SHOW_OPTION_DISABLED;
      error= 1;
    }
    else
      opt_using_transactions=1;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
375
#endif
376
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
377 378 379 380 381 382 383 384 385 386 387
}

	/* close, flush or restart databases */
	/* Ignore this for other databases than ours */

int ha_panic(enum ha_panic_function flag)
{
  int error=0;
#ifndef NO_HASH
  error|=h_panic(flag);			/* fix hash */
#endif
388 389
#ifdef HAVE_ISAM
  error|=mrg_panic(flag);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
390
  error|=nisam_panic(flag);
391 392
#endif
  error|=heap_panic(flag);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
393 394 395
  error|=mi_panic(flag);
  error|=myrg_panic(flag);
#ifdef HAVE_BERKELEY_DB
396
  if (have_berkeley_db == SHOW_OPTION_YES)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
397
    error|=berkeley_end();
398 399
#endif
#ifdef HAVE_INNOBASE_DB
400
  if (have_innodb == SHOW_OPTION_YES)
401
    error|=innobase_end();
402 403 404 405
#endif
#ifdef HAVE_NDBCLUSTER_DB
  if (have_ndbcluster == SHOW_OPTION_YES)
    error|=ndbcluster_end();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
406
#endif
407 408
  if (ha_finish_errors())
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
409 410 411
  return error;
} /* ha_panic */

412 413 414
void ha_drop_database(char* path)
{
#ifdef HAVE_INNOBASE_DB
415
  if (have_innodb == SHOW_OPTION_YES)
416 417
    innobase_drop_database(path);
#endif
418 419 420 421
#ifdef HAVE_NDBCLUSTER_DB
  if (have_ndbcluster == SHOW_OPTION_YES)
    ndbcluster_drop_database(path);
#endif
422
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
423

424 425 426
void ha_close_connection(THD* thd)
{
#ifdef HAVE_INNOBASE_DB
427
  if (have_innodb == SHOW_OPTION_YES)
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
428
    innobase_close_connection(thd);
429
#endif
430 431 432 433
#ifdef HAVE_NDBCLUSTER_DB
  if (have_ndbcluster == SHOW_OPTION_YES)
    ndbcluster_close_connection(thd);
#endif
434 435
}

436
/*
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
437
  This is used to commit or rollback a single statement depending on the value
438 439 440 441 442
  of error. Note that if the autocommit is on, then the following call inside
  InnoDB will commit or rollback the whole transaction (= the statement). The
  autocommit mechanism built into InnoDB is based on counting locks, but if
  the user has used LOCK TABLES then that mechanism does not know to do the
  commit.
443 444
*/

bk@work.mysql.com's avatar
bk@work.mysql.com committed
445 446 447
int ha_autocommit_or_rollback(THD *thd, int error)
{
  DBUG_ENTER("ha_autocommit_or_rollback");
448
#ifdef USING_TRANSACTIONS
449
  if (opt_using_transactions)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
450
  {
451 452 453 454 455 456 457
    if (!error)
    {
      if (ha_commit_stmt(thd))
	error=1;
    }
    else
      (void) ha_rollback_stmt(thd);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
458

459
    thd->variables.tx_isolation=thd->session_tx_isolation;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
460 461 462 463 464
  }
#endif
  DBUG_RETURN(error);
}

465 466 467
/*
  This function is called when MySQL writes the log segment of a
  transaction to the binlog. It is called when the LOCK_log mutex is
468
  reserved. Here we communicate to transactional table handlers what
469 470 471 472 473
  binlog position corresponds to the current transaction. The handler
  can store it and in recovery print to the user, so that the user
  knows from what position in the binlog to start possible
  roll-forward, for example, if the crashed server was a slave in
  replication. This function also calls the commit of the table
474
  handler, because the order of transactions in the log of the table
475
  handler must be the same as in the binlog.
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
476 477 478
  NOTE that to eliminate the bottleneck of the group commit, we do not
  flush the handler log files here, but only later in a call of
  ha_commit_complete().
479 480

  arguments:
481
  thd:           the thread handle of the current connection
482 483
  log_file_name: latest binlog file name
  end_offset:	 the offset in the binlog file up to which we wrote
484
  return value:  0 if success, 1 if error
485
*/
486

487 488 489
int ha_report_binlog_offset_and_commit(THD *thd,
				       char *log_file_name,
				       my_off_t end_offset)
490
{
491 492
  int  error= 0;
#ifdef HAVE_INNOBASE_DB
493 494
  THD_TRANS *trans;
  trans = &thd->transaction.all;
495
  if (trans->innodb_active_trans)
496
  {
497 498 499 500 501 502
    /*
      If we updated some InnoDB tables (innodb_active_trans is true), the
      binlog coords will be reported into InnoDB during the InnoDB commit
      (innobase_report_binlog_offset_and_commit). But if we updated only
      non-InnoDB tables, we need an explicit call to report it.
    */
503
    if ((error=innobase_report_binlog_offset_and_commit(thd,
504 505 506
                                                        trans->innobase_tid,
                                                        log_file_name,
                                                        end_offset)))
507 508 509 510 511
    {
      my_error(ER_ERROR_DURING_COMMIT, MYF(0), error);
      error=1;
    }
  }
512 513
  else if (opt_innodb_safe_binlog) // Don't report if not useful
    innobase_store_binlog_offset_and_flush_log(log_file_name, end_offset);
514 515 516
#endif
  return error;
}
517

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
/*
  Flushes the handler log files (if my.cnf settings do not free us from it)
  after we have called ha_report_binlog_offset_and_commit(). To eliminate
  the bottleneck from the group commit, this should be called when
  LOCK_log has been released in log.cc.

  arguments:
  thd:           the thread handle of the current connection
  return value:  always 0
*/

int ha_commit_complete(THD *thd)
{
#ifdef HAVE_INNOBASE_DB
  THD_TRANS *trans;
  trans = &thd->transaction.all;
  if (trans->innobase_tid)
  {
    innobase_commit_complete(trans->innobase_tid);

    trans->innodb_active_trans=0;
  }
#endif
  return 0;
}
543

544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
/*
  This function should be called when MySQL sends rows of a SELECT result set
  or the EOF mark to the client. It releases a possible adaptive hash index
  S-latch held by thd in InnoDB and also releases a possible InnoDB query
  FIFO ticket to enter InnoDB. To save CPU time, InnoDB allows a thd to
  keep them over several calls of the InnoDB handler interface when a join
  is executed. But when we let the control to pass to the client they have
  to be released because if the application program uses mysql_use_result(),
  it may deadlock on the S-latch if the application on another connection
  performs another SQL query. In MySQL-4.1 this is even more important because
  there a connection can have several SELECT queries open at the same time.

  arguments:
  thd:           the thread handle of the current connection
  return value:  always 0
*/

int ha_release_temporary_latches(THD *thd)
{
#ifdef HAVE_INNOBASE_DB
  THD_TRANS *trans;
  trans = &thd->transaction.all;
  if (trans->innobase_tid)
    innobase_release_temporary_latches(trans->innobase_tid);
#endif
  return 0;
}

572 573 574 575 576 577 578 579 580 581 582 583 584 585 586

/* 
  Export statistics for different engines. Currently we use it only for
  InnoDB.
*/

int ha_update_statistics()
{
#ifdef HAVE_INNOBASE_DB
  if (opt_innodb)
    innodb_export_status();
#endif
  return 0;
}

587
int ha_commit_trans(THD *thd, THD_TRANS* trans)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
588 589
{
  int error=0;
590
  DBUG_ENTER("ha_commit_trans");
591
#ifdef USING_TRANSACTIONS
592
  if (opt_using_transactions)
593
  {
serg@serg.mylan's avatar
serg@serg.mylan committed
594
    bool transaction_commited= 0;
595
    bool operation_done= 0, need_start_waiters= 0;
596

597
    /* If transaction has done some updates to tables */
598 599
    if (trans == &thd->transaction.all && mysql_bin_log.is_open() &&
        my_b_tell(&thd->transaction.trans_log))
600
    {
601
      if ((error= wait_if_global_read_lock(thd, 0, 0)))
602
      {
603 604 605
        /*
          Note that ROLLBACK [TO SAVEPOINT] does not have this test; it's
          because ROLLBACK never updates data, so needn't wait on the lock.
606
        */
607 608 609 610 611 612 613 614
        my_error(ER_ERROR_DURING_COMMIT, MYF(0), error);
        error= 1;
      }
      else
        need_start_waiters= 1;
      if (mysql_bin_log.is_open())
      {
        mysql_bin_log.write(thd, &thd->transaction.trans_log, 1);
serg@serg.mylan's avatar
serg@serg.mylan committed
615 616 617 618 619 620 621 622 623 624
        statistic_increment(binlog_cache_use, &LOCK_status);
        if (thd->transaction.trans_log.disk_writes != 0)
        {
          /* 
            We have to do this after addition of trans_log to main binlog since
            this operation can cause flushing of end of trans_log to disk. 
          */
          statistic_increment(binlog_cache_disk_use, &LOCK_status);
          thd->transaction.trans_log.disk_writes= 0;
        }
625 626 627
        reinit_io_cache(&thd->transaction.trans_log,
                        WRITE_CACHE, (my_off_t) 0, 0, 1);
        thd->transaction.trans_log.end_of_file= max_binlog_cache_size;
628
      }
629
    }
630 631 632 633 634
#ifdef HAVE_NDBCLUSTER_DB
    if (trans->ndb_tid)
    {
      if ((error=ndbcluster_commit(thd,trans->ndb_tid)))
      {
635
	if (error == -1)
636 637
	  my_message(ER_ERROR_DURING_COMMIT, ER(ER_ERROR_DURING_COMMIT),
		     MYF(0));
638 639 640 641 642 643 644
        error=1;
      }
      if (trans == &thd->transaction.all)
        operation_done= transaction_commited= 1;
      trans->ndb_tid=0;
    }
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
645
#ifdef HAVE_BERKELEY_DB
646
    if (trans->bdb_tid)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
647
    {
648 649 650 651 652
      if ((error=berkeley_commit(thd,trans->bdb_tid)))
      {
	my_error(ER_ERROR_DURING_COMMIT, MYF(0), error);
	error=1;
      }
653
      else
654 655
	if (!(thd->options & OPTION_BEGIN))
	  transaction_commited= 1; 
656
      trans->bdb_tid=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
657
    }
658 659
#endif
#ifdef HAVE_INNOBASE_DB
660
    if (trans->innobase_tid)
661
    {
662 663 664 665 666
      if ((error=innobase_commit(thd,trans->innobase_tid)))
      {
	my_error(ER_ERROR_DURING_COMMIT, MYF(0), error);
	error=1;
      }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
667
      trans->innodb_active_trans=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
668
      if (trans == &thd->transaction.all)
669
	operation_done= transaction_commited= 1;
670
    }
671
#endif
672
#ifdef HAVE_QUERY_CACHE
673
    if (transaction_commited && thd->transaction.changed_tables)
674
      query_cache.invalidate(thd->transaction.changed_tables);
675
#endif /*HAVE_QUERY_CACHE*/
676
    if (error && trans == &thd->transaction.all && mysql_bin_log.is_open())
serg@serg.mylan's avatar
serg@serg.mylan committed
677
      sql_print_error("Got error during commit;  Binlog is not up to date!");
678
    thd->variables.tx_isolation=thd->session_tx_isolation;
679
    if (operation_done)
680
    {
681
      statistic_increment(thd->status_var.ha_commit_count,&LOCK_status);
682 683
      thd->transaction.cleanup();
    }
684 685
    if (need_start_waiters)
      start_waiting_global_read_lock(thd);
686
  }
687
#endif // using transactions
bk@work.mysql.com's avatar
bk@work.mysql.com committed
688 689 690
  DBUG_RETURN(error);
}

691

692
int ha_rollback_trans(THD *thd, THD_TRANS *trans)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
693 694
{
  int error=0;
695
  DBUG_ENTER("ha_rollback_trans");
696 697
#ifdef USING_TRANSACTIONS
  if (opt_using_transactions)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
698
  {
699
    bool operation_done=0;
700 701 702 703 704 705
    /*
      As rollback can be 30 times slower than insert in InnoDB, and user may
      not know there's rollback (if it's because of a dupl row), better warn.
    */
    const char *save_proc_info= thd->proc_info;
    thd->proc_info= "Rolling back";
706 707 708 709 710
#ifdef HAVE_NDBCLUSTER_DB
    if (trans->ndb_tid)
    {
      if ((error=ndbcluster_rollback(thd, trans->ndb_tid)))
      {
711
	if (error == -1)
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
712 713
	  my_message(ER_ERROR_DURING_ROLLBACK, ER(ER_ERROR_DURING_ROLLBACK),
                     MYF(0));
714 715 716 717 718 719
        error=1;
      }
      trans->ndb_tid = 0;
      operation_done=1;
    }
#endif
720 721
#ifdef HAVE_BERKELEY_DB
    if (trans->bdb_tid)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
722
    {
723 724 725 726 727 728
      if ((error=berkeley_rollback(thd, trans->bdb_tid)))
      {
	my_error(ER_ERROR_DURING_ROLLBACK, MYF(0), error);
	error=1;
      }
      trans->bdb_tid=0;
729
      operation_done=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
730
    }
731 732
#endif
#ifdef HAVE_INNOBASE_DB
733
    if (trans->innobase_tid)
734
    {
735 736 737 738 739
      if ((error=innobase_rollback(thd, trans->innobase_tid)))
      {
	my_error(ER_ERROR_DURING_ROLLBACK, MYF(0), error);
	error=1;
      }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
740
      trans->innodb_active_trans=0;
741
      operation_done=1;
742
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
743
#endif
744
    if ((trans == &thd->transaction.all) && mysql_bin_log.is_open())
guilhem@mysql.com's avatar
guilhem@mysql.com committed
745 746
    {
      /* 
747 748 749 750 751
         Update the binary log with a BEGIN/ROLLBACK block if we have
         cached some queries and we updated some non-transactional
         table. Such cases should be rare (updating a
         non-transactional table inside a transaction...).  Count disk
         writes to trans_log in any case.
guilhem@mysql.com's avatar
guilhem@mysql.com committed
752
      */
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
      if (my_b_tell(&thd->transaction.trans_log))
      {
        if (unlikely(thd->options & OPTION_STATUS_NO_TRANS_UPDATE))
          mysql_bin_log.write(thd, &thd->transaction.trans_log, 0);
        statistic_increment(binlog_cache_use, &LOCK_status);
        if (thd->transaction.trans_log.disk_writes != 0)
        {
          /* 
            We have to do this after addition of trans_log to main binlog since
            this operation can cause flushing of end of trans_log to disk. 
          */
          statistic_increment(binlog_cache_disk_use, &LOCK_status);
          thd->transaction.trans_log.disk_writes= 0;
        }
      }
guilhem@mysql.com's avatar
guilhem@mysql.com committed
768
      /* Flushed or not, empty the binlog cache */
769
      reinit_io_cache(&thd->transaction.trans_log,
guilhem@mysql.com's avatar
guilhem@mysql.com committed
770 771
                      WRITE_CACHE, (my_off_t) 0, 0, 1);
      thd->transaction.trans_log.end_of_file= max_binlog_cache_size;
772 773
      if (operation_done)
        thd->transaction.cleanup();
guilhem@mysql.com's avatar
guilhem@mysql.com committed
774
    }
775
    thd->variables.tx_isolation=thd->session_tx_isolation;
776
    if (operation_done)
777
      statistic_increment(thd->status_var.ha_rollback_count,&LOCK_status);
778
    thd->proc_info= save_proc_info;
779 780
  }
#endif /* USING_TRANSACTIONS */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
781 782 783
  DBUG_RETURN(error);
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
784 785

/*
guilhem@mysql.com's avatar
guilhem@mysql.com committed
786 787 788 789 790 791 792 793 794 795
  Rolls the current transaction back to a savepoint.
  Return value: 0 if success, 1 if there was not a savepoint of the given
  name.
  NOTE: how do we handle this (unlikely but legal) case:
  [transaction] + [update to non-trans table] + [rollback to savepoint] ?
  The problem occurs when a savepoint is before the update to the
  non-transactional table. Then when there's a rollback to the savepoint, if we
  simply truncate the binlog cache, we lose the part of the binlog cache where
  the update is. If we want to not lose it, we need to write the SAVEPOINT
  command and the ROLLBACK TO SAVEPOINT command to the binlog cache. The latter
796 797 798 799 800 801
  is easy: it's just write at the end of the binlog cache, but the former
  should be *inserted* to the place where the user called SAVEPOINT. The
  solution is that when the user calls SAVEPOINT, we write it to the binlog
  cache (so no need to later insert it). As transactions are never intermixed
  in the binary log (i.e. they are serialized), we won't have conflicts with
  savepoint names when using mysqlbinlog or in the slave SQL thread.
guilhem@mysql.com's avatar
guilhem@mysql.com committed
802 803 804 805 806
  Then when ROLLBACK TO SAVEPOINT is called, if we updated some
  non-transactional table, we don't truncate the binlog cache but instead write
  ROLLBACK TO SAVEPOINT to it; otherwise we truncate the binlog cache (which
  will chop the SAVEPOINT command from the binlog cache, which is good as in
  that case there is no need to have it in the binlog).
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
*/

int ha_rollback_to_savepoint(THD *thd, char *savepoint_name)
{
  my_off_t binlog_cache_pos=0;
  bool operation_done=0;
  int error=0;
  DBUG_ENTER("ha_rollback_to_savepoint");
#ifdef USING_TRANSACTIONS
  if (opt_using_transactions)
  {
#ifdef HAVE_INNOBASE_DB
    /*
    Retrieve the trans_log binlog cache position corresponding to the
    savepoint, and if the rollback is successful inside InnoDB reset the write
    position in the binlog cache to what it was at the savepoint.
    */
    if ((error=innobase_rollback_to_savepoint(thd, savepoint_name,
						  &binlog_cache_pos)))
    {
      my_error(ER_ERROR_DURING_ROLLBACK, MYF(0), error);
      error=1;
    }
830
    else if (mysql_bin_log.is_open())
guilhem@mysql.com's avatar
guilhem@mysql.com committed
831 832 833 834 835 836 837 838 839
    {
      /* 
         Write ROLLBACK TO SAVEPOINT to the binlog cache if we have updated some
         non-transactional table. Otherwise, truncate the binlog cache starting
         from the SAVEPOINT command.
      */
      if (unlikely((thd->options & OPTION_STATUS_NO_TRANS_UPDATE) &&
                   my_b_tell(&thd->transaction.trans_log)))
      {
840
        Query_log_event qinfo(thd, thd->query, thd->query_length, TRUE, FALSE);
guilhem@mysql.com's avatar
guilhem@mysql.com committed
841 842 843 844 845 846 847
        if (mysql_bin_log.write(&qinfo))
          error= 1;
      }
      else
        reinit_io_cache(&thd->transaction.trans_log, WRITE_CACHE,
                        binlog_cache_pos, 0, 0);
    }
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
848 849 850
    operation_done=1;
#endif
    if (operation_done)
851
      statistic_increment(thd->status_var.ha_rollback_count,&LOCK_status);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
  }
#endif /* USING_TRANSACTIONS */

  DBUG_RETURN(error);
}


/*
Sets a transaction savepoint.
Return value: always 0, that is, succeeds always
*/

int ha_savepoint(THD *thd, char *savepoint_name)
{
  int error=0;
  DBUG_ENTER("ha_savepoint");
#ifdef USING_TRANSACTIONS
  if (opt_using_transactions)
  {
871
    /* Write it to the binary log (see comments of ha_rollback_to_savepoint) */
guilhem@mysql.com's avatar
guilhem@mysql.com committed
872 873
    if (mysql_bin_log.is_open())
    {
874 875 876 877
#ifdef HAVE_INNOBASE_DB
      innobase_savepoint(thd,savepoint_name,
                         my_b_tell(&thd->transaction.trans_log));
#endif
878
      Query_log_event qinfo(thd, thd->query, thd->query_length, TRUE, FALSE);
guilhem@mysql.com's avatar
guilhem@mysql.com committed
879 880 881
      if (mysql_bin_log.write(&qinfo))
	error= 1;
    }
882 883 884 885
#ifdef HAVE_INNOBASE_DB
    else
      innobase_savepoint(thd,savepoint_name,0);
#endif
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
886 887 888 889 890
  }
#endif /* USING_TRANSACTIONS */
  DBUG_RETURN(error);
}

891 892 893 894 895 896 897 898 899 900 901 902

int ha_start_consistent_snapshot(THD *thd)
{
#ifdef HAVE_INNOBASE_DB
  if ((have_innodb == SHOW_OPTION_YES) &&
      !innobase_start_trx_and_assign_read_view(thd))
    return 0;
#endif
  /*
    Same idea as when one wants to CREATE TABLE in one engine which does not
    exist:
  */
903 904 905
  push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
               "This MySQL server does not support any "
               "consistent-read capable storage engine");
906 907 908 909
  return 0;
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
910 911 912 913
bool ha_flush_logs()
{
  bool result=0;
#ifdef HAVE_BERKELEY_DB
914
  if ((have_berkeley_db == SHOW_OPTION_YES) &&
915
      berkeley_flush_logs())
bk@work.mysql.com's avatar
bk@work.mysql.com committed
916
    result=1;
917 918
#endif
#ifdef HAVE_INNOBASE_DB
919
  if ((have_innodb == SHOW_OPTION_YES) &&
920
      innobase_flush_logs())
921
    result=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
922 923 924 925
#endif
  return result;
}

926 927 928 929
/*
  This should return ENOENT if the file doesn't exists.
  The .frm file will be deleted only if we return 0 or ENOENT
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
930 931 932

int ha_delete_table(enum db_type table_type, const char *path)
{
933
  handler *file;
934
  char tmp_path[FN_REFLEN];
935 936 937 938

  /* DB_TYPE_UNKNOWN is used in ALTER TABLE when renaming only .frm files */
  if (table_type == DB_TYPE_UNKNOWN ||
      ! (file=get_new_handler((TABLE*) 0, table_type)))
939
    return ENOENT;
940

941 942 943 944
  if (lower_case_table_names == 2 && !(file->table_flags() & HA_FILE_BASED))
  {
    /* Ensure that table handler get path in lower case */
    strmov(tmp_path, path);
945
    my_casedn_str(files_charset_info, tmp_path);
946 947
    path= tmp_path;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
948 949 950 951
  int error=file->delete_table(path);
  delete file;
  return error;
}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
952

953

bk@work.mysql.com's avatar
bk@work.mysql.com committed
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
void ha_store_ptr(byte *buff, uint pack_length, my_off_t pos)
{
  switch (pack_length) {
#if SIZEOF_OFF_T > 4
  case 8: mi_int8store(buff,pos); break;
  case 7: mi_int7store(buff,pos); break;
  case 6: mi_int6store(buff,pos); break;
  case 5: mi_int5store(buff,pos); break;
#endif
  case 4: mi_int4store(buff,pos); break;
  case 3: mi_int3store(buff,pos); break;
  case 2: mi_int2store(buff,(uint) pos); break;
  case 1: buff[0]= (uchar) pos; break;
  }
  return;
}

my_off_t ha_get_ptr(byte *ptr, uint pack_length)
{
  my_off_t pos;
  switch (pack_length) {
#if SIZEOF_OFF_T > 4
  case 8:
    pos= (my_off_t) mi_uint8korr(ptr);
    break;
  case 7:
    pos= (my_off_t) mi_uint7korr(ptr);
    break;
  case 6:
    pos= (my_off_t) mi_uint6korr(ptr);
    break;
  case 5:
    pos= (my_off_t) mi_uint5korr(ptr);
    break;
#endif
  case 4:
    pos= (my_off_t) mi_uint4korr(ptr);
    break;
  case 3:
    pos= (my_off_t) mi_uint3korr(ptr);
    break;
  case 2:
    pos= (my_off_t) mi_uint2korr(ptr);
    break;
  case 1:
    pos= (my_off_t) mi_uint2korr(ptr);
    break;
  default:
    pos=0;					// Impossible
    break;
  }
 return pos;
}

/****************************************************************************
** General handler functions
****************************************************************************/

	/* Open database-handler. Try O_RDONLY if can't open as O_RDWR */
	/* Don't wait for locks if not HA_OPEN_WAIT_IF_LOCKED is set */

int handler::ha_open(const char *name, int mode, int test_if_locked)
{
  int error;
1018
  DBUG_ENTER("handler::ha_open");
1019
  DBUG_PRINT("enter",("name: %s  db_type: %d  db_stat: %d  mode: %d  lock_test: %d",
1020
		      name, table->s->db_type, table->db_stat, mode,
1021
		      test_if_locked));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038

  if ((error=open(name,mode,test_if_locked)))
  {
    if ((error == EACCES || error == EROFS) && mode == O_RDWR &&
	(table->db_stat & HA_TRY_READ_ONLY))
    {
      table->db_stat|=HA_READ_ONLY;
      error=open(name,O_RDONLY,test_if_locked);
    }
  }
  if (error)
  {
    my_errno=error;			/* Safeguard */
    DBUG_PRINT("error",("error: %d  errno: %d",error,errno));
  }
  else
  {
1039
    if (table->s->db_options_in_use & HA_OPTION_READ_ONLY_DATA)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1040
      table->db_stat|=HA_READ_ONLY;
1041 1042
    (void) extra(HA_EXTRA_NO_READCHECK);	// Not needed in SQL

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1043
    if (!alloc_root_inited(&table->mem_root))	// If temporary table
1044
      ref=(byte*) sql_alloc(ALIGN_SIZE(ref_length)*2);
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1045 1046 1047
    else
      ref=(byte*) alloc_root(&table->mem_root, ALIGN_SIZE(ref_length)*2);
    if (!ref)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
    {
      close();
      error=HA_ERR_OUT_OF_MEM;
    }
    else
      dupp_ref=ref+ALIGN_SIZE(ref_length);
  }
  DBUG_RETURN(error);
}

1058 1059 1060 1061 1062
/*
  Read first row (only) from a table
  This is never called for InnoDB or BDB tables, as these table types
  has the HA_NOT_EXACT_COUNT set.
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1063

1064
int handler::read_first_row(byte * buf, uint primary_key)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1065 1066
{
  register int error;
1067
  DBUG_ENTER("handler::read_first_row");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1068

1069
  statistic_increment(current_thd->status_var.ha_read_first_count,&LOCK_status);
1070 1071 1072 1073

  /*
    If there is very few deleted rows in the table, find the first row by
    scanning the table.
1074
    TODO remove the test for HA_READ_ORDER
1075
  */
1076 1077
  if (deleted < 10 || primary_key >= MAX_KEY ||
      !(index_flags(primary_key, 0, 0) & HA_READ_ORDER))
1078
  {
1079
    (void) ha_rnd_init(1);
1080
    while ((error= rnd_next(buf)) == HA_ERR_RECORD_DELETED) ;
1081
    (void) ha_rnd_end();
1082 1083 1084 1085
  }
  else
  {
    /* Find the first row through the primary key */
1086
    (void) ha_index_init(primary_key);
1087
    error=index_first(buf);
1088
    (void) ha_index_end();
1089
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1090 1091 1092 1093
  DBUG_RETURN(error);
}


1094
/*
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
  Generate the next auto-increment number based on increment and offset
  
  In most cases increment= offset= 1, in which case we get:
  1,2,3,4,5,...
  If increment=10 and offset=5 and previous number is 1, we get:
  1,5,15,25,35,...
*/

inline ulonglong
next_insert_id(ulonglong nr,struct system_variables *variables)
{
  nr= (((nr+ variables->auto_increment_increment -
         variables->auto_increment_offset)) /
       (ulonglong) variables->auto_increment_increment);
  return (nr* (ulonglong) variables->auto_increment_increment +
          variables->auto_increment_offset);
}


/*
  Updates columns with type NEXT_NUMBER if:

  - If column value is set to NULL (in which case
    auto_increment_field_not_null is 0)
  - If column is set to 0 and (sql_mode & MODE_NO_AUTO_VALUE_ON_ZERO) is not
    set. In the future we will only set NEXT_NUMBER fields if one sets them
    to NULL (or they are not included in the insert list).


  There are two different cases when the above is true:

  - thd->next_insert_id == 0  (This is the normal case)
    In this case we set the set the column for the first row to the value
    next_insert_id(get_auto_increment(column))) which is normally
    max-used-column-value +1.

    We call get_auto_increment() only for the first row in a multi-row
    statement. For the following rows we generate new numbers based on the
    last used number.

  - thd->next_insert_id != 0.  This happens when we have read a statement
    from the binary log or when one has used SET LAST_INSERT_ID=#.

    In this case we will set the column to the value of next_insert_id.
    The next row will be given the id
    next_insert_id(next_insert_id)

    The idea is that generated auto_increment values are predictable and
    independent of the column values in the table.  This is needed to be
    able to replicate into a table that already has rows with a higher
    auto-increment value than the one that is inserted.

    After we have already generated an auto-increment number and the user
    inserts a column with a higher value than the last used one, we will
    start counting from the inserted value.

    thd->next_insert_id is cleared after it's been used for a statement.
1152
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1153 1154 1155

void handler::update_auto_increment()
{
1156 1157 1158
  ulonglong nr;
  THD *thd= table->in_use;
  struct system_variables *variables= &thd->variables;
1159
  DBUG_ENTER("handler::update_auto_increment");
1160 1161 1162 1163 1164 1165 1166 1167

  /*
    We must save the previous value to be able to restore it if the
    row was not inserted
  */
  thd->prev_insert_id= thd->next_insert_id;

  if ((nr= table->next_number_field->val_int()) != 0 ||
1168
      table->auto_increment_field_not_null &&
1169
      thd->variables.sql_mode & MODE_NO_AUTO_VALUE_ON_ZERO)
1170
  {
1171
    /* Clear flag for next row */
monty@mysql.com's avatar
monty@mysql.com committed
1172
    table->auto_increment_field_not_null= FALSE;
1173
    /* Mark that we didn't generate a new value **/
1174
    auto_increment_column_changed=0;
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185

    /* Update next_insert_id if we have already generated a value */
    if (thd->clear_next_insert_id && nr >= thd->next_insert_id)
    {
      if (variables->auto_increment_increment != 1)
        nr= next_insert_id(nr, variables);
      else
        nr++;
      thd->next_insert_id= nr;
      DBUG_PRINT("info",("next_insert_id: %lu", (ulong) nr));
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1186
    DBUG_VOID_RETURN;
1187
  }
monty@mysql.com's avatar
monty@mysql.com committed
1188
  table->auto_increment_field_not_null= FALSE;
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
  if (!(nr= thd->next_insert_id))
  {
    nr= get_auto_increment();
    if (variables->auto_increment_increment != 1)
      nr= next_insert_id(nr-1, variables);
    /*
      Update next row based on the found value. This way we don't have to
      call the handler for every generated auto-increment value on a
      multi-row statement
    */
    thd->next_insert_id= nr;
  }

  DBUG_PRINT("info",("auto_increment: %lu", (ulong) nr));

  /* Mark that we should clear next_insert_id before next stmt */
  thd->clear_next_insert_id= 1;

  if (!table->next_number_field->store((longlong) nr))
1208
    thd->insert_id((ulonglong) nr);
1209 1210
  else
    thd->insert_id(table->next_number_field->val_int());
1211 1212 1213 1214 1215 1216

  /*
    We can't set next_insert_id if the auto-increment key is not the
    first key part, as there is no guarantee that the first parts will be in
    sequence
  */
1217
  if (!table->s->next_number_key_offset)
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
  {
    /*
      Set next insert id to point to next auto-increment value to be able to
      handle multi-row statements
      This works even if auto_increment_increment > 1
    */
    thd->next_insert_id= next_insert_id(nr, variables);
  }
  else
    thd->next_insert_id= 0;

  /* Mark that we generated a new value */
1230
  auto_increment_column_changed=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1231 1232 1233
  DBUG_VOID_RETURN;
}

1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
/*
  restore_auto_increment

  In case of error on write, we restore the last used next_insert_id value
  because the previous value was not used.
*/

void handler::restore_auto_increment()
{
  THD *thd= table->in_use;
  if (thd->next_insert_id)
    thd->next_insert_id= thd->prev_insert_id;
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1248

1249
ulonglong handler::get_auto_increment()
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1250
{
1251
  ulonglong nr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1252
  int error;
1253

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1254
  (void) extra(HA_EXTRA_KEYREAD);
1255 1256
  index_init(table->s->next_number_index);
  if (!table->s->next_number_key_offset)
1257 1258 1259 1260 1261 1262
  {						// Autoincrement at key-start
    error=index_last(table->record[1]);
  }
  else
  {
    byte key[MAX_KEY_LENGTH];
1263
    key_copy(key, table->record[0],
1264 1265 1266 1267
             table->key_info + table->s->next_number_index,
             table->s->next_number_key_offset);
    error= index_read(table->record[1], key, table->s->next_number_key_offset,
                      HA_READ_PREFIX_LAST);
1268 1269
  }

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1270 1271 1272
  if (error)
    nr=1;
  else
1273 1274
    nr= ((ulonglong) table->next_number_field->
         val_int_offset(table->s->rec_buff_length)+1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1275
  index_end();
1276
  (void) extra(HA_EXTRA_NO_KEYREAD);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1277 1278 1279 1280 1281 1282 1283
  return nr;
}

	/* Print error that we got from handler function */

void handler::print_error(int error, myf errflag)
{
1284
  DBUG_ENTER("handler::print_error");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1285 1286 1287 1288
  DBUG_PRINT("enter",("error: %d",error));

  int textno=ER_GET_ERRNO;
  switch (error) {
1289 1290 1291
  case EACCES:
    textno=ER_OPEN_AS_READONLY;
    break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
  case EAGAIN:
    textno=ER_FILE_USED;
    break;
  case ENOENT:
    textno=ER_FILE_NOT_FOUND;
    break;
  case HA_ERR_KEY_NOT_FOUND:
  case HA_ERR_NO_ACTIVE_RECORD:
  case HA_ERR_END_OF_FILE:
    textno=ER_KEY_NOT_FOUND;
    break;
1303
  case HA_ERR_WRONG_MRG_TABLE_DEF:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1304 1305 1306 1307 1308 1309 1310 1311 1312
    textno=ER_WRONG_MRG_TABLE;
    break;
  case HA_ERR_FOUND_DUPP_KEY:
  {
    uint key_nr=get_dup_key(error);
    if ((int) key_nr >= 0)
    {
      /* Write the dupplicated key in the error message */
      char key[MAX_KEY_LENGTH];
1313
      String str(key,sizeof(key),system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1314
      key_unpack(&str,table,(uint) key_nr);
1315
      uint max_length=MYSQL_ERRMSG_SIZE-(uint) strlen(ER(ER_DUP_ENTRY));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1316 1317 1318 1319 1320
      if (str.length() >= max_length)
      {
	str.length(max_length-4);
	str.append("...");
      }
1321
      my_error(ER_DUP_ENTRY, MYF(0), str.c_ptr(), key_nr+1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1322 1323 1324 1325 1326
      DBUG_VOID_RETURN;
    }
    textno=ER_DUP_KEY;
    break;
  }
1327 1328 1329
  case HA_ERR_NULL_IN_SPATIAL:
    textno= ER_UNKNOWN_ERROR;
    DBUG_VOID_RETURN;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1330 1331 1332 1333 1334 1335 1336 1337 1338
  case HA_ERR_FOUND_DUPP_UNIQUE:
    textno=ER_DUP_UNIQUE;
    break;
  case HA_ERR_RECORD_CHANGED:
    textno=ER_CHECKREAD;
    break;
  case HA_ERR_CRASHED:
    textno=ER_NOT_KEYFILE;
    break;
1339 1340 1341
  case HA_ERR_WRONG_IN_RECORD:
    textno= ER_CRASHED_ON_USAGE;
    break;
1342 1343 1344
  case HA_ERR_CRASHED_ON_USAGE:
    textno=ER_CRASHED_ON_USAGE;
    break;
1345 1346 1347
  case HA_ERR_NOT_A_TABLE:
    textno= error;
    break;
1348 1349 1350
  case HA_ERR_CRASHED_ON_REPAIR:
    textno=ER_CRASHED_ON_REPAIR;
    break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1351
  case HA_ERR_OUT_OF_MEM:
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1352
    my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), errflag);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
    DBUG_VOID_RETURN;
  case HA_ERR_WRONG_COMMAND:
    textno=ER_ILLEGAL_HA;
    break;
  case HA_ERR_OLD_FILE:
    textno=ER_OLD_KEYFILE;
    break;
  case HA_ERR_UNSUPPORTED:
    textno=ER_UNSUPPORTED_EXTENSION;
    break;
  case HA_ERR_RECORD_FILE_FULL:
    textno=ER_RECORD_FILE_FULL;
    break;
1366 1367 1368
  case HA_ERR_INDEX_FILE_FULL:
    textno= errno;
    break;
1369 1370 1371 1372 1373 1374
  case HA_ERR_LOCK_WAIT_TIMEOUT:
    textno=ER_LOCK_WAIT_TIMEOUT;
    break;
  case HA_ERR_LOCK_TABLE_FULL:
    textno=ER_LOCK_TABLE_FULL;
    break;
1375 1376 1377
  case HA_ERR_LOCK_DEADLOCK:
    textno=ER_LOCK_DEADLOCK;
    break;
1378 1379 1380
  case HA_ERR_READ_ONLY_TRANSACTION:
    textno=ER_READ_ONLY_TRANSACTION;
    break;
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1381 1382 1383 1384 1385 1386 1387 1388 1389
  case HA_ERR_CANNOT_ADD_FOREIGN:
    textno=ER_CANNOT_ADD_FOREIGN;
    break;
  case HA_ERR_ROW_IS_REFERENCED:
    textno=ER_ROW_IS_REFERENCED;
    break;
  case HA_ERR_NO_REFERENCED_ROW:
    textno=ER_NO_REFERENCED_ROW;
    break;
1390 1391
  case HA_ERR_NO_SUCH_TABLE:
  {
1392 1393 1394 1395 1396
    /*
      We have to use path to find database name instead of using
      table->table_cache_key because if the table didn't exist, then
      table_cache_key was not set up
    */
1397 1398
    char *db;
    char buff[FN_REFLEN];
1399
    uint length= dirname_part(buff,table->s->path);
1400 1401
    buff[length-1]=0;
    db=buff+dirname_length(buff);
1402
    my_error(ER_NO_SUCH_TABLE, MYF(0), db, table->alias);
1403 1404
    break;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1405 1406
  default:
    {
1407 1408 1409
      /* The error was "unknown" to this function.
	 Ask handler if it has got a message for this error */
      bool temporary= FALSE;
1410 1411 1412
      String str;
      temporary= get_error_message(error, &str);
      if (!str.is_empty())
1413
      {
1414
	const char* engine= table_type();
1415
	if (temporary)
1416
	  my_error(ER_GET_TEMPORARY_ERRMSG, MYF(0), error, str.ptr(), engine);
1417
	else
1418
	  my_error(ER_GET_ERRMSG, MYF(0), error, str.ptr(), engine);
1419 1420 1421
      }
      else       
	my_error(ER_GET_ERRNO,errflag,error);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1422 1423 1424
      DBUG_VOID_RETURN;
    }
  }
1425
  my_error(textno, errflag, table->alias, error);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1426 1427 1428
  DBUG_VOID_RETURN;
}

1429

1430 1431 1432 1433
/* 
   Return an error message specific to this handler
   
   SYNOPSIS
1434 1435
   error        error code previously returned by handler
   buf          Pointer to String where to add error message
1436
   
1437
   Returns true if this is a temporary error
1438 1439
 */

1440
bool handler::get_error_message(int error, String* buf)
1441
{
1442
  return FALSE;
1443 1444 1445
}


1446
/* Return key if error because of duplicated keys */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1447 1448 1449

uint handler::get_dup_key(int error)
{
1450
  DBUG_ENTER("handler::get_dup_key");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1451
  table->file->errkey  = (uint) -1;
1452 1453
  if (error == HA_ERR_FOUND_DUPP_KEY || error == HA_ERR_FOUND_DUPP_UNIQUE ||
      error == HA_ERR_NULL_IN_SPATIAL)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1454 1455 1456 1457
    info(HA_STATUS_ERRKEY | HA_STATUS_NO_LOCK);
  DBUG_RETURN(table->file->errkey);
}

1458

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1459 1460
int handler::delete_table(const char *name)
{
1461
  int error=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1462 1463 1464
  for (const char **ext=bas_ext(); *ext ; ext++)
  {
    if (delete_file(name,*ext,2))
1465 1466 1467 1468
    {
      if ((error=errno) != ENOENT)
	break;
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1469
  }
1470
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
}


int handler::rename_table(const char * from, const char * to)
{
  DBUG_ENTER("handler::rename_table");
  for (const char **ext=bas_ext(); *ext ; ext++)
  {
    if (rename_file_ext(from,to,*ext))
      DBUG_RETURN(my_errno);
  }
  DBUG_RETURN(0);
}

1485
/*
1486
  Tell the handler to turn on or off transaction in the handler
1487
*/
1488

1489
int ha_enable_transaction(THD *thd, bool on)
1490 1491 1492
{
  int error=0;

1493 1494
  DBUG_ENTER("ha_enable_transaction");
  thd->transaction.on= on;
1495 1496 1497
  DBUG_RETURN(error);
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1498 1499 1500 1501 1502
int handler::index_next_same(byte *buf, const byte *key, uint keylen)
{
  int error;
  if (!(error=index_next(buf)))
  {
1503
    if (key_cmp_if_same(table, key, active_index, keylen))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
    {
      table->status=STATUS_NOT_FOUND;
      error=HA_ERR_END_OF_FILE;
    }
  }
  return error;
}


/****************************************************************************
** Some general functions that isn't in the handler class
****************************************************************************/

1517 1518 1519 1520
/*
  Initiates table-file and calls apropriate database-creator
  Returns 1 if something got wrong
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1521 1522 1523 1524 1525 1526

int ha_create_table(const char *name, HA_CREATE_INFO *create_info,
		    bool update_create_info)
{
  int error;
  TABLE table;
1527
  char name_buff[FN_REFLEN];
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1528 1529
  DBUG_ENTER("ha_create_table");

1530
  if (openfrm(current_thd, name,"",0,(uint) READ_ALL, 0, &table))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1531 1532 1533 1534 1535
    DBUG_RETURN(1);
  if (update_create_info)
  {
    update_create_info_from_table(create_info, &table);
  }
1536 1537 1538 1539 1540
  if (lower_case_table_names == 2 &&
      !(table.file->table_flags() & HA_FILE_BASED))
  {
    /* Ensure that handler gets name in lower case */
    strmov(name_buff, name);
1541
    my_casedn_str(files_charset_info, name_buff);
1542 1543 1544
    name= name_buff;
  }

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1545 1546
  error=table.file->create(name,&table,create_info);
  VOID(closefrm(&table));
1547
  if (error)
1548
    my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), name,error);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1549 1550 1551
  DBUG_RETURN(error != 0);
}

1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
/*
  Try to discover table from engine and 
  if found, write the frm file to disk.
  
  RETURN VALUES:
   0 : Table existed in engine and created 
       on disk if so requested
   1 : Table does not exist
  >1 : error

*/

int ha_create_table_from_engine(THD* thd, 
				const char *db, 
				const char *name,
				bool create_if_found)
{
1569 1570 1571
  int error;
  const void *frmblob;
  uint frmlen;
1572 1573 1574 1575
  char path[FN_REFLEN];
  HA_CREATE_INFO create_info;
  TABLE table;
  DBUG_ENTER("ha_create_table_from_engine");
1576 1577
  DBUG_PRINT("enter", ("name '%s'.'%s'  create_if_found: %d",
                       db, name, create_if_found));
1578 1579 1580 1581 1582

  bzero((char*) &create_info,sizeof(create_info));

  if ((error= ha_discover(thd, db, name, &frmblob, &frmlen)))
    DBUG_RETURN(error); 
1583 1584 1585 1586
  /*
    Table exists in handler
    frmblob and frmlen are set
  */
1587

1588
  if (create_if_found)
1589 1590 1591 1592 1593 1594
  {
    (void)strxnmov(path,FN_REFLEN,mysql_data_home,"/",db,"/",name,NullS);
    // Save the frm file    
    if ((error = writefrm(path, frmblob, frmlen)))
      goto err_end;

1595
    if (openfrm(thd, path,"",0,(uint) READ_ALL, 0, &table))
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
      DBUG_RETURN(1);

    update_create_info_from_table(&create_info, &table);
    create_info.table_options|= HA_CREATE_FROM_ENGINE;

    if (lower_case_table_names == 2 &&
	!(table.file->table_flags() & HA_FILE_BASED))
    {
      /* Ensure that handler gets name in lower case */
      my_casedn_str(files_charset_info, path);
    }
    
    error=table.file->create(path,&table,&create_info);
    VOID(closefrm(&table));
  }

err_end:
1613
  my_free((char*) frmblob, MYF(MY_ALLOW_ZERO_PTR));
1614 1615 1616
  DBUG_RETURN(error);  
}

1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
static int NEAR_F delete_file(const char *name,const char *ext,int extflag)
{
  char buff[FN_REFLEN];
  VOID(fn_format(buff,name,"",ext,extflag | 4));
  return(my_delete_with_symlink(buff,MYF(MY_WME)));
}

void st_ha_check_opt::init()
{
  flags= sql_flags= 0;
  sort_buffer_size = current_thd->variables.myisam_sort_buff_size;
}


/*****************************************************************************
  Key cache handling.

  This code is only relevant for ISAM/MyISAM tables

  key_cache->cache may be 0 only in the case where a key cache is not
  initialized or when we where not able to init the key cache in a previous
  call to ha_init_key_cache() (probably out of memory)
*****************************************************************************/

/* Init a key cache if it has not been initied before */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1642

1643

1644
int ha_init_key_cache(const char *name, KEY_CACHE *key_cache)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1645
{
1646 1647
  DBUG_ENTER("ha_init_key_cache");

1648
  if (!key_cache->key_cache_inited)
1649 1650
  {
    pthread_mutex_lock(&LOCK_global_system_variables);
1651 1652 1653 1654
    long tmp_buff_size= (long) key_cache->param_buff_size;
    long tmp_block_size= (long) key_cache->param_block_size;
    uint division_limit= key_cache->param_division_limit;
    uint age_threshold=  key_cache->param_age_threshold;
1655
    pthread_mutex_unlock(&LOCK_global_system_variables);
1656
    DBUG_RETURN(!init_key_cache(key_cache,
1657 1658
				tmp_block_size,
				tmp_buff_size,
1659
				division_limit, age_threshold));
1660
  }
1661
  DBUG_RETURN(0);
1662 1663
}

1664 1665 1666

/* Resize key cache */

1667
int ha_resize_key_cache(KEY_CACHE *key_cache)
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1668
{
1669 1670
  DBUG_ENTER("ha_resize_key_cache");

1671
  if (key_cache->key_cache_inited)
1672 1673
  {
    pthread_mutex_lock(&LOCK_global_system_variables);
1674 1675 1676 1677
    long tmp_buff_size= (long) key_cache->param_buff_size;
    long tmp_block_size= (long) key_cache->param_block_size;
    uint division_limit= key_cache->param_division_limit;
    uint age_threshold=  key_cache->param_age_threshold;
1678
    pthread_mutex_unlock(&LOCK_global_system_variables);
1679 1680 1681
    DBUG_RETURN(!resize_key_cache(key_cache, tmp_block_size,
				  tmp_buff_size,
				  division_limit, age_threshold));
1682
  }
1683
  DBUG_RETURN(0);
1684 1685
}

1686 1687 1688

/* Change parameters for key cache (like size) */

1689
int ha_change_key_cache_param(KEY_CACHE *key_cache)
1690
{
1691 1692 1693 1694 1695 1696 1697 1698
  if (key_cache->key_cache_inited)
  {
    pthread_mutex_lock(&LOCK_global_system_variables);
    uint division_limit= key_cache->param_division_limit;
    uint age_threshold=  key_cache->param_age_threshold;
    pthread_mutex_unlock(&LOCK_global_system_variables);
    change_key_cache_param(key_cache, division_limit, age_threshold);
  }
1699 1700
  return 0;
}
1701

1702 1703
/* Free memory allocated by a key cache */

1704
int ha_end_key_cache(KEY_CACHE *key_cache)
1705
{
1706
  end_key_cache(key_cache, 1);		// Can never fail
1707
  return 0;
1708
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1709

1710
/* Move all tables from one key cache to another one */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1711

1712 1713
int ha_change_key_cache(KEY_CACHE *old_key_cache,
			KEY_CACHE *new_key_cache)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1714
{
1715 1716
  mi_change_key_cache(old_key_cache, new_key_cache);
  return 0;
1717
}
1718 1719


1720 1721
/*
  Try to discover one table from handler(s)
1722 1723 1724 1725

  RETURN
    0  ok. In this case *frmblob and *frmlen are set
    1  error.  frmblob and frmlen may not be set
1726 1727
*/

1728 1729
int ha_discover(THD *thd, const char *db, const char *name,
		const void **frmblob, uint *frmlen)
1730 1731 1732
{
  int error= 1; // Table does not exist in any handler
  DBUG_ENTER("ha_discover");
1733
  DBUG_PRINT("enter", ("db: %s, name: %s", db, name));
1734 1735
#ifdef HAVE_NDBCLUSTER_DB
  if (have_ndbcluster == SHOW_OPTION_YES)
1736
    error= ndbcluster_discover(thd, db, name, frmblob, frmlen);
1737 1738 1739 1740 1741 1742 1743
#endif
  if (!error)
    statistic_increment(ha_discover_count,&LOCK_status);
  DBUG_RETURN(error);
}


1744
/*
1745 1746 1747
  Call this function in order to give the handler the possiblity 
  to ask engine if there are any new tables that should be written to disk 
  or any dropped tables that need to be removed from disk
1748 1749
*/

1750 1751
int
ha_find_files(THD *thd,const char *db,const char *path,
1752
	      const char *wild, bool dir, List<char> *files)
1753 1754
{
  int error= 0;
1755 1756 1757
  DBUG_ENTER("ha_find_files");
  DBUG_PRINT("enter", ("db: %s, path: %s, wild: %s, dir: %d", 
		       db, path, wild, dir));
1758 1759
#ifdef HAVE_NDBCLUSTER_DB
  if (have_ndbcluster == SHOW_OPTION_YES)
1760
    error= ndbcluster_find_files(thd, db, path, wild, dir, files);
1761 1762
#endif
  DBUG_RETURN(error);
1763 1764
  
  
1765 1766
}

1767 1768
#ifdef NOT_YET_USED

1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
/*
  Ask handler if the table exists in engine

  RETURN
    0                   Table does not exist
    1                   Table exists
    #                   Error code

 */
int ha_table_exists(THD* thd, const char* db, const char* name)
{
  int error= 2;
  DBUG_ENTER("ha_table_exists");
  DBUG_PRINT("enter", ("db: %s, name: %s", db, name));
#ifdef HAVE_NDBCLUSTER_DB
  if (have_ndbcluster == SHOW_OPTION_YES)
    error= ndbcluster_table_exists(thd, db, name);
#endif
  DBUG_RETURN(error);
}

1790
#endif
1791 1792


ingo@mysql.com's avatar
ingo@mysql.com committed
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
/*
  Read the first row of a multi-range set.

  SYNOPSIS
    read_multi_range_first()
    found_range_p       Returns a pointer to the element in 'ranges' that
                        corresponds to the returned row.
    ranges              An array of KEY_MULTI_RANGE range descriptions.
    range_count         Number of ranges in 'ranges'.
    sorted		If result should be sorted per key.
    buffer              A HANDLER_BUFFER for internal handler usage.

  NOTES
    Record is read into table->record[0].
    *found_range_p returns a valid value only if read_multi_range_first()
    returns 0.
    Sorting is done within each range. If you want an overall sort, enter
    'ranges' with sorted ranges.

  RETURN
    0			OK, found a row
    HA_ERR_END_OF_FILE	No rows in range
    #			Error code
*/

int handler::read_multi_range_first(KEY_MULTI_RANGE **found_range_p,
                                    KEY_MULTI_RANGE *ranges, uint range_count,
                                    bool sorted, HANDLER_BUFFER *buffer)
{
  int result= HA_ERR_END_OF_FILE;
  DBUG_ENTER("handler::read_multi_range_first");
  multi_range_sorted= sorted;
  multi_range_buffer= buffer;

  for (multi_range_curr= ranges, multi_range_end= ranges + range_count;
       multi_range_curr < multi_range_end;
       multi_range_curr++)
  {
    result= read_range_first(multi_range_curr->start_key.length ?
                             &multi_range_curr->start_key : 0,
                             multi_range_curr->end_key.length ?
                             &multi_range_curr->end_key : 0,
                             test(multi_range_curr->range_flag & EQ_RANGE),
                             multi_range_sorted);
    if (result != HA_ERR_END_OF_FILE)
      break;
  }

  *found_range_p= multi_range_curr;
  DBUG_PRINT("exit",("result %d", result));
  DBUG_RETURN(result);
}


/*
  Read the next row of a multi-range set.

  SYNOPSIS
    read_multi_range_next()
    found_range_p       Returns a pointer to the element in 'ranges' that
                        corresponds to the returned row.

  NOTES
    Record is read into table->record[0].
    *found_range_p returns a valid value only if read_multi_range_next()
    returns 0.

  RETURN
    0			OK, found a row
    HA_ERR_END_OF_FILE	No (more) rows in range
    #			Error code
*/

int handler::read_multi_range_next(KEY_MULTI_RANGE **found_range_p)
{
  int result;
  DBUG_ENTER("handler::read_multi_range_next");

  /* We should not be called after the last call returned EOF. */
  DBUG_ASSERT(multi_range_curr < multi_range_end);

  do
  {
    /* Save a call if there can be only one row in range. */
    if (multi_range_curr->range_flag != (UNIQUE_RANGE | EQ_RANGE))
    {
      result= read_range_next();

      /* On success or non-EOF errors jump to the end. */
      if (result != HA_ERR_END_OF_FILE)
        break;
    }
    else
    {
      /*
        We need to set this for the last range only, but checking this
        condition is more expensive than just setting the result code.
      */
      result= HA_ERR_END_OF_FILE;
    }

    /* Try the next range(s) until one matches a record. */
    for (multi_range_curr++;
         multi_range_curr < multi_range_end;
         multi_range_curr++)
    {
      result= read_range_first(multi_range_curr->start_key.length ?
                               &multi_range_curr->start_key : 0,
                               multi_range_curr->end_key.length ?
                               &multi_range_curr->end_key : 0,
                               test(multi_range_curr->range_flag & EQ_RANGE),
                               multi_range_sorted);
      if (result != HA_ERR_END_OF_FILE)
        break;
    }
  }
  while ((result == HA_ERR_END_OF_FILE) &&
         (multi_range_curr < multi_range_end));

  *found_range_p= multi_range_curr;
  DBUG_PRINT("exit",("handler::read_multi_range_next: result %d", result));
  DBUG_RETURN(result);
}


1918 1919 1920 1921 1922 1923 1924 1925
/*
  Read first row between two ranges.
  Store ranges for future calls to read_range_next

  SYNOPSIS
    read_range_first()
    start_key		Start key. Is 0 if no min range
    end_key		End key.  Is 0 if no max range
1926
    eq_range_arg	Set to 1 if start_key == end_key		
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939
    sorted		Set to 1 if result should be sorted per key

  NOTES
    Record is read into table->record[0]

  RETURN
    0			Found row
    HA_ERR_END_OF_FILE	No rows in range
    #			Error code
*/

int handler::read_range_first(const key_range *start_key,
			      const key_range *end_key,
1940
			      bool eq_range_arg, bool sorted)
1941 1942 1943 1944
{
  int result;
  DBUG_ENTER("handler::read_range_first");

1945
  eq_range= eq_range_arg;
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963
  end_range= 0;
  if (end_key)
  {
    end_range= &save_end_range;
    save_end_range= *end_key;
    key_compare_result_on_equal= ((end_key->flag == HA_READ_BEFORE_KEY) ? 1 :
				  (end_key->flag == HA_READ_AFTER_KEY) ? -1 : 0);
  }
  range_key_part= table->key_info[active_index].key_part;

  if (!start_key)			// Read first record
    result= index_first(table->record[0]);
  else
    result= index_read(table->record[0],
		       start_key->key,
		       start_key->length,
		       start_key->flag);
  if (result)
1964 1965 1966
    DBUG_RETURN((result == HA_ERR_KEY_NOT_FOUND) 
		? HA_ERR_END_OF_FILE
		: result);
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986

  DBUG_RETURN (compare_key(end_range) <= 0 ? 0 : HA_ERR_END_OF_FILE);
}


/*
  Read next row between two ranges.

  SYNOPSIS
    read_range_next()

  NOTES
    Record is read into table->record[0]

  RETURN
    0			Found row
    HA_ERR_END_OF_FILE	No rows in range
    #			Error code
*/

1987
int handler::read_range_next()
1988 1989 1990 1991 1992
{
  int result;
  DBUG_ENTER("handler::read_range_next");

  if (eq_range)
1993 1994 1995 1996 1997 1998 1999
  {
    /* We trust that index_next_same always gives a row in range */
    DBUG_RETURN(index_next_same(table->record[0],
                                end_range->key,
                                end_range->length));
  }
  result= index_next(table->record[0]);
2000 2001 2002 2003 2004 2005 2006
  if (result)
    DBUG_RETURN(result);
  DBUG_RETURN(compare_key(end_range) <= 0 ? 0 : HA_ERR_END_OF_FILE);
}


/*
2007
  Compare if found key (in row) is over max-value
2008 2009 2010

  SYNOPSIS
    compare_key
2011
    range		range to compare to row. May be 0 for no range
2012 2013
 
  NOTES
2014
    See key.cc::key_cmp() for details
2015 2016

  RETURN
2017 2018
    The return value is SIGN(key_in_row - range_key):

2019 2020 2021 2022 2023 2024 2025
    0			Key is equal to range or 'range' == 0 (no range)
   -1			Key is less than range
    1			Key is larger than range
*/

int handler::compare_key(key_range *range)
{
2026
  int cmp;
2027 2028
  if (!range)
    return 0;					// No max range
2029 2030 2031 2032
  cmp= key_cmp(range_key_part, range->key, range->length);
  if (!cmp)
    cmp= key_compare_result_on_equal;
  return cmp;
2033
}
2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045

int handler::index_read_idx(byte * buf, uint index, const byte * key,
			     uint key_len, enum ha_rkey_function find_flag)
{
  int error= ha_index_init(index);
  if (!error)
    error= index_read(buf, key, key_len, find_flag);
  if (!error)
    error= ha_index_end();
  return error;
}

2046

2047 2048 2049 2050 2051 2052 2053 2054
/*
  Returns a list of all known extensions.

  SYNOPSIS
    ha_known_exts()
 
  NOTES
    No mutexes, worst case race is a minor surplus memory allocation
2055 2056
    We have to recreate the extension map if mysqld is restarted (for example
    within libmysqld)
2057 2058 2059 2060

  RETURN VALUE
    pointer		pointer to TYPELIB structure
*/
2061

2062 2063
TYPELIB *ha_known_exts(void)
{
2064
  if (!known_extensions.type_names || mysys_usage_id != known_extensions_id)
2065 2066 2067 2068
  {
    show_table_type_st *types;
    List<char> found_exts;
    List_iterator_fast<char> it(found_exts);
2069 2070 2071 2072
    const char **ext, *old_ext;

    known_extensions_id= mysys_usage_id;
    found_exts.push_back((char*) ".db");
2073 2074 2075 2076 2077 2078 2079
    for (types= sys_table_types; types->type; types++)
    {      
      if (*types->value == SHOW_OPTION_YES)
      {
	handler *file= get_new_handler(0,(enum db_type) types->db_type);
	for (ext= file->bas_ext(); *ext; ext++)
	{
2080 2081 2082
	  while ((old_ext= it++))
          {
	    if (!strcmp(old_ext, *ext))
2083
	      break;
2084 2085 2086
          }
	  if (!old_ext)
	    found_exts.push_back((char *) *ext);
2087 2088 2089 2090 2091 2092

	  it.rewind();
	}
	delete file;
      }
    }
2093 2094 2095
    ext= (const char **) my_once_alloc(sizeof(char *)*
                                       (found_exts.elements+1),
                                       MYF(MY_WME | MY_FAE));
2096 2097 2098 2099
    
    DBUG_ASSERT(ext);
    known_extensions.count= found_exts.elements;
    known_extensions.type_names= ext;
2100 2101 2102 2103

    while ((old_ext= it++))
      *ext++= old_ext;
    *ext= 0;
2104 2105 2106
  }
  return &known_extensions;
}