~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/myisam/mi_extra.c

  • Committer: Brian Aker
  • Date: 2008-08-22 19:59:34 UTC
  • Revision ID: brian@tangent.org-20080822195934-c2krkt6759jct6mf
Removed dead bits.

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
 
57
51
                                        /* Next/prev gives first/last */
58
52
    if (info->opt_flag & READ_CACHE_USED)
59
53
    {
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));
 
54
      reinit_io_cache(&info->rec_cache,READ_CACHE,0,
 
55
                      (bool) (info->lock_type != F_UNLCK),
 
56
                      (bool) test(info->update & HA_STATE_ROW_CHANGED)
 
57
                      );
63
58
    }
64
59
    info->update= ((info->update & HA_STATE_CHANGED) | HA_STATE_NEXT_FOUND |
65
60
                   HA_STATE_PREV_FOUND);
69
64
        (share->options & HA_OPTION_PACK_RECORD))
70
65
    {
71
66
      error=1;                  /* Not possibly if not locked */
72
 
      errno=EACCES;
 
67
      my_errno=EACCES;
73
68
      break;
74
69
    }
75
70
    if (info->s->file_map) /* Don't use cache if mmap */
77
72
    if (info->opt_flag & WRITE_CACHE_USED)
78
73
    {
79
74
      info->opt_flag&= ~WRITE_CACHE_USED;
80
 
      if ((error= info->rec_cache.end_io_cache()))
 
75
      if ((error=end_io_cache(&info->rec_cache)))
81
76
        break;
82
77
    }
83
78
    if (!(info->opt_flag &
84
79
          (READ_CACHE_USED | WRITE_CACHE_USED | MEMMAP_USED)))
85
80
    {
86
81
      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))))
 
82
                   my_default_record_cache_size);
 
83
      if (!(init_io_cache(&info->rec_cache,info->dfile,
 
84
                         (uint) min(info->state->data_file_length+1,
 
85
                                    cache_size),
 
86
                          READ_CACHE,0L,(bool) (info->lock_type != F_UNLCK),
 
87
                          MYF(share->write_flag & MY_WAIT_IF_FULL))))
91
88
      {
92
89
        info->opt_flag|=READ_CACHE_USED;
93
90
        info->update&= ~HA_STATE_ROW_CHANGED;
99
96
  case HA_EXTRA_REINIT_CACHE:
100
97
    if (info->opt_flag & READ_CACHE_USED)
101
98
    {
102
 
      info->rec_cache.reinit_io_cache(internal::READ_CACHE,info->nextpos,
 
99
      reinit_io_cache(&info->rec_cache,READ_CACHE,info->nextpos,
103
100
                      (bool) (info->lock_type != F_UNLCK),
104
101
                      (bool) test(info->update & HA_STATE_ROW_CHANGED));
105
102
      info->update&= ~HA_STATE_ROW_CHANGED;
115
112
    }
116
113
 
117
114
    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))))
 
115
                 my_default_record_cache_size);
 
116
    if (!(info->opt_flag &
 
117
          (READ_CACHE_USED | WRITE_CACHE_USED | OPT_NO_ROWS)) &&
 
118
        !share->state.header.uniques)
 
119
      if (!(init_io_cache(&info->rec_cache,info->dfile, cache_size,
 
120
                         WRITE_CACHE,info->state->data_file_length,
 
121
                          (bool) (info->lock_type != F_UNLCK),
 
122
                          MYF(share->write_flag & MY_WAIT_IF_FULL))))
125
123
      {
126
 
        info->opt_flag|=WRITE_CACHE_USED;
127
 
        info->update&= ~(HA_STATE_ROW_CHANGED |
 
124
        info->opt_flag|=WRITE_CACHE_USED;
 
125
        info->update&= ~(HA_STATE_ROW_CHANGED |
128
126
                         HA_STATE_WRITE_AT_END |
129
127
                         HA_STATE_EXTEND_BLOCK);
130
128
      }
131
 
    }
132
129
    break;
133
130
  case HA_EXTRA_PREPARE_FOR_UPDATE:
134
131
    if (info->s->data_file_type != DYNAMIC_RECORD)
138
135
    if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
139
136
    {
140
137
      info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
141
 
      error= info->rec_cache.end_io_cache();
 
138
      error=end_io_cache(&info->rec_cache);
142
139
      /* Sergei will insert full text index caching here */
143
140
    }
144
 
#if !defined(TARGET_OS_SOLARIS)
 
141
#if defined(HAVE_MMAP) && defined(HAVE_MADVISE)
145
142
    if (info->opt_flag & MEMMAP_USED)
146
143
      madvise((char*) share->file_map, share->state.state.data_file_length,
147
144
              MADV_RANDOM);
166
163
  case HA_EXTRA_KEYREAD:                        /* Read only keys to record */
167
164
  case HA_EXTRA_REMEMBER_POS:
168
165
    info->opt_flag |= REMEMBER_OLD_POS;
169
 
    memmove(info->lastkey+share->base.max_key_length*2,
170
 
            info->lastkey,info->lastkey_length);
 
166
    memcpy(info->lastkey+share->base.max_key_length*2,
 
167
           info->lastkey,info->lastkey_length);
171
168
    info->save_update=  info->update;
172
169
    info->save_lastinx= info->lastinx;
173
170
    info->save_lastpos= info->lastpos;
183
180
  case HA_EXTRA_RESTORE_POS:
184
181
    if (info->opt_flag & REMEMBER_OLD_POS)
185
182
    {
186
 
      memmove(info->lastkey,
187
 
              info->lastkey+share->base.max_key_length*2,
188
 
              info->save_lastkey_length);
 
183
      memcpy(info->lastkey,
 
184
             info->lastkey+share->base.max_key_length*2,
 
185
             info->save_lastkey_length);
189
186
      info->update=     info->save_update | HA_STATE_WRITTEN;
190
187
      info->lastinx=    info->save_lastinx;
191
188
      info->lastpos=    info->save_lastpos;
212
209
    if (mi_is_any_key_active(share->state.key_map))
213
210
    {
214
211
      MI_KEYDEF *key=share->keyinfo;
215
 
      uint32_t i;
 
212
      uint i;
216
213
      for (i=0 ; i < share->base.keys ; i++,key++)
217
214
      {
218
215
        if (!(key->flag & HA_NOSAME) && info->s->base.auto_key != i+1)
237
234
    }
238
235
    break;
239
236
  case HA_EXTRA_FORCE_REOPEN:
240
 
    THR_LOCK_myisam.lock();
 
237
    pthread_mutex_lock(&THR_LOCK_myisam);
241
238
    share->last_version= 0L;                    /* Impossible version */
242
 
    THR_LOCK_myisam.unlock();
 
239
    pthread_mutex_unlock(&THR_LOCK_myisam);
243
240
    break;
244
241
  case HA_EXTRA_PREPARE_FOR_DROP:
245
 
    THR_LOCK_myisam.lock();
 
242
    pthread_mutex_lock(&THR_LOCK_myisam);
246
243
    share->last_version= 0L;                    /* Impossible version */
247
244
#ifdef __WIN__REMOVE_OBSOLETE_WORKAROUND
248
245
    /* Close the isam and data files as Win32 can't drop an open table */
 
246
    pthread_mutex_lock(&share->intern_lock);
249
247
    if (flush_key_blocks(share->key_cache, share->kfile,
250
248
                         (function == HA_EXTRA_FORCE_REOPEN ?
251
249
                          FLUSH_RELEASE : FLUSH_IGNORE_CHANGED)))
252
250
    {
253
 
      error=errno;
 
251
      error=my_errno;
254
252
      share->changed=1;
255
253
      mi_print_error(info->s, HA_ERR_CRASHED);
256
254
      mi_mark_crashed(info);                    /* Fatal error found */
264
262
    {
265
263
      info->was_locked=info->lock_type;
266
264
      if (mi_lock_database(info,F_UNLCK))
267
 
        error=errno;
 
265
        error=my_errno;
268
266
      info->lock_type = F_UNLCK;
269
267
    }
270
268
    if (share->kfile >= 0)
271
269
      _mi_decrement_open_count(info);
272
 
    if (share->kfile >= 0 && internal::my_close(share->kfile,MYF(0)))
273
 
      error=errno;
 
270
    if (share->kfile >= 0 && my_close(share->kfile,MYF(0)))
 
271
      error=my_errno;
274
272
    {
275
 
      list<MI_INFO *>::iterator it= myisam_open_list.begin();
276
 
      while (it != myisam_open_list.end())
 
273
      LIST *list_element ;
 
274
      for (list_element=myisam_open_list ;
 
275
           list_element ;
 
276
           list_element=list_element->next)
277
277
      {
278
 
        MI_INFO *tmpinfo= *it;
 
278
        MI_INFO *tmpinfo=(MI_INFO*) list_element->data;
279
279
        if (tmpinfo->s == info->s)
280
280
        {
281
 
          if (tmpinfo->dfile >= 0 && internal::my_close(tmpinfo->dfile,MYF(0)))
282
 
            error = errno;
 
281
          if (tmpinfo->dfile >= 0 && my_close(tmpinfo->dfile,MYF(0)))
 
282
            error = my_errno;
283
283
          tmpinfo->dfile= -1;
284
284
        }
285
 
        ++it;
286
285
      }
287
286
    }
288
287
    share->kfile= -1;                           /* Files aren't open anymore */
 
288
    pthread_mutex_unlock(&share->intern_lock);
289
289
#endif
290
 
    THR_LOCK_myisam.unlock();
 
290
    pthread_mutex_unlock(&THR_LOCK_myisam);
291
291
    break;
292
292
  case HA_EXTRA_FLUSH:
293
293
    if (!share->temporary)
294
 
      flush_key_blocks(share->getKeyCache(), share->kfile, FLUSH_KEEP);
 
294
      flush_key_blocks(share->key_cache, share->kfile, FLUSH_KEEP);
295
295
#ifdef HAVE_PWRITE
296
296
    _mi_decrement_open_count(info);
297
297
#endif
298
298
    if (share->not_flushed)
299
299
    {
300
 
      share->not_flushed= false;
 
300
      share->not_flushed=0;
 
301
      if (my_sync(share->kfile, MYF(0)))
 
302
        error= my_errno;
 
303
      if (my_sync(info->dfile, MYF(0)))
 
304
        error= my_errno;
 
305
      if (error)
 
306
      {
 
307
        share->changed=1;
 
308
        mi_print_error(info->s, HA_ERR_CRASHED);
 
309
        mi_mark_crashed(info);                  /* Fatal error found */
 
310
      }
301
311
    }
302
312
    if (share->base.blobs)
303
 
      mi_alloc_rec_buff(info, SIZE_MAX, &info->rec_buff);
 
313
      mi_alloc_rec_buff(info, -1, &info->rec_buff);
304
314
    break;
305
315
  case HA_EXTRA_NORMAL:                         /* Theese isn't in use */
306
316
    info->quick_mode=0;
313
323
      info->opt_flag|= OPT_NO_ROWS;
314
324
    break;
315
325
  case HA_EXTRA_PRELOAD_BUFFER_SIZE:
316
 
    info->preload_buff_size= *((uint32_t *) extra_arg);
 
326
    info->preload_buff_size= *((uint32_t *) extra_arg); 
317
327
    break;
318
328
  case HA_EXTRA_CHANGE_KEY_TO_UNIQUE:
319
329
  case HA_EXTRA_CHANGE_KEY_TO_DUP:
320
330
    mi_extra_keyflag(info, function);
321
331
    break;
 
332
  case HA_EXTRA_MMAP:
 
333
#ifdef HAVE_MMAP
 
334
    pthread_mutex_lock(&share->intern_lock);
 
335
    /*
 
336
      Memory map the data file if it is not already mapped. It is safe
 
337
      to memory map a file while other threads are using file I/O on it.
 
338
      Assigning a new address to a function pointer is an atomic
 
339
      operation. intern_lock prevents that two or more mappings are done
 
340
      at the same time.
 
341
    */
 
342
    if (!share->file_map)
 
343
    {
 
344
      if (mi_dynmap_file(info, share->state.state.data_file_length))
 
345
      {
 
346
        error= my_errno= errno;
 
347
      }
 
348
      else
 
349
      {
 
350
        share->file_read= mi_mmap_pread;
 
351
        share->file_write= mi_mmap_pwrite;
 
352
      }
 
353
    }
 
354
    pthread_mutex_unlock(&share->intern_lock);
 
355
#endif
 
356
    break;
322
357
  case HA_EXTRA_KEY_CACHE:
323
358
  case HA_EXTRA_NO_KEY_CACHE:
324
359
  default:
329
364
} /* mi_extra */
330
365
 
331
366
 
 
367
void mi_set_index_cond_func(MI_INFO *info, index_cond_func_t func,
 
368
                            void *func_arg)
 
369
{
 
370
  info->index_cond_func= func;
 
371
  info->index_cond_func_arg= func_arg;
 
372
}
 
373
 
332
374
/*
333
375
    Start/Stop Inserting Duplicates Into a Table, WL#1648.
334
376
 */
335
377
static void mi_extra_keyflag(MI_INFO *info, enum ha_extra_function function)
336
378
{
337
 
  uint32_t  idx;
 
379
  uint  idx;
338
380
 
339
381
  for (idx= 0; idx< info->s->base.keys; idx++)
340
382
  {
366
408
  if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
367
409
  {
368
410
    info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
369
 
    error= info->rec_cache.end_io_cache();
 
411
    error= end_io_cache(&info->rec_cache);
370
412
  }
371
413
  if (share->base.blobs)
372
 
    mi_alloc_rec_buff(info, SIZE_MAX, &info->rec_buff);
373
 
#if !defined(TARGET_OS_SOLARIS)
 
414
    mi_alloc_rec_buff(info, -1, &info->rec_buff);
 
415
#if defined(HAVE_MMAP) && defined(HAVE_MADVISE)
374
416
  if (info->opt_flag & MEMMAP_USED)
375
417
    madvise((char*) share->file_map, share->state.state.data_file_length,
376
418
            MADV_RANDOM);