~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2006 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
17
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
18
#include <drizzled/server_includes.h>
212.5.28 by Monty Taylor
Moved my_bit and my_list
19
#include <mysys/my_bit.h>
685.1.3 by Monty Taylor
Turned off stdinc - and then fixed the carnage.
20
#include "myisampack.h"
1 by brian
clean slate
21
#include "ha_myisam.h"
1130.3.28 by Monty Taylor
Moved heapdef.h and myisamdef.h to *_priv.h for easier filtering for include guard check.
22
#include "myisam_priv.h"
1130.3.1 by Monty Taylor
Moved multi_malloc into drizzled since it's not going away any time soon. Also,
23
#include "mysys/my_bit.h"
24
#include "drizzled/util/test.h"
25
#include "drizzled/error.h"
26
#include "drizzled/errmsg_print.h"
27
#include "drizzled/gettext.h"
28
#include "drizzled/session.h"
1130.3.9 by Monty Taylor
Wrapped table_proto_write.cc code in namespace drizzled.
29
#include "drizzled/plugin/client.h"
1130.3.1 by Monty Taylor
Moved multi_malloc into drizzled since it's not going away any time soon. Also,
30
#include "drizzled/table.h"
31
#include "drizzled/field/timestamp.h"
32
#include "drizzled/memory/multi_malloc.h"
1 by brian
clean slate
33
960.2.41 by Monty Taylor
Made StorageEngine name private. Added constructor param and accessor method.
34
#include <string>
1183.1.21 by Brian Aker
Fixed temp engines to no longer write out DFE. I have two designs right now
35
#include <map>
1067.4.8 by Nathan Williams
Converted all usages of cmin/cmax in plugin directory to std::min/max.
36
#include <algorithm>
960.2.41 by Monty Taylor
Made StorageEngine name private. Added constructor param and accessor method.
37
38
using namespace std;
39
40
static const string engine_name("MyISAM");
41
598 by Brian Aker
Removed mutex which are myisam from global to just engine.
42
pthread_mutex_t THR_LOCK_myisam= PTHREAD_MUTEX_INITIALIZER;
1 by brian
clean slate
43
753 by Brian Aker
Converted myisam_repair_threads to being in module for MyISAM
44
static uint32_t repair_threads;
754 by Brian Aker
Make block_size for myisam fit into plugin.
45
static uint32_t block_size;
788 by Brian Aker
Move MyISAM bits to myisam plugin
46
static uint64_t max_sort_file_size;
789 by Brian Aker
MyISAM fix.
47
static uint64_t sort_buffer_size;
753 by Brian Aker
Converted myisam_repair_threads to being in module for MyISAM
48
1 by brian
clean slate
49
/*****************************************************************************
50
** MyISAM tables
51
*****************************************************************************/
52
1039.3.1 by Stewart Smith
move bas_ext to StorageEngine instead of handler
53
static const char *ha_myisam_exts[] = {
54
  ".MYI",
55
  ".MYD",
56
  NULL
57
};
58
1208 by Brian Aker
Merge Jay
59
class MyisamEngine : public drizzled::plugin::StorageEngine
1 by brian
clean slate
60
{
960.2.41 by Monty Taylor
Made StorageEngine name private. Added constructor param and accessor method.
61
public:
964.1.4 by Monty Taylor
Moved flags into private area.
62
  MyisamEngine(string name_arg)
1130.1.4 by Monty Taylor
Moved StorageEngine into plugin namespace.
63
   : drizzled::plugin::StorageEngine(name_arg, 
64
                                     HTON_CAN_RECREATE | 
1183.1.21 by Brian Aker
Fixed temp engines to no longer write out DFE. I have two designs right now
65
                                     HTON_HAS_DATA_DICTIONARY |
1130.1.4 by Monty Taylor
Moved StorageEngine into plugin namespace.
66
                                     HTON_TEMPORARY_ONLY | 
67
                                     HTON_FILE_BASED ) {}
960.2.41 by Monty Taylor
Made StorageEngine name private. Added constructor param and accessor method.
68
1183.1.21 by Brian Aker
Fixed temp engines to no longer write out DFE. I have two designs right now
69
  ~MyisamEngine()
70
  { }
71
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
72
  virtual Cursor *create(TableShare *table,
960.2.33 by Monty Taylor
Converted MyISAM.
73
                          MEM_ROOT *mem_root)
74
  {
960.2.38 by Monty Taylor
Removed extraneous send myself to myself argument.
75
    return new (mem_root) ha_myisam(this, table);
960.2.33 by Monty Taylor
Converted MyISAM.
76
  }
1039.3.1 by Stewart Smith
move bas_ext to StorageEngine instead of handler
77
78
  const char **bas_ext() const {
79
    return ha_myisam_exts;
80
  }
1039.3.3 by Stewart Smith
Move handler::create to StorageEngine::create_table
81
1183.1.6 by Brian Aker
Simplify createTable()
82
  int doCreateTable(Session *, const char *table_name,
1183.1.18 by Brian Aker
Fixed references to doCreateTable()
83
                    Table& table_arg,
1183.1.20 by Brian Aker
Quick pass through ha_create in doTableCreate() to turn it into a reference.
84
                    HA_CREATE_INFO& ha_create_info,
1183.1.18 by Brian Aker
Fixed references to doCreateTable()
85
                    drizzled::message::Table&);
1095.3.29 by Stewart Smith
s/Impl/Implementation/
86
1183.1.7 by Brian Aker
Fix name conventions for rename table
87
  int doRenameTable(Session*, const char *from, const char *to);
1095.3.29 by Stewart Smith
s/Impl/Implementation/
88
1183.1.29 by Brian Aker
Clean up interface so that Truncate sets the propper engine when
89
  int doDropTable(Session&, const string table_name);
1183.1.21 by Brian Aker
Fixed temp engines to no longer write out DFE. I have two designs right now
90
1183.1.29 by Brian Aker
Clean up interface so that Truncate sets the propper engine when
91
  int doGetTableDefinition(Session& session,
92
                           const char* path,
1183.5.1 by Brian Aker
Extended definition interface (filename building should now be moved
93
                           const char *db,
94
                           const char *table_name,
95
                           const bool is_tmp,
96
                           drizzled::message::Table *table_proto);
1183.1.21 by Brian Aker
Fixed temp engines to no longer write out DFE. I have two designs right now
97
98
  /* Temp only engine, so do not return values. */
99
  void doGetTableNames(CachedDirectory &, string& , set<string>&) { };
100
960.2.33 by Monty Taylor
Converted MyISAM.
101
};
1 by brian
clean slate
102
1183.1.29 by Brian Aker
Clean up interface so that Truncate sets the propper engine when
103
int MyisamEngine::doGetTableDefinition(Session&,
104
                                       const char* path,
1183.5.1 by Brian Aker
Extended definition interface (filename building should now be moved
105
                                       const char *,
106
                                       const char *,
107
                                       const bool,
108
                                       drizzled::message::Table *table_proto)
1183.1.21 by Brian Aker
Fixed temp engines to no longer write out DFE. I have two designs right now
109
{
1183.1.30 by Brian Aker
Added engine internal locks (instead of the session based....)
110
  int error= 1;
1208 by Brian Aker
Merge Jay
111
  ProtoCache::iterator iter;
112
113
  pthread_mutex_lock(&proto_cache_mutex);
114
  iter= proto_cache.find(path);
115
116
  if (iter!= proto_cache.end())
117
  {
118
    if (table_proto)
119
      table_proto->CopyFrom(((*iter).second));
1183.1.30 by Brian Aker
Added engine internal locks (instead of the session based....)
120
    error= EEXIST;
1208 by Brian Aker
Merge Jay
121
  }
122
  pthread_mutex_unlock(&proto_cache_mutex);
1183.1.21 by Brian Aker
Fixed temp engines to no longer write out DFE. I have two designs right now
123
1183.1.30 by Brian Aker
Added engine internal locks (instead of the session based....)
124
  return error;
1183.1.21 by Brian Aker
Fixed temp engines to no longer write out DFE. I have two designs right now
125
}
126
1126.2.2 by Brian Aker
Remove need for protocol from myisam
127
/* 
128
  Convert to push_Warnings if you ever care about this, otherwise, it is a no-op.
129
*/
1 by brian
clean slate
130
1126.2.2 by Brian Aker
Remove need for protocol from myisam
131
static void mi_check_print_msg(MI_CHECK *,	const char* ,
132
                               const char *, va_list )
1 by brian
clean slate
133
{
134
}
135
136
137
/*
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
138
  Convert Table object to MyISAM key and column definition
1 by brian
clean slate
139
140
  SYNOPSIS
141
    table2myisam()
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
142
      table_arg   in     Table object.
1 by brian
clean slate
143
      keydef_out  out    MyISAM key definition.
144
      recinfo_out out    MyISAM column definition.
145
      records_out out    Number of fields.
146
147
  DESCRIPTION
148
    This function will allocate and initialize MyISAM key and column
149
    definition for further use in mi_create or for a check for underlying
150
    table conformance in merge engine.
151
152
    The caller needs to free *recinfo_out after use. Since *recinfo_out
1130.3.1 by Monty Taylor
Moved multi_malloc into drizzled since it's not going away any time soon. Also,
153
    and *keydef_out are allocated with a multi_malloc, *keydef_out
1 by brian
clean slate
154
    is freed automatically when *recinfo_out is freed.
155
156
  RETURN VALUE
157
    0  OK
158
    !0 error code
159
*/
160
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
161
static int table2myisam(Table *table_arg, MI_KEYDEF **keydef_out,
162
                        MI_COLUMNDEF **recinfo_out, uint32_t *records_out)
1 by brian
clean slate
163
{
482 by Brian Aker
Remove uint.
164
  uint32_t i, j, recpos, minpos, fieldpos, temp_length, length;
1 by brian
clean slate
165
  enum ha_base_keytype type= HA_KEYTYPE_BINARY;
481 by Brian Aker
Remove all of uchar.
166
  unsigned char *record;
1 by brian
clean slate
167
  KEY *pos;
168
  MI_KEYDEF *keydef;
169
  MI_COLUMNDEF *recinfo, *recinfo_pos;
170
  HA_KEYSEG *keyseg;
1000.1.3 by Brian Aker
Renamed TABLE_SHARE to TableShare
171
  TableShare *share= table_arg->s;
482 by Brian Aker
Remove uint.
172
  uint32_t options= share->db_options_in_use;
1130.3.1 by Monty Taylor
Moved multi_malloc into drizzled since it's not going away any time soon. Also,
173
  if (!(drizzled::memory::multi_malloc(false,
1 by brian
clean slate
174
          recinfo_out, (share->fields * 2 + 2) * sizeof(MI_COLUMNDEF),
175
          keydef_out, share->keys * sizeof(MI_KEYDEF),
1130.3.1 by Monty Taylor
Moved multi_malloc into drizzled since it's not going away any time soon. Also,
176
          &keyseg, (share->key_parts + share->keys) * sizeof(HA_KEYSEG),
461 by Monty Taylor
Removed NullS. bu-bye.
177
          NULL)))
971.6.11 by Eric Day
Removed purecov messages.
178
    return(HA_ERR_OUT_OF_MEM);
1 by brian
clean slate
179
  keydef= *keydef_out;
180
  recinfo= *recinfo_out;
181
  pos= table_arg->key_info;
182
  for (i= 0; i < share->keys; i++, pos++)
183
  {
249 by Brian Aker
Random key cleanup (it is a friday...)
184
    keydef[i].flag= ((uint16_t) pos->flags & (HA_NOSAME));
185
    keydef[i].key_alg= HA_KEY_ALG_BTREE;
1 by brian
clean slate
186
    keydef[i].block_length= pos->block_size;
187
    keydef[i].seg= keyseg;
188
    keydef[i].keysegs= pos->key_parts;
189
    for (j= 0; j < pos->key_parts; j++)
190
    {
191
      Field *field= pos->key_part[j].field;
192
      type= field->key_type();
193
      keydef[i].seg[j].flag= pos->key_part[j].key_part_flag;
194
195
      if (options & HA_OPTION_PACK_KEYS ||
196
          (pos->flags & (HA_PACK_KEY | HA_BINARY_PACK_KEY |
197
                         HA_SPACE_PACK_USED)))
198
      {
199
        if (pos->key_part[j].length > 8 &&
200
            (type == HA_KEYTYPE_TEXT ||
201
             (type == HA_KEYTYPE_BINARY && !field->zero_pack())))
202
        {
203
          /* No blobs here */
204
          if (j == 0)
205
            keydef[i].flag|= HA_PACK_KEY;
241 by Brian Aker
First pass of CHAR removal.
206
          if ((((int) (pos->key_part[j].length - field->decimals())) >= 4))
1 by brian
clean slate
207
            keydef[i].seg[j].flag|= HA_SPACE_PACK;
208
        }
209
        else if (j == 0 && (!(pos->flags & HA_NOSAME) || pos->key_length > 16))
210
          keydef[i].flag|= HA_BINARY_PACK_KEY;
211
      }
212
      keydef[i].seg[j].type= (int) type;
213
      keydef[i].seg[j].start= pos->key_part[j].offset;
214
      keydef[i].seg[j].length= pos->key_part[j].length;
215
      keydef[i].seg[j].bit_start= keydef[i].seg[j].bit_end=
216
        keydef[i].seg[j].bit_length= 0;
217
      keydef[i].seg[j].bit_pos= 0;
218
      keydef[i].seg[j].language= field->charset()->number;
219
220
      if (field->null_ptr)
221
      {
222
        keydef[i].seg[j].null_bit= field->null_bit;
223
        keydef[i].seg[j].null_pos= (uint) (field->null_ptr-
481 by Brian Aker
Remove all of uchar.
224
                                           (unsigned char*) table_arg->record[0]);
1 by brian
clean slate
225
      }
226
      else
227
      {
228
        keydef[i].seg[j].null_bit= 0;
229
        keydef[i].seg[j].null_pos= 0;
230
      }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
231
      if (field->type() == DRIZZLE_TYPE_BLOB)
1 by brian
clean slate
232
      {
233
        keydef[i].seg[j].flag|= HA_BLOB_PART;
234
        /* save number of bytes used to pack length */
235
        keydef[i].seg[j].bit_start= (uint) (field->pack_length() -
236
                                            share->blob_ptr_size);
237
      }
238
    }
239
    keyseg+= pos->key_parts;
240
  }
241
  if (table_arg->found_next_number_field)
242
    keydef[share->next_number_index].flag|= HA_AUTO_KEY;
243
  record= table_arg->record[0];
244
  recpos= 0;
245
  recinfo_pos= recinfo;
383.7.1 by Andrey Zhakov
Initial submit of code and tests
246
  while (recpos < (uint) share->stored_rec_length)
1 by brian
clean slate
247
  {
248
    Field **field, *found= 0;
249
    minpos= share->reclength;
250
    length= 0;
251
252
    for (field= table_arg->field; *field; field++)
253
    {
254
      if ((fieldpos= (*field)->offset(record)) >= recpos &&
255
          fieldpos <= minpos)
256
      {
257
        /* skip null fields */
258
        if (!(temp_length= (*field)->pack_length_in_rec()))
259
          continue; /* Skip null-fields */
260
        if (! found || fieldpos < minpos ||
261
            (fieldpos == minpos && temp_length < length))
262
        {
263
          minpos= fieldpos;
264
          found= *field;
265
          length= temp_length;
266
        }
267
      }
268
    }
269
    if (recpos != minpos)
270
    { // Reserved space (Null bits?)
212.6.12 by Mats Kindahl
Removing redundant use of casts in MyISAM storage for memcmp(), memcpy(), memset(), and memmove().
271
      memset(recinfo_pos, 0, sizeof(*recinfo_pos));
1 by brian
clean slate
272
      recinfo_pos->type= (int) FIELD_NORMAL;
206 by Brian Aker
Removed final uint dead types.
273
      recinfo_pos++->length= (uint16_t) (minpos - recpos);
1 by brian
clean slate
274
    }
275
    if (!found)
276
      break;
277
278
    if (found->flags & BLOB_FLAG)
279
      recinfo_pos->type= (int) FIELD_BLOB;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
280
    else if (found->type() == DRIZZLE_TYPE_VARCHAR)
1 by brian
clean slate
281
      recinfo_pos->type= FIELD_VARCHAR;
282
    else if (!(options & HA_OPTION_PACK_RECORD))
283
      recinfo_pos->type= (int) FIELD_NORMAL;
284
    else if (found->zero_pack())
285
      recinfo_pos->type= (int) FIELD_SKIP_ZERO;
286
    else
241 by Brian Aker
First pass of CHAR removal.
287
      recinfo_pos->type= (int) ((length <= 3) ?  FIELD_NORMAL : FIELD_SKIP_PRESPACE);
1 by brian
clean slate
288
    if (found->null_ptr)
289
    {
290
      recinfo_pos->null_bit= found->null_bit;
291
      recinfo_pos->null_pos= (uint) (found->null_ptr -
481 by Brian Aker
Remove all of uchar.
292
                                     (unsigned char*) table_arg->record[0]);
1 by brian
clean slate
293
    }
294
    else
295
    {
296
      recinfo_pos->null_bit= 0;
297
      recinfo_pos->null_pos= 0;
298
    }
206 by Brian Aker
Removed final uint dead types.
299
    (recinfo_pos++)->length= (uint16_t) length;
1 by brian
clean slate
300
    recpos= minpos + length;
301
  }
302
  *records_out= (uint) (recinfo_pos - recinfo);
51.1.89 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
303
  return(0);
1 by brian
clean slate
304
}
305
306
307
/*
308
  Check for underlying table conformance
309
310
  SYNOPSIS
311
    check_definition()
312
      t1_keyinfo       in    First table key definition
313
      t1_recinfo       in    First table record definition
314
      t1_keys          in    Number of keys in first table
315
      t1_recs          in    Number of records in first table
316
      t2_keyinfo       in    Second table key definition
317
      t2_recinfo       in    Second table record definition
318
      t2_keys          in    Number of keys in second table
319
      t2_recs          in    Number of records in second table
320
      strict           in    Strict check switch
321
322
  DESCRIPTION
323
    This function compares two MyISAM definitions. By intention it was done
324
    to compare merge table definition against underlying table definition.
325
    It may also be used to compare dot-frm and MYI definitions of MyISAM
326
    table as well to compare different MyISAM table definitions.
327
328
    For merge table it is not required that number of keys in merge table
329
    must exactly match number of keys in underlying table. When calling this
330
    function for underlying table conformance check, 'strict' flag must be
331
    set to false, and converted merge definition must be passed as t1_*.
332
333
    Otherwise 'strict' flag must be set to 1 and it is not required to pass
334
    converted dot-frm definition as t1_*.
335
336
  RETURN VALUE
337
    0 - Equal definitions.
338
    1 - Different definitions.
339
340
  TODO
341
    - compare FULLTEXT keys;
342
    - compare SPATIAL keys;
343
    - compare FIELD_SKIP_ZERO which is converted to FIELD_NORMAL correctly
344
      (should be corretly detected in table2myisam).
345
*/
346
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
347
static int check_definition(MI_KEYDEF *t1_keyinfo, MI_COLUMNDEF *t1_recinfo,
348
                            uint32_t t1_keys, uint32_t t1_recs,
349
                            MI_KEYDEF *t2_keyinfo, MI_COLUMNDEF *t2_recinfo,
350
                            uint32_t t2_keys, uint32_t t2_recs, bool strict)
1 by brian
clean slate
351
{
482 by Brian Aker
Remove uint.
352
  uint32_t i, j;
1 by brian
clean slate
353
  if ((strict ? t1_keys != t2_keys : t1_keys > t2_keys))
354
  {
51.1.89 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
355
    return(1);
1 by brian
clean slate
356
  }
357
  if (t1_recs != t2_recs)
358
  {
51.1.89 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
359
    return(1);
1 by brian
clean slate
360
  }
361
  for (i= 0; i < t1_keys; i++)
362
  {
363
    HA_KEYSEG *t1_keysegs= t1_keyinfo[i].seg;
364
    HA_KEYSEG *t2_keysegs= t2_keyinfo[i].seg;
365
    if (t1_keyinfo[i].keysegs != t2_keyinfo[i].keysegs ||
366
        t1_keyinfo[i].key_alg != t2_keyinfo[i].key_alg)
367
    {
51.1.89 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
368
      return(1);
1 by brian
clean slate
369
    }
370
    for (j=  t1_keyinfo[i].keysegs; j--;)
371
    {
206 by Brian Aker
Removed final uint dead types.
372
      uint8_t t1_keysegs_j__type= t1_keysegs[j].type;
1 by brian
clean slate
373
374
      /*
375
        Table migration from 4.1 to 5.1. In 5.1 a *TEXT key part is
376
        always HA_KEYTYPE_VARTEXT2. In 4.1 we had only the equivalent of
377
        HA_KEYTYPE_VARTEXT1. Since we treat both the same on MyISAM
378
        level, we can ignore a mismatch between these types.
379
      */
380
      if ((t1_keysegs[j].flag & HA_BLOB_PART) &&
381
          (t2_keysegs[j].flag & HA_BLOB_PART))
382
      {
383
        if ((t1_keysegs_j__type == HA_KEYTYPE_VARTEXT2) &&
384
            (t2_keysegs[j].type == HA_KEYTYPE_VARTEXT1))
971.6.11 by Eric Day
Removed purecov messages.
385
          t1_keysegs_j__type= HA_KEYTYPE_VARTEXT1;
1 by brian
clean slate
386
        else if ((t1_keysegs_j__type == HA_KEYTYPE_VARBINARY2) &&
387
                 (t2_keysegs[j].type == HA_KEYTYPE_VARBINARY1))
971.6.11 by Eric Day
Removed purecov messages.
388
          t1_keysegs_j__type= HA_KEYTYPE_VARBINARY1;
1 by brian
clean slate
389
      }
390
391
      if (t1_keysegs_j__type != t2_keysegs[j].type ||
392
          t1_keysegs[j].language != t2_keysegs[j].language ||
393
          t1_keysegs[j].null_bit != t2_keysegs[j].null_bit ||
394
          t1_keysegs[j].length != t2_keysegs[j].length)
395
      {
51.1.89 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
396
        return(1);
1 by brian
clean slate
397
      }
398
    }
399
  }
400
  for (i= 0; i < t1_recs; i++)
401
  {
402
    MI_COLUMNDEF *t1_rec= &t1_recinfo[i];
403
    MI_COLUMNDEF *t2_rec= &t2_recinfo[i];
404
    /*
405
      FIELD_SKIP_ZERO can be changed to FIELD_NORMAL in mi_create,
406
      see NOTE1 in mi_create.c
407
    */
408
    if ((t1_rec->type != t2_rec->type &&
409
         !(t1_rec->type == (int) FIELD_SKIP_ZERO &&
410
           t1_rec->length == 1 &&
411
           t2_rec->type == (int) FIELD_NORMAL)) ||
412
        t1_rec->length != t2_rec->length ||
413
        t1_rec->null_bit != t2_rec->null_bit)
414
    {
51.1.89 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
415
      return(1);
1 by brian
clean slate
416
    }
417
  }
51.1.89 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
418
  return(0);
1 by brian
clean slate
419
}
420
421
422
extern "C" {
423
424
volatile int *killed_ptr(MI_CHECK *param)
425
{
426
  /* In theory Unsafe conversion, but should be ok for now */
520.1.22 by Brian Aker
Second pass of thd cleanup
427
  return (int*) &(((Session *)(param->session))->killed);
1 by brian
clean slate
428
}
429
430
void mi_check_print_error(MI_CHECK *param, const char *fmt,...)
431
{
432
  param->error_printed|=1;
433
  param->out_flag|= O_DATA_LOST;
434
  va_list args;
435
  va_start(args, fmt);
436
  mi_check_print_msg(param, "error", fmt, args);
437
  va_end(args);
438
}
439
440
void mi_check_print_info(MI_CHECK *param, const char *fmt,...)
441
{
442
  va_list args;
443
  va_start(args, fmt);
444
  mi_check_print_msg(param, "info", fmt, args);
445
  va_end(args);
446
}
447
448
void mi_check_print_warning(MI_CHECK *param, const char *fmt,...)
449
{
450
  param->warning_printed=1;
451
  param->out_flag|= O_DATA_LOST;
452
  va_list args;
453
  va_start(args, fmt);
454
  mi_check_print_msg(param, "warning", fmt, args);
455
  va_end(args);
456
}
457
458
/**
459
  Report list of threads (and queries) accessing a table, thread_id of a
460
  thread that detected corruption, ource file name and line number where
461
  this corruption was detected, optional extra information (string).
462
463
  This function is intended to be used when table corruption is detected.
464
465
  @param[in] file      MI_INFO object.
466
  @param[in] message   Optional error message.
467
  @param[in] sfile     Name of source file.
468
  @param[in] sline     Line number in source file.
469
470
  @return void
471
*/
472
473
void _mi_report_crashed(MI_INFO *file, const char *message,
482 by Brian Aker
Remove uint.
474
                        const char *sfile, uint32_t sline)
1 by brian
clean slate
475
{
520.1.22 by Brian Aker
Second pass of thd cleanup
476
  Session *cur_session;
1 by brian
clean slate
477
  pthread_mutex_lock(&file->s->intern_lock);
916.1.35 by Padraig O'Sullivan
Removing the last of LIST from the MyISAM storage engine.
478
  if ((cur_session= file->in_use))
755.2.1 by Mark Atwood
replace sql_print_error etc with errmsg_print
479
    errmsg_printf(ERRMSG_LVL_ERROR, _("Got an error from thread_id=%"PRIu64", %s:%d"),
553 by Monty Taylor
Changed my_thread_id type.
480
                    cur_session->thread_id,
1 by brian
clean slate
481
                    sfile, sline);
482
  else
755.2.1 by Mark Atwood
replace sql_print_error etc with errmsg_print
483
    errmsg_printf(ERRMSG_LVL_ERROR, _("Got an error from unknown thread, %s:%d"), sfile, sline);
1 by brian
clean slate
484
  if (message)
755.2.1 by Mark Atwood
replace sql_print_error etc with errmsg_print
485
    errmsg_printf(ERRMSG_LVL_ERROR, "%s", message);
916.1.35 by Padraig O'Sullivan
Removing the last of LIST from the MyISAM storage engine.
486
  list<Session *>::iterator it= file->s->in_use->begin();
487
  while (it != file->s->in_use->end())
1 by brian
clean slate
488
  {
755.2.1 by Mark Atwood
replace sql_print_error etc with errmsg_print
489
    errmsg_printf(ERRMSG_LVL_ERROR, "%s", _("Unknown thread accessing table"));
916.1.35 by Padraig O'Sullivan
Removing the last of LIST from the MyISAM storage engine.
490
    ++it;
1 by brian
clean slate
491
  }
492
  pthread_mutex_unlock(&file->s->intern_lock);
493
}
494
77.1.13 by Monty Taylor
Fixed wonky linkage thing.
495
}
1 by brian
clean slate
496
1130.1.4 by Monty Taylor
Moved StorageEngine into plugin namespace.
497
ha_myisam::ha_myisam(drizzled::plugin::StorageEngine *engine_arg,
498
                     TableShare *table_arg)
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
499
  : Cursor(engine_arg, table_arg),
1130.1.4 by Monty Taylor
Moved StorageEngine into plugin namespace.
500
    file(0),
501
    int_table_flags(HA_NULL_IN_KEY |
502
                    HA_DUPLICATE_POS |
503
                    HA_CAN_INDEX_BLOBS |
504
                    HA_AUTO_PART_KEY |
505
                    HA_NO_TRANSACTIONS |
506
                    HA_HAS_RECORDS |
507
                    HA_STATS_RECORDS_IS_EXACT |
508
                    HA_NEED_READ_RANGE_BUFFER |
509
                    HA_MRR_CANT_SORT),
510
     can_enable_indexes(1)
1 by brian
clean slate
511
{}
512
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
513
Cursor *ha_myisam::clone(MEM_ROOT *mem_root)
1 by brian
clean slate
514
{
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
515
  ha_myisam *new_handler= static_cast <ha_myisam *>(Cursor::clone(mem_root));
1 by brian
clean slate
516
  if (new_handler)
517
    new_handler->file->state= file->state;
518
  return new_handler;
519
}
520
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
521
const char *ha_myisam::index_type(uint32_t )
1 by brian
clean slate
522
{
74 by Brian Aker
More removal of FT from MyISAM
523
  return "BTREE";
1 by brian
clean slate
524
}
525
526
/* Name is here without an extension */
482 by Brian Aker
Remove uint.
527
int ha_myisam::open(const char *name, int mode, uint32_t test_if_locked)
1 by brian
clean slate
528
{
529
  MI_KEYDEF *keyinfo;
530
  MI_COLUMNDEF *recinfo= 0;
482 by Brian Aker
Remove uint.
531
  uint32_t recs;
532
  uint32_t i;
1 by brian
clean slate
533
534
  /*
535
    If the user wants to have memory mapped data files, add an
536
    open_flag. Do not memory map temporary tables because they are
537
    expected to be inserted and thus extended a lot. Memory mapping is
538
    efficient for files that keep their size, but very inefficient for
539
    growing files. Using an open_flag instead of calling mi_extra(...
540
    HA_EXTRA_MMAP ...) after mi_open() has the advantage that the
541
    mapping is not repeated for every open, but just done on the initial
542
    open, when the MyISAM share is created. Everytime the server
543
    requires to open a new instance of a table it calls this method. We
544
    will always supply HA_OPEN_MMAP for a permanent table. However, the
545
    MyISAM storage engine will ignore this flag if this is a secondary
546
    open of a table that is in use by other threads already (if the
547
    MyISAM share exists already).
548
  */
1115.1.7 by Brian Aker
Remove dead myisam program
549
  if (!(file=mi_open(name, mode, test_if_locked)))
1 by brian
clean slate
550
    return (my_errno ? my_errno : -1);
1115.1.7 by Brian Aker
Remove dead myisam program
551
1 by brian
clean slate
552
  if (!table->s->tmp_table) /* No need to perform a check for tmp table */
553
  {
554
    if ((my_errno= table2myisam(table, &keyinfo, &recinfo, &recs)))
555
    {
556
      goto err;
557
    }
558
    if (check_definition(keyinfo, recinfo, table->s->keys, recs,
559
                         file->s->keyinfo, file->s->rec,
560
                         file->s->base.keys, file->s->base.fields, true))
561
    {
562
      my_errno= HA_ERR_CRASHED;
563
      goto err;
564
    }
565
  }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
566
1 by brian
clean slate
567
  if (test_if_locked & (HA_OPEN_IGNORE_IF_LOCKED | HA_OPEN_TMP_TABLE))
398.1.10 by Monty Taylor
Actually removed VOID() this time.
568
    mi_extra(file, HA_EXTRA_NO_WAIT_LOCK, 0);
1 by brian
clean slate
569
570
  info(HA_STATUS_NO_LOCK | HA_STATUS_VARIABLE | HA_STATUS_CONST);
571
  if (!(test_if_locked & HA_OPEN_WAIT_IF_LOCKED))
398.1.10 by Monty Taylor
Actually removed VOID() this time.
572
    mi_extra(file, HA_EXTRA_WAIT_LOCK, 0);
1 by brian
clean slate
573
  if (!table->s->db_record_offset)
574
    int_table_flags|=HA_REC_NOT_IN_SEQ;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
575
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
576
  keys_with_parts.reset();
1 by brian
clean slate
577
  for (i= 0; i < table->s->keys; i++)
578
  {
579
    table->key_info[i].block_size= file->s->keyinfo[i].block_length;
580
581
    KEY_PART_INFO *kp= table->key_info[i].key_part;
582
    KEY_PART_INFO *kp_end= kp + table->key_info[i].key_parts;
583
    for (; kp != kp_end; kp++)
584
    {
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
585
      if (!kp->field->part_of_key.test(i))
1 by brian
clean slate
586
      {
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
587
        keys_with_parts.set(i);
1 by brian
clean slate
588
        break;
589
      }
590
    }
591
  }
592
  my_errno= 0;
593
  goto end;
594
 err:
595
  this->close();
596
 end:
597
  /*
1130.3.1 by Monty Taylor
Moved multi_malloc into drizzled since it's not going away any time soon. Also,
598
    Both recinfo and keydef are allocated by multi_malloc(), thus only
1 by brian
clean slate
599
    recinfo must be freed.
600
  */
601
  if (recinfo)
481 by Brian Aker
Remove all of uchar.
602
    free((unsigned char*) recinfo);
1 by brian
clean slate
603
  return my_errno;
604
}
605
606
int ha_myisam::close(void)
607
{
608
  MI_INFO *tmp=file;
609
  file=0;
610
  return mi_close(tmp);
611
}
612
481 by Brian Aker
Remove all of uchar.
613
int ha_myisam::write_row(unsigned char *buf)
1 by brian
clean slate
614
{
615
  ha_statistic_increment(&SSV::ha_write_count);
616
617
  /*
618
    If we have an auto_increment column and we are writing a changed row
619
    or a new row, then update the auto_increment value in the record.
620
  */
621
  if (table->next_number_field && buf == table->record[0])
622
  {
623
    int error;
624
    if ((error= update_auto_increment()))
625
      return error;
626
  }
627
  return mi_write(file,buf);
628
}
629
630
520.1.22 by Brian Aker
Second pass of thd cleanup
631
int ha_myisam::repair(Session *session, MI_CHECK &param, bool do_optimize)
1 by brian
clean slate
632
{
633
  int error=0;
789 by Brian Aker
MyISAM fix.
634
  uint32_t local_testflag= param.testflag;
1 by brian
clean slate
635
  bool optimize_done= !do_optimize, statistics_done=0;
520.1.22 by Brian Aker
Second pass of thd cleanup
636
  const char *old_proc_info= session->get_proc_info();
1 by brian
clean slate
637
  char fixed_name[FN_REFLEN];
638
  MYISAM_SHARE* share = file->s;
639
  ha_rows rows= file->state->records;
640
641
  /*
642
    Normally this method is entered with a properly opened table. If the
643
    repair fails, it can be repeated with more elaborate options. Under
644
    special circumstances it can happen that a repair fails so that it
645
    closed the data file and cannot re-open it. In this case file->dfile
646
    is set to -1. We must not try another repair without an open data
647
    file. (Bug #25289)
648
  */
649
  if (file->dfile == -1)
650
  {
755.2.1 by Mark Atwood
replace sql_print_error etc with errmsg_print
651
    errmsg_printf(ERRMSG_LVL_INFO, "Retrying repair of: '%s' failed. "
1 by brian
clean slate
652
                          "Please try REPAIR EXTENDED or myisamchk",
653
                          table->s->path.str);
51.1.89 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
654
    return(HA_ADMIN_FAILED);
1 by brian
clean slate
655
  }
656
657
  param.db_name=    table->s->db.str;
658
  param.table_name= table->alias;
659
  param.tmpfile_createflag = O_RDWR | O_TRUNC;
660
  param.using_global_keycache = 1;
520.1.22 by Brian Aker
Second pass of thd cleanup
661
  param.session= session;
1 by brian
clean slate
662
  param.out_flag= 0;
779.3.20 by Monty Taylor
Fixed Solaris warnings for MyISAM.
663
  param.sort_buffer_length= (size_t)sort_buffer_size;
641.4.3 by Toru Maesaka
Final pass of replacing MySQL's my_stpcpy() with appropriate libc calls
664
  strcpy(fixed_name,file->filename);
1 by brian
clean slate
665
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
666
  // Don't lock tables if we have used LOCK Table
1054.1.8 by Brian Aker
Remove lock_tables list from session.
667
  if (mi_lock_database(file, table->s->tmp_table ? F_EXTRA_LCK : F_WRLCK))
1 by brian
clean slate
668
  {
669
    mi_check_print_error(&param,ER(ER_CANT_LOCK),my_errno);
51.1.89 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
670
    return(HA_ADMIN_FAILED);
1 by brian
clean slate
671
  }
672
673
  if (!do_optimize ||
674
      ((file->state->del || share->state.split != file->state->records) &&
675
       (!(param.testflag & T_QUICK) ||
676
	!(share->state.changed & STATE_NOT_OPTIMIZED_KEYS))))
677
  {
678
    uint64_t key_map= ((local_testflag & T_CREATE_MISSING_KEYS) ?
679
			mi_get_mask_all_keys_active(share->base.keys) :
680
			share->state.key_map);
482 by Brian Aker
Remove uint.
681
    uint32_t testflag=param.testflag;
1 by brian
clean slate
682
    if (mi_test_if_sort_rep(file,file->state->records,key_map,0) &&
683
	(local_testflag & T_REP_BY_SORT))
684
    {
685
      local_testflag|= T_STATISTICS;
686
      param.testflag|= T_STATISTICS;		// We get this for free
687
      statistics_done=1;
753 by Brian Aker
Converted myisam_repair_threads to being in module for MyISAM
688
      if (repair_threads > 1)
1 by brian
clean slate
689
      {
690
        char buf[40];
691
        /* TODO: respect myisam_repair_threads variable */
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
692
        snprintf(buf, 40, "Repair with %d threads", my_count_bits(key_map));
520.1.22 by Brian Aker
Second pass of thd cleanup
693
        session->set_proc_info(buf);
1 by brian
clean slate
694
        error = mi_repair_parallel(&param, file, fixed_name,
695
            param.testflag & T_QUICK);
520.1.22 by Brian Aker
Second pass of thd cleanup
696
        session->set_proc_info("Repair done"); // to reset proc_info, as
1 by brian
clean slate
697
                                      // it was pointing to local buffer
698
      }
699
      else
700
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
701
        session->set_proc_info("Repair by sorting");
1 by brian
clean slate
702
        error = mi_repair_by_sort(&param, file, fixed_name,
703
            param.testflag & T_QUICK);
704
      }
705
    }
706
    else
707
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
708
      session->set_proc_info("Repair with keycache");
1 by brian
clean slate
709
      param.testflag &= ~T_REP_BY_SORT;
710
      error=  mi_repair(&param, file, fixed_name,
711
			param.testflag & T_QUICK);
712
    }
713
    param.testflag=testflag;
714
    optimize_done=1;
715
  }
716
  if (!error)
717
  {
718
    if ((local_testflag & T_SORT_INDEX) &&
719
	(share->state.changed & STATE_NOT_SORTED_PAGES))
720
    {
721
      optimize_done=1;
520.1.22 by Brian Aker
Second pass of thd cleanup
722
      session->set_proc_info("Sorting index");
1 by brian
clean slate
723
      error=mi_sort_index(&param,file,fixed_name);
724
    }
725
    if (!statistics_done && (local_testflag & T_STATISTICS))
726
    {
727
      if (share->state.changed & STATE_NOT_ANALYZED)
728
      {
729
	optimize_done=1;
520.1.22 by Brian Aker
Second pass of thd cleanup
730
	session->set_proc_info("Analyzing");
1 by brian
clean slate
731
	error = chk_key(&param, file);
732
      }
733
      else
734
	local_testflag&= ~T_STATISTICS;		// Don't update statistics
735
    }
736
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
737
  session->set_proc_info("Saving state");
1 by brian
clean slate
738
  if (!error)
739
  {
740
    if ((share->state.changed & STATE_CHANGED) || mi_is_crashed(file))
741
    {
742
      share->state.changed&= ~(STATE_CHANGED | STATE_CRASHED |
743
			       STATE_CRASHED_ON_REPAIR);
744
      file->update|=HA_STATE_CHANGED | HA_STATE_ROW_CHANGED;
745
    }
746
    /*
747
      the following 'if', thought conceptually wrong,
748
      is a useful optimization nevertheless.
749
    */
750
    if (file->state != &file->s->state.state)
751
      file->s->state.state = *file->state;
752
    if (file->s->base.auto_key)
753
      update_auto_increment_key(&param, file, 1);
754
    if (optimize_done)
755
      error = update_state_info(&param, file,
756
				UPDATE_TIME | UPDATE_OPEN_COUNT |
757
				(local_testflag &
758
				 T_STATISTICS ? UPDATE_STAT : 0));
759
    info(HA_STATUS_NO_LOCK | HA_STATUS_TIME | HA_STATUS_VARIABLE |
760
	 HA_STATUS_CONST);
761
    if (rows != file->state->records && ! (param.testflag & T_VERY_SILENT))
762
    {
763
      char llbuff[22],llbuff2[22];
764
      mi_check_print_warning(&param,"Number of rows changed from %s to %s",
765
			     llstr(rows,llbuff),
766
			     llstr(file->state->records,llbuff2));
767
    }
768
  }
769
  else
770
  {
771
    mi_mark_crashed_on_repair(file);
772
    file->update |= HA_STATE_CHANGED | HA_STATE_ROW_CHANGED;
773
    update_state_info(&param, file, 0);
774
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
775
  session->set_proc_info(old_proc_info);
1054.1.8 by Brian Aker
Remove lock_tables list from session.
776
  mi_lock_database(file,F_UNLCK);
777
51.1.89 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
778
  return(error ? HA_ADMIN_FAILED :
1 by brian
clean slate
779
	      !optimize_done ? HA_ADMIN_ALREADY_DONE : HA_ADMIN_OK);
780
}
781
782
783
/*
784
  Disable indexes, making it persistent if requested.
785
786
  SYNOPSIS
787
    disable_indexes()
788
    mode        mode of operation:
789
                HA_KEY_SWITCH_NONUNIQ      disable all non-unique keys
790
                HA_KEY_SWITCH_ALL          disable all keys
791
                HA_KEY_SWITCH_NONUNIQ_SAVE dis. non-uni. and make persistent
792
                HA_KEY_SWITCH_ALL_SAVE     dis. all keys and make persistent
793
794
  IMPLEMENTATION
795
    HA_KEY_SWITCH_NONUNIQ       is not implemented.
796
    HA_KEY_SWITCH_ALL_SAVE      is not implemented.
797
798
  RETURN
799
    0  ok
800
    HA_ERR_WRONG_COMMAND  mode not implemented.
801
*/
802
482 by Brian Aker
Remove uint.
803
int ha_myisam::disable_indexes(uint32_t mode)
1 by brian
clean slate
804
{
805
  int error;
806
807
  if (mode == HA_KEY_SWITCH_ALL)
808
  {
809
    /* call a storage engine function to switch the key map */
810
    error= mi_disable_indexes(file);
811
  }
812
  else if (mode == HA_KEY_SWITCH_NONUNIQ_SAVE)
813
  {
814
    mi_extra(file, HA_EXTRA_NO_KEYS, 0);
815
    info(HA_STATUS_CONST);                        // Read new key info
816
    error= 0;
817
  }
818
  else
819
  {
820
    /* mode not implemented */
821
    error= HA_ERR_WRONG_COMMAND;
822
  }
823
  return error;
824
}
825
826
827
/*
828
  Enable indexes, making it persistent if requested.
829
830
  SYNOPSIS
831
    enable_indexes()
832
    mode        mode of operation:
833
                HA_KEY_SWITCH_NONUNIQ      enable all non-unique keys
834
                HA_KEY_SWITCH_ALL          enable all keys
835
                HA_KEY_SWITCH_NONUNIQ_SAVE en. non-uni. and make persistent
836
                HA_KEY_SWITCH_ALL_SAVE     en. all keys and make persistent
837
838
  DESCRIPTION
839
    Enable indexes, which might have been disabled by disable_index() before.
840
    The modes without _SAVE work only if both data and indexes are empty,
841
    since the MyISAM repair would enable them persistently.
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
842
    To be sure in these cases, call Cursor::delete_all_rows() before.
1 by brian
clean slate
843
844
  IMPLEMENTATION
845
    HA_KEY_SWITCH_NONUNIQ       is not implemented.
846
    HA_KEY_SWITCH_ALL_SAVE      is not implemented.
847
848
  RETURN
849
    0  ok
850
    !=0  Error, among others:
851
    HA_ERR_CRASHED  data or index is non-empty. Delete all rows and retry.
852
    HA_ERR_WRONG_COMMAND  mode not implemented.
853
*/
854
482 by Brian Aker
Remove uint.
855
int ha_myisam::enable_indexes(uint32_t mode)
1 by brian
clean slate
856
{
857
  int error;
858
859
  if (mi_is_all_keys_active(file->s->state.key_map, file->s->base.keys))
860
  {
861
    /* All indexes are enabled already. */
862
    return 0;
863
  }
864
865
  if (mode == HA_KEY_SWITCH_ALL)
866
  {
867
    error= mi_enable_indexes(file);
868
    /*
869
       Do not try to repair on error,
870
       as this could make the enabled state persistent,
871
       but mode==HA_KEY_SWITCH_ALL forbids it.
872
    */
873
  }
874
  else if (mode == HA_KEY_SWITCH_NONUNIQ_SAVE)
875
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
876
    Session *session=current_session;
1 by brian
clean slate
877
    MI_CHECK param;
520.1.22 by Brian Aker
Second pass of thd cleanup
878
    const char *save_proc_info= session->get_proc_info();
879
    session->set_proc_info("Creating index");
1 by brian
clean slate
880
    myisamchk_init(&param);
881
    param.op_name= "recreating_index";
882
    param.testflag= (T_SILENT | T_REP_BY_SORT | T_QUICK |
883
                     T_CREATE_MISSING_KEYS);
884
    param.myf_rw&= ~MY_WAIT_IF_FULL;
779.3.20 by Monty Taylor
Fixed Solaris warnings for MyISAM.
885
    param.sort_buffer_length=  (size_t)sort_buffer_size;
1115.1.3 by Brian Aker
Remove final bits of "myisam" specific from drizzled.cc
886
    param.stats_method= MI_STATS_METHOD_NULLS_NOT_EQUAL;
520.1.22 by Brian Aker
Second pass of thd cleanup
887
    if ((error= (repair(session,param,0) != HA_ADMIN_OK)) && param.retry_repair)
1 by brian
clean slate
888
    {
755.2.1 by Mark Atwood
replace sql_print_error etc with errmsg_print
889
      errmsg_printf(ERRMSG_LVL_WARN, "Warning: Enabling keys got errno %d on %s.%s, retrying",
1 by brian
clean slate
890
                        my_errno, param.db_name, param.table_name);
891
      /* Repairing by sort failed. Now try standard repair method. */
892
      param.testflag&= ~(T_REP_BY_SORT | T_QUICK);
520.1.22 by Brian Aker
Second pass of thd cleanup
893
      error= (repair(session,param,0) != HA_ADMIN_OK);
1 by brian
clean slate
894
      /*
895
        If the standard repair succeeded, clear all error messages which
896
        might have been set by the first repair. They can still be seen
897
        with SHOW WARNINGS then.
898
      */
899
      if (! error)
520.1.22 by Brian Aker
Second pass of thd cleanup
900
        session->clear_error();
1 by brian
clean slate
901
    }
902
    info(HA_STATUS_CONST);
520.1.22 by Brian Aker
Second pass of thd cleanup
903
    session->set_proc_info(save_proc_info);
1 by brian
clean slate
904
  }
905
  else
906
  {
907
    /* mode not implemented */
908
    error= HA_ERR_WRONG_COMMAND;
909
  }
910
  return error;
911
}
912
913
914
/*
915
  Test if indexes are disabled.
916
917
918
  SYNOPSIS
919
    indexes_are_disabled()
920
      no parameters
921
922
923
  RETURN
924
    0  indexes are not disabled
925
    1  all indexes are disabled
926
   [2  non-unique indexes are disabled - NOT YET IMPLEMENTED]
927
*/
928
929
int ha_myisam::indexes_are_disabled(void)
930
{
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
931
1 by brian
clean slate
932
  return mi_indexes_are_disabled(file);
933
}
934
935
936
/*
937
  prepare for a many-rows insert operation
938
  e.g. - disable indexes (if they can be recreated fast) or
939
  activate special bulk-insert optimizations
940
941
  SYNOPSIS
942
    start_bulk_insert(rows)
943
    rows        Rows to be inserted
944
                0 if we don't know
945
946
  NOTICE
947
    Do not forget to call end_bulk_insert() later!
948
*/
949
950
void ha_myisam::start_bulk_insert(ha_rows rows)
951
{
520.1.22 by Brian Aker
Second pass of thd cleanup
952
  Session *session= current_session;
1116.1.2 by Brian Aker
Remove options which are just for internal optimizations.
953
  ulong size= session->variables.read_buff_size;
1 by brian
clean slate
954
955
  /* don't enable row cache if too few rows */
956
  if (! rows || (rows > MI_MIN_ROWS_TO_USE_WRITE_CACHE))
957
    mi_extra(file, HA_EXTRA_WRITE_CACHE, (void*) &size);
958
959
  can_enable_indexes= mi_is_all_keys_active(file->s->state.key_map,
960
                                            file->s->base.keys);
961
362 by Brian Aker
No more dead special flags...
962
  /*
963
    Only disable old index if the table was empty and we are inserting
964
    a lot of rows.
965
    We should not do this for only a few rows as this is slower and
966
    we don't want to update the key statistics based of only a few rows.
967
  */
968
  if (file->state->records == 0 && can_enable_indexes &&
969
      (!rows || rows >= MI_MIN_ROWS_TO_DISABLE_INDEXES))
970
    mi_disable_non_unique_index(file,rows);
971
  else
1 by brian
clean slate
972
    if (!file->bulk_insert &&
973
        (!rows || rows >= MI_MIN_ROWS_TO_USE_BULK_INSERT))
974
    {
779.3.20 by Monty Taylor
Fixed Solaris warnings for MyISAM.
975
      mi_init_bulk_insert(file,
976
                          (size_t)session->variables.bulk_insert_buff_size,
977
                          rows);
1 by brian
clean slate
978
    }
979
}
980
981
/*
982
  end special bulk-insert optimizations,
983
  which have been activated by start_bulk_insert().
984
985
  SYNOPSIS
986
    end_bulk_insert()
987
    no arguments
988
989
  RETURN
990
    0     OK
991
    != 0  Error
992
*/
993
994
int ha_myisam::end_bulk_insert()
995
{
996
  mi_end_bulk_insert(file);
997
  int err=mi_extra(file, HA_EXTRA_NO_CACHE, 0);
998
  return err ? err : can_enable_indexes ?
999
                     enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE) : 0;
1000
}
1001
1002
1003
481 by Brian Aker
Remove all of uchar.
1004
int ha_myisam::update_row(const unsigned char *old_data, unsigned char *new_data)
1 by brian
clean slate
1005
{
1006
  ha_statistic_increment(&SSV::ha_update_count);
1007
  if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_UPDATE)
1008
    table->timestamp_field->set_time();
1009
  return mi_update(file,old_data,new_data);
1010
}
1011
481 by Brian Aker
Remove all of uchar.
1012
int ha_myisam::delete_row(const unsigned char *buf)
1 by brian
clean slate
1013
{
1014
  ha_statistic_increment(&SSV::ha_delete_count);
1015
  return mi_delete(file,buf);
1016
}
1017
1018
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
1019
int ha_myisam::index_init(uint32_t idx, bool )
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1020
{
1 by brian
clean slate
1021
  active_index=idx;
163 by Brian Aker
Merge Monty's code.
1022
  //in_range_read= false;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1023
  return 0;
1 by brian
clean slate
1024
}
1025
1026
1027
int ha_myisam::index_end()
1028
{
1029
  active_index=MAX_KEY;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1030
  return 0;
1 by brian
clean slate
1031
}
1032
1033
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
1034
uint32_t ha_myisam::index_flags(uint32_t inx, uint32_t, bool) const
1035
{
1036
  return ((table_share->key_info[inx].algorithm == HA_KEY_ALG_FULLTEXT) ?
1037
          0 : HA_READ_NEXT | HA_READ_PREV | HA_READ_RANGE |
1038
          HA_READ_ORDER | HA_KEYREAD_ONLY |
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
1039
          (keys_with_parts.test(inx)?0:HA_DO_INDEX_COND_PUSHDOWN));
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
1040
}
1041
1042
481 by Brian Aker
Remove all of uchar.
1043
int ha_myisam::index_read_map(unsigned char *buf, const unsigned char *key,
1 by brian
clean slate
1044
                              key_part_map keypart_map,
1045
                              enum ha_rkey_function find_flag)
1046
{
51.1.89 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
1047
  assert(inited==INDEX);
1 by brian
clean slate
1048
  ha_statistic_increment(&SSV::ha_read_key_count);
1049
  int error=mi_rkey(file, buf, active_index, key, keypart_map, find_flag);
1050
  table->status=error ? STATUS_NOT_FOUND: 0;
1051
  return error;
1052
}
1053
482 by Brian Aker
Remove uint.
1054
int ha_myisam::index_read_idx_map(unsigned char *buf, uint32_t index, const unsigned char *key,
1 by brian
clean slate
1055
                                  key_part_map keypart_map,
1056
                                  enum ha_rkey_function find_flag)
1057
{
1058
  ha_statistic_increment(&SSV::ha_read_key_count);
1059
  int error=mi_rkey(file, buf, index, key, keypart_map, find_flag);
1060
  table->status=error ? STATUS_NOT_FOUND: 0;
1061
  return error;
1062
}
1063
481 by Brian Aker
Remove all of uchar.
1064
int ha_myisam::index_read_last_map(unsigned char *buf, const unsigned char *key,
1 by brian
clean slate
1065
                                   key_part_map keypart_map)
1066
{
51.1.89 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
1067
  assert(inited==INDEX);
1 by brian
clean slate
1068
  ha_statistic_increment(&SSV::ha_read_key_count);
1069
  int error=mi_rkey(file, buf, active_index, key, keypart_map,
1070
                    HA_READ_PREFIX_LAST);
1071
  table->status=error ? STATUS_NOT_FOUND: 0;
51.1.89 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
1072
  return(error);
1 by brian
clean slate
1073
}
1074
481 by Brian Aker
Remove all of uchar.
1075
int ha_myisam::index_next(unsigned char *buf)
1 by brian
clean slate
1076
{
51.1.89 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
1077
  assert(inited==INDEX);
1 by brian
clean slate
1078
  ha_statistic_increment(&SSV::ha_read_next_count);
1079
  int error=mi_rnext(file,buf,active_index);
1080
  table->status=error ? STATUS_NOT_FOUND: 0;
1081
  return error;
1082
}
1083
481 by Brian Aker
Remove all of uchar.
1084
int ha_myisam::index_prev(unsigned char *buf)
1 by brian
clean slate
1085
{
51.1.89 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
1086
  assert(inited==INDEX);
1 by brian
clean slate
1087
  ha_statistic_increment(&SSV::ha_read_prev_count);
1088
  int error=mi_rprev(file,buf, active_index);
1089
  table->status=error ? STATUS_NOT_FOUND: 0;
1090
  return error;
1091
}
1092
481 by Brian Aker
Remove all of uchar.
1093
int ha_myisam::index_first(unsigned char *buf)
1 by brian
clean slate
1094
{
51.1.89 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
1095
  assert(inited==INDEX);
1 by brian
clean slate
1096
  ha_statistic_increment(&SSV::ha_read_first_count);
1097
  int error=mi_rfirst(file, buf, active_index);
1098
  table->status=error ? STATUS_NOT_FOUND: 0;
1099
  return error;
1100
}
1101
481 by Brian Aker
Remove all of uchar.
1102
int ha_myisam::index_last(unsigned char *buf)
1 by brian
clean slate
1103
{
51.1.89 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
1104
  assert(inited==INDEX);
1 by brian
clean slate
1105
  ha_statistic_increment(&SSV::ha_read_last_count);
1106
  int error=mi_rlast(file, buf, active_index);
1107
  table->status=error ? STATUS_NOT_FOUND: 0;
1108
  return error;
1109
}
1110
481 by Brian Aker
Remove all of uchar.
1111
int ha_myisam::index_next_same(unsigned char *buf,
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
1112
			       const unsigned char *,
1113
			       uint32_t )
1 by brian
clean slate
1114
{
1115
  int error;
51.1.89 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
1116
  assert(inited==INDEX);
1 by brian
clean slate
1117
  ha_statistic_increment(&SSV::ha_read_next_count);
1118
  do
1119
  {
1120
    error= mi_rnext_same(file,buf);
1121
  } while (error == HA_ERR_RECORD_DELETED);
1122
  table->status=error ? STATUS_NOT_FOUND: 0;
1123
  return error;
1124
}
1125
1126
int ha_myisam::read_range_first(const key_range *start_key,
1127
		 	        const key_range *end_key,
1128
			        bool eq_range_arg,
1129
                                bool sorted /* ignored */)
1130
{
1131
  int res;
1132
  //if (!eq_range_arg)
163 by Brian Aker
Merge Monty's code.
1133
  //  in_range_read= true;
1 by brian
clean slate
1134
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
1135
  res= Cursor::read_range_first(start_key, end_key, eq_range_arg, sorted);
1 by brian
clean slate
1136
1137
  //if (res)
163 by Brian Aker
Merge Monty's code.
1138
  //  in_range_read= false;
1 by brian
clean slate
1139
  return res;
1140
}
1141
1142
1143
int ha_myisam::read_range_next()
1144
{
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
1145
  int res= Cursor::read_range_next();
1 by brian
clean slate
1146
  //if (res)
163 by Brian Aker
Merge Monty's code.
1147
  //  in_range_read= false;
1 by brian
clean slate
1148
  return res;
1149
}
1150
1151
1152
int ha_myisam::rnd_init(bool scan)
1153
{
1154
  if (scan)
1155
    return mi_scan_init(file);
1156
  return mi_reset(file);                        // Free buffers
1157
}
1158
481 by Brian Aker
Remove all of uchar.
1159
int ha_myisam::rnd_next(unsigned char *buf)
1 by brian
clean slate
1160
{
1161
  ha_statistic_increment(&SSV::ha_read_rnd_next_count);
1162
  int error=mi_scan(file, buf);
1163
  table->status=error ? STATUS_NOT_FOUND: 0;
1164
  return error;
1165
}
1166
481 by Brian Aker
Remove all of uchar.
1167
int ha_myisam::restart_rnd_next(unsigned char *buf, unsigned char *pos)
1 by brian
clean slate
1168
{
1169
  return rnd_pos(buf,pos);
1170
}
1171
481 by Brian Aker
Remove all of uchar.
1172
int ha_myisam::rnd_pos(unsigned char *buf, unsigned char *pos)
1 by brian
clean slate
1173
{
1174
  ha_statistic_increment(&SSV::ha_read_rnd_count);
1175
  int error=mi_rrnd(file, buf, my_get_ptr(pos,ref_length));
1176
  table->status=error ? STATUS_NOT_FOUND: 0;
1177
  return error;
1178
}
1179
1180
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
1181
void ha_myisam::position(const unsigned char *)
1 by brian
clean slate
1182
{
1183
  my_off_t row_position= mi_position(file);
1184
  my_store_ptr(ref, ref_length, row_position);
1185
}
1186
482 by Brian Aker
Remove uint.
1187
int ha_myisam::info(uint32_t flag)
1 by brian
clean slate
1188
{
1189
  MI_ISAMINFO misam_info;
1190
  char name_buff[FN_REFLEN];
1191
1192
  (void) mi_status(file,&misam_info,flag);
1193
  if (flag & HA_STATUS_VARIABLE)
1194
  {
1195
    stats.records=           misam_info.records;
1196
    stats.deleted=           misam_info.deleted;
1197
    stats.data_file_length=  misam_info.data_file_length;
1198
    stats.index_file_length= misam_info.index_file_length;
1199
    stats.delete_length=     misam_info.delete_length;
1200
    stats.check_time=        misam_info.check_time;
1201
    stats.mean_rec_length=   misam_info.mean_reclength;
1202
  }
1203
  if (flag & HA_STATUS_CONST)
1204
  {
1000.1.3 by Brian Aker
Renamed TABLE_SHARE to TableShare
1205
    TableShare *share= table->s;
1 by brian
clean slate
1206
    stats.max_data_file_length=  misam_info.max_data_file_length;
1207
    stats.max_index_file_length= misam_info.max_index_file_length;
1208
    stats.create_time= misam_info.create_time;
1209
    ref_length= misam_info.reflength;
1210
    share->db_options_in_use= misam_info.options;
754 by Brian Aker
Make block_size for myisam fit into plugin.
1211
    stats.block_size= block_size;        /* record block size */
1 by brian
clean slate
1212
1213
    /* Update share */
1214
    if (share->tmp_table == NO_TMP_TABLE)
1215
      pthread_mutex_lock(&share->mutex);
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
1216
    set_prefix(share->keys_in_use, share->keys);
1095.6.1 by Padraig O'Sullivan
Fix for 32 bit Solaris builds. Issue was with conversion of 64-bit unsigned
1217
    /*
1218
     * Due to bug 394932 (32-bit solaris build failure), we need
1219
     * to convert the uint64_t key_map member of the misam_info
1220
     * structure in to a std::bitset so that we can logically and
1221
     * it with the share->key_in_use key_map.
1222
     */
1223
    ostringstream ostr;
1224
    string binary_key_map;
1225
    uint64_t num= misam_info.key_map;
1226
    /*
1227
     * Convert the uint64_t to a binary
1228
     * string representation of it.
1229
     */
1230
    while (num > 0)
1231
    {
1232
      uint64_t bin_digit= num % 2;
1233
      ostr << bin_digit;
1234
      num/= 2;
1235
    }
1236
    binary_key_map.append(ostr.str());
1237
    /*
1238
     * Now we have the binary string representation of the
1239
     * flags, we need to fill that string representation out
1240
     * with the appropriate number of bits. This is needed
1241
     * since key_map is declared as a std::bitset of a certain bit
1242
     * width that depends on the MAX_INDEXES variable. 
1243
     */
1244
    if (MAX_INDEXES <= 64)
1245
    {
1246
      size_t len= 72 - binary_key_map.length();
1247
      string all_zeros(len, '0');
1248
      binary_key_map.insert(binary_key_map.begin(),
1249
                            all_zeros.begin(),
1250
                            all_zeros.end());
1251
    }
1252
    else
1253
    {
1254
      size_t len= (MAX_INDEXES + 7) / 8 * 8;
1255
      string all_zeros(len, '0');
1256
      binary_key_map.insert(binary_key_map.begin(),
1257
                            all_zeros.begin(),
1258
                            all_zeros.end());
1259
    }
1260
    key_map tmp_map(binary_key_map);
1261
    share->keys_in_use&= tmp_map;
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
1262
    share->keys_for_keyread&= share->keys_in_use;
1 by brian
clean slate
1263
    share->db_record_offset= misam_info.record_offset;
1264
    if (share->key_parts)
212.6.12 by Mats Kindahl
Removing redundant use of casts in MyISAM storage for memcmp(), memcpy(), memset(), and memmove().
1265
      memcpy(table->key_info[0].rec_per_key,
1266
	     misam_info.rec_per_key,
1 by brian
clean slate
1267
	     sizeof(table->key_info[0].rec_per_key)*share->key_parts);
1268
    if (share->tmp_table == NO_TMP_TABLE)
1269
      pthread_mutex_unlock(&share->mutex);
1270
1271
   /*
1272
     Set data_file_name and index_file_name to point at the symlink value
1273
     if table is symlinked (Ie;  Real name is not same as generated name)
1274
   */
1275
    data_file_name= index_file_name= 0;
1276
    fn_format(name_buff, file->filename, "", MI_NAME_DEXT,
1277
              MY_APPEND_EXT | MY_UNPACK_FILENAME);
1278
    if (strcmp(name_buff, misam_info.data_file_name))
1279
      data_file_name=misam_info.data_file_name;
1280
    fn_format(name_buff, file->filename, "", MI_NAME_IEXT,
1281
              MY_APPEND_EXT | MY_UNPACK_FILENAME);
1282
    if (strcmp(name_buff, misam_info.index_file_name))
1283
      index_file_name=misam_info.index_file_name;
1284
  }
1285
  if (flag & HA_STATUS_ERRKEY)
1286
  {
1287
    errkey  = misam_info.errkey;
1288
    my_store_ptr(dup_ref, ref_length, misam_info.dupp_key_pos);
1289
  }
1290
  if (flag & HA_STATUS_TIME)
1291
    stats.update_time = misam_info.update_time;
1292
  if (flag & HA_STATUS_AUTO)
1293
    stats.auto_increment_value= misam_info.auto_increment;
1294
1295
  return 0;
1296
}
1297
1298
1299
int ha_myisam::extra(enum ha_extra_function operation)
1300
{
1301
  return mi_extra(file, operation, 0);
1302
}
1303
1304
int ha_myisam::reset(void)
1305
{
1306
  return mi_reset(file);
1307
}
1308
1309
/* To be used with WRITE_CACHE and EXTRA_CACHE */
1310
61 by Brian Aker
Conversion of handler type.
1311
int ha_myisam::extra_opt(enum ha_extra_function operation, uint32_t cache_size)
1 by brian
clean slate
1312
{
1313
  return mi_extra(file, operation, (void*) &cache_size);
1314
}
1315
1316
int ha_myisam::delete_all_rows()
1317
{
1318
  return mi_delete_all_rows(file);
1319
}
1320
1183.1.29 by Brian Aker
Clean up interface so that Truncate sets the propper engine when
1321
int MyisamEngine::doDropTable(Session&, const string table_path)
1 by brian
clean slate
1322
{
1208 by Brian Aker
Merge Jay
1323
  ProtoCache::iterator iter;
1324
1325
  pthread_mutex_lock(&proto_cache_mutex);
1326
  iter= proto_cache.find(table_path.c_str());
1327
1328
  if (iter!= proto_cache.end())
1329
    proto_cache.erase(iter);
1330
1331
  pthread_mutex_unlock(&proto_cache_mutex);
1183.1.30 by Brian Aker
Added engine internal locks (instead of the session based....)
1332
1183.1.21 by Brian Aker
Fixed temp engines to no longer write out DFE. I have two designs right now
1333
  return mi_delete_table(table_path.c_str());
1 by brian
clean slate
1334
}
1335
1336
520.1.22 by Brian Aker
Second pass of thd cleanup
1337
int ha_myisam::external_lock(Session *session, int lock_type)
1 by brian
clean slate
1338
{
916.1.35 by Padraig O'Sullivan
Removing the last of LIST from the MyISAM storage engine.
1339
  file->in_use= session;
1 by brian
clean slate
1340
  return mi_lock_database(file, !table->s->tmp_table ?
1341
			  lock_type : ((lock_type == F_UNLCK) ?
1342
				       F_UNLCK : F_EXTRA_LCK));
1343
}
1344
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
1345
THR_LOCK_DATA **ha_myisam::store_lock(Session *,
1 by brian
clean slate
1346
				      THR_LOCK_DATA **to,
1347
				      enum thr_lock_type lock_type)
1348
{
1349
  if (lock_type != TL_IGNORE && file->lock.type == TL_UNLOCK)
1350
    file->lock.type=lock_type;
1351
  *to++= &file->lock;
1046.1.7 by Brian Aker
Style cleanup.
1352
1 by brian
clean slate
1353
  return to;
1354
}
1355
1183.1.6 by Brian Aker
Simplify createTable()
1356
int MyisamEngine::doCreateTable(Session *, const char *table_name,
1183.1.18 by Brian Aker
Fixed references to doCreateTable()
1357
                                Table& table_arg,
1183.1.20 by Brian Aker
Quick pass through ha_create in doTableCreate() to turn it into a reference.
1358
                                HA_CREATE_INFO& ha_create_info,
1183.1.18 by Brian Aker
Fixed references to doCreateTable()
1359
                                drizzled::message::Table& create_proto)
1 by brian
clean slate
1360
{
1361
  int error;
779.3.10 by Monty Taylor
Turned on -Wshadow.
1362
  uint32_t create_flags= 0, create_records;
1 by brian
clean slate
1363
  char buff[FN_REFLEN];
1364
  MI_KEYDEF *keydef;
1365
  MI_COLUMNDEF *recinfo;
1366
  MI_CREATE_INFO create_info;
1183.1.18 by Brian Aker
Fixed references to doCreateTable()
1367
  TableShare *share= table_arg.s;
482 by Brian Aker
Remove uint.
1368
  uint32_t options= share->db_options_in_use;
1183.1.18 by Brian Aker
Fixed references to doCreateTable()
1369
  if ((error= table2myisam(&table_arg, &keydef, &recinfo, &create_records)))
971.6.11 by Eric Day
Removed purecov messages.
1370
    return(error);
212.6.12 by Mats Kindahl
Removing redundant use of casts in MyISAM storage for memcmp(), memcpy(), memset(), and memmove().
1371
  memset(&create_info, 0, sizeof(create_info));
1183.1.18 by Brian Aker
Fixed references to doCreateTable()
1372
  create_info.max_rows= create_proto.options().max_rows();
1373
  create_info.reloc_rows= create_proto.options().min_rows();
1 by brian
clean slate
1374
  create_info.with_auto_increment= share->next_number_key_offset == 0;
1183.1.20 by Brian Aker
Quick pass through ha_create in doTableCreate() to turn it into a reference.
1375
  create_info.auto_increment= (ha_create_info.auto_increment_value ?
1376
                               ha_create_info.auto_increment_value -1 :
1 by brian
clean slate
1377
                               (uint64_t) 0);
1183.1.18 by Brian Aker
Fixed references to doCreateTable()
1378
  create_info.data_file_length= (create_proto.options().max_rows() *
1379
                                 create_proto.options().avg_row_length());
1121.1.2 by Brian Aker
Fix storing data/index path
1380
  create_info.data_file_name= NULL;
1381
  create_info.index_file_name=  NULL;
1 by brian
clean slate
1382
  create_info.language= share->table_charset->number;
1383
1183.1.20 by Brian Aker
Quick pass through ha_create in doTableCreate() to turn it into a reference.
1384
  if (ha_create_info.options & HA_LEX_CREATE_TMP_TABLE)
1 by brian
clean slate
1385
    create_flags|= HA_CREATE_TMP_TABLE;
1183.1.20 by Brian Aker
Quick pass through ha_create in doTableCreate() to turn it into a reference.
1386
  if (ha_create_info.options & HA_CREATE_KEEP_FILES)
1 by brian
clean slate
1387
    create_flags|= HA_CREATE_KEEP_FILES;
1388
  if (options & HA_OPTION_PACK_RECORD)
1389
    create_flags|= HA_PACK_RECORD;
1390
1391
  /* TODO: Check that the following fn_format is really needed */
1039.3.3 by Stewart Smith
Move handler::create to StorageEngine::create_table
1392
  error= mi_create(fn_format(buff, table_name, "", "",
1 by brian
clean slate
1393
                             MY_UNPACK_FILENAME|MY_APPEND_EXT),
1394
                   share->keys, keydef,
779.3.10 by Monty Taylor
Turned on -Wshadow.
1395
                   create_records, recinfo,
1 by brian
clean slate
1396
                   0, (MI_UNIQUEDEF*) 0,
1397
                   &create_info, create_flags);
481 by Brian Aker
Remove all of uchar.
1398
  free((unsigned char*) recinfo);
1183.1.21 by Brian Aker
Fixed temp engines to no longer write out DFE. I have two designs right now
1399
1208 by Brian Aker
Merge Jay
1400
  pthread_mutex_lock(&proto_cache_mutex);
1401
  proto_cache.insert(make_pair(table_name, create_proto));
1402
  pthread_mutex_unlock(&proto_cache_mutex);
1183.1.21 by Brian Aker
Fixed temp engines to no longer write out DFE. I have two designs right now
1403
1404
  return error;
1 by brian
clean slate
1405
}
1406
1407
1183.1.7 by Brian Aker
Fix name conventions for rename table
1408
int MyisamEngine::doRenameTable(Session*,
1409
                                const char *from, const char *to)
1 by brian
clean slate
1410
{
1411
  return mi_rename(from,to);
1412
}
1413
1414
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
1415
void ha_myisam::get_auto_increment(uint64_t ,
1416
                                   uint64_t ,
1417
                                   uint64_t ,
1 by brian
clean slate
1418
                                   uint64_t *first_value,
1419
                                   uint64_t *nb_reserved_values)
1420
{
1421
  uint64_t nr;
1422
  int error;
481 by Brian Aker
Remove all of uchar.
1423
  unsigned char key[MI_MAX_KEY_LENGTH];
1 by brian
clean slate
1424
1425
  if (!table->s->next_number_key_offset)
1426
  {						// Autoincrement at key-start
1427
    ha_myisam::info(HA_STATUS_AUTO);
1428
    *first_value= stats.auto_increment_value;
1429
    /* MyISAM has only table-level lock, so reserves to +inf */
163 by Brian Aker
Merge Monty's code.
1430
    *nb_reserved_values= UINT64_MAX;
1 by brian
clean slate
1431
    return;
1432
  }
1433
1434
  /* it's safe to call the following if bulk_insert isn't on */
1435
  mi_flush_bulk_insert(file, table->s->next_number_index);
1436
1437
  (void) extra(HA_EXTRA_KEYREAD);
1438
  key_copy(key, table->record[0],
1439
           table->key_info + table->s->next_number_index,
1440
           table->s->next_number_key_offset);
1441
  error= mi_rkey(file, table->record[1], (int) table->s->next_number_index,
1442
                 key, make_prev_keypart_map(table->s->next_number_keypart),
1443
                 HA_READ_PREFIX_LAST);
1444
  if (error)
1445
    nr= 1;
1446
  else
1447
  {
1448
    /* Get data from record[1] */
1449
    nr= ((uint64_t) table->next_number_field->
1450
         val_int_offset(table->s->rec_buff_length)+1);
1451
  }
1452
  extra(HA_EXTRA_NO_KEYREAD);
1453
  *first_value= nr;
1454
  /*
1455
    MySQL needs to call us for next row: assume we are inserting ("a",null)
1456
    here, we return 3, and next this statement will want to insert ("b",null):
1457
    there is no reason why ("b",3+1) would be the good row to insert: maybe it
1458
    already exists, maybe 3+1 is too large...
1459
  */
1460
  *nb_reserved_values= 1;
1461
}
1462
1463
1464
/*
1465
  Find out how many rows there is in the given range
1466
1467
  SYNOPSIS
1468
    records_in_range()
1469
    inx			Index to use
1470
    min_key		Start of range.  Null pointer if from first key
1471
    max_key		End of range. Null pointer if to last key
1472
1473
  NOTES
1474
    min_key.flag can have one of the following values:
1475
      HA_READ_KEY_EXACT		Include the key in the range
1476
      HA_READ_AFTER_KEY		Don't include key in range
1477
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1478
    max_key.flag can have one of the following values:
1 by brian
clean slate
1479
      HA_READ_BEFORE_KEY	Don't include key in range
1480
      HA_READ_AFTER_KEY		Include all 'end_key' values in the range
1481
1482
  RETURN
1483
   HA_POS_ERROR		Something is wrong with the index tree.
1484
   0			There is no matching keys in the given range
1485
   number > 0		There is approximately 'number' matching rows in
1486
			the range.
1487
*/
1488
482 by Brian Aker
Remove uint.
1489
ha_rows ha_myisam::records_in_range(uint32_t inx, key_range *min_key,
1 by brian
clean slate
1490
                                    key_range *max_key)
1491
{
1492
  return (ha_rows) mi_records_in_range(file, (int) inx, min_key, max_key);
1493
}
1494
1495
482 by Brian Aker
Remove uint.
1496
uint32_t ha_myisam::checksum() const
1 by brian
clean slate
1497
{
1498
  return (uint)file->state->checksum;
1499
}
1500
971.1.51 by Monty Taylor
New-style plugin registration now works.
1501
static MyisamEngine *engine= NULL;
1502
1110.1.5 by Monty Taylor
Renamed PluginRegistry to plugin::Registry.
1503
static int myisam_init(drizzled::plugin::Registry &registry)
971.1.51 by Monty Taylor
New-style plugin registration now works.
1504
{
1106.4.2 by Brian Aker
Remove multi key cache
1505
  int error;
971.1.51 by Monty Taylor
New-style plugin registration now works.
1506
  engine= new MyisamEngine(engine_name);
1130.1.8 by Monty Taylor
Added polymorphic add/remove methods around slot add/remove methods.
1507
  registry.add(engine);
971.1.51 by Monty Taylor
New-style plugin registration now works.
1508
1509
  pthread_mutex_init(&THR_LOCK_myisam,MY_MUTEX_INIT_FAST);
1510
1106.4.2 by Brian Aker
Remove multi key cache
1511
  /* call ha_init_key_cache() on all key caches to init them */
1512
  error= init_key_cache(dflt_key_cache,
1513
                        (uint32_t) dflt_key_cache->param_block_size,
1514
                        (uint32_t) dflt_key_cache->param_buff_size,
1515
                        dflt_key_cache->param_division_limit, 
1516
                        dflt_key_cache->param_age_threshold);
1517
1518
  if (error == 0)
1519
    exit(1); /* Memory Allocation Failure */
1520
971.1.51 by Monty Taylor
New-style plugin registration now works.
1521
  return 0;
1522
}
1523
1110.1.5 by Monty Taylor
Renamed PluginRegistry to plugin::Registry.
1524
static int myisam_deinit(drizzled::plugin::Registry &registry)
971.1.51 by Monty Taylor
New-style plugin registration now works.
1525
{
1130.1.8 by Monty Taylor
Added polymorphic add/remove methods around slot add/remove methods.
1526
  registry.remove(engine);
960.2.33 by Monty Taylor
Converted MyISAM.
1527
  delete engine;
1528
598 by Brian Aker
Removed mutex which are myisam from global to just engine.
1529
  pthread_mutex_destroy(&THR_LOCK_myisam);
1106.4.2 by Brian Aker
Remove multi key cache
1530
  end_key_cache(dflt_key_cache, 1);		// Can never fail
598 by Brian Aker
Removed mutex which are myisam from global to just engine.
1531
224.2.3 by Brian Aker
Fix for memory leak in shutdown/restart of an engine (not fixed in 5.1)
1532
  return mi_panic(HA_PANIC_CLOSE);
1 by brian
clean slate
1533
}
1534
754 by Brian Aker
Make block_size for myisam fit into plugin.
1535
static DRIZZLE_SYSVAR_UINT(block_size, block_size,
1536
                           PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
1537
                           N_("Block size to be used for MyISAM index pages."),
1538
                           NULL, NULL, MI_KEY_BLOCK_LENGTH, MI_MIN_KEY_BLOCK_LENGTH, 
1539
                           MI_MAX_KEY_BLOCK_LENGTH, 0);
1540
753 by Brian Aker
Converted myisam_repair_threads to being in module for MyISAM
1541
static DRIZZLE_SYSVAR_UINT(repair_threads, repair_threads,
754 by Brian Aker
Make block_size for myisam fit into plugin.
1542
                           PLUGIN_VAR_RQCMDARG,
1543
                           N_("Number of threads to use when repairing MyISAM tables. The value of "
1544
                              "1 disables parallel repair."),
1545
                           NULL, NULL, 1, 1, UINT32_MAX, 0);
753 by Brian Aker
Converted myisam_repair_threads to being in module for MyISAM
1546
788 by Brian Aker
Move MyISAM bits to myisam plugin
1547
static DRIZZLE_SYSVAR_ULONGLONG(max_sort_file_size, max_sort_file_size,
789 by Brian Aker
MyISAM fix.
1548
                                PLUGIN_VAR_RQCMDARG,
1549
                                N_("Don't use the fast sort index method to created index if the temporary file would get bigger than this."),
1550
                                NULL, NULL, INT32_MAX, 0, UINT64_MAX, 0);
1551
1552
static DRIZZLE_SYSVAR_ULONGLONG(sort_buffer_size, sort_buffer_size,
1553
                                PLUGIN_VAR_RQCMDARG,
1554
                                N_("The buffer that is allocated when sorting the index when doing a REPAIR or when creating indexes with CREATE INDEX or ALTER TABLE."),
779.3.20 by Monty Taylor
Fixed Solaris warnings for MyISAM.
1555
                                NULL, NULL, 8192*1024, 1024, SIZE_MAX, 0);
788 by Brian Aker
Move MyISAM bits to myisam plugin
1556
790 by Brian Aker
More myisam plugin conversion.
1557
extern uint32_t data_pointer_size;
1558
static DRIZZLE_SYSVAR_UINT(data_pointer_size, data_pointer_size,
1559
                           PLUGIN_VAR_RQCMDARG,
1560
                           N_("Default pointer size to be used for MyISAM tables."),
1561
                           NULL, NULL, 6, 2, 7, 0);
1562
753 by Brian Aker
Converted myisam_repair_threads to being in module for MyISAM
1563
static struct st_mysql_sys_var* system_variables[]= {
754 by Brian Aker
Make block_size for myisam fit into plugin.
1564
  DRIZZLE_SYSVAR(block_size),
753 by Brian Aker
Converted myisam_repair_threads to being in module for MyISAM
1565
  DRIZZLE_SYSVAR(repair_threads),
788 by Brian Aker
Move MyISAM bits to myisam plugin
1566
  DRIZZLE_SYSVAR(max_sort_file_size),
789 by Brian Aker
MyISAM fix.
1567
  DRIZZLE_SYSVAR(sort_buffer_size),
790 by Brian Aker
More myisam plugin conversion.
1568
  DRIZZLE_SYSVAR(data_pointer_size),
753 by Brian Aker
Converted myisam_repair_threads to being in module for MyISAM
1569
  NULL
1570
};
1571
1 by brian
clean slate
1572
1192.3.7 by Monty Taylor
Added code necessary for building plugins dynamically.
1573
drizzle_declare_plugin
1 by brian
clean slate
1574
{
1575
  "MyISAM",
177.4.3 by mark
ripped out more plugin ABI and API version checking, and plugin versions are now strings
1576
  "1.0",
1 by brian
clean slate
1577
  "MySQL AB",
1578
  "Default engine as of MySQL 3.23 with great performance",
1579
  PLUGIN_LICENSE_GPL,
1580
  myisam_init, /* Plugin Init */
224.2.3 by Brian Aker
Fix for memory leak in shutdown/restart of an engine (not fixed in 5.1)
1581
  myisam_deinit, /* Plugin Deinit */
1 by brian
clean slate
1582
  NULL,                       /* status variables                */
753 by Brian Aker
Converted myisam_repair_threads to being in module for MyISAM
1583
  system_variables,           /* system variables */
1 by brian
clean slate
1584
  NULL                        /* config options                  */
1585
}
813.2.2 by Toru Maesaka
Renamed mysql_declare_plugin_end to drizzle_declare_plugin_end
1586
drizzle_declare_plugin_end;