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/table/instance/shared.h>
24
#include <drizzled/definition/cache.h>
25
#include <drizzled/plugin/event_observer.h>
36
Shared::Shared(const identifier::Table::Type type_arg,
37
const identifier::Table &identifier,
38
char *path_arg, uint32_t path_length_arg) :
39
TableShare(type_arg, identifier, path_arg, path_length_arg),
44
Shared::Shared(const identifier::Table &identifier) :
45
TableShare(identifier, identifier.getKey()),
50
Shared::shared_ptr Shared::foundTableShare(Shared::shared_ptr share)
53
We found an existing table definition. Return it if we didn't get
54
an error when reading the table definition from file.
58
/* Table definition contained an error */
59
share->open_table_error(share->error, share->open_errno, share->errarg);
61
return Shared::shared_ptr();
64
share->incrementTableCount();
72
Get a shared instance for a table.
76
table_list Table that should be opened
78
key_length Length of key
79
error out: Error code from open_table_def()
82
Get a table definition from the table definition cache.
83
If it doesn't exist, create a new from the table definition file.
86
We must have wrlock on table::Cache::singleton().mutex() when we come here
94
Shared::shared_ptr Shared::make_shared(Session *session,
95
const identifier::Table &identifier,
98
Shared::shared_ptr share;
102
/* Read table definition from cache */
103
if ((share= definition::Cache::singleton().find(identifier.getKey())))
104
return foundTableShare(share);
106
share.reset(new Shared(message::Table::STANDARD, identifier));
108
if (share->open_table_def(*session, identifier))
110
in_error= share->error;
112
return Shared::shared_ptr();
114
share->incrementTableCount(); // Mark in use
116
plugin::EventObserver::registerTableEvents(*share);
118
bool ret= definition::Cache::singleton().insert(identifier.getKey(), share);
121
return Shared::shared_ptr();
128
assert(getTableCount() == 0);
129
plugin::EventObserver::deregisterTableEvents(*this);
133
/*****************************************************************************
134
Functions to handle table definition cach (TableShare)
135
*****************************************************************************/
138
Mark that we are not using table share anymore.
145
If ref_count goes to zero and (we have done a refresh or if we have
146
already too many open table shares) then delete the definition.
149
void release(TableShare *share)
151
bool to_be_deleted= false;
152
//safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
155
if (not share->decrementTableCount())
163
definition::Cache::singleton().erase(share->getCacheKey());
167
void release(TableShare::shared_ptr &share)
169
bool to_be_deleted= false;
171
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
175
if (not share->decrementTableCount())
183
definition::Cache::singleton().erase(share->getCacheKey());
187
void release(const identifier::Table &identifier)
189
TableShare::shared_ptr share= definition::Cache::singleton().find(identifier.getKey());
192
share->resetVersion();
193
if (share->getTableCount() == 0)
195
definition::Cache::singleton().erase(identifier.getKey());
201
} /* namespace instance */
202
} /* namespace table */
203
} /* namespace drizzled */