00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <config.h>
00022
00023 #include <drizzled/definition/cache.h>
00024 #include <drizzled/error.h>
00025 #include <drizzled/message/schema.h>
00026 #include <drizzled/plugin/event_observer.h>
00027 #include <drizzled/table/instance/shared.h>
00028 #include <drizzled/plugin/storage_engine.h>
00029
00030 namespace drizzled {
00031 namespace table {
00032 namespace instance {
00033
00034 Shared::Shared(const identifier::Table::Type type_arg,
00035 const identifier::Table &identifier,
00036 char *path_arg, uint32_t path_length_arg) :
00037 TableShare(type_arg, identifier, path_arg, path_length_arg),
00038 event_observers(NULL)
00039 {
00040 }
00041
00042 Shared::Shared(const identifier::Table &identifier,
00043 message::schema::shared_ptr schema_message) :
00044 TableShare(message::Table::STANDARD, identifier, NULL, 0),
00045 _schema(schema_message),
00046 event_observers(NULL)
00047 {
00048 }
00049
00050 Shared::Shared(const identifier::Table &identifier) :
00051 TableShare(identifier, identifier.getKey()),
00052 event_observers(NULL)
00053 {
00054 }
00055
00056 bool Shared::is_replicated() const
00057 {
00058 if (_schema)
00059 {
00060 if (not message::is_replicated(*_schema))
00061 return false;
00062 }
00063
00064 assert(getTableMessage());
00065 return message::is_replicated(*getTableMessage());
00066 }
00067
00068
00069 Shared::shared_ptr Shared::foundTableShare(Shared::shared_ptr share)
00070 {
00071
00072
00073
00074
00075 if (share->error)
00076 {
00077
00078 share->open_table_error(share->error, share->open_errno, share->errarg);
00079
00080 return Shared::shared_ptr();
00081 }
00082
00083 share->incrementTableCount();
00084
00085 return share;
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 Shared::shared_ptr Shared::make_shared(Session *session,
00114 const identifier::Table &identifier,
00115 int &in_error)
00116 {
00117 Shared::shared_ptr share;
00118
00119 in_error= 0;
00120
00121
00122 if ((share= definition::Cache::find(identifier.getKey())))
00123 return foundTableShare(share);
00124
00125 drizzled::message::schema::shared_ptr schema_message_ptr= plugin::StorageEngine::getSchemaDefinition(identifier);
00126
00127 if (not schema_message_ptr)
00128 {
00129 drizzled::my_error(ER_SCHEMA_DOES_NOT_EXIST, identifier);
00130 return Shared::shared_ptr();
00131 }
00132
00133 share.reset(new Shared(identifier, schema_message_ptr));
00134
00135 if (share->open_table_def(*session, identifier))
00136 {
00137 in_error= share->error;
00138
00139 return Shared::shared_ptr();
00140 }
00141 share->incrementTableCount();
00142
00143 plugin::EventObserver::registerTableEvents(*share);
00144
00145 bool ret= definition::Cache::insert(identifier.getKey(), share);
00146
00147 if (not ret)
00148 {
00149 drizzled::my_error(ER_UNKNOWN_ERROR);
00150 return Shared::shared_ptr();
00151 }
00152
00153 return share;
00154 }
00155
00156 Shared::~Shared()
00157 {
00158 assert(getTableCount() == 0);
00159 plugin::EventObserver::deregisterTableEvents(*this);
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 void release(TableShare *share)
00180 {
00181 bool to_be_deleted= false;
00182
00183
00184 share->lock();
00185 if (not share->decrementTableCount())
00186 {
00187 to_be_deleted= true;
00188 }
00189 share->unlock();
00190
00191 if (to_be_deleted)
00192 {
00193 definition::Cache::erase(share->getCacheKey());
00194 }
00195 }
00196
00197 void release(TableShare::shared_ptr &share)
00198 {
00199 bool to_be_deleted= false;
00200 #if 0
00201 safe_mutex_assert_owner(table::Cache::mutex().native_handle);
00202 #endif
00203
00204 share->lock();
00205 if (not share->decrementTableCount())
00206 {
00207 to_be_deleted= true;
00208 }
00209 share->unlock();
00210
00211 if (to_be_deleted)
00212 {
00213 definition::Cache::erase(share->getCacheKey());
00214 }
00215 }
00216
00217 void release(const identifier::Table &identifier)
00218 {
00219 TableShare::shared_ptr share= definition::Cache::find(identifier.getKey());
00220 if (share)
00221 {
00222 share->resetVersion();
00223 if (share->getTableCount() == 0)
00224 {
00225 definition::Cache::erase(identifier.getKey());
00226 }
00227 }
00228 }
00229
00230
00231 }
00232 }
00233 }