~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Lee Bieber
  • Date: 2011-03-29 22:31:41 UTC
  • mfrom: (2257.1.3 build)
  • Revision ID: kalebral@gmail.com-20110329223141-yxc22h3l2he58sk0
Merge Andrew - 743842: Build failure using GCC 4.6
Merge Stewart - 738022: CachedDirectory silently fails to add entries if stat() fails
Merge Olaf - Common fwd: add copyright, add more declaration

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/definition/cache.h>
 
24
#include <drizzled/error.h>
 
25
#include <drizzled/message/schema.h>
 
26
#include <drizzled/plugin/event_observer.h>
 
27
#include <drizzled/table/instance/shared.h>
 
28
#include <drizzled/plugin/storage_engine.h>
 
29
 
 
30
namespace drizzled
 
31
{
 
32
 
 
33
namespace table
 
34
{
 
35
 
 
36
namespace instance
 
37
{
 
38
 
 
39
Shared::Shared(const identifier::Table::Type type_arg,
 
40
               const identifier::Table &identifier,
 
41
               char *path_arg, uint32_t path_length_arg) :
 
42
  TableShare(type_arg, identifier, path_arg, path_length_arg),
 
43
  event_observers(NULL)
 
44
{
 
45
}
 
46
 
 
47
Shared::Shared(const identifier::Table &identifier,
 
48
               message::schema::shared_ptr schema_message) :
 
49
  TableShare(message::Table::STANDARD, identifier, NULL, 0),
 
50
  _schema(schema_message),
 
51
  event_observers(NULL)
 
52
{
 
53
}
 
54
 
 
55
Shared::Shared(const identifier::Table &identifier) :
 
56
  TableShare(identifier, identifier.getKey()),
 
57
  event_observers(NULL)
 
58
{
 
59
}
 
60
 
 
61
bool Shared::is_replicated() const
 
62
{
 
63
  if (_schema)
 
64
  {
 
65
    if (not message::is_replicated(*_schema))
 
66
      return false;
 
67
  }
 
68
 
 
69
  assert(getTableMessage());
 
70
  return message::is_replicated(*getTableMessage());
 
71
}
 
72
 
 
73
 
 
74
Shared::shared_ptr Shared::foundTableShare(Shared::shared_ptr share)
 
75
{
 
76
  /*
 
77
    We found an existing table definition. Return it if we didn't get
 
78
    an error when reading the table definition from file.
 
79
  */
 
80
  if (share->error)
 
81
  {
 
82
    /* Table definition contained an error */
 
83
    share->open_table_error(share->error, share->open_errno, share->errarg);
 
84
 
 
85
    return Shared::shared_ptr();
 
86
  }
 
87
 
 
88
  share->incrementTableCount();
 
89
 
 
90
  return share;
 
91
}
 
92
 
 
93
 
 
94
 
 
95
/*
 
96
  Get a shared instance for a table.
 
97
 
 
98
  get_table_share()
 
99
  session                       Thread handle
 
100
  table_list            Table that should be opened
 
101
  key                   Table cache key
 
102
  key_length            Length of key
 
103
  error                 out: Error code from open_table_def()
 
104
 
 
105
  IMPLEMENTATION
 
106
  Get a table definition from the table definition cache.
 
107
  If it doesn't exist, create a new from the table definition file.
 
108
 
 
109
  NOTES
 
110
  We must have wrlock on table::Cache::singleton().mutex() when we come here
 
111
  (To be changed later)
 
112
 
 
113
  RETURN
 
114
  0  Error
 
115
#  Share for table
 
116
*/
 
117
 
 
118
Shared::shared_ptr Shared::make_shared(Session *session, 
 
119
                                       const identifier::Table &identifier,
 
120
                                       int &in_error)
 
121
{
 
122
  Shared::shared_ptr share;
 
123
 
 
124
  in_error= 0;
 
125
 
 
126
  /* Read table definition from cache */
 
127
  if ((share= definition::Cache::singleton().find(identifier.getKey())))
 
128
    return foundTableShare(share);
 
129
  
 
130
  drizzled::message::schema::shared_ptr schema_message_ptr= plugin::StorageEngine::getSchemaDefinition(identifier);
 
131
 
 
132
  if (not schema_message_ptr)
 
133
  {
 
134
    drizzled::my_error(ER_SCHEMA_DOES_NOT_EXIST, identifier);
 
135
    return Shared::shared_ptr();
 
136
  }
 
137
 
 
138
  share.reset(new Shared(identifier, schema_message_ptr));
 
139
 
 
140
  if (share->open_table_def(*session, identifier))
 
141
  {
 
142
    in_error= share->error;
 
143
 
 
144
    return Shared::shared_ptr();
 
145
  }
 
146
  share->incrementTableCount();                         // Mark in use
 
147
  
 
148
  plugin::EventObserver::registerTableEvents(*share);
 
149
 
 
150
  bool ret= definition::Cache::singleton().insert(identifier.getKey(), share);
 
151
 
 
152
  if (not ret)
 
153
  {
 
154
    drizzled::my_error(ER_UNKNOWN_ERROR);
 
155
    return Shared::shared_ptr();
 
156
  }
 
157
 
 
158
  return share;
 
159
}
 
160
 
 
161
Shared::~Shared()
 
162
{
 
163
  assert(getTableCount() == 0);
 
164
  plugin::EventObserver::deregisterTableEvents(*this);
 
165
}
 
166
 
 
167
 
 
168
/*****************************************************************************
 
169
  Functions to handle table definition cach (TableShare)
 
170
 *****************************************************************************/
 
171
 
 
172
/*
 
173
  Mark that we are not using table share anymore.
 
174
 
 
175
  SYNOPSIS
 
176
  release()
 
177
  share         Table share
 
178
 
 
179
  IMPLEMENTATION
 
180
  If ref_count goes to zero and (we have done a refresh or if we have
 
181
  already too many open table shares) then delete the definition.
 
182
*/
 
183
 
 
184
void release(TableShare *share)
 
185
{
 
186
  bool to_be_deleted= false;
 
187
  //safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
 
188
 
 
189
  share->lock();
 
190
  if (not share->decrementTableCount())
 
191
  {
 
192
    to_be_deleted= true;
 
193
  }
 
194
  share->unlock();
 
195
 
 
196
  if (to_be_deleted)
 
197
  {
 
198
    definition::Cache::singleton().erase(share->getCacheKey());
 
199
  }
 
200
}
 
201
 
 
202
void release(TableShare::shared_ptr &share)
 
203
{
 
204
  bool to_be_deleted= false;
 
205
#if 0
 
206
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
 
207
#endif
 
208
 
 
209
  share->lock();
 
210
  if (not share->decrementTableCount())
 
211
  {
 
212
    to_be_deleted= true;
 
213
  }
 
214
  share->unlock();
 
215
 
 
216
  if (to_be_deleted)
 
217
  {
 
218
    definition::Cache::singleton().erase(share->getCacheKey());
 
219
  }
 
220
}
 
221
 
 
222
void release(const identifier::Table &identifier)
 
223
{
 
224
  TableShare::shared_ptr share= definition::Cache::singleton().find(identifier.getKey());
 
225
  if (share)
 
226
  {
 
227
    share->resetVersion(); 
 
228
    if (share->getTableCount() == 0)
 
229
    {
 
230
      definition::Cache::singleton().erase(identifier.getKey());
 
231
    }
 
232
  }
 
233
}
 
234
 
 
235
 
 
236
} /* namespace instance */
 
237
} /* namespace table */
 
238
} /* namespace drizzled */