1
/* Copyright (C) 2003 MySQL AB
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.
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.
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
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
20
#include "myisamdef.h"
23
Assign pages of the index file for a table to a key cache
26
mi_assign_to_key_cache()
28
key_map map of indexes to assign to the key cache
29
key_cache_ptr pointer to the key cache handle
30
assign_lock Mutex to lock during assignment
33
One must have a READ lock or a WRITE lock on the table when calling
34
the function to ensure that there is no other writers to it.
36
The caller must also ensure that one doesn't call this function from
37
two different threads with the same table.
40
At present pages for all indexes must be assigned to the same key cache.
41
In future only pages for indexes specified in the key_map parameter
42
of the table will be assigned to the specified key cache.
49
int mi_assign_to_key_cache(MI_INFO *info,
50
ulonglong key_map __attribute__((unused)),
54
MYISAM_SHARE* share= info->s;
55
DBUG_ENTER("mi_assign_to_key_cache");
56
DBUG_PRINT("enter",("old_key_cache_handle: 0x%lx new_key_cache_handle: 0x%lx",
57
(long) share->key_cache, (long) key_cache));
60
Skip operation if we didn't change key cache. This can happen if we
61
call this for all open instances of the same table
63
if (share->key_cache == key_cache)
67
First flush all blocks for the table in the old key cache.
68
This is to ensure that the disk is consistent with the data pages
69
in memory (which may not be the case if the table uses delayed_key_write)
71
Note that some other read thread may still fill in the key cache with
72
new blocks during this call and after, but this doesn't matter as
73
all threads will start using the new key cache for their next call to
74
myisam library and we know that there will not be any changed blocks
78
if (flush_key_blocks(share->key_cache, share->kfile, FLUSH_RELEASE))
81
mi_print_error(info->s, HA_ERR_CRASHED);
82
mi_mark_crashed(info); /* Mark that table must be checked */
86
Flush the new key cache for this file. This is needed to ensure
87
that there is no old blocks (with outdated data) left in the new key
88
cache from an earlier assign_to_keycache operation
90
(This can never fail as there is never any not written data in the
93
(void) flush_key_blocks(key_cache, share->kfile, FLUSH_RELEASE);
96
ensure that setting the key cache and changing the multi_key_cache
99
pthread_mutex_lock(&share->intern_lock);
101
Tell all threads to use the new key cache
102
This should be seen at the lastes for the next call to an myisam function.
104
share->key_cache= key_cache;
106
/* store the key cache in the global hash structure for future opens */
107
if (multi_key_cache_set((uchar*) share->unique_file_name,
108
share->unique_name_length,
111
pthread_mutex_unlock(&share->intern_lock);
117
Change all MyISAM entries that uses one key cache to another key cache
120
mi_change_key_cache()
121
old_key_cache Old key cache
122
new_key_cache New key cache
125
This is used when we delete one key cache.
127
To handle the case where some other threads tries to open an MyISAM
128
table associated with the to-be-deleted key cache while this operation
129
is running, we have to call 'multi_key_cache_change()' from this
130
function while we have a lock on the MyISAM table list structure.
132
This is safe as long as it's only MyISAM that is using this specific
137
void mi_change_key_cache(KEY_CACHE *old_key_cache,
138
KEY_CACHE *new_key_cache)
141
DBUG_ENTER("mi_change_key_cache");
144
Lock list to ensure that no one can close the table while we manipulate it
146
pthread_mutex_lock(&THR_LOCK_myisam);
147
for (pos=myisam_open_list ; pos ; pos=pos->next)
149
MI_INFO *info= (MI_INFO*) pos->data;
150
MYISAM_SHARE *share= info->s;
151
if (share->key_cache == old_key_cache)
152
mi_assign_to_key_cache(info, (ulonglong) ~0, new_key_cache);
156
We have to do the following call while we have the lock on the
157
MyISAM list structure to ensure that another thread is not trying to
158
open a new table that will be associted with the old key cache
160
multi_key_cache_change(old_key_cache, new_key_cache);
161
pthread_mutex_unlock(&THR_LOCK_myisam);