~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/myisam/ha_myisam.cc

  • Committer: Brian Aker
  • Date: 2009-08-24 19:19:39 UTC
  • mfrom: (1121.1.6 merge)
  • Revision ID: brian@gaz-20090824191939-xcn528r7gwjc48h3
Merge Brian

Show diffs side-by-side

added added

removed removed

Lines of Context:
69
69
   : StorageEngine(name_arg, 
70
70
                   HTON_CAN_RECREATE | 
71
71
                   HTON_TEMPORARY_ONLY | 
72
 
                   HTON_FILE_BASED |
73
 
                   HTON_DATA_DIR |
74
 
                   HTON_INDEX_DIR) {}
 
72
                   HTON_FILE_BASED ) {}
75
73
 
76
74
  virtual handler *create(TableShare *table,
77
75
                          MEM_ROOT *mem_root)
638
636
  return mi_write(file,buf);
639
637
}
640
638
 
641
 
int ha_myisam::check(Session* session, HA_CHECK_OPT* check_opt)
642
 
{
643
 
  if (!file) return HA_ADMIN_INTERNAL_ERROR;
644
 
  int error;
645
 
  MI_CHECK param;
646
 
  MYISAM_SHARE* share = file->s;
647
 
  const char *old_proc_info= session->get_proc_info();
648
 
 
649
 
  session->set_proc_info("Checking table");
650
 
  myisamchk_init(&param);
651
 
  param.session = session;
652
 
  param.op_name =   "check";
653
 
  param.db_name=    table->s->db.str;
654
 
  param.table_name= table->alias;
655
 
  param.testflag = check_opt->flags | T_CHECK | T_SILENT;
656
 
  param.stats_method= MI_STATS_METHOD_NULLS_NOT_EQUAL;
657
 
 
658
 
  if (!(table->db_stat & HA_READ_ONLY))
659
 
    param.testflag|= T_STATISTICS;
660
 
  param.using_global_keycache = 1;
661
 
 
662
 
  if (!mi_is_crashed(file) &&
663
 
      (((param.testflag & T_CHECK_ONLY_CHANGED) &&
664
 
        !(share->state.changed & (STATE_CHANGED | STATE_CRASHED |
665
 
                                  STATE_CRASHED_ON_REPAIR)) &&
666
 
        share->state.open_count == 0) ||
667
 
       ((param.testflag & T_FAST) && (share->state.open_count ==
668
 
                                      (uint) (share->global_changed ? 1 : 0)))))
669
 
    return HA_ADMIN_ALREADY_DONE;
670
 
 
671
 
  error = chk_status(&param, file);             // Not fatal
672
 
  error = chk_size(&param, file);
673
 
  if (!error)
674
 
    error |= chk_del(&param, file, param.testflag);
675
 
  if (!error)
676
 
    error = chk_key(&param, file);
677
 
  if (!error)
678
 
  {
679
 
    if ((!(param.testflag & T_QUICK) &&
680
 
         ((share->options &
681
 
           (HA_OPTION_PACK_RECORD | HA_OPTION_COMPRESS_RECORD)) ||
682
 
          (param.testflag & (T_EXTEND | T_MEDIUM)))) ||
683
 
        mi_is_crashed(file))
684
 
    {
685
 
      uint32_t old_testflag=param.testflag;
686
 
      param.testflag|=T_MEDIUM;
687
 
      if (!(error= init_io_cache(&param.read_cache, file->dfile,
688
 
                                 my_default_record_cache_size, READ_CACHE,
689
 
                                 share->pack.header_length, 1, MYF(MY_WME))))
690
 
      {
691
 
        error= chk_data_link(&param, file, param.testflag & T_EXTEND);
692
 
        end_io_cache(&(param.read_cache));
693
 
      }
694
 
      param.testflag= old_testflag;
695
 
    }
696
 
  }
697
 
  if (!error)
698
 
  {
699
 
    if ((share->state.changed & (STATE_CHANGED |
700
 
                                 STATE_CRASHED_ON_REPAIR |
701
 
                                 STATE_CRASHED | STATE_NOT_ANALYZED)) ||
702
 
        (param.testflag & T_STATISTICS) ||
703
 
        mi_is_crashed(file))
704
 
    {
705
 
      file->update|=HA_STATE_CHANGED | HA_STATE_ROW_CHANGED;
706
 
      pthread_mutex_lock(&share->intern_lock);
707
 
      share->state.changed&= ~(STATE_CHANGED | STATE_CRASHED |
708
 
                               STATE_CRASHED_ON_REPAIR);
709
 
      if (!(table->db_stat & HA_READ_ONLY))
710
 
        error=update_state_info(&param,file,UPDATE_TIME | UPDATE_OPEN_COUNT |
711
 
                                UPDATE_STAT);
712
 
      pthread_mutex_unlock(&share->intern_lock);
713
 
      info(HA_STATUS_NO_LOCK | HA_STATUS_TIME | HA_STATUS_VARIABLE |
714
 
           HA_STATUS_CONST);
715
 
    }
716
 
  }
717
 
  else if (!mi_is_crashed(file) && !session->killed)
718
 
  {
719
 
    mi_mark_crashed(file);
720
 
    file->update |= HA_STATE_CHANGED | HA_STATE_ROW_CHANGED;
721
 
  }
722
 
 
723
 
  session->set_proc_info(old_proc_info);
724
 
  return error ? HA_ADMIN_CORRUPT : HA_ADMIN_OK;
725
 
}
726
 
 
727
 
 
728
 
/*
729
 
  analyze the key distribution in the table
730
 
  As the table may be only locked for read, we have to take into account that
731
 
  two threads may do an analyze at the same time!
732
 
*/
733
 
 
734
 
int ha_myisam::analyze(Session *session,
735
 
                       HA_CHECK_OPT* )
736
 
{
737
 
  int error=0;
738
 
  MI_CHECK param;
739
 
  MYISAM_SHARE* share = file->s;
740
 
 
741
 
  myisamchk_init(&param);
742
 
  param.session = session;
743
 
  param.op_name=    "analyze";
744
 
  param.db_name=    table->s->db.str;
745
 
  param.table_name= table->alias;
746
 
  param.testflag= (T_FAST | T_CHECK | T_SILENT | T_STATISTICS |
747
 
                   T_DONT_CHECK_CHECKSUM);
748
 
  param.using_global_keycache = 1;
749
 
  param.stats_method= MI_STATS_METHOD_NULLS_NOT_EQUAL;
750
 
 
751
 
  if (!(share->state.changed & STATE_NOT_ANALYZED))
752
 
    return HA_ADMIN_ALREADY_DONE;
753
 
 
754
 
  error = chk_key(&param, file);
755
 
  if (!error)
756
 
  {
757
 
    pthread_mutex_lock(&share->intern_lock);
758
 
    error=update_state_info(&param,file,UPDATE_STAT);
759
 
    pthread_mutex_unlock(&share->intern_lock);
760
 
  }
761
 
  else if (!mi_is_crashed(file) && !session->killed)
762
 
    mi_mark_crashed(file);
763
 
  return error ? HA_ADMIN_CORRUPT : HA_ADMIN_OK;
764
 
}
765
 
 
766
 
 
767
 
int ha_myisam::repair(Session* session, HA_CHECK_OPT *check_opt)
768
 
{
769
 
  int error;
770
 
  MI_CHECK param;
771
 
  ha_rows start_records;
772
 
 
773
 
  if (!file) return HA_ADMIN_INTERNAL_ERROR;
774
 
 
775
 
  myisamchk_init(&param);
776
 
  param.session = session;
777
 
  param.op_name=  "repair";
778
 
  param.testflag= ((check_opt->flags & ~(T_EXTEND)) |
779
 
                   T_SILENT | T_FORCE_CREATE | T_CALC_CHECKSUM |
780
 
                   (check_opt->flags & T_EXTEND ? T_REP : T_REP_BY_SORT));
781
 
  param.sort_buffer_length=  (size_t)sort_buffer_size;
782
 
 
783
 
  // Release latches since this can take a long time
784
 
  ha_release_temporary_latches(session);
785
 
 
786
 
  start_records=file->state->records;
787
 
  while ((error=repair(session,param,0)) && param.retry_repair)
788
 
  {
789
 
    param.retry_repair=0;
790
 
    if (test_all_bits(param.testflag,
791
 
                      (uint) (T_RETRY_WITHOUT_QUICK | T_QUICK)))
792
 
    {
793
 
      param.testflag&= ~T_RETRY_WITHOUT_QUICK;
794
 
      errmsg_printf(ERRMSG_LVL_INFO, "Retrying repair of: '%s' without quick",
795
 
                            table->s->path.str);
796
 
      continue;
797
 
    }
798
 
    param.testflag&= ~T_QUICK;
799
 
    if ((param.testflag & T_REP_BY_SORT))
800
 
    {
801
 
      param.testflag= (param.testflag & ~T_REP_BY_SORT) | T_REP;
802
 
      errmsg_printf(ERRMSG_LVL_INFO, "Retrying repair of: '%s' with keycache",
803
 
                            table->s->path.str);
804
 
      continue;
805
 
    }
806
 
    break;
807
 
  }
808
 
  if (!error && start_records != file->state->records &&
809
 
      !(check_opt->flags & T_VERY_SILENT))
810
 
  {
811
 
    char llbuff[22],llbuff2[22];
812
 
    errmsg_printf(ERRMSG_LVL_INFO, "Found %s of %s rows when repairing '%s'",
813
 
                          llstr(file->state->records, llbuff),
814
 
                          llstr(start_records, llbuff2),
815
 
                          table->s->path.str);
816
 
  }
817
 
  return error;
818
 
}
819
 
 
820
 
int ha_myisam::optimize(Session* session, HA_CHECK_OPT *check_opt)
821
 
{
822
 
  int error;
823
 
  if (!file) return HA_ADMIN_INTERNAL_ERROR;
824
 
  MI_CHECK param;
825
 
 
826
 
  myisamchk_init(&param);
827
 
  param.session = session;
828
 
  param.op_name= "optimize";
829
 
  param.testflag= (check_opt->flags | T_SILENT | T_FORCE_CREATE |
830
 
                   T_REP_BY_SORT | T_STATISTICS | T_SORT_INDEX);
831
 
  param.sort_buffer_length= (size_t)sort_buffer_size;
832
 
  if ((error= repair(session,param,1)) && param.retry_repair)
833
 
  {
834
 
    errmsg_printf(ERRMSG_LVL_WARN, "Warning: Optimize table got errno %d on %s.%s, retrying",
835
 
                      my_errno, param.db_name, param.table_name);
836
 
    param.testflag&= ~T_REP_BY_SORT;
837
 
    error= repair(session,param,1);
838
 
  }
839
 
  return error;
840
 
}
841
 
 
842
639
 
843
640
int ha_myisam::repair(Session *session, MI_CHECK &param, bool do_optimize)
844
641
{
993
790
 
994
791
 
995
792
/*
996
 
  Assign table indexes to a specific key cache.
997
 
*/
998
 
 
999
 
int ha_myisam::assign_to_keycache(Session* session, HA_CHECK_OPT *check_opt)
1000
 
{
1001
 
  KEY_CACHE *new_key_cache= check_opt->key_cache;
1002
 
  const char *errmsg= 0;
1003
 
  int error= HA_ADMIN_OK;
1004
 
  TableList *table_list= table->pos_in_table_list;
1005
 
 
1006
 
  table->keys_in_use_for_query.reset();
1007
 
 
1008
 
  if (table_list->process_index_hints(table))
1009
 
    return(HA_ADMIN_FAILED);
1010
 
 
1011
 
  if ((error= mi_assign_to_key_cache(file, new_key_cache)))
1012
 
  {
1013
 
    char buf[STRING_BUFFER_USUAL_SIZE];
1014
 
    snprintf(buf, sizeof(buf),
1015
 
                "Failed to flush to index file (errno: %d)", error);
1016
 
    errmsg= buf;
1017
 
    error= HA_ADMIN_CORRUPT;
1018
 
  }
1019
 
 
1020
 
  if (error != HA_ADMIN_OK)
1021
 
  {
1022
 
    /* Send error to user */
1023
 
    MI_CHECK param;
1024
 
    myisamchk_init(&param);
1025
 
    param.session= session;
1026
 
    param.op_name=    "assign_to_keycache";
1027
 
    param.db_name=    table->s->db.str;
1028
 
    param.table_name= table->s->table_name.str;
1029
 
    param.testflag= 0;
1030
 
    mi_check_print_error(&param, errmsg);
1031
 
  }
1032
 
  return(error);
1033
 
}
1034
 
 
1035
 
 
1036
 
/*
1037
793
  Disable indexes, making it persistent if requested.
1038
794
 
1039
795
  SYNOPSIS
1254
1010
 
1255
1011
 
1256
1012
 
1257
 
bool ha_myisam::is_crashed() const
1258
 
{
1259
 
  return (file->s->state.changed & STATE_CRASHED ||
1260
 
          (file->s->state.open_count));
1261
 
}
1262
 
 
1263
1013
int ha_myisam::update_row(const unsigned char *old_data, unsigned char *new_data)
1264
1014
{
1265
1015
  ha_statistic_increment(&SSV::ha_update_count);
1626
1376
                               (uint64_t) 0);
1627
1377
  create_info.data_file_length= (create_proto->options().max_rows() *
1628
1378
                                 create_proto->options().avg_row_length());
1629
 
  create_info.data_file_name= ha_create_info->data_file_name;
1630
 
  create_info.index_file_name= ha_create_info->index_file_name;
 
1379
  create_info.data_file_name= NULL;
 
1380
  create_info.index_file_name=  NULL;
1631
1381
  create_info.language= share->table_charset->number;
1632
1382
 
1633
1383
  if (ha_create_info->options & HA_LEX_CREATE_TMP_TABLE)