1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2011 Brian Aker
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.
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.
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
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>
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),
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),
55
Shared::Shared(const identifier::Table &identifier) :
56
TableShare(identifier, identifier.getKey()),
61
bool Shared::is_replicated() const
65
if (not message::is_replicated(*_schema))
69
assert(getTableMessage());
70
return message::is_replicated(*getTableMessage());
74
Shared::shared_ptr Shared::foundTableShare(Shared::shared_ptr share)
77
We found an existing table definition. Return it if we didn't get
78
an error when reading the table definition from file.
82
/* Table definition contained an error */
83
share->open_table_error(share->error, share->open_errno, share->errarg);
85
return Shared::shared_ptr();
88
share->incrementTableCount();
96
Get a shared instance for a table.
100
table_list Table that should be opened
102
key_length Length of key
103
error out: Error code from open_table_def()
106
Get a table definition from the table definition cache.
107
If it doesn't exist, create a new from the table definition file.
110
We must have wrlock on table::Cache::singleton().mutex() when we come here
111
(To be changed later)
118
Shared::shared_ptr Shared::make_shared(Session *session,
119
const identifier::Table &identifier,
122
Shared::shared_ptr share;
126
/* Read table definition from cache */
127
if ((share= definition::Cache::singleton().find(identifier.getKey())))
128
return foundTableShare(share);
130
drizzled::message::schema::shared_ptr schema_message_ptr= plugin::StorageEngine::getSchemaDefinition(identifier);
132
if (not schema_message_ptr)
134
drizzled::my_error(ER_SCHEMA_DOES_NOT_EXIST, identifier);
135
return Shared::shared_ptr();
138
share.reset(new Shared(identifier, schema_message_ptr));
140
if (share->open_table_def(*session, identifier))
142
in_error= share->error;
144
return Shared::shared_ptr();
146
share->incrementTableCount(); // Mark in use
148
plugin::EventObserver::registerTableEvents(*share);
150
bool ret= definition::Cache::singleton().insert(identifier.getKey(), share);
154
drizzled::my_error(ER_UNKNOWN_ERROR);
155
return Shared::shared_ptr();
163
assert(getTableCount() == 0);
164
plugin::EventObserver::deregisterTableEvents(*this);
168
/*****************************************************************************
169
Functions to handle table definition cach (TableShare)
170
*****************************************************************************/
173
Mark that we are not using table share anymore.
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.
184
void release(TableShare *share)
186
bool to_be_deleted= false;
187
//safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
190
if (not share->decrementTableCount())
198
definition::Cache::singleton().erase(share->getCacheKey());
202
void release(TableShare::shared_ptr &share)
204
bool to_be_deleted= false;
206
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
210
if (not share->decrementTableCount())
218
definition::Cache::singleton().erase(share->getCacheKey());
222
void release(const identifier::Table &identifier)
224
TableShare::shared_ptr share= definition::Cache::singleton().find(identifier.getKey());
227
share->resetVersion();
228
if (share->getTableCount() == 0)
230
definition::Cache::singleton().erase(identifier.getKey());
236
} /* namespace instance */
237
} /* namespace table */
238
} /* namespace drizzled */