19#include <drizzled/internal/my_bit.h>
20#include "myisampack.h"
22#include "myisam_priv.h"
23#include <drizzled/option.h>
24#include <drizzled/internal/my_bit.h>
25#include <drizzled/internal/m_string.h>
26#include <drizzled/util/test.h>
27#include <drizzled/error.h>
28#include <drizzled/errmsg_print.h>
29#include <drizzled/gettext.h>
30#include <drizzled/session.h>
31#include <drizzled/plugin.h>
32#include <drizzled/plugin/client.h>
33#include <drizzled/table.h>
34#include <drizzled/memory/multi_malloc.h>
35#include <drizzled/plugin/daemon.h>
36#include <drizzled/session/table_messages.h>
37#include <drizzled/plugin/storage_engine.h>
38#include <drizzled/key.h>
39#include <drizzled/statistics_variables.h>
40#include <drizzled/system_variables.h>
41#include <drizzled/key_part_info.h>
43#include <boost/algorithm/string.hpp>
44#include <boost/scoped_ptr.hpp>
51#include <boost/program_options.hpp>
54namespace po= boost::program_options;
59static const string engine_name(
"MyISAM");
61boost::mutex THR_LOCK_myisam;
63static uint32_t myisam_key_cache_block_size= KEY_CACHE_BLOCK_SIZE;
64static uint64_t max_sort_file_size;
66static sort_buffer_constraint sort_buffer_size;
72static const char *ha_myisam_exts[] = {
78class MyisamEngine :
public plugin::StorageEngine
81 explicit MyisamEngine(
string name_arg) :
82 plugin::StorageEngine(name_arg,
83 HTON_CAN_INDEX_BLOBS |
84 HTON_STATS_RECORDS_IS_EXACT |
94 virtual ~MyisamEngine()
96 mi_panic(HA_PANIC_CLOSE);
104 const char **bas_ext()
const {
105 return ha_myisam_exts;
111 const message::Table&);
117 int doGetTableDefinition(
Session& session,
119 message::Table &table_message);
121 uint32_t max_supported_keys()
const {
return MI_MAX_KEY; }
122 uint32_t max_supported_key_length()
const {
return MI_MAX_KEY_LENGTH; }
123 uint32_t max_supported_key_part_length()
const {
return MI_MAX_KEY_LENGTH; }
125 uint32_t index_flags(drizzled::message::Table::Index::IndexType)
const
127 return (HA_READ_NEXT |
137 drizzled::identifier::table::vector &set_of_identifiers);
138 bool validateCreateTableOption(
const std::string &key,
const std::string &state)
141 if (boost::iequals(key,
"ROW_FORMAT"))
152 drizzled::identifier::table::vector&)
158 return session.getMessageCache().doesTableMessageExist(identifier);
161int MyisamEngine::doGetTableDefinition(
Session &session,
163 message::Table &table_message)
165 if (session.getMessageCache().getTableMessage(identifier, table_message))
174static void mi_check_print_msg(MI_CHECK *,
const char* ,
175 const char *, va_list )
204static int table2myisam(
Table *table_arg, MI_KEYDEF **keydef_out,
205 MI_COLUMNDEF **recinfo_out, uint32_t *records_out)
207 uint32_t i, j, recpos, minpos, fieldpos, temp_length, length;
208 enum ha_base_keytype
type= HA_KEYTYPE_BINARY;
209 unsigned char *record;
211 MI_COLUMNDEF *recinfo, *recinfo_pos;
213 TableShare *share= table_arg->getMutableShare();
214 uint32_t options= share->db_options_in_use;
215 if (!(memory::multi_malloc(
false,
216 recinfo_out, (share->sizeFields() * 2 + 2) *
sizeof(MI_COLUMNDEF),
217 keydef_out, share->sizeKeys() *
sizeof(MI_KEYDEF),
218 &keyseg, (share->key_parts + share->sizeKeys()) *
sizeof(HA_KEYSEG),
220 return(HA_ERR_OUT_OF_MEM);
222 recinfo= *recinfo_out;
223 for (i= 0; i < share->sizeKeys(); i++)
226 keydef[i].flag= ((uint16_t) pos->flags & (HA_NOSAME));
227 keydef[i].key_alg= message::Table::Index::BTREE;
228 keydef[i].block_length= pos->block_size;
229 keydef[i].seg= keyseg;
230 keydef[i].keysegs= pos->key_parts;
231 for (j= 0; j < pos->key_parts; j++)
233 Field *field= pos->key_part[j].field;
234 type= field->key_type();
235 keydef[i].seg[j].flag= pos->key_part[j].key_part_flag;
237 if (options & HA_OPTION_PACK_KEYS ||
238 (pos->flags & (HA_PACK_KEY | HA_BINARY_PACK_KEY |
239 HA_SPACE_PACK_USED)))
241 if (pos->key_part[j].length > 8 &&
242 (
type == HA_KEYTYPE_TEXT ||
243 (
type == HA_KEYTYPE_BINARY && !field->zero_pack())))
247 keydef[i].flag|= HA_PACK_KEY;
248 if ((((
int) (pos->key_part[j].length - field->decimals())) >= 4))
249 keydef[i].seg[j].flag|= HA_SPACE_PACK;
251 else if (j == 0 && (!(pos->flags & HA_NOSAME) || pos->key_length > 16))
252 keydef[i].flag|= HA_BINARY_PACK_KEY;
254 keydef[i].seg[j].type= (int)
type;
255 keydef[i].seg[j].start= pos->key_part[j].offset;
256 keydef[i].seg[j].length= pos->key_part[j].length;
257 keydef[i].seg[j].bit_start= keydef[i].seg[j].bit_end=
258 keydef[i].seg[j].bit_length= 0;
259 keydef[i].seg[j].bit_pos= 0;
260 keydef[i].seg[j].language= field->charset()->number;
264 keydef[i].seg[j].null_bit= field->null_bit;
265 keydef[i].seg[j].null_pos= (uint) (field->null_ptr-
266 (
unsigned char*) table_arg->getInsertRecord());
270 keydef[i].seg[j].null_bit= 0;
271 keydef[i].seg[j].null_pos= 0;
273 if (field->type() == DRIZZLE_TYPE_BLOB)
275 keydef[i].seg[j].flag|= HA_BLOB_PART;
277 keydef[i].seg[j].bit_start= (uint) (field->pack_length() -
278 share->sizeBlobPtr());
281 keyseg+= pos->key_parts;
284 keydef[share->next_number_index].flag|= HA_AUTO_KEY;
285 record= table_arg->getInsertRecord();
287 recinfo_pos= recinfo;
289 while (recpos < (uint) share->sizeStoredRecord())
291 Field **field, *found= 0;
292 minpos= share->getRecordLength();
295 for (field= table_arg->getFields(); *field; field++)
297 if ((fieldpos= (*field)->offset(record)) >= recpos &&
301 if (!(temp_length= (*field)->pack_length_in_rec()))
304 if (! found || fieldpos < minpos ||
305 (fieldpos == minpos && temp_length < length))
313 if (recpos != minpos)
315 memset(recinfo_pos, 0,
sizeof(*recinfo_pos));
316 recinfo_pos->type= (int) FIELD_NORMAL;
317 recinfo_pos++->length= (uint16_t) (minpos - recpos);
322 if (found->flags & BLOB_FLAG)
323 recinfo_pos->type= (int) FIELD_BLOB;
324 else if (found->type() == DRIZZLE_TYPE_VARCHAR)
325 recinfo_pos->type= FIELD_VARCHAR;
326 else if (!(options & HA_OPTION_PACK_RECORD))
327 recinfo_pos->type= (int) FIELD_NORMAL;
328 else if (found->zero_pack())
329 recinfo_pos->type= (int) FIELD_SKIP_ZERO;
331 recinfo_pos->type= (int) ((length <= 3) ? FIELD_NORMAL : FIELD_SKIP_PRESPACE);
334 recinfo_pos->null_bit= found->
null_bit;
335 recinfo_pos->null_pos= (uint) (found->
null_ptr -
336 (
unsigned char*) table_arg->getInsertRecord());
340 recinfo_pos->null_bit= 0;
341 recinfo_pos->null_pos= 0;
343 (recinfo_pos++)->length= (uint16_t) length;
344 recpos= minpos + length;
346 *records_out= (uint) (recinfo_pos - recinfo);
352 file->s->state.auto_increment= value;
396static int check_definition(MI_KEYDEF *t1_keyinfo, MI_COLUMNDEF *t1_recinfo,
397 uint32_t t1_keys, uint32_t t1_recs,
398 MI_KEYDEF *t2_keyinfo, MI_COLUMNDEF *t2_recinfo,
399 uint32_t t2_keys, uint32_t t2_recs,
bool strict)
402 if ((strict ? t1_keys != t2_keys : t1_keys > t2_keys))
406 if (t1_recs != t2_recs)
410 for (i= 0; i < t1_keys; i++)
412 HA_KEYSEG *t1_keysegs= t1_keyinfo[i].seg;
413 HA_KEYSEG *t2_keysegs= t2_keyinfo[i].seg;
414 if (t1_keyinfo[i].keysegs != t2_keyinfo[i].keysegs ||
415 t1_keyinfo[i].key_alg != t2_keyinfo[i].key_alg)
419 for (j= t1_keyinfo[i].keysegs; j--;)
421 uint8_t t1_keysegs_j__type= t1_keysegs[j].type;
429 if ((t1_keysegs[j].flag & HA_BLOB_PART) &&
430 (t2_keysegs[j].flag & HA_BLOB_PART))
432 if ((t1_keysegs_j__type == HA_KEYTYPE_VARTEXT2) &&
433 (t2_keysegs[j].
type == HA_KEYTYPE_VARTEXT1))
434 t1_keysegs_j__type= HA_KEYTYPE_VARTEXT1;
435 else if ((t1_keysegs_j__type == HA_KEYTYPE_VARBINARY2) &&
436 (t2_keysegs[j].
type == HA_KEYTYPE_VARBINARY1))
437 t1_keysegs_j__type= HA_KEYTYPE_VARBINARY1;
440 if (t1_keysegs_j__type != t2_keysegs[j].
type ||
441 t1_keysegs[j].language != t2_keysegs[j].language ||
442 t1_keysegs[j].null_bit != t2_keysegs[j].null_bit ||
443 t1_keysegs[j].length != t2_keysegs[j].length)
449 for (i= 0; i < t1_recs; i++)
451 MI_COLUMNDEF *t1_rec= &t1_recinfo[i];
452 MI_COLUMNDEF *t2_rec= &t2_recinfo[i];
457 if ((t1_rec->type != t2_rec->type &&
458 !(t1_rec->type == (
int) FIELD_SKIP_ZERO &&
459 t1_rec->length == 1 &&
460 t2_rec->type == (
int) FIELD_NORMAL)) ||
461 t1_rec->length != t2_rec->length ||
462 t1_rec->null_bit != t2_rec->null_bit)
471volatile int *killed_ptr(MI_CHECK *param)
474 return (
int*) (((
Session *)(param->session))->getKilledPtr());
477void mi_check_print_error(MI_CHECK *param,
const char *fmt,...)
479 param->error_printed|=1;
480 param->out_flag|= O_DATA_LOST;
483 mi_check_print_msg(param,
"error", fmt, args);
487void mi_check_print_info(MI_CHECK *param,
const char *fmt,...)
491 mi_check_print_msg(param,
"info", fmt, args);
495void mi_check_print_warning(MI_CHECK *param,
const char *fmt,...)
497 param->warning_printed=1;
498 param->out_flag|= O_DATA_LOST;
501 mi_check_print_msg(param,
"warning", fmt, args);
520void _mi_report_crashed(MI_INFO *file,
const char *message,
521 const char *sfile, uint32_t sline)
524 if ((cur_session= file->in_use))
526 errmsg_printf(error::ERROR, _(
"Got an error from thread_id=%" PRIu64
", %s:%d"),
527 cur_session->thread_id,
532 errmsg_printf(error::ERROR, _(
"Got an error from unknown thread, %s:%d"), sfile, sline);
536 errmsg_printf(error::ERROR,
"%s", message);
538 list<Session *>::iterator it= file->s->in_use->begin();
539 while (it != file->s->in_use->end())
541 errmsg_printf(error::ERROR,
"%s", _(
"Unknown thread accessing table"));
546ha_myisam::ha_myisam(plugin::StorageEngine &engine_arg,
548 :
Cursor(engine_arg, table_arg),
550 can_enable_indexes(true),
556 ha_myisam *new_handler=
static_cast <ha_myisam *
>(Cursor::clone(mem_root));
558 new_handler->file->state= file->state;
562const char *ha_myisam::index_type(uint32_t )
571 MI_COLUMNDEF *recinfo= 0;
590 if (!(file= mi_open(identifier, mode, test_if_locked)))
591 return (errno ? errno : -1);
593 if (getTable()->getShare()->getType() == message::Table::STANDARD)
595 if ((errno= table2myisam(getTable(), &keyinfo, &recinfo, &recs)))
599 if (check_definition(keyinfo, recinfo, getTable()->getShare()->sizeKeys(), recs,
600 file->s->keyinfo, file->s->rec,
601 file->s->base.keys, file->s->base.fields,
true))
603 errno= HA_ERR_CRASHED;
608 assert(test_if_locked);
609 if (test_if_locked & (HA_OPEN_IGNORE_IF_LOCKED | HA_OPEN_TMP_TABLE))
610 mi_extra(file, HA_EXTRA_NO_WAIT_LOCK, 0);
612 info(HA_STATUS_NO_LOCK | HA_STATUS_VARIABLE | HA_STATUS_CONST);
613 if (!(test_if_locked & HA_OPEN_WAIT_IF_LOCKED))
614 mi_extra(file, HA_EXTRA_WAIT_LOCK, 0);
615 if (!getTable()->getShare()->db_record_offset)
619 keys_with_parts.reset();
620 for (i= 0; i < getTable()->getShare()->sizeKeys(); i++)
622 getTable()->key_info[i].block_size= file->s->keyinfo[i].block_length;
624 KeyPartInfo *kp= getTable()->key_info[i].key_part;
625 KeyPartInfo *kp_end= kp + getTable()->key_info[i].key_parts;
626 for (; kp != kp_end; kp++)
628 if (!kp->field->part_of_key.test(i))
630 keys_with_parts.set(i);
645 free((
unsigned char*) recinfo);
649int ha_myisam::close(
void)
653 return mi_close(tmp);
656int ha_myisam::doInsertRecord(
unsigned char *buf)
662 if (getTable()->next_number_field && buf == getTable()->getInsertRecord())
665 if ((error= update_auto_increment()))
668 return mi_write(file,buf);
672int ha_myisam::repair(
Session *session, MI_CHECK ¶m,
bool do_optimize)
675 uint32_t local_testflag= param.testflag;
676 bool optimize_done= !do_optimize, statistics_done=0;
677 const char *old_proc_info= session->get_proc_info();
678 char fixed_name[FN_REFLEN];
679 MYISAM_SHARE* share = file->s;
680 ha_rows rows= file->state->records;
690 if (file->dfile == -1)
692 errmsg_printf(error::INFO,
"Retrying repair of: '%s' failed. "
693 "Please try REPAIR EXTENDED or myisamchk",
694 getTable()->getShare()->getPath());
695 return(HA_ADMIN_FAILED);
698 param.db_name= getTable()->getShare()->getSchemaName();
699 param.table_name= getTable()->getAlias();
700 param.tmpfile_createflag = O_RDWR | O_TRUNC;
701 param.using_global_keycache = 1;
702 param.session= session;
704 param.sort_buffer_length=
static_cast<size_t>(sort_buffer_size);
705 strcpy(fixed_name,file->filename);
708 if (mi_lock_database(file, getTable()->getShare()->getType() ? F_EXTRA_LCK : F_WRLCK))
710 mi_check_print_error(¶m,ER(ER_CANT_LOCK),errno);
711 return(HA_ADMIN_FAILED);
715 ((file->state->del || share->state.split != file->state->records) &&
716 (!(param.testflag & T_QUICK) ||
717 !(share->state.changed & STATE_NOT_OPTIMIZED_KEYS))))
719 uint64_t key_map= ((local_testflag & T_CREATE_MISSING_KEYS) ?
720 mi_get_mask_all_keys_active(share->base.keys) :
721 share->state.key_map);
722 uint32_t testflag=param.testflag;
723 if (mi_test_if_sort_rep(file,file->state->records,key_map,0) &&
724 (local_testflag & T_REP_BY_SORT))
726 local_testflag|= T_STATISTICS;
727 param.testflag|= T_STATISTICS;
730 session->set_proc_info(
"Repair by sorting");
731 error = mi_repair_by_sort(¶m, file, fixed_name,
732 param.testflag & T_QUICK);
738 param.testflag &= ~T_REP_BY_SORT;
739 error= mi_repair(¶m, file, fixed_name,
740 param.testflag & T_QUICK);
742 param.testflag=testflag;
747 if ((local_testflag & T_SORT_INDEX) &&
748 (share->state.changed & STATE_NOT_SORTED_PAGES))
751 session->set_proc_info(
"Sorting index");
752 error=mi_sort_index(¶m,file,fixed_name);
754 if (!statistics_done && (local_testflag & T_STATISTICS))
756 if (share->state.changed & STATE_NOT_ANALYZED)
759 session->set_proc_info(
"Analyzing");
760 error = chk_key(¶m, file);
763 local_testflag&= ~T_STATISTICS;
766 session->set_proc_info(
"Saving state");
769 if ((share->state.changed & STATE_CHANGED) || mi_is_crashed(file))
771 share->state.changed&= ~(STATE_CHANGED | STATE_CRASHED |
772 STATE_CRASHED_ON_REPAIR);
773 file->update|=HA_STATE_CHANGED | HA_STATE_ROW_CHANGED;
779 if (file->state != &file->s->state.state)
780 file->s->state.state = *file->state;
781 if (file->s->base.auto_key)
782 update_auto_increment_key(¶m, file, 1);
784 error = update_state_info(¶m, file,
785 UPDATE_TIME | UPDATE_OPEN_COUNT |
787 T_STATISTICS ? UPDATE_STAT : 0));
788 info(HA_STATUS_NO_LOCK | HA_STATUS_TIME | HA_STATUS_VARIABLE |
790 if (rows != file->state->records && ! (param.testflag & T_VERY_SILENT))
792 char llbuff[22],llbuff2[22];
793 mi_check_print_warning(¶m,
"Number of rows changed from %s to %s",
794 internal::llstr(rows,llbuff),
795 internal::llstr(file->state->records,llbuff2));
800 mi_mark_crashed_on_repair(file);
801 file->update |= HA_STATE_CHANGED | HA_STATE_ROW_CHANGED;
802 update_state_info(¶m, file, 0);
804 session->set_proc_info(old_proc_info);
805 mi_lock_database(file,F_UNLCK);
807 return(error ? HA_ADMIN_FAILED :
808 !optimize_done ? HA_ADMIN_ALREADY_DONE : HA_ADMIN_OK);
832int ha_myisam::disable_indexes(uint32_t mode)
836 if (mode == HA_KEY_SWITCH_ALL)
839 error= mi_disable_indexes(file);
841 else if (mode == HA_KEY_SWITCH_NONUNIQ_SAVE)
843 mi_extra(file, HA_EXTRA_NO_KEYS, 0);
844 info(HA_STATUS_CONST);
850 error= HA_ERR_WRONG_COMMAND;
884int ha_myisam::enable_indexes(uint32_t mode)
888 if (mi_is_all_keys_active(file->s->state.key_map, file->s->base.keys))
894 if (mode == HA_KEY_SWITCH_ALL)
896 error= mi_enable_indexes(file);
903 else if (mode == HA_KEY_SWITCH_NONUNIQ_SAVE)
905 Session *session= getTable()->in_use;
906 boost::scoped_ptr<MI_CHECK> param_ap(
new MI_CHECK);
907 MI_CHECK ¶m= *param_ap.get();
908 const char *save_proc_info= session->get_proc_info();
910 myisamchk_init(¶m);
911 param.op_name=
"recreating_index";
912 param.testflag= (T_SILENT | T_REP_BY_SORT | T_QUICK |
913 T_CREATE_MISSING_KEYS);
914 param.myf_rw&= ~MY_WAIT_IF_FULL;
915 param.sort_buffer_length=
static_cast<size_t>(sort_buffer_size);
916 param.stats_method= MI_STATS_METHOD_NULLS_NOT_EQUAL;
917 if ((error= (repair(session,param,0) != HA_ADMIN_OK)) && param.retry_repair)
919 errmsg_printf(error::WARN,
"Warning: Enabling keys got errno %d on %s.%s, retrying",
920 errno, param.db_name, param.table_name);
922 param.testflag&= ~(T_REP_BY_SORT | T_QUICK);
923 error= (repair(session,param,0) != HA_ADMIN_OK);
930 session->clear_error();
932 info(HA_STATUS_CONST);
938 error= HA_ERR_WRONG_COMMAND;
959int ha_myisam::indexes_are_disabled(
void)
962 return mi_indexes_are_disabled(file);
980void ha_myisam::start_bulk_insert(ha_rows rows)
982 Session *session= getTable()->in_use;
983 ulong size= session->
variables.read_buff_size;
986 if (! rows || (rows > MI_MIN_ROWS_TO_USE_WRITE_CACHE))
987 mi_extra(file, HA_EXTRA_WRITE_CACHE, (
void*) &size);
989 can_enable_indexes= mi_is_all_keys_active(file->s->state.key_map,
998 if (file->state->records == 0 && can_enable_indexes &&
999 (!rows || rows >= MI_MIN_ROWS_TO_DISABLE_INDEXES))
1000 mi_disable_non_unique_index(file,rows);
1002 if (!file->bulk_insert &&
1003 (!rows || rows >= MI_MIN_ROWS_TO_USE_BULK_INSERT))
1005 mi_init_bulk_insert(file,
1006 (
size_t)session->
variables.bulk_insert_buff_size,
1024int ha_myisam::end_bulk_insert()
1026 mi_end_bulk_insert(file);
1027 int err=mi_extra(file, HA_EXTRA_NO_CACHE, 0);
1028 return err ? err : can_enable_indexes ?
1029 enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE) : 0;
1034int ha_myisam::doUpdateRecord(
const unsigned char *old_data,
unsigned char *new_data)
1036 return mi_update(file,old_data,new_data);
1039int ha_myisam::doDeleteRecord(
const unsigned char *buf)
1041 return mi_delete(file,buf);
1045int ha_myisam::doStartIndexScan(uint32_t idx,
bool )
1053int ha_myisam::doEndIndexScan()
1055 active_index=MAX_KEY;
1061 key_part_map keypart_map,
1062 enum ha_rkey_function find_flag)
1064 assert(inited==INDEX);
1065 ha_statistic_increment(&system_status_var::ha_read_key_count);
1066 int error=mi_rkey(file, buf, active_index, key, keypart_map, find_flag);
1067 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1072 key_part_map keypart_map,
1073 enum ha_rkey_function find_flag)
1075 ha_statistic_increment(&system_status_var::ha_read_key_count);
1076 int error=mi_rkey(file, buf, index, key, keypart_map, find_flag);
1077 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1082 key_part_map keypart_map)
1084 assert(inited==INDEX);
1085 ha_statistic_increment(&system_status_var::ha_read_key_count);
1086 int error=mi_rkey(file, buf, active_index, key, keypart_map,
1087 HA_READ_PREFIX_LAST);
1088 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1092int ha_myisam::index_next(
unsigned char *buf)
1094 assert(inited==INDEX);
1095 ha_statistic_increment(&system_status_var::ha_read_next_count);
1096 int error=mi_rnext(file,buf,active_index);
1097 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1101int ha_myisam::index_prev(
unsigned char *buf)
1103 assert(inited==INDEX);
1104 ha_statistic_increment(&system_status_var::ha_read_prev_count);
1105 int error=mi_rprev(file,buf, active_index);
1106 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1110int ha_myisam::index_first(
unsigned char *buf)
1112 assert(inited==INDEX);
1113 ha_statistic_increment(&system_status_var::ha_read_first_count);
1114 int error=mi_rfirst(file, buf, active_index);
1115 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1119int ha_myisam::index_last(
unsigned char *buf)
1121 assert(inited==INDEX);
1122 ha_statistic_increment(&system_status_var::ha_read_last_count);
1123 int error=mi_rlast(file, buf, active_index);
1124 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1128int ha_myisam::index_next_same(
unsigned char *buf,
1129 const unsigned char *,
1133 assert(inited==INDEX);
1134 ha_statistic_increment(&system_status_var::ha_read_next_count);
1137 error= mi_rnext_same(file,buf);
1138 }
while (error == HA_ERR_RECORD_DELETED);
1139 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1172 return mi_scan_init(file);
1173 return mi_reset(file);
1176int ha_myisam::rnd_next(
unsigned char *buf)
1178 ha_statistic_increment(&system_status_var::ha_read_rnd_next_count);
1179 int error=mi_scan(file, buf);
1180 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1184int ha_myisam::rnd_pos(
unsigned char *buf,
unsigned char *pos)
1186 ha_statistic_increment(&system_status_var::ha_read_rnd_count);
1187 int error=mi_rrnd(file, buf, internal::my_get_ptr(pos,
ref_length));
1188 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1193void ha_myisam::position(
const unsigned char *)
1195 internal::my_off_t row_position= mi_position(file);
1196 internal::my_store_ptr(ref,
ref_length, row_position);
1199int ha_myisam::info(uint32_t flag)
1201 MI_ISAMINFO misam_info;
1203 (void) mi_status(file,&misam_info,flag);
1204 if (flag & HA_STATUS_VARIABLE)
1206 stats.records= misam_info.records;
1207 stats.deleted= misam_info.deleted;
1208 stats.data_file_length= misam_info.data_file_length;
1209 stats.index_file_length= misam_info.index_file_length;
1210 stats.delete_length= misam_info.delete_length;
1211 stats.check_time= misam_info.check_time;
1212 stats.mean_rec_length= misam_info.mean_reclength;
1214 if (flag & HA_STATUS_CONST)
1216 TableShare *share= getTable()->getMutableShare();
1217 stats.max_data_file_length= misam_info.max_data_file_length;
1218 stats.max_index_file_length= misam_info.max_index_file_length;
1219 stats.create_time= misam_info.create_time;
1221 share->db_options_in_use= misam_info.options;
1222 stats.block_size= myisam_key_cache_block_size;
1224 set_prefix(share->keys_in_use, share->sizeKeys());
1232 string binary_key_map;
1233 uint64_t num= misam_info.key_map;
1240 uint64_t bin_digit= num % 2;
1244 binary_key_map.append(ostr.str());
1252 if (MAX_INDEXES <= 64)
1254 size_t len= 72 - binary_key_map.length();
1255 string all_zeros(len,
'0');
1256 binary_key_map.insert(binary_key_map.begin(),
1262 size_t len= (MAX_INDEXES + 7) / 8 * 8;
1263 string all_zeros(len,
'0');
1264 binary_key_map.insert(binary_key_map.begin(),
1268 key_map tmp_map(binary_key_map);
1269 share->keys_in_use&= tmp_map;
1270 share->keys_for_keyread&= share->keys_in_use;
1271 share->db_record_offset= misam_info.record_offset;
1272 if (share->key_parts)
1273 memcpy(getTable()->key_info[0].rec_per_key,
1274 misam_info.rec_per_key,
1275 sizeof(getTable()->key_info[0].rec_per_key)*share->key_parts);
1276 assert(share->getType() != message::Table::STANDARD);
1279 if (flag & HA_STATUS_ERRKEY)
1281 errkey = misam_info.errkey;
1282 internal::my_store_ptr(dup_ref,
ref_length, misam_info.dupp_key_pos);
1284 if (flag & HA_STATUS_TIME)
1285 stats.update_time = misam_info.update_time;
1286 if (flag & HA_STATUS_AUTO)
1287 stats.auto_increment_value= misam_info.auto_increment;
1293int ha_myisam::extra(
enum ha_extra_function operation)
1295 return mi_extra(file, operation, 0);
1300 return mi_reset(file);
1305int ha_myisam::extra_opt(
enum ha_extra_function operation, uint32_t cache_size)
1307 return mi_extra(file, operation, (
void*) &cache_size);
1312 return mi_delete_all_rows(file);
1315int MyisamEngine::doDropTable(
Session &session,
1318 session.getMessageCache().removeTableMessage(identifier);
1320 return mi_delete_table(identifier.getPath().c_str());
1326 file->in_use= session;
1327 return mi_lock_database(file, !getTable()->getShare()->getType() ?
1328 lock_type : ((lock_type == F_UNLCK) ?
1329 F_UNLCK : F_EXTRA_LCK));
1332int MyisamEngine::doCreateTable(
Session &session,
1335 const message::Table& create_proto)
1338 uint32_t create_flags= 0, create_records;
1339 char buff[FN_REFLEN];
1341 MI_COLUMNDEF *recinfo;
1342 MI_CREATE_INFO create_info;
1343 TableShare *share= table_arg.getMutableShare();
1344 uint32_t options= share->db_options_in_use;
1345 if ((error= table2myisam(&table_arg, &keydef, &recinfo, &create_records)))
1347 create_info.max_rows= create_proto.options().max_rows();
1348 create_info.reloc_rows= create_proto.options().min_rows();
1349 create_info.with_auto_increment= share->next_number_key_offset == 0;
1350 create_info.auto_increment= (create_proto.options().has_auto_increment_value() ?
1351 create_proto.options().auto_increment_value() -1 :
1353 create_info.data_file_length= (create_proto.options().max_rows() *
1354 create_proto.options().avg_row_length());
1355 create_info.data_file_name= NULL;
1356 create_info.index_file_name= NULL;
1357 create_info.language= share->table_charset->number;
1359 if (create_proto.type() == message::Table::TEMPORARY)
1360 create_flags|= HA_CREATE_TMP_TABLE;
1361 if (options & HA_OPTION_PACK_RECORD)
1362 create_flags|= HA_PACK_RECORD;
1365 error= mi_create(internal::fn_format(buff, identifier.getPath().c_str(),
"",
"",
1366 MY_UNPACK_FILENAME|MY_APPEND_EXT),
1367 share->sizeKeys(), keydef,
1368 create_records, recinfo,
1369 0, (MI_UNIQUEDEF*) 0,
1370 &create_info, create_flags);
1371 free((
unsigned char*) recinfo);
1373 session.getMessageCache().storeTableMessage(identifier, create_proto);
1381 session.getMessageCache().renameTableMessage(from, to);
1383 return mi_rename(from.getPath().c_str(), to.getPath().c_str());
1387void ha_myisam::get_auto_increment(uint64_t ,
1390 uint64_t *first_value,
1391 uint64_t *nb_reserved_values)
1395 unsigned char key[MI_MAX_KEY_LENGTH];
1397 if (!getTable()->getShare()->next_number_key_offset)
1399 ha_myisam::info(HA_STATUS_AUTO);
1400 *first_value= stats.auto_increment_value;
1402 *nb_reserved_values= UINT64_MAX;
1407 mi_flush_bulk_insert(file, getTable()->getShare()->next_number_index);
1409 (void) extra(HA_EXTRA_KEYREAD);
1410 key_copy(key, getTable()->getInsertRecord(),
1411 &getTable()->key_info[getTable()->getShare()->next_number_index],
1412 getTable()->getShare()->next_number_key_offset);
1413 error= mi_rkey(file, getTable()->getUpdateRecord(), (
int) getTable()->getShare()->next_number_index,
1414 key, make_prev_keypart_map(getTable()->getShare()->next_number_keypart),
1415 HA_READ_PREFIX_LAST);
1421 nr= ((uint64_t) getTable()->next_number_field->
1422 val_int_offset(getTable()->getShare()->rec_buff_length)+1);
1424 extra(HA_EXTRA_NO_KEYREAD);
1432 *nb_reserved_values= 1;
1461ha_rows ha_myisam::records_in_range(uint32_t inx,
key_range *min_key,
1464 return (ha_rows) mi_records_in_range(file, (
int) inx, min_key, max_key);
1468uint32_t ha_myisam::checksum()
const
1470 return (uint)file->state->checksum;
1479 &max_sort_file_size,
1480 context.getOptions()[
"max-sort-file-size"].as<uint64_t>()));
1488 context(
"max-sort-file-size",
1489 po::value<uint64_t>(&max_sort_file_size)->default_value(INT32_MAX),
1490 _(
"Don't use the fast sort index method to created index if the temporary file would get bigger than this."));
1491 context(
"sort-buffer-size",
1492 po::value<sort_buffer_constraint>(&sort_buffer_size)->default_value(8192*1024),
1493 _(
"The buffer that is allocated when sorting the index when doing a REPAIR or when creating indexes with CREATE INDEX or ALTER TABLE."));
1497DRIZZLE_DECLARE_PLUGIN
1503 N_(
"MyISAM storage engine: non-transactional, legacy, deprecated"),
1509DRIZZLE_DECLARE_PLUGIN_END;
virtual int read_range_first(const key_range *start_key, const key_range *end_key, bool eq_range, bool sorted)
virtual int read_range_next()
void set_proc_info(const char *info)
drizzle_system_variables & variables
Field * found_next_number_field
int index_read_idx_map(unsigned char *buf, uint32_t index, const unsigned char *key, drizzled::key_part_map keypart_map, enum drizzled::ha_rkey_function find_flag)
Positions an index cursor to the index specified in the handle. Fetches the row if available....
int read_range_first(const drizzled::key_range *start_key, const drizzled::key_range *end_key, bool eq_range_arg, bool sorted)
int doStartTableScan(bool scan)
int delete_all_rows(void)
int index_read_map(unsigned char *buf, const unsigned char *key, drizzled::key_part_map keypart_map, enum drizzled::ha_rkey_function find_flag)
Positions an index cursor to the index specified in the handle. Fetches the row if available....
int reset_auto_increment(uint64_t value)
int external_lock(drizzled::Session *session, int lock_type)
int index_read_last_map(unsigned char *buf, const unsigned char *key, drizzled::key_part_map keypart_map)
The following functions works like index_read, but it find the last row with the current key value or...
Convert from decimal to binary representation.
TODO: Rename this file - func.h is stupid.
An Proxy Wrapper around boost::program_options::variables_map.