drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
mi_extra.cc
1/* Copyright (C) 2000-2005 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16#include "myisam_priv.h"
17#include <drizzled/util/test.h>
18#include <sys/types.h>
19#include <sys/mman.h>
20
21#include <string.h>
22#include <algorithm>
23
24using namespace drizzled;
25using namespace std;
26
27static void mi_extra_keyflag(MI_INFO *info, enum ha_extra_function function);
28
29
30/*
31 Set options and buffers to optimize table handling
32
33 SYNOPSIS
34 mi_extra()
35 info open table
36 function operation
37 extra_arg Pointer to extra argument (normally pointer to uint32_t)
38 Used when function is one of:
39 HA_EXTRA_WRITE_CACHE
40 HA_EXTRA_CACHE
41 RETURN VALUES
42 0 ok
43 # error
44*/
45
46int mi_extra(MI_INFO *info, enum ha_extra_function function, void *extra_arg)
47{
48 int error=0;
49 uint32_t cache_size;
50 MYISAM_SHARE *share=info->s;
51
52 switch (function) {
53 case HA_EXTRA_RESET_STATE: /* Reset state (don't free buffers) */
54 info->lastinx= 0; /* Use first index as def */
55 info->last_search_keypage=info->lastpos= HA_OFFSET_ERROR;
56 info->page_changed=1;
57 /* Next/prev gives first/last */
58 if (info->opt_flag & READ_CACHE_USED)
59 {
60 info->rec_cache.reinit_io_cache(internal::READ_CACHE,0,
61 (bool) (info->lock_type != F_UNLCK),
62 (bool) test(info->update & HA_STATE_ROW_CHANGED));
63 }
64 info->update= ((info->update & HA_STATE_CHANGED) | HA_STATE_NEXT_FOUND |
65 HA_STATE_PREV_FOUND);
66 break;
67 case HA_EXTRA_CACHE:
68 if (info->lock_type == F_UNLCK &&
69 (share->options & HA_OPTION_PACK_RECORD))
70 {
71 error=1; /* Not possibly if not locked */
72 errno=EACCES;
73 break;
74 }
75 if (info->s->file_map) /* Don't use cache if mmap */
76 break;
77 if (info->opt_flag & WRITE_CACHE_USED)
78 {
79 info->opt_flag&= ~WRITE_CACHE_USED;
80 if ((error= info->rec_cache.end_io_cache()))
81 break;
82 }
83 if (!(info->opt_flag &
84 (READ_CACHE_USED | WRITE_CACHE_USED | MEMMAP_USED)))
85 {
86 cache_size= (extra_arg ? *(uint32_t*) extra_arg :
87 internal::my_default_record_cache_size);
88 if (!(info->rec_cache.init_io_cache(info->dfile, (uint) min((uint32_t)info->state->data_file_length+1, cache_size),
89 internal::READ_CACHE,0L,(bool) (info->lock_type != F_UNLCK),
90 MYF(share->write_flag & MY_WAIT_IF_FULL))))
91 {
92 info->opt_flag|=READ_CACHE_USED;
93 info->update&= ~HA_STATE_ROW_CHANGED;
94 }
95 if (share->concurrent_insert)
96 info->rec_cache.end_of_file=info->state->data_file_length;
97 }
98 break;
99 case HA_EXTRA_REINIT_CACHE:
100 if (info->opt_flag & READ_CACHE_USED)
101 {
102 info->rec_cache.reinit_io_cache(internal::READ_CACHE,info->nextpos,
103 (bool) (info->lock_type != F_UNLCK),
104 (bool) test(info->update & HA_STATE_ROW_CHANGED));
105 info->update&= ~HA_STATE_ROW_CHANGED;
106 if (share->concurrent_insert)
107 info->rec_cache.end_of_file=info->state->data_file_length;
108 }
109 break;
110 case HA_EXTRA_WRITE_CACHE:
111 if (info->lock_type == F_UNLCK)
112 {
113 error=1; /* Not possibly if not locked */
114 break;
115 }
116
117 cache_size= (extra_arg ? *(uint32_t*) extra_arg :
118 internal::my_default_record_cache_size);
119 if (not (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED | OPT_NO_ROWS)) && !share->state.header.uniques)
120 {
121 if (not (info->rec_cache.init_io_cache(info->dfile, cache_size,
122 internal::WRITE_CACHE,info->state->data_file_length,
123 (bool) (info->lock_type != F_UNLCK),
124 MYF(share->write_flag & MY_WAIT_IF_FULL))))
125 {
126 info->opt_flag|=WRITE_CACHE_USED;
127 info->update&= ~(HA_STATE_ROW_CHANGED |
128 HA_STATE_WRITE_AT_END |
129 HA_STATE_EXTEND_BLOCK);
130 }
131 }
132 break;
133 case HA_EXTRA_PREPARE_FOR_UPDATE:
134 if (info->s->data_file_type != DYNAMIC_RECORD)
135 break;
136 /* Remove read/write cache if dynamic rows */
137 /* fallthrough */
138 case HA_EXTRA_NO_CACHE:
139 if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
140 {
141 info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
142 error= info->rec_cache.end_io_cache();
143 /* Sergei will insert full text index caching here */
144 }
145 if (info->opt_flag & MEMMAP_USED)
146 madvise((char*) share->file_map, share->state.state.data_file_length,
147 MADV_RANDOM);
148 break;
149 case HA_EXTRA_FLUSH_CACHE:
150 if (info->opt_flag & WRITE_CACHE_USED)
151 {
152 if ((error= info->rec_cache.flush()))
153 {
154 mi_print_error(info->s, HA_ERR_CRASHED);
155 mi_mark_crashed(info); /* Fatal error found */
156 }
157 }
158 break;
159 case HA_EXTRA_NO_READCHECK:
160 info->opt_flag&= ~READ_CHECK_USED; /* No readcheck */
161 break;
162 case HA_EXTRA_READCHECK:
163 info->opt_flag|= READ_CHECK_USED;
164 break;
165 case HA_EXTRA_KEYREAD: /* Read only keys to record */
166 case HA_EXTRA_REMEMBER_POS:
167 info->opt_flag |= REMEMBER_OLD_POS;
168 memmove(info->lastkey+share->base.max_key_length*2,
169 info->lastkey,info->lastkey_length);
170 info->save_update= info->update;
171 info->save_lastinx= info->lastinx;
172 info->save_lastpos= info->lastpos;
173 info->save_lastkey_length=info->lastkey_length;
174 if (function == HA_EXTRA_REMEMBER_POS)
175 break;
176 /* fall through */
177 case HA_EXTRA_KEYREAD_CHANGE_POS:
178 info->opt_flag |= KEY_READ_USED;
179 info->read_record=_mi_read_key_record;
180 break;
181 case HA_EXTRA_NO_KEYREAD:
182 case HA_EXTRA_RESTORE_POS:
183 if (info->opt_flag & REMEMBER_OLD_POS)
184 {
185 memmove(info->lastkey,
186 info->lastkey+share->base.max_key_length*2,
187 info->save_lastkey_length);
188 info->update= info->save_update | HA_STATE_WRITTEN;
189 info->lastinx= info->save_lastinx;
190 info->lastpos= info->save_lastpos;
191 info->lastkey_length=info->save_lastkey_length;
192 }
193 info->read_record= share->read_record;
194 info->opt_flag&= ~(KEY_READ_USED | REMEMBER_OLD_POS);
195 break;
196 case HA_EXTRA_NO_USER_CHANGE: /* Database is somehow locked agains changes */
197 info->lock_type= F_EXTRA_LCK; /* Simulate as locked */
198 break;
199 case HA_EXTRA_WAIT_LOCK:
200 info->lock_wait=0;
201 break;
202 case HA_EXTRA_NO_WAIT_LOCK:
203 info->lock_wait=MY_DONT_WAIT;
204 break;
205 case HA_EXTRA_NO_KEYS:
206 if (info->lock_type == F_UNLCK)
207 {
208 error=1; /* Not possibly if not lock */
209 break;
210 }
211 if (mi_is_any_key_active(share->state.key_map))
212 {
213 MI_KEYDEF *key=share->keyinfo;
214 uint32_t i;
215 for (i=0 ; i < share->base.keys ; i++,key++)
216 {
217 if (!(key->flag & HA_NOSAME) && info->s->base.auto_key != i+1)
218 {
219 mi_clear_key_active(share->state.key_map, i);
220 info->update|= HA_STATE_CHANGED;
221 }
222 }
223
224 if (!share->changed)
225 {
226 share->state.changed|= STATE_CHANGED | STATE_NOT_ANALYZED;
227 share->changed=1; /* Update on close */
228 if (!share->global_changed)
229 {
230 share->global_changed=1;
231 share->state.open_count++;
232 }
233 }
234 share->state.state= *info->state;
235 error=mi_state_info_write(share->kfile,&share->state,1 | 2);
236 }
237 break;
238 case HA_EXTRA_FORCE_REOPEN:
239 THR_LOCK_myisam.lock();
240 share->last_version= 0L; /* Impossible version */
241 THR_LOCK_myisam.unlock();
242 break;
243 case HA_EXTRA_PREPARE_FOR_DROP:
244 THR_LOCK_myisam.lock();
245 share->last_version= 0L; /* Impossible version */
246 THR_LOCK_myisam.unlock();
247 break;
248 case HA_EXTRA_FLUSH:
249#ifdef HAVE_PWRITE
250 _mi_decrement_open_count(info);
251#endif
252 if (share->not_flushed)
253 {
254 share->not_flushed= false;
255 }
256 if (share->base.blobs)
257 mi_alloc_rec_buff(info, SIZE_MAX, &info->rec_buff);
258 break;
259 case HA_EXTRA_NORMAL: /* Theese isn't in use */
260 info->quick_mode=0;
261 break;
262 case HA_EXTRA_QUICK:
263 info->quick_mode=1;
264 break;
265 case HA_EXTRA_NO_ROWS:
266 if (!share->state.header.uniques)
267 info->opt_flag|= OPT_NO_ROWS;
268 break;
269 case HA_EXTRA_PRELOAD_BUFFER_SIZE:
270 info->preload_buff_size= *((uint32_t *) extra_arg);
271 break;
272 case HA_EXTRA_CHANGE_KEY_TO_UNIQUE:
273 case HA_EXTRA_CHANGE_KEY_TO_DUP:
274 mi_extra_keyflag(info, function);
275 break;
276 case HA_EXTRA_KEY_CACHE:
277 case HA_EXTRA_NO_KEY_CACHE:
278 default:
279 break;
280 }
281
282 return(error);
283} /* mi_extra */
284
285
286/*
287 Start/Stop Inserting Duplicates Into a Table, WL#1648.
288 */
289static void mi_extra_keyflag(MI_INFO *info, enum ha_extra_function function)
290{
291 uint32_t idx;
292
293 for (idx= 0; idx< info->s->base.keys; idx++)
294 {
295 switch (function) {
296 case HA_EXTRA_CHANGE_KEY_TO_UNIQUE:
297 info->s->keyinfo[idx].flag|= HA_NOSAME;
298 break;
299 case HA_EXTRA_CHANGE_KEY_TO_DUP:
300 info->s->keyinfo[idx].flag&= ~(HA_NOSAME);
301 break;
302 default:
303 break;
304 }
305 }
306}
307
308
309int mi_reset(MI_INFO *info)
310{
311 int error= 0;
312 MYISAM_SHARE *share=info->s;
313 /*
314 Free buffers and reset the following flags:
315 EXTRA_CACHE, EXTRA_WRITE_CACHE, EXTRA_KEYREAD, EXTRA_QUICK
316
317 If the row buffer cache is large (for dynamic tables), reduce it
318 to save memory.
319 */
320 if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
321 {
322 info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
323 error= info->rec_cache.end_io_cache();
324 }
325 if (share->base.blobs)
326 mi_alloc_rec_buff(info, SIZE_MAX, &info->rec_buff);
327 if (info->opt_flag & MEMMAP_USED)
328 madvise((char*) share->file_map, share->state.state.data_file_length,
329 MADV_RANDOM);
330 info->opt_flag&= ~(KEY_READ_USED | REMEMBER_OLD_POS);
331 info->quick_mode=0;
332 info->lastinx= 0; /* Use first index as def */
333 info->last_search_keypage= info->lastpos= HA_OFFSET_ERROR;
334 info->page_changed= 1;
335 info->update= ((info->update & HA_STATE_CHANGED) | HA_STATE_NEXT_FOUND |
336 HA_STATE_PREV_FOUND);
337 return(error);
338}
bool reinit_io_cache(cache_type type_arg, my_off_t seek_offset, bool use_async_io, bool clear_cache)
Reset the cache.
int end_io_cache()
Free an io_cache_st object.
int init_io_cache(int file, size_t cachesize, cache_type type, my_off_t seek_offset, bool use_async_io, myf cache_myflags)
Initialize an io_cache_st object.
TODO: Rename this file - func.h is stupid.
STL namespace.