~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2004 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
/* close a isam-database */
17
/*
18
  TODO:
19
   We need to have a separate mutex on the closed file to allow other threads
20
   to open other files during the time we flush the cache and close this file
21
*/
22
23
#include "myisamdef.h"
24
25
int mi_close(register MI_INFO *info)
26
{
27
  int error=0,flag;
28
  MYISAM_SHARE *share=info->s;
29
  DBUG_ENTER("mi_close");
30
  DBUG_PRINT("enter",("base: 0x%lx  reopen: %u  locks: %u",
31
		      (long) info, (uint) share->reopen,
32
                      (uint) share->tot_locks));
33
34
  pthread_mutex_lock(&THR_LOCK_myisam);
35
  if (info->lock_type == F_EXTRA_LCK)
36
    info->lock_type=F_UNLCK;			/* HA_EXTRA_NO_USER_CHANGE */
37
38
  if (share->reopen == 1 && share->kfile >= 0)
39
    _mi_decrement_open_count(info);
40
41
  if (info->lock_type != F_UNLCK)
42
  {
43
    if (mi_lock_database(info,F_UNLCK))
44
      error=my_errno;
45
  }
46
  pthread_mutex_lock(&share->intern_lock);
47
48
  if (share->options & HA_OPTION_READ_ONLY_DATA)
49
  {
50
    share->r_locks--;
51
    share->tot_locks--;
52
  }
53
  if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
54
  {
55
    if (end_io_cache(&info->rec_cache))
56
      error=my_errno;
57
    info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
58
  }
59
  flag= !--share->reopen;
60
  myisam_open_list=list_delete(myisam_open_list,&info->open_list);
61
  pthread_mutex_unlock(&share->intern_lock);
62
63
  my_free(mi_get_rec_buff_ptr(info, info->rec_buff), MYF(MY_ALLOW_ZERO_PTR));
64
  if (flag)
65
  {
66
    if (share->kfile >= 0 &&
67
	flush_key_blocks(share->key_cache, share->kfile,
68
			 share->temporary ? FLUSH_IGNORE_CHANGED :
69
			 FLUSH_RELEASE))
70
      error=my_errno;
71
    if (share->kfile >= 0)
72
    {
73
      /*
74
        If we are crashed, we can safely flush the current state as it will
75
        not change the crashed state.
76
        We can NOT write the state in other cases as other threads
77
        may be using the file at this point
78
      */
79
      if (share->mode != O_RDONLY && mi_is_crashed(info))
80
	mi_state_info_write(share->kfile, &share->state, 1);
81
      if (my_close(share->kfile,MYF(0)))
82
        error = my_errno;
83
    }
84
#ifdef HAVE_MMAP
85
    if (share->file_map)
86
      _mi_unmap_file(info);
87
#endif
88
    if (share->decode_trees)
89
    {
90
      my_free((uchar*) share->decode_trees,MYF(0));
91
      my_free((uchar*) share->decode_tables,MYF(0));
92
    }
93
#ifdef THREAD
94
    thr_lock_delete(&share->lock);
95
    VOID(pthread_mutex_destroy(&share->intern_lock));
96
    {
97
      int i,keys;
98
      keys = share->state.header.keys;
99
      VOID(rwlock_destroy(&share->mmap_lock));
100
      for(i=0; i<keys; i++) {
101
	VOID(rwlock_destroy(&share->key_root_lock[i]));
102
      }
103
    }
104
#endif
105
    my_free((uchar*) info->s,MYF(0));
106
  }
107
  pthread_mutex_unlock(&THR_LOCK_myisam);
108
  if (info->ftparser_param)
109
  {
110
    my_free((uchar*)info->ftparser_param, MYF(0));
111
    info->ftparser_param= 0;
112
  }
113
  if (info->dfile >= 0 && my_close(info->dfile,MYF(0)))
114
    error = my_errno;
115
116
  myisam_log_command(MI_LOG_CLOSE,info,NULL,0,error);
117
  my_free((uchar*) info,MYF(0));
118
119
  if (error)
120
  {
121
    DBUG_RETURN(my_errno=error);
122
  }
123
  DBUG_RETURN(0);
124
} /* mi_close */