1
/* Copyright (C) 2000-2005 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 */
16
#include "myisamdef.h"
17
#ifdef HAVE_SYS_MMAN_H
21
static void mi_extra_keyflag(MI_INFO *info, enum ha_extra_function function);
25
Set options and buffers to optimize table handling
31
extra_arg Pointer to extra argument (normally pointer to ulong)
32
Used when function is one of:
40
int mi_extra(MI_INFO *info, enum ha_extra_function function, void *extra_arg)
44
MYISAM_SHARE *share=info->s;
45
DBUG_ENTER("mi_extra");
46
DBUG_PRINT("enter",("function: %d",(int) function));
49
case HA_EXTRA_RESET_STATE: /* Reset state (don't free buffers) */
50
info->lastinx= 0; /* Use first index as def */
51
info->last_search_keypage=info->lastpos= HA_OFFSET_ERROR;
53
/* Next/prev gives first/last */
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))
68
error=1; /* Not possibly if not locked */
72
if (info->s->file_map) /* Don't use cache if mmap */
74
#if defined(HAVE_MMAP) && defined(HAVE_MADVISE)
75
if ((share->options & HA_OPTION_COMPRESS_RECORD))
77
pthread_mutex_lock(&share->intern_lock);
78
if (_mi_memmap_file(info))
80
/* We don't nead MADV_SEQUENTIAL if small file */
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);
84
pthread_mutex_unlock(&share->intern_lock);
87
pthread_mutex_unlock(&share->intern_lock);
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) 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)
128
error=1; /* Not possibly if not locked */
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)
151
/* Remove read/write cache if dynamic rows */
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);
157
/* Sergei will insert full text index caching here */
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); /* Fatal error found */
175
case HA_EXTRA_NO_READCHECK:
176
info->opt_flag&= ~READ_CHECK_USED; /* No readcheck */
178
case HA_EXTRA_READCHECK:
179
info->opt_flag|= READ_CHECK_USED;
181
case HA_EXTRA_KEYREAD: /* Read only keys to record */
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: /* Database is somehow locked agains changes */
213
info->lock_type= F_EXTRA_LCK; /* Simulate as locked */
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)
224
error=1; /* Not possibly if not lock */
227
if (mi_is_any_key_active(share->state.key_map))
229
MI_KEYDEF *key=share->keyinfo;
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;
243
share->changed=1; /* Update on close */
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:
255
pthread_mutex_lock(&THR_LOCK_myisam);
256
share->last_version= 0L; /* Impossible version */
257
pthread_mutex_unlock(&THR_LOCK_myisam);
259
case HA_EXTRA_PREPARE_FOR_DROP:
260
pthread_mutex_lock(&THR_LOCK_myisam);
261
share->last_version= 0L; /* Impossible version */
262
#ifdef __WIN__REMOVE_OBSOLETE_WORKAROUND
263
/* Close the isam and data files as Win32 can't drop an open table */
264
pthread_mutex_lock(&share->intern_lock);
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); /* Fatal error found */
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);
288
if (share->kfile >= 0 && my_close(share->kfile,MYF(0)))
292
for (list_element=myisam_open_list ;
294
list_element=list_element->next)
296
MI_INFO *tmpinfo=(MI_INFO*) list_element->data;
297
if (tmpinfo->s == info->s)
299
if (tmpinfo->dfile >= 0 && my_close(tmpinfo->dfile,MYF(0)))
305
share->kfile= -1; /* Files aren't open anymore */
306
pthread_mutex_unlock(&share->intern_lock);
308
pthread_mutex_unlock(&THR_LOCK_myisam);
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;
319
if (my_sync(share->kfile, MYF(0)))
321
if (my_sync(info->dfile, MYF(0)))
326
mi_print_error(info->s, HA_ERR_CRASHED);
327
mi_mark_crashed(info); /* Fatal error found */
330
if (share->base.blobs)
331
mi_alloc_rec_buff(info, -1, &info->rec_buff);
333
case HA_EXTRA_NORMAL: /* Theese isn't in use */
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);
352
pthread_mutex_lock(&share->intern_lock);
354
Memory map the data file if it is not already mapped. It is safe
355
to memory map a file while other threads are using file I/O on it.
356
Assigning a new address to a function pointer is an atomic
357
operation. intern_lock prevents that two or more mappings are done
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;
369
share->file_read= mi_mmap_pread;
370
share->file_write= mi_mmap_pwrite;
373
pthread_mutex_unlock(&share->intern_lock);
376
case HA_EXTRA_MARK_AS_LOG_TABLE:
377
pthread_mutex_lock(&share->intern_lock);
378
share->is_log_table= TRUE;
379
pthread_mutex_unlock(&share->intern_lock);
381
case HA_EXTRA_KEY_CACHE:
382
case HA_EXTRA_NO_KEY_CACHE:
389
myisam_log_command(MI_LOG_EXTRA,info,(uchar*) tmp,1,error);
395
void mi_set_index_cond_func(MI_INFO *info, index_cond_func_t func,
398
info->index_cond_func= func;
399
info->index_cond_func_arg= func_arg;
403
Start/Stop Inserting Duplicates Into a Table, WL#1648.
405
static void mi_extra_keyflag(MI_INFO *info, enum ha_extra_function function)
409
for (idx= 0; idx< info->s->base.keys; idx++)
412
case HA_EXTRA_CHANGE_KEY_TO_UNIQUE:
413
info->s->keyinfo[idx].flag|= HA_NOSAME;
415
case HA_EXTRA_CHANGE_KEY_TO_DUP:
416
info->s->keyinfo[idx].flag&= ~(HA_NOSAME);
425
int mi_reset(MI_INFO *info)
428
MYISAM_SHARE *share=info->s;
429
DBUG_ENTER("mi_reset");
431
Free buffers and reset the following flags:
432
EXTRA_CACHE, EXTRA_WRITE_CACHE, EXTRA_KEYREAD, EXTRA_QUICK
434
If the row buffer cache is large (for dynamic tables), reduce it
437
if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
439
info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
440
error= end_io_cache(&info->rec_cache);
442
if (share->base.blobs)
443
mi_alloc_rec_buff(info, -1, &info->rec_buff);
444
#if defined(HAVE_MMAP) && defined(HAVE_MADVISE)
445
if (info->opt_flag & MEMMAP_USED)
446
madvise((char*) share->file_map, share->state.state.data_file_length,
449
info->opt_flag&= ~(KEY_READ_USED | REMEMBER_OLD_POS);
451
info->lastinx= 0; /* Use first index as def */
452
info->last_search_keypage= info->lastpos= HA_OFFSET_ERROR;
453
info->page_changed= 1;
454
info->update= ((info->update & HA_STATE_CHANGED) | HA_STATE_NEXT_FOUND |
455
HA_STATE_PREV_FOUND);