~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/myisam/ha_myisam.cc

  • Committer: Paul McCullagh
  • Date: 2010-05-26 10:17:56 UTC
  • mto: (1567.1.3 new-staging)
  • mto: This revision was merged to the branch mainline in revision 1568.
  • Revision ID: paul.mccullagh@primebase.org-20100526101756-3qdwb9id3qhog0z1
Adjust for namespace changes

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"
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
 
#include <boost/program_options.hpp>
47
 
#include <drizzled/module/option_map.h>
48
 
 
49
 
namespace po= boost::program_options;
50
45
 
51
46
using namespace std;
52
47
using namespace drizzled;
53
48
 
 
49
extern pthread_mutex_t LOCK_global_system_variables;
54
50
static const string engine_name("MyISAM");
55
51
 
56
 
boost::mutex THR_LOCK_myisam;
 
52
pthread_mutex_t THR_LOCK_myisam= PTHREAD_MUTEX_INITIALIZER;
57
53
 
58
 
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;
59
56
static uint32_t myisam_key_cache_size;
60
57
static uint32_t myisam_key_cache_division_limit;
61
58
static uint32_t myisam_key_cache_age_threshold;
62
59
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;
65
 
 
66
 
void st_mi_isam_share::setKeyCache()
67
 
{
68
 
  (void)init_key_cache(&key_cache,
69
 
                       myisam_key_cache_block_size,
70
 
                       myisam_key_cache_size,
71
 
                       myisam_key_cache_division_limit, 
72
 
                       myisam_key_cache_age_threshold);
73
 
}
 
60
static uint64_t sort_buffer_size;
74
61
 
75
62
/*****************************************************************************
76
63
** MyISAM tables
99
86
                          HTON_AUTO_PART_KEY |
100
87
                          HTON_SKIP_STORE_LOCK)
101
88
  {
 
89
    pthread_mutex_init(&THR_LOCK_myisam,MY_MUTEX_INIT_FAST);
102
90
  }
103
91
 
104
92
  virtual ~MyisamEngine()
105
93
  { 
 
94
    pthread_mutex_destroy(&THR_LOCK_myisam);
 
95
    end_key_cache(dflt_key_cache, 1);           // Can never fail
 
96
 
106
97
    mi_panic(HA_PANIC_CLOSE);
107
98
  }
108
99
 
109
 
  virtual Cursor *create(Table &table)
 
100
  virtual Cursor *create(TableShare &table,
 
101
                         memory::Root *mem_root)
110
102
  {
111
 
    return new ha_myisam(*this, table);
 
103
    return new (mem_root) ha_myisam(*this, table);
112
104
  }
113
105
 
114
106
  const char **bas_ext() const {
117
109
 
118
110
  int doCreateTable(Session&,
119
111
                    Table& table_arg,
120
 
                    const TableIdentifier &identifier,
 
112
                    drizzled::TableIdentifier &identifier,
121
113
                    message::Table&);
122
114
 
123
 
  int doRenameTable(Session&, const TableIdentifier &from, const TableIdentifier &to);
 
115
  int doRenameTable(Session&, TableIdentifier &from, TableIdentifier &to);
124
116
 
125
 
  int doDropTable(Session&, const TableIdentifier &identifier);
 
117
  int doDropTable(Session&, drizzled::TableIdentifier &identifier);
126
118
 
127
119
  int doGetTableDefinition(Session& session,
128
 
                           const TableIdentifier &identifier,
 
120
                           drizzled::TableIdentifier &identifier,
129
121
                           message::Table &table_message);
130
122
 
 
123
  /* Temp only engine, so do not return values. */
 
124
  void doGetTableNames(CachedDirectory &, SchemaIdentifier &, set<string>&) { };
 
125
 
131
126
  uint32_t max_supported_keys()          const { return MI_MAX_KEY; }
132
127
  uint32_t max_supported_key_length()    const { return MI_MAX_KEY_LENGTH; }
133
128
  uint32_t max_supported_key_part_length() const { return MI_MAX_KEY_LENGTH; }
140
135
            HA_READ_ORDER |
141
136
            HA_KEYREAD_ONLY);
142
137
  }
143
 
  bool doDoesTableExist(Session& session, const TableIdentifier &identifier);
 
138
  bool doDoesTableExist(Session& session, TableIdentifier &identifier);
144
139
 
145
140
  void doGetTableIdentifiers(drizzled::CachedDirectory &directory,
146
 
                             const drizzled::SchemaIdentifier &schema_identifier,
147
 
                             drizzled::TableIdentifier::vector &set_of_identifiers);
 
141
                             drizzled::SchemaIdentifier &schema_identifier,
 
142
                             drizzled::TableIdentifiers &set_of_identifiers);
148
143
  bool validateCreateTableOption(const std::string &key, const std::string &state)
149
144
  {
150
145
    (void)state;
158
153
};
159
154
 
160
155
void MyisamEngine::doGetTableIdentifiers(drizzled::CachedDirectory&,
161
 
                                         const drizzled::SchemaIdentifier&,
162
 
                                         drizzled::TableIdentifier::vector&)
 
156
                                         drizzled::SchemaIdentifier&,
 
157
                                         drizzled::TableIdentifiers&)
163
158
{
164
159
}
165
160
 
166
 
bool MyisamEngine::doDoesTableExist(Session &session, const TableIdentifier &identifier)
 
161
bool MyisamEngine::doDoesTableExist(Session &session, TableIdentifier &identifier)
167
162
{
168
 
  return session.getMessageCache().doesTableMessageExist(identifier);
 
163
  return session.doesTableMessageExist(identifier);
169
164
}
170
165
 
171
166
int MyisamEngine::doGetTableDefinition(Session &session,
172
 
                                       const TableIdentifier &identifier,
 
167
                                       drizzled::TableIdentifier &identifier,
173
168
                                       message::Table &table_message)
174
169
{
175
 
  if (session.getMessageCache().getTableMessage(identifier, table_message))
 
170
  if (session.getTableMessage(identifier, table_message))
176
171
    return EEXIST;
177
172
  return ENOENT;
178
173
}
217
212
  uint32_t i, j, recpos, minpos, fieldpos, temp_length, length;
218
213
  enum ha_base_keytype type= HA_KEYTYPE_BINARY;
219
214
  unsigned char *record;
 
215
  KeyInfo *pos;
220
216
  MI_KEYDEF *keydef;
221
217
  MI_COLUMNDEF *recinfo, *recinfo_pos;
222
218
  HA_KEYSEG *keyseg;
223
219
  TableShare *share= table_arg->getMutableShare();
224
220
  uint32_t options= share->db_options_in_use;
225
221
  if (!(memory::multi_malloc(false,
226
 
          recinfo_out, (share->sizeFields() * 2 + 2) * sizeof(MI_COLUMNDEF),
227
 
          keydef_out, share->sizeKeys() * sizeof(MI_KEYDEF),
228
 
          &keyseg, (share->key_parts + share->sizeKeys()) * sizeof(HA_KEYSEG),
 
222
          recinfo_out, (share->fields * 2 + 2) * sizeof(MI_COLUMNDEF),
 
223
          keydef_out, share->keys * sizeof(MI_KEYDEF),
 
224
          &keyseg, (share->key_parts + share->keys) * sizeof(HA_KEYSEG),
229
225
          NULL)))
230
226
    return(HA_ERR_OUT_OF_MEM);
231
227
  keydef= *keydef_out;
232
228
  recinfo= *recinfo_out;
233
 
  for (i= 0; i < share->sizeKeys(); i++)
 
229
  pos= table_arg->key_info;
 
230
  for (i= 0; i < share->keys; i++, pos++)
234
231
  {
235
 
    KeyInfo *pos= &table_arg->key_info[i];
236
232
    keydef[i].flag= ((uint16_t) pos->flags & (HA_NOSAME));
237
233
    keydef[i].key_alg= HA_KEY_ALG_BTREE;
238
234
    keydef[i].block_length= pos->block_size;
273
269
      {
274
270
        keydef[i].seg[j].null_bit= field->null_bit;
275
271
        keydef[i].seg[j].null_pos= (uint) (field->null_ptr-
276
 
                                           (unsigned char*) table_arg->getInsertRecord());
 
272
                                           (unsigned char*) table_arg->record[0]);
277
273
      }
278
274
      else
279
275
      {
292
288
  }
293
289
  if (table_arg->found_next_number_field)
294
290
    keydef[share->next_number_index].flag|= HA_AUTO_KEY;
295
 
  record= table_arg->getInsertRecord();
 
291
  record= table_arg->record[0];
296
292
  recpos= 0;
297
293
  recinfo_pos= recinfo;
298
294
  while (recpos < (uint) share->stored_rec_length)
299
295
  {
300
296
    Field **field, *found= 0;
301
 
    minpos= share->getRecordLength();
 
297
    minpos= share->reclength;
302
298
    length= 0;
303
299
 
304
 
    for (field= table_arg->getFields(); *field; field++)
 
300
    for (field= table_arg->field; *field; field++)
305
301
    {
306
302
      if ((fieldpos= (*field)->offset(record)) >= recpos &&
307
303
          fieldpos <= minpos)
341
337
    {
342
338
      recinfo_pos->null_bit= found->null_bit;
343
339
      recinfo_pos->null_pos= (uint) (found->null_ptr -
344
 
                                     (unsigned char*) table_arg->getInsertRecord());
 
340
                                     (unsigned char*) table_arg->record[0]);
345
341
    }
346
342
    else
347
343
    {
479
475
volatile int *killed_ptr(MI_CHECK *param)
480
476
{
481
477
  /* In theory Unsafe conversion, but should be ok for now */
482
 
  return (int*) (((Session *)(param->session))->getKilledPtr());
 
478
  return (int*) &(((Session *)(param->session))->killed);
483
479
}
484
480
 
485
481
void mi_check_print_error(MI_CHECK *param, const char *fmt,...)
529
525
                        const char *sfile, uint32_t sline)
530
526
{
531
527
  Session *cur_session;
 
528
  pthread_mutex_lock(&file->s->intern_lock);
532
529
  if ((cur_session= file->in_use))
533
530
    errmsg_printf(ERRMSG_LVL_ERROR, _("Got an error from thread_id=%"PRIu64", %s:%d"),
534
531
                    cur_session->thread_id,
543
540
    errmsg_printf(ERRMSG_LVL_ERROR, "%s", _("Unknown thread accessing table"));
544
541
    ++it;
545
542
  }
 
543
  pthread_mutex_unlock(&file->s->intern_lock);
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),
567
565
}
568
566
 
569
567
/* Name is here without an extension */
570
 
int ha_myisam::doOpen(const drizzled::TableIdentifier &identifier, int mode, uint32_t test_if_locked)
 
568
int ha_myisam::open(const char *name, int mode, uint32_t test_if_locked)
571
569
{
572
570
  MI_KEYDEF *keyinfo;
573
571
  MI_COLUMNDEF *recinfo= 0;
589
587
    open of a table that is in use by other threads already (if the
590
588
    MyISAM share exists already).
591
589
  */
592
 
  if (!(file= mi_open(identifier, mode, test_if_locked)))
 
590
  if (!(file=mi_open(name, 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()->tmp_table) /* 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()->keys, recs,
602
600
                         file->s->keyinfo, file->s->rec,
603
601
                         file->s->base.keys, file->s->base.fields, true))
604
602
    {
607
605
    }
608
606
  }
609
607
 
610
 
  assert(test_if_locked);
611
608
  if (test_if_locked & (HA_OPEN_IGNORE_IF_LOCKED | HA_OPEN_TMP_TABLE))
612
609
    mi_extra(file, HA_EXTRA_NO_WAIT_LOCK, 0);
613
610
 
614
611
  info(HA_STATUS_NO_LOCK | HA_STATUS_VARIABLE | HA_STATUS_CONST);
615
612
  if (!(test_if_locked & HA_OPEN_WAIT_IF_LOCKED))
616
613
    mi_extra(file, HA_EXTRA_WAIT_LOCK, 0);
617
 
  if (!getTable()->getShare()->db_record_offset)
 
614
  if (!table->getShare()->db_record_offset)
618
615
    is_ordered= false;
619
616
 
620
617
 
621
618
  keys_with_parts.reset();
622
 
  for (i= 0; i < getTable()->getShare()->sizeKeys(); i++)
 
619
  for (i= 0; i < table->getShare()->keys; i++)
623
620
  {
624
 
    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;
625
622
 
626
 
    KeyPartInfo *kp= getTable()->key_info[i].key_part;
627
 
    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;
628
625
    for (; kp != kp_end; kp++)
629
626
    {
630
627
      if (!kp->field->part_of_key.test(i))
657
654
 
658
655
int ha_myisam::doInsertRecord(unsigned char *buf)
659
656
{
 
657
  ha_statistic_increment(&system_status_var::ha_write_count);
 
658
 
660
659
  /*
661
660
    If we have an auto_increment column and we are writing a changed row
662
661
    or a new row, then update the auto_increment value in the record.
663
662
  */
664
 
  if (getTable()->next_number_field && buf == getTable()->getInsertRecord())
 
663
  if (table->next_number_field && buf == table->record[0])
665
664
  {
666
665
    int error;
667
666
    if ((error= update_auto_increment()))
693
692
  {
694
693
    errmsg_printf(ERRMSG_LVL_INFO, "Retrying repair of: '%s' failed. "
695
694
                          "Please try REPAIR EXTENDED or myisamchk",
696
 
                          getTable()->getShare()->getPath());
 
695
                          table->getShare()->getPath());
697
696
    return(HA_ADMIN_FAILED);
698
697
  }
699
698
 
700
 
  param.db_name=    getTable()->getShare()->getSchemaName();
701
 
  param.table_name= getTable()->getAlias();
 
699
  param.db_name=    table->getShare()->getSchemaName();
 
700
  param.table_name= table->alias;
702
701
  param.tmpfile_createflag = O_RDWR | O_TRUNC;
703
702
  param.using_global_keycache = 1;
704
703
  param.session= session;
705
704
  param.out_flag= 0;
706
 
  param.sort_buffer_length= static_cast<size_t>(sort_buffer_size);
 
705
  param.sort_buffer_length= (size_t)sort_buffer_size;
707
706
  strcpy(fixed_name,file->filename);
708
707
 
709
708
  // Don't lock tables if we have used LOCK Table
710
 
  if (mi_lock_database(file, getTable()->getShare()->getType() ? F_EXTRA_LCK : F_WRLCK))
 
709
  if (mi_lock_database(file, table->getShare()->tmp_table ? F_EXTRA_LCK : F_WRLCK))
711
710
  {
712
711
    mi_check_print_error(&param,ER(ER_CANT_LOCK),errno);
713
712
    return(HA_ADMIN_FAILED);
728
727
      local_testflag|= T_STATISTICS;
729
728
      param.testflag|= T_STATISTICS;            // We get this for free
730
729
      statistics_done=1;
 
730
      if (repair_threads > 1)
 
731
      {
 
732
        char buf[40];
 
733
        /* TODO: respect myisam_repair_threads variable */
 
734
        snprintf(buf, 40, "Repair with %d threads", internal::my_count_bits(key_map));
 
735
        session->set_proc_info(buf);
 
736
        error = mi_repair_parallel(&param, file, fixed_name,
 
737
            param.testflag & T_QUICK);
 
738
        session->set_proc_info("Repair done"); // to reset proc_info, as
 
739
                                      // it was pointing to local buffer
 
740
      }
 
741
      else
731
742
      {
732
743
        session->set_proc_info("Repair by sorting");
733
744
        error = mi_repair_by_sort(&param, file, fixed_name,
904
915
  }
905
916
  else if (mode == HA_KEY_SWITCH_NONUNIQ_SAVE)
906
917
  {
907
 
    Session *session= getTable()->in_use;
908
 
    boost::scoped_ptr<MI_CHECK> param_ap(new MI_CHECK);
909
 
    MI_CHECK &param= *param_ap.get();
 
918
    Session *session=current_session;
 
919
    MI_CHECK param;
910
920
    const char *save_proc_info= session->get_proc_info();
911
921
    session->set_proc_info("Creating index");
912
922
    myisamchk_init(&param);
914
924
    param.testflag= (T_SILENT | T_REP_BY_SORT | T_QUICK |
915
925
                     T_CREATE_MISSING_KEYS);
916
926
    param.myf_rw&= ~MY_WAIT_IF_FULL;
917
 
    param.sort_buffer_length=  static_cast<size_t>(sort_buffer_size);
 
927
    param.sort_buffer_length=  (size_t)sort_buffer_size;
918
928
    param.stats_method= MI_STATS_METHOD_NULLS_NOT_EQUAL;
919
929
    if ((error= (repair(session,param,0) != HA_ADMIN_OK)) && param.retry_repair)
920
930
    {
981
991
 
982
992
void ha_myisam::start_bulk_insert(ha_rows rows)
983
993
{
984
 
  Session *session= getTable()->in_use;
 
994
  Session *session= current_session;
985
995
  ulong size= session->variables.read_buff_size;
986
996
 
987
997
  /* don't enable row cache if too few rows */
1035
1045
 
1036
1046
int ha_myisam::doUpdateRecord(const unsigned char *old_data, unsigned char *new_data)
1037
1047
{
 
1048
  ha_statistic_increment(&system_status_var::ha_update_count);
 
1049
  if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_UPDATE)
 
1050
    table->timestamp_field->set_time();
1038
1051
  return mi_update(file,old_data,new_data);
1039
1052
}
1040
1053
 
1041
1054
int ha_myisam::doDeleteRecord(const unsigned char *buf)
1042
1055
{
 
1056
  ha_statistic_increment(&system_status_var::ha_delete_count);
1043
1057
  return mi_delete(file,buf);
1044
1058
}
1045
1059
 
1066
1080
  assert(inited==INDEX);
1067
1081
  ha_statistic_increment(&system_status_var::ha_read_key_count);
1068
1082
  int error=mi_rkey(file, buf, active_index, key, keypart_map, find_flag);
1069
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1083
  table->status=error ? STATUS_NOT_FOUND: 0;
1070
1084
  return error;
1071
1085
}
1072
1086
 
1076
1090
{
1077
1091
  ha_statistic_increment(&system_status_var::ha_read_key_count);
1078
1092
  int error=mi_rkey(file, buf, index, key, keypart_map, find_flag);
1079
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1093
  table->status=error ? STATUS_NOT_FOUND: 0;
1080
1094
  return error;
1081
1095
}
1082
1096
 
1087
1101
  ha_statistic_increment(&system_status_var::ha_read_key_count);
1088
1102
  int error=mi_rkey(file, buf, active_index, key, keypart_map,
1089
1103
                    HA_READ_PREFIX_LAST);
1090
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1104
  table->status=error ? STATUS_NOT_FOUND: 0;
1091
1105
  return(error);
1092
1106
}
1093
1107
 
1096
1110
  assert(inited==INDEX);
1097
1111
  ha_statistic_increment(&system_status_var::ha_read_next_count);
1098
1112
  int error=mi_rnext(file,buf,active_index);
1099
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1113
  table->status=error ? STATUS_NOT_FOUND: 0;
1100
1114
  return error;
1101
1115
}
1102
1116
 
1105
1119
  assert(inited==INDEX);
1106
1120
  ha_statistic_increment(&system_status_var::ha_read_prev_count);
1107
1121
  int error=mi_rprev(file,buf, active_index);
1108
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1122
  table->status=error ? STATUS_NOT_FOUND: 0;
1109
1123
  return error;
1110
1124
}
1111
1125
 
1114
1128
  assert(inited==INDEX);
1115
1129
  ha_statistic_increment(&system_status_var::ha_read_first_count);
1116
1130
  int error=mi_rfirst(file, buf, active_index);
1117
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1131
  table->status=error ? STATUS_NOT_FOUND: 0;
1118
1132
  return error;
1119
1133
}
1120
1134
 
1123
1137
  assert(inited==INDEX);
1124
1138
  ha_statistic_increment(&system_status_var::ha_read_last_count);
1125
1139
  int error=mi_rlast(file, buf, active_index);
1126
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1140
  table->status=error ? STATUS_NOT_FOUND: 0;
1127
1141
  return error;
1128
1142
}
1129
1143
 
1138
1152
  {
1139
1153
    error= mi_rnext_same(file,buf);
1140
1154
  } while (error == HA_ERR_RECORD_DELETED);
1141
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1155
  table->status=error ? STATUS_NOT_FOUND: 0;
1142
1156
  return error;
1143
1157
}
1144
1158
 
1179
1193
{
1180
1194
  ha_statistic_increment(&system_status_var::ha_read_rnd_next_count);
1181
1195
  int error=mi_scan(file, buf);
1182
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1196
  table->status=error ? STATUS_NOT_FOUND: 0;
1183
1197
  return error;
1184
1198
}
1185
1199
 
1187
1201
{
1188
1202
  ha_statistic_increment(&system_status_var::ha_read_rnd_count);
1189
1203
  int error=mi_rrnd(file, buf, internal::my_get_ptr(pos,ref_length));
1190
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
1204
  table->status=error ? STATUS_NOT_FOUND: 0;
1191
1205
  return error;
1192
1206
}
1193
1207
 
1216
1230
  }
1217
1231
  if (flag & HA_STATUS_CONST)
1218
1232
  {
1219
 
    TableShare *share= getTable()->getMutableShare();
 
1233
    TableShare *share= table->getMutableShare();
1220
1234
    stats.max_data_file_length=  misam_info.max_data_file_length;
1221
1235
    stats.max_index_file_length= misam_info.max_index_file_length;
1222
1236
    stats.create_time= misam_info.create_time;
1224
1238
    share->db_options_in_use= misam_info.options;
1225
1239
    stats.block_size= myisam_key_cache_block_size;        /* record block size */
1226
1240
 
1227
 
    set_prefix(share->keys_in_use, share->sizeKeys());
 
1241
    /* Update share */
 
1242
    if (share->tmp_table == message::Table::STANDARD)
 
1243
      pthread_mutex_lock(&share->mutex);
 
1244
    set_prefix(share->keys_in_use, share->keys);
1228
1245
    /*
1229
1246
     * Due to bug 394932 (32-bit solaris build failure), we need
1230
1247
     * to convert the uint64_t key_map member of the misam_info
1273
1290
    share->keys_for_keyread&= share->keys_in_use;
1274
1291
    share->db_record_offset= misam_info.record_offset;
1275
1292
    if (share->key_parts)
1276
 
      memcpy(getTable()->key_info[0].rec_per_key,
 
1293
      memcpy(table->key_info[0].rec_per_key,
1277
1294
             misam_info.rec_per_key,
1278
 
             sizeof(getTable()->key_info[0].rec_per_key)*share->key_parts);
1279
 
    assert(share->getType() != message::Table::STANDARD);
 
1295
             sizeof(table->key_info[0].rec_per_key)*share->key_parts);
 
1296
    if (share->tmp_table == message::Table::STANDARD)
 
1297
      pthread_mutex_unlock(&share->mutex);
1280
1298
 
1281
1299
   /*
1282
1300
     Set data_file_name and index_file_name to point at the symlink value
1329
1347
}
1330
1348
 
1331
1349
int MyisamEngine::doDropTable(Session &session,
1332
 
                              const TableIdentifier &identifier)
 
1350
                              drizzled::TableIdentifier &identifier)
1333
1351
{
1334
 
  session.getMessageCache().removeTableMessage(identifier);
 
1352
  session.removeTableMessage(identifier);
1335
1353
 
1336
1354
  return mi_delete_table(identifier.getPath().c_str());
1337
1355
}
1340
1358
int ha_myisam::external_lock(Session *session, int lock_type)
1341
1359
{
1342
1360
  file->in_use= session;
1343
 
  return mi_lock_database(file, !getTable()->getShare()->getType() ?
 
1361
  return mi_lock_database(file, !table->getShare()->tmp_table ?
1344
1362
                          lock_type : ((lock_type == F_UNLCK) ?
1345
1363
                                       F_UNLCK : F_EXTRA_LCK));
1346
1364
}
1347
1365
 
1348
1366
int MyisamEngine::doCreateTable(Session &session,
1349
1367
                                Table& table_arg,
1350
 
                                const TableIdentifier &identifier,
 
1368
                                drizzled::TableIdentifier &identifier,
1351
1369
                                message::Table& create_proto)
1352
1370
{
1353
1371
  int error;
1381
1399
  /* TODO: Check that the following internal::fn_format is really needed */
1382
1400
  error= mi_create(internal::fn_format(buff, identifier.getPath().c_str(), "", "",
1383
1401
                                       MY_UNPACK_FILENAME|MY_APPEND_EXT),
1384
 
                   share->sizeKeys(), keydef,
 
1402
                   share->keys, keydef,
1385
1403
                   create_records, recinfo,
1386
1404
                   0, (MI_UNIQUEDEF*) 0,
1387
1405
                   &create_info, create_flags);
1388
1406
  free((unsigned char*) recinfo);
1389
1407
 
1390
 
  session.getMessageCache().storeTableMessage(identifier, create_proto);
 
1408
  session.storeTableMessage(identifier, create_proto);
1391
1409
 
1392
1410
  return error;
1393
1411
}
1394
1412
 
1395
1413
 
1396
 
int MyisamEngine::doRenameTable(Session &session, const TableIdentifier &from, const TableIdentifier &to)
 
1414
int MyisamEngine::doRenameTable(Session &session, TableIdentifier &from, TableIdentifier &to)
1397
1415
{
1398
 
  session.getMessageCache().renameTableMessage(from, to);
 
1416
  session.renameTableMessage(from, to);
1399
1417
 
1400
1418
  return mi_rename(from.getPath().c_str(), to.getPath().c_str());
1401
1419
}
1411
1429
  int error;
1412
1430
  unsigned char key[MI_MAX_KEY_LENGTH];
1413
1431
 
1414
 
  if (!getTable()->getShare()->next_number_key_offset)
 
1432
  if (!table->getShare()->next_number_key_offset)
1415
1433
  {                                             // Autoincrement at key-start
1416
1434
    ha_myisam::info(HA_STATUS_AUTO);
1417
1435
    *first_value= stats.auto_increment_value;
1421
1439
  }
1422
1440
 
1423
1441
  /* it's safe to call the following if bulk_insert isn't on */
1424
 
  mi_flush_bulk_insert(file, getTable()->getShare()->next_number_index);
 
1442
  mi_flush_bulk_insert(file, table->getShare()->next_number_index);
1425
1443
 
1426
1444
  (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),
 
1445
  key_copy(key, table->record[0],
 
1446
           table->key_info + table->getShare()->next_number_index,
 
1447
           table->getShare()->next_number_key_offset);
 
1448
  error= mi_rkey(file, table->record[1], (int) table->getShare()->next_number_index,
 
1449
                 key, make_prev_keypart_map(table->getShare()->next_number_keypart),
1432
1450
                 HA_READ_PREFIX_LAST);
1433
1451
  if (error)
1434
1452
    nr= 1;
1435
1453
  else
1436
1454
  {
1437
 
    /* Get data from getUpdateRecord() */
1438
 
    nr= ((uint64_t) getTable()->next_number_field->
1439
 
         val_int_offset(getTable()->getShare()->rec_buff_length)+1);
 
1455
    /* Get data from record[1] */
 
1456
    nr= ((uint64_t) table->next_number_field->
 
1457
         val_int_offset(table->getShare()->rec_buff_length)+1);
1440
1458
  }
1441
1459
  extra(HA_EXTRA_NO_KEYREAD);
1442
1460
  *first_value= nr;
1487
1505
  return (uint)file->state->checksum;
1488
1506
}
1489
1507
 
 
1508
static MyisamEngine *engine= NULL;
 
1509
 
1490
1510
static int myisam_init(module::Context &context)
1491
 
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>()));
 
1511
{
 
1512
  engine= new MyisamEngine(engine_name);
 
1513
  context.add(engine);
 
1514
 
 
1515
  /* call ha_init_key_cache() on all key caches to init them */
 
1516
  int error= init_key_cache(dflt_key_cache,
 
1517
                            myisam_key_cache_block_size,
 
1518
                            myisam_key_cache_size,
 
1519
                            myisam_key_cache_division_limit, 
 
1520
                            myisam_key_cache_age_threshold);
 
1521
 
 
1522
  if (error == 0)
 
1523
    exit(1); /* Memory Allocation Failure */
1498
1524
 
1499
1525
  return 0;
1500
1526
}
1501
1527
 
1502
1528
 
1503
 
static void init_options(drizzled::module::option_context &context)
1504
 
{
1505
 
  context("max-sort-file-size",
1506
 
          po::value<uint64_t>(&max_sort_file_size)->default_value(INT32_MAX),
1507
 
          N_("Don't use the fast sort index method to created index if the temporary file would get bigger than this."));
1508
 
  context("sort-buffer-size",
1509
 
          po::value<sort_buffer_constraint>(&sort_buffer_size)->default_value(8192*1024),
1510
 
          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
 
}
 
1529
static void sys_var_key_cache_size_update(Session *session, drizzle_sys_var *var, void *, const void *save)
 
1530
{
 
1531
  uint32_t tmp= *static_cast<const uint32_t *>(save);
 
1532
  bool error= 0;
 
1533
 
 
1534
        struct option option_limits;
 
1535
  plugin_opt_set_limits(&option_limits, var);
 
1536
        option_limits.name= "myisam_key_cache_size";
 
1537
 
 
1538
  if (dflt_key_cache->in_init)
 
1539
    return;
 
1540
 
 
1541
  myisam_key_cache_size= static_cast<uint32_t>(fix_unsigned(session, static_cast<uint64_t>(tmp), &option_limits));
 
1542
 
 
1543
  /* If key cache didn't existed initialize it, else resize it */
 
1544
  dflt_key_cache->in_init= 1;
 
1545
 
 
1546
  error= ! resize_key_cache(dflt_key_cache,
 
1547
                                                                                                                myisam_key_cache_block_size,
 
1548
                            myisam_key_cache_size,
 
1549
                            myisam_key_cache_division_limit,
 
1550
                                                                                                          myisam_key_cache_age_threshold);
 
1551
  dflt_key_cache->in_init= 0;
 
1552
}
 
1553
 
 
1554
static void sys_var_key_cache_block_size_update(Session *session, drizzle_sys_var *var, void *, const void *save)
 
1555
{
 
1556
  uint32_t tmp= *static_cast<const uint32_t *>(save);
 
1557
  bool error= 0;
 
1558
 
 
1559
        struct option option_limits;
 
1560
  plugin_opt_set_limits(&option_limits, var);
 
1561
        option_limits.name= "myisam_key_cache_block_size";
 
1562
 
 
1563
  if (dflt_key_cache->in_init)
 
1564
    return;
 
1565
 
 
1566
  myisam_key_cache_block_size= static_cast<uint32_t>(fix_unsigned(session, static_cast<uint64_t>(tmp), &option_limits));
 
1567
 
 
1568
  dflt_key_cache->in_init= 1;
 
1569
 
 
1570
  error= ! resize_key_cache(dflt_key_cache,
 
1571
                                                                                                                myisam_key_cache_block_size,
 
1572
                            myisam_key_cache_size,
 
1573
                            myisam_key_cache_division_limit,
 
1574
                                                                                                          myisam_key_cache_age_threshold);
 
1575
 
 
1576
  dflt_key_cache->in_init= 0;
 
1577
}
 
1578
 
 
1579
static void sys_var_key_cache_division_limit_update(Session *session, drizzle_sys_var *var, void *, const void *save)
 
1580
{
 
1581
  uint32_t tmp= *static_cast<const uint32_t *>(save);
 
1582
  bool error= 0;
 
1583
 
 
1584
        struct option option_limits;
 
1585
  plugin_opt_set_limits(&option_limits, var);
 
1586
        option_limits.name= "myisam_key_cache_division_limit";
 
1587
 
 
1588
  if (dflt_key_cache->in_init)
 
1589
    return;
 
1590
 
 
1591
  myisam_key_cache_division_limit= static_cast<uint32_t>(fix_unsigned(session, static_cast<uint64_t>(tmp), &option_limits));
 
1592
 
 
1593
  dflt_key_cache->in_init= 1;
 
1594
 
 
1595
  error= ! resize_key_cache(dflt_key_cache,
 
1596
                                                                                                                myisam_key_cache_block_size,
 
1597
                            myisam_key_cache_size,
 
1598
                            myisam_key_cache_division_limit,
 
1599
                                                                                                          myisam_key_cache_age_threshold);
 
1600
 
 
1601
  dflt_key_cache->in_init= 0;
 
1602
}
 
1603
 
 
1604
static void sys_var_key_cache_age_threshold_update(Session *session, drizzle_sys_var *var, void *, const void *save)
 
1605
{
 
1606
  uint32_t tmp= *static_cast<const uint32_t *>(save);
 
1607
  bool error= 0;
 
1608
 
 
1609
        struct option option_limits;
 
1610
  plugin_opt_set_limits(&option_limits, var);
 
1611
        option_limits.name= "myisam_key_cache_age_threshold";
 
1612
 
 
1613
  if (dflt_key_cache->in_init)
 
1614
    return;
 
1615
 
 
1616
  myisam_key_cache_age_threshold= static_cast<uint32_t>(fix_unsigned(session, static_cast<uint64_t>(tmp), &option_limits));
 
1617
 
 
1618
  dflt_key_cache->in_init= 1;
 
1619
 
 
1620
  error= ! resize_key_cache(dflt_key_cache,
 
1621
                                                                                                                myisam_key_cache_block_size,
 
1622
                            myisam_key_cache_size,
 
1623
                            myisam_key_cache_division_limit,
 
1624
                                                                                                          myisam_key_cache_age_threshold);
 
1625
 
 
1626
  dflt_key_cache->in_init= 0;
 
1627
}
 
1628
 
 
1629
static DRIZZLE_SYSVAR_UINT(key_cache_block_size,
 
1630
                            myisam_key_cache_block_size,
 
1631
                            PLUGIN_VAR_RQCMDARG,
 
1632
                            N_("Block size to be used for MyISAM index pages."),
 
1633
                            NULL,
 
1634
                            sys_var_key_cache_block_size_update,
 
1635
                            KEY_CACHE_BLOCK_SIZE,
 
1636
                            512, 
 
1637
                            16 * 1024,
 
1638
                            0);
 
1639
 
 
1640
static DRIZZLE_SYSVAR_UINT(key_cache_age_threshold, myisam_key_cache_age_threshold,
 
1641
                            PLUGIN_VAR_RQCMDARG,
 
1642
                            N_("This characterizes the number of hits a hot block has to be untouched "
 
1643
                            "until it is considered aged enough to be downgraded to a warm block. "
 
1644
                            "This specifies the percentage ratio of that number of hits to the "
 
1645
                            "total number of blocks in key cache"),
 
1646
                            NULL,
 
1647
                            sys_var_key_cache_age_threshold_update,
 
1648
                            300,
 
1649
                            100, 
 
1650
                            UINT32_MAX,
 
1651
                            0);
 
1652
 
 
1653
static DRIZZLE_SYSVAR_UINT(key_cache_division_limit, myisam_key_cache_division_limit,
 
1654
                            PLUGIN_VAR_RQCMDARG,
 
1655
                            N_("The minimum percentage of warm blocks in key cache"),
 
1656
                            NULL,
 
1657
                            sys_var_key_cache_division_limit_update,
 
1658
                            100,
 
1659
                            1, 
 
1660
                            100,
 
1661
                            0);
 
1662
 
 
1663
static DRIZZLE_SYSVAR_UINT(key_cache_size,
 
1664
                            myisam_key_cache_size,
 
1665
                            PLUGIN_VAR_RQCMDARG,
 
1666
                            N_("The size of the buffer used for index blocks for MyISAM tables. "
 
1667
                            "Increase this to get better index handling (for all reads and multiple "
 
1668
                            "writes) to as much as you can afford;"),
 
1669
                            NULL,
 
1670
                            sys_var_key_cache_size_update,
 
1671
                            KEY_CACHE_SIZE,
 
1672
                            1 * 1024 * 1024, 
 
1673
                            UINT32_MAX,
 
1674
                            IO_SIZE);
 
1675
 
 
1676
static DRIZZLE_SYSVAR_UINT(repair_threads, repair_threads,
 
1677
                            PLUGIN_VAR_RQCMDARG,
 
1678
                            N_("Number of threads to use when repairing MyISAM tables. The value of "
 
1679
                            "1 disables parallel repair."),
 
1680
                            NULL, NULL, 1, 1, UINT32_MAX, 0);
 
1681
 
 
1682
static DRIZZLE_SYSVAR_ULONGLONG(max_sort_file_size, max_sort_file_size,
 
1683
                                PLUGIN_VAR_RQCMDARG,
 
1684
                                N_("Don't use the fast sort index method to created index if the temporary file would get bigger than this."),
 
1685
                                NULL, NULL, INT32_MAX, 0, UINT64_MAX, 0);
 
1686
 
 
1687
static DRIZZLE_SYSVAR_ULONGLONG(sort_buffer_size, sort_buffer_size,
 
1688
                                PLUGIN_VAR_RQCMDARG,
 
1689
                                N_("The buffer that is allocated when sorting the index when doing a REPAIR or when creating indexes with CREATE INDEX or ALTER TABLE."),
 
1690
                                NULL, NULL, 8192*1024, 1024, SIZE_MAX, 0);
 
1691
 
 
1692
extern uint32_t data_pointer_size;
 
1693
static DRIZZLE_SYSVAR_UINT(data_pointer_size, data_pointer_size,
 
1694
                            PLUGIN_VAR_RQCMDARG,
 
1695
                            N_("Default pointer size to be used for MyISAM tables."),
 
1696
                            NULL, NULL, 6, 2, 7, 0);
 
1697
 
 
1698
static drizzle_sys_var* sys_variables[]= {
 
1699
  DRIZZLE_SYSVAR(key_cache_block_size),
 
1700
  DRIZZLE_SYSVAR(key_cache_size),
 
1701
  DRIZZLE_SYSVAR(key_cache_division_limit),
 
1702
  DRIZZLE_SYSVAR(key_cache_age_threshold),
 
1703
  DRIZZLE_SYSVAR(repair_threads),
 
1704
  DRIZZLE_SYSVAR(max_sort_file_size),
 
1705
  DRIZZLE_SYSVAR(sort_buffer_size),
 
1706
  DRIZZLE_SYSVAR(data_pointer_size),
 
1707
  NULL
 
1708
};
1512
1709
 
1513
1710
 
1514
1711
DRIZZLE_DECLARE_PLUGIN
1515
1712
{
1516
1713
  DRIZZLE_VERSION_ID,
1517
1714
  "MyISAM",
1518
 
  "2.0",
 
1715
  "1.0",
1519
1716
  "MySQL AB",
1520
1717
  "Default engine as of MySQL 3.23 with great performance",
1521
1718
  PLUGIN_LICENSE_GPL,
1522
1719
  myisam_init, /* Plugin Init */
1523
 
  NULL,           /* system variables */
1524
 
  init_options                        /* config options                  */
 
1720
  sys_variables,           /* system variables */
 
1721
  NULL                        /* config options                  */
1525
1722
}
1526
1723
DRIZZLE_DECLARE_PLUGIN_END;