~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/myisam/mi_close.cc

  • Committer: Brian Aker
  • Date: 2009-08-15 00:59:30 UTC
  • mfrom: (1115.1.7 merge)
  • Revision ID: brian@gaz-20090815005930-q47yenjrq1esiwsz
Merge of Trond + Brian

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 "myisam_priv.h"
24
 
#include <cstdlib>
25
 
 
26
 
using namespace drizzled;
27
 
 
28
 
int mi_close(MI_INFO *info)
29
 
{
30
 
  int error=0,flag;
31
 
  MYISAM_SHARE *share=info->s;
32
 
 
33
 
  pthread_mutex_lock(&THR_LOCK_myisam);
34
 
  if (info->lock_type == F_EXTRA_LCK)
35
 
    info->lock_type=F_UNLCK;                    /* HA_EXTRA_NO_USER_CHANGE */
36
 
 
37
 
  if (share->reopen == 1 && share->kfile >= 0)
38
 
    _mi_decrement_open_count(info);
39
 
 
40
 
  if (info->lock_type != F_UNLCK)
41
 
  {
42
 
    if (mi_lock_database(info,F_UNLCK))
43
 
      error=errno;
44
 
  }
45
 
  pthread_mutex_lock(&share->intern_lock);
46
 
 
47
 
  if (share->options & HA_OPTION_READ_ONLY_DATA)
48
 
  {
49
 
    share->r_locks--;
50
 
    share->tot_locks--;
51
 
  }
52
 
  if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
53
 
  {
54
 
    if (end_io_cache(&info->rec_cache))
55
 
      error=errno;
56
 
    info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
57
 
  }
58
 
  flag= !--share->reopen;
59
 
  myisam_open_list.remove(info);
60
 
  pthread_mutex_unlock(&share->intern_lock);
61
 
 
62
 
  void * rec_buff_ptr= mi_get_rec_buff_ptr(info, info->rec_buff);
63
 
  if (rec_buff_ptr != NULL)
64
 
    free(rec_buff_ptr);
65
 
  if (flag)
66
 
  {
67
 
    if (share->kfile >= 0 &&
68
 
        flush_key_blocks(share->key_cache, share->kfile,
69
 
                         share->temporary ? FLUSH_IGNORE_CHANGED :
70
 
                         FLUSH_RELEASE))
71
 
      error=errno;
72
 
    if (share->kfile >= 0)
73
 
    {
74
 
      /*
75
 
        If we are crashed, we can safely flush the current state as it will
76
 
        not change the crashed state.
77
 
        We can NOT write the state in other cases as other threads
78
 
        may be using the file at this point
79
 
      */
80
 
      if (share->mode != O_RDONLY && mi_is_crashed(info))
81
 
        mi_state_info_write(share->kfile, &share->state, 1);
82
 
      if (internal::my_close(share->kfile,MYF(0)))
83
 
        error = errno;
84
 
    }
85
 
    if (share->decode_trees)
86
 
    {
87
 
      free((unsigned char*) share->decode_trees);
88
 
      free((unsigned char*) share->decode_tables);
89
 
    }
90
 
    thr_lock_delete(&share->lock);
91
 
    pthread_mutex_destroy(&share->intern_lock);
92
 
    {
93
 
      int i,keys;
94
 
      keys = share->state.header.keys;
95
 
      pthread_rwlock_destroy(&share->mmap_lock);
96
 
      for(i=0; i<keys; i++) {
97
 
        pthread_rwlock_destroy(&share->key_root_lock[i]);
98
 
      }
99
 
    }
100
 
    delete info->s->in_use;
101
 
    free((unsigned char*) info->s);
102
 
  }
103
 
  pthread_mutex_unlock(&THR_LOCK_myisam);
104
 
 
105
 
  if (info->dfile >= 0 && internal::my_close(info->dfile,MYF(0)))
106
 
    error = errno;
107
 
 
108
 
  free((unsigned char*) info);
109
 
 
110
 
  if (error)
111
 
  {
112
 
    return(errno=error);
113
 
  }
114
 
  return(0);
115
 
} /* mi_close */