参数uchar* head 是已经分配好内存的64个字节的地址

http://mysql.taobao.org/monthly/2015/08/07/

/** *先从.frm文件读取64字节 *第28和第29字节记录了key_length * *利用lseek定位到第30字节,并读取key_length长度的信息到disk_buff中 *disk_buff[0]代表了索引的个数 keys_num *disk_buff[1]代表了索引中列的个数  key_parts_num * *为了把disk_buff中的信息copy到结构体中,还要分配 keys*sizeof(KEY)+key_parts*sizeof(KEY_PARTS_INFO)大小的内存 key_buff,类型为KEY* * * *做循环 *share->key_info=key_buff; *key_part = key_buff+keys_num; * for(int i=0;i<keys;key_buff++)     key_buff->key_length=xxx;     xxxxx   key_buf->key_part = key_part;   for(j=key_buff->key_parts;j>=0;j--,key_part++)    key_part->fieldnr=xxx * *share->field = field_ptr //field_ptr为新分配的内存 *share->fileds = xxx; * for(int i=0;i<xxx;field_ptr++ ) *   field_ptr=xxx; * */

static int open_binary_frm(THD *thd, TABLE_SHARE *share, uchar *head,
                           File file)
{
  ;
  uint new_frm_ver, field_pack_length, new_field_pack_flag;
  uint interval_count, interval_parts, read_length, int_length;
  uint db_create_options, keys, key_parts, n_length;
  uint key_info_length, com_length, null_bit_pos;
  uint extra_rec_buf_length;
  uint i,j;
  bool use_hash;
  char *keynames, *names, *comment_pos;
  uchar forminfo[];
  uchar *record;
  uchar *disk_buff, *strpos, *null_flags, *null_pos;
  ulong pos, record_offset, *rec_per_key, rec_buff_length;
  handler *handler_file= ;
  KEY    *keyinfo;
  KEY_PART_INFO *key_part;
  SQL_CRYPT *crypted=;
  Field  **field_ptr, *reg_field;
  const char **interval_array;
  enum legacy_db_type legacy_db_type;
  my_bitmap_map *bitmaps;
  uchar *extra_segment_buff= ;
  ;
  uchar *format_section_fields= ;
  DBUG_ENTER("open_binary_frm");

  new_field_pack_flag= head[];
  new_frm_ver= (head[] - FRM_VER);
  field_pack_length= new_frm_ver <  ?  : ;
  disk_buff= ;

  error= ;
  /* Position of the form in the form file. */
  if (!(pos= get_form_pos(file, head)))
    goto err;                                   /* purecov: inspected */

  mysql_file_seek(file,pos,MY_SEEK_SET,MYF());
  ,MYF(MY_NABP)))
    goto err;
  share->frm_version= head[];

  share->max_rows= uint4korr(head+);
  share->min_rows= uint4korr(head+);

  /* Read keyinformation */
  key_info_length= (uint) uint2korr(head+28);
  mysql_file_seek(file, (), MY_SEEK_SET, MYF());

  /**   *disk_buff已经被分配了内存,大小为key_info_length   */
  if (read_string(file,(uchar**) &disk_buff,key_info_length))
    goto err;                                   /* purecov: inspected */
  ] & 0x80)
  {
    share->keys=      keys=      (disk_buff[] << ) | (disk_buff[] & 0x7f);
    share->key_parts= key_parts= uint2korr(disk_buff+);
  }
  else
  {
    share->keys=      keys=      disk_buff[];
    share->key_parts= key_parts= disk_buff[];
  }
 
  /**   *因为keyinfo是MYSQL服务器这边的结构体,因此需要分配内存   */
  n_length=keys*sizeof(KEY)+key_parts*sizeof(KEY_PART_INFO);
  ))))
    goto err;
  bzero((char*) keyinfo,n_length);

  /**   *share->key_info代表keyinfo这段内存起始地址   *keyinfo的类型为KEY*,故keyinfo++,意味着数组递增   *   */
  share->key_info= keyinfo;
  key_part= reinterpret_cast<KEY_PART_INFO*>(keyinfo+keys);
  strpos=disk_buff+;

  if (!(rec_per_key= (ulong*) alloc_root(&share->mem_root,
                                         sizeof(ulong)*key_parts)))
    goto err;

   ; i < keys ; i++, keyinfo++)
  {
    keyinfo->table= ;                           // Updated in open_frm
    )
    {
      keyinfo->flags=       (uint) uint2korr(strpos) ^ HA_NOSAME;      //索引长度,索引中列的个数
      keyinfo->key_length= ();
      keyinfo->key_parts=  (];
      keyinfo->algorithm=  (];
      keyinfo->block_size= uint2korr(strpos+);
      strpos+=;
    }

    keyinfo->key_part=     key_part;
    keyinfo->rec_per_key= rec_per_key;
    for (j=keyinfo->key_parts ; j-- ; key_part++)
    {
      *rec_per_key++=;
      key_part->fieldnr=    (uint16) (uint2korr(strpos) & FIELD_NR_MASK);
      key_part->offset= ()-;
      key_part->key_type=    ();
    }
  }
  keynames=(char*) key_part;
  strpos+= (strmov(keynames, (;

  //reading index comments
  ; i < keys; i++, keyinfo++)
  {
    if (keyinfo->flags & HA_USES_COMMENT)
    {
      keyinfo->comment.length= uint2korr(strpos);
      keyinfo->comment.str= strmake_root(&share->mem_root, (,
                                         keyinfo->comment.length);
      strpos+=  + keyinfo->comment.length;
    }
  }

  share->key_block_size= uint2korr(head+);

  share->fields= uint2korr(forminfo+);
  pos= uint2korr(forminfo+);   /* Length of all screens */
  n_length= uint2korr(forminfo+);

  share->null_fields= uint2korr(forminfo+);
  com_length= uint2korr(forminfo+);
if (!(field_ptr = (Field **)
    alloc_root(&share->mem_root,
           ()*sizeof(Field*)+
               interval_count*sizeof(TYPELIB)+
               (share->fields+interval_parts+
                keys+)*sizeof(char *)+
               (n_length+int_length+com_length)))))
    goto err;                                   /* purecov: inspected */
    /**   *field_ptr也是刚分配内存的起始地址,赋值给share->field   */
  share->field= field_ptr;
  read_length=(uint) (share->fields * field_pack_length +
              pos+ (uint) (n_length+int_length+com_length));
  if (read_string(file,(uchar**) &disk_buff,read_length))
    goto err;                                   /* purecov: inspected */
  strpos= disk_buff+pos;

if (keynames)
    fix_type_pointers(&interval_array, &share->keynames, , &keynames);

 /* Allocate handler */
  if (!(handler_file= get_new_handler(share, thd->mem_root,
                                      share->db_type())))
    goto err;

  record= share->default_values-;              /* Fieldstart = 1 */
  if (share->null_field_first)
  {
    null_flags= null_pos= (uchar*) record+;
    null_bit_pos= (db_create_options & HA_OPTION_PACK_RECORD) ?  : ;
    /*
      null_bytes below is only correct under the condition that
      there are no bit fields.  Correct values is set below after the
      table struct is initialized
    */
    share->null_bytes= (share->null_fields + null_bit_pos + ) / ;
  }
 ; i < share->fields; i++, strpos+=field_pack_length, field_ptr++)
  {
    uint pack_flag, interval_nr, unireg_type, recpos, field_length;
    enum_field_types field_type;
    CHARSET_INFO *charset=NULL;
    Field::geometry_type geom_type= Field::GEOM_GEOMETRY;
    LEX_STRING comment;

    )
    {
      /* new frm file in 4.1 */
      field_length= uint2korr(strpos+);
      recpos=        uint3korr(strpos+);
      pack_flag=    uint2korr(strpos+);
      unireg_type=  (];
      interval_nr=  (];
      );
      field_type=(enum_field_types) (];

    }

    /**     *这里的field_ptr其实是个FIELD*的对象     */
    *field_ptr= reg_field=
      make_field(share, record+recpos,
         (uint32) field_length,
         null_pos, null_bit_pos,
         pack_flag,
         field_type,
         charset,
         geom_type,
         (Field::utype) MTYP_TYPENR(unireg_type),
         (interval_nr ?
          share->intervals+interval_nr- :
          (TYPELIB*) ),
         share->fieldnames.type_names[i]);

  }//循环结束
  *field_ptr=;                    // End marker

  /* Fix key->name and key_part->field */
  if (key_parts)
  {
    uint primary_key=(uint) (find_type(primary_key_name, &share->keynames,
                                       FIND_TYPE_NO_PREFIX) - );
    longlong ha_option= handler_file->ha_table_flags();
    keyinfo= share->key_info;
    key_part= keyinfo->key_part;
        /**     *share->keys本身是个结构体数组,     *里面每一个key,设置其name,     *key中的key_part也是个结构体数组,其中的filednr为share->filed该数组中的下标     *     */
     ; key < share->keys ; key++,keyinfo++)
    {
      ;
      keyinfo->name=(char*) share->keynames.type_names[key];
      ; i < keyinfo->key_parts ; key_part++,i++)
      {
        Field *field;

        key_part->fieldnr= (uint16) find_field(share->field,share->default_values,(uint) key_part->offset,(uint)                   key_part->length);

        field= key_part->field= share->field[key_part->fieldnr-];
        key_part->type= field->key_type();
        if (field->null_ptr)
        {
          key_part->null_offset=(uint) ((uchar*) field->null_ptr -
                                        share->default_values);
          key_part->null_bit= field->null_bit;
          key_part->store_length+=HA_KEY_NULL_LENGTH;
          keyinfo->flags|=HA_NULL_PART_KEY;
          keyinfo->extra_length+= HA_KEY_NULL_LENGTH;
          keyinfo->key_length+= HA_KEY_NULL_LENGTH;
        }

      }

      share->total_key_length+= keyinfo->key_length;

    }

  }
/*
    the correct null_bytes can now be set, since bitfields have been taken
    into account
  */
  share->null_bytes= (null_pos - (uchar*) null_flags +
                      (null_bit_pos + ) / );
  share->last_null_bit_pos= null_bit_pos;

  share->db_low_byte_first= handler_file->low_byte_first();
  share->column_bitmap_size= bitmap_buffer_size(share->fields);

  if (!(bitmaps= (my_bitmap_map*) alloc_root(&share->mem_root,
                                             share->column_bitmap_size)))
    goto err;
  bitmap_init(&share->all_set, bitmaps, share->fields, FALSE);
  bitmap_set_all(&share->all_set);

  delete handler_file;

  my_free(extra_segment_buff);
  DBUG_RETURN ();

 err:
  share->error= error;
  share->open_errno= my_errno;
  share->errarg= errarg;
  my_free(disk_buff);
  my_free(extra_segment_buff);
  delete crypted;
  delete handler_file;
  my_hash_free(&share->name_hash);
  if (share->ha_data_destroy)
  {
    share->ha_data_destroy(share->ha_data);
    share->ha_data_destroy= NULL;
  }
#ifdef WITH_PARTITION_STORAGE_ENGINE
  if (share->ha_part_data_destroy)
  {
    share->ha_part_data_destroy(share->ha_part_data);
    share->ha_data_destroy= NULL;
  }
#endif /* WITH_PARTITION_STORAGE_ENGINE */

  open_table_error(share, error, share->open_errno, errarg);
  DBUG_RETURN(error);
} /* open_binary_frm */

open_binary_frm的更多相关文章

  1. 【MySQL】frm文件解析

    官网说明:http://dev.mysql.com/doc/internals/en/frm-file-format.html frm是MySQL表结构定义文件,通常frm文件是不会损坏的,但是如果出 ...

  2. Innodb加载数据字典 && flush tables

    测试了两个case,属于之前blog的遗留问题: innodb如何加载数据字典 flush tables都做了什么操作 先来看下innodb加载数据字典: 首次使用:select * from tt; ...

随机推荐

  1. html5 webApp常用Meta标签

    Html5 webApp常用Meta标签 <meta charset="UTF-8"> <meta name="viewport" conte ...

  2. js实用功能

    //日期格式转换 Date.prototype.format = function (format) {     /*      * eg:format="yyyy-MM-dd hh:mm: ...

  3. centos msyql 安装与配置

    Mysql具有简单易学.体积小等优点,深受编程开发初学者的喜爱 工具/原料 接入Internet的Centos计算机 安装Mysql 1 Centos 6.6下安装Mysql很简单, yum list ...

  4. spark分片个数的确定及Spark内存错误(GC error)的迂回解决方式

    我们知道,spark中每个分片都代表着一部分数据,那么分片数量如何被确认的呢? 首先我们使用最常见的HDFS+Spark,sparkDeploy的方式来讨论,spark读取HDFS数据使用的是spar ...

  5. HDU 3642 Get The Treasury (线段树扫描线,求体积并)

    参考链接 : http://blog.csdn.net/zxy_snow/article/details/6870127 题意:给你n个立方体,求覆盖三次以上(包括三次)的区域的体积 思路:先将z坐标 ...

  6. SSH 使用JUnit测试

    前提是引入两个包:org.springframework.test-3.1.3.RELEASE和JUnit4. package com.qk.test; import javax.annotation ...

  7. fuel openstack 在 VirtualBox 上的部署

    搞了两天,终于搞好了.说说过程: 一开始用的6.1版本的Fuel.iso,按照国内外的教程装了几遍,但是网路验证始终不能通过.自己又不是很懂网络.后来在网上看到说6.1版的需要fuel-master连 ...

  8. AcmeAir安装AI探针--企业版

    通过脚本安装AI探针请点击通过脚本自动安装探针 一.安装企业版AI探针准备工作: 1. 准备好可用的docker版AcmeAir应用 2. 准备好可用的企业版AIServer 3. 下载好合适版本的J ...

  9. Awstats分析Nginx日志

    1.nginx日志格式设定 log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$ ...

  10. WPF如何用TreeView制作好友列表、播放列表

    WPF如何用TreeView制作好友列表.播放列表 前言 TreeView这个控件对于我来说是用得比较多的,以前做的小聊天软件(好友列表).音乐播放器(播放列表).类库展示器(树形类结构)等都用的是T ...