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 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)
43
Shared::Shared(const TableIdentifier &identifier) :
44
TableShare(identifier, identifier.getKey())
48
Shared::shared_ptr Shared::foundTableShare(Shared::shared_ptr share)
51
We found an existing table definition. Return it if we didn't get
52
an error when reading the table definition from file.
56
/* Table definition contained an error */
57
share->open_table_error(share->error, share->open_errno, share->errarg);
59
return Shared::shared_ptr();
62
share->incrementTableCount();
70
Get a shared instance for a table.
74
table_list Table that should be opened
76
key_length Length of key
77
error out: Error code from open_table_def()
80
Get a table definition from the table definition cache.
81
If it doesn't exist, create a new from the table definition file.
84
We must have wrlock on table::Cache::singleton().mutex() when we come here
92
Shared::shared_ptr Shared::make_shared(Session *session,
93
const TableIdentifier &identifier,
96
Shared::shared_ptr share;
100
/* Read table definition from cache */
101
if ((share= definition::Cache::singleton().find(identifier.getKey())))
102
return foundTableShare(share);
104
share.reset(new Shared(message::Table::STANDARD, identifier));
106
if (share->open_table_def(*session, identifier))
108
in_error= share->error;
110
return Shared::shared_ptr();
112
share->incrementTableCount(); // Mark in use
114
plugin::EventObserver::registerTableEvents(*share);
116
bool ret= definition::Cache::singleton().insert(identifier.getKey(), share);
119
return Shared::shared_ptr();
126
assert(getTableCount() == 0);
130
/*****************************************************************************
131
Functions to handle table definition cach (TableShare)
132
*****************************************************************************/
135
Mark that we are not using table share anymore.
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.
146
void release(TableShare *share)
148
bool to_be_deleted= false;
149
//safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
152
if (not share->decrementTableCount())
160
definition::Cache::singleton().erase(share->getCacheKey());
164
void release(TableShare::shared_ptr &share)
166
bool to_be_deleted= false;
168
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
172
if (not share->decrementTableCount())
180
definition::Cache::singleton().erase(share->getCacheKey());
184
void release(const TableIdentifier &identifier)
186
TableShare::shared_ptr share= definition::Cache::singleton().find(identifier.getKey());
189
share->resetVersion();
190
if (share->getTableCount() == 0)
192
definition::Cache::singleton().erase(identifier.getKey());
198
} /* namespace instance */
199
} /* namespace table */
200
} /* namespace drizzled */