~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/myisam/ha_myisam.cc

  • Committer: tdavies
  • Date: 2010-10-10 05:31:41 UTC
  • mto: (1827.1.3 trunk-drizzle)
  • mto: This revision was merged to the branch mainline in revision 1829.
  • Revision ID: tdavies@molly-20101010053141-7rbcb8fe8a6xxrn8
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
#include "drizzled/errmsg_print.h"
29
29
#include "drizzled/gettext.h"
30
30
#include "drizzled/session.h"
31
 
#include "drizzled/plugin.h"
 
31
#include "drizzled/set_var.h"
 
32
#include <drizzled/plugin.h>
32
33
#include "drizzled/plugin/client.h"
33
34
#include "drizzled/table.h"
34
35
#include "drizzled/field/timestamp.h"
36
37
#include "drizzled/plugin/daemon.h"
37
38
 
38
39
#include <boost/algorithm/string.hpp>
39
 
#include <boost/scoped_ptr.hpp>
40
40
 
41
41
#include <string>
42
42
#include <sstream>
43
43
#include <map>
44
44
#include <algorithm>
45
 
#include <memory>
46
45
#include <boost/program_options.hpp>
47
46
#include <drizzled/module/option_map.h>
48
47
 
60
59
static uint32_t myisam_key_cache_division_limit;
61
60
static uint32_t myisam_key_cache_age_threshold;
62
61
static uint64_t max_sort_file_size;
63
 
typedef constrained_check<size_t, SIZE_MAX, 1024, 1024> sort_buffer_constraint;
64
 
static sort_buffer_constraint sort_buffer_size;
 
62
static uint64_t sort_buffer_size;
65
63
 
66
64
void st_mi_isam_share::setKeyCache()
67
65
{
106
104
    mi_panic(HA_PANIC_CLOSE);
107
105
  }
108
106
 
109
 
  virtual Cursor *create(Table &table)
 
107
  virtual Cursor *create(TableShare &table)
110
108
  {
111
109
    return new ha_myisam(*this, table);
112
110
  }
144
142
 
145
143
  void doGetTableIdentifiers(drizzled::CachedDirectory &directory,
146
144
                             const drizzled::SchemaIdentifier &schema_identifier,
147
 
                             drizzled::TableIdentifier::vector &set_of_identifiers);
 
145
                             drizzled::TableIdentifiers &set_of_identifiers);
148
146
  bool validateCreateTableOption(const std::string &key, const std::string &state)
149
147
  {
150
148
    (void)state;
159
157
 
160
158
void MyisamEngine::doGetTableIdentifiers(drizzled::CachedDirectory&,
161
159
                                         const drizzled::SchemaIdentifier&,
162
 
                                         drizzled::TableIdentifier::vector&)
 
160
                                         drizzled::TableIdentifiers&)
163
161
{
164
162
}
165
163
 
166
164
bool MyisamEngine::doDoesTableExist(Session &session, const TableIdentifier &identifier)
167
165
{
168
 
  return session.getMessageCache().doesTableMessageExist(identifier);
 
166
  return session.doesTableMessageExist(identifier);
169
167
}
170
168
 
171
169
int MyisamEngine::doGetTableDefinition(Session &session,
172
170
                                       const TableIdentifier &identifier,
173
171
                                       message::Table &table_message)
174
172
{
175
 
  if (session.getMessageCache().getTableMessage(identifier, table_message))
 
173
  if (session.getTableMessage(identifier, table_message))
176
174
    return EEXIST;
177
175
  return ENOENT;
178
176
}
479
477
volatile int *killed_ptr(MI_CHECK *param)
480
478
{
481
479
  /* In theory Unsafe conversion, but should be ok for now */
482
 
  return (int*) (((Session *)(param->session))->getKilledPtr());
 
480
  return (int*) &(((Session *)(param->session))->killed);
483
481
}
484
482
 
485
483
void mi_check_print_error(MI_CHECK *param, const char *fmt,...)
546
544
}
547
545
 
548
546
ha_myisam::ha_myisam(plugin::StorageEngine &engine_arg,
549
 
                     Table &table_arg)
 
547
                     TableShare &table_arg)
550
548
  : Cursor(engine_arg, table_arg),
551
549
  file(0),
552
550
  can_enable_indexes(true),
592
590
  if (!(file= mi_open(identifier, mode, test_if_locked)))
593
591
    return (errno ? errno : -1);
594
592
 
595
 
  if (!getTable()->getShare()->getType()) /* No need to perform a check for tmp table */
 
593
  if (!table->getShare()->getType()) /* No need to perform a check for tmp table */
596
594
  {
597
 
    if ((errno= table2myisam(getTable(), &keyinfo, &recinfo, &recs)))
 
595
    if ((errno= table2myisam(table, &keyinfo, &recinfo, &recs)))
598
596
    {
599
597
      goto err;
600
598
    }
601
 
    if (check_definition(keyinfo, recinfo, getTable()->getShare()->sizeKeys(), recs,
 
599
    if (check_definition(keyinfo, recinfo, table->getShare()->sizeKeys(), recs,
602
600
                         file->s->keyinfo, file->s->rec,
603
601
                         file->s->base.keys, file->s->base.fields, true))
604
602
    {
614
612
  info(HA_STATUS_NO_LOCK | HA_STATUS_VARIABLE | HA_STATUS_CONST);
615
613
  if (!(test_if_locked & HA_OPEN_WAIT_IF_LOCKED))
616
614
    mi_extra(file, HA_EXTRA_WAIT_LOCK, 0);
617
 
  if (!getTable()->getShare()->db_record_offset)
 
615
  if (!table->getShare()->db_record_offset)
618
616
    is_ordered= false;
619
617
 
620
618
 
621
619
  keys_with_parts.reset();
622
 
  for (i= 0; i < getTable()->getShare()->sizeKeys(); i++)
 
620
  for (i= 0; i < table->getShare()->sizeKeys(); i++)
623
621
  {
624
 
    getTable()->key_info[i].block_size= file->s->keyinfo[i].block_length;
 
622
    table->key_info[i].block_size= file->s->keyinfo[i].block_length;
625
623
 
626
 
    KeyPartInfo *kp= getTable()->key_info[i].key_part;
627
 
    KeyPartInfo *kp_end= kp + getTable()->key_info[i].key_parts;
 
624
    KeyPartInfo *kp= table->key_info[i].key_part;
 
625
    KeyPartInfo *kp_end= kp + table->key_info[i].key_parts;
628
626
    for (; kp != kp_end; kp++)
629
627
    {
630
628
      if (!kp->field->part_of_key.test(i))
661
659
    If we have an auto_increment column and we are writing a changed row
662
660
    or a new row, then update the auto_increment value in the record.
663
661
  */
664
 
  if (getTable()->next_number_field && buf == getTable()->getInsertRecord())
 
662
  if (table->next_number_field && buf == table->getInsertRecord())
665
663
  {
666
664
    int error;
667
665
    if ((error= update_auto_increment()))
693
691
  {
694
692
    errmsg_printf(ERRMSG_LVL_INFO, "Retrying repair of: '%s' failed. "
695
693
                          "Please try REPAIR EXTENDED or myisamchk",
696
 
                          getTable()->getShare()->getPath());
 
694
                          table->getShare()->getPath());
697
695
    return(HA_ADMIN_FAILED);
698
696
  }
699
697
 
700
 
  param.db_name=    getTable()->getShare()->getSchemaName();
701
 
  param.table_name= getTable()->getAlias();
 
698
  param.db_name=    table->getShare()->getSchemaName();
 
699
  param.table_name= table->getAlias();
702
700
  param.tmpfile_createflag = O_RDWR | O_TRUNC;
703
701
  param.using_global_keycache = 1;
704
702
  param.session= session;
705
703
  param.out_flag= 0;
706
 
  param.sort_buffer_length= static_cast<size_t>(sort_buffer_size);
 
704
  param.sort_buffer_length= (size_t)sort_buffer_size;
707
705
  strcpy(fixed_name,file->filename);
708
706
 
709
707
  // Don't lock tables if we have used LOCK Table
710
 
  if (mi_lock_database(file, getTable()->getShare()->getType() ? F_EXTRA_LCK : F_WRLCK))
 
708
  if (mi_lock_database(file, table->getShare()->getType() ? F_EXTRA_LCK : F_WRLCK))
711
709
  {
712
710
    mi_check_print_error(&param,ER(ER_CANT_LOCK),errno);
713
711
    return(HA_ADMIN_FAILED);
904
902
  }
905
903
  else if (mode == HA_KEY_SWITCH_NONUNIQ_SAVE)
906
904
  {
907
 
    Session *session= getTable()->in_use;
908
 
    boost::scoped_ptr<MI_CHECK> param_ap(new MI_CHECK);
909
 
    MI_CHECK &param= *param_ap.get();
 
905
    Session *session= table->in_use;
 
906
    MI_CHECK param;
910
907
    const char *save_proc_info= session->get_proc_info();
911
908
    session->set_proc_info("Creating index");
912
909
    myisamchk_init(&param);
914
911
    param.testflag= (T_SILENT | T_REP_BY_SORT | T_QUICK |
915
912
                     T_CREATE_MISSING_KEYS);
916
913
    param.myf_rw&= ~MY_WAIT_IF_FULL;
917
 
    param.sort_buffer_length=  static_cast<size_t>(sort_buffer_size);
 
914
    param.sort_buffer_length=  (size_t)sort_buffer_size;
918
915
    param.stats_method= MI_STATS_METHOD_NULLS_NOT_EQUAL;
919
916
    if ((error= (repair(session,param,0) != HA_ADMIN_OK)) && param.retry_repair)
920
917
    {
981
978
 
982
979
void ha_myisam::start_bulk_insert(ha_rows rows)
983
980
{
984
 
  Session *session= getTable()->in_use;
 
981
  Session *session= table->in_use;
985
982
  ulong size= session->variables.read_buff_size;
986
983
 
987
984
  /* don't enable row cache if too few rows */
1066
1063
  assert(inited==INDEX);
1067
1064
  ha_statistic_increment(&system_status_var::ha_read_key_count);
1068
1065
  int error=mi_rkey(file, buf, active_index, key, keypart_map, find_flag);
1069
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1066
  table->status=error ? STATUS_NOT_FOUND: 0;
1070
1067
  return error;
1071
1068
}
1072
1069
 
1076
1073
{
1077
1074
  ha_statistic_increment(&system_status_var::ha_read_key_count);
1078
1075
  int error=mi_rkey(file, buf, index, key, keypart_map, find_flag);
1079
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1076
  table->status=error ? STATUS_NOT_FOUND: 0;
1080
1077
  return error;
1081
1078
}
1082
1079
 
1087
1084
  ha_statistic_increment(&system_status_var::ha_read_key_count);
1088
1085
  int error=mi_rkey(file, buf, active_index, key, keypart_map,
1089
1086
                    HA_READ_PREFIX_LAST);
1090
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1087
  table->status=error ? STATUS_NOT_FOUND: 0;
1091
1088
  return(error);
1092
1089
}
1093
1090
 
1096
1093
  assert(inited==INDEX);
1097
1094
  ha_statistic_increment(&system_status_var::ha_read_next_count);
1098
1095
  int error=mi_rnext(file,buf,active_index);
1099
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1096
  table->status=error ? STATUS_NOT_FOUND: 0;
1100
1097
  return error;
1101
1098
}
1102
1099
 
1105
1102
  assert(inited==INDEX);
1106
1103
  ha_statistic_increment(&system_status_var::ha_read_prev_count);
1107
1104
  int error=mi_rprev(file,buf, active_index);
1108
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1105
  table->status=error ? STATUS_NOT_FOUND: 0;
1109
1106
  return error;
1110
1107
}
1111
1108
 
1114
1111
  assert(inited==INDEX);
1115
1112
  ha_statistic_increment(&system_status_var::ha_read_first_count);
1116
1113
  int error=mi_rfirst(file, buf, active_index);
1117
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1114
  table->status=error ? STATUS_NOT_FOUND: 0;
1118
1115
  return error;
1119
1116
}
1120
1117
 
1123
1120
  assert(inited==INDEX);
1124
1121
  ha_statistic_increment(&system_status_var::ha_read_last_count);
1125
1122
  int error=mi_rlast(file, buf, active_index);
1126
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1123
  table->status=error ? STATUS_NOT_FOUND: 0;
1127
1124
  return error;
1128
1125
}
1129
1126
 
1138
1135
  {
1139
1136
    error= mi_rnext_same(file,buf);
1140
1137
  } while (error == HA_ERR_RECORD_DELETED);
1141
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1138
  table->status=error ? STATUS_NOT_FOUND: 0;
1142
1139
  return error;
1143
1140
}
1144
1141
 
1179
1176
{
1180
1177
  ha_statistic_increment(&system_status_var::ha_read_rnd_next_count);
1181
1178
  int error=mi_scan(file, buf);
1182
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1179
  table->status=error ? STATUS_NOT_FOUND: 0;
1183
1180
  return error;
1184
1181
}
1185
1182
 
1187
1184
{
1188
1185
  ha_statistic_increment(&system_status_var::ha_read_rnd_count);
1189
1186
  int error=mi_rrnd(file, buf, internal::my_get_ptr(pos,ref_length));
1190
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1187
  table->status=error ? STATUS_NOT_FOUND: 0;
1191
1188
  return error;
1192
1189
}
1193
1190
 
1216
1213
  }
1217
1214
  if (flag & HA_STATUS_CONST)
1218
1215
  {
1219
 
    TableShare *share= getTable()->getMutableShare();
 
1216
    TableShare *share= table->getMutableShare();
1220
1217
    stats.max_data_file_length=  misam_info.max_data_file_length;
1221
1218
    stats.max_index_file_length= misam_info.max_index_file_length;
1222
1219
    stats.create_time= misam_info.create_time;
1273
1270
    share->keys_for_keyread&= share->keys_in_use;
1274
1271
    share->db_record_offset= misam_info.record_offset;
1275
1272
    if (share->key_parts)
1276
 
      memcpy(getTable()->key_info[0].rec_per_key,
 
1273
      memcpy(table->key_info[0].rec_per_key,
1277
1274
             misam_info.rec_per_key,
1278
 
             sizeof(getTable()->key_info[0].rec_per_key)*share->key_parts);
 
1275
             sizeof(table->key_info[0].rec_per_key)*share->key_parts);
1279
1276
    assert(share->getType() != message::Table::STANDARD);
1280
1277
 
1281
1278
   /*
1331
1328
int MyisamEngine::doDropTable(Session &session,
1332
1329
                              const TableIdentifier &identifier)
1333
1330
{
1334
 
  session.getMessageCache().removeTableMessage(identifier);
 
1331
  session.removeTableMessage(identifier);
1335
1332
 
1336
1333
  return mi_delete_table(identifier.getPath().c_str());
1337
1334
}
1340
1337
int ha_myisam::external_lock(Session *session, int lock_type)
1341
1338
{
1342
1339
  file->in_use= session;
1343
 
  return mi_lock_database(file, !getTable()->getShare()->getType() ?
 
1340
  return mi_lock_database(file, !table->getShare()->getType() ?
1344
1341
                          lock_type : ((lock_type == F_UNLCK) ?
1345
1342
                                       F_UNLCK : F_EXTRA_LCK));
1346
1343
}
1387
1384
                   &create_info, create_flags);
1388
1385
  free((unsigned char*) recinfo);
1389
1386
 
1390
 
  session.getMessageCache().storeTableMessage(identifier, create_proto);
 
1387
  session.storeTableMessage(identifier, create_proto);
1391
1388
 
1392
1389
  return error;
1393
1390
}
1395
1392
 
1396
1393
int MyisamEngine::doRenameTable(Session &session, const TableIdentifier &from, const TableIdentifier &to)
1397
1394
{
1398
 
  session.getMessageCache().renameTableMessage(from, to);
 
1395
  session.renameTableMessage(from, to);
1399
1396
 
1400
1397
  return mi_rename(from.getPath().c_str(), to.getPath().c_str());
1401
1398
}
1411
1408
  int error;
1412
1409
  unsigned char key[MI_MAX_KEY_LENGTH];
1413
1410
 
1414
 
  if (!getTable()->getShare()->next_number_key_offset)
 
1411
  if (!table->getShare()->next_number_key_offset)
1415
1412
  {                                             // Autoincrement at key-start
1416
1413
    ha_myisam::info(HA_STATUS_AUTO);
1417
1414
    *first_value= stats.auto_increment_value;
1421
1418
  }
1422
1419
 
1423
1420
  /* it's safe to call the following if bulk_insert isn't on */
1424
 
  mi_flush_bulk_insert(file, getTable()->getShare()->next_number_index);
 
1421
  mi_flush_bulk_insert(file, table->getShare()->next_number_index);
1425
1422
 
1426
1423
  (void) extra(HA_EXTRA_KEYREAD);
1427
 
  key_copy(key, getTable()->getInsertRecord(),
1428
 
           &getTable()->key_info[getTable()->getShare()->next_number_index],
1429
 
           getTable()->getShare()->next_number_key_offset);
1430
 
  error= mi_rkey(file, getTable()->getUpdateRecord(), (int) getTable()->getShare()->next_number_index,
1431
 
                 key, make_prev_keypart_map(getTable()->getShare()->next_number_keypart),
 
1424
  key_copy(key, table->getInsertRecord(),
 
1425
           &table->key_info[table->getShare()->next_number_index],
 
1426
           table->getShare()->next_number_key_offset);
 
1427
  error= mi_rkey(file, table->getUpdateRecord(), (int) table->getShare()->next_number_index,
 
1428
                 key, make_prev_keypart_map(table->getShare()->next_number_keypart),
1432
1429
                 HA_READ_PREFIX_LAST);
1433
1430
  if (error)
1434
1431
    nr= 1;
1435
1432
  else
1436
1433
  {
1437
1434
    /* Get data from getUpdateRecord() */
1438
 
    nr= ((uint64_t) getTable()->next_number_field->
1439
 
         val_int_offset(getTable()->getShare()->rec_buff_length)+1);
 
1435
    nr= ((uint64_t) table->next_number_field->
 
1436
         val_int_offset(table->getShare()->rec_buff_length)+1);
1440
1437
  }
1441
1438
  extra(HA_EXTRA_NO_KEYREAD);
1442
1439
  *first_value= nr;
1487
1484
  return (uint)file->state->checksum;
1488
1485
}
1489
1486
 
 
1487
static MyisamEngine *engine= NULL;
 
1488
 
1490
1489
static int myisam_init(module::Context &context)
1491
1490
1492
 
  context.add(new MyisamEngine(engine_name));
1493
 
  context.registerVariable(new sys_var_constrained_value<size_t>("sort-buffer-size",
1494
 
                                                                 sort_buffer_size));
1495
 
  context.registerVariable(new sys_var_uint64_t_ptr("max_sort_file_size",
1496
 
                                                    &max_sort_file_size,
1497
 
                                                    context.getOptions()["max-sort-file-size"].as<uint64_t>()));
 
1491
  const module::option_map &vm= context.getOptions();
 
1492
 
 
1493
  if (vm.count("max-sort-file-size"))
 
1494
  {
 
1495
    if (max_sort_file_size > UINT64_MAX)
 
1496
    {
 
1497
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for max-sort-file-size\n"));
 
1498
      exit(-1);
 
1499
    }
 
1500
  }
 
1501
 
 
1502
  if (vm.count("sort-buffer-size"))
 
1503
  {
 
1504
    if (sort_buffer_size < 1024 || sort_buffer_size > SIZE_MAX)
 
1505
    {
 
1506
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for sort-buffer-size\n"));
 
1507
      exit(-1);
 
1508
    }
 
1509
  }
 
1510
 
 
1511
  engine= new MyisamEngine(engine_name);
 
1512
  context.add(engine);
1498
1513
 
1499
1514
  return 0;
1500
1515
}
1501
1516
 
1502
1517
 
 
1518
static DRIZZLE_SYSVAR_ULONGLONG(max_sort_file_size, max_sort_file_size,
 
1519
                                PLUGIN_VAR_RQCMDARG,
 
1520
                                N_("Don't use the fast sort index method to created index if the temporary file would get bigger than this."),
 
1521
                                NULL, NULL, INT32_MAX, 0, UINT64_MAX, 0);
 
1522
 
 
1523
static DRIZZLE_SYSVAR_ULONGLONG(sort_buffer_size, sort_buffer_size,
 
1524
                                PLUGIN_VAR_RQCMDARG,
 
1525
                                N_("The buffer that is allocated when sorting the index when doing a REPAIR or when creating indexes with CREATE INDEX or ALTER TABLE."),
 
1526
                                NULL, NULL, 8192*1024, 1024, SIZE_MAX, 0);
 
1527
 
1503
1528
static void init_options(drizzled::module::option_context &context)
1504
1529
{
1505
1530
  context("max-sort-file-size",
1506
1531
          po::value<uint64_t>(&max_sort_file_size)->default_value(INT32_MAX),
1507
1532
          N_("Don't use the fast sort index method to created index if the temporary file would get bigger than this."));
1508
1533
  context("sort-buffer-size",
1509
 
          po::value<sort_buffer_constraint>(&sort_buffer_size)->default_value(8192*1024),
 
1534
          po::value<uint64_t>(&sort_buffer_size)->default_value(8192*1024),
1510
1535
          N_("The buffer that is allocated when sorting the index when doing a REPAIR or when creating indexes with CREATE INDEX or ALTER TABLE."));
1511
1536
}
1512
1537
 
 
1538
static drizzle_sys_var* sys_variables[]= {
 
1539
  DRIZZLE_SYSVAR(max_sort_file_size),
 
1540
  DRIZZLE_SYSVAR(sort_buffer_size),
 
1541
  NULL
 
1542
};
 
1543
 
1513
1544
 
1514
1545
DRIZZLE_DECLARE_PLUGIN
1515
1546
{
1520
1551
  "Default engine as of MySQL 3.23 with great performance",
1521
1552
  PLUGIN_LICENSE_GPL,
1522
1553
  myisam_init, /* Plugin Init */
1523
 
  NULL,           /* system variables */
 
1554
  sys_variables,           /* system variables */
1524
1555
  init_options                        /* config options                  */
1525
1556
}
1526
1557
DRIZZLE_DECLARE_PLUGIN_END;