~stewart/drizzle/embedded-innodb-create-select-transaction-arrgh

« back to all changes in this revision

Viewing changes to storage/myisam/mi_extra.c

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include "myisamdef.h"
 
17
#ifdef HAVE_SYS_MMAN_H
 
18
#include <sys/mman.h>
 
19
#endif
 
20
 
 
21
static void mi_extra_keyflag(MI_INFO *info, enum ha_extra_function function);
 
22
 
 
23
 
 
24
/*
 
25
  Set options and buffers to optimize table handling
 
26
 
 
27
  SYNOPSIS
 
28
    mi_extra()
 
29
    info        open table
 
30
    function    operation
 
31
    extra_arg   Pointer to extra argument (normally pointer to ulong)
 
32
                Used when function is one of:
 
33
                HA_EXTRA_WRITE_CACHE
 
34
                HA_EXTRA_CACHE
 
35
  RETURN VALUES
 
36
    0  ok
 
37
    #  error
 
38
*/
 
39
 
 
40
int mi_extra(MI_INFO *info, enum ha_extra_function function, void *extra_arg)
 
41
{
 
42
  int error=0;
 
43
  ulong cache_size;
 
44
  MYISAM_SHARE *share=info->s;
 
45
  DBUG_ENTER("mi_extra");
 
46
  DBUG_PRINT("enter",("function: %d",(int) function));
 
47
 
 
48
  switch (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;
 
52
    info->page_changed=1;
 
53
                                        /* Next/prev gives first/last */
 
54
    if (info->opt_flag & READ_CACHE_USED)
 
55
    {
 
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)
 
59
                      );
 
60
    }
 
61
    info->update= ((info->update & HA_STATE_CHANGED) | HA_STATE_NEXT_FOUND |
 
62
                   HA_STATE_PREV_FOUND);
 
63
    break;
 
64
  case HA_EXTRA_CACHE:
 
65
    if (info->lock_type == F_UNLCK &&
 
66
        (share->options & HA_OPTION_PACK_RECORD))
 
67
    {
 
68
      error=1;                  /* Not possibly if not locked */
 
69
      my_errno=EACCES;
 
70
      break;
 
71
    }
 
72
    if (info->s->file_map) /* Don't use cache if mmap */
 
73
      break;
 
74
#if defined(HAVE_MMAP) && defined(HAVE_MADVISE)
 
75
    if ((share->options & HA_OPTION_COMPRESS_RECORD))
 
76
    {
 
77
      pthread_mutex_lock(&share->intern_lock);
 
78
      if (_mi_memmap_file(info))
 
79
      {
 
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);
 
85
        break;
 
86
      }
 
87
      pthread_mutex_unlock(&share->intern_lock);
 
88
    }
 
89
#endif
 
90
    if (info->opt_flag & WRITE_CACHE_USED)
 
91
    {
 
92
      info->opt_flag&= ~WRITE_CACHE_USED;
 
93
      if ((error=end_io_cache(&info->rec_cache)))
 
94
        break;
 
95
    }
 
96
    if (!(info->opt_flag &
 
97
          (READ_CACHE_USED | WRITE_CACHE_USED | MEMMAP_USED)))
 
98
    {
 
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,
 
103
                                    cache_size),
 
104
                          READ_CACHE,0L,(pbool) (info->lock_type != F_UNLCK),
 
105
                          MYF(share->write_flag & MY_WAIT_IF_FULL))))
 
106
      {
 
107
        info->opt_flag|=READ_CACHE_USED;
 
108
        info->update&= ~HA_STATE_ROW_CHANGED;
 
109
      }
 
110
      if (share->concurrent_insert)
 
111
        info->rec_cache.end_of_file=info->state->data_file_length;
 
112
    }
 
113
    break;
 
114
  case HA_EXTRA_REINIT_CACHE:
 
115
    if (info->opt_flag & READ_CACHE_USED)
 
116
    {
 
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;
 
123
    }
 
124
    break;
 
125
  case HA_EXTRA_WRITE_CACHE:
 
126
    if (info->lock_type == F_UNLCK)
 
127
    {
 
128
      error=1;                  /* Not possibly if not locked */
 
129
      break;
 
130
    }
 
131
 
 
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))))
 
141
      {
 
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);
 
146
      }
 
147
    break;
 
148
  case HA_EXTRA_PREPARE_FOR_UPDATE:
 
149
    if (info->s->data_file_type != DYNAMIC_RECORD)
 
150
      break;
 
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))
 
154
    {
 
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 */
 
158
    }
 
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,
 
162
              MADV_RANDOM);
 
163
#endif
 
164
    break;
 
165
  case HA_EXTRA_FLUSH_CACHE:
 
166
    if (info->opt_flag & WRITE_CACHE_USED)
 
167
    {
 
168
      if ((error=flush_io_cache(&info->rec_cache)))
 
169
      {
 
170
        mi_print_error(info->s, HA_ERR_CRASHED);
 
171
        mi_mark_crashed(info);                  /* Fatal error found */
 
172
      }
 
173
    }
 
174
    break;
 
175
  case HA_EXTRA_NO_READCHECK:
 
176
    info->opt_flag&= ~READ_CHECK_USED;          /* No readcheck */
 
177
    break;
 
178
  case HA_EXTRA_READCHECK:
 
179
    info->opt_flag|= READ_CHECK_USED;
 
180
    break;
 
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)
 
191
      break;
 
192
    /* fall through */
 
193
  case HA_EXTRA_KEYREAD_CHANGE_POS:
 
194
    info->opt_flag |= KEY_READ_USED;
 
195
    info->read_record=_mi_read_key_record;
 
196
    break;
 
197
  case HA_EXTRA_NO_KEYREAD:
 
198
  case HA_EXTRA_RESTORE_POS:
 
199
    if (info->opt_flag & REMEMBER_OLD_POS)
 
200
    {
 
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;
 
208
    }
 
209
    info->read_record=  share->read_record;
 
210
    info->opt_flag&= ~(KEY_READ_USED | REMEMBER_OLD_POS);
 
211
    break;
 
212
  case HA_EXTRA_NO_USER_CHANGE: /* Database is somehow locked agains changes */
 
213
    info->lock_type= F_EXTRA_LCK; /* Simulate as locked */
 
214
    break;
 
215
  case HA_EXTRA_WAIT_LOCK:
 
216
    info->lock_wait=0;
 
217
    break;
 
218
  case HA_EXTRA_NO_WAIT_LOCK:
 
219
    info->lock_wait=MY_DONT_WAIT;
 
220
    break;
 
221
  case HA_EXTRA_NO_KEYS:
 
222
    if (info->lock_type == F_UNLCK)
 
223
    {
 
224
      error=1;                                  /* Not possibly if not lock */
 
225
      break;
 
226
    }
 
227
    if (mi_is_any_key_active(share->state.key_map))
 
228
    {
 
229
      MI_KEYDEF *key=share->keyinfo;
 
230
      uint i;
 
231
      for (i=0 ; i < share->base.keys ; i++,key++)
 
232
      {
 
233
        if (!(key->flag & HA_NOSAME) && info->s->base.auto_key != i+1)
 
234
        {
 
235
          mi_clear_key_active(share->state.key_map, i);
 
236
          info->update|= HA_STATE_CHANGED;
 
237
        }
 
238
      }
 
239
 
 
240
      if (!share->changed)
 
241
      {
 
242
        share->state.changed|= STATE_CHANGED | STATE_NOT_ANALYZED;
 
243
        share->changed=1;                       /* Update on close */
 
244
        if (!share->global_changed)
 
245
        {
 
246
          share->global_changed=1;
 
247
          share->state.open_count++;
 
248
        }
 
249
      }
 
250
      share->state.state= *info->state;
 
251
      error=mi_state_info_write(share->kfile,&share->state,1 | 2);
 
252
    }
 
253
    break;
 
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);
 
258
    break;
 
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)))
 
268
    {
 
269
      error=my_errno;
 
270
      share->changed=1;
 
271
      mi_print_error(info->s, HA_ERR_CRASHED);
 
272
      mi_mark_crashed(info);                    /* Fatal error found */
 
273
    }
 
274
    if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
 
275
    {
 
276
      info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
 
277
      error=end_io_cache(&info->rec_cache);
 
278
    }
 
279
    if (info->lock_type != F_UNLCK && ! info->was_locked)
 
280
    {
 
281
      info->was_locked=info->lock_type;
 
282
      if (mi_lock_database(info,F_UNLCK))
 
283
        error=my_errno;
 
284
      info->lock_type = F_UNLCK;
 
285
    }
 
286
    if (share->kfile >= 0)
 
287
      _mi_decrement_open_count(info);
 
288
    if (share->kfile >= 0 && my_close(share->kfile,MYF(0)))
 
289
      error=my_errno;
 
290
    {
 
291
      LIST *list_element ;
 
292
      for (list_element=myisam_open_list ;
 
293
           list_element ;
 
294
           list_element=list_element->next)
 
295
      {
 
296
        MI_INFO *tmpinfo=(MI_INFO*) list_element->data;
 
297
        if (tmpinfo->s == info->s)
 
298
        {
 
299
          if (tmpinfo->dfile >= 0 && my_close(tmpinfo->dfile,MYF(0)))
 
300
            error = my_errno;
 
301
          tmpinfo->dfile= -1;
 
302
        }
 
303
      }
 
304
    }
 
305
    share->kfile= -1;                           /* Files aren't open anymore */
 
306
    pthread_mutex_unlock(&share->intern_lock);
 
307
#endif
 
308
    pthread_mutex_unlock(&THR_LOCK_myisam);
 
309
    break;
 
310
  case HA_EXTRA_FLUSH:
 
311
    if (!share->temporary)
 
312
      flush_key_blocks(share->key_cache, share->kfile, FLUSH_KEEP);
 
313
#ifdef HAVE_PWRITE
 
314
    _mi_decrement_open_count(info);
 
315
#endif
 
316
    if (share->not_flushed)
 
317
    {
 
318
      share->not_flushed=0;
 
319
      if (my_sync(share->kfile, MYF(0)))
 
320
        error= my_errno;
 
321
      if (my_sync(info->dfile, MYF(0)))
 
322
        error= my_errno;
 
323
      if (error)
 
324
      {
 
325
        share->changed=1;
 
326
        mi_print_error(info->s, HA_ERR_CRASHED);
 
327
        mi_mark_crashed(info);                  /* Fatal error found */
 
328
      }
 
329
    }
 
330
    if (share->base.blobs)
 
331
      mi_alloc_rec_buff(info, -1, &info->rec_buff);
 
332
    break;
 
333
  case HA_EXTRA_NORMAL:                         /* Theese isn't in use */
 
334
    info->quick_mode=0;
 
335
    break;
 
336
  case HA_EXTRA_QUICK:
 
337
    info->quick_mode=1;
 
338
    break;
 
339
  case HA_EXTRA_NO_ROWS:
 
340
    if (!share->state.header.uniques)
 
341
      info->opt_flag|= OPT_NO_ROWS;
 
342
    break;
 
343
  case HA_EXTRA_PRELOAD_BUFFER_SIZE:
 
344
    info->preload_buff_size= *((ulong *) extra_arg); 
 
345
    break;
 
346
  case HA_EXTRA_CHANGE_KEY_TO_UNIQUE:
 
347
  case HA_EXTRA_CHANGE_KEY_TO_DUP:
 
348
    mi_extra_keyflag(info, function);
 
349
    break;
 
350
  case HA_EXTRA_MMAP:
 
351
#ifdef HAVE_MMAP
 
352
    pthread_mutex_lock(&share->intern_lock);
 
353
    /*
 
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
 
358
      at the same time.
 
359
    */
 
360
    if (!share->file_map)
 
361
    {
 
362
      if (mi_dynmap_file(info, share->state.state.data_file_length))
 
363
      {
 
364
        DBUG_PRINT("warning",("mmap failed: errno: %d",errno));
 
365
        error= my_errno= errno;
 
366
      }
 
367
      else
 
368
      {
 
369
        share->file_read= mi_mmap_pread;
 
370
        share->file_write= mi_mmap_pwrite;
 
371
      }
 
372
    }
 
373
    pthread_mutex_unlock(&share->intern_lock);
 
374
#endif
 
375
    break;
 
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);
 
380
    break;
 
381
  case HA_EXTRA_KEY_CACHE:
 
382
  case HA_EXTRA_NO_KEY_CACHE:
 
383
  default:
 
384
    break;
 
385
  }
 
386
  {
 
387
    char tmp[1];
 
388
    tmp[0]=function;
 
389
    myisam_log_command(MI_LOG_EXTRA,info,(uchar*) tmp,1,error);
 
390
  }
 
391
  DBUG_RETURN(error);
 
392
} /* mi_extra */
 
393
 
 
394
 
 
395
void mi_set_index_cond_func(MI_INFO *info, index_cond_func_t func,
 
396
                            void *func_arg)
 
397
{
 
398
  info->index_cond_func= func;
 
399
  info->index_cond_func_arg= func_arg;
 
400
}
 
401
 
 
402
/*
 
403
    Start/Stop Inserting Duplicates Into a Table, WL#1648.
 
404
 */
 
405
static void mi_extra_keyflag(MI_INFO *info, enum ha_extra_function function)
 
406
{
 
407
  uint  idx;
 
408
 
 
409
  for (idx= 0; idx< info->s->base.keys; idx++)
 
410
  {
 
411
    switch (function) {
 
412
    case HA_EXTRA_CHANGE_KEY_TO_UNIQUE:
 
413
      info->s->keyinfo[idx].flag|= HA_NOSAME;
 
414
      break;
 
415
    case HA_EXTRA_CHANGE_KEY_TO_DUP:
 
416
      info->s->keyinfo[idx].flag&= ~(HA_NOSAME);
 
417
      break;
 
418
    default:
 
419
      break;
 
420
    }
 
421
  }
 
422
}
 
423
 
 
424
 
 
425
int mi_reset(MI_INFO *info)
 
426
{
 
427
  int error= 0;
 
428
  MYISAM_SHARE *share=info->s;
 
429
  DBUG_ENTER("mi_reset");
 
430
  /*
 
431
    Free buffers and reset the following flags:
 
432
    EXTRA_CACHE, EXTRA_WRITE_CACHE, EXTRA_KEYREAD, EXTRA_QUICK
 
433
 
 
434
    If the row buffer cache is large (for dynamic tables), reduce it
 
435
    to save memory.
 
436
  */
 
437
  if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
 
438
  {
 
439
    info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
 
440
    error= end_io_cache(&info->rec_cache);
 
441
  }
 
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,
 
447
            MADV_RANDOM);
 
448
#endif
 
449
  info->opt_flag&= ~(KEY_READ_USED | REMEMBER_OLD_POS);
 
450
  info->quick_mode=0;
 
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);
 
456
  DBUG_RETURN(error);
 
457
}