Home > Database > Mysql Tutorial > body text

MySQL内核:innodb动态数组内部实现[组图]_MySQL

php.cn
Release: 2016-06-01 13:53:37
original
899 people have browsed it

  动态数组涉及的文件是innodb存储引擎的三个文件:dyn0dyn.h、dyn0dyn.ic以及dyn0dyn.c。

  这是一个基本的组件功能,是作为一个动态的虚拟线性数组。数组的基本元素是byte。动态数组dyn主要用来存放mtr的锁定信息以及log。Dyn在实现上,如果block需要分裂节点,则会使用一个内存堆。每个blok块存储数据的数据字段的长度是固定的(默认值是512),但是不一定会完全用完。假设需要存储的数据项的尺寸大于数据块时,该数据项被分拆,这种情况主要用于log的缓冲。

  2. 数据结构

  typedef struct dyn_block_struct  dyn_block_t;
typedef dyn_block_t     dyn_array_t;
#defineDYN_ARRAY_DATA_SIZE  512
  struct dyn_block_struct{
mem_heap_t*  heap;
ulint    used;
byte    data[DYN_ARRAY_DATA_SIZE];
UT_LIST_BASE_NODE_T(dyn_block_t)  base;
UT_LIST_NODE_T(dyn_block_t)     list;
#ifdef UNIV_DEBUG
ulint    buf_end;
ulint    magic_n;
#endif
};
  //下面两个是公共宏定义,见ut0lst.h
#define UT_LIST_BASE_NODE_T(TYPE)
struct {
ulintcount;/* count of nodes in list */
TYPE *start;/* pointer to list start, NULL if empty */
TYPE *end;/* pointer to list end, NULL if empty */
}
  #define UT_LIST_NODE_T(TYPE)
struct {
TYPE *prev;/* pointer to the previous node,
 NULL if start of list */
TYPE *next;/* pointer to next node, NULL if end of list */
}

  字段解释:

  1) heap

  从字面上理解,heap是内存堆的意思。从上面的结构体中,我们可以看出,dyn_array_t就是dyn_block_struct。这个结构体里面的字段date用来存放数据,used显示已经使用的字节数量,假设这时候还要存储一个大小为长度为x的数据,这时候data里面已经存储不了,所以就需要再分配一个新的block节点。那么分配结构所需要的内存从哪里来了,就从第一个节点里面的heap里面分配。

  这里面,我们还需要主意一点。虽然数据结构用的是同一个dyn_block_struct,但是我们称第一个节点为arr,表明这个是动态数据的头节点。其它的节点,我们称为block节点。

  我们这里面先来看看,刚开始创立dyn的函数是怎么实现的:

  UNIV_INLINE
dyn_array_t*
dyn_array_create(
/*=============*/
  /* out: initialized dyn array */
dyn_array_t*arr)/* in: pointer to a memory buffer of
  size sizeof(dyn_array_t) */
{
ut_ad(arr);
ut_ad(DYN_ARRAY_DATA_SIZE   arr->heap = NULL;
arr->used = 0;
  #ifdef UNIV_DEBUG
arr->buf_end = 0;
arr->magic_n = DYN_BLOCK_MAGIC_N;
#endif
return(arr);
}

  其中的ud_ad是属于红判断,相当于assert。这个我们暂时不表。这个函数是在mtr创建的时候来调用的,在调用的时候已经分配了arr指针。

  在dyn_array_create函数中,系统将heap字段设置为NULL,Used设置为0。Buf_end和magic_n是调试的时候使用的,每个结构体定义都会唯一的对应一个magic_n值。这样在调试的时候,就可以快速进行问题的跟踪。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Latest issues
Popular Tutorials
More>
Latest downloads
More>
web effects
Website source code
Website materials
Front end template
About us Disclaimer Sitemap
PHP Chinese website:Public welfare online PHP training,Help PHP learners grow quickly!