Drizzled Public API Documentation

mi_close.cc
00001 /* Copyright (C) 2000-2004 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 /* close a isam-database */
00017 /*
00018   TODO:
00019    We need to have a separate mutex on the closed file to allow other threads
00020    to open other files during the time we flush the cache and close this file
00021 */
00022 
00023 #include "myisam_priv.h"
00024 #include <cstdlib>
00025 
00026 using namespace drizzled;
00027 
00028 int mi_close(MI_INFO *info)
00029 {
00030   int error=0,flag;
00031   MYISAM_SHARE *share=info->s;
00032 
00033   THR_LOCK_myisam.lock();
00034   if (info->lock_type == F_EXTRA_LCK)
00035     info->lock_type=F_UNLCK;      /* HA_EXTRA_NO_USER_CHANGE */
00036 
00037   if (share->reopen == 1 && share->kfile >= 0)
00038     _mi_decrement_open_count(info);
00039 
00040   if (info->lock_type != F_UNLCK)
00041   {
00042     if (mi_lock_database(info,F_UNLCK))
00043       error=errno;
00044   }
00045 
00046   if (share->options & HA_OPTION_READ_ONLY_DATA)
00047   {
00048     share->r_locks--;
00049     share->tot_locks--;
00050   }
00051   if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
00052   {
00053     if (info->rec_cache.end_io_cache())
00054       error=errno;
00055     info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
00056   }
00057   flag= !--share->reopen;
00058   myisam_open_list.remove(info);
00059 
00060   void * rec_buff_ptr= mi_get_rec_buff_ptr(info, info->rec_buff);
00061   if (rec_buff_ptr != NULL)
00062     free(rec_buff_ptr);
00063   if (flag)
00064   {
00065     if (share->kfile >= 0 &&
00066   flush_key_blocks(share->getKeyCache(), share->kfile,
00067        share->temporary ? FLUSH_IGNORE_CHANGED :
00068        FLUSH_RELEASE))
00069       error=errno;
00070     end_key_cache(share->getKeyCache(), true);
00071     if (share->kfile >= 0)
00072     {
00073       /*
00074         If we are crashed, we can safely flush the current state as it will
00075         not change the crashed state.
00076         We can NOT write the state in other cases as other threads
00077         may be using the file at this point
00078       */
00079       if (share->mode != O_RDONLY && mi_is_crashed(info))
00080   mi_state_info_write(share->kfile, &share->state, 1);
00081       if (internal::my_close(share->kfile,MYF(0)))
00082         error = errno;
00083     }
00084     if (share->decode_trees)
00085     {
00086       free((unsigned char*) share->decode_trees);
00087       free((unsigned char*) share->decode_tables);
00088     }
00089     delete info->s->in_use;
00090     free((unsigned char*) info->s);
00091   }
00092   THR_LOCK_myisam.unlock();
00093 
00094   if (info->dfile >= 0 && internal::my_close(info->dfile,MYF(0)))
00095     error = errno;
00096 
00097   free((unsigned char*) info);
00098 
00099   if (error)
00100   {
00101     return(errno=error);
00102   }
00103   return(0);
00104 } /* mi_close */