1/* Copyright (c) 2003, 2011, Oracle and/or its affiliates.
2 Copyright (c) 2010, 2011, Monty Program Ab
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16
17/*
18 Handling of multiple key caches
19
20 The idea is to have a thread safe hash on the table name,
21 with a default key cache value that is returned if the table name is not in
22 the cache.
23*/
24
25#include "mysys_priv.h"
26#include <keycache.h>
27#include <hash.h>
28#include <m_string.h>
29#include "my_safehash.h"
30
31/*****************************************************************************
32 Functions to handle the key cache objects
33*****************************************************************************/
34
35/* Variable to store all key cache objects */
36static SAFE_HASH key_cache_hash;
37
38
39my_bool multi_keycache_init(void)
40{
41 return safe_hash_init(&key_cache_hash, 16, (uchar*) dflt_key_cache);
42}
43
44
45void multi_keycache_free(void)
46{
47 safe_hash_free(&key_cache_hash);
48}
49
50/*
51 Get a key cache to be used for a specific table.
52
53 SYNOPSIS
54 multi_key_cache_search()
55 key key to find (usually table path)
56 uint length Length of key.
57 def Default value if no key cache
58
59 NOTES
60 This function is coded in such a way that we will return the
61 default key cache even if one never called multi_keycache_init.
62 This will ensure that it works with old MyISAM clients.
63
64 RETURN
65 key cache to use
66*/
67
68KEY_CACHE *multi_key_cache_search(uchar *key, uint length,
69 KEY_CACHE *def)
70{
71 if (!key_cache_hash.hash.records)
72 return def;
73 return (KEY_CACHE*) safe_hash_search(&key_cache_hash, key, length,
74 (void*) def);
75}
76
77
78/*
79 Assosiate a key cache with a key
80
81
82 SYONOPSIS
83 multi_key_cache_set()
84 key key (path to table etc..)
85 length Length of key
86 key_cache cache to assococite with the table
87
88 NOTES
89 This can be used both to insert a new entry and change an existing
90 entry
91*/
92
93
94my_bool multi_key_cache_set(const uchar *key, uint length,
95 KEY_CACHE *key_cache)
96{
97 return safe_hash_set(&key_cache_hash, key, length, (uchar*) key_cache);
98}
99
100
101void multi_key_cache_change(KEY_CACHE *old_data,
102 KEY_CACHE *new_data)
103{
104 safe_hash_change(&key_cache_hash, (uchar*) old_data, (uchar*) new_data);
105}
106
107
108