~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/myisam/ha_myisam.cc

Merge Joe, plus I updated the tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
 
17
17
 
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"
 
35
#include "drizzled/field/timestamp.h"
34
36
#include "drizzled/memory/multi_malloc.h"
35
37
#include "drizzled/plugin/daemon.h"
36
38
 
37
 
#include <drizzled/plugin/storage_engine.h>
38
 
 
39
39
#include <boost/algorithm/string.hpp>
40
 
#include <boost/scoped_ptr.hpp>
41
40
 
42
41
#include <string>
43
42
#include <sstream>
44
43
#include <map>
45
44
#include <algorithm>
46
 
#include <memory>
47
 
#include <boost/program_options.hpp>
48
 
#include <drizzled/module/option_map.h>
49
 
 
50
 
namespace po= boost::program_options;
51
45
 
52
46
using namespace std;
53
47
using namespace drizzled;
54
48
 
 
49
extern pthread_mutex_t LOCK_global_system_variables;
55
50
static const string engine_name("MyISAM");
56
51
 
57
 
boost::mutex THR_LOCK_myisam;
 
52
pthread_mutex_t THR_LOCK_myisam= PTHREAD_MUTEX_INITIALIZER;
58
53
 
59
 
static uint32_t myisam_key_cache_block_size= KEY_CACHE_BLOCK_SIZE;
 
54
static uint32_t repair_threads;
 
55
static uint32_t myisam_key_cache_block_size;
60
56
static uint32_t myisam_key_cache_size;
61
57
static uint32_t myisam_key_cache_division_limit;
62
58
static uint32_t myisam_key_cache_age_threshold;
63
59
static uint64_t max_sort_file_size;
64
 
typedef constrained_check<size_t, SIZE_MAX, 1024, 1024> sort_buffer_constraint;
65
 
static sort_buffer_constraint sort_buffer_size;
66
 
 
67
 
void st_mi_isam_share::setKeyCache()
68
 
{
69
 
  (void)init_key_cache(&key_cache,
70
 
                       myisam_key_cache_block_size,
71
 
                       myisam_key_cache_size,
72
 
                       myisam_key_cache_division_limit, 
73
 
                       myisam_key_cache_age_threshold);
74
 
}
 
60
static uint64_t sort_buffer_size;
75
61
 
76
62
/*****************************************************************************
77
63
** MyISAM tables
100
86
                          HTON_AUTO_PART_KEY |
101
87
                          HTON_SKIP_STORE_LOCK)
102
88
  {
 
89
    pthread_mutex_init(&THR_LOCK_myisam,MY_MUTEX_INIT_FAST);
103
90
  }
104
91
 
105
92
  virtual ~MyisamEngine()
106
93
  { 
 
94
    pthread_mutex_destroy(&THR_LOCK_myisam);
 
95
    end_key_cache(dflt_key_cache, 1);           // Can never fail
 
96
 
107
97
    mi_panic(HA_PANIC_CLOSE);
108
98
  }
109
99
 
110
 
  virtual Cursor *create(Table &table)
 
100
  virtual Cursor *create(TableShare &table,
 
101
                         memory::Root *mem_root)
111
102
  {
112
 
    return new ha_myisam(*this, table);
 
103
    return new (mem_root) ha_myisam(*this, table);
113
104
  }
114
105
 
115
106
  const char **bas_ext() const {
118
109
 
119
110
  int doCreateTable(Session&,
120
111
                    Table& table_arg,
121
 
                    const identifier::Table &identifier,
 
112
                    const TableIdentifier &identifier,
122
113
                    message::Table&);
123
114
 
124
 
  int doRenameTable(Session&, const identifier::Table &from, const identifier::Table &to);
 
115
  int doRenameTable(Session&, const TableIdentifier &from, const TableIdentifier &to);
125
116
 
126
 
  int doDropTable(Session&, const identifier::Table &identifier);
 
117
  int doDropTable(Session&, const TableIdentifier &identifier);
127
118
 
128
119
  int doGetTableDefinition(Session& session,
129
 
                           const identifier::Table &identifier,
 
120
                           const TableIdentifier &identifier,
130
121
                           message::Table &table_message);
131
122
 
 
123
  /* Temp only engine, so do not return values. */
 
124
  void doGetTableNames(CachedDirectory &, const SchemaIdentifier &, set<string>&) { };
 
125
 
132
126
  uint32_t max_supported_keys()          const { return MI_MAX_KEY; }
133
127
  uint32_t max_supported_key_length()    const { return MI_MAX_KEY_LENGTH; }
134
128
  uint32_t max_supported_key_part_length() const { return MI_MAX_KEY_LENGTH; }
141
135
            HA_READ_ORDER |
142
136
            HA_KEYREAD_ONLY);
143
137
  }
144
 
  bool doDoesTableExist(Session& session, const identifier::Table &identifier);
 
138
  bool doDoesTableExist(Session& session, const TableIdentifier &identifier);
145
139
 
146
140
  void doGetTableIdentifiers(drizzled::CachedDirectory &directory,
147
 
                             const drizzled::identifier::Schema &schema_identifier,
148
 
                             drizzled::identifier::Table::vector &set_of_identifiers);
 
141
                             const drizzled::SchemaIdentifier &schema_identifier,
 
142
                             drizzled::TableIdentifiers &set_of_identifiers);
149
143
  bool validateCreateTableOption(const std::string &key, const std::string &state)
150
144
  {
151
145
    (void)state;
159
153
};
160
154
 
161
155
void MyisamEngine::doGetTableIdentifiers(drizzled::CachedDirectory&,
162
 
                                         const drizzled::identifier::Schema&,
163
 
                                         drizzled::identifier::Table::vector&)
 
156
                                         const drizzled::SchemaIdentifier&,
 
157
                                         drizzled::TableIdentifiers&)
164
158
{
165
159
}
166
160
 
167
 
bool MyisamEngine::doDoesTableExist(Session &session, const identifier::Table &identifier)
 
161
bool MyisamEngine::doDoesTableExist(Session &session, const TableIdentifier &identifier)
168
162
{
169
 
  return session.getMessageCache().doesTableMessageExist(identifier);
 
163
  return session.doesTableMessageExist(identifier);
170
164
}
171
165
 
172
166
int MyisamEngine::doGetTableDefinition(Session &session,
173
 
                                       const identifier::Table &identifier,
 
167
                                       const TableIdentifier &identifier,
174
168
                                       message::Table &table_message)
175
169
{
176
 
  if (session.getMessageCache().getTableMessage(identifier, table_message))
 
170
  if (session.getTableMessage(identifier, table_message))
177
171
    return EEXIST;
178
172
  return ENOENT;
179
173
}
274
268
      {
275
269
        keydef[i].seg[j].null_bit= field->null_bit;
276
270
        keydef[i].seg[j].null_pos= (uint) (field->null_ptr-
277
 
                                           (unsigned char*) table_arg->getInsertRecord());
 
271
                                           (unsigned char*) table_arg->record[0]);
278
272
      }
279
273
      else
280
274
      {
286
280
        keydef[i].seg[j].flag|= HA_BLOB_PART;
287
281
        /* save number of bytes used to pack length */
288
282
        keydef[i].seg[j].bit_start= (uint) (field->pack_length() -
289
 
                                            share->sizeBlobPtr());
 
283
                                            share->blob_ptr_size);
290
284
      }
291
285
    }
292
286
    keyseg+= pos->key_parts;
293
287
  }
294
288
  if (table_arg->found_next_number_field)
295
289
    keydef[share->next_number_index].flag|= HA_AUTO_KEY;
296
 
  record= table_arg->getInsertRecord();
 
290
  record= table_arg->record[0];
297
291
  recpos= 0;
298
292
  recinfo_pos= recinfo;
299
 
 
300
 
  while (recpos < (uint) share->sizeStoredRecord())
 
293
  while (recpos < (uint) share->stored_rec_length)
301
294
  {
302
295
    Field **field, *found= 0;
303
296
    minpos= share->getRecordLength();
311
304
        /* skip null fields */
312
305
        if (!(temp_length= (*field)->pack_length_in_rec()))
313
306
          continue; /* Skip null-fields */
314
 
 
315
307
        if (! found || fieldpos < minpos ||
316
308
            (fieldpos == minpos && temp_length < length))
317
309
        {
344
336
    {
345
337
      recinfo_pos->null_bit= found->null_bit;
346
338
      recinfo_pos->null_pos= (uint) (found->null_ptr -
347
 
                                     (unsigned char*) table_arg->getInsertRecord());
 
339
                                     (unsigned char*) table_arg->record[0]);
348
340
    }
349
341
    else
350
342
    {
482
474
volatile int *killed_ptr(MI_CHECK *param)
483
475
{
484
476
  /* In theory Unsafe conversion, but should be ok for now */
485
 
  return (int*) (((Session *)(param->session))->getKilledPtr());
 
477
  return (int*) &(((Session *)(param->session))->killed);
486
478
}
487
479
 
488
480
void mi_check_print_error(MI_CHECK *param, const char *fmt,...)
532
524
                        const char *sfile, uint32_t sline)
533
525
{
534
526
  Session *cur_session;
 
527
  pthread_mutex_lock(&file->s->intern_lock);
535
528
  if ((cur_session= file->in_use))
536
 
  {
537
 
    errmsg_printf(error::ERROR, _("Got an error from thread_id=%"PRIu64", %s:%d"),
 
529
    errmsg_printf(ERRMSG_LVL_ERROR, _("Got an error from thread_id=%"PRIu64", %s:%d"),
538
530
                    cur_session->thread_id,
539
531
                    sfile, sline);
540
 
  }
541
532
  else
542
 
  {
543
 
    errmsg_printf(error::ERROR, _("Got an error from unknown thread, %s:%d"), sfile, sline);
544
 
  }
545
 
 
 
533
    errmsg_printf(ERRMSG_LVL_ERROR, _("Got an error from unknown thread, %s:%d"), sfile, sline);
546
534
  if (message)
547
 
    errmsg_printf(error::ERROR, "%s", message);
548
 
 
 
535
    errmsg_printf(ERRMSG_LVL_ERROR, "%s", message);
549
536
  list<Session *>::iterator it= file->s->in_use->begin();
550
537
  while (it != file->s->in_use->end())
551
538
  {
552
 
    errmsg_printf(error::ERROR, "%s", _("Unknown thread accessing table"));
 
539
    errmsg_printf(ERRMSG_LVL_ERROR, "%s", _("Unknown thread accessing table"));
553
540
    ++it;
554
541
  }
 
542
  pthread_mutex_unlock(&file->s->intern_lock);
555
543
}
556
544
 
557
545
ha_myisam::ha_myisam(plugin::StorageEngine &engine_arg,
558
 
                     Table &table_arg)
 
546
                     TableShare &table_arg)
559
547
  : Cursor(engine_arg, table_arg),
560
548
  file(0),
561
549
  can_enable_indexes(true),
576
564
}
577
565
 
578
566
/* Name is here without an extension */
579
 
int ha_myisam::doOpen(const drizzled::identifier::Table &identifier, int mode, uint32_t test_if_locked)
 
567
int ha_myisam::open(const char *name, int mode, uint32_t test_if_locked)
580
568
{
581
569
  MI_KEYDEF *keyinfo;
582
570
  MI_COLUMNDEF *recinfo= 0;
598
586
    open of a table that is in use by other threads already (if the
599
587
    MyISAM share exists already).
600
588
  */
601
 
  if (!(file= mi_open(identifier, mode, test_if_locked)))
 
589
  if (!(file=mi_open(name, mode, test_if_locked)))
602
590
    return (errno ? errno : -1);
603
591
 
604
 
  if (!getTable()->getShare()->getType()) /* No need to perform a check for tmp table */
 
592
  if (!table->getShare()->getType()) /* No need to perform a check for tmp table */
605
593
  {
606
 
    if ((errno= table2myisam(getTable(), &keyinfo, &recinfo, &recs)))
 
594
    if ((errno= table2myisam(table, &keyinfo, &recinfo, &recs)))
607
595
    {
608
596
      goto err;
609
597
    }
610
 
    if (check_definition(keyinfo, recinfo, getTable()->getShare()->sizeKeys(), recs,
 
598
    if (check_definition(keyinfo, recinfo, table->getShare()->sizeKeys(), recs,
611
599
                         file->s->keyinfo, file->s->rec,
612
600
                         file->s->base.keys, file->s->base.fields, true))
613
601
    {
623
611
  info(HA_STATUS_NO_LOCK | HA_STATUS_VARIABLE | HA_STATUS_CONST);
624
612
  if (!(test_if_locked & HA_OPEN_WAIT_IF_LOCKED))
625
613
    mi_extra(file, HA_EXTRA_WAIT_LOCK, 0);
626
 
  if (!getTable()->getShare()->db_record_offset)
 
614
  if (!table->getShare()->db_record_offset)
627
615
    is_ordered= false;
628
616
 
629
617
 
630
618
  keys_with_parts.reset();
631
 
  for (i= 0; i < getTable()->getShare()->sizeKeys(); i++)
 
619
  for (i= 0; i < table->getShare()->sizeKeys(); i++)
632
620
  {
633
 
    getTable()->key_info[i].block_size= file->s->keyinfo[i].block_length;
 
621
    table->key_info[i].block_size= file->s->keyinfo[i].block_length;
634
622
 
635
 
    KeyPartInfo *kp= getTable()->key_info[i].key_part;
636
 
    KeyPartInfo *kp_end= kp + getTable()->key_info[i].key_parts;
 
623
    KeyPartInfo *kp= table->key_info[i].key_part;
 
624
    KeyPartInfo *kp_end= kp + table->key_info[i].key_parts;
637
625
    for (; kp != kp_end; kp++)
638
626
    {
639
627
      if (!kp->field->part_of_key.test(i))
670
658
    If we have an auto_increment column and we are writing a changed row
671
659
    or a new row, then update the auto_increment value in the record.
672
660
  */
673
 
  if (getTable()->next_number_field && buf == getTable()->getInsertRecord())
 
661
  if (table->next_number_field && buf == table->record[0])
674
662
  {
675
663
    int error;
676
664
    if ((error= update_auto_increment()))
700
688
  */
701
689
  if (file->dfile == -1)
702
690
  {
703
 
    errmsg_printf(error::INFO, "Retrying repair of: '%s' failed. "
704
 
                  "Please try REPAIR EXTENDED or myisamchk",
705
 
                  getTable()->getShare()->getPath());
 
691
    errmsg_printf(ERRMSG_LVL_INFO, "Retrying repair of: '%s' failed. "
 
692
                          "Please try REPAIR EXTENDED or myisamchk",
 
693
                          table->getShare()->getPath());
706
694
    return(HA_ADMIN_FAILED);
707
695
  }
708
696
 
709
 
  param.db_name=    getTable()->getShare()->getSchemaName();
710
 
  param.table_name= getTable()->getAlias();
 
697
  param.db_name=    table->getShare()->getSchemaName();
 
698
  param.table_name= table->alias;
711
699
  param.tmpfile_createflag = O_RDWR | O_TRUNC;
712
700
  param.using_global_keycache = 1;
713
701
  param.session= session;
714
702
  param.out_flag= 0;
715
 
  param.sort_buffer_length= static_cast<size_t>(sort_buffer_size);
 
703
  param.sort_buffer_length= (size_t)sort_buffer_size;
716
704
  strcpy(fixed_name,file->filename);
717
705
 
718
706
  // Don't lock tables if we have used LOCK Table
719
 
  if (mi_lock_database(file, getTable()->getShare()->getType() ? F_EXTRA_LCK : F_WRLCK))
 
707
  if (mi_lock_database(file, table->getShare()->getType() ? F_EXTRA_LCK : F_WRLCK))
720
708
  {
721
709
    mi_check_print_error(&param,ER(ER_CANT_LOCK),errno);
722
710
    return(HA_ADMIN_FAILED);
913
901
  }
914
902
  else if (mode == HA_KEY_SWITCH_NONUNIQ_SAVE)
915
903
  {
916
 
    Session *session= getTable()->in_use;
917
 
    boost::scoped_ptr<MI_CHECK> param_ap(new MI_CHECK);
918
 
    MI_CHECK &param= *param_ap.get();
 
904
    Session *session= table->in_use;
 
905
    MI_CHECK param;
919
906
    const char *save_proc_info= session->get_proc_info();
920
907
    session->set_proc_info("Creating index");
921
908
    myisamchk_init(&param);
923
910
    param.testflag= (T_SILENT | T_REP_BY_SORT | T_QUICK |
924
911
                     T_CREATE_MISSING_KEYS);
925
912
    param.myf_rw&= ~MY_WAIT_IF_FULL;
926
 
    param.sort_buffer_length=  static_cast<size_t>(sort_buffer_size);
 
913
    param.sort_buffer_length=  (size_t)sort_buffer_size;
927
914
    param.stats_method= MI_STATS_METHOD_NULLS_NOT_EQUAL;
928
915
    if ((error= (repair(session,param,0) != HA_ADMIN_OK)) && param.retry_repair)
929
916
    {
930
 
      errmsg_printf(error::WARN, "Warning: Enabling keys got errno %d on %s.%s, retrying",
 
917
      errmsg_printf(ERRMSG_LVL_WARN, "Warning: Enabling keys got errno %d on %s.%s, retrying",
931
918
                        errno, param.db_name, param.table_name);
932
919
      /* Repairing by sort failed. Now try standard repair method. */
933
920
      param.testflag&= ~(T_REP_BY_SORT | T_QUICK);
937
924
        might have been set by the first repair. They can still be seen
938
925
        with SHOW WARNINGS then.
939
926
      */
940
 
      if (not error)
 
927
      if (! error)
941
928
        session->clear_error();
942
929
    }
943
930
    info(HA_STATUS_CONST);
990
977
 
991
978
void ha_myisam::start_bulk_insert(ha_rows rows)
992
979
{
993
 
  Session *session= getTable()->in_use;
 
980
  Session *session= table->in_use;
994
981
  ulong size= session->variables.read_buff_size;
995
982
 
996
983
  /* don't enable row cache if too few rows */
1075
1062
  assert(inited==INDEX);
1076
1063
  ha_statistic_increment(&system_status_var::ha_read_key_count);
1077
1064
  int error=mi_rkey(file, buf, active_index, key, keypart_map, find_flag);
1078
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1065
  table->status=error ? STATUS_NOT_FOUND: 0;
1079
1066
  return error;
1080
1067
}
1081
1068
 
1085
1072
{
1086
1073
  ha_statistic_increment(&system_status_var::ha_read_key_count);
1087
1074
  int error=mi_rkey(file, buf, index, key, keypart_map, find_flag);
1088
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1075
  table->status=error ? STATUS_NOT_FOUND: 0;
1089
1076
  return error;
1090
1077
}
1091
1078
 
1096
1083
  ha_statistic_increment(&system_status_var::ha_read_key_count);
1097
1084
  int error=mi_rkey(file, buf, active_index, key, keypart_map,
1098
1085
                    HA_READ_PREFIX_LAST);
1099
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1086
  table->status=error ? STATUS_NOT_FOUND: 0;
1100
1087
  return(error);
1101
1088
}
1102
1089
 
1105
1092
  assert(inited==INDEX);
1106
1093
  ha_statistic_increment(&system_status_var::ha_read_next_count);
1107
1094
  int error=mi_rnext(file,buf,active_index);
1108
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1095
  table->status=error ? STATUS_NOT_FOUND: 0;
1109
1096
  return error;
1110
1097
}
1111
1098
 
1114
1101
  assert(inited==INDEX);
1115
1102
  ha_statistic_increment(&system_status_var::ha_read_prev_count);
1116
1103
  int error=mi_rprev(file,buf, active_index);
1117
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1104
  table->status=error ? STATUS_NOT_FOUND: 0;
1118
1105
  return error;
1119
1106
}
1120
1107
 
1123
1110
  assert(inited==INDEX);
1124
1111
  ha_statistic_increment(&system_status_var::ha_read_first_count);
1125
1112
  int error=mi_rfirst(file, buf, active_index);
1126
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1113
  table->status=error ? STATUS_NOT_FOUND: 0;
1127
1114
  return error;
1128
1115
}
1129
1116
 
1132
1119
  assert(inited==INDEX);
1133
1120
  ha_statistic_increment(&system_status_var::ha_read_last_count);
1134
1121
  int error=mi_rlast(file, buf, active_index);
1135
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1122
  table->status=error ? STATUS_NOT_FOUND: 0;
1136
1123
  return error;
1137
1124
}
1138
1125
 
1147
1134
  {
1148
1135
    error= mi_rnext_same(file,buf);
1149
1136
  } while (error == HA_ERR_RECORD_DELETED);
1150
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1137
  table->status=error ? STATUS_NOT_FOUND: 0;
1151
1138
  return error;
1152
1139
}
1153
1140
 
1188
1175
{
1189
1176
  ha_statistic_increment(&system_status_var::ha_read_rnd_next_count);
1190
1177
  int error=mi_scan(file, buf);
1191
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1178
  table->status=error ? STATUS_NOT_FOUND: 0;
1192
1179
  return error;
1193
1180
}
1194
1181
 
1196
1183
{
1197
1184
  ha_statistic_increment(&system_status_var::ha_read_rnd_count);
1198
1185
  int error=mi_rrnd(file, buf, internal::my_get_ptr(pos,ref_length));
1199
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1186
  table->status=error ? STATUS_NOT_FOUND: 0;
1200
1187
  return error;
1201
1188
}
1202
1189
 
1225
1212
  }
1226
1213
  if (flag & HA_STATUS_CONST)
1227
1214
  {
1228
 
    TableShare *share= getTable()->getMutableShare();
 
1215
    TableShare *share= table->getMutableShare();
1229
1216
    stats.max_data_file_length=  misam_info.max_data_file_length;
1230
1217
    stats.max_index_file_length= misam_info.max_index_file_length;
1231
1218
    stats.create_time= misam_info.create_time;
1233
1220
    share->db_options_in_use= misam_info.options;
1234
1221
    stats.block_size= myisam_key_cache_block_size;        /* record block size */
1235
1222
 
 
1223
    /* Update share */
 
1224
    if (share->getType() == message::Table::STANDARD)
 
1225
      pthread_mutex_lock(&share->mutex);
1236
1226
    set_prefix(share->keys_in_use, share->sizeKeys());
1237
1227
    /*
1238
1228
     * Due to bug 394932 (32-bit solaris build failure), we need
1282
1272
    share->keys_for_keyread&= share->keys_in_use;
1283
1273
    share->db_record_offset= misam_info.record_offset;
1284
1274
    if (share->key_parts)
1285
 
      memcpy(getTable()->key_info[0].rec_per_key,
 
1275
      memcpy(table->key_info[0].rec_per_key,
1286
1276
             misam_info.rec_per_key,
1287
 
             sizeof(getTable()->key_info[0].rec_per_key)*share->key_parts);
1288
 
    assert(share->getType() != message::Table::STANDARD);
 
1277
             sizeof(table->key_info[0].rec_per_key)*share->key_parts);
 
1278
    if (share->getType() == message::Table::STANDARD)
 
1279
      pthread_mutex_unlock(&share->mutex);
1289
1280
 
1290
1281
   /*
1291
1282
     Set data_file_name and index_file_name to point at the symlink value
1338
1329
}
1339
1330
 
1340
1331
int MyisamEngine::doDropTable(Session &session,
1341
 
                              const identifier::Table &identifier)
 
1332
                              const TableIdentifier &identifier)
1342
1333
{
1343
 
  session.getMessageCache().removeTableMessage(identifier);
 
1334
  session.removeTableMessage(identifier);
1344
1335
 
1345
1336
  return mi_delete_table(identifier.getPath().c_str());
1346
1337
}
1349
1340
int ha_myisam::external_lock(Session *session, int lock_type)
1350
1341
{
1351
1342
  file->in_use= session;
1352
 
  return mi_lock_database(file, !getTable()->getShare()->getType() ?
 
1343
  return mi_lock_database(file, !table->getShare()->getType() ?
1353
1344
                          lock_type : ((lock_type == F_UNLCK) ?
1354
1345
                                       F_UNLCK : F_EXTRA_LCK));
1355
1346
}
1356
1347
 
1357
1348
int MyisamEngine::doCreateTable(Session &session,
1358
1349
                                Table& table_arg,
1359
 
                                const identifier::Table &identifier,
 
1350
                                const TableIdentifier &identifier,
1360
1351
                                message::Table& create_proto)
1361
1352
{
1362
1353
  int error;
1396
1387
                   &create_info, create_flags);
1397
1388
  free((unsigned char*) recinfo);
1398
1389
 
1399
 
  session.getMessageCache().storeTableMessage(identifier, create_proto);
 
1390
  session.storeTableMessage(identifier, create_proto);
1400
1391
 
1401
1392
  return error;
1402
1393
}
1403
1394
 
1404
1395
 
1405
 
int MyisamEngine::doRenameTable(Session &session, const identifier::Table &from, const identifier::Table &to)
 
1396
int MyisamEngine::doRenameTable(Session &session, const TableIdentifier &from, const TableIdentifier &to)
1406
1397
{
1407
 
  session.getMessageCache().renameTableMessage(from, to);
 
1398
  session.renameTableMessage(from, to);
1408
1399
 
1409
1400
  return mi_rename(from.getPath().c_str(), to.getPath().c_str());
1410
1401
}
1420
1411
  int error;
1421
1412
  unsigned char key[MI_MAX_KEY_LENGTH];
1422
1413
 
1423
 
  if (!getTable()->getShare()->next_number_key_offset)
 
1414
  if (!table->getShare()->next_number_key_offset)
1424
1415
  {                                             // Autoincrement at key-start
1425
1416
    ha_myisam::info(HA_STATUS_AUTO);
1426
1417
    *first_value= stats.auto_increment_value;
1430
1421
  }
1431
1422
 
1432
1423
  /* it's safe to call the following if bulk_insert isn't on */
1433
 
  mi_flush_bulk_insert(file, getTable()->getShare()->next_number_index);
 
1424
  mi_flush_bulk_insert(file, table->getShare()->next_number_index);
1434
1425
 
1435
1426
  (void) extra(HA_EXTRA_KEYREAD);
1436
 
  key_copy(key, getTable()->getInsertRecord(),
1437
 
           &getTable()->key_info[getTable()->getShare()->next_number_index],
1438
 
           getTable()->getShare()->next_number_key_offset);
1439
 
  error= mi_rkey(file, getTable()->getUpdateRecord(), (int) getTable()->getShare()->next_number_index,
1440
 
                 key, make_prev_keypart_map(getTable()->getShare()->next_number_keypart),
 
1427
  key_copy(key, table->record[0],
 
1428
           &table->key_info[table->getShare()->next_number_index],
 
1429
           table->getShare()->next_number_key_offset);
 
1430
  error= mi_rkey(file, table->record[1], (int) table->getShare()->next_number_index,
 
1431
                 key, make_prev_keypart_map(table->getShare()->next_number_keypart),
1441
1432
                 HA_READ_PREFIX_LAST);
1442
1433
  if (error)
1443
1434
    nr= 1;
1444
1435
  else
1445
1436
  {
1446
 
    /* Get data from getUpdateRecord() */
1447
 
    nr= ((uint64_t) getTable()->next_number_field->
1448
 
         val_int_offset(getTable()->getShare()->rec_buff_length)+1);
 
1437
    /* Get data from record[1] */
 
1438
    nr= ((uint64_t) table->next_number_field->
 
1439
         val_int_offset(table->getShare()->rec_buff_length)+1);
1449
1440
  }
1450
1441
  extra(HA_EXTRA_NO_KEYREAD);
1451
1442
  *first_value= nr;
1496
1487
  return (uint)file->state->checksum;
1497
1488
}
1498
1489
 
 
1490
static MyisamEngine *engine= NULL;
 
1491
 
1499
1492
static int myisam_init(module::Context &context)
1500
 
1501
 
  context.add(new MyisamEngine(engine_name));
1502
 
  context.registerVariable(new sys_var_constrained_value<size_t>("sort-buffer-size",
1503
 
                                                                 sort_buffer_size));
1504
 
  context.registerVariable(new sys_var_uint64_t_ptr("max_sort_file_size",
1505
 
                                                    &max_sort_file_size,
1506
 
                                                    context.getOptions()["max-sort-file-size"].as<uint64_t>()));
 
1493
{
 
1494
  engine= new MyisamEngine(engine_name);
 
1495
  context.add(engine);
 
1496
 
 
1497
  /* call ha_init_key_cache() on all key caches to init them */
 
1498
  int error= init_key_cache(dflt_key_cache,
 
1499
                            myisam_key_cache_block_size,
 
1500
                            myisam_key_cache_size,
 
1501
                            myisam_key_cache_division_limit, 
 
1502
                            myisam_key_cache_age_threshold);
 
1503
 
 
1504
  if (error == 0)
 
1505
    exit(1); /* Memory Allocation Failure */
1507
1506
 
1508
1507
  return 0;
1509
1508
}
1510
1509
 
1511
1510
 
1512
 
static void init_options(drizzled::module::option_context &context)
1513
 
{
1514
 
  context("max-sort-file-size",
1515
 
          po::value<uint64_t>(&max_sort_file_size)->default_value(INT32_MAX),
1516
 
          _("Don't use the fast sort index method to created index if the temporary file would get bigger than this."));
1517
 
  context("sort-buffer-size",
1518
 
          po::value<sort_buffer_constraint>(&sort_buffer_size)->default_value(8192*1024),
1519
 
          _("The buffer that is allocated when sorting the index when doing a REPAIR or when creating indexes with CREATE INDEX or ALTER TABLE."));
1520
 
}
 
1511
static void sys_var_key_cache_size_update(Session *session, drizzle_sys_var *var, void *, const void *save)
 
1512
{
 
1513
  uint32_t tmp= *static_cast<const uint32_t *>(save);
 
1514
  bool error= 0;
 
1515
 
 
1516
        struct option option_limits;
 
1517
  plugin_opt_set_limits(&option_limits, var);
 
1518
        option_limits.name= "myisam_key_cache_size";
 
1519
 
 
1520
  if (dflt_key_cache->in_init)
 
1521
    return;
 
1522
 
 
1523
  myisam_key_cache_size= static_cast<uint32_t>(fix_unsigned(session, static_cast<uint64_t>(tmp), &option_limits));
 
1524
 
 
1525
  /* If key cache didn't existed initialize it, else resize it */
 
1526
  dflt_key_cache->in_init= 1;
 
1527
 
 
1528
  error= ! resize_key_cache(dflt_key_cache,
 
1529
                                                                                                                myisam_key_cache_block_size,
 
1530
                            myisam_key_cache_size,
 
1531
                            myisam_key_cache_division_limit,
 
1532
                                                                                                          myisam_key_cache_age_threshold);
 
1533
  dflt_key_cache->in_init= 0;
 
1534
}
 
1535
 
 
1536
static void sys_var_key_cache_block_size_update(Session *session, drizzle_sys_var *var, void *, const void *save)
 
1537
{
 
1538
  uint32_t tmp= *static_cast<const uint32_t *>(save);
 
1539
  bool error= 0;
 
1540
 
 
1541
        struct option option_limits;
 
1542
  plugin_opt_set_limits(&option_limits, var);
 
1543
        option_limits.name= "myisam_key_cache_block_size";
 
1544
 
 
1545
  if (dflt_key_cache->in_init)
 
1546
    return;
 
1547
 
 
1548
  myisam_key_cache_block_size= static_cast<uint32_t>(fix_unsigned(session, static_cast<uint64_t>(tmp), &option_limits));
 
1549
 
 
1550
  dflt_key_cache->in_init= 1;
 
1551
 
 
1552
  error= ! resize_key_cache(dflt_key_cache,
 
1553
                                                                                                                myisam_key_cache_block_size,
 
1554
                            myisam_key_cache_size,
 
1555
                            myisam_key_cache_division_limit,
 
1556
                                                                                                          myisam_key_cache_age_threshold);
 
1557
 
 
1558
  dflt_key_cache->in_init= 0;
 
1559
}
 
1560
 
 
1561
static void sys_var_key_cache_division_limit_update(Session *session, drizzle_sys_var *var, void *, const void *save)
 
1562
{
 
1563
  uint32_t tmp= *static_cast<const uint32_t *>(save);
 
1564
  bool error= 0;
 
1565
 
 
1566
        struct option option_limits;
 
1567
  plugin_opt_set_limits(&option_limits, var);
 
1568
        option_limits.name= "myisam_key_cache_division_limit";
 
1569
 
 
1570
  if (dflt_key_cache->in_init)
 
1571
    return;
 
1572
 
 
1573
  myisam_key_cache_division_limit= static_cast<uint32_t>(fix_unsigned(session, static_cast<uint64_t>(tmp), &option_limits));
 
1574
 
 
1575
  dflt_key_cache->in_init= 1;
 
1576
 
 
1577
  error= ! resize_key_cache(dflt_key_cache,
 
1578
                                                                                                                myisam_key_cache_block_size,
 
1579
                            myisam_key_cache_size,
 
1580
                            myisam_key_cache_division_limit,
 
1581
                                                                                                          myisam_key_cache_age_threshold);
 
1582
 
 
1583
  dflt_key_cache->in_init= 0;
 
1584
}
 
1585
 
 
1586
static void sys_var_key_cache_age_threshold_update(Session *session, drizzle_sys_var *var, void *, const void *save)
 
1587
{
 
1588
  uint32_t tmp= *static_cast<const uint32_t *>(save);
 
1589
  bool error= 0;
 
1590
 
 
1591
        struct option option_limits;
 
1592
  plugin_opt_set_limits(&option_limits, var);
 
1593
        option_limits.name= "myisam_key_cache_age_threshold";
 
1594
 
 
1595
  if (dflt_key_cache->in_init)
 
1596
    return;
 
1597
 
 
1598
  myisam_key_cache_age_threshold= static_cast<uint32_t>(fix_unsigned(session, static_cast<uint64_t>(tmp), &option_limits));
 
1599
 
 
1600
  dflt_key_cache->in_init= 1;
 
1601
 
 
1602
  error= ! resize_key_cache(dflt_key_cache,
 
1603
                                                                                                                myisam_key_cache_block_size,
 
1604
                            myisam_key_cache_size,
 
1605
                            myisam_key_cache_division_limit,
 
1606
                                                                                                          myisam_key_cache_age_threshold);
 
1607
 
 
1608
  dflt_key_cache->in_init= 0;
 
1609
}
 
1610
 
 
1611
static DRIZZLE_SYSVAR_UINT(key_cache_block_size,
 
1612
                            myisam_key_cache_block_size,
 
1613
                            PLUGIN_VAR_RQCMDARG,
 
1614
                            N_("Block size to be used for MyISAM index pages."),
 
1615
                            NULL,
 
1616
                            sys_var_key_cache_block_size_update,
 
1617
                            KEY_CACHE_BLOCK_SIZE,
 
1618
                            512, 
 
1619
                            16 * 1024,
 
1620
                            0);
 
1621
 
 
1622
static DRIZZLE_SYSVAR_UINT(key_cache_age_threshold, myisam_key_cache_age_threshold,
 
1623
                            PLUGIN_VAR_RQCMDARG,
 
1624
                            N_("This characterizes the number of hits a hot block has to be untouched "
 
1625
                            "until it is considered aged enough to be downgraded to a warm block. "
 
1626
                            "This specifies the percentage ratio of that number of hits to the "
 
1627
                            "total number of blocks in key cache"),
 
1628
                            NULL,
 
1629
                            sys_var_key_cache_age_threshold_update,
 
1630
                            300,
 
1631
                            100, 
 
1632
                            UINT32_MAX,
 
1633
                            0);
 
1634
 
 
1635
static DRIZZLE_SYSVAR_UINT(key_cache_division_limit, myisam_key_cache_division_limit,
 
1636
                            PLUGIN_VAR_RQCMDARG,
 
1637
                            N_("The minimum percentage of warm blocks in key cache"),
 
1638
                            NULL,
 
1639
                            sys_var_key_cache_division_limit_update,
 
1640
                            100,
 
1641
                            1, 
 
1642
                            100,
 
1643
                            0);
 
1644
 
 
1645
static DRIZZLE_SYSVAR_UINT(key_cache_size,
 
1646
                            myisam_key_cache_size,
 
1647
                            PLUGIN_VAR_RQCMDARG,
 
1648
                            N_("The size of the buffer used for index blocks for MyISAM tables. "
 
1649
                            "Increase this to get better index handling (for all reads and multiple "
 
1650
                            "writes) to as much as you can afford;"),
 
1651
                            NULL,
 
1652
                            sys_var_key_cache_size_update,
 
1653
                            KEY_CACHE_SIZE,
 
1654
                            1 * 1024 * 1024, 
 
1655
                            UINT32_MAX,
 
1656
                            IO_SIZE);
 
1657
 
 
1658
static DRIZZLE_SYSVAR_UINT(repair_threads, repair_threads,
 
1659
                            PLUGIN_VAR_RQCMDARG,
 
1660
                            N_("Number of threads to use when repairing MyISAM tables. The value of "
 
1661
                            "1 disables parallel repair."),
 
1662
                            NULL, NULL, 1, 1, UINT32_MAX, 0);
 
1663
 
 
1664
static DRIZZLE_SYSVAR_ULONGLONG(max_sort_file_size, max_sort_file_size,
 
1665
                                PLUGIN_VAR_RQCMDARG,
 
1666
                                N_("Don't use the fast sort index method to created index if the temporary file would get bigger than this."),
 
1667
                                NULL, NULL, INT32_MAX, 0, UINT64_MAX, 0);
 
1668
 
 
1669
static DRIZZLE_SYSVAR_ULONGLONG(sort_buffer_size, sort_buffer_size,
 
1670
                                PLUGIN_VAR_RQCMDARG,
 
1671
                                N_("The buffer that is allocated when sorting the index when doing a REPAIR or when creating indexes with CREATE INDEX or ALTER TABLE."),
 
1672
                                NULL, NULL, 8192*1024, 1024, SIZE_MAX, 0);
 
1673
 
 
1674
extern uint32_t data_pointer_size;
 
1675
static DRIZZLE_SYSVAR_UINT(data_pointer_size, data_pointer_size,
 
1676
                            PLUGIN_VAR_RQCMDARG,
 
1677
                            N_("Default pointer size to be used for MyISAM tables."),
 
1678
                            NULL, NULL, 6, 2, 7, 0);
 
1679
 
 
1680
static drizzle_sys_var* sys_variables[]= {
 
1681
  DRIZZLE_SYSVAR(key_cache_block_size),
 
1682
  DRIZZLE_SYSVAR(key_cache_size),
 
1683
  DRIZZLE_SYSVAR(key_cache_division_limit),
 
1684
  DRIZZLE_SYSVAR(key_cache_age_threshold),
 
1685
  DRIZZLE_SYSVAR(repair_threads),
 
1686
  DRIZZLE_SYSVAR(max_sort_file_size),
 
1687
  DRIZZLE_SYSVAR(sort_buffer_size),
 
1688
  DRIZZLE_SYSVAR(data_pointer_size),
 
1689
  NULL
 
1690
};
1521
1691
 
1522
1692
 
1523
1693
DRIZZLE_DECLARE_PLUGIN
1524
1694
{
1525
1695
  DRIZZLE_VERSION_ID,
1526
1696
  "MyISAM",
1527
 
  "2.0",
 
1697
  "1.0",
1528
1698
  "MySQL AB",
1529
1699
  "Default engine as of MySQL 3.23 with great performance",
1530
1700
  PLUGIN_LICENSE_GPL,
1531
1701
  myisam_init, /* Plugin Init */
1532
 
  NULL,           /* depends */
1533
 
  init_options                        /* config options                  */
 
1702
  sys_variables,           /* system variables */
 
1703
  NULL                        /* config options                  */
1534
1704
}
1535
1705
DRIZZLE_DECLARE_PLUGIN_END;