~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table/instance/shared.cc

  • Committer: Brian Aker
  • Date: 2011-01-13 07:03:39 UTC
  • mfrom: (2077.1.3 drizzle)
  • Revision ID: brian@gir-3-20110113070339-bnfp4yvngb6frhru
Merge in all of the fixes for definition to instance.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2011 Brian Aker
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include <drizzled/table/instance/shared.h>
 
24
#include <drizzled/definition/cache.h>
 
25
#include <drizzled/plugin/event_observer.h>
 
26
 
 
27
namespace drizzled
 
28
{
 
29
 
 
30
namespace table
 
31
{
 
32
 
 
33
namespace instance
 
34
{
 
35
 
 
36
Shared::Shared(const TableIdentifier::Type type_arg,
 
37
               const TableIdentifier &identifier,
 
38
               char *path_arg, uint32_t path_length_arg) :
 
39
  TableShare(type_arg, identifier, path_arg, path_length_arg)
 
40
{
 
41
}
 
42
 
 
43
Shared::Shared(const TableIdentifier &identifier) :
 
44
  TableShare(identifier, identifier.getKey())
 
45
{
 
46
}
 
47
 
 
48
Shared::shared_ptr Shared::foundTableShare(Shared::shared_ptr share)
 
49
{
 
50
  /*
 
51
    We found an existing table definition. Return it if we didn't get
 
52
    an error when reading the table definition from file.
 
53
  */
 
54
  if (share->error)
 
55
  {
 
56
    /* Table definition contained an error */
 
57
    share->open_table_error(share->error, share->open_errno, share->errarg);
 
58
 
 
59
    return Shared::shared_ptr();
 
60
  }
 
61
 
 
62
  share->incrementTableCount();
 
63
 
 
64
  return share;
 
65
}
 
66
 
 
67
 
 
68
 
 
69
/*
 
70
  Get a shared instance for a table.
 
71
 
 
72
  get_table_share()
 
73
  session                       Thread handle
 
74
  table_list            Table that should be opened
 
75
  key                   Table cache key
 
76
  key_length            Length of key
 
77
  error                 out: Error code from open_table_def()
 
78
 
 
79
  IMPLEMENTATION
 
80
  Get a table definition from the table definition cache.
 
81
  If it doesn't exist, create a new from the table definition file.
 
82
 
 
83
  NOTES
 
84
  We must have wrlock on table::Cache::singleton().mutex() when we come here
 
85
  (To be changed later)
 
86
 
 
87
  RETURN
 
88
  0  Error
 
89
#  Share for table
 
90
*/
 
91
 
 
92
Shared::shared_ptr Shared::make_shared(Session *session, 
 
93
                                       const TableIdentifier &identifier,
 
94
                                       int &in_error)
 
95
{
 
96
  Shared::shared_ptr share;
 
97
 
 
98
  in_error= 0;
 
99
 
 
100
  /* Read table definition from cache */
 
101
  if ((share= definition::Cache::singleton().find(identifier.getKey())))
 
102
    return foundTableShare(share);
 
103
 
 
104
  share.reset(new Shared(message::Table::STANDARD, identifier));
 
105
  
 
106
  if (share->open_table_def(*session, identifier))
 
107
  {
 
108
    in_error= share->error;
 
109
 
 
110
    return Shared::shared_ptr();
 
111
  }
 
112
  share->incrementTableCount();                         // Mark in use
 
113
  
 
114
  plugin::EventObserver::registerTableEvents(*share);
 
115
 
 
116
  bool ret= definition::Cache::singleton().insert(identifier.getKey(), share);
 
117
 
 
118
  if (not ret)
 
119
    return Shared::shared_ptr();
 
120
 
 
121
  return share;
 
122
}
 
123
 
 
124
Shared::~Shared()
 
125
{
 
126
  assert(getTableCount() == 0);
 
127
}
 
128
 
 
129
 
 
130
/*****************************************************************************
 
131
  Functions to handle table definition cach (TableShare)
 
132
 *****************************************************************************/
 
133
 
 
134
/*
 
135
  Mark that we are not using table share anymore.
 
136
 
 
137
  SYNOPSIS
 
138
  release()
 
139
  share         Table share
 
140
 
 
141
  IMPLEMENTATION
 
142
  If ref_count goes to zero and (we have done a refresh or if we have
 
143
  already too many open table shares) then delete the definition.
 
144
*/
 
145
 
 
146
void release(TableShare *share)
 
147
{
 
148
  bool to_be_deleted= false;
 
149
  //safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
 
150
 
 
151
  share->lock();
 
152
  if (not share->decrementTableCount())
 
153
  {
 
154
    to_be_deleted= true;
 
155
  }
 
156
  share->unlock();
 
157
 
 
158
  if (to_be_deleted)
 
159
  {
 
160
    definition::Cache::singleton().erase(share->getCacheKey());
 
161
  }
 
162
}
 
163
 
 
164
void release(TableShare::shared_ptr &share)
 
165
{
 
166
  bool to_be_deleted= false;
 
167
#if 0
 
168
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
 
169
#endif
 
170
 
 
171
  share->lock();
 
172
  if (not share->decrementTableCount())
 
173
  {
 
174
    to_be_deleted= true;
 
175
  }
 
176
  share->unlock();
 
177
 
 
178
  if (to_be_deleted)
 
179
  {
 
180
    definition::Cache::singleton().erase(share->getCacheKey());
 
181
  }
 
182
}
 
183
 
 
184
void release(const TableIdentifier &identifier)
 
185
{
 
186
  TableShare::shared_ptr share= definition::Cache::singleton().find(identifier.getKey());
 
187
  if (share)
 
188
  {
 
189
    share->resetVersion(); 
 
190
    if (share->getTableCount() == 0)
 
191
    {
 
192
      definition::Cache::singleton().erase(identifier.getKey());
 
193
    }
 
194
  }
 
195
}
 
196
 
 
197
 
 
198
} /* namespace instance */
 
199
} /* namespace table */
 
200
} /* namespace drizzled */