MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
keycaches.cc
1 /* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software Foundation,
14  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15 
16 #include "keycaches.h"
17 
18 /****************************************************************************
19  Named list handling
20 ****************************************************************************/
21 
22 NAMED_ILIST key_caches;
23 
24 uchar* find_named(I_List<NAMED_ILINK> *list, const char *name, uint length,
25  NAMED_ILINK **found)
26 {
28  NAMED_ILINK *element;
29  while ((element= it++))
30  {
31  if (element->cmp(name, length))
32  {
33  if (found)
34  *found= element;
35  return element->data;
36  }
37  }
38  return 0;
39 }
40 
41 
42 void NAMED_ILIST::delete_elements(void (*free_element)(const char *name, uchar*))
43 {
44  NAMED_ILINK *element;
45  DBUG_ENTER("NAMED_ILIST::delete_elements");
46  while ((element= get()))
47  {
48  (*free_element)(element->name, element->data);
49  delete element;
50  }
51  DBUG_VOID_RETURN;
52 }
53 
54 
55 /* Key cache functions */
56 
57 LEX_STRING default_key_cache_base= {C_STRING_WITH_LEN("default")};
58 
59 KEY_CACHE zero_key_cache;
60 
61 KEY_CACHE *get_key_cache(LEX_STRING *cache_name)
62 {
63  if (!cache_name || ! cache_name->length)
64  cache_name= &default_key_cache_base;
65  return ((KEY_CACHE*) find_named(&key_caches,
66  cache_name->str, cache_name->length, 0));
67 }
68 
69 KEY_CACHE *create_key_cache(const char *name, uint length)
70 {
71  KEY_CACHE *key_cache;
72  DBUG_ENTER("create_key_cache");
73  DBUG_PRINT("enter",("name: %.*s", length, name));
74 
75  if ((key_cache= (KEY_CACHE*) my_malloc(sizeof(KEY_CACHE),
76  MYF(MY_ZEROFILL | MY_WME))))
77  {
78  if (!new NAMED_ILINK(&key_caches, name, length, (uchar*) key_cache))
79  {
80  my_free(key_cache);
81  key_cache= 0;
82  }
83  else
84  {
85  /*
86  Set default values for a key cache
87  The values in dflt_key_cache_var is set by my_getopt() at startup
88 
89  We don't set 'buff_size' as this is used to enable the key cache
90  */
91  key_cache->param_block_size= dflt_key_cache_var.param_block_size;
92  key_cache->param_division_limit= dflt_key_cache_var.param_division_limit;
93  key_cache->param_age_threshold= dflt_key_cache_var.param_age_threshold;
94  }
95  }
96  DBUG_RETURN(key_cache);
97 }
98 
99 
100 KEY_CACHE *get_or_create_key_cache(const char *name, uint length)
101 {
102  LEX_STRING key_cache_name;
103  KEY_CACHE *key_cache;
104 
105  key_cache_name.str= (char *) name;
106  key_cache_name.length= length;
107  if (!(key_cache= get_key_cache(&key_cache_name)))
108  key_cache= create_key_cache(name, length);
109  return key_cache;
110 }
111 
112 
113 void free_key_cache(const char *name, KEY_CACHE *key_cache)
114 {
115  end_key_cache(key_cache, 1); // Can never fail
116  my_free(key_cache);
117 }
118 
119 
120 bool process_key_caches(process_key_cache_t func)
121 {
122  I_List_iterator<NAMED_ILINK> it(key_caches);
123  NAMED_ILINK *element;
124 
125  while ((element= it++))
126  {
127  KEY_CACHE *key_cache= (KEY_CACHE *) element->data;
128  func(element->name, key_cache);
129  }
130  return 0;
131 }
132