~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2003 MySQL AB
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
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
/*
17
  Key cache assignments
18
*/
19
20
#include "myisamdef.h"
21
22
/*
23
  Assign pages of the index file for a table to a key cache
24
25
  SYNOPSIS
26
    mi_assign_to_key_cache()
27
      info          open table
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
28
      key_map       map of indexes to assign to the key cache
1 by brian
clean slate
29
      key_cache_ptr pointer to the key cache handle
30
      assign_lock   Mutex to lock during assignment
31
32
  PREREQUESTS
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.
35
36
    The caller must also ensure that one doesn't call this function from
37
    two different threads with the same table.
38
39
  NOTES
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.
43
44
  RETURN VALUE
45
    0  If a success
46
    #  Error code
47
*/
48
779.3.1 by Monty Taylor
More cleanup.
49
int mi_assign_to_key_cache(MI_INFO *info, uint64_t key_map,
1 by brian
clean slate
50
			   KEY_CACHE *key_cache)
51
{
779.3.1 by Monty Taylor
More cleanup.
52
  (void)key_map;
1 by brian
clean slate
53
  int error= 0;
54
  MYISAM_SHARE* share= info->s;
55
56
  /*
57
    Skip operation if we didn't change key cache. This can happen if we
58
    call this for all open instances of the same table
59
  */
60
  if (share->key_cache == key_cache)
51.1.102 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
61
    return(0);
1 by brian
clean slate
62
63
  /*
64
    First flush all blocks for the table in the old key cache.
65
    This is to ensure that the disk is consistent with the data pages
66
    in memory (which may not be the case if the table uses delayed_key_write)
67
68
    Note that some other read thread may still fill in the key cache with
69
    new blocks during this call and after, but this doesn't matter as
70
    all threads will start using the new key cache for their next call to
71
    myisam library and we know that there will not be any changed blocks
72
    in the old key cache.
73
  */
74
75
  if (flush_key_blocks(share->key_cache, share->kfile, FLUSH_RELEASE))
76
  {
77
    error= my_errno;
78
    mi_print_error(info->s, HA_ERR_CRASHED);
79
    mi_mark_crashed(info);		/* Mark that table must be checked */
80
  }
81
82
  /*
83
    Flush the new key cache for this file.  This is needed to ensure
84
    that there is no old blocks (with outdated data) left in the new key
85
    cache from an earlier assign_to_keycache operation
86
87
    (This can never fail as there is never any not written data in the
88
    new key cache)
89
  */
90
  (void) flush_key_blocks(key_cache, share->kfile, FLUSH_RELEASE);
91
92
  /*
93
    ensure that setting the key cache and changing the multi_key_cache
94
    is done atomicly
95
  */
96
  pthread_mutex_lock(&share->intern_lock);
97
  /*
98
    Tell all threads to use the new key cache
99
    This should be seen at the lastes for the next call to an myisam function.
100
  */
101
  share->key_cache= key_cache;
102
103
  /* store the key cache in the global hash structure for future opens */
481 by Brian Aker
Remove all of uchar.
104
  if (multi_key_cache_set((unsigned char*) share->unique_file_name,
1 by brian
clean slate
105
                          share->unique_name_length,
106
			  share->key_cache))
107
    error= my_errno;
108
  pthread_mutex_unlock(&share->intern_lock);
51.1.102 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
109
  return(error);
1 by brian
clean slate
110
}
111
112
113
/*
114
  Change all MyISAM entries that uses one key cache to another key cache
115
116
  SYNOPSIS
117
    mi_change_key_cache()
118
    old_key_cache	Old key cache
119
    new_key_cache	New key cache
120
121
  NOTES
122
    This is used when we delete one key cache.
123
124
    To handle the case where some other threads tries to open an MyISAM
125
    table associated with the to-be-deleted key cache while this operation
126
    is running, we have to call 'multi_key_cache_change()' from this
127
    function while we have a lock on the MyISAM table list structure.
128
129
    This is safe as long as it's only MyISAM that is using this specific
130
    key cache.
131
*/
132
133
134
void mi_change_key_cache(KEY_CACHE *old_key_cache,
135
			 KEY_CACHE *new_key_cache)
136
{
137
  LIST *pos;
138
139
  /*
140
    Lock list to ensure that no one can close the table while we manipulate it
141
  */
142
  pthread_mutex_lock(&THR_LOCK_myisam);
143
  for (pos=myisam_open_list ; pos ; pos=pos->next)
144
  {
145
    MI_INFO *info= (MI_INFO*) pos->data;
146
    MYISAM_SHARE *share= info->s;
147
    if (share->key_cache == old_key_cache)
365.2.8 by Monty Taylor
More MAX macros.
148
      mi_assign_to_key_cache(info, UINT64_MAX, new_key_cache);
1 by brian
clean slate
149
  }
150
151
  /*
152
    We have to do the following call while we have the lock on the
153
    MyISAM list structure to ensure that another thread is not trying to
154
    open a new table that will be associted with the old key cache
155
  */
156
  multi_key_cache_change(old_key_cache, new_key_cache);
157
  pthread_mutex_unlock(&THR_LOCK_myisam);
51.1.102 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
158
  return;
1 by brian
clean slate
159
}