1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 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/error.h>
24
#include <plugin/schema_engine/schema.h>
25
#include <drizzled/schema.h>
26
#include <drizzled/sql_table.h>
27
#include <drizzled/charset.h>
28
#include <drizzled/cursor.h>
29
#include <drizzled/data_home.h>
31
#include <drizzled/pthread_globals.h>
33
#include <drizzled/execute.h>
35
#include <drizzled/internal/my_sys.h>
36
#include <drizzled/cached_directory.h>
40
#include <sys/types.h>
42
#include <boost/foreach.hpp>
43
#include <google/protobuf/io/zero_copy_stream.h>
44
#include <google/protobuf/io/zero_copy_stream_impl.h>
51
using namespace drizzled;
53
const char* MY_DB_OPT_FILE= "db.opt";
54
const char* DEFAULT_FILE_EXTENSION= ".dfe"; // Deep Fried Elephant
56
static const char* g_schema_exts[] =
62
drizzled::plugin::StorageEngine("schema",
63
HTON_ALTER_NOT_SUPPORTED |
64
HTON_HAS_SCHEMA_DICTIONARY |
65
HTON_SKIP_STORE_LOCK |
66
HTON_TEMPORARY_NOT_SUPPORTED),
67
schema_cache_filled(false)
69
table_definition_ext= DEFAULT_FILE_EXTENSION;
74
CachedDirectory directory(getDataHomeCatalog().file_string(), CachedDirectory::DIRECTORY);
75
CachedDirectory::Entries files= directory.getEntries();
76
boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
78
BOOST_FOREACH(CachedDirectory::Entries::reference entry, files)
80
if (not entry->filename.compare(GLOBAL_TEMPORARY_EXT))
82
message::Schema schema_message;
83
if (readSchemaFile(entry->filename, schema_message))
85
identifier::Schema schema_identifier(schema_message.name());
87
pair<SchemaCache::iterator, bool> ret=
88
schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
90
assert(ret.second); // If this has happened, something really bad is going down.
95
void Schema::doGetSchemaIdentifiers(identifier::schema::vector &set_of_names)
98
BOOST_FOREACH(SchemaCache::reference iter, schema_cache)
99
set_of_names.push_back(iter.second->name());
100
mutex.unlock_shared();
103
drizzled::message::schema::shared_ptr Schema::doGetSchemaDefinition(const identifier::Schema &schema_identifier)
106
SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
107
if (iter != schema_cache.end())
109
drizzled::message::schema::shared_ptr schema_message= iter->second;
110
mutex.unlock_shared();
111
return schema_message;
113
mutex.unlock_shared();
114
return drizzled::message::schema::shared_ptr();
118
bool Schema::doCreateSchema(const drizzled::message::Schema &schema_message)
120
identifier::Schema schema_identifier(schema_message.name());
122
if (mkdir(schema_identifier.getPath().c_str(), 0777) == -1)
124
sql_perror(schema_identifier.getPath().c_str());
128
if (not writeSchemaFile(schema_identifier, schema_message))
130
rmdir(schema_identifier.getPath().c_str());
135
boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
136
pair<SchemaCache::iterator, bool> ret=
137
schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
139
assert(ret.second); // If this has happened, something really bad is going down.
143
bool Schema::doDropSchema(const identifier::Schema &schema_identifier)
145
string schema_file(schema_identifier.getPath());
146
schema_file.append(1, FN_LIBCHAR);
147
schema_file.append(MY_DB_OPT_FILE);
149
if (not doGetSchemaDefinition(schema_identifier))
152
// No db.opt file, no love from us.
153
if (access(schema_file.c_str(), F_OK))
155
sql_perror(schema_file.c_str());
159
if (unlink(schema_file.c_str()))
161
sql_perror(schema_file.c_str());
165
if (rmdir(schema_identifier.getPath().c_str()))
167
sql_perror(schema_identifier.getPath().c_str());
168
//@todo If this happens, we want a report of it. For the moment I dump
169
//to stderr so I can catch it in Hudson.
170
CachedDirectory dir(schema_identifier.getPath());
174
boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
175
schema_cache.erase(schema_identifier.getPath());
180
bool Schema::doAlterSchema(const drizzled::message::Schema &schema_message)
182
identifier::Schema schema_identifier(schema_message.name());
184
if (access(schema_identifier.getPath().c_str(), F_OK))
187
if (writeSchemaFile(schema_identifier, schema_message))
189
boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
190
schema_cache.erase(schema_identifier.getPath());
192
pair<SchemaCache::iterator, bool> ret=
193
schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
195
assert(ret.second); // If this has happened, something really bad is going down.
202
path is path to database, not schema file
204
@note we do the rename to make it crash safe.
206
bool Schema::writeSchemaFile(const identifier::Schema &schema_identifier, const message::Schema &db)
208
char schema_file_tmp[FN_REFLEN];
209
string schema_file(schema_identifier.getPath());
212
schema_file.append(1, FN_LIBCHAR);
213
schema_file.append(MY_DB_OPT_FILE);
215
snprintf(schema_file_tmp, FN_REFLEN, "%sXXXXXX", schema_file.c_str());
217
int fd= mkstemp(schema_file_tmp);
221
sql_perror(schema_file_tmp);
229
success= db.SerializeToFileDescriptor(fd);
238
my_error(ER_CORRUPT_SCHEMA_DEFINITION, MYF(0), schema_file.c_str(),
239
db.InitializationErrorString().empty() ? "unknown" : db.InitializationErrorString().c_str());
242
sql_perror(schema_file_tmp);
244
if (unlink(schema_file_tmp))
245
sql_perror(schema_file_tmp);
252
sql_perror(schema_file_tmp);
254
if (unlink(schema_file_tmp))
255
sql_perror(schema_file_tmp);
260
if (rename(schema_file_tmp, schema_file.c_str()) == -1)
262
if (unlink(schema_file_tmp))
263
sql_perror(schema_file_tmp);
272
bool Schema::readSchemaFile(const drizzled::identifier::Schema &schema_identifier, drizzled::message::Schema &schema)
274
return readSchemaFile(schema_identifier.getPath(), schema);
277
bool Schema::readSchemaFile(std::string db_opt_path, drizzled::message::Schema &schema)
280
Pass an empty file name, and the database options file name as extension
281
to avoid table name to file name encoding.
283
db_opt_path.append(1, FN_LIBCHAR);
284
db_opt_path.append(MY_DB_OPT_FILE);
286
fstream input(db_opt_path.c_str(), ios::in | ios::binary);
289
@note If parsing fails, either someone has done a "mkdir" or has deleted their opt file.
290
So what do we do? We muddle through the adventure by generating
291
one with a name in it, and the charset set to the default.
295
if (schema.ParseFromIstream(&input))
300
my_error(ER_CORRUPT_SCHEMA_DEFINITION, MYF(0), db_opt_path.c_str(),
301
schema.InitializationErrorString().empty() ? "unknown" : schema.InitializationErrorString().c_str());
305
sql_perror(db_opt_path.c_str());
311
void Schema::doGetTableIdentifiers(drizzled::CachedDirectory&,
312
const drizzled::identifier::Schema&,
313
drizzled::identifier::table::vector&)
317
const char** Schema::bas_ext() const
319
return g_schema_exts;