Drizzled Public API Documentation

mi_extra.cc

00001 /* Copyright (C) 2000-2005 MySQL AB
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00016 #include "myisam_priv.h"
00017 #include <drizzled/util/test.h>
00018 #include <sys/types.h>
00019 #include <sys/mman.h>
00020 
00021 #include <string.h>
00022 #include <algorithm>
00023 
00024 using namespace drizzled;
00025 using namespace std;
00026 
00027 static void mi_extra_keyflag(MI_INFO *info, enum ha_extra_function function);
00028 
00029 
00030 /*
00031   Set options and buffers to optimize table handling
00032 
00033   SYNOPSIS
00034     mi_extra()
00035     info  open table
00036     function  operation
00037     extra_arg Pointer to extra argument (normally pointer to uint32_t)
00038         Used when function is one of:
00039     HA_EXTRA_WRITE_CACHE
00040     HA_EXTRA_CACHE
00041   RETURN VALUES
00042     0  ok
00043     #  error
00044 */
00045 
00046 int mi_extra(MI_INFO *info, enum ha_extra_function function, void *extra_arg)
00047 {
00048   int error=0;
00049   uint32_t cache_size;
00050   MYISAM_SHARE *share=info->s;
00051 
00052   switch (function) {
00053   case HA_EXTRA_RESET_STATE:    /* Reset state (don't free buffers) */
00054     info->lastinx= 0;     /* Use first index as def */
00055     info->last_search_keypage=info->lastpos= HA_OFFSET_ERROR;
00056     info->page_changed=1;
00057           /* Next/prev gives first/last */
00058     if (info->opt_flag & READ_CACHE_USED)
00059     {
00060       info->rec_cache.reinit_io_cache(internal::READ_CACHE,0,
00061                                       (bool) (info->lock_type != F_UNLCK),
00062                                       (bool) test(info->update & HA_STATE_ROW_CHANGED));
00063     }
00064     info->update= ((info->update & HA_STATE_CHANGED) | HA_STATE_NEXT_FOUND |
00065        HA_STATE_PREV_FOUND);
00066     break;
00067   case HA_EXTRA_CACHE:
00068     if (info->lock_type == F_UNLCK &&
00069   (share->options & HA_OPTION_PACK_RECORD))
00070     {
00071       error=1;      /* Not possibly if not locked */
00072       errno=EACCES;
00073       break;
00074     }
00075     if (info->s->file_map) /* Don't use cache if mmap */
00076       break;
00077     if (info->opt_flag & WRITE_CACHE_USED)
00078     {
00079       info->opt_flag&= ~WRITE_CACHE_USED;
00080       if ((error= info->rec_cache.end_io_cache()))
00081   break;
00082     }
00083     if (!(info->opt_flag &
00084     (READ_CACHE_USED | WRITE_CACHE_USED | MEMMAP_USED)))
00085     {
00086       cache_size= (extra_arg ? *(uint32_t*) extra_arg :
00087        internal::my_default_record_cache_size);
00088       if (!(info->rec_cache.init_io_cache(info->dfile, (uint) min((uint32_t)info->state->data_file_length+1, cache_size),
00089                                           internal::READ_CACHE,0L,(bool) (info->lock_type != F_UNLCK),
00090                                           MYF(share->write_flag & MY_WAIT_IF_FULL))))
00091       {
00092   info->opt_flag|=READ_CACHE_USED;
00093   info->update&= ~HA_STATE_ROW_CHANGED;
00094       }
00095       if (share->concurrent_insert)
00096   info->rec_cache.end_of_file=info->state->data_file_length;
00097     }
00098     break;
00099   case HA_EXTRA_REINIT_CACHE:
00100     if (info->opt_flag & READ_CACHE_USED)
00101     {
00102       info->rec_cache.reinit_io_cache(internal::READ_CACHE,info->nextpos,
00103           (bool) (info->lock_type != F_UNLCK),
00104           (bool) test(info->update & HA_STATE_ROW_CHANGED));
00105       info->update&= ~HA_STATE_ROW_CHANGED;
00106       if (share->concurrent_insert)
00107   info->rec_cache.end_of_file=info->state->data_file_length;
00108     }
00109     break;
00110   case HA_EXTRA_WRITE_CACHE:
00111     if (info->lock_type == F_UNLCK)
00112     {
00113       error=1;      /* Not possibly if not locked */
00114       break;
00115     }
00116 
00117     cache_size= (extra_arg ? *(uint32_t*) extra_arg :
00118      internal::my_default_record_cache_size);
00119     if (not (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED | OPT_NO_ROWS)) && !share->state.header.uniques)
00120     {
00121       if (not (info->rec_cache.init_io_cache(info->dfile, cache_size,
00122                                              internal::WRITE_CACHE,info->state->data_file_length,
00123                                              (bool) (info->lock_type != F_UNLCK),
00124                                              MYF(share->write_flag & MY_WAIT_IF_FULL))))
00125       {
00126         info->opt_flag|=WRITE_CACHE_USED;
00127         info->update&= ~(HA_STATE_ROW_CHANGED |
00128        HA_STATE_WRITE_AT_END |
00129        HA_STATE_EXTEND_BLOCK);
00130       }
00131     }
00132     break;
00133   case HA_EXTRA_PREPARE_FOR_UPDATE:
00134     if (info->s->data_file_type != DYNAMIC_RECORD)
00135       break;
00136     /* Remove read/write cache if dynamic rows */
00137   case HA_EXTRA_NO_CACHE:
00138     if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
00139     {
00140       info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
00141       error= info->rec_cache.end_io_cache();
00142       /* Sergei will insert full text index caching here */
00143     }
00144 #if !defined(TARGET_OS_SOLARIS)
00145     if (info->opt_flag & MEMMAP_USED)
00146       madvise((char*) share->file_map, share->state.state.data_file_length,
00147               MADV_RANDOM);
00148 #endif
00149     break;
00150   case HA_EXTRA_FLUSH_CACHE:
00151     if (info->opt_flag & WRITE_CACHE_USED)
00152     {
00153       if ((error=flush_io_cache(&info->rec_cache)))
00154       {
00155         mi_print_error(info->s, HA_ERR_CRASHED);
00156   mi_mark_crashed(info);      /* Fatal error found */
00157       }
00158     }
00159     break;
00160   case HA_EXTRA_NO_READCHECK:
00161     info->opt_flag&= ~READ_CHECK_USED;    /* No readcheck */
00162     break;
00163   case HA_EXTRA_READCHECK:
00164     info->opt_flag|= READ_CHECK_USED;
00165     break;
00166   case HA_EXTRA_KEYREAD:      /* Read only keys to record */
00167   case HA_EXTRA_REMEMBER_POS:
00168     info->opt_flag |= REMEMBER_OLD_POS;
00169     memmove(info->lastkey+share->base.max_key_length*2,
00170             info->lastkey,info->lastkey_length);
00171     info->save_update=  info->update;
00172     info->save_lastinx= info->lastinx;
00173     info->save_lastpos= info->lastpos;
00174     info->save_lastkey_length=info->lastkey_length;
00175     if (function == HA_EXTRA_REMEMBER_POS)
00176       break;
00177     /* fall through */
00178   case HA_EXTRA_KEYREAD_CHANGE_POS:
00179     info->opt_flag |= KEY_READ_USED;
00180     info->read_record=_mi_read_key_record;
00181     break;
00182   case HA_EXTRA_NO_KEYREAD:
00183   case HA_EXTRA_RESTORE_POS:
00184     if (info->opt_flag & REMEMBER_OLD_POS)
00185     {
00186       memmove(info->lastkey,
00187               info->lastkey+share->base.max_key_length*2,
00188               info->save_lastkey_length);
00189       info->update= info->save_update | HA_STATE_WRITTEN;
00190       info->lastinx=  info->save_lastinx;
00191       info->lastpos=  info->save_lastpos;
00192       info->lastkey_length=info->save_lastkey_length;
00193     }
00194     info->read_record=  share->read_record;
00195     info->opt_flag&= ~(KEY_READ_USED | REMEMBER_OLD_POS);
00196     break;
00197   case HA_EXTRA_NO_USER_CHANGE: /* Database is somehow locked agains changes */
00198     info->lock_type= F_EXTRA_LCK; /* Simulate as locked */
00199     break;
00200   case HA_EXTRA_WAIT_LOCK:
00201     info->lock_wait=0;
00202     break;
00203   case HA_EXTRA_NO_WAIT_LOCK:
00204     info->lock_wait=MY_DONT_WAIT;
00205     break;
00206   case HA_EXTRA_NO_KEYS:
00207     if (info->lock_type == F_UNLCK)
00208     {
00209       error=1;          /* Not possibly if not lock */
00210       break;
00211     }
00212     if (mi_is_any_key_active(share->state.key_map))
00213     {
00214       MI_KEYDEF *key=share->keyinfo;
00215       uint32_t i;
00216       for (i=0 ; i < share->base.keys ; i++,key++)
00217       {
00218         if (!(key->flag & HA_NOSAME) && info->s->base.auto_key != i+1)
00219         {
00220           mi_clear_key_active(share->state.key_map, i);
00221           info->update|= HA_STATE_CHANGED;
00222         }
00223       }
00224 
00225       if (!share->changed)
00226       {
00227   share->state.changed|= STATE_CHANGED | STATE_NOT_ANALYZED;
00228   share->changed=1;     /* Update on close */
00229   if (!share->global_changed)
00230   {
00231     share->global_changed=1;
00232     share->state.open_count++;
00233   }
00234       }
00235       share->state.state= *info->state;
00236       error=mi_state_info_write(share->kfile,&share->state,1 | 2);
00237     }
00238     break;
00239   case HA_EXTRA_FORCE_REOPEN:
00240     THR_LOCK_myisam.lock();
00241     share->last_version= 0L;      /* Impossible version */
00242     THR_LOCK_myisam.unlock();
00243     break;
00244   case HA_EXTRA_PREPARE_FOR_DROP:
00245     THR_LOCK_myisam.lock();
00246     share->last_version= 0L;      /* Impossible version */
00247 #ifdef __WIN__REMOVE_OBSOLETE_WORKAROUND
00248     /* Close the isam and data files as Win32 can't drop an open table */
00249     if (flush_key_blocks(share->key_cache, share->kfile,
00250        (function == HA_EXTRA_FORCE_REOPEN ?
00251         FLUSH_RELEASE : FLUSH_IGNORE_CHANGED)))
00252     {
00253       error=errno;
00254       share->changed=1;
00255       mi_print_error(info->s, HA_ERR_CRASHED);
00256       mi_mark_crashed(info);      /* Fatal error found */
00257     }
00258     if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
00259     {
00260       info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
00261       error=end_io_cache(&info->rec_cache);
00262     }
00263     if (info->lock_type != F_UNLCK && ! info->was_locked)
00264     {
00265       info->was_locked=info->lock_type;
00266       if (mi_lock_database(info,F_UNLCK))
00267   error=errno;
00268       info->lock_type = F_UNLCK;
00269     }
00270     if (share->kfile >= 0)
00271       _mi_decrement_open_count(info);
00272     if (share->kfile >= 0 && internal::my_close(share->kfile,MYF(0)))
00273       error=errno;
00274     {
00275       list<MI_INFO *>::iterator it= myisam_open_list.begin();
00276       while (it != myisam_open_list.end())
00277       {
00278   MI_INFO *tmpinfo= *it;
00279   if (tmpinfo->s == info->s)
00280   {
00281     if (tmpinfo->dfile >= 0 && internal::my_close(tmpinfo->dfile,MYF(0)))
00282       error = errno;
00283     tmpinfo->dfile= -1;
00284   }
00285         ++it;
00286       }
00287     }
00288     share->kfile= -1;       /* Files aren't open anymore */
00289 #endif
00290     THR_LOCK_myisam.unlock();
00291     break;
00292   case HA_EXTRA_FLUSH:
00293     if (!share->temporary)
00294       flush_key_blocks(share->getKeyCache(), share->kfile, FLUSH_KEEP);
00295 #ifdef HAVE_PWRITE
00296     _mi_decrement_open_count(info);
00297 #endif
00298     if (share->not_flushed)
00299     {
00300       share->not_flushed= false;
00301     }
00302     if (share->base.blobs)
00303       mi_alloc_rec_buff(info, SIZE_MAX, &info->rec_buff);
00304     break;
00305   case HA_EXTRA_NORMAL:       /* Theese isn't in use */
00306     info->quick_mode=0;
00307     break;
00308   case HA_EXTRA_QUICK:
00309     info->quick_mode=1;
00310     break;
00311   case HA_EXTRA_NO_ROWS:
00312     if (!share->state.header.uniques)
00313       info->opt_flag|= OPT_NO_ROWS;
00314     break;
00315   case HA_EXTRA_PRELOAD_BUFFER_SIZE:
00316     info->preload_buff_size= *((uint32_t *) extra_arg);
00317     break;
00318   case HA_EXTRA_CHANGE_KEY_TO_UNIQUE:
00319   case HA_EXTRA_CHANGE_KEY_TO_DUP:
00320     mi_extra_keyflag(info, function);
00321     break;
00322   case HA_EXTRA_KEY_CACHE:
00323   case HA_EXTRA_NO_KEY_CACHE:
00324   default:
00325     break;
00326   }
00327 
00328   return(error);
00329 } /* mi_extra */
00330 
00331 
00332 /*
00333     Start/Stop Inserting Duplicates Into a Table, WL#1648.
00334  */
00335 static void mi_extra_keyflag(MI_INFO *info, enum ha_extra_function function)
00336 {
00337   uint32_t  idx;
00338 
00339   for (idx= 0; idx< info->s->base.keys; idx++)
00340   {
00341     switch (function) {
00342     case HA_EXTRA_CHANGE_KEY_TO_UNIQUE:
00343       info->s->keyinfo[idx].flag|= HA_NOSAME;
00344       break;
00345     case HA_EXTRA_CHANGE_KEY_TO_DUP:
00346       info->s->keyinfo[idx].flag&= ~(HA_NOSAME);
00347       break;
00348     default:
00349       break;
00350     }
00351   }
00352 }
00353 
00354 
00355 int mi_reset(MI_INFO *info)
00356 {
00357   int error= 0;
00358   MYISAM_SHARE *share=info->s;
00359   /*
00360     Free buffers and reset the following flags:
00361     EXTRA_CACHE, EXTRA_WRITE_CACHE, EXTRA_KEYREAD, EXTRA_QUICK
00362 
00363     If the row buffer cache is large (for dynamic tables), reduce it
00364     to save memory.
00365   */
00366   if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
00367   {
00368     info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
00369     error= info->rec_cache.end_io_cache();
00370   }
00371   if (share->base.blobs)
00372     mi_alloc_rec_buff(info, SIZE_MAX, &info->rec_buff);
00373 #if !defined(TARGET_OS_SOLARIS)
00374   if (info->opt_flag & MEMMAP_USED)
00375     madvise((char*) share->file_map, share->state.state.data_file_length,
00376             MADV_RANDOM);
00377 #endif
00378   info->opt_flag&= ~(KEY_READ_USED | REMEMBER_OLD_POS);
00379   info->quick_mode=0;
00380   info->lastinx= 0;     /* Use first index as def */
00381   info->last_search_keypage= info->lastpos= HA_OFFSET_ERROR;
00382   info->page_changed= 1;
00383   info->update= ((info->update & HA_STATE_CHANGED) | HA_STATE_NEXT_FOUND |
00384                  HA_STATE_PREV_FOUND);
00385   return(error);
00386 }