16 #include "myisamdef.h"
17 #ifdef HAVE_SYS_MMAN_H
21 static void mi_extra_keyflag(
MI_INFO *info,
enum ha_extra_function
function);
40 int mi_extra(
MI_INFO *info,
enum ha_extra_function
function,
void *extra_arg)
45 DBUG_ENTER(
"mi_extra");
46 DBUG_PRINT(
"enter",(
"function: %d",(
int)
function));
49 case HA_EXTRA_RESET_STATE:
51 info->last_search_keypage=info->lastpos= HA_OFFSET_ERROR;
54 if (info->opt_flag & READ_CACHE_USED)
56 reinit_io_cache(&info->rec_cache,READ_CACHE,0,
57 (pbool) (info->lock_type != F_UNLCK),
58 (pbool)
test(info->update & HA_STATE_ROW_CHANGED)
61 info->update= ((info->update & HA_STATE_CHANGED) | HA_STATE_NEXT_FOUND |
65 if (info->lock_type == F_UNLCK &&
66 (share->options & HA_OPTION_PACK_RECORD))
72 if (info->s->file_map)
74 #if defined(HAVE_MMAP) && defined(HAVE_MADVISE)
75 if ((share->options & HA_OPTION_COMPRESS_RECORD))
78 if (_mi_memmap_file(info))
81 madvise((
char*) share->file_map, share->state.state.data_file_length,
82 share->state.state.data_file_length <= RECORD_CACHE_SIZE*16 ?
83 MADV_RANDOM : MADV_SEQUENTIAL);
90 if (info->opt_flag & WRITE_CACHE_USED)
92 info->opt_flag&= ~WRITE_CACHE_USED;
93 if ((error=end_io_cache(&info->rec_cache)))
96 if (!(info->opt_flag &
97 (READ_CACHE_USED | WRITE_CACHE_USED | MEMMAP_USED)))
99 cache_size= (extra_arg ? *(ulong*) extra_arg :
100 my_default_record_cache_size);
101 if (!(init_io_cache(&info->rec_cache,info->dfile,
102 (uint) MY_MIN(info->state->data_file_length + 1,
104 READ_CACHE,0L,(pbool) (info->lock_type != F_UNLCK),
105 MYF(share->write_flag & MY_WAIT_IF_FULL))))
107 info->opt_flag|=READ_CACHE_USED;
108 info->update&= ~HA_STATE_ROW_CHANGED;
110 if (share->concurrent_insert)
111 info->rec_cache.end_of_file=info->state->data_file_length;
114 case HA_EXTRA_REINIT_CACHE:
115 if (info->opt_flag & READ_CACHE_USED)
117 reinit_io_cache(&info->rec_cache,READ_CACHE,info->nextpos,
118 (pbool) (info->lock_type != F_UNLCK),
119 (pbool)
test(info->update & HA_STATE_ROW_CHANGED));
120 info->update&= ~HA_STATE_ROW_CHANGED;
121 if (share->concurrent_insert)
122 info->rec_cache.end_of_file=info->state->data_file_length;
125 case HA_EXTRA_WRITE_CACHE:
126 if (info->lock_type == F_UNLCK)
132 cache_size= (extra_arg ? *(ulong*) extra_arg :
133 my_default_record_cache_size);
134 if (!(info->opt_flag &
135 (READ_CACHE_USED | WRITE_CACHE_USED | OPT_NO_ROWS)) &&
136 !share->state.header.uniques)
137 if (!(init_io_cache(&info->rec_cache,info->dfile, cache_size,
138 WRITE_CACHE,info->state->data_file_length,
139 (pbool) (info->lock_type != F_UNLCK),
140 MYF(share->write_flag & MY_WAIT_IF_FULL))))
142 info->opt_flag|=WRITE_CACHE_USED;
143 info->update&= ~(HA_STATE_ROW_CHANGED |
144 HA_STATE_WRITE_AT_END |
145 HA_STATE_EXTEND_BLOCK);
148 case HA_EXTRA_PREPARE_FOR_UPDATE:
149 if (info->s->data_file_type != DYNAMIC_RECORD)
152 case HA_EXTRA_NO_CACHE:
153 if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
155 info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
156 error=end_io_cache(&info->rec_cache);
159 #if defined(HAVE_MMAP) && defined(HAVE_MADVISE)
160 if (info->opt_flag & MEMMAP_USED)
161 madvise((
char*) share->file_map, share->state.state.data_file_length,
165 case HA_EXTRA_FLUSH_CACHE:
166 if (info->opt_flag & WRITE_CACHE_USED)
168 if ((error=flush_io_cache(&info->rec_cache)))
170 mi_print_error(info->s, HA_ERR_CRASHED);
171 mi_mark_crashed(info);
175 case HA_EXTRA_NO_READCHECK:
176 info->opt_flag&= ~READ_CHECK_USED;
178 case HA_EXTRA_READCHECK:
179 info->opt_flag|= READ_CHECK_USED;
181 case HA_EXTRA_KEYREAD:
182 case HA_EXTRA_REMEMBER_POS:
183 info->opt_flag |= REMEMBER_OLD_POS;
184 bmove((uchar*) info->lastkey+share->base.max_key_length*2,
185 (uchar*) info->lastkey,info->lastkey_length);
186 info->save_update= info->update;
187 info->save_lastinx= info->lastinx;
188 info->save_lastpos= info->lastpos;
189 info->save_lastkey_length=info->lastkey_length;
190 if (
function == HA_EXTRA_REMEMBER_POS)
193 case HA_EXTRA_KEYREAD_CHANGE_POS:
194 info->opt_flag |= KEY_READ_USED;
195 info->read_record=_mi_read_key_record;
197 case HA_EXTRA_NO_KEYREAD:
198 case HA_EXTRA_RESTORE_POS:
199 if (info->opt_flag & REMEMBER_OLD_POS)
201 bmove((uchar*) info->lastkey,
202 (uchar*) info->lastkey+share->base.max_key_length*2,
203 info->save_lastkey_length);
204 info->update= info->save_update | HA_STATE_WRITTEN;
205 info->lastinx= info->save_lastinx;
206 info->lastpos= info->save_lastpos;
207 info->lastkey_length=info->save_lastkey_length;
209 info->read_record= share->read_record;
210 info->opt_flag&= ~(KEY_READ_USED | REMEMBER_OLD_POS);
212 case HA_EXTRA_NO_USER_CHANGE:
213 info->lock_type= F_EXTRA_LCK;
215 case HA_EXTRA_WAIT_LOCK:
218 case HA_EXTRA_NO_WAIT_LOCK:
219 info->lock_wait=MY_DONT_WAIT;
221 case HA_EXTRA_NO_KEYS:
222 if (info->lock_type == F_UNLCK)
227 if (mi_is_any_key_active(share->state.key_map))
231 for (i=0 ; i < share->base.keys ; i++,key++)
233 if (!(key->flag & HA_NOSAME) && info->s->base.auto_key != i+1)
235 mi_clear_key_active(share->state.key_map, i);
236 info->update|= HA_STATE_CHANGED;
242 share->state.changed|= STATE_CHANGED | STATE_NOT_ANALYZED;
244 if (!share->global_changed)
246 share->global_changed=1;
247 share->state.open_count++;
250 share->state.state= *info->state;
251 error=mi_state_info_write(share->kfile,&share->state,1 | 2);
254 case HA_EXTRA_FORCE_REOPEN:
256 share->last_version= 0L;
259 case HA_EXTRA_PREPARE_FOR_DROP:
261 share->last_version= 0L;
262 #ifdef __WIN__REMOVE_OBSOLETE_WORKAROUND
265 if (flush_key_blocks(share->key_cache, share->kfile,
266 (
function == HA_EXTRA_FORCE_REOPEN ?
267 FLUSH_RELEASE : FLUSH_IGNORE_CHANGED)))
271 mi_print_error(info->s, HA_ERR_CRASHED);
272 mi_mark_crashed(info);
274 if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
276 info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
277 error=end_io_cache(&info->rec_cache);
279 if (info->lock_type != F_UNLCK && ! info->was_locked)
281 info->was_locked=info->lock_type;
282 if (mi_lock_database(info,F_UNLCK))
284 info->lock_type = F_UNLCK;
286 if (share->kfile >= 0)
287 _mi_decrement_open_count(info);
292 for (list_element=myisam_open_list ;
294 list_element=list_element->next)
297 if (tmpinfo->s == info->s)
311 if (!share->temporary)
312 flush_key_blocks(share->key_cache, share->kfile, FLUSH_KEEP);
314 _mi_decrement_open_count(info);
316 if (share->not_flushed)
318 share->not_flushed=0;
326 mi_print_error(info->s, HA_ERR_CRASHED);
327 mi_mark_crashed(info);
330 if (share->base.blobs)
331 mi_alloc_rec_buff(info, -1, &info->rec_buff);
333 case HA_EXTRA_NORMAL:
339 case HA_EXTRA_NO_ROWS:
340 if (!share->state.header.uniques)
341 info->opt_flag|= OPT_NO_ROWS;
343 case HA_EXTRA_PRELOAD_BUFFER_SIZE:
344 info->preload_buff_size= *((ulong *) extra_arg);
346 case HA_EXTRA_CHANGE_KEY_TO_UNIQUE:
347 case HA_EXTRA_CHANGE_KEY_TO_DUP:
348 mi_extra_keyflag(info,
function);
360 if (!share->file_map)
362 if (mi_dynmap_file(info, share->state.state.data_file_length))
364 DBUG_PRINT(
"warning",(
"mmap failed: errno: %d",errno));
365 error= my_errno= errno;
371 case HA_EXTRA_MARK_AS_LOG_TABLE:
373 share->is_log_table= TRUE;
376 case HA_EXTRA_KEY_CACHE:
377 case HA_EXTRA_NO_KEY_CACHE:
384 myisam_log_command(MI_LOG_EXTRA,info,(uchar*) tmp,1,error);
390 void mi_set_index_cond_func(
MI_INFO *info, index_cond_func_t func,
393 info->index_cond_func= func;
394 info->index_cond_func_arg= func_arg;
400 static void mi_extra_keyflag(
MI_INFO *info,
enum ha_extra_function
function)
404 for (idx= 0; idx< info->s->base.keys; idx++)
407 case HA_EXTRA_CHANGE_KEY_TO_UNIQUE:
408 info->s->keyinfo[idx].flag|= HA_NOSAME;
410 case HA_EXTRA_CHANGE_KEY_TO_DUP:
411 info->s->keyinfo[idx].flag&= ~(HA_NOSAME);
424 DBUG_ENTER(
"mi_reset");
432 if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
434 info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
435 error= end_io_cache(&info->rec_cache);
437 if (share->base.blobs)
438 mi_alloc_rec_buff(info, -1, &info->rec_buff);
439 #if defined(HAVE_MMAP) && defined(HAVE_MADVISE)
440 if (info->opt_flag & MEMMAP_USED)
441 madvise((
char*) share->file_map, share->state.state.data_file_length,
444 info->opt_flag&= ~(KEY_READ_USED | REMEMBER_OLD_POS);
447 info->last_search_keypage= info->lastpos= HA_OFFSET_ERROR;
448 info->page_changed= 1;
449 info->update= ((info->update & HA_STATE_CHANGED) | HA_STATE_NEXT_FOUND |
450 HA_STATE_PREV_FOUND);