1 /* Copyright (c) 2002, 2015, 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, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software Foundation,
21 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22
23
24 #include "sp_cache.h"
25
26 #include "my_atomic.h"
27 #include "sp_head.h"
28
29
30 /*
31 Cache of stored routines.
32 */
33
34 extern "C"
35 {
hash_get_key_for_sp_head(const uchar * ptr,size_t * plen,my_bool first)36 static uchar *hash_get_key_for_sp_head(const uchar *ptr, size_t *plen,
37 my_bool first)
38 {
39 sp_head *sp= (sp_head *)ptr;
40 *plen= sp->m_qname.length;
41 return (uchar*) sp->m_qname.str;
42 }
43
44
hash_free_sp_head(void * p)45 static void hash_free_sp_head(void *p)
46 {
47 sp_head *sp= (sp_head *)p;
48 delete sp;
49 }
50 }
51
52
53 class sp_cache
54 {
55 public:
sp_cache()56 sp_cache()
57 {
58 my_hash_init(&m_hashtable, system_charset_info, 0, 0, 0,
59 hash_get_key_for_sp_head, hash_free_sp_head, 0,
60 key_memory_sp_cache);
61 }
62
~sp_cache()63 ~sp_cache()
64 {
65 my_hash_free(&m_hashtable);
66 }
67
68 /**
69 Inserts a sp_head object into a hash table.
70
71 @returns Success status
72 @return TRUE Failure
73 @return FALSE Success
74 */
insert(sp_head * sp)75 bool insert(sp_head *sp)
76 {
77 return my_hash_insert(&m_hashtable, (const uchar *)sp);
78 }
79
lookup(char * name,size_t namelen)80 sp_head *lookup(char *name, size_t namelen)
81 {
82 return (sp_head *) my_hash_search(&m_hashtable, (const uchar *)name,
83 namelen);
84 }
85
remove(sp_head * sp)86 void remove(sp_head *sp)
87 {
88 my_hash_delete(&m_hashtable, (uchar *)sp);
89 }
90
91 /**
92 Remove all elements from a stored routine cache if the current
93 number of elements exceeds the argument value.
94
95 @param[in] upper_limit_for_elements Soft upper limit of elements that
96 can be stored in the cache.
97 */
enforce_limit(ulong upper_limit_for_elements)98 void enforce_limit(ulong upper_limit_for_elements)
99 {
100 if (m_hashtable.records > upper_limit_for_elements)
101 my_hash_reset(&m_hashtable);
102 }
103
104 private:
105 /* All routines in this cache */
106 HASH m_hashtable;
107 }; // class sp_cache
108
109
110 static int64 volatile Cversion= 0;
111
112
113 /*
114 Clear the cache *cp and set *cp to NULL.
115
116 SYNOPSIS
117 sp_cache_clear()
118 cp Pointer to cache to clear
119
120 NOTE
121 This function doesn't invalidate other caches.
122 */
123
sp_cache_clear(sp_cache ** cp)124 void sp_cache_clear(sp_cache **cp)
125 {
126 sp_cache *c= *cp;
127
128 if (c)
129 {
130 delete c;
131 *cp= NULL;
132 }
133 }
134
135
136 /*
137 Insert a routine into the cache.
138
139 SYNOPSIS
140 sp_cache_insert()
141 cp The cache to put routine into
142 sp Routine to insert.
143
144 TODO: Perhaps it will be more straightforward if in case we returned an
145 error from this function when we couldn't allocate sp_cache. (right
146 now failure to put routine into cache will cause a 'SP not found'
147 error to be reported at some later time)
148 */
149
sp_cache_insert(sp_cache ** cp,sp_head * sp)150 void sp_cache_insert(sp_cache **cp, sp_head *sp)
151 {
152 sp_cache *c;
153
154 if (!(c= *cp))
155 {
156 if (!(c= new sp_cache()))
157 return; // End of memory error
158 }
159 sp->set_sp_cache_version(sp_cache_version());
160 DBUG_PRINT("info",("sp_cache: inserting: %.*s", (int) sp->m_qname.length,
161 sp->m_qname.str));
162 c->insert(sp);
163 *cp= c; // Update *cp if it was NULL
164 }
165
166
167 /*
168 Look up a routine in the cache.
169 SYNOPSIS
170 sp_cache_lookup()
171 cp Cache to look into
172 name Name of rutine to find
173
174 NOTE
175 An obsolete (but not more obsolete then since last
176 sp_cache_flush_obsolete call) routine may be returned.
177
178 RETURN
179 The routine or
180 NULL if the routine not found.
181 */
182
sp_cache_lookup(sp_cache ** cp,sp_name * name)183 sp_head *sp_cache_lookup(sp_cache **cp, sp_name *name)
184 {
185 sp_cache *c= *cp;
186 if (! c)
187 return NULL;
188 return c->lookup(name->m_qname.str, name->m_qname.length);
189 }
190
191
192 /*
193 Invalidate all routines in all caches.
194
195 SYNOPSIS
196 sp_cache_invalidate()
197
198 NOTE
199 This is called when a VIEW definition is created or modified (and in some
200 other contexts). We can't destroy sp_head objects here as one may modify
201 VIEW definitions from prelocking-free SPs.
202 */
203
sp_cache_invalidate()204 void sp_cache_invalidate()
205 {
206 DBUG_PRINT("info",("sp_cache: invalidating"));
207 my_atomic_add64(&Cversion, 1);
208 }
209
210
211 /**
212 Remove an out-of-date SP from the cache.
213
214 @param[in] cp Cache to flush
215 @param[in] sp SP to remove.
216
217 @note This invalidates pointers to sp_head objects this thread
218 uses. In practice that means 'dont call this function when
219 inside SP'.
220 */
221
sp_cache_flush_obsolete(sp_cache ** cp,sp_head ** sp)222 void sp_cache_flush_obsolete(sp_cache **cp, sp_head **sp)
223 {
224 if ((*sp)->sp_cache_version() < sp_cache_version() && !(*sp)->is_invoked())
225 {
226 (*cp)->remove(*sp);
227 *sp= NULL;
228 }
229 }
230
231
232 /**
233 Return the current global version of the cache.
234 */
235
sp_cache_version()236 int64 sp_cache_version()
237 {
238 return my_atomic_load64(&Cversion);
239 }
240
241
242 /**
243 Enforce that the current number of elements in the cache don't exceed
244 the argument value by flushing the cache if necessary.
245
246 @param[in] c Cache to check
247 @param[in] upper_limit_for_elements Soft upper limit for number of sp_head
248 objects that can be stored in the cache.
249 */
250 void
sp_cache_enforce_limit(sp_cache * c,ulong upper_limit_for_elements)251 sp_cache_enforce_limit(sp_cache *c, ulong upper_limit_for_elements)
252 {
253 if (c)
254 c->enforce_limit(upper_limit_for_elements);
255 }
256