改进MySQL的table_cache(二)

2014-11-24 12:01:53 · 作者: · 浏览: 2
tance of the table should be reopen or represents a name-lock

{}

}

if (table)

************

从unused_tables链表中移除刚找到的table

************

else

***********

创建一个新的table实例,并插入到open cache中

***********

while (open_cache.records > table_cache_size && unused_tables) //当cache满时,从中释放未使用的TABLE实例

hash_delete(&open_cache,(uchar*) unused_tables);

if (table_list->create) //创建一个新表

{

*******

检查表是否存在:check_if_table_exists

*******

在table cache的hash中创建一个placeholder(占位符):table_cache_insert_placeholder

将占位符链到open tables list上:

table->open_placeholder= 1;

table->next= thd->open_tables;

thd->open_tables= table;

return table

}

创建一个新的table实例

分配内存table=(TABLE*) my_malloc(sizeof(*table),MYF(MY_WME))

error= open_unireg_entry(thd, table, table_list, alias, key, key_length,

mem_root, (flags & OPEN_VIEW_NO_PARSE));

如果是视图or error < 0 释放内存,返回;

my_hash_insert(&open_cache,(uchar*) table)

------------------------------------------------

patch:http://bugs.mysql.com/bug.php id=33948

增加3个指针:

hash_head:

hash_prev: always point to unused table cached items

hash_next: always point to used table cached items

修改的函数:

free_cache_entry //释放一个表的内存。

close_thread_table //move one table to free list

reopen_name_locked_table //重新打开表,保持链表结构

table_cache_insert_placeholder

open_table

------------------------------------------------------------------------

总结:

增加了三个指针:

hash_head:

hash_prev:

hash_next:

!.............................!head!.........................!

head的左边为空闲item链表

head的右边为占用的item链表

所有item通过hash_prev和hash_next进行双向指针

右边的item的hash_head指向head

操作链表:

1)插入新空闲item:在head节点前加入

2)插入新的被占用item:在head后面加入

3)从链表中删除item:

---若该item为head,修改head右侧的item的hash_head指向head->next

---否则,直接删除item,并释放内存。。

查询空闲节点:

1) 找到head

2) 检测head是否in_use,为False则table = head, true则找到table = head->prev

3)当table 不为NULL时,表示找到一个item,将其插入到head右侧

3) table依旧为NULL---->创建新item,将其插入到head右侧

------------------------------

转载请注明:印风