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   free(rec_buff_ptr);
00062   if (flag)
00063   {
00064     if (share->kfile >= 0)
00065     {
00066       /*
00067         If we are crashed, we can safely flush the current state as it will
00068         not change the crashed state.
00069         We can NOT write the state in other cases as other threads
00070         may be using the file at this point
00071       */
00072       if (share->mode != O_RDONLY && mi_is_crashed(info))
00073   mi_state_info_write(share->kfile, &share->state, 1);
00074       if (internal::my_close(share->kfile,MYF(0)))
00075         error = errno;
00076     }
00077     if (share->decode_trees)
00078     {
00079       free((unsigned char*) share->decode_trees);
00080       free((unsigned char*) share->decode_tables);
00081     }
00082     delete info->s->in_use;
00083     free((unsigned char*) info->s);
00084   }
00085   THR_LOCK_myisam.unlock();
00086 
00087   if (info->dfile >= 0 && internal::my_close(info->dfile,MYF(0)))
00088     error = errno;
00089 
00090   free((unsigned char*) info);
00091 
00092   if (error)
00093   {
00094     return(errno=error);
00095   }
00096   return(0);
00097 } /* mi_close */