ha_maria.cc 90.9 KB
Newer Older
1 2 3 4
/* Copyright (C) 2006,2004 MySQL AB & MySQL Finland AB & TCX DataKonsult AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


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

21
#define MYSQL_SERVER 1
22
#include "mysql_priv.h"
23
#include <mysql/plugin.h>
24
#include <m_ctype.h>
25
#include <my_dir.h>
26
#include <myisampack.h>
unknown's avatar
unknown committed
27
#include <my_bit.h>
28
#include "ha_maria.h"
29
#include "trnman_public.h"
30

31
C_MODE_START
32 33
#include "maria_def.h"
#include "ma_rt_index.h"
34
#include "ma_blockrec.h"
unknown's avatar
unknown committed
35 36
#include "ma_checkpoint.h"
#include "ma_recovery.h"
37
C_MODE_END
38

unknown's avatar
unknown committed
39 40 41 42
/*
  Note that in future versions, only *transactional* Maria tables can
  rollback, so this flag should be up or down conditionally.
*/
unknown's avatar
unknown committed
43 44 45 46 47
#define MARIA_CANNOT_ROLLBACK HA_NO_TRANSACTIONS
#ifdef MARIA_CANNOT_ROLLBACK
#define trans_register_ha(A, B, C)  do { /* nothing */ } while(0)
#endif

unknown's avatar
unknown committed
48 49 50
ulong pagecache_division_limit, pagecache_age_threshold;
ulonglong pagecache_buffer_size;

unknown's avatar
unknown committed
51 52 53 54 55 56 57 58 59
/**
   @todo For now there is no way for a user to set a different value of
   maria_recover_options, i.e. auto-check-and-repair is always disabled.
   We could enable it. As the auto-repair is initiated when opened from the
   SQL layer (open_unireg_entry(), check_and_repair()), it does not happen
   when Maria's Recovery internally opens the table to apply log records to
   it, which is good. It would happen only after Recovery, if the table is
   still corrupted.
*/
60
ulong maria_recover_options= HA_RECOVER_NONE;
unknown's avatar
unknown committed
61
handlerton *maria_hton;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

/* bits in maria_recover_options */
const char *maria_recover_names[]=
{
  "DEFAULT", "BACKUP", "FORCE", "QUICK", NullS
};
TYPELIB maria_recover_typelib=
{
  array_elements(maria_recover_names) - 1, "",
  maria_recover_names, NULL
};

const char *maria_stats_method_names[]=
{
  "nulls_unequal", "nulls_equal",
  "nulls_ignored", NullS
};
TYPELIB maria_stats_method_typelib=
{
  array_elements(maria_stats_method_names) - 1, "",
  maria_stats_method_names, NULL
};

85 86 87 88 89 90 91 92 93 94
/* transactions log purge mode */
const char *maria_translog_purge_type_names[]=
{
  "immediate", "external", "at_flush", NullS
};
TYPELIB maria_translog_purge_type_typelib=
{
  array_elements(maria_translog_purge_type_names) - 1, "",
  maria_translog_purge_type_names, NULL
};
95 96 97 98 99 100 101 102 103 104 105
const char *maria_sync_log_dir_names[]=
{
  "NEVER", "NEWFILE", "ALWAYS", NullS
};

TYPELIB maria_sync_log_dir_typelib=
{
  array_elements(maria_sync_log_dir_names) - 1, "",
  maria_sync_log_dir_names, NULL
};

unknown's avatar
unknown committed
106 107 108 109 110
/** @brief Interval between background checkpoints in seconds */
static ulong checkpoint_interval;
static void update_checkpoint_interval(MYSQL_THD thd,
                                       struct st_mysql_sys_var *var,
                                       void *var_ptr, void *save);
111 112 113
static void update_log_file_size(MYSQL_THD thd,
                                 struct st_mysql_sys_var *var,
                                 void *var_ptr, void *save);
unknown's avatar
unknown committed
114

115 116 117 118 119 120
static MYSQL_SYSVAR_ULONG(block_size, maria_block_size,
       PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
       "Block size to be used for MARIA index pages.", 0, 0,
       MARIA_KEY_BLOCK_LENGTH, MARIA_MIN_KEY_BLOCK_LENGTH,
       MARIA_MAX_KEY_BLOCK_LENGTH, MARIA_MIN_KEY_BLOCK_LENGTH);

unknown's avatar
unknown committed
121
static MYSQL_SYSVAR_ULONG(checkpoint_interval, checkpoint_interval,
unknown's avatar
unknown committed
122
       PLUGIN_VAR_RQCMDARG,
unknown's avatar
unknown committed
123 124
       "Interval between automatic checkpoints, in seconds; 0 means"
       " 'no automatic checkpoints' which makes sense only for testing.",
unknown's avatar
unknown committed
125
       NULL, update_checkpoint_interval, 30, 0, UINT_MAX, 1);
unknown's avatar
unknown committed
126

unknown's avatar
unknown committed
127 128 129 130
static MYSQL_SYSVAR_BOOL(page_checksum, maria_page_checksums, 0,
       "Maintain page checksums (can be overridden per table "
       "with PAGE_CHECKSUM clause in CREATE TABLE)", 0, 0, 1);

131 132 133 134 135 136 137 138 139 140 141 142 143 144
static MYSQL_SYSVAR_ULONG(log_file_size, log_file_size,
       PLUGIN_VAR_RQCMDARG,
       "Limit for transaction log size",
       NULL, update_log_file_size, TRANSLOG_FILE_SIZE,
       TRANSLOG_MIN_FILE_SIZE, 0xffffffffL, TRANSLOG_PAGE_SIZE);

static MYSQL_SYSVAR_ENUM(log_purge_type, log_purge_type,
       PLUGIN_VAR_RQCMDARG,
       "Specifies how maria transactional log will be purged. "
       "Possible values of name are \"immediate\", \"external\" "
       "and \"at_flush\"",
       NULL, NULL, TRANSLOG_PURGE_IMMIDIATE,
       &maria_translog_purge_type_typelib);

145 146 147 148 149 150
static MYSQL_SYSVAR_ULONGLONG(max_sort_file_size,
       maria_max_temp_length, PLUGIN_VAR_RQCMDARG,
       "Don't use the fast sort index method to created index if the "
       "temporary file would get bigger than this.",
       0, 0, MAX_FILE_SIZE, 0, MAX_FILE_SIZE, 1024*1024);

unknown's avatar
unknown committed
151 152 153 154 155 156 157 158 159 160 161
static MYSQL_SYSVAR_ULONG(pagecache_age_threshold,
       pagecache_age_threshold, PLUGIN_VAR_RQCMDARG,
       "This characterizes the number of hits a hot block has to be untouched "
       "until it is considered aged enough to be downgraded to a warm block. "
       "This specifies the percentage ratio of that number of hits to the "
       "total number of blocks in the page cache.", 0, 0,
        300, 100, ~0L, 100);

static MYSQL_SYSVAR_ULONGLONG(pagecache_buffer_size, pagecache_buffer_size,
       PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
       "The size of the buffer used for index blocks for Maria tables. "
unknown's avatar
unknown committed
162 163
       "Increase this to get better index handling (for all reads and "
       "multiple writes) to as much as you can afford.", 0, 0,
unknown's avatar
unknown committed
164 165 166 167 168 169 170
       KEY_CACHE_SIZE, MALLOC_OVERHEAD, ~(ulong) 0, IO_SIZE);

static MYSQL_SYSVAR_ULONG(pagecache_division_limit, pagecache_division_limit,
       PLUGIN_VAR_RQCMDARG,
       "The minimum percentage of warm blocks in key cache", 0, 0,
       100,  1, 100, 1);

171 172 173 174 175 176 177 178 179 180 181 182
static MYSQL_THDVAR_ULONG(repair_threads, PLUGIN_VAR_RQCMDARG,
       "Number of threads to use when repairing maria tables. The value of 1 "
       "disables parallel repair.",
       0, 0, 1, 1, ~0L, 1);

static MYSQL_THDVAR_ULONG(sort_buffer_size, PLUGIN_VAR_RQCMDARG,
       "The buffer that is allocated when sorting the index when doing a "
       "REPAIR or when creating indexes with CREATE INDEX or ALTER TABLE.",
       0, 0, 8192*1024, 4, ~0L, 1);

static MYSQL_THDVAR_ENUM(stats_method, PLUGIN_VAR_RQCMDARG,
       "Specifies how maria index statistics collection code should threat "
unknown's avatar
unknown committed
183
       "NULLs. Possible values are \"nulls_unequal\", \"nulls_equal\", "
184
       "and \"nulls_ignored\".", 0, 0, 0, &maria_stats_method_typelib);
185

186 187
static MYSQL_SYSVAR_ENUM(sync_log_dir, sync_log_dir, PLUGIN_VAR_RQCMDARG,
       "Controls syncing directory after log file growth and new file "
unknown's avatar
unknown committed
188 189
       "creation. Possible values are \"never\", \"newfile\" and "
       "\"always\").", NULL, NULL, TRANSLOG_SYNC_DIR_NEWFILE,
190 191
       &maria_sync_log_dir_typelib);

192 193 194 195
/*****************************************************************************
** MARIA tables
*****************************************************************************/

196 197 198
static handler *maria_create_handler(handlerton *hton,
                                     TABLE_SHARE * table,
                                     MEM_ROOT *mem_root)
199
{
200
  return new (mem_root) ha_maria(hton, table);
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
}


// collect errors printed by maria_check routines

static void _ma_check_print_msg(HA_CHECK *param, const char *msg_type,
                                const char *fmt, va_list args)
{
  THD *thd= (THD *) param->thd;
  Protocol *protocol= thd->protocol;
  uint length, msg_length;
  char msgbuf[MARIA_MAX_MSG_BUF];
  char name[NAME_LEN * 2 + 2];

  msg_length= my_vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
  msgbuf[sizeof(msgbuf) - 1]= 0;                // healthy paranoia

  DBUG_PRINT(msg_type, ("message: %s", msgbuf));

  if (!thd->vio_ok())
  {
    sql_print_error(msgbuf);
    return;
  }

  if (param->testflag &
      (T_CREATE_MISSING_KEYS | T_SAFE_REPAIR | T_AUTO_REPAIR))
  {
    my_message(ER_NOT_KEYFILE, msgbuf, MYF(MY_WME));
    return;
  }
  length= (uint) (strxmov(name, param->db_name, ".", param->table_name,
                          NullS) - name);
234 235 236 237 238 239 240 241
  /*
    TODO: switch from protocol to push_warning here. The main reason we didn't
    it yet is parallel repair. Due to following trace:
    ma_check_print_msg/push_warning/sql_alloc/my_pthread_getspecific_ptr.

    Also we likely need to lock mutex here (in both cases with protocol and
    push_warning).
  */
242 243 244 245 246 247 248 249 250 251 252
  protocol->prepare_for_resend();
  protocol->store(name, length, system_charset_info);
  protocol->store(param->op_name, system_charset_info);
  protocol->store(msg_type, system_charset_info);
  protocol->store(msgbuf, msg_length, system_charset_info);
  if (protocol->write())
    sql_print_error("Failed on my_net_write, writing to stderr instead: %s\n",
                    msgbuf);
  return;
}

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

/*
  Convert TABLE object to Maria key and column definition

  SYNOPSIS
    table2maria()
      table_arg   in     TABLE object.
      keydef_out  out    Maria key definition.
      recinfo_out out    Maria column definition.
      records_out out    Number of fields.

  DESCRIPTION
    This function will allocate and initialize Maria key and column
    definition for further use in ma_create or for a check for underlying
    table conformance in merge engine.

  RETURN VALUE
    0  OK
271
    # error code
272 273
*/

274 275 276 277
static int table2maria(TABLE *table_arg, data_file_type row_type,
                       MARIA_KEYDEF **keydef_out,
                       MARIA_COLUMNDEF **recinfo_out, uint *records_out,
                       MARIA_CREATE_INFO *create_info)
278 279 280
{
  uint i, j, recpos, minpos, fieldpos, temp_length, length;
  enum ha_base_keytype type= HA_KEYTYPE_BINARY;
unknown's avatar
unknown committed
281
  uchar *record;
282 283 284 285 286 287 288
  KEY *pos;
  MARIA_KEYDEF *keydef;
  MARIA_COLUMNDEF *recinfo, *recinfo_pos;
  HA_KEYSEG *keyseg;
  TABLE_SHARE *share= table_arg->s;
  uint options= share->db_options_in_use;
  DBUG_ENTER("table2maria");
289

290 291 292
  if (row_type == BLOCK_RECORD)
    options|= HA_OPTION_PACK_RECORD;

293 294 295 296 297 298 299 300 301 302 303 304
  if (!(my_multi_malloc(MYF(MY_WME),
          recinfo_out, (share->fields * 2 + 2) * sizeof(MARIA_COLUMNDEF),
          keydef_out, share->keys * sizeof(MARIA_KEYDEF),
          &keyseg,
          (share->key_parts + share->keys) * sizeof(HA_KEYSEG),
          NullS)))
    DBUG_RETURN(HA_ERR_OUT_OF_MEM); /* purecov: inspected */
  keydef= *keydef_out;
  recinfo= *recinfo_out;
  pos= table_arg->key_info;
  for (i= 0; i < share->keys; i++, pos++)
  {
305 306
    keydef[i].flag= (uint16) (pos->flags & (HA_NOSAME | HA_FULLTEXT |
                                            HA_SPATIAL));
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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
    keydef[i].key_alg= pos->algorithm == HA_KEY_ALG_UNDEF ?
      (pos->flags & HA_SPATIAL ? HA_KEY_ALG_RTREE : HA_KEY_ALG_BTREE) :
      pos->algorithm;
    keydef[i].block_length= pos->block_size;
    keydef[i].seg= keyseg;
    keydef[i].keysegs= pos->key_parts;
    for (j= 0; j < pos->key_parts; j++)
    {
      Field *field= pos->key_part[j].field;
      type= field->key_type();
      keydef[i].seg[j].flag= pos->key_part[j].key_part_flag;

      if (options & HA_OPTION_PACK_KEYS ||
          (pos->flags & (HA_PACK_KEY | HA_BINARY_PACK_KEY |
                         HA_SPACE_PACK_USED)))
      {
        if (pos->key_part[j].length > 8 &&
            (type == HA_KEYTYPE_TEXT ||
             type == HA_KEYTYPE_NUM ||
             (type == HA_KEYTYPE_BINARY && !field->zero_pack())))
        {
          /* No blobs here */
          if (j == 0)
            keydef[i].flag|= HA_PACK_KEY;
          if (!(field->flags & ZEROFILL_FLAG) &&
              (field->type() == MYSQL_TYPE_STRING ||
               field->type() == MYSQL_TYPE_VAR_STRING ||
               ((int) (pos->key_part[j].length - field->decimals())) >= 4))
            keydef[i].seg[j].flag|= HA_SPACE_PACK;
        }
        else if (j == 0 && (!(pos->flags & HA_NOSAME) || pos->key_length > 16))
          keydef[i].flag|= HA_BINARY_PACK_KEY;
      }
      keydef[i].seg[j].type= (int) type;
      keydef[i].seg[j].start= pos->key_part[j].offset;
      keydef[i].seg[j].length= pos->key_part[j].length;
      keydef[i].seg[j].bit_start= keydef[i].seg[j].bit_end=
        keydef[i].seg[j].bit_length= 0;
      keydef[i].seg[j].bit_pos= 0;
      keydef[i].seg[j].language= field->charset()->number;

      if (field->null_ptr)
      {
        keydef[i].seg[j].null_bit= field->null_bit;
        keydef[i].seg[j].null_pos= (uint) (field->null_ptr-
                                           (uchar*) table_arg->record[0]);
      }
      else
      {
        keydef[i].seg[j].null_bit= 0;
        keydef[i].seg[j].null_pos= 0;
      }
      if (field->type() == MYSQL_TYPE_BLOB ||
          field->type() == MYSQL_TYPE_GEOMETRY)
      {
        keydef[i].seg[j].flag|= HA_BLOB_PART;
        /* save number of bytes used to pack length */
        keydef[i].seg[j].bit_start= (uint) (field->pack_length() -
                                            share->blob_ptr_size);
      }
      else if (field->type() == MYSQL_TYPE_BIT)
      {
        keydef[i].seg[j].bit_length= ((Field_bit *) field)->bit_len;
        keydef[i].seg[j].bit_start= ((Field_bit *) field)->bit_ofs;
        keydef[i].seg[j].bit_pos= (uint) (((Field_bit *) field)->bit_ptr -
                                          (uchar*) table_arg->record[0]);
      }
    }
    keyseg+= pos->key_parts;
  }
  if (table_arg->found_next_number_field)
    keydef[share->next_number_index].flag|= HA_AUTO_KEY;
  record= table_arg->record[0];
  recpos= 0;
  recinfo_pos= recinfo;
382 383
  create_info->null_bytes= table_arg->s->null_bytes;

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
  while (recpos < (uint) share->reclength)
  {
    Field **field, *found= 0;
    minpos= share->reclength;
    length= 0;

    for (field= table_arg->field; *field; field++)
    {
      if ((fieldpos= (*field)->offset(record)) >= recpos &&
          fieldpos <= minpos)
      {
        /* skip null fields */
        if (!(temp_length= (*field)->pack_length_in_rec()))
          continue; /* Skip null-fields */
        if (! found || fieldpos < minpos ||
            (fieldpos == minpos && temp_length < length))
        {
          minpos= fieldpos;
          found= *field;
          length= temp_length;
        }
      }
    }
    DBUG_PRINT("loop", ("found: 0x%lx  recpos: %d  minpos: %d  length: %d",
                        (long) found, recpos, minpos, length));
    if (!found)
      break;

    if (found->flags & BLOB_FLAG)
      recinfo_pos->type= FIELD_BLOB;
    else if (found->type() == MYSQL_TYPE_VARCHAR)
      recinfo_pos->type= FIELD_VARCHAR;
416 417
    else if (!(options & HA_OPTION_PACK_RECORD) ||
             (found->zero_pack() && (found->flags & PRI_KEY_FLAG)))
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
      recinfo_pos->type= FIELD_NORMAL;
    else if (found->zero_pack())
      recinfo_pos->type= FIELD_SKIP_ZERO;
    else
      recinfo_pos->type= ((length <= 3 ||
                           (found->flags & ZEROFILL_FLAG)) ?
                          FIELD_NORMAL :
                          found->type() == MYSQL_TYPE_STRING ||
                          found->type() == MYSQL_TYPE_VAR_STRING ?
                          FIELD_SKIP_ENDSPACE :
                          FIELD_SKIP_PRESPACE);
    if (found->null_ptr)
    {
      recinfo_pos->null_bit= found->null_bit;
      recinfo_pos->null_pos= (uint) (found->null_ptr -
                                     (uchar*) table_arg->record[0]);
    }
    else
    {
      recinfo_pos->null_bit= 0;
      recinfo_pos->null_pos= 0;
    }
    (recinfo_pos++)->length= (uint16) length;
    recpos= minpos + length;
    DBUG_PRINT("loop", ("length: %d  type: %d",
                        recinfo_pos[-1].length,recinfo_pos[-1].type));
  }
  *records_out= (uint) (recinfo_pos - recinfo);
  DBUG_RETURN(0);
}


/*
  Check for underlying table conformance

  SYNOPSIS
    maria_check_definition()
      t1_keyinfo       in    First table key definition
      t1_recinfo       in    First table record definition
      t1_keys          in    Number of keys in first table
      t1_recs          in    Number of records in first table
      t2_keyinfo       in    Second table key definition
      t2_recinfo       in    Second table record definition
      t2_keys          in    Number of keys in second table
      t2_recs          in    Number of records in second table
      strict           in    Strict check switch

  DESCRIPTION
    This function compares two Maria definitions. By intention it was done
    to compare merge table definition against underlying table definition.
    It may also be used to compare dot-frm and MAI definitions of Maria
    table as well to compare different Maria table definitions.

    For merge table it is not required that number of keys in merge table
    must exactly match number of keys in underlying table. When calling this
    function for underlying table conformance check, 'strict' flag must be
    set to false, and converted merge definition must be passed as t1_*.

    Otherwise 'strict' flag must be set to 1 and it is not required to pass
    converted dot-frm definition as t1_*.

  RETURN VALUE
    0 - Equal definitions.
    1 - Different definitions.

483 484 485 486
  TODO
    - compare FULLTEXT keys;
    - compare SPATIAL keys;
    - compare FIELD_SKIP_ZERO which is converted to FIELD_NORMAL correctly
487
      (should be correctly detected in table2maria).
488
*/
489

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
int maria_check_definition(MARIA_KEYDEF *t1_keyinfo,
                           MARIA_COLUMNDEF *t1_recinfo,
                           uint t1_keys, uint t1_recs,
                           MARIA_KEYDEF *t2_keyinfo,
                           MARIA_COLUMNDEF *t2_recinfo,
                           uint t2_keys, uint t2_recs, bool strict)
{
  uint i, j;
  DBUG_ENTER("maria_check_definition");
  if ((strict ? t1_keys != t2_keys : t1_keys > t2_keys))
  {
    DBUG_PRINT("error", ("Number of keys differs: t1_keys=%u, t2_keys=%u",
                         t1_keys, t2_keys));
    DBUG_RETURN(1);
  }
  if (t1_recs != t2_recs)
  {
    DBUG_PRINT("error", ("Number of recs differs: t1_recs=%u, t2_recs=%u",
                         t1_recs, t2_recs));
    DBUG_RETURN(1);
  }
  for (i= 0; i < t1_keys; i++)
  {
    HA_KEYSEG *t1_keysegs= t1_keyinfo[i].seg;
    HA_KEYSEG *t2_keysegs= t2_keyinfo[i].seg;
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
    if (t1_keyinfo[i].flag & HA_FULLTEXT && t2_keyinfo[i].flag & HA_FULLTEXT)
      continue;
    else if (t1_keyinfo[i].flag & HA_FULLTEXT ||
             t2_keyinfo[i].flag & HA_FULLTEXT)
    {
       DBUG_PRINT("error", ("Key %d has different definition", i));
       DBUG_PRINT("error", ("t1_fulltext= %d, t2_fulltext=%d",
                            test(t1_keyinfo[i].flag & HA_FULLTEXT),
                            test(t2_keyinfo[i].flag & HA_FULLTEXT)));
       DBUG_RETURN(1);
    }
    if (t1_keyinfo[i].flag & HA_SPATIAL && t2_keyinfo[i].flag & HA_SPATIAL)
      continue;
    else if (t1_keyinfo[i].flag & HA_SPATIAL ||
             t2_keyinfo[i].flag & HA_SPATIAL)
    {
       DBUG_PRINT("error", ("Key %d has different definition", i));
       DBUG_PRINT("error", ("t1_spatial= %d, t2_spatial=%d",
                            test(t1_keyinfo[i].flag & HA_SPATIAL),
                            test(t2_keyinfo[i].flag & HA_SPATIAL)));
       DBUG_RETURN(1);
    }
537 538 539 540 541 542 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
    if (t1_keyinfo[i].keysegs != t2_keyinfo[i].keysegs ||
        t1_keyinfo[i].key_alg != t2_keyinfo[i].key_alg)
    {
      DBUG_PRINT("error", ("Key %d has different definition", i));
      DBUG_PRINT("error", ("t1_keysegs=%d, t1_key_alg=%d",
                           t1_keyinfo[i].keysegs, t1_keyinfo[i].key_alg));
      DBUG_PRINT("error", ("t2_keysegs=%d, t2_key_alg=%d",
                           t2_keyinfo[i].keysegs, t2_keyinfo[i].key_alg));
      DBUG_RETURN(1);
    }
    for (j=  t1_keyinfo[i].keysegs; j--;)
    {
      if (t1_keysegs[j].type != t2_keysegs[j].type ||
          t1_keysegs[j].language != t2_keysegs[j].language ||
          t1_keysegs[j].null_bit != t2_keysegs[j].null_bit ||
          t1_keysegs[j].length != t2_keysegs[j].length)
      {
        DBUG_PRINT("error", ("Key segment %d (key %d) has different "
                             "definition", j, i));
        DBUG_PRINT("error", ("t1_type=%d, t1_language=%d, t1_null_bit=%d, "
                             "t1_length=%d",
                             t1_keysegs[j].type, t1_keysegs[j].language,
                             t1_keysegs[j].null_bit, t1_keysegs[j].length));
        DBUG_PRINT("error", ("t2_type=%d, t2_language=%d, t2_null_bit=%d, "
                             "t2_length=%d",
                             t2_keysegs[j].type, t2_keysegs[j].language,
                             t2_keysegs[j].null_bit, t2_keysegs[j].length));

        DBUG_RETURN(1);
      }
    }
  }
569

570 571 572 573
  for (i= 0; i < t1_recs; i++)
  {
    MARIA_COLUMNDEF *t1_rec= &t1_recinfo[i];
    MARIA_COLUMNDEF *t2_rec= &t2_recinfo[i];
574 575 576 577 578 579 580 581
    /*
      FIELD_SKIP_ZERO can be changed to FIELD_NORMAL in maria_create,
      see NOTE1 in ma_create.c
    */
    if ((t1_rec->type != t2_rec->type &&
         !(t1_rec->type == (int) FIELD_SKIP_ZERO &&
           t1_rec->length == 1 &&
           t2_rec->type == (int) FIELD_NORMAL)) ||
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
        t1_rec->length != t2_rec->length ||
        t1_rec->null_bit != t2_rec->null_bit)
    {
      DBUG_PRINT("error", ("Field %d has different definition", i));
      DBUG_PRINT("error", ("t1_type=%d, t1_length=%d, t1_null_bit=%d",
                           t1_rec->type, t1_rec->length, t1_rec->null_bit));
      DBUG_PRINT("error", ("t2_type=%d, t2_length=%d, t2_null_bit=%d",
                           t2_rec->type, t2_rec->length, t2_rec->null_bit));
      DBUG_RETURN(1);
    }
  }
  DBUG_RETURN(0);
}


597 598 599 600 601 602 603 604 605 606 607
extern "C" {

volatile int *_ma_killed_ptr(HA_CHECK *param)
{
  /* In theory Unsafe conversion, but should be ok for now */
  return (int*) &(((THD *) (param->thd))->killed);
}


void _ma_check_print_error(HA_CHECK *param, const char *fmt, ...)
{
608 609
  va_list args;
  DBUG_ENTER("_ma_check_print_error");
610 611 612 613 614
  param->error_printed |= 1;
  param->out_flag |= O_DATA_LOST;
  va_start(args, fmt);
  _ma_check_print_msg(param, "error", fmt, args);
  va_end(args);
615
  DBUG_VOID_RETURN;
616 617 618 619 620 621
}


void _ma_check_print_info(HA_CHECK *param, const char *fmt, ...)
{
  va_list args;
622
  DBUG_ENTER("_ma_check_print_info");
623 624 625
  va_start(args, fmt);
  _ma_check_print_msg(param, "info", fmt, args);
  va_end(args);
626
  DBUG_VOID_RETURN;
627 628 629 630 631
}


void _ma_check_print_warning(HA_CHECK *param, const char *fmt, ...)
{
632 633
  va_list args;
  DBUG_ENTER("_ma_check_print_warning");
634 635 636 637 638
  param->warning_printed= 1;
  param->out_flag |= O_DATA_LOST;
  va_start(args, fmt);
  _ma_check_print_msg(param, "warning", fmt, args);
  va_end(args);
639
  DBUG_VOID_RETURN;
640 641 642 643 644
}

}


645 646
ha_maria::ha_maria(handlerton *hton, TABLE_SHARE *table_arg):
handler(hton, table_arg), file(0),
647
int_table_flags(HA_NULL_IN_KEY | HA_CAN_FULLTEXT | HA_CAN_SQL_HANDLER |
648
                HA_BINLOG_ROW_CAPABLE | HA_BINLOG_STMT_CAPABLE |
649
                HA_DUPLICATE_POS | HA_CAN_INDEX_BLOBS | HA_AUTO_PART_KEY |
unknown's avatar
unknown committed
650
                HA_FILE_BASED | HA_CAN_GEOMETRY | MARIA_CANNOT_ROLLBACK |
unknown's avatar
unknown committed
651
                HA_CAN_BIT_FIELD | HA_CAN_RTREEKEYS |
652
                HA_HAS_RECORDS | HA_STATS_RECORDS_IS_EXACT),
unknown's avatar
unknown committed
653
can_enable_indexes(1), bulk_insert_with_repair_trans(FALSE)
654 655 656
{}


657 658 659 660 661 662 663 664 665
handler *ha_maria::clone(MEM_ROOT *mem_root)
{
  ha_maria *new_handler= static_cast <ha_maria *>(handler::clone(mem_root));
  if (new_handler)
    new_handler->file->state= file->state;
  return new_handler;
}


666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
static const char *ha_maria_exts[]=
{
  MARIA_NAME_IEXT,
  MARIA_NAME_DEXT,
  NullS
};


const char **ha_maria::bas_ext() const
{
  return ha_maria_exts;
}


const char *ha_maria::index_type(uint key_number)
{
  return ((table->key_info[key_number].flags & HA_FULLTEXT) ?
          "FULLTEXT" :
          (table->key_info[key_number].flags & HA_SPATIAL) ?
          "SPATIAL" :
          (table->key_info[key_number].algorithm == HA_KEY_ALG_RTREE) ?
          "RTREE" : "BTREE");
}


unknown's avatar
unknown committed
691 692 693 694 695 696 697
double ha_maria::scan_time()
{
  if (file->s->data_file_type == BLOCK_RECORD)
    return ulonglong2double(stats.data_file_length - file->s->block_size) / max(file->s->block_size / 2, IO_SIZE) + 2;
  return handler::scan_time();
}

698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
/*
  We need to be able to store at least two keys on an index page as the
  splitting algorithms depends on this. (With only one key on a page
  we also can't use any compression, which may make the index file much
  larger)
  We use HA_MAX_KEY_BUFF as this is a stack restriction imposed by the
  handler interface.

  We also need to reserve place for a record pointer (8) and 3 bytes
  per key segment to store the length of the segment + possible null bytes.
  These extra bytes are required here so that maria_create() will surely
  accept any keys created which the returned key data storage length.
*/

uint ha_maria::max_supported_key_length() const
{
  uint tmp= (maria_max_key_length() - 8 - HA_MAX_KEY_SEG*3);
  return min(HA_MAX_KEY_BUFF, tmp);
}

unknown's avatar
unknown committed
718

719 720 721
#ifdef HAVE_REPLICATION
int ha_maria::net_read_dump(NET * net)
{
unknown's avatar
unknown committed
722
  int data_fd= file->dfile.file;
723 724 725 726 727 728 729 730 731 732 733 734 735 736
  int error= 0;

  my_seek(data_fd, 0L, MY_SEEK_SET, MYF(MY_WME));
  for (;;)
  {
    ulong packet_len= my_net_read(net);
    if (!packet_len)
      break;                                    // end of file
    if (packet_len == packet_error)
    {
      sql_print_error("ha_maria::net_read_dump - read error ");
      error= -1;
      goto err;
    }
unknown's avatar
unknown committed
737
    if (my_write(data_fd, (uchar *) net->read_pos, (uint) packet_len,
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
                 MYF(MY_WME | MY_FNABP)))
    {
      error= errno;
      goto err;
    }
  }
err:
  return error;
}


int ha_maria::dump(THD * thd, int fd)
{
  MARIA_SHARE *share= file->s;
  NET *net= &thd->net;
unknown's avatar
unknown committed
753
  uint block_size= share->block_size;
754
  my_off_t bytes_to_read= share->state.state.data_file_length;
unknown's avatar
unknown committed
755
  int data_fd= file->dfile.file;
unknown's avatar
unknown committed
756
  uchar *buf= (uchar *) my_malloc(block_size, MYF(MY_WME));
757 758 759 760 761 762 763
  if (!buf)
    return ENOMEM;

  int error= 0;
  my_seek(data_fd, 0L, MY_SEEK_SET, MYF(MY_WME));
  for (; bytes_to_read > 0;)
  {
764
    size_t bytes= my_read(data_fd, buf, block_size, MYF(MY_WME));
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
    if (bytes == MY_FILE_ERROR)
    {
      error= errno;
      goto err;
    }

    if (fd >= 0)
    {
      if (my_write(fd, buf, bytes, MYF(MY_WME | MY_FNABP)))
      {
        error= errno ? errno : EPIPE;
        goto err;
      }
    }
    else
    {
unknown's avatar
unknown committed
781
      if (my_net_write(net, buf, bytes))
782 783 784 785 786 787 788 789 790 791
      {
        error= errno ? errno : EPIPE;
        goto err;
      }
    }
    bytes_to_read -= bytes;
  }

  if (fd < 0)
  {
unknown's avatar
unknown committed
792
    if (my_net_write(net, (uchar*) "", 0))
793 794 795 796 797
      error= errno ? errno : EPIPE;
    net_flush(net);
  }

err:
unknown's avatar
unknown committed
798
  my_free((uchar*) buf, MYF(0));
799 800 801 802 803 804 805 806 807
  return error;
}
#endif                                          /* HAVE_REPLICATION */

        /* Name is here without an extension */

int ha_maria::open(const char *name, int mode, uint test_if_locked)
{
  uint i;
808 809 810 811 812 813 814 815 816 817

#ifdef NOT_USED
  /*
    If the user wants to have memory mapped data files, add an
    open_flag. Do not memory map temporary tables because they are
    expected to be inserted and thus extended a lot. Memory mapping is
    efficient for files that keep their size, but very inefficient for
    growing files. Using an open_flag instead of calling ma_extra(...
    HA_EXTRA_MMAP ...) after maxs_open() has the advantage that the
    mapping is not repeated for every open, but just done on the initial
unknown's avatar
unknown committed
818
    open, when the MyISAM share is created. Every time the server
819 820 821 822 823 824 825 826 827 828
    requires to open a new instance of a table it calls this method. We
    will always supply HA_OPEN_MMAP for a permanent table. However, the
    Maria storage engine will ignore this flag if this is a secondary
    open of a table that is in use by other threads already (if the
    Maria share exists already).
  */
  if (!(test_if_locked & HA_OPEN_TMP_TABLE) && opt_maria_use_mmap)
    test_if_locked|= HA_OPEN_MMAP;
#endif

829 830 831 832 833 834 835 836 837
  if (!(file= maria_open(name, mode, test_if_locked | HA_OPEN_FROM_SQL_LAYER)))
    return (my_errno ? my_errno : -1);

  if (test_if_locked & (HA_OPEN_IGNORE_IF_LOCKED | HA_OPEN_TMP_TABLE))
    VOID(maria_extra(file, HA_EXTRA_NO_WAIT_LOCK, 0));

  info(HA_STATUS_NO_LOCK | HA_STATUS_VARIABLE | HA_STATUS_CONST);
  if (!(test_if_locked & HA_OPEN_WAIT_IF_LOCKED))
    VOID(maria_extra(file, HA_EXTRA_WAIT_LOCK, 0));
838
  if ((data_file_type= file->s->data_file_type) != STATIC_RECORD)
839
    int_table_flags |= HA_REC_NOT_IN_SEQ;
unknown's avatar
unknown committed
840 841 842 843 844 845 846 847 848 849 850
  if (!file->s->base.born_transactional)
  {
    /*
      INSERT DELAYED cannot work with transactional tables (because it cannot
      stand up to "when client gets ok the data is safe on disk": the record
      may not even be inserted). In the future, we could enable it back (as a
      client doing INSERT DELAYED knows the specificities; but we then should
      make sure to regularly commit in the delayed_insert thread). 
    */
    int_table_flags|= HA_CAN_INSERT_DELAYED;
  }
851 852 853 854 855
  if (file->s->options & (HA_OPTION_CHECKSUM | HA_OPTION_COMPRESS_RECORD))
    int_table_flags |= HA_HAS_CHECKSUM;

  for (i= 0; i < table->s->keys; i++)
  {
unknown's avatar
unknown committed
856
    plugin_ref parser= table->key_info[i].parser;
857 858
    if (table->key_info[i].flags & HA_USES_PARSER)
      file->s->keyinfo[i].parser=
unknown's avatar
unknown committed
859
        (struct st_mysql_ftparser *)plugin_decl(parser)->info;
860
    table->key_info[i].block_size= file->s->keyinfo[i].block_length;
861
  }
862 863
  my_errno= 0;
  return my_errno;
864 865 866 867 868 869 870 871 872 873 874
}


int ha_maria::close(void)
{
  MARIA_HA *tmp= file;
  file= 0;
  return maria_close(tmp);
}


unknown's avatar
unknown committed
875
int ha_maria::write_row(uchar * buf)
876
{
877
  ha_statistic_increment(&SSV::ha_write_count);
878 879 880 881 882 883 884 885 886 887

  /* If we have a timestamp column, update it to the current time */
  if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_INSERT)
    table->timestamp_field->set_time();

  /*
     If we have an auto_increment column and we are writing a changed row
     or a new row, then update the auto_increment value in the record.
  */
  if (table->next_number_field && buf == table->record[0])
888 889 890 891 892
  {
    int error;
    if ((error= update_auto_increment()))
      return error;
  }
893 894 895 896 897 898 899 900 901 902 903
  return maria_write(file, buf);
}


int ha_maria::check(THD * thd, HA_CHECK_OPT * check_opt)
{
  if (!file)
    return HA_ADMIN_INTERNAL_ERROR;
  int error;
  HA_CHECK param;
  MARIA_SHARE *share= file->s;
unknown's avatar
unknown committed
904
  const char *old_proc_info= thd_proc_info(thd, "Checking table");
905

unknown's avatar
unknown committed
906
  maria_chk_init(&param);
907 908 909 910 911
  param.thd= thd;
  param.op_name= "check";
  param.db_name= table->s->db.str;
  param.table_name= table->alias;
  param.testflag= check_opt->flags | T_CHECK | T_SILENT;
912
  param.stats_method= (enum_handler_stats_method)THDVAR(thd,stats_method);
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930

  if (!(table->db_stat & HA_READ_ONLY))
    param.testflag |= T_STATISTICS;
  param.using_global_keycache= 1;

  if (!maria_is_crashed(file) &&
      (((param.testflag & T_CHECK_ONLY_CHANGED) &&
        !(share->state.changed & (STATE_CHANGED | STATE_CRASHED |
                                  STATE_CRASHED_ON_REPAIR)) &&
        share->state.open_count == 0) ||
       ((param.testflag & T_FAST) && (share->state.open_count ==
                                      (uint) (share->global_changed ? 1 :
                                              0)))))
    return HA_ADMIN_ALREADY_DONE;

  error= maria_chk_status(&param, file);                // Not fatal
  error= maria_chk_size(&param, file);
  if (!error)
931
    error|= maria_chk_del(&param, file, param.testflag);
932 933 934 935 936 937 938 939 940
  if (!error)
    error= maria_chk_key(&param, file);
  if (!error)
  {
    if ((!(param.testflag & T_QUICK) &&
         ((share->options &
           (HA_OPTION_PACK_RECORD | HA_OPTION_COMPRESS_RECORD)) ||
          (param.testflag & (T_EXTEND | T_MEDIUM)))) || maria_is_crashed(file))
    {
941
      ulonglong old_testflag= param.testflag;
942
      param.testflag |= T_MEDIUM;
unknown's avatar
unknown committed
943
      if (!(error= init_io_cache(&param.read_cache, file->dfile.file,
944 945 946
                                 my_default_record_cache_size, READ_CACHE,
                                 share->pack.header_length, 1, MYF(MY_WME))))
      {
947 948
        error= maria_chk_data_link(&param, file,
                                   test(param.testflag & T_EXTEND));
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
        end_io_cache(&(param.read_cache));
      }
      param.testflag= old_testflag;
    }
  }
  if (!error)
  {
    if ((share->state.changed & (STATE_CHANGED |
                                 STATE_CRASHED_ON_REPAIR |
                                 STATE_CRASHED | STATE_NOT_ANALYZED)) ||
        (param.testflag & T_STATISTICS) || maria_is_crashed(file))
    {
      file->update |= HA_STATE_CHANGED | HA_STATE_ROW_CHANGED;
      pthread_mutex_lock(&share->intern_lock);
      share->state.changed &= ~(STATE_CHANGED | STATE_CRASHED |
                                STATE_CRASHED_ON_REPAIR);
      if (!(table->db_stat & HA_READ_ONLY))
966 967 968
        error= maria_update_state_info(&param, file,
                                       UPDATE_TIME | UPDATE_OPEN_COUNT |
                                       UPDATE_STAT);
969 970 971 972 973 974 975 976 977 978 979
      pthread_mutex_unlock(&share->intern_lock);
      info(HA_STATUS_NO_LOCK | HA_STATUS_TIME | HA_STATUS_VARIABLE |
           HA_STATUS_CONST);
    }
  }
  else if (!maria_is_crashed(file) && !thd->killed)
  {
    maria_mark_crashed(file);
    file->update |= HA_STATE_CHANGED | HA_STATE_ROW_CHANGED;
  }

unknown's avatar
unknown committed
980
  thd_proc_info(thd, old_proc_info);
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
  return error ? HA_ADMIN_CORRUPT : HA_ADMIN_OK;
}


/*
  Analyze the key distribution in the table
  As the table may be only locked for read, we have to take into account that
  two threads may do an analyze at the same time!
*/

int ha_maria::analyze(THD *thd, HA_CHECK_OPT * check_opt)
{
  int error= 0;
  HA_CHECK param;
  MARIA_SHARE *share= file->s;

unknown's avatar
unknown committed
997
  maria_chk_init(&param);
998 999 1000 1001 1002 1003 1004
  param.thd= thd;
  param.op_name= "analyze";
  param.db_name= table->s->db.str;
  param.table_name= table->alias;
  param.testflag= (T_FAST | T_CHECK | T_SILENT | T_STATISTICS |
                   T_DONT_CHECK_CHECKSUM);
  param.using_global_keycache= 1;
1005
  param.stats_method= (enum_handler_stats_method)THDVAR(thd,stats_method);
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027

  if (!(share->state.changed & STATE_NOT_ANALYZED))
    return HA_ADMIN_ALREADY_DONE;

  error= maria_chk_key(&param, file);
  if (!error)
  {
    pthread_mutex_lock(&share->intern_lock);
    error= maria_update_state_info(&param, file, UPDATE_STAT);
    pthread_mutex_unlock(&share->intern_lock);
  }
  else if (!maria_is_crashed(file) && !thd->killed)
    maria_mark_crashed(file);
  return error ? HA_ADMIN_CORRUPT : HA_ADMIN_OK;
}


int ha_maria::restore(THD * thd, HA_CHECK_OPT *check_opt)
{
  HA_CHECK_OPT tmp_check_opt;
  char *backup_dir= thd->lex->backup_dir;
  char src_path[FN_REFLEN], dst_path[FN_REFLEN];
1028
  char table_name[FN_REFLEN];
1029 1030 1031 1032
  int error;
  const char *errmsg;
  DBUG_ENTER("restore");

1033 1034 1035
  VOID(tablename_to_filename(table->s->table_name.str, table_name,
                             sizeof(table_name)));

1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
  if (fn_format_relative_to_data_home(src_path, table_name, backup_dir,
                                      MARIA_NAME_DEXT))
    DBUG_RETURN(HA_ADMIN_INVALID);

  strxmov(dst_path, table->s->normalized_path.str, MARIA_NAME_DEXT, NullS);
  if (my_copy(src_path, dst_path, MYF(MY_WME)))
  {
    error= HA_ADMIN_FAILED;
    errmsg= "Failed in my_copy (Error %d)";
    goto err;
  }

  tmp_check_opt.init();
  tmp_check_opt.flags |= T_VERY_SILENT | T_CALC_CHECKSUM | T_QUICK;
  DBUG_RETURN(repair(thd, &tmp_check_opt));

err:
  {
    HA_CHECK param;
unknown's avatar
unknown committed
1055
    maria_chk_init(&param);
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
    param.thd= thd;
    param.op_name= "restore";
    param.db_name= table->s->db.str;
    param.table_name= table->s->table_name.str;
    param.testflag= 0;
    _ma_check_print_error(&param, errmsg, my_errno);
    DBUG_RETURN(error);
  }
}


int ha_maria::backup(THD * thd, HA_CHECK_OPT *check_opt)
{
  char *backup_dir= thd->lex->backup_dir;
  char src_path[FN_REFLEN], dst_path[FN_REFLEN];
1071
  char table_name[FN_REFLEN];
1072 1073 1074 1075
  int error;
  const char *errmsg;
  DBUG_ENTER("ha_maria::backup");

1076 1077 1078
  VOID(tablename_to_filename(table->s->table_name.str, table_name,
                             sizeof(table_name)));

1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
  if (fn_format_relative_to_data_home(dst_path, table_name, backup_dir,
                                      reg_ext))
  {
    errmsg= "Failed in fn_format() for .frm file (errno: %d)";
    error= HA_ADMIN_INVALID;
    goto err;
  }

  strxmov(src_path, table->s->normalized_path.str, reg_ext, NullS);
  if (my_copy(src_path, dst_path,
              MYF(MY_WME | MY_HOLD_ORIGINAL_MODES | MY_DONT_OVERWRITE_FILE)))
  {
    error= HA_ADMIN_FAILED;
    errmsg= "Failed copying .frm file (errno: %d)";
    goto err;
  }

  /* Change extension */
  if (fn_format_relative_to_data_home(dst_path, table_name, backup_dir,
                                      MARIA_NAME_DEXT))
  {
    errmsg= "Failed in fn_format() for .MYD file (errno: %d)";
    error= HA_ADMIN_INVALID;
    goto err;
  }

  strxmov(src_path, table->s->normalized_path.str, MARIA_NAME_DEXT, NullS);
unknown's avatar
unknown committed
1106 1107 1108 1109 1110 1111 1112
  if (_ma_flush_table_files(file, MARIA_FLUSH_DATA, FLUSH_FORCE_WRITE,
                            FLUSH_KEEP))
  {
    error= HA_ADMIN_FAILED;
    errmsg= "Failed in flush (Error %d)";
    goto err;
  }
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
  if (my_copy(src_path, dst_path,
              MYF(MY_WME | MY_HOLD_ORIGINAL_MODES | MY_DONT_OVERWRITE_FILE)))
  {
    errmsg= "Failed copying .MYD file (errno: %d)";
    error= HA_ADMIN_FAILED;
    goto err;
  }
  DBUG_RETURN(HA_ADMIN_OK);

err:
  {
    HA_CHECK param;
unknown's avatar
unknown committed
1125
    maria_chk_init(&param);
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
    param.thd= thd;
    param.op_name= "backup";
    param.db_name= table->s->db.str;
    param.table_name= table->s->table_name.str;
    param.testflag= 0;
    _ma_check_print_error(&param, errmsg, my_errno);
    DBUG_RETURN(error);
  }
}


int ha_maria::repair(THD * thd, HA_CHECK_OPT *check_opt)
{
  int error;
  HA_CHECK param;
  ha_rows start_records;

  if (!file)
    return HA_ADMIN_INTERNAL_ERROR;

unknown's avatar
unknown committed
1146
  maria_chk_init(&param);
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
  param.thd= thd;
  param.op_name= "repair";
  param.testflag= ((check_opt->flags & ~(T_EXTEND)) |
                   T_SILENT | T_FORCE_CREATE | T_CALC_CHECKSUM |
                   (check_opt->flags & T_EXTEND ? T_REP : T_REP_BY_SORT));
  param.sort_buffer_length= check_opt->sort_buffer_size;
  start_records= file->state->records;
  while ((error= repair(thd, param, 0)) && param.retry_repair)
  {
    param.retry_repair= 0;
    if (test_all_bits(param.testflag,
                      (uint) (T_RETRY_WITHOUT_QUICK | T_QUICK)))
    {
      param.testflag &= ~T_RETRY_WITHOUT_QUICK;
      sql_print_information("Retrying repair of: '%s' without quick",
1162
                            table->s->path.str);
1163 1164 1165 1166 1167 1168 1169
      continue;
    }
    param.testflag &= ~T_QUICK;
    if ((param.testflag & T_REP_BY_SORT))
    {
      param.testflag= (param.testflag & ~T_REP_BY_SORT) | T_REP;
      sql_print_information("Retrying repair of: '%s' with keycache",
1170
                            table->s->path.str);
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
      continue;
    }
    break;
  }
  if (!error && start_records != file->state->records &&
      !(check_opt->flags & T_VERY_SILENT))
  {
    char llbuff[22], llbuff2[22];
    sql_print_information("Found %s of %s rows when repairing '%s'",
                          llstr(file->state->records, llbuff),
1181 1182
                          llstr(start_records, llbuff2),
                          table->s->path.str);
1183 1184 1185 1186
  }
  return error;
}

1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
int ha_maria::zerofill(THD * thd, HA_CHECK_OPT *check_opt)
{
  int error;
  HA_CHECK param;

  if (!file)
    return HA_ADMIN_INTERNAL_ERROR;

  maria_chk_init(&param);
  param.thd= thd;
  param.op_name= "zerofill";
  param.testflag= check_opt->flags | T_SILENT | T_ZEROFILL;
  param.sort_buffer_length= check_opt->sort_buffer_size;
  error=maria_zerofill(&param, file, file->s->open_file_name);

  return error;
}

1205 1206 1207 1208 1209 1210 1211
int ha_maria::optimize(THD * thd, HA_CHECK_OPT *check_opt)
{
  int error;
  if (!file)
    return HA_ADMIN_INTERNAL_ERROR;
  HA_CHECK param;

unknown's avatar
unknown committed
1212
  maria_chk_init(&param);
1213 1214 1215 1216 1217 1218 1219
  param.thd= thd;
  param.op_name= "optimize";
  param.testflag= (check_opt->flags | T_SILENT | T_FORCE_CREATE |
                   T_REP_BY_SORT | T_STATISTICS | T_SORT_INDEX);
  param.sort_buffer_length= check_opt->sort_buffer_size;
  if ((error= repair(thd, param, 1)) && param.retry_repair)
  {
1220 1221
    sql_print_warning("Warning: Optimize table got errno %d on %s.%s, retrying",
                      my_errno, param.db_name, param.table_name);
1222 1223 1224 1225 1226 1227 1228
    param.testflag &= ~T_REP_BY_SORT;
    error= repair(thd, param, 1);
  }
  return error;
}


1229
int ha_maria::repair(THD *thd, HA_CHECK &param, bool do_optimize)
1230 1231
{
  int error= 0;
1232
  ulonglong local_testflag= param.testflag;
1233
  bool optimize_done= !do_optimize, statistics_done= 0;
1234 1235 1236 1237 1238 1239
  const char *old_proc_info= thd->proc_info;
  char fixed_name[FN_REFLEN];
  MARIA_SHARE *share= file->s;
  ha_rows rows= file->state->records;
  DBUG_ENTER("ha_maria::repair");

1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
  /*
    Normally this method is entered with a properly opened table. If the
    repair fails, it can be repeated with more elaborate options. Under
    special circumstances it can happen that a repair fails so that it
    closed the data file and cannot re-open it. In this case file->dfile
    is set to -1. We must not try another repair without an open data
    file. (Bug #25289)
  */
  if (file->dfile.file == -1)
  {
    sql_print_information("Retrying repair of: '%s' failed. "
                          "Please try REPAIR EXTENDED or maria_chk",
                          table->s->path.str);
    DBUG_RETURN(HA_ADMIN_FAILED);
  }

1256 1257 1258 1259 1260 1261 1262
  param.db_name= table->s->db.str;
  param.table_name= table->alias;
  param.tmpfile_createflag= O_RDWR | O_TRUNC;
  param.using_global_keycache= 1;
  param.thd= thd;
  param.tmpdir= &mysql_tmpdir_list;
  param.out_flag= 0;
1263
  strmov(fixed_name, file->s->open_file_name);
1264 1265 1266 1267 1268 1269 1270 1271 1272

  // Don't lock tables if we have used LOCK TABLE
  if (!thd->locked_tables &&
      maria_lock_database(file, table->s->tmp_table ? F_EXTRA_LCK : F_WRLCK))
  {
    _ma_check_print_error(&param, ER(ER_CANT_LOCK), my_errno);
    DBUG_RETURN(HA_ADMIN_FAILED);
  }

1273
  if (!do_optimize ||
unknown's avatar
unknown committed
1274 1275 1276 1277 1278 1279
      ((file->s->data_file_type == BLOCK_RECORD) ?
       (share->state.changed & STATE_NOT_OPTIMIZED_ROWS) :
       (file->state->del || share->state.split != file->state->records)) &&
      (!(param.testflag & T_QUICK) ||
       (share->state.changed & (STATE_NOT_OPTIMIZED_KEYS |
                                STATE_NOT_OPTIMIZED_ROWS))))
1280 1281 1282 1283
  {
    ulonglong key_map= ((local_testflag & T_CREATE_MISSING_KEYS) ?
                        maria_get_mask_all_keys_active(share->base.keys) :
                        share->state.key_map);
1284
    ulonglong save_testflag= param.testflag;
1285 1286 1287 1288 1289 1290
    if (maria_test_if_sort_rep(file, file->state->records, key_map, 0) &&
        (local_testflag & T_REP_BY_SORT))
    {
      local_testflag |= T_STATISTICS;
      param.testflag |= T_STATISTICS;           // We get this for free
      statistics_done= 1;
1291
      /* TODO: Remove BLOCK_RECORD test when parallel works with blocks */
1292
      if (THDVAR(thd,repair_threads) > 1 &&
1293
          file->s->data_file_type != BLOCK_RECORD)
1294 1295 1296 1297
      {
        char buf[40];
        /* TODO: respect maria_repair_threads variable */
        my_snprintf(buf, 40, "Repair with %d threads", my_count_bits(key_map));
unknown's avatar
unknown committed
1298
        thd_proc_info(thd, buf);
1299
        param.testflag|= T_REP_PARALLEL;
1300
        error= maria_repair_parallel(&param, file, fixed_name,
1301
                                     test(param.testflag & T_QUICK));
unknown's avatar
unknown committed
1302 1303
        /* to reset proc_info, as it was pointing to local buffer */
        thd_proc_info(thd, "Repair done");
1304 1305 1306
      }
      else
      {
unknown's avatar
unknown committed
1307
        thd_proc_info(thd, "Repair by sorting");
1308
        param.testflag|= T_REP_BY_SORT;
1309
        error= maria_repair_by_sort(&param, file, fixed_name,
1310
                                    test(param.testflag & T_QUICK));
1311 1312 1313 1314
      }
    }
    else
    {
unknown's avatar
unknown committed
1315
      thd_proc_info(thd, "Repair with keycache");
1316
      param.testflag &= ~(T_REP_BY_SORT | T_REP_PARALLEL);
1317 1318
      error= maria_repair(&param, file, fixed_name,
                          test(param.testflag & T_QUICK));
1319
    }
1320
    param.testflag= save_testflag;
1321 1322 1323 1324 1325 1326 1327 1328
    optimize_done= 1;
  }
  if (!error)
  {
    if ((local_testflag & T_SORT_INDEX) &&
        (share->state.changed & STATE_NOT_SORTED_PAGES))
    {
      optimize_done= 1;
unknown's avatar
unknown committed
1329
      thd_proc_info(thd, "Sorting index");
1330 1331 1332 1333 1334 1335 1336
      error= maria_sort_index(&param, file, fixed_name);
    }
    if (!statistics_done && (local_testflag & T_STATISTICS))
    {
      if (share->state.changed & STATE_NOT_ANALYZED)
      {
        optimize_done= 1;
unknown's avatar
unknown committed
1337
        thd_proc_info(thd, "Analyzing");
1338 1339 1340 1341 1342 1343
        error= maria_chk_key(&param, file);
      }
      else
        local_testflag &= ~T_STATISTICS;        // Don't update statistics
    }
  }
unknown's avatar
unknown committed
1344
  thd_proc_info(thd, "Saving state");
unknown's avatar
unknown committed
1345
  pthread_mutex_lock(&share->intern_lock);
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
  if (!error)
  {
    if ((share->state.changed & STATE_CHANGED) || maria_is_crashed(file))
    {
      share->state.changed &= ~(STATE_CHANGED | STATE_CRASHED |
                                STATE_CRASHED_ON_REPAIR);
      file->update |= HA_STATE_CHANGED | HA_STATE_ROW_CHANGED;
    }
    /*
       the following 'if', thought conceptually wrong,
       is a useful optimization nevertheless.
    */
    if (file->state != &file->s->state.state)
      file->s->state.state= *file->state;
    if (file->s->base.auto_key)
      _ma_update_auto_increment_key(&param, file, 1);
    if (optimize_done)
      error= maria_update_state_info(&param, file,
                               UPDATE_TIME | UPDATE_OPEN_COUNT |
                               (local_testflag &
                                T_STATISTICS ? UPDATE_STAT : 0));
    info(HA_STATUS_NO_LOCK | HA_STATUS_TIME | HA_STATUS_VARIABLE |
         HA_STATUS_CONST);
    if (rows != file->state->records && !(param.testflag & T_VERY_SILENT))
    {
      char llbuff[22], llbuff2[22];
      _ma_check_print_warning(&param, "Number of rows changed from %s to %s",
                              llstr(rows, llbuff),
                              llstr(file->state->records, llbuff2));
unknown's avatar
unknown committed
1375 1376 1377
      /* Abort if warning was converted to error */
      if (current_thd->is_error())
        error= 1;
1378 1379 1380 1381 1382 1383 1384 1385
    }
  }
  else
  {
    maria_mark_crashed_on_repair(file);
    file->update |= HA_STATE_CHANGED | HA_STATE_ROW_CHANGED;
    maria_update_state_info(&param, file, 0);
  }
unknown's avatar
unknown committed
1386
  pthread_mutex_unlock(&share->intern_lock);
unknown's avatar
unknown committed
1387
  thd_proc_info(thd, old_proc_info);
1388 1389
  if (!thd->locked_tables)
    maria_lock_database(file, F_UNLCK);
unknown's avatar
unknown committed
1390 1391 1392 1393 1394
  error= error ? HA_ADMIN_FAILED :
    (optimize_done ?
     (write_log_record_for_repair(&param, file) ? HA_ADMIN_FAILED :
      HA_ADMIN_OK) : HA_ADMIN_ALREADY_DONE);
  DBUG_RETURN(error);
1395 1396 1397 1398 1399 1400 1401 1402 1403
}


/*
  Assign table indexes to a specific key cache.
*/

int ha_maria::assign_to_keycache(THD * thd, HA_CHECK_OPT *check_opt)
{
unknown's avatar
unknown committed
1404
#if 0 && NOT_IMPLEMENTED
unknown's avatar
unknown committed
1405
  PAGECACHE *new_pagecache= check_opt->pagecache;
1406 1407
  const char *errmsg= 0;
  int error= HA_ADMIN_OK;
1408
  ulonglong map;
1409 1410 1411
  TABLE_LIST *table_list= table->pos_in_table_list;
  DBUG_ENTER("ha_maria::assign_to_keycache");

unknown's avatar
unknown committed
1412

unknown's avatar
unknown committed
1413 1414 1415 1416 1417 1418 1419 1420
  table->keys_in_use_for_query.clear_all();

  if (table_list->process_index_hints(table))
    DBUG_RETURN(HA_ADMIN_FAILED);
  map= ~(ulonglong) 0;
  if (!table->keys_in_use_for_query.is_clear_all())
    /* use all keys if there's no list specified by the user through hints */
    map= table->keys_in_use_for_query.to_ulonglong();
1421

unknown's avatar
unknown committed
1422
  if ((error= maria_assign_to_pagecache(file, map, new_pagecache)))
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
  {
    char buf[STRING_BUFFER_USUAL_SIZE];
    my_snprintf(buf, sizeof(buf),
                "Failed to flush to index file (errno: %d)", error);
    errmsg= buf;
    error= HA_ADMIN_CORRUPT;
  }

  if (error != HA_ADMIN_OK)
  {
    /* Send error to user */
    HA_CHECK param;
unknown's avatar
unknown committed
1435
    maria_chk_init(&param);
1436 1437 1438 1439 1440 1441 1442 1443
    param.thd= thd;
    param.op_name= "assign_to_keycache";
    param.db_name= table->s->db.str;
    param.table_name= table->s->table_name.str;
    param.testflag= 0;
    _ma_check_print_error(&param, errmsg);
  }
  DBUG_RETURN(error);
unknown's avatar
unknown committed
1444 1445 1446
#else
  return  HA_ADMIN_NOT_IMPLEMENTED;
#endif
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
}


/*
  Preload pages of the index file for a table into the key cache.
*/

int ha_maria::preload_keys(THD * thd, HA_CHECK_OPT *check_opt)
{
  int error;
  const char *errmsg;
1458
  ulonglong map;
1459 1460
  TABLE_LIST *table_list= table->pos_in_table_list;
  my_bool ignore_leaves= table_list->ignore_leaves;
1461
  char buf[ERRMSGSIZE+20];
1462 1463 1464

  DBUG_ENTER("ha_maria::preload_keys");

unknown's avatar
unknown committed
1465 1466 1467 1468 1469 1470
  table->keys_in_use_for_query.clear_all();

  if (table_list->process_index_hints(table))
    DBUG_RETURN(HA_ADMIN_FAILED);

  map= ~(ulonglong) 0;
1471
  /* Check validity of the index references */
unknown's avatar
unknown committed
1472 1473 1474
  if (!table->keys_in_use_for_query.is_clear_all())
    /* use all keys if there's no list specified by the user through hints */
    map= table->keys_in_use_for_query.to_ulonglong();
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501

  maria_extra(file, HA_EXTRA_PRELOAD_BUFFER_SIZE,
              (void*) &thd->variables.preload_buff_size);

  if ((error= maria_preload(file, map, ignore_leaves)))
  {
    switch (error) {
    case HA_ERR_NON_UNIQUE_BLOCK_SIZE:
      errmsg= "Indexes use different block sizes";
      break;
    case HA_ERR_OUT_OF_MEM:
      errmsg= "Failed to allocate buffer";
      break;
    default:
      my_snprintf(buf, ERRMSGSIZE,
                  "Failed to read from index file (errno: %d)", my_errno);
      errmsg= buf;
    }
    error= HA_ADMIN_FAILED;
    goto err;
  }

  DBUG_RETURN(HA_ADMIN_OK);

err:
  {
    HA_CHECK param;
unknown's avatar
unknown committed
1502
    maria_chk_init(&param);
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
    param.thd= thd;
    param.op_name= "preload_keys";
    param.db_name= table->s->db.str;
    param.table_name= table->s->table_name.str;
    param.testflag= 0;
    _ma_check_print_error(&param, errmsg);
    DBUG_RETURN(error);
  }
}


/*
  Disable indexes, making it persistent if requested.

  SYNOPSIS
    disable_indexes()
    mode        mode of operation:
                HA_KEY_SWITCH_NONUNIQ      disable all non-unique keys
                HA_KEY_SWITCH_ALL          disable all keys
                HA_KEY_SWITCH_NONUNIQ_SAVE dis. non-uni. and make persistent
                HA_KEY_SWITCH_ALL_SAVE     dis. all keys and make persistent

  IMPLEMENTATION
    HA_KEY_SWITCH_NONUNIQ       is not implemented.
    HA_KEY_SWITCH_ALL_SAVE      is not implemented.

  RETURN
    0  ok
    HA_ERR_WRONG_COMMAND  mode not implemented.
*/

int ha_maria::disable_indexes(uint mode)
{
  int error;

  if (mode == HA_KEY_SWITCH_ALL)
  {
    /* call a storage engine function to switch the key map */
    error= maria_disable_indexes(file);
  }
  else if (mode == HA_KEY_SWITCH_NONUNIQ_SAVE)
  {
    maria_extra(file, HA_EXTRA_NO_KEYS, 0);
    info(HA_STATUS_CONST);                      // Read new key info
    error= 0;
  }
  else
  {
    /* mode not implemented */
    error= HA_ERR_WRONG_COMMAND;
  }
  return error;
}


/*
  Enable indexes, making it persistent if requested.

  SYNOPSIS
    enable_indexes()
    mode        mode of operation:
                HA_KEY_SWITCH_NONUNIQ      enable all non-unique keys
                HA_KEY_SWITCH_ALL          enable all keys
                HA_KEY_SWITCH_NONUNIQ_SAVE en. non-uni. and make persistent
                HA_KEY_SWITCH_ALL_SAVE     en. all keys and make persistent

  DESCRIPTION
    Enable indexes, which might have been disabled by disable_index() before.
    The modes without _SAVE work only if both data and indexes are empty,
    since the MARIA repair would enable them persistently.
    To be sure in these cases, call handler::delete_all_rows() before.

  IMPLEMENTATION
    HA_KEY_SWITCH_NONUNIQ       is not implemented.
    HA_KEY_SWITCH_ALL_SAVE      is not implemented.

  RETURN
    0  ok
    !=0  Error, among others:
    HA_ERR_CRASHED  data or index is non-empty. Delete all rows and retry.
    HA_ERR_WRONG_COMMAND  mode not implemented.
*/

int ha_maria::enable_indexes(uint mode)
{
  int error;

  if (maria_is_all_keys_active(file->s->state.key_map, file->s->base.keys))
  {
    /* All indexes are enabled already. */
    return 0;
  }

  if (mode == HA_KEY_SWITCH_ALL)
  {
    error= maria_enable_indexes(file);
    /*
       Do not try to repair on error,
       as this could make the enabled state persistent,
       but mode==HA_KEY_SWITCH_ALL forbids it.
    */
  }
  else if (mode == HA_KEY_SWITCH_NONUNIQ_SAVE)
  {
    THD *thd= current_thd;
    HA_CHECK param;
unknown's avatar
unknown committed
1609
    const char *save_proc_info= thd_proc_info(thd, "Creating index");
unknown's avatar
unknown committed
1610
    maria_chk_init(&param);
1611 1612
    param.op_name= "recreating_index";
    param.testflag= (T_SILENT | T_REP_BY_SORT | T_QUICK |
unknown's avatar
unknown committed
1613
                     T_CREATE_MISSING_KEYS | T_SAFE_REPAIR);
unknown's avatar
unknown committed
1614 1615 1616 1617 1618 1619 1620 1621
    if (bulk_insert_with_repair_trans)
    {
      /*
        Don't bump create_rename_lsn, because UNDO_BULK_INSERT_WITH_REPAIR
        should not be skipped in case of crash during repair.
      */
      param.testflag|= T_NO_CREATE_RENAME_LSN;
    }
1622
    param.myf_rw &= ~MY_WAIT_IF_FULL;
1623 1624
    param.sort_buffer_length= THDVAR(thd,sort_buffer_size);
    param.stats_method= (enum_handler_stats_method)THDVAR(thd,stats_method);
1625 1626 1627
    param.tmpdir= &mysql_tmpdir_list;
    if ((error= (repair(thd, param, 0) != HA_ADMIN_OK)) && param.retry_repair)
    {
1628 1629
      sql_print_warning("Warning: Enabling keys got errno %d on %s.%s, retrying",
                        my_errno, param.db_name, param.table_name);
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
      /* Repairing by sort failed. Now try standard repair method. */
      param.testflag &= ~(T_REP_BY_SORT | T_QUICK);
      error= (repair(thd, param, 0) != HA_ADMIN_OK);
      /*
         If the standard repair succeeded, clear all error messages which
         might have been set by the first repair. They can still be seen
         with SHOW WARNINGS then.
      */
      if (!error)
        thd->clear_error();
    }
    info(HA_STATUS_CONST);
unknown's avatar
unknown committed
1642
    thd_proc_info(thd, save_proc_info);
1643 1644 1645 1646 1647 1648
  }
  else
  {
    /* mode not implemented */
    error= HA_ERR_WRONG_COMMAND;
  }
unknown's avatar
unknown committed
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
  DBUG_EXECUTE_IF("maria_flush_whole_log",
                  {
                    DBUG_PRINT("maria_flush_whole_log", ("now"));
                    translog_flush(translog_get_horizon());
                  });
  DBUG_EXECUTE_IF("maria_crash_enable_index",
                  {
                    DBUG_PRINT("maria_crash_enable_index", ("now"));
                    fflush(DBUG_FILE);
                    abort();
                  });
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
  return error;
}


/*
  Test if indexes are disabled.


  SYNOPSIS
    indexes_are_disabled()
      no parameters


  RETURN
    0  indexes are not disabled
    1  all indexes are disabled
   [2  non-unique indexes are disabled - NOT YET IMPLEMENTED]
*/

int ha_maria::indexes_are_disabled(void)
{
  return maria_indexes_are_disabled(file);
}


/*
  prepare for a many-rows insert operation
  e.g. - disable indexes (if they can be recreated fast) or
  activate special bulk-insert optimizations

  SYNOPSIS
    start_bulk_insert(rows)
    rows        Rows to be inserted
                0 if we don't know

  NOTICE
    Do not forget to call end_bulk_insert() later!
*/

void ha_maria::start_bulk_insert(ha_rows rows)
{
  DBUG_ENTER("ha_maria::start_bulk_insert");
  THD *thd= current_thd;
  ulong size= min(thd->variables.read_buff_size,
1704
                  (ulong) (table->s->avg_row_length * rows));
1705 1706 1707 1708 1709 1710 1711
  DBUG_PRINT("info", ("start_bulk_insert: rows %lu size %lu",
                      (ulong) rows, size));

  /* don't enable row cache if too few rows */
  if (!rows || (rows > MARIA_MIN_ROWS_TO_USE_WRITE_CACHE))
    maria_extra(file, HA_EXTRA_WRITE_CACHE, (void*) &size);

1712 1713
  can_enable_indexes= (maria_is_all_keys_active(file->s->state.key_map,
                                                file->s->base.keys));
unknown's avatar
unknown committed
1714
  bulk_insert_with_repair_trans= FALSE;
1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725

  if (!(specialflag & SPECIAL_SAFE_MODE))
  {
    /*
       Only disable old index if the table was empty and we are inserting
       a lot of rows.
       We should not do this for only a few rows as this is slower and
       we don't want to update the key statistics based of only a few rows.
    */
    if (file->state->records == 0 && can_enable_indexes &&
        (!rows || rows >= MARIA_MIN_ROWS_TO_DISABLE_INDEXES))
unknown's avatar
unknown committed
1726
    {
1727
      maria_disable_non_unique_index(file, rows);
unknown's avatar
unknown committed
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
      if (file->s->now_transactional)
      {
        bulk_insert_with_repair_trans= TRUE;
        write_log_record_for_bulk_insert_with_repair(file);
        /*
          Pages currently in the page cache have type PAGECACHE_LSN_PAGE, we
          are not allowed to overwrite them with PAGECACHE_PLAIN_PAGE, so
          throw them away. It is not losing data, because we just wrote and
          forced an UNDO which will for sure empty the table if we crash.
        */
        _ma_flush_table_files(file, MARIA_FLUSH_DATA|MARIA_FLUSH_INDEX,
                              FLUSH_IGNORE_CHANGED, FLUSH_IGNORE_CHANGED);
        _ma_tmp_disable_logging_for_table(file, TRUE);
      }
    }
1743 1744
    else if (!file->bulk_insert &&
             (!rows || rows >= MARIA_MIN_ROWS_TO_USE_BULK_INSERT))
1745 1746 1747
    {
      maria_init_bulk_insert(file, thd->variables.bulk_insert_buff_size, rows);
    }
unknown's avatar
unknown committed
1748 1749 1750 1751 1752
    /**
       @todo If we have 0 records here, there is no need to log REDO/UNDO for
       each data row, we can just log some special UNDO which will empty the
       data file if need to rollback.
    */
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
  }
  DBUG_VOID_RETURN;
}


/*
  end special bulk-insert optimizations,
  which have been activated by start_bulk_insert().

  SYNOPSIS
    end_bulk_insert()
    no arguments

  RETURN
    0     OK
    != 0  Error
*/

int ha_maria::end_bulk_insert()
{
unknown's avatar
unknown committed
1773 1774
  int err;
  DBUG_ENTER("ha_maria::end_bulk_insert");
1775
  maria_end_bulk_insert(file);
unknown's avatar
unknown committed
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
  if ((err= maria_extra(file, HA_EXTRA_NO_CACHE, 0)))
    goto end;
  if (can_enable_indexes)
    err= enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
end:
  if (bulk_insert_with_repair_trans)
  {
    DBUG_ASSERT(can_enable_indexes);
    /* table was transactional just before start_bulk_insert() */
    _ma_reenable_logging_for_table(file);
  }
  DBUG_RETURN(err);
1788 1789 1790 1791 1792
}


bool ha_maria::check_and_repair(THD *thd)
{
1793
  int error;
1794 1795 1796 1797 1798 1799 1800
  int marked_crashed;
  char *old_query;
  uint old_query_length;
  HA_CHECK_OPT check_opt;
  DBUG_ENTER("ha_maria::check_and_repair");

  check_opt.init();
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818

  if (file->s->state.changed & STATE_MOVED)
  {
    sql_print_information("Zerofilling table:   '%s'", table->s->path.str);
    if (!(error= zerofill(thd, &check_opt)))
      DBUG_RETURN(0);
  }
  else
    error= 1;

  /*
    if we got this far - the table is crashed.
    but don't auto-repair if maria_recover_options is not set
  */
  if (!maria_recover_options)
    DBUG_RETURN(error);

  error= 0;
1819 1820 1821 1822
  check_opt.flags= T_MEDIUM | T_AUTO_REPAIR;
  // Don't use quick if deleted rows
  if (!file->state->del && (maria_recover_options & HA_RECOVER_QUICK))
    check_opt.flags |= T_QUICK;
1823
  sql_print_warning("Checking table:   '%s'", table->s->path.str);
1824 1825 1826 1827 1828 1829 1830 1831 1832 1833

  old_query= thd->query;
  old_query_length= thd->query_length;
  pthread_mutex_lock(&LOCK_thread_count);
  thd->query= table->s->table_name.str;
  thd->query_length= table->s->table_name.length;
  pthread_mutex_unlock(&LOCK_thread_count);

  if ((marked_crashed= maria_is_crashed(file)) || check(thd, &check_opt))
  {
1834
    sql_print_warning("Recovering table: '%s'", table->s->path.str);
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
    check_opt.flags=
      ((maria_recover_options & HA_RECOVER_BACKUP ? T_BACKUP_DATA : 0) |
       (marked_crashed ? 0 : T_QUICK) |
       (maria_recover_options & HA_RECOVER_FORCE ? 0 : T_SAFE_REPAIR) |
       T_AUTO_REPAIR);
    if (repair(thd, &check_opt))
      error= 1;
  }
  pthread_mutex_lock(&LOCK_thread_count);
  thd->query= old_query;
  thd->query_length= old_query_length;
  pthread_mutex_unlock(&LOCK_thread_count);
  DBUG_RETURN(error);
}


bool ha_maria::is_crashed() const
{
1853
  return (file->s->state.changed & (STATE_CRASHED | STATE_MOVED) ||
1854 1855 1856 1857
          (my_disable_locking && file->s->state.open_count));
}


unknown's avatar
unknown committed
1858
int ha_maria::update_row(const uchar * old_data, uchar * new_data)
1859
{
1860
  ha_statistic_increment(&SSV::ha_update_count);
1861 1862 1863 1864 1865 1866
  if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_UPDATE)
    table->timestamp_field->set_time();
  return maria_update(file, old_data, new_data);
}


unknown's avatar
unknown committed
1867
int ha_maria::delete_row(const uchar * buf)
1868
{
1869
  ha_statistic_increment(&SSV::ha_delete_count);
1870 1871 1872 1873
  return maria_delete(file, buf);
}


1874 1875 1876
int ha_maria::index_read_map(uchar * buf, const uchar * key,
			     key_part_map keypart_map,
			     enum ha_rkey_function find_flag)
1877 1878
{
  DBUG_ASSERT(inited == INDEX);
1879 1880
  ha_statistic_increment(&SSV::ha_read_key_count);
  int error= maria_rkey(file, buf, active_index, key, keypart_map, find_flag);
1881 1882 1883 1884 1885
  table->status= error ? STATUS_NOT_FOUND : 0;
  return error;
}


1886 1887 1888
int ha_maria::index_read_idx_map(uchar * buf, uint index, const uchar * key,
				 key_part_map keypart_map,
				 enum ha_rkey_function find_flag)
1889
{
1890 1891
  ha_statistic_increment(&SSV::ha_read_key_count);
  int error= maria_rkey(file, buf, index, key, keypart_map, find_flag);
1892 1893 1894 1895 1896
  table->status= error ? STATUS_NOT_FOUND : 0;
  return error;
}


1897 1898
int ha_maria::index_read_last_map(uchar * buf, const uchar * key,
				  key_part_map keypart_map)
1899
{
1900
  DBUG_ENTER("ha_maria::index_read_last_map");
1901
  DBUG_ASSERT(inited == INDEX);
1902 1903
  ha_statistic_increment(&SSV::ha_read_key_count);
  int error= maria_rkey(file, buf, active_index, key, keypart_map,
1904 1905
                        HA_READ_PREFIX_LAST);
  table->status= error ? STATUS_NOT_FOUND : 0;
1906
  DBUG_RETURN(error);
1907 1908 1909
}


unknown's avatar
unknown committed
1910
int ha_maria::index_next(uchar * buf)
1911 1912
{
  DBUG_ASSERT(inited == INDEX);
1913
  ha_statistic_increment(&SSV::ha_read_next_count);
1914 1915 1916 1917 1918 1919
  int error= maria_rnext(file, buf, active_index);
  table->status= error ? STATUS_NOT_FOUND : 0;
  return error;
}


unknown's avatar
unknown committed
1920
int ha_maria::index_prev(uchar * buf)
1921 1922
{
  DBUG_ASSERT(inited == INDEX);
1923
  ha_statistic_increment(&SSV::ha_read_prev_count);
1924 1925 1926 1927 1928 1929
  int error= maria_rprev(file, buf, active_index);
  table->status= error ? STATUS_NOT_FOUND : 0;
  return error;
}


unknown's avatar
unknown committed
1930
int ha_maria::index_first(uchar * buf)
1931 1932
{
  DBUG_ASSERT(inited == INDEX);
1933
  ha_statistic_increment(&SSV::ha_read_first_count);
1934 1935 1936 1937 1938 1939
  int error= maria_rfirst(file, buf, active_index);
  table->status= error ? STATUS_NOT_FOUND : 0;
  return error;
}


unknown's avatar
unknown committed
1940
int ha_maria::index_last(uchar * buf)
1941 1942
{
  DBUG_ASSERT(inited == INDEX);
1943
  ha_statistic_increment(&SSV::ha_read_last_count);
1944 1945 1946 1947 1948 1949
  int error= maria_rlast(file, buf, active_index);
  table->status= error ? STATUS_NOT_FOUND : 0;
  return error;
}


unknown's avatar
unknown committed
1950 1951
int ha_maria::index_next_same(uchar * buf,
                              const uchar *key __attribute__ ((unused)),
1952 1953 1954
                              uint length __attribute__ ((unused)))
{
  DBUG_ASSERT(inited == INDEX);
1955
  ha_statistic_increment(&SSV::ha_read_next_count);
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965
  int error= maria_rnext_same(file, buf);
  table->status= error ? STATUS_NOT_FOUND : 0;
  return error;
}


int ha_maria::rnd_init(bool scan)
{
  if (scan)
    return maria_scan_init(file);
1966
  return maria_reset(file);                        // Free buffers
1967 1968 1969
}


unknown's avatar
unknown committed
1970 1971 1972 1973 1974 1975 1976 1977
int ha_maria::rnd_end()
{
  /* Safe to call even if we don't have started a scan */
  maria_scan_end(file);
  return 0;
}


unknown's avatar
unknown committed
1978
int ha_maria::rnd_next(uchar *buf)
1979
{
1980
  ha_statistic_increment(&SSV::ha_read_rnd_next_count);
1981 1982 1983 1984 1985 1986
  int error= maria_scan(file, buf);
  table->status= error ? STATUS_NOT_FOUND : 0;
  return error;
}


1987
int ha_maria::remember_rnd_pos()
1988
{
1989
  return (*file->s->scan_remember_pos)(file, &remember_pos);
1990 1991 1992
}


1993 1994 1995 1996 1997 1998 1999 2000
int ha_maria::restart_rnd_next(uchar *buf)
{
  (*file->s->scan_restore_pos)(file, remember_pos);
  return rnd_next(buf);
}


int ha_maria::rnd_pos(uchar *buf, uchar *pos)
2001
{
2002
  ha_statistic_increment(&SSV::ha_read_rnd_count);
2003 2004 2005 2006 2007 2008
  int error= maria_rrnd(file, buf, my_get_ptr(pos, ref_length));
  table->status= error ? STATUS_NOT_FOUND : 0;
  return error;
}


2009
void ha_maria::position(const uchar *record)
2010
{
2011 2012
  my_off_t row_position= maria_position(file);
  my_store_ptr(ref, ref_length, row_position);
2013 2014 2015
}


2016
int ha_maria::info(uint flag)
2017
{
2018
  MARIA_INFO maria_info;
2019 2020
  char name_buff[FN_REFLEN];

2021
  (void) maria_status(file, &maria_info, flag);
2022 2023
  if (flag & HA_STATUS_VARIABLE)
  {
2024 2025 2026 2027 2028 2029 2030
    stats.records=           maria_info.records;
    stats.deleted=           maria_info.deleted;
    stats.data_file_length=  maria_info.data_file_length;
    stats.index_file_length= maria_info.index_file_length;
    stats.delete_length=     maria_info.delete_length;
    stats.check_time=        maria_info.check_time;
    stats.mean_rec_length=   maria_info.mean_reclength;
2031 2032 2033 2034
  }
  if (flag & HA_STATUS_CONST)
  {
    TABLE_SHARE *share= table->s;
2035 2036 2037 2038 2039
    stats.max_data_file_length=  maria_info.max_data_file_length;
    stats.max_index_file_length= maria_info.max_index_file_length;
    stats.create_time= maria_info.create_time;
    ref_length= maria_info.reflength;
    share->db_options_in_use= maria_info.options;
2040
    stats.block_size= maria_block_size;
2041 2042 2043 2044 2045

    /* Update share */
    if (share->tmp_table == NO_TMP_TABLE)
      pthread_mutex_lock(&share->mutex);
    share->keys_in_use.set_prefix(share->keys);
2046
    share->keys_in_use.intersect_extended(maria_info.key_map);
2047
    share->keys_for_keyread.intersect(share->keys_in_use);
2048
    share->db_record_offset= maria_info.record_offset;
2049
    if (share->key_parts)
2050 2051 2052 2053 2054 2055
    {
      ulong *to= table->key_info[0].rec_per_key, *end;
      double *from= maria_info.rec_per_key;
      for (end= to+ share->key_parts ; to < end ; to++, from++)
        *to= (ulong) (*from + 0.5);
    }
2056 2057 2058 2059 2060 2061 2062 2063
    if (share->tmp_table == NO_TMP_TABLE)
      pthread_mutex_unlock(&share->mutex);

    /*
       Set data_file_name and index_file_name to point at the symlink value
       if table is symlinked (Ie;  Real name is not same as generated name)
    */
    data_file_name= index_file_name= 0;
2064
    fn_format(name_buff, file->s->open_file_name, "", MARIA_NAME_DEXT,
2065 2066 2067
              MY_APPEND_EXT | MY_UNPACK_FILENAME);
    if (strcmp(name_buff, maria_info.data_file_name))
      data_file_name=maria_info.data_file_name;
2068
    fn_format(name_buff, file->s->open_file_name, "", MARIA_NAME_IEXT,
2069 2070 2071
              MY_APPEND_EXT | MY_UNPACK_FILENAME);
    if (strcmp(name_buff, maria_info.index_file_name))
      index_file_name=maria_info.index_file_name;
2072 2073 2074
  }
  if (flag & HA_STATUS_ERRKEY)
  {
2075 2076
    errkey= maria_info.errkey;
    my_store_ptr(dup_ref, ref_length, maria_info.dup_key_pos);
2077 2078
  }
  /* Faster to always update, than to do it based on flag */
2079 2080
  stats.update_time= maria_info.update_time;
  stats.auto_increment_value= maria_info.auto_increment;
2081 2082

  return 0;
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
}


int ha_maria::extra(enum ha_extra_function operation)
{
  if ((specialflag & SPECIAL_SAFE_MODE) && operation == HA_EXTRA_KEYREAD)
    return 0;
  return maria_extra(file, operation, 0);
}

2093 2094 2095 2096
int ha_maria::reset(void)
{
  return maria_reset(file);
}
2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109

/* To be used with WRITE_CACHE and EXTRA_CACHE */

int ha_maria::extra_opt(enum ha_extra_function operation, ulong cache_size)
{
  if ((specialflag & SPECIAL_SAFE_MODE) && operation == HA_EXTRA_WRITE_CACHE)
    return 0;
  return maria_extra(file, operation, (void*) &cache_size);
}


int ha_maria::delete_all_rows()
{
unknown's avatar
unknown committed
2110 2111 2112 2113 2114 2115 2116 2117 2118 2119
  if (file->s->now_transactional &&
      ((table->in_use->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) ||
       table->in_use->locked_tables))
  {
    /*
      We are not in autocommit mode or user have done LOCK TABLES.
      We must do the delete row by row to be able to rollback the command
    */
    return HA_ERR_WRONG_COMMAND;
  }
2120 2121 2122 2123 2124 2125 2126 2127 2128
  return maria_delete_all_rows(file);
}


int ha_maria::delete_table(const char *name)
{
  return maria_delete_table(name);
}

2129
#define THD_TRN (*(TRN **)thd_ha_data(thd, maria_hton))
2130 2131 2132

int ha_maria::external_lock(THD *thd, int lock_type)
{
2133 2134
  TRN *trn= THD_TRN;
  DBUG_ENTER("ha_maria::external_lock");
unknown's avatar
unknown committed
2135 2136 2137 2138 2139 2140 2141 2142
  /*
    We don't test now_transactional because it may vary between lock/unlock
    and thus confuse our reference counting.
    It is critical to skip non-transactional tables: user-visible temporary
    tables get an external_lock() when read/written for the first time, but no
    corresponding unlock (they just stay locked and are later dropped while
    locked); if a tmp table was transactional, "SELECT FROM non_tmp, tmp"
    would never commit as its "locked_tables" count would stay 1.
unknown's avatar
unknown committed
2143 2144 2145 2146
    When Maria has has_transactions()==TRUE, open_temporary_table()
    (sql_base.cc) will use TRANSACTIONAL_TMP_TABLE and thus the
    external_lock(F_UNLCK) will happen and we can then allow the user to
    create transactional temporary tables.
unknown's avatar
unknown committed
2147 2148
  */
  if (!file->s->base.born_transactional)
2149
    goto skip_transaction;
2150 2151
  if (lock_type != F_UNLCK)
  {
unknown's avatar
unknown committed
2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165
    if (!trn)  /* no transaction yet - open it now */
    {
      trn= trnman_new_trn(& thd->mysys_var->mutex,
                          & thd->mysys_var->suspend,
                          thd->thread_stack + STACK_DIRECTION *
                          (my_thread_stack_size - STACK_MIN_SIZE));
      if (unlikely(!trn))
        DBUG_RETURN(HA_ERR_OUT_OF_MEM);

      DBUG_PRINT("info", ("THD_TRN set to 0x%lx", (ulong)trn));
      THD_TRN= trn;
      if (thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))
        trans_register_ha(thd, TRUE, maria_hton);
    }
unknown's avatar
unknown committed
2166
    file->trn= trn;
unknown's avatar
unknown committed
2167 2168 2169 2170
    if (!trnman_increment_locked_tables(trn))
    {
      trans_register_ha(thd, FALSE, maria_hton);
      trnman_new_statement(trn);
2171
    }
unknown's avatar
unknown committed
2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185
    if (!thd->transaction.on)
    {
      /*
        No need to log REDOs/UNDOs. If this is an internal temporary table
        which will be renamed to a permanent table (like in ALTER TABLE),
        the rename happens after unlocking so will be durable (and the table
        will get its create_rename_lsn).
        Note: if we wanted to enable users to have an old backup and apply
        tons of archived logs to roll-forward, we could then not disable
        REDOs/UNDOs in this case.
      */
      DBUG_PRINT("info", ("Disabling logging for table"));
      _ma_tmp_disable_logging_for_table(file, TRUE);
    }
2186 2187 2188
  }
  else
  {
unknown's avatar
unknown committed
2189 2190
    _ma_reenable_logging_for_table(file);
    /** @todo zero file->trn also in commit and rollback */
unknown's avatar
unknown committed
2191
    file->trn= NULL;
2192 2193 2194 2195 2196
    if (trn && trnman_has_locked_tables(trn))
    {
      if (!trnman_decrement_locked_tables(trn))
      {
        /* autocommit ? rollback a transaction */
unknown's avatar
unknown committed
2197
#ifdef MARIA_CANNOT_ROLLBACK
2198 2199
        if (ma_commit(trn))
          DBUG_RETURN(1);
unknown's avatar
unknown committed
2200 2201
        THD_TRN= 0;
#else
2202 2203 2204 2205 2206 2207
        if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
        {
          trnman_rollback_trn(trn);
          DBUG_PRINT("info", ("THD_TRN set to 0x0"));
          THD_TRN= 0;
        }
unknown's avatar
unknown committed
2208
#endif
2209 2210 2211
      }
    }
  }
2212
skip_transaction:
2213 2214 2215
  DBUG_RETURN(maria_lock_database(file, !table->s->tmp_table ?
                                  lock_type : ((lock_type == F_UNLCK) ?
                                               F_UNLCK : F_EXTRA_LCK)));
2216 2217
}

2218 2219
int ha_maria::start_stmt(THD *thd, thr_lock_type lock_type)
{
unknown's avatar
unknown committed
2220
  TRN *trn;
unknown's avatar
unknown committed
2221
  if (file->s->base.born_transactional)
2222
  {
unknown's avatar
unknown committed
2223
    trn= THD_TRN;
2224
    DBUG_ASSERT(trn); // this may be called only after external_lock()
2225
    DBUG_ASSERT(trnman_has_locked_tables(trn));
2226
    DBUG_ASSERT(lock_type != F_UNLCK);
unknown's avatar
unknown committed
2227 2228 2229 2230 2231 2232 2233
    /*
      If there was an implicit commit under this LOCK TABLES by a previous
      statement (like a DDL), at least if that previous statement was about a
      different ha_maria than 'this' then this->file->trn is a stale
      pointer. We fix it:
    */
    file->trn= trn;
2234 2235 2236 2237 2238 2239 2240
    /*
      As external_lock() was already called, don't increment locked_tables.
      Note that we call the function below possibly several times when
      statement starts (once per table). This is ok as long as that function
      does cheap operations. Otherwise, we will need to do it only on first
      call to start_stmt().
    */
2241 2242 2243 2244
    trnman_new_statement(trn);
  }
  return 0;
}
2245

unknown's avatar
unknown committed
2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288

/**
  Performs an implicit commit of the Maria transaction and creates a new
  one.

  This can be considered a hack. When Maria loses HA_NO_TRANSACTIONS it will
  be participant in the connection's transaction and so the implicit commits
  (ha_commit()) (like in end_active_trans()) will do the implicit commit
  without need to call this function which can then be removed.
*/

int ha_maria::implicit_commit(THD *thd)
{
#ifndef MARIA_CANNOT_ROLLBACK
#error this method should be removed
#endif
  TRN *trn;
  int error= 0;
  DBUG_ENTER("ha_maria::implicit_commit");
  if ((trn= THD_TRN) != NULL)
  {
    uint locked_tables= trnman_has_locked_tables(trn);
    if (unlikely(ma_commit(trn)))
      error= 1;
    /*
      We need to create a new transaction and put it in THD_TRN. Indeed,
      tables may be under LOCK TABLES, and so they will start the next
      statement assuming they have a trn (see ha_maria::start_stmt()).
    */
    trn= trnman_new_trn(& thd->mysys_var->mutex,
                        & thd->mysys_var->suspend,
                        thd->thread_stack + STACK_DIRECTION *
                        (my_thread_stack_size - STACK_MIN_SIZE));
    /* This is just a commit, tables stay locked if they were: */
    trnman_reset_locked_tables(trn, locked_tables);
    THD_TRN= trn;
    if (unlikely(trn == NULL))
      error= HA_ERR_OUT_OF_MEM;
  }
  DBUG_RETURN(error);
}


2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304
THR_LOCK_DATA **ha_maria::store_lock(THD *thd,
                                     THR_LOCK_DATA **to,
                                     enum thr_lock_type lock_type)
{
  if (lock_type != TL_IGNORE && file->lock.type == TL_UNLOCK)
    file->lock.type= lock_type;
  *to++= &file->lock;
  return to;
}


void ha_maria::update_create_info(HA_CREATE_INFO *create_info)
{
  ha_maria::info(HA_STATUS_AUTO | HA_STATUS_CONST);
  if (!(create_info->used_fields & HA_CREATE_USED_AUTO))
  {
2305
    create_info->auto_increment_value= stats.auto_increment_value;
2306 2307 2308
  }
  create_info->data_file_name= data_file_name;
  create_info->index_file_name= index_file_name;
2309 2310 2311 2312
  /* We need to restore the row type as Maria can change it */
  if (create_info->row_type != ROW_TYPE_DEFAULT &&
      !(create_info->used_fields & HA_CREATE_USED_ROW_FORMAT))
    create_info->row_type= get_row_type();
2313 2314 2315 2316 2317 2318
  /*
    Show always page checksums, as this can be forced with
    maria_page_checksums variable
  */
  if (file->s->options & HA_OPTION_PAGE_CHECKSUM)
    create_info->page_checksum= HA_CHOICE_YES;
2319 2320 2321
}


unknown's avatar
unknown committed
2322 2323 2324 2325 2326
enum row_type ha_maria::get_row_type() const
{
  switch (file->s->data_file_type) {
  case STATIC_RECORD:     return ROW_TYPE_FIXED;
  case DYNAMIC_RECORD:    return ROW_TYPE_DYNAMIC;
2327
  case BLOCK_RECORD:      return ROW_TYPE_PAGE;
unknown's avatar
unknown committed
2328 2329 2330 2331 2332 2333
  case COMPRESSED_RECORD: return ROW_TYPE_COMPRESSED;
  default:                return ROW_TYPE_NOT_USED;
  }
}


2334 2335
static enum data_file_type maria_row_type(HA_CREATE_INFO *info,
                                          my_bool ignore_transactional)
unknown's avatar
unknown committed
2336
{
2337
  if (info->transactional == HA_CHOICE_YES && ! ignore_transactional)
2338
    return BLOCK_RECORD;
unknown's avatar
unknown committed
2339 2340 2341 2342 2343 2344 2345 2346
  switch (info->row_type) {
  case ROW_TYPE_FIXED:   return STATIC_RECORD;
  case ROW_TYPE_DYNAMIC: return DYNAMIC_RECORD;
  default:               return BLOCK_RECORD;
  }
}


2347
int ha_maria::create(const char *name, register TABLE *table_arg,
2348
                     HA_CREATE_INFO *ha_create_info)
2349 2350
{
  int error;
2351
  uint create_flags= 0, record_count, i;
2352 2353
  char buff[FN_REFLEN];
  MARIA_KEYDEF *keydef;
2354 2355
  MARIA_COLUMNDEF *recinfo;
  MARIA_CREATE_INFO create_info;
2356 2357
  TABLE_SHARE *share= table_arg->s;
  uint options= share->db_options_in_use;
unknown's avatar
unknown committed
2358
  enum data_file_type row_type;
2359 2360
  DBUG_ENTER("ha_maria::create");

2361
  for (i= 0; i < share->keys; i++)
2362
  {
2363
    if (table_arg->key_info[i].flags & HA_USES_PARSER)
2364
    {
2365
      create_flags|= HA_CREATE_RELIES_ON_SQL_LAYER;
2366 2367 2368
      break;
    }
  }
2369
  /* Note: BLOCK_RECORD is used if table is transactional */
2370
  row_type= maria_row_type(ha_create_info, 0);
2371 2372 2373 2374 2375 2376 2377 2378
  if (ha_create_info->transactional == HA_CHOICE_YES &&
      ha_create_info->row_type != ROW_TYPE_PAGE &&
      ha_create_info->row_type != ROW_TYPE_NOT_USED &&
      ha_create_info->row_type != ROW_TYPE_DEFAULT)
    push_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
                 ER_ILLEGAL_HA_CREATE_OPTION,
                 "Row format set to PAGE because of TRANSACTIONAL=1 option");

2379
  bzero((char*) &create_info, sizeof(create_info));
2380 2381 2382
  if ((error= table2maria(table_arg, row_type, &keydef, &recinfo,
                          &record_count, &create_info)))
    DBUG_RETURN(error); /* purecov: inspected */
2383 2384
  create_info.max_rows= share->max_rows;
  create_info.reloc_rows= share->min_rows;
2385 2386 2387 2388
  create_info.with_auto_increment= share->next_number_key_offset == 0;
  create_info.auto_increment= (ha_create_info->auto_increment_value ?
                               ha_create_info->auto_increment_value -1 :
                               (ulonglong) 0);
2389 2390
  create_info.data_file_length= ((ulonglong) share->max_rows *
                                 share->avg_row_length);
2391 2392
  create_info.data_file_name= ha_create_info->data_file_name;
  create_info.index_file_name= ha_create_info->index_file_name;
2393 2394 2395 2396 2397 2398

  /*
    Table is transactional:
    - If the user specify that table is transactional (in this case
      row type is forced to BLOCK_RECORD)
    - If they specify BLOCK_RECORD without specifying transactional behaviour
2399

unknown's avatar
unknown committed
2400 2401 2402
    Shouldn't this test be pushed down to maria_create()? Because currently,
    ma_test1 -T crashes: it creates a table with DYNAMIC_RECORD but has
    born_transactional==1, which confuses some recovery-related code.
2403
  */
2404 2405
  create_info.transactional= (row_type == BLOCK_RECORD &&
                              ha_create_info->transactional != HA_CHOICE_NO);
2406

2407 2408
  if (ha_create_info->options & HA_LEX_CREATE_TMP_TABLE)
    create_flags|= HA_CREATE_TMP_TABLE;
2409 2410
  if (ha_create_info->options & HA_CREATE_KEEP_FILES)
    create_flags|= HA_CREATE_KEEP_FILES;
2411
  if (options & HA_OPTION_PACK_RECORD)
2412
    create_flags|= HA_PACK_RECORD;
2413
  if (options & HA_OPTION_CHECKSUM)
2414
    create_flags|= HA_CREATE_CHECKSUM;
2415
  if (options & HA_OPTION_DELAY_KEY_WRITE)
2416
    create_flags|= HA_CREATE_DELAY_KEY_WRITE;
2417 2418
  if ((ha_create_info->page_checksum == HA_CHOICE_UNDEF &&
       maria_page_checksums) ||
unknown's avatar
unknown committed
2419
       ha_create_info->page_checksum ==  HA_CHOICE_YES)
2420
    create_flags|= HA_CREATE_PAGE_CHECKSUM;
2421 2422 2423

  /* TODO: Check that the following fn_format is really needed */
  error=
unknown's avatar
unknown committed
2424 2425
    maria_create(fn_format(buff, name, "", "",
                           MY_UNPACK_FILENAME | MY_APPEND_EXT),
2426
                 row_type, share->keys, keydef,
2427
                 record_count,  recinfo,
2428 2429
                 0, (MARIA_UNIQUEDEF *) 0,
                 &create_info, create_flags);
2430

unknown's avatar
unknown committed
2431
  my_free((uchar*) recinfo, MYF(0));
2432 2433 2434 2435 2436 2437 2438 2439 2440 2441
  DBUG_RETURN(error);
}


int ha_maria::rename_table(const char *from, const char *to)
{
  return maria_rename(from, to);
}


2442 2443 2444 2445
void ha_maria::get_auto_increment(ulonglong offset, ulonglong increment,
                                  ulonglong nb_desired_values,
                                  ulonglong *first_value,
                                  ulonglong *nb_reserved_values)
2446 2447 2448
{
  ulonglong nr;
  int error;
unknown's avatar
unknown committed
2449
  uchar key[HA_MAX_KEY_LENGTH];
2450 2451 2452 2453

  if (!table->s->next_number_key_offset)
  {                                             // Autoincrement at key-start
    ha_maria::info(HA_STATUS_AUTO);
2454 2455 2456 2457
    *first_value= stats.auto_increment_value;
    /* Maria has only table-level lock for now, so reserves to +inf */
    *nb_reserved_values= ULONGLONG_MAX;
    return;
2458 2459 2460 2461 2462 2463 2464 2465 2466 2467
  }

  /* it's safe to call the following if bulk_insert isn't on */
  maria_flush_bulk_insert(file, table->s->next_number_index);

  (void) extra(HA_EXTRA_KEYREAD);
  key_copy(key, table->record[0],
           table->key_info + table->s->next_number_index,
           table->s->next_number_key_offset);
  error= maria_rkey(file, table->record[1], (int) table->s->next_number_index,
2468 2469
                    key, make_prev_keypart_map(table->s->next_number_keypart),
                    HA_READ_PREFIX_LAST);
2470 2471 2472 2473 2474 2475 2476 2477 2478
  if (error)
    nr= 1;
  else
  {
    /* Get data from record[1] */
    nr= ((ulonglong) table->next_number_field->
         val_int_offset(table->s->rec_buff_length) + 1);
  }
  extra(HA_EXTRA_NO_KEYREAD);
2479 2480 2481 2482 2483 2484 2485 2486
  *first_value= nr;
  /*
    MySQL needs to call us for next row: assume we are inserting ("a",null)
    here, we return 3, and next this statement will want to insert ("b",null):
    there is no reason why ("b",3+1) would be the good row to insert: maybe it
    already exists, maybe 3+1 is too large...
  */
  *nb_reserved_values= 1;
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521
}


/*
  Find out how many rows there is in the given range

  SYNOPSIS
    records_in_range()
    inx                 Index to use
    min_key             Start of range.  Null pointer if from first key
    max_key             End of range. Null pointer if to last key

  NOTES
    min_key.flag can have one of the following values:
      HA_READ_KEY_EXACT         Include the key in the range
      HA_READ_AFTER_KEY         Don't include key in range

    max_key.flag can have one of the following values:
      HA_READ_BEFORE_KEY        Don't include key in range
      HA_READ_AFTER_KEY         Include all 'end_key' values in the range

  RETURN
   HA_POS_ERROR         Something is wrong with the index tree.
   0                    There is no matching keys in the given range
   number > 0           There is approximately 'number' matching rows in
                        the range.
*/

ha_rows ha_maria::records_in_range(uint inx, key_range *min_key,
                                   key_range *max_key)
{
  return (ha_rows) maria_records_in_range(file, (int) inx, min_key, max_key);
}


unknown's avatar
unknown committed
2522
int ha_maria::ft_read(uchar * buf)
2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544
{
  int error;

  if (!ft_handler)
    return -1;

  thread_safe_increment(table->in_use->status_var.ha_read_next_count,
                        &LOCK_status);  // why ?

  error= ft_handler->please->read_next(ft_handler, (char*) buf);

  table->status= error ? STATUS_NOT_FOUND : 0;
  return error;
}


uint ha_maria::checksum() const
{
  return (uint) file->state->checksum;
}


2545
bool ha_maria::check_if_incompatible_data(HA_CREATE_INFO *create_info,
2546 2547 2548 2549
                                          uint table_changes)
{
  uint options= table->s->db_options_in_use;

2550 2551 2552
  if (create_info->auto_increment_value != stats.auto_increment_value ||
      create_info->data_file_name != data_file_name ||
      create_info->index_file_name != index_file_name ||
2553
      (maria_row_type(create_info,  1) != data_file_type &&
2554
       create_info->row_type != ROW_TYPE_DEFAULT) ||
2555 2556
      table_changes == IS_EQUAL_NO ||
      table_changes & IS_EQUAL_PACK_LENGTH) // Not implemented yet
2557 2558 2559 2560
    return COMPATIBLE_DATA_NO;

  if ((options & (HA_OPTION_PACK_RECORD | HA_OPTION_CHECKSUM |
                  HA_OPTION_DELAY_KEY_WRITE)) !=
2561
      (create_info->table_options & (HA_OPTION_PACK_RECORD | HA_OPTION_CHECKSUM |
2562 2563 2564 2565
                              HA_OPTION_DELAY_KEY_WRITE)))
    return COMPATIBLE_DATA_NO;
  return COMPATIBLE_DATA_YES;
}
unknown's avatar
unknown committed
2566

2567 2568

static int maria_hton_panic(handlerton *hton, ha_panic_function flag)
2569
{
unknown's avatar
unknown committed
2570 2571 2572
  /* If no background checkpoints, we need to do one now */
  return ((checkpoint_interval == 0) ?
          ma_checkpoint_execute(CHECKPOINT_FULL, FALSE) : 0) | maria_panic(flag);
2573
}
2574

2575 2576 2577

static int maria_commit(handlerton *hton __attribute__ ((unused)),
                        THD *thd, bool all)
2578
{
2579 2580
  TRN *trn= THD_TRN;
  DBUG_ENTER("maria_commit");
unknown's avatar
unknown committed
2581
  trnman_reset_locked_tables(trn, 0);
2582 2583 2584 2585 2586
  /* statement or transaction ? */
  if ((thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) && !all)
    DBUG_RETURN(0); // end of statement
  DBUG_PRINT("info", ("THD_TRN set to 0x0"));
  THD_TRN= 0;
2587
  DBUG_RETURN(ma_commit(trn)); // end of transaction
2588
}
2589

2590 2591 2592 2593 2594 2595

static int maria_rollback(handlerton *hton __attribute__ ((unused)),
                          THD *thd, bool all)
{
  TRN *trn= THD_TRN;
  DBUG_ENTER("maria_rollback");
unknown's avatar
unknown committed
2596
  trnman_reset_locked_tables(trn, 0);
2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609
  /* statement or transaction ? */
  if ((thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) && !all)
  {
    trnman_rollback_statement(trn);
    DBUG_RETURN(0); // end of statement
  }
  DBUG_PRINT("info", ("THD_TRN set to 0x0"));
  THD_TRN= 0;
  DBUG_RETURN(trnman_rollback_trn(trn) ?
              HA_ERR_OUT_OF_MEM : 0); // end of transaction
}


2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641

/**
  @brief flush log handler

  @param hton            maria handlerton (unused)

  @retval FALSE OK
  @retval TRUE  Error
*/

bool maria_flush_logs(handlerton *hton)
{
  return test(translog_purge_at_flush());
}


#define SHOW_MSG_LEN (FN_REFLEN + 20)
/**
  @brief show status handler

  @param hton            maria handlerton
  @param thd             thread handler
  @param print           print function
  @param stat            type of status
*/

bool maria_show_status(handlerton *hton,
                       THD *thd,
                       stat_print_fn *print,
                       enum ha_stat_type stat)
{
  char engine_name[]= "maria";
2642
  switch (stat) {
2643
  case HA_ENGINE_LOGS:
2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663
  {
    TRANSLOG_ADDRESS horizon= translog_get_horizon();
    uint32 last_file= LSN_FILE_NO(horizon);
    uint32 first_needed= translog_get_first_needed_file();
    uint32 first_file= translog_get_first_file(horizon);
    uint32 i;
    const char unknown[]= "unknown";
    const char needed[]= "in use";
    const char unneeded[]= "free";
    char path[FN_REFLEN];

    if (first_file == 0)
    {
      const char error[]= "error";
      print(thd, engine_name, sizeof(engine_name),
            STRING_WITH_LEN(""), error, sizeof(error));
      break;
    }

    for (i= first_file; i <= last_file; i++)
2664
    {
2665 2666 2667 2668 2669 2670 2671 2672
      char *file;
      const char *status;
      uint length, status_len;
      MY_STAT stat_buff, *stat;
      const char error[]= "can't stat";
      char object[SHOW_MSG_LEN];
      file= translog_filename_by_fileno(i, path);
      if (!(stat= my_stat(file, &stat_buff, MYF(MY_WME))))
2673
      {
2674 2675
        status= error;
        status_len= sizeof(error);
2676
        length= my_snprintf(object, SHOW_MSG_LEN, "Size unknown ; %s", file);
2677
      }
2678
      else
2679
      {
2680
        if (first_needed == 0)
2681
        {
2682 2683 2684 2685 2686 2687 2688
          status= unknown;
          status_len= sizeof(unknown);
        }
        else if (i < first_needed)
        {
          status= unneeded;
          status_len= sizeof(unneeded);
2689 2690 2691
        }
        else
        {
2692 2693
          status= needed;
          status_len= sizeof(needed);
2694
        }
2695 2696
        length= my_snprintf(object, SHOW_MSG_LEN, "Size %12lu ; %s",
                            (ulong) stat->st_size, file);
2697
      }
2698 2699 2700

      print(thd, engine_name, sizeof(engine_name),
            object, length, status, status_len);
2701
    }
2702 2703
    break;
  }
2704 2705 2706 2707 2708 2709 2710 2711
  case HA_ENGINE_STATUS:
  case HA_ENGINE_MUTEX:
  default:
    break;
  }
  return 0;
}

2712 2713
static int ha_maria_init(void *p)
{
2714
  int res;
2715 2716
  maria_hton= (handlerton *)p;
  maria_hton->state= SHOW_OPTION_YES;
unknown's avatar
unknown committed
2717
  maria_hton->db_type= DB_TYPE_UNKNOWN;
2718
  maria_hton->create= maria_create_handler;
2719 2720 2721
  maria_hton->panic= maria_hton_panic;
  maria_hton->commit= maria_commit;
  maria_hton->rollback= maria_rollback;
2722 2723
  maria_hton->flush_logs= maria_flush_logs;
  maria_hton->show_status= maria_show_status;
2724 2725
  /* TODO: decide if we support Maria being used for log tables */
  maria_hton->flags= HTON_CAN_RECREATE | HTON_SUPPORT_LOG_TABLES;
2726 2727
  bzero(maria_log_pagecache, sizeof(*maria_log_pagecache));
  maria_data_root= mysql_real_data_home;
2728
  maria_tmpdir= &mysql_tmpdir_list;             /* For REDO */
unknown's avatar
unknown committed
2729
  res= maria_init() || ma_control_file_create_or_open() ||
2730
    !init_pagecache(maria_pagecache,
2731
                    (size_t) pagecache_buffer_size, pagecache_division_limit,
2732 2733
                    pagecache_age_threshold, MARIA_KEY_BLOCK_LENGTH, 0) ||
    !init_pagecache(maria_log_pagecache,
2734
                    TRANSLOG_PAGECACHE_SIZE, 0, 0,
2735
                    TRANSLOG_PAGE_SIZE, 0) ||
2736
    translog_init(maria_data_root, log_file_size,
2737
                  MYSQL_VERSION_ID, server_id, maria_log_pagecache,
2738
                  TRANSLOG_DEFAULT_FLAGS, 0) ||
unknown's avatar
unknown committed
2739
    maria_recover() ||
unknown's avatar
unknown committed
2740
    ma_checkpoint_init(checkpoint_interval);
2741
  maria_multi_threaded= TRUE;
2742
  return res ? HA_ERR_INITIALIZATION : 0;
2743 2744
}

2745

2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763
#ifdef HAVE_QUERY_CACHE
/**
  @brief Register a named table with a call back function to the query cache.

  @param thd The thread handle
  @param table_key A pointer to the table name in the table cache
  @param key_length The length of the table name
  @param[out] engine_callback The pointer to the storage engine call back
    function, currently 0
  @param[out] engine_data Engine data will be set to 0.

  @note Despite the name of this function, it is used to check each statement
    before it is cached and not to register a table or callback function.

  @see handler::register_query_cache_table

  @return The error code. The engine_data and engine_callback will be set to 0.
    @retval TRUE Success
unknown's avatar
unknown committed
2764
    @retval FALSE An error occurred
2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820
*/

my_bool ha_maria::register_query_cache_table(THD *thd, char *table_name,
					     uint table_name_len,
					     qc_engine_callback
					     *engine_callback,
					     ulonglong *engine_data)
{
  /*
    No call back function is needed to determine if a cached statement
    is valid or not.
  */
  *engine_callback= 0;

  /*
    No engine data is needed.
  */
  *engine_data= 0;

  /*
    If a concurrent INSERT has happened just before the currently processed
    SELECT statement, the total size of the table is unknown.

    To determine if the table size is known, the current thread's snap shot of
    the table size with the actual table size are compared.

    If the table size is unknown the SELECT statement can't be cached.
  */
  ulonglong actual_data_file_length;
  ulonglong current_data_file_length;

  /*
    POSIX visibility rules specify that "2. Whatever memory values a
    thread can see when it unlocks a mutex <...> can also be seen by any
    thread that later locks the same mutex". In this particular case,
    concurrent insert thread had modified the data_file_length in
    MYISAM_SHARE before it has unlocked (or even locked)
    structure_guard_mutex. So, here we're guaranteed to see at least that
    value after we've locked the same mutex. We can see a later value
    (modified by some other thread) though, but it's ok, as we only want
    to know if the variable was changed, the actual new value doesn't matter
  */
  actual_data_file_length= file->s->state.state.data_file_length;
  current_data_file_length= file->save_state.data_file_length;

  if (current_data_file_length != actual_data_file_length)
  {
    /* Don't cache current statement. */
    return FALSE;
  }

  /* It is ok to try to cache current statement. */
  return TRUE;
}
#endif

2821 2822
static struct st_mysql_sys_var* system_variables[]= {
  MYSQL_SYSVAR(block_size),
unknown's avatar
unknown committed
2823
  MYSQL_SYSVAR(checkpoint_interval),
unknown's avatar
unknown committed
2824
  MYSQL_SYSVAR(page_checksum),
2825 2826
  MYSQL_SYSVAR(log_file_size),
  MYSQL_SYSVAR(log_purge_type),
2827
  MYSQL_SYSVAR(max_sort_file_size),
unknown's avatar
unknown committed
2828 2829 2830
  MYSQL_SYSVAR(pagecache_age_threshold),
  MYSQL_SYSVAR(pagecache_buffer_size),
  MYSQL_SYSVAR(pagecache_division_limit),
2831 2832 2833
  MYSQL_SYSVAR(repair_threads),
  MYSQL_SYSVAR(sort_buffer_size),
  MYSQL_SYSVAR(stats_method),
2834
  MYSQL_SYSVAR(sync_log_dir),
2835 2836 2837 2838
  NULL
};


unknown's avatar
unknown committed
2839
/**
unknown's avatar
unknown committed
2840
   @brief Updates the checkpoint interval and restarts the background thread.
unknown's avatar
unknown committed
2841
*/
unknown's avatar
unknown committed
2842

unknown's avatar
unknown committed
2843
static void update_checkpoint_interval(MYSQL_THD thd,
unknown's avatar
unknown committed
2844 2845 2846
                                        struct st_mysql_sys_var *var,
                                        void *var_ptr, void *save)
{
unknown's avatar
unknown committed
2847 2848
  ma_checkpoint_end();
  ma_checkpoint_init(*(ulong *)var_ptr= (ulong)(*(long *)save));
unknown's avatar
unknown committed
2849 2850
}

2851 2852 2853 2854 2855 2856 2857 2858 2859
/**
   @brief Updates the transaction log file limit.
*/

static void update_log_file_size(MYSQL_THD thd,
                                 struct st_mysql_sys_var *var,
                                 void *var_ptr, void *save)
{
  uint32 size= (uint32)((ulong)(*(long *)save));
unknown's avatar
unknown committed
2860
  translog_set_file_size(size);
2861 2862 2863
  *(ulong *)var_ptr= size;
}

unknown's avatar
unknown committed
2864
static SHOW_VAR status_variables[]= {
2865 2866 2867
  {"Maria_pagecache_blocks_not_flushed", (char*) &maria_pagecache_var.global_blocks_changed, SHOW_LONG_NOFLUSH},
  {"Maria_pagecache_blocks_unused",      (char*) &maria_pagecache_var.blocks_unused, SHOW_LONG_NOFLUSH},
  {"Maria_pagecache_blocks_used",        (char*) &maria_pagecache_var.blocks_used, SHOW_LONG_NOFLUSH},
unknown's avatar
unknown committed
2868 2869 2870 2871 2872 2873
  {"Maria_pagecache_read_requests",      (char*) &maria_pagecache_var.global_cache_r_requests, SHOW_LONGLONG},
  {"Maria_pagecache_reads",              (char*) &maria_pagecache_var.global_cache_read, SHOW_LONGLONG},
  {"Maria_pagecache_write_requests",     (char*) &maria_pagecache_var.global_cache_w_requests, SHOW_LONGLONG},
  {"Maria_pagecache_writes",             (char*) &maria_pagecache_var.global_cache_write, SHOW_LONGLONG},
  {NullS, NullS, SHOW_LONG}
};
2874

2875
struct st_mysql_storage_engine maria_storage_engine=
2876
{ MYSQL_HANDLERTON_INTERFACE_VERSION };
2877

unknown's avatar
unknown committed
2878 2879 2880
mysql_declare_plugin(maria)
{
  MYSQL_STORAGE_ENGINE_PLUGIN,
2881
  &maria_storage_engine,
unknown's avatar
unknown committed
2882
  "MARIA",
unknown's avatar
unknown committed
2883
  "MySQL AB",
2884
  "Traditional transactional MySQL tables",
2885
  PLUGIN_LICENSE_GPL,
2886 2887 2888
  ha_maria_init,              /* Plugin Init                     */
  NULL,                       /* Plugin Deinit                   */
  0x0100,                     /* 1.0                             */
unknown's avatar
unknown committed
2889
  status_variables,           /* status variables                */
2890 2891
  system_variables,           /* system variables                */
  NULL
unknown's avatar
unknown committed
2892 2893
}
mysql_declare_plugin_end;