~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/myisam/mi_extra.c

  • Committer: Brian Aker
  • Date: 2008-07-14 22:07:42 UTC
  • Revision ID: brian@tangent.org-20080714220742-y7fjh1mitrfcgfij
Second pass cleanup on removal of my_uint types

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
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 */
15
15
 
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>
20
 
 
21
 
#include <string.h>
22
 
#include <algorithm>
23
 
 
24
 
using namespace drizzled;
25
 
using namespace std;
 
19
#endif
26
20
 
27
21
static void mi_extra_keyflag(MI_INFO *info, enum ha_extra_function function);
28
22
 
34
28
    mi_extra()
35
29
    info        open table
36
30
    function    operation
37
 
    extra_arg   Pointer to extra argument (normally pointer to uint32_t)
 
31
    extra_arg   Pointer to extra argument (normally pointer to ulong)
38
32
                Used when function is one of:
39
33
                HA_EXTRA_WRITE_CACHE
40
34
                HA_EXTRA_CACHE
46
40
int mi_extra(MI_INFO *info, enum ha_extra_function function, void *extra_arg)
47
41
{
48
42
  int error=0;
49
 
  uint32_t cache_size;
 
43
  ulong cache_size;
50
44
  MYISAM_SHARE *share=info->s;
 
45
  DBUG_ENTER("mi_extra");
 
46
  DBUG_PRINT("enter",("function: %d",(int) function));
51
47
 
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)
59
55
    {
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)
 
59
                      );
63
60
    }
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))
70
67
    {
71
68
      error=1;                  /* Not possibly if not locked */
72
 
      errno=EACCES;
 
69
      my_errno=EACCES;
73
70
      break;
74
71
    }
75
72
    if (info->s->file_map) /* Don't use cache if mmap */
76
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
77
90
    if (info->opt_flag & WRITE_CACHE_USED)
78
91
    {
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)))
81
94
        break;
82
95
    }
83
96
    if (!(info->opt_flag &
84
97
          (READ_CACHE_USED | WRITE_CACHE_USED | MEMMAP_USED)))
85
98
    {
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,
 
103
                                    cache_size),
 
104
                          READ_CACHE,0L,(bool) (info->lock_type != F_UNLCK),
 
105
                          MYF(share->write_flag & MY_WAIT_IF_FULL))))
91
106
      {
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)
101
116
    {
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;
114
129
      break;
115
130
    }
116
131
 
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))))
 
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))))
125
141
      {
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);
130
146
      }
131
 
    }
132
147
    break;
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))
139
154
    {
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 */
143
158
    }
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,
147
162
              MADV_RANDOM);
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)
185
200
    {
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))
213
228
    {
214
229
      MI_KEYDEF *key=share->keyinfo;
215
 
      uint32_t i;
 
230
      uint i;
216
231
      for (i=0 ; i < share->base.keys ; i++,key++)
217
232
      {
218
233
        if (!(key->flag & HA_NOSAME) && info->s->base.auto_key != i+1)
237
252
    }
238
253
    break;
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);
243
258
    break;
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)))
252
268
    {
253
 
      error=errno;
 
269
      error=my_errno;
254
270
      share->changed=1;
255
271
      mi_print_error(info->s, HA_ERR_CRASHED);
256
272
      mi_mark_crashed(info);                    /* Fatal error found */
264
280
    {
265
281
      info->was_locked=info->lock_type;
266
282
      if (mi_lock_database(info,F_UNLCK))
267
 
        error=errno;
 
283
        error=my_errno;
268
284
      info->lock_type = F_UNLCK;
269
285
    }
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)))
273
 
      error=errno;
 
288
    if (share->kfile >= 0 && my_close(share->kfile,MYF(0)))
 
289
      error=my_errno;
274
290
    {
275
 
      list<MI_INFO *>::iterator it= myisam_open_list.begin();
276
 
      while (it != myisam_open_list.end())
 
291
      LIST *list_element ;
 
292
      for (list_element=myisam_open_list ;
 
293
           list_element ;
 
294
           list_element=list_element->next)
277
295
      {
278
 
        MI_INFO *tmpinfo= *it;
 
296
        MI_INFO *tmpinfo=(MI_INFO*) list_element->data;
279
297
        if (tmpinfo->s == info->s)
280
298
        {
281
 
          if (tmpinfo->dfile >= 0 && internal::my_close(tmpinfo->dfile,MYF(0)))
282
 
            error = errno;
 
299
          if (tmpinfo->dfile >= 0 && my_close(tmpinfo->dfile,MYF(0)))
 
300
            error = my_errno;
283
301
          tmpinfo->dfile= -1;
284
302
        }
285
 
        ++it;
286
303
      }
287
304
    }
288
305
    share->kfile= -1;                           /* Files aren't open anymore */
 
306
    pthread_mutex_unlock(&share->intern_lock);
289
307
#endif
290
 
    THR_LOCK_myisam.unlock();
 
308
    pthread_mutex_unlock(&THR_LOCK_myisam);
291
309
    break;
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);
297
315
#endif
298
316
    if (share->not_flushed)
299
317
    {
300
 
      share->not_flushed= false;
 
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
      }
301
329
    }
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);
304
332
    break;
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;
314
342
    break;
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); 
317
345
    break;
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);
321
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;
322
376
  case HA_EXTRA_KEY_CACHE:
323
377
  case HA_EXTRA_NO_KEY_CACHE:
324
378
  default:
325
379
    break;
326
380
  }
327
 
 
328
 
  return(error);
 
381
  {
 
382
    char tmp[1];
 
383
    tmp[0]=function;
 
384
    myisam_log_command(MI_LOG_EXTRA,info,(uchar*) tmp,1,error);
 
385
  }
 
386
  DBUG_RETURN(error);
329
387
} /* mi_extra */
330
388
 
331
389
 
 
390
void mi_set_index_cond_func(MI_INFO *info, index_cond_func_t func,
 
391
                            void *func_arg)
 
392
{
 
393
  info->index_cond_func= func;
 
394
  info->index_cond_func_arg= func_arg;
 
395
}
 
396
 
332
397
/*
333
398
    Start/Stop Inserting Duplicates Into a Table, WL#1648.
334
399
 */
335
400
static void mi_extra_keyflag(MI_INFO *info, enum ha_extra_function function)
336
401
{
337
 
  uint32_t  idx;
 
402
  uint  idx;
338
403
 
339
404
  for (idx= 0; idx< info->s->base.keys; idx++)
340
405
  {
356
421
{
357
422
  int error= 0;
358
423
  MYISAM_SHARE *share=info->s;
 
424
  DBUG_ENTER("mi_reset");
359
425
  /*
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))
367
433
  {
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);
370
436
  }
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,
376
442
            MADV_RANDOM);
382
448
  info->page_changed= 1;
383
449
  info->update= ((info->update & HA_STATE_CHANGED) | HA_STATE_NEXT_FOUND |
384
450
                 HA_STATE_PREV_FOUND);
385
 
  return(error);
 
451
  DBUG_RETURN(error);
386
452
}