得到一个table_share

1)先从table_def_cache中查找, 如果有, 直接返回

2)如果没有找到,

   为table_share分配内存,初始化,打开.frm文件,并将share放置hashTable中

   如果hashTable的容量超过限制,根据lru策略,在oldest_unused_tables中循环删除table share

注意:

  oldest_unused_tables中的元素是close_table时放入进去的

  1. /*
  2. Get TABLE_SHARE for a table.
  3.  
  4. get_table_share()
  5. thd Thread handle
  6. table_list Table that should be opened
  7. key Table cache key
  8. key_length Length of key
  9. db_flags Flags to open_table_def():
  10. OPEN_VIEW
  11. error out: Error code from open_table_def()
  12.  
  13. IMPLEMENTATION
  14. Get a table definition from the table definition cache.
  15. If it doesn't exist, create a new from the table definition file.
  16.  
  17. NOTES
  18. We must have wrlock on LOCK_open when we come here
  19. (To be changed later)
  20.  
  21. RETURN
  22. 0 Error
  23. # Share for table
  24. */
  25.  
  26. TABLE_SHARE *get_table_share(THD *thd, TABLE_LIST *table_list, char *key,
  27. uint key_length, uint db_flags, int *error,
  28. my_hash_value_type hash_value)
  29. {
  30. TABLE_SHARE *share;
  31. DBUG_ENTER("get_table_share");
  32.  
  33. *error= ;
  34.  
  35. /*
  36. To be able perform any operation on table we should own
  37. some kind of metadata lock on it.
  38. */
  39. DBUG_ASSERT(thd->mdl_context.is_lock_owner(MDL_key::TABLE,
  40. table_list->db,
  41. table_list->table_name,
  42. MDL_SHARED));
  43.  
  44. /* Read table definition from cache */
  45. if ((share= (TABLE_SHARE*) my_hash_search_using_hash_value(&table_def_cache,
  46. hash_value, (uchar*) key, key_length)))
  47. goto found;
  48.  
  49. if (!(share= alloc_table_share(table_list, key, key_length)))
  50. {
  51. DBUG_RETURN();
  52. }
  53.  
  54. /*
  55. We assign a new table id under the protection of LOCK_open.
  56. We do this instead of creating a new mutex
  57. and using it for the sole purpose of serializing accesses to a
  58. static variable, we assign the table id here. We assign it to the
  59. share before inserting it into the table_def_cache to be really
  60. sure that it cannot be read from the cache without having a table
  61. id assigned.
  62.  
  63. CAVEAT. This means that the table cannot be used for
  64. binlogging/replication purposes, unless get_table_share() has been
  65. called directly or indirectly.
  66. */
  67. assign_new_table_id(share);
  68.  
  69. if (my_hash_insert(&table_def_cache, (uchar*) share))
  70. {
  71. free_table_share(share);
  72. DBUG_RETURN(); // return error
  73. }
  74. if (open_table_def(thd, share, db_flags))
  75. {
  76. *error= share->error;
  77. (void) my_hash_delete(&table_def_cache, (uchar*) share);
  78. DBUG_RETURN();
  79. }
  80. share->ref_count++; // Mark in use
  81. DBUG_PRINT("exit", ("share: 0x%lx ref_count: %u",
  82. (ulong) share, share->ref_count));
  83. DBUG_RETURN(share);
  84.  
  85. found:
  86. /*
  87. We found an existing table definition. Return it if we didn't get
  88. an error when reading the table definition from file.
  89. */
  90. if (share->error)
  91. {
  92. /* Table definition contained an error */
  93. open_table_error(share, share->error, share->open_errno, share->errarg);
  94. DBUG_RETURN();
  95. }
  96. if (share->is_view && !(db_flags & OPEN_VIEW))
  97. {
  98. open_table_error(share, , ENOENT, );
  99. DBUG_RETURN();
  100. }
  101.  
  102. ++share->ref_count;
  103.  
  104. && share->prev)
  105. {
  106. /*
  107. Share was not used before and it was in the old_unused_share list
  108. Unlink share from this list
  109. */
  110. DBUG_PRINT("info", ("Unlinking from not used list"));
  111. *share->prev= share->next;
  112. share->next->prev= share->prev;
  113. share->next= ;
  114. share->prev= ;
  115. }
  116.  
  117. /* Free cache if too big */
  118. while (table_def_cache.records > table_def_size &&
  119. oldest_unused_share->next)
  120. my_hash_delete(&table_def_cache, (uchar*) oldest_unused_share);
  121.  
  122. DBUG_PRINT("exit", ("share: 0x%lx ref_count: %u",
  123. (ulong) share, share->ref_count));
  124. DBUG_RETURN(share);
  125. }

函数get_table_share的更多相关文章

  1. Python 小而美的函数

    python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况   any any(iterable) ...

  2. 探究javascript对象和数组的异同,及函数变量缓存技巧

    javascript中最经典也最受非议的一句话就是:javascript中一切皆是对象.这篇重点要提到的,就是任何jser都不陌生的Object和Array. 有段时间曾经很诧异,到底两种数据类型用来 ...

  3. JavaScript权威指南 - 函数

    函数本身就是一段JavaScript代码,定义一次但可能被调用任意次.如果函数挂载在一个对象上,作为对象的一个属性,通常这种函数被称作对象的方法.用于初始化一个新创建的对象的函数被称作构造函数. 相对 ...

  4. C++对C的函数拓展

    一,内联函数 1.内联函数的概念 C++中的const常量可以用来代替宏常数的定义,例如:用const int a = 10来替换# define a 10.那么C++中是否有什么解决方案来替代宏代码 ...

  5. 菜鸟Python学习笔记第一天:关于一些函数库的使用

    2017年1月3日 星期二 大一学习一门新的计算机语言真的很难,有时候连函数拼写出错查错都能查半天,没办法,谁让我英语太渣. 关于计算机语言的学习我想还是从C语言学习开始为好,Python有很多语言的 ...

  6. javascript中的this与函数讲解

    前言 javascript中没有块级作用域(es6以前),javascript中作用域分为函数作用域和全局作用域.并且,大家可以认为全局作用域其实就是Window函数的函数作用域,我们编写的js代码, ...

  7. 复杂的 Hash 函数组合有意义吗?

    很久以前看到一篇文章,讲某个大网站储存用户口令时,会经过十分复杂的处理.怎么个复杂记不得了,大概就是先 Hash,结果加上一些特殊字符再 Hash,结果再加上些字符.再倒序.再怎么怎么的.再 Hash ...

  8. JS核心系列:浅谈函数的作用域

    一.作用域(scope) 所谓作用域就是:变量在声明它们的函数体以及这个函数体嵌套的任意函数体内都是有定义的. function scope(){ var foo = "global&quo ...

  9. C++中的时间函数

    C++获取时间函数众多,何时该用什么函数,拿到的是什么时间?该怎么用?很多人都会混淆. 本文是本人经历了几款游戏客户端和服务器开发后,对游戏中时间获取的一点总结. 最早学习游戏客户端时,为了获取最精确 ...

随机推荐

  1. python学习小结2:if和while控制语句

    if语句 if语句中,代码块是按缩进的空格数量来判断的,也就是说空格数量一致的相邻行会被当作一个代码块,当if的条件成立的时候它就会得到执行. x = 100 if x > 50: print ...

  2. C#截取文件的文件夹地址

    创建文件 if (!File.Exists(file_name)) { File.Create(file_name).Close(); } using System.IO; 如果没有.Close(), ...

  3. State of Hyperparameter Selection

    State of Hyperparameter Selection DANIEL SALTIEL VIEW NOTEBOOK Historically hyperparameter determina ...

  4. 使用EF code first和asp.net mvc4遇到的问题总结

    最近使用EF code first和asp.net mvc4做项目,遇到些问题,记录一下. 一.EF code first 生成外键列问题. 一般情况下,都是先写一个int型外键id属性,然后写一个外 ...

  5. uva 10626

    dp 记忆化搜索 3个1元和1个10元的情况不能少 #include <cstdio> #include <cstdlib> #include <cmath> #i ...

  6. uva 307

    排序之后再剪枝,有点神 #include <cstdio> #include <cstdlib> #include <cmath> #include <map ...

  7. MATERIALIZED VIEW

    Oracle的实体化视图提供了强大的功能,可以用在不同的环境中,实体化视图和表一样可以直接进行查询.实体化视图可以基于分区表,实体化视图本身也可以分区. 主要用于预先计算并保存表连接或聚集等耗时较多的 ...

  8. auto_ptr的设计动机

    auto_ptr的设计动机 C++标准程序库提供的auto_ptr是一种智能型指针(smart pointer),帮助程序员防止“被异常抛出时发生资源泄露”. 函数的操作经常依以下模式进行: 1.获取 ...

  9. HDU4758 Walk Through Squares AC自动机&&dp

    这道题当时做的时候觉得是数论题,包含两个01串什么的,但是算重复的时候又很蛋疼,赛后听说是字符串,然后就觉得很有可能.昨天队友问到这一题,在学了AC自动机之后就觉得简单了许多.那个时候不懂AC自动机, ...

  10. POJ 3277 City Horizon(叶子节点为[a,a+1)的线段树+离散化)

    网上还有用unique函数和lowerbound函数离散的方法,可以百度搜下题解就有. 这里给出介绍unique函数的链接:http://www.cnblogs.com/zhangshu/archiv ...