18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
#include "plugin/schema_engine/schema.h"
24
#include "drizzled/db.h"
25
#include "drizzled/sql_table.h"
26
#include "drizzled/global_charset_info.h"
27
#include "drizzled/charset.h"
28
#include "drizzled/charset_info.h"
29
#include "drizzled/cursor.h"
30
#include "drizzled/data_home.h"
32
#include "drizzled/internal/my_sys.h"
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>
35
39
#include <sys/stat.h>
36
40
#include <sys/types.h>
42
#include <boost/foreach.hpp>
38
43
#include <google/protobuf/io/zero_copy_stream.h>
39
44
#include <google/protobuf/io/zero_copy_stream_impl.h>
45
50
using namespace std;
46
51
using namespace drizzled;
49
#define MY_DB_OPT_FILE "db.opt"
50
#define DEFAULT_FILE_EXTENSION ".dfe" // Deep Fried Elephant
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[] =
53
62
drizzled::plugin::StorageEngine("schema",
54
63
HTON_ALTER_NOT_SUPPORTED |
55
64
HTON_HAS_SCHEMA_DICTIONARY |
60
69
table_definition_ext= DEFAULT_FILE_EXTENSION;
67
72
void Schema::prime()
69
74
CachedDirectory directory(getDataHomeCatalog().file_string(), CachedDirectory::DIRECTORY);
70
75
CachedDirectory::Entries files= directory.getEntries();
74
for (CachedDirectory::Entries::iterator fileIter= files.begin();
75
fileIter != files.end(); fileIter++)
76
boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
78
BOOST_FOREACH(CachedDirectory::Entries::reference entry, files)
77
CachedDirectory::Entry *entry= *fileIter;
78
message::Schema schema_message;
80
80
if (not entry->filename.compare(GLOBAL_TEMPORARY_EXT))
83
SchemaIdentifier filename(entry->filename);
84
if (readSchemaFile(filename, schema_message))
82
message::Schema schema_message;
83
if (readSchemaFile(entry->filename, schema_message))
86
SchemaIdentifier schema_identifier(schema_message.name());
85
identifier::Schema schema_identifier(schema_message.name());
88
87
pair<SchemaCache::iterator, bool> ret=
89
88
schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
91
if (ret.second == false)
93
abort(); // If this has happened, something really bad is going down.
90
assert(ret.second); // If this has happened, something really bad is going down.
100
void Schema::doGetSchemaIdentifiers(SchemaIdentifier::vector &set_of_names)
95
void Schema::doGetSchemaIdentifiers(identifier::schema::vector &set_of_names)
102
97
mutex.lock_shared();
104
for (SchemaCache::iterator iter= schema_cache.begin();
105
iter != schema_cache.end();
108
set_of_names.push_back(SchemaIdentifier((*iter).second->name()));
98
BOOST_FOREACH(SchemaCache::reference iter, schema_cache)
99
set_of_names.push_back(iter.second->name());
111
100
mutex.unlock_shared();
114
bool Schema::doGetSchemaDefinition(const SchemaIdentifier &schema_identifier, message::schema::shared_ptr &schema_message)
103
drizzled::message::schema::shared_ptr Schema::doGetSchemaDefinition(const identifier::Schema &schema_identifier)
116
105
mutex.lock_shared();
117
106
SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
119
107
if (iter != schema_cache.end())
121
schema_message= (*iter).second;
109
drizzled::message::schema::shared_ptr schema_message= iter->second;
122
110
mutex.unlock_shared();
111
return schema_message;
125
113
mutex.unlock_shared();
114
return drizzled::message::schema::shared_ptr();
131
118
bool Schema::doCreateSchema(const drizzled::message::Schema &schema_message)
133
SchemaIdentifier schema_identifier(schema_message.name());
120
identifier::Schema schema_identifier(schema_message.name());
135
122
if (mkdir(schema_identifier.getPath().c_str(), 0777) == -1)
124
sql_perror(schema_identifier.getPath().c_str());
138
128
if (not writeSchemaFile(schema_identifier, schema_message))
147
pair<SchemaCache::iterator, bool> ret=
148
schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
151
if (ret.second == false)
153
abort(); // If this has happened, something really bad is going down.
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.
161
bool Schema::doDropSchema(const SchemaIdentifier &schema_identifier)
143
bool Schema::doDropSchema(const identifier::Schema &schema_identifier)
163
message::schema::shared_ptr schema_message;
165
145
string schema_file(schema_identifier.getPath());
166
146
schema_file.append(1, FN_LIBCHAR);
167
147
schema_file.append(MY_DB_OPT_FILE);
169
if (not doGetSchemaDefinition(schema_identifier, schema_message))
149
if (not doGetSchemaDefinition(schema_identifier))
172
152
// No db.opt file, no love from us.
173
153
if (access(schema_file.c_str(), F_OK))
175
perror(schema_file.c_str());
155
sql_perror(schema_file.c_str());
179
159
if (unlink(schema_file.c_str()))
181
perror(schema_file.c_str());
161
sql_perror(schema_file.c_str());
185
165
if (rmdir(schema_identifier.getPath().c_str()))
187
perror(schema_identifier.getPath().c_str());
167
sql_perror(schema_identifier.getPath().c_str());
188
168
//@todo If this happens, we want a report of it. For the moment I dump
189
169
//to stderr so I can catch it in Hudson.
190
170
CachedDirectory dir(schema_identifier.getPath());
174
boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
195
175
schema_cache.erase(schema_identifier.getPath());
201
180
bool Schema::doAlterSchema(const drizzled::message::Schema &schema_message)
203
SchemaIdentifier schema_identifier(schema_message.name());
182
identifier::Schema schema_identifier(schema_message.name());
205
184
if (access(schema_identifier.getPath().c_str(), F_OK))
208
187
if (writeSchemaFile(schema_identifier, schema_message))
212
schema_cache.erase(schema_identifier.getPath());
214
pair<SchemaCache::iterator, bool> ret=
215
schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
217
if (ret.second == false)
219
abort(); // If this has happened, something really bad is going down.
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.
299
bool Schema::readSchemaFile(const drizzled::SchemaIdentifier &schema_identifier, drizzled::message::Schema &schema)
272
bool Schema::readSchemaFile(const drizzled::identifier::Schema &schema_identifier, drizzled::message::Schema &schema)
301
string db_opt_path(schema_identifier.getPath());
274
return readSchemaFile(schema_identifier.getPath(), schema);
277
bool Schema::readSchemaFile(std::string db_opt_path, drizzled::message::Schema &schema)
304
280
Pass an empty file name, and the database options file name as extension
305
281
to avoid table name to file name encoding.
329
perror(db_opt_path.c_str());
305
sql_perror(db_opt_path.c_str());
335
311
void Schema::doGetTableIdentifiers(drizzled::CachedDirectory&,
336
const drizzled::SchemaIdentifier&,
337
drizzled::TableIdentifier::vector&)
312
const drizzled::identifier::Schema&,
313
drizzled::identifier::table::vector&)
317
const char** Schema::bas_ext() const
319
return g_schema_exts;