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