12
12
You should have received a copy of the GNU General Public License
13
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 */
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include "myisam_priv.h"
17
#include <drizzled/util/test.h>
18
#include <sys/types.h>
16
#include "myisamdef.h"
17
#ifdef HAVE_SYS_MMAN_H
19
18
#include <sys/mman.h>
24
using namespace drizzled;
27
21
static void mi_extra_keyflag(MI_INFO *info, enum ha_extra_function function);
46
40
int mi_extra(MI_INFO *info, enum ha_extra_function function, void *extra_arg)
50
44
MYISAM_SHARE *share=info->s;
45
DBUG_ENTER("mi_extra");
46
DBUG_PRINT("enter",("function: %d",(int) function));
52
48
switch (function) {
53
49
case HA_EXTRA_RESET_STATE: /* Reset state (don't free buffers) */
57
53
/* Next/prev gives first/last */
58
54
if (info->opt_flag & READ_CACHE_USED)
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));
56
reinit_io_cache(&info->rec_cache,READ_CACHE,0,
57
(bool) (info->lock_type != F_UNLCK),
58
(bool) test(info->update & HA_STATE_ROW_CHANGED)
64
61
info->update= ((info->update & HA_STATE_CHANGED) | HA_STATE_NEXT_FOUND |
65
62
HA_STATE_PREV_FOUND);
69
66
(share->options & HA_OPTION_PACK_RECORD))
71
68
error=1; /* Not possibly if not locked */
75
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);
77
90
if (info->opt_flag & WRITE_CACHE_USED)
79
92
info->opt_flag&= ~WRITE_CACHE_USED;
80
if ((error= info->rec_cache.end_io_cache()))
93
if ((error=end_io_cache(&info->rec_cache)))
83
96
if (!(info->opt_flag &
84
97
(READ_CACHE_USED | WRITE_CACHE_USED | MEMMAP_USED)))
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))))
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,(bool) (info->lock_type != F_UNLCK),
105
MYF(share->write_flag & MY_WAIT_IF_FULL))))
92
107
info->opt_flag|=READ_CACHE_USED;
93
108
info->update&= ~HA_STATE_ROW_CHANGED;
99
114
case HA_EXTRA_REINIT_CACHE:
100
115
if (info->opt_flag & READ_CACHE_USED)
102
info->rec_cache.reinit_io_cache(internal::READ_CACHE,info->nextpos,
117
reinit_io_cache(&info->rec_cache,READ_CACHE,info->nextpos,
103
118
(bool) (info->lock_type != F_UNLCK),
104
119
(bool) test(info->update & HA_STATE_ROW_CHANGED));
105
120
info->update&= ~HA_STATE_ROW_CHANGED;
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)
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))))
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
(bool) (info->lock_type != F_UNLCK),
140
MYF(share->write_flag & MY_WAIT_IF_FULL))))
126
info->opt_flag|=WRITE_CACHE_USED;
127
info->update&= ~(HA_STATE_ROW_CHANGED |
142
info->opt_flag|=WRITE_CACHE_USED;
143
info->update&= ~(HA_STATE_ROW_CHANGED |
128
144
HA_STATE_WRITE_AT_END |
129
145
HA_STATE_EXTEND_BLOCK);
133
148
case HA_EXTRA_PREPARE_FOR_UPDATE:
134
149
if (info->s->data_file_type != DYNAMIC_RECORD)
138
153
if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
140
155
info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
141
error= info->rec_cache.end_io_cache();
156
error=end_io_cache(&info->rec_cache);
142
157
/* Sergei will insert full text index caching here */
144
#if !defined(TARGET_OS_SOLARIS)
159
#if defined(HAVE_MMAP) && defined(HAVE_MADVISE)
145
160
if (info->opt_flag & MEMMAP_USED)
146
161
madvise((char*) share->file_map, share->state.state.data_file_length,
166
181
case HA_EXTRA_KEYREAD: /* Read only keys to record */
167
182
case HA_EXTRA_REMEMBER_POS:
168
183
info->opt_flag |= REMEMBER_OLD_POS;
169
memmove(info->lastkey+share->base.max_key_length*2,
170
info->lastkey,info->lastkey_length);
184
bmove((uchar*) info->lastkey+share->base.max_key_length*2,
185
(uchar*) info->lastkey,info->lastkey_length);
171
186
info->save_update= info->update;
172
187
info->save_lastinx= info->lastinx;
173
188
info->save_lastpos= info->lastpos;
183
198
case HA_EXTRA_RESTORE_POS:
184
199
if (info->opt_flag & REMEMBER_OLD_POS)
186
memmove(info->lastkey,
187
info->lastkey+share->base.max_key_length*2,
188
info->save_lastkey_length);
201
bmove((uchar*) info->lastkey,
202
(uchar*) info->lastkey+share->base.max_key_length*2,
203
info->save_lastkey_length);
189
204
info->update= info->save_update | HA_STATE_WRITTEN;
190
205
info->lastinx= info->save_lastinx;
191
206
info->lastpos= info->save_lastpos;
212
227
if (mi_is_any_key_active(share->state.key_map))
214
229
MI_KEYDEF *key=share->keyinfo;
216
231
for (i=0 ; i < share->base.keys ; i++,key++)
218
233
if (!(key->flag & HA_NOSAME) && info->s->base.auto_key != i+1)
239
254
case HA_EXTRA_FORCE_REOPEN:
240
THR_LOCK_myisam.lock();
255
pthread_mutex_lock(&THR_LOCK_myisam);
241
256
share->last_version= 0L; /* Impossible version */
242
THR_LOCK_myisam.unlock();
257
pthread_mutex_unlock(&THR_LOCK_myisam);
244
259
case HA_EXTRA_PREPARE_FOR_DROP:
245
THR_LOCK_myisam.lock();
260
pthread_mutex_lock(&THR_LOCK_myisam);
246
261
share->last_version= 0L; /* Impossible version */
247
262
#ifdef __WIN__REMOVE_OBSOLETE_WORKAROUND
248
263
/* Close the isam and data files as Win32 can't drop an open table */
264
pthread_mutex_lock(&share->intern_lock);
249
265
if (flush_key_blocks(share->key_cache, share->kfile,
250
266
(function == HA_EXTRA_FORCE_REOPEN ?
251
267
FLUSH_RELEASE : FLUSH_IGNORE_CHANGED)))
254
270
share->changed=1;
255
271
mi_print_error(info->s, HA_ERR_CRASHED);
256
272
mi_mark_crashed(info); /* Fatal error found */
265
281
info->was_locked=info->lock_type;
266
282
if (mi_lock_database(info,F_UNLCK))
268
284
info->lock_type = F_UNLCK;
270
286
if (share->kfile >= 0)
271
287
_mi_decrement_open_count(info);
272
if (share->kfile >= 0 && internal::my_close(share->kfile,MYF(0)))
288
if (share->kfile >= 0 && my_close(share->kfile,MYF(0)))
275
list<MI_INFO *>::iterator it= myisam_open_list.begin();
276
while (it != myisam_open_list.end())
292
for (list_element=myisam_open_list ;
294
list_element=list_element->next)
278
MI_INFO *tmpinfo= *it;
296
MI_INFO *tmpinfo=(MI_INFO*) list_element->data;
279
297
if (tmpinfo->s == info->s)
281
if (tmpinfo->dfile >= 0 && internal::my_close(tmpinfo->dfile,MYF(0)))
299
if (tmpinfo->dfile >= 0 && my_close(tmpinfo->dfile,MYF(0)))
283
301
tmpinfo->dfile= -1;
288
305
share->kfile= -1; /* Files aren't open anymore */
306
pthread_mutex_unlock(&share->intern_lock);
290
THR_LOCK_myisam.unlock();
308
pthread_mutex_unlock(&THR_LOCK_myisam);
292
310
case HA_EXTRA_FLUSH:
293
311
if (!share->temporary)
294
flush_key_blocks(share->getKeyCache(), share->kfile, FLUSH_KEEP);
312
flush_key_blocks(share->key_cache, share->kfile, FLUSH_KEEP);
295
313
#ifdef HAVE_PWRITE
296
314
_mi_decrement_open_count(info);
298
316
if (share->not_flushed)
300
share->not_flushed= false;
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 */
302
330
if (share->base.blobs)
303
mi_alloc_rec_buff(info, SIZE_MAX, &info->rec_buff);
331
mi_alloc_rec_buff(info, -1, &info->rec_buff);
305
333
case HA_EXTRA_NORMAL: /* Theese isn't in use */
306
334
info->quick_mode=0;
313
341
info->opt_flag|= OPT_NO_ROWS;
315
343
case HA_EXTRA_PRELOAD_BUFFER_SIZE:
316
info->preload_buff_size= *((uint32_t *) extra_arg);
344
info->preload_buff_size= *((ulong *) extra_arg);
318
346
case HA_EXTRA_CHANGE_KEY_TO_UNIQUE:
319
347
case HA_EXTRA_CHANGE_KEY_TO_DUP:
320
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);
322
376
case HA_EXTRA_KEY_CACHE:
323
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;
333
398
Start/Stop Inserting Duplicates Into a Table, WL#1648.
335
400
static void mi_extra_keyflag(MI_INFO *info, enum ha_extra_function function)
339
404
for (idx= 0; idx< info->s->base.keys; idx++)
358
423
MYISAM_SHARE *share=info->s;
424
DBUG_ENTER("mi_reset");
360
426
Free buffers and reset the following flags:
361
427
EXTRA_CACHE, EXTRA_WRITE_CACHE, EXTRA_KEYREAD, EXTRA_QUICK
366
432
if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
368
434
info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
369
error= info->rec_cache.end_io_cache();
435
error= end_io_cache(&info->rec_cache);
371
437
if (share->base.blobs)
372
mi_alloc_rec_buff(info, SIZE_MAX, &info->rec_buff);
373
#if !defined(TARGET_OS_SOLARIS)
438
mi_alloc_rec_buff(info, -1, &info->rec_buff);
439
#if defined(HAVE_MMAP) && defined(HAVE_MADVISE)
374
440
if (info->opt_flag & MEMMAP_USED)
375
441
madvise((char*) share->file_map, share->state.state.data_file_length,