~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/schema_engine/schema.cc

  • Committer: Daniel Nichter
  • Date: 2011-10-23 16:01:37 UTC
  • mto: This revision was merged to the branch mainline in revision 2448.
  • Revision ID: daniel@percona.com-20111023160137-7ac3blgz8z4tf8za
Add Administration Getting Started and Logging.  Capitalize SQL clause keywords.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2010 Brian Aker
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
 
 
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>
 
30
 
 
31
#include <drizzled/pthread_globals.h>
 
32
 
 
33
#include <drizzled/execute.h>
 
34
 
 
35
#include <drizzled/internal/my_sys.h>
 
36
#include <drizzled/cached_directory.h>
 
37
 
 
38
#include <fcntl.h>
 
39
#include <sys/stat.h>
 
40
#include <sys/types.h>
 
41
 
 
42
#include <boost/foreach.hpp>
 
43
#include <google/protobuf/io/zero_copy_stream.h>
 
44
#include <google/protobuf/io/zero_copy_stream_impl.h>
 
45
 
 
46
#include <iostream>
 
47
#include <fstream>
 
48
#include <string>
 
49
 
 
50
using namespace std;
 
51
using namespace drizzled;
 
52
 
 
53
const char* MY_DB_OPT_FILE= "db.opt";
 
54
const char* DEFAULT_FILE_EXTENSION= ".dfe"; // Deep Fried Elephant
 
55
 
 
56
static const char* g_schema_exts[] = 
 
57
{
 
58
  NULL
 
59
};
 
60
 
 
61
Schema::Schema() :
 
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)
 
68
{
 
69
  table_definition_ext= DEFAULT_FILE_EXTENSION;
 
70
}
 
71
 
 
72
void Schema::prime()
 
73
{
 
74
  CachedDirectory directory(getDataHomeCatalog().file_string(), CachedDirectory::DIRECTORY);
 
75
  CachedDirectory::Entries files= directory.getEntries();
 
76
  boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
 
77
 
 
78
  BOOST_FOREACH(CachedDirectory::Entries::reference entry, files)
 
79
  {
 
80
    if (not entry->filename.compare(GLOBAL_TEMPORARY_EXT))
 
81
      continue;
 
82
    message::Schema schema_message;
 
83
    if (readSchemaFile(entry->filename, schema_message))
 
84
    {
 
85
      identifier::Schema schema_identifier(schema_message.name());
 
86
 
 
87
      pair<SchemaCache::iterator, bool> ret=
 
88
        schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
 
89
 
 
90
      assert(ret.second); // If this has happened, something really bad is going down.
 
91
    }
 
92
  }
 
93
}
 
94
 
 
95
void Schema::doGetSchemaIdentifiers(identifier::schema::vector &set_of_names)
 
96
{
 
97
  mutex.lock_shared();
 
98
  BOOST_FOREACH(SchemaCache::reference iter, schema_cache)
 
99
    set_of_names.push_back(iter.second->name());
 
100
  mutex.unlock_shared();
 
101
}
 
102
 
 
103
drizzled::message::schema::shared_ptr Schema::doGetSchemaDefinition(const identifier::Schema &schema_identifier)
 
104
{
 
105
  mutex.lock_shared();
 
106
  SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
 
107
  if (iter != schema_cache.end())
 
108
  {
 
109
    drizzled::message::schema::shared_ptr schema_message= iter->second;
 
110
    mutex.unlock_shared();
 
111
    return schema_message;
 
112
  }
 
113
  mutex.unlock_shared();
 
114
  return drizzled::message::schema::shared_ptr();
 
115
}
 
116
 
 
117
 
 
118
bool Schema::doCreateSchema(const drizzled::message::Schema &schema_message)
 
119
{
 
120
  identifier::Schema schema_identifier(schema_message.name());
 
121
 
 
122
  if (mkdir(schema_identifier.getPath().c_str(), 0777) == -1)
 
123
  {
 
124
    sql_perror(schema_identifier.getPath().c_str());
 
125
    return false;
 
126
  }
 
127
 
 
128
  if (not writeSchemaFile(schema_identifier, schema_message))
 
129
  {
 
130
    rmdir(schema_identifier.getPath().c_str());
 
131
 
 
132
    return false;
 
133
  }
 
134
 
 
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)));
 
138
 
 
139
  assert(ret.second); // If this has happened, something really bad is going down.
 
140
  return true;
 
141
}
 
142
 
 
143
bool Schema::doDropSchema(const identifier::Schema &schema_identifier)
 
144
{
 
145
  string schema_file(schema_identifier.getPath());
 
146
  schema_file.append(1, FN_LIBCHAR);
 
147
  schema_file.append(MY_DB_OPT_FILE);
 
148
 
 
149
  if (not doGetSchemaDefinition(schema_identifier))
 
150
    return false;
 
151
 
 
152
  // No db.opt file, no love from us.
 
153
  if (access(schema_file.c_str(), F_OK))
 
154
  {
 
155
    sql_perror(schema_file.c_str());
 
156
    return false;
 
157
  }
 
158
 
 
159
  if (unlink(schema_file.c_str()))
 
160
  {
 
161
    sql_perror(schema_file.c_str());
 
162
    return false;
 
163
  }
 
164
 
 
165
  if (rmdir(schema_identifier.getPath().c_str()))
 
166
  {
 
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());
 
171
    cerr << dir;
 
172
  }
 
173
 
 
174
  boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
 
175
  schema_cache.erase(schema_identifier.getPath());
 
176
 
 
177
  return true;
 
178
}
 
179
 
 
180
bool Schema::doAlterSchema(const drizzled::message::Schema &schema_message)
 
181
{
 
182
  identifier::Schema schema_identifier(schema_message.name());
 
183
 
 
184
  if (access(schema_identifier.getPath().c_str(), F_OK))
 
185
    return false;
 
186
 
 
187
  if (writeSchemaFile(schema_identifier, schema_message))
 
188
  {
 
189
    boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
 
190
    schema_cache.erase(schema_identifier.getPath());
 
191
 
 
192
    pair<SchemaCache::iterator, bool> ret=
 
193
      schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
 
194
 
 
195
    assert(ret.second); // If this has happened, something really bad is going down.
 
196
  }
 
197
 
 
198
  return true;
 
199
}
 
200
 
 
201
/**
 
202
  path is path to database, not schema file 
 
203
 
 
204
  @note we do the rename to make it crash safe.
 
205
*/
 
206
bool Schema::writeSchemaFile(const identifier::Schema &schema_identifier, const message::Schema &db)
 
207
{
 
208
  char schema_file_tmp[FN_REFLEN];
 
209
  string schema_file(schema_identifier.getPath());
 
210
 
 
211
 
 
212
  schema_file.append(1, FN_LIBCHAR);
 
213
  schema_file.append(MY_DB_OPT_FILE);
 
214
 
 
215
  snprintf(schema_file_tmp, FN_REFLEN, "%sXXXXXX", schema_file.c_str());
 
216
 
 
217
  int fd= mkstemp(schema_file_tmp);
 
218
 
 
219
  if (fd == -1)
 
220
  {
 
221
    sql_perror(schema_file_tmp);
 
222
 
 
223
    return false;
 
224
  }
 
225
 
 
226
  bool success;
 
227
 
 
228
  try {
 
229
    success= db.SerializeToFileDescriptor(fd);
 
230
  }
 
231
  catch (...)
 
232
  {
 
233
    success= false;
 
234
  }
 
235
 
 
236
  if (not success)
 
237
  {
 
238
    my_error(ER_CORRUPT_SCHEMA_DEFINITION, MYF(0), schema_file.c_str(),
 
239
             db.InitializationErrorString().empty() ? "unknown" :  db.InitializationErrorString().c_str());
 
240
 
 
241
    if (close(fd) == -1)
 
242
      sql_perror(schema_file_tmp);
 
243
 
 
244
    if (unlink(schema_file_tmp))
 
245
      sql_perror(schema_file_tmp);
 
246
 
 
247
    return false;
 
248
  }
 
249
 
 
250
  if (close(fd) == -1)
 
251
  {
 
252
    sql_perror(schema_file_tmp);
 
253
 
 
254
    if (unlink(schema_file_tmp))
 
255
      sql_perror(schema_file_tmp);
 
256
 
 
257
    return false;
 
258
  }
 
259
 
 
260
  if (rename(schema_file_tmp, schema_file.c_str()) == -1)
 
261
  {
 
262
    if (unlink(schema_file_tmp))
 
263
      sql_perror(schema_file_tmp);
 
264
 
 
265
    return false;
 
266
  }
 
267
 
 
268
  return true;
 
269
}
 
270
 
 
271
 
 
272
bool Schema::readSchemaFile(const drizzled::identifier::Schema &schema_identifier, drizzled::message::Schema &schema)
 
273
{
 
274
  return readSchemaFile(schema_identifier.getPath(), schema); 
 
275
}
 
276
 
 
277
bool Schema::readSchemaFile(std::string db_opt_path, drizzled::message::Schema &schema)
 
278
{
 
279
  /*
 
280
    Pass an empty file name, and the database options file name as extension
 
281
    to avoid table name to file name encoding.
 
282
  */
 
283
  db_opt_path.append(1, FN_LIBCHAR);
 
284
  db_opt_path.append(MY_DB_OPT_FILE);
 
285
 
 
286
  fstream input(db_opt_path.c_str(), ios::in | ios::binary);
 
287
 
 
288
  /**
 
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.
 
292
  */
 
293
  if (input.good())
 
294
  {
 
295
    if (schema.ParseFromIstream(&input))
 
296
    {
 
297
      return true;
 
298
    }
 
299
 
 
300
    my_error(ER_CORRUPT_SCHEMA_DEFINITION, MYF(0), db_opt_path.c_str(),
 
301
             schema.InitializationErrorString().empty() ? "unknown" :  schema.InitializationErrorString().c_str());
 
302
  }
 
303
  else
 
304
  {
 
305
    sql_perror(db_opt_path.c_str());
 
306
  }
 
307
 
 
308
  return false;
 
309
}
 
310
 
 
311
void Schema::doGetTableIdentifiers(drizzled::CachedDirectory&,
 
312
                                   const drizzled::identifier::Schema&,
 
313
                                   drizzled::identifier::table::vector&)
 
314
{
 
315
}
 
316
 
 
317
const char** Schema::bas_ext() const
 
318
{
 
319
  return g_schema_exts;
 
320
}