58
62
schema_cache_filled(false)
60
64
table_definition_ext= DEFAULT_FILE_EXTENSION;
61
pthread_rwlock_init(&schema_lock, NULL);
67
pthread_rwlock_destroy(&schema_lock);
70
71
void Schema::prime()
72
CachedDirectory directory(drizzle_data_home, CachedDirectory::DIRECTORY);
73
CachedDirectory directory(getDataHomeCatalog().file_string(), CachedDirectory::DIRECTORY);
73
74
CachedDirectory::Entries files= directory.getEntries();
75
pthread_rwlock_wrlock(&schema_lock);
75
boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
77
77
for (CachedDirectory::Entries::iterator fileIter= files.begin();
78
78
fileIter != files.end(); fileIter++)
80
80
CachedDirectory::Entry *entry= *fileIter;
81
81
message::Schema schema_message;
83
if (not entry->filename.compare(GLOBAL_TEMPORARY_EXT))
83
86
if (readSchemaFile(entry->filename, schema_message))
85
SchemaIdentifier schema_identifier(schema_message.name());
88
identifier::Schema schema_identifier(schema_message.name());
87
90
pair<SchemaCache::iterator, bool> ret=
88
schema_cache.insert(make_pair(schema_identifier.getPath(), schema_message));
91
schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
90
93
if (ret.second == false)
92
95
abort(); // If this has happened, something really bad is going down.
96
pthread_rwlock_unlock(&schema_lock);
99
void Schema::doGetSchemaIdentifiers(SchemaIdentifierList &set_of_names)
101
if (not pthread_rwlock_rdlock(&schema_lock))
101
void Schema::startup(drizzled::Session &)
105
void Schema::doGetSchemaIdentifiers(identifier::Schema::vector &set_of_names)
103
109
for (SchemaCache::iterator iter= schema_cache.begin();
104
110
iter != schema_cache.end();
107
set_of_names.push_back(SchemaIdentifier((*iter).second.name()));
113
set_of_names.push_back(identifier::Schema((*iter).second->name()));
109
pthread_rwlock_unlock(&schema_lock);
114
// If for some reason getting a lock should fail, we resort to disk
116
CachedDirectory directory(drizzle_data_home, CachedDirectory::DIRECTORY);
118
CachedDirectory::Entries files= directory.getEntries();
120
for (CachedDirectory::Entries::iterator fileIter= files.begin();
121
fileIter != files.end(); fileIter++)
123
CachedDirectory::Entry *entry= *fileIter;
124
set_of_names.push_back(entry->filename);
116
mutex.unlock_shared();
128
bool Schema::doGetSchemaDefinition(SchemaIdentifier &schema_identifier, message::Schema &schema_message)
119
bool Schema::doGetSchemaDefinition(const identifier::Schema &schema_identifier, message::schema::shared_ptr &schema_message)
130
if (not pthread_rwlock_rdlock(&schema_lock))
122
SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
124
if (iter != schema_cache.end())
132
SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
134
if (iter != schema_cache.end())
136
schema_message.CopyFrom(((*iter).second));
137
pthread_rwlock_unlock(&schema_lock);
140
pthread_rwlock_unlock(&schema_lock);
126
schema_message= (*iter).second;
127
mutex.unlock_shared();
131
mutex.unlock_shared();
145
// Fail to disk based means
146
return readSchemaFile(schema_identifier.getPath(), schema_message);
149
137
bool Schema::doCreateSchema(const drizzled::message::Schema &schema_message)
151
SchemaIdentifier schema_identifier(schema_message.name());
139
identifier::Schema schema_identifier(schema_message.name());
153
141
if (mkdir(schema_identifier.getPath().c_str(), 0777) == -1)
143
sql_perror(schema_identifier.getPath().c_str());
156
147
if (not writeSchemaFile(schema_identifier, schema_message))
163
if (not pthread_rwlock_wrlock(&schema_lock))
165
pair<SchemaCache::iterator, bool> ret=
166
schema_cache.insert(make_pair(schema_identifier.getPath(), schema_message));
169
if (ret.second == false)
171
abort(); // If this has happened, something really bad is going down.
173
pthread_rwlock_unlock(&schema_lock);
155
boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
156
pair<SchemaCache::iterator, bool> ret=
157
schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
160
if (ret.second == false)
162
abort(); // If this has happened, something really bad is going down.
179
bool Schema::doDropSchema(SchemaIdentifier &schema_identifier)
169
bool Schema::doDropSchema(const identifier::Schema &schema_identifier)
181
message::Schema schema_message;
171
message::schema::shared_ptr schema_message;
183
173
string schema_file(schema_identifier.getPath());
184
174
schema_file.append(1, FN_LIBCHAR);
190
180
// No db.opt file, no love from us.
191
181
if (access(schema_file.c_str(), F_OK))
193
perror(schema_file.c_str());
183
sql_perror(schema_file.c_str());
197
187
if (unlink(schema_file.c_str()))
199
perror(schema_file.c_str());
189
sql_perror(schema_file.c_str());
203
193
if (rmdir(schema_identifier.getPath().c_str()))
205
perror(schema_identifier.getPath().c_str());
195
sql_perror(schema_identifier.getPath().c_str());
206
196
//@todo If this happens, we want a report of it. For the moment I dump
207
197
//to stderr so I can catch it in Hudson.
208
198
CachedDirectory dir(schema_identifier.getPath());
212
if (not pthread_rwlock_wrlock(&schema_lock))
214
schema_cache.erase(schema_identifier.getPath());
215
pthread_rwlock_unlock(&schema_lock);
202
boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
203
schema_cache.erase(schema_identifier.getPath());
221
208
bool Schema::doAlterSchema(const drizzled::message::Schema &schema_message)
223
SchemaIdentifier schema_identifier(schema_message.name());
210
identifier::Schema schema_identifier(schema_message.name());
225
212
if (access(schema_identifier.getPath().c_str(), F_OK))
228
215
if (writeSchemaFile(schema_identifier, schema_message))
230
if (not pthread_rwlock_wrlock(&schema_lock))
232
schema_cache.erase(schema_identifier.getPath());
234
pair<SchemaCache::iterator, bool> ret=
235
schema_cache.insert(make_pair(schema_identifier.getPath(), schema_message));
237
if (ret.second == false)
239
abort(); // If this has happened, something really bad is going down.
242
pthread_rwlock_unlock(&schema_lock);
246
abort(); // This would leave us out of sync, suck.
217
boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
218
schema_cache.erase(schema_identifier.getPath());
220
pair<SchemaCache::iterator, bool> ret=
221
schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
223
if (ret.second == false)
225
abort(); // If this has happened, something really bad is going down.
273
perror(schema_file_tmp);
252
sql_perror(schema_file_tmp);
278
if (not db.SerializeToFileDescriptor(fd))
280
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
281
db.InitializationErrorString().c_str());
260
success= db.SerializeToFileDescriptor(fd);
269
my_error(ER_CORRUPT_SCHEMA_DEFINITION, MYF(0), schema_file.c_str(),
270
db.InitializationErrorString().empty() ? "unknown" : db.InitializationErrorString().c_str());
283
272
if (close(fd) == -1)
284
perror(schema_file_tmp);
273
sql_perror(schema_file_tmp);
286
275
if (unlink(schema_file_tmp))
287
perror(schema_file_tmp);
276
sql_perror(schema_file_tmp);
292
281
if (close(fd) == -1)
294
perror(schema_file_tmp);
283
sql_perror(schema_file_tmp);
296
285
if (unlink(schema_file_tmp))
297
perror(schema_file_tmp);
286
sql_perror(schema_file_tmp);
314
bool Schema::readSchemaFile(const std::string &schema_file_name, drizzled::message::Schema &schema_message)
303
bool Schema::readSchemaFile(const drizzled::identifier::Schema &schema_identifier, drizzled::message::Schema &schema)
316
string db_opt_path(schema_file_name);
305
return readSchemaFile(schema_identifier.getPath(), schema);
308
bool Schema::readSchemaFile(std::string db_opt_path, drizzled::message::Schema &schema)
319
311
Pass an empty file name, and the database options file name as extension
320
312
to avoid table name to file name encoding.
332
324
if (input.good())
334
if (schema_message.ParseFromIstream(&input))
326
if (schema.ParseFromIstream(&input))
339
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
340
schema_message.InitializationErrorString().c_str());
331
my_error(ER_CORRUPT_SCHEMA_DEFINITION, MYF(0), db_opt_path.c_str(),
332
schema.InitializationErrorString().empty() ? "unknown" : schema.InitializationErrorString().c_str());
344
perror(db_opt_path.c_str());
336
sql_perror(db_opt_path.c_str());
350
bool Schema::doCanCreateTable(drizzled::TableIdentifier &identifier)
352
if (static_cast<SchemaIdentifier&>(identifier) == TEMPORARY_IDENTIFIER)
360
342
void Schema::doGetTableIdentifiers(drizzled::CachedDirectory&,
361
drizzled::SchemaIdentifier&,
362
drizzled::TableIdentifiers&)
343
const drizzled::identifier::Schema&,
344
drizzled::identifier::Table::vector&)