~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/schema_engine/schema.cc

  • Committer: Joe Daly
  • Date: 2010-04-29 03:08:04 UTC
  • mto: This revision was merged to the branch mainline in revision 1523.
  • Revision ID: skinny.moey@gmail.com-20100429030804-ppssp19xwyrgm5of
remove com_stat_vars and rework lock in scoreboard to not lock on Sessions locating a slot they previously owned

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include <config.h>
22
 
 
23
 
#include <plugin/schema_engine/schema.h>
24
 
#include <drizzled/schema.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>
31
 
 
32
 
#include <drizzled/pthread_globals.h>
33
 
 
34
 
#include <drizzled/execute.h>
35
 
 
36
 
#include <drizzled/internal/my_sys.h>
 
21
#include "config.h"
 
22
 
 
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
 
 
31
#include "drizzled/internal/my_sys.h"
37
32
 
38
33
#include <fcntl.h>
39
34
#include <sys/stat.h>
49
44
using namespace std;
50
45
using namespace drizzled;
51
46
 
 
47
static SchemaIdentifier TEMPORARY_IDENTIFIER("TEMPORARY");
52
48
 
53
49
#define MY_DB_OPT_FILE "db.opt"
54
50
#define DEFAULT_FILE_EXTENSION ".dfe" // Deep Fried Elephant
62
58
  schema_cache_filled(false)
63
59
{
64
60
  table_definition_ext= DEFAULT_FILE_EXTENSION;
 
61
  pthread_rwlock_init(&schema_lock, NULL);
 
62
  prime();
65
63
}
66
64
 
67
65
Schema::~Schema()
68
66
{
 
67
  pthread_rwlock_destroy(&schema_lock);
69
68
}
70
69
 
71
70
void Schema::prime()
72
71
{
73
 
  CachedDirectory directory(getDataHomeCatalog().file_string(), CachedDirectory::DIRECTORY);
 
72
  CachedDirectory directory(data_home, CachedDirectory::DIRECTORY);
74
73
  CachedDirectory::Entries files= directory.getEntries();
75
 
  boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
 
74
 
 
75
  pthread_rwlock_wrlock(&schema_lock);
76
76
 
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;
82
82
 
83
 
    if (not entry->filename.compare(GLOBAL_TEMPORARY_EXT))
84
 
      continue;
85
 
 
86
83
    if (readSchemaFile(entry->filename, schema_message))
87
84
    {
88
 
      identifier::Schema schema_identifier(schema_message.name());
 
85
      SchemaIdentifier schema_identifier(schema_message.name());
89
86
 
90
87
      pair<SchemaCache::iterator, bool> ret=
91
 
        schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
 
88
        schema_cache.insert(make_pair(schema_identifier.getPath(), schema_message));
92
89
 
93
90
      if (ret.second == false)
94
 
      {
 
91
     {
95
92
        abort(); // If this has happened, something really bad is going down.
96
93
      }
97
94
    }
98
95
  }
99
 
}
100
 
 
101
 
void Schema::startup(drizzled::Session &)
102
 
{
103
 
}
104
 
 
105
 
void Schema::doGetSchemaIdentifiers(identifier::Schema::vector &set_of_names)
106
 
{
107
 
  mutex.lock_shared();
 
96
  pthread_rwlock_unlock(&schema_lock);
 
97
}
 
98
 
 
99
void Schema::doGetSchemaIdentifiers(SchemaIdentifierList &set_of_names)
 
100
{
 
101
  if (not pthread_rwlock_rdlock(&schema_lock))
108
102
  {
109
103
    for (SchemaCache::iterator iter= schema_cache.begin();
110
104
         iter != schema_cache.end();
111
105
         iter++)
112
106
    {
113
 
      set_of_names.push_back(identifier::Schema((*iter).second->name()));
 
107
      set_of_names.push_back(SchemaIdentifier((*iter).second.name()));
114
108
    }
115
 
  }
116
 
  mutex.unlock_shared();
 
109
    pthread_rwlock_unlock(&schema_lock);
 
110
 
 
111
    return;
 
112
  }
 
113
 
 
114
  // If for some reason getting a lock should fail, we resort to disk
 
115
 
 
116
  CachedDirectory directory(data_home, CachedDirectory::DIRECTORY);
 
117
 
 
118
  CachedDirectory::Entries files= directory.getEntries();
 
119
 
 
120
  for (CachedDirectory::Entries::iterator fileIter= files.begin();
 
121
       fileIter != files.end(); fileIter++)
 
122
  {
 
123
    CachedDirectory::Entry *entry= *fileIter;
 
124
    set_of_names.push_back(entry->filename);
 
125
  }
117
126
}
118
127
 
119
 
drizzled::message::schema::shared_ptr Schema::doGetSchemaDefinition(const identifier::Schema &schema_identifier)
 
128
bool Schema::doGetSchemaDefinition(SchemaIdentifier &schema_identifier, message::Schema &schema_message)
120
129
{
121
 
  mutex.lock_shared();
122
 
  SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
123
 
 
124
 
  if (iter != schema_cache.end())
 
130
  if (not pthread_rwlock_rdlock(&schema_lock))
125
131
  {
126
 
    drizzled::message::schema::shared_ptr schema_message;
127
 
    schema_message= (*iter).second;
128
 
    mutex.unlock_shared();
129
 
 
130
 
    return schema_message;
 
132
    SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
 
133
 
 
134
    if (iter != schema_cache.end())
 
135
    {
 
136
      schema_message.CopyFrom(((*iter).second));
 
137
      pthread_rwlock_unlock(&schema_lock);
 
138
      return true;
 
139
    }
 
140
    pthread_rwlock_unlock(&schema_lock);
 
141
 
 
142
    return false;
131
143
  }
132
 
  mutex.unlock_shared();
133
144
 
134
 
  return drizzled::message::schema::shared_ptr();
 
145
  // Fail to disk based means
 
146
  return readSchemaFile(schema_identifier.getPath(), schema_message);
135
147
}
136
148
 
137
 
 
138
149
bool Schema::doCreateSchema(const drizzled::message::Schema &schema_message)
139
150
{
140
 
  identifier::Schema schema_identifier(schema_message.name());
 
151
  SchemaIdentifier schema_identifier(schema_message.name());
141
152
 
142
153
  if (mkdir(schema_identifier.getPath().c_str(), 0777) == -1)
143
 
  {
144
 
    sql_perror(schema_identifier.getPath().c_str());
145
154
    return false;
146
 
  }
147
155
 
148
156
  if (not writeSchemaFile(schema_identifier, schema_message))
149
157
  {
152
160
    return false;
153
161
  }
154
162
 
 
163
  if (not pthread_rwlock_wrlock(&schema_lock))
155
164
  {
156
 
    boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
157
 
    pair<SchemaCache::iterator, bool> ret=
158
 
      schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
159
 
 
160
 
 
161
 
    if (ret.second == false)
162
 
    {
163
 
      abort(); // If this has happened, something really bad is going down.
164
 
    }
 
165
      pair<SchemaCache::iterator, bool> ret=
 
166
        schema_cache.insert(make_pair(schema_identifier.getPath(), schema_message));
 
167
 
 
168
 
 
169
      if (ret.second == false)
 
170
      {
 
171
        abort(); // If this has happened, something really bad is going down.
 
172
      }
 
173
    pthread_rwlock_unlock(&schema_lock);
165
174
  }
166
175
 
167
176
  return true;
168
177
}
169
178
 
170
 
bool Schema::doDropSchema(const identifier::Schema &schema_identifier)
 
179
bool Schema::doDropSchema(SchemaIdentifier &schema_identifier)
171
180
{
 
181
  message::Schema schema_message;
 
182
 
172
183
  string schema_file(schema_identifier.getPath());
173
184
  schema_file.append(1, FN_LIBCHAR);
174
185
  schema_file.append(MY_DB_OPT_FILE);
175
186
 
176
 
  if (not doGetSchemaDefinition(schema_identifier))
 
187
  if (not doGetSchemaDefinition(schema_identifier, schema_message))
177
188
    return false;
178
189
 
179
190
  // No db.opt file, no love from us.
180
191
  if (access(schema_file.c_str(), F_OK))
181
192
  {
182
 
    sql_perror(schema_file.c_str());
 
193
    perror(schema_file.c_str());
183
194
    return false;
184
195
  }
185
196
 
186
197
  if (unlink(schema_file.c_str()))
187
198
  {
188
 
    sql_perror(schema_file.c_str());
 
199
    perror(schema_file.c_str());
189
200
    return false;
190
201
  }
191
202
 
192
203
  if (rmdir(schema_identifier.getPath().c_str()))
193
204
  {
194
 
    sql_perror(schema_identifier.getPath().c_str());
 
205
    perror(schema_identifier.getPath().c_str());
195
206
    //@todo If this happens, we want a report of it. For the moment I dump
196
207
    //to stderr so I can catch it in Hudson.
197
208
    CachedDirectory dir(schema_identifier.getPath());
198
209
    cerr << dir;
199
210
  }
200
211
 
201
 
  boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
202
 
  schema_cache.erase(schema_identifier.getPath());
 
212
  if (not pthread_rwlock_wrlock(&schema_lock))
 
213
  {
 
214
    schema_cache.erase(schema_identifier.getPath());
 
215
    pthread_rwlock_unlock(&schema_lock);
 
216
  }
203
217
 
204
218
  return true;
205
219
}
206
220
 
207
221
bool Schema::doAlterSchema(const drizzled::message::Schema &schema_message)
208
222
{
209
 
  identifier::Schema schema_identifier(schema_message.name());
 
223
  SchemaIdentifier schema_identifier(schema_message.name());
210
224
 
211
225
  if (access(schema_identifier.getPath().c_str(), F_OK))
212
226
    return false;
213
227
 
214
228
  if (writeSchemaFile(schema_identifier, schema_message))
215
229
  {
216
 
    boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
217
 
    schema_cache.erase(schema_identifier.getPath());
218
 
 
219
 
    pair<SchemaCache::iterator, bool> ret=
220
 
      schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
221
 
 
222
 
    if (ret.second == false)
223
 
    {
224
 
      abort(); // If this has happened, something really bad is going down.
 
230
    if (not pthread_rwlock_wrlock(&schema_lock))
 
231
    {
 
232
      schema_cache.erase(schema_identifier.getPath());
 
233
 
 
234
      pair<SchemaCache::iterator, bool> ret=
 
235
        schema_cache.insert(make_pair(schema_identifier.getPath(), schema_message));
 
236
 
 
237
      if (ret.second == false)
 
238
      {
 
239
        abort(); // If this has happened, something really bad is going down.
 
240
      }
 
241
 
 
242
      pthread_rwlock_unlock(&schema_lock);
 
243
    }
 
244
    else
 
245
    {
 
246
      abort(); // This would leave us out of sync, suck.
225
247
    }
226
248
  }
227
249
 
233
255
 
234
256
  @note we do the rename to make it crash safe.
235
257
*/
236
 
bool Schema::writeSchemaFile(const identifier::Schema &schema_identifier, const message::Schema &db)
 
258
bool Schema::writeSchemaFile(SchemaIdentifier &schema_identifier, const message::Schema &db)
237
259
{
238
260
  char schema_file_tmp[FN_REFLEN];
239
261
  string schema_file(schema_identifier.getPath());
248
270
 
249
271
  if (fd == -1)
250
272
  {
251
 
    sql_perror(schema_file_tmp);
 
273
    perror(schema_file_tmp);
252
274
 
253
275
    return false;
254
276
  }
255
277
 
256
 
  bool success;
257
 
 
258
 
  try {
259
 
    success= db.SerializeToFileDescriptor(fd);
260
 
  }
261
 
  catch (...)
262
 
  {
263
 
    success= false;
264
 
  }
265
 
 
266
 
  if (not success)
267
 
  {
268
 
    my_error(ER_CORRUPT_SCHEMA_DEFINITION, MYF(0), schema_file.c_str(),
269
 
             db.InitializationErrorString().empty() ? "unknown" :  db.InitializationErrorString().c_str());
 
278
  if (not db.SerializeToFileDescriptor(fd))
 
279
  {
 
280
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
 
281
             db.InitializationErrorString().c_str());
270
282
 
271
283
    if (close(fd) == -1)
272
 
      sql_perror(schema_file_tmp);
 
284
      perror(schema_file_tmp);
273
285
 
274
286
    if (unlink(schema_file_tmp))
275
 
      sql_perror(schema_file_tmp);
 
287
      perror(schema_file_tmp);
276
288
 
277
289
    return false;
278
290
  }
279
291
 
280
292
  if (close(fd) == -1)
281
293
  {
282
 
    sql_perror(schema_file_tmp);
 
294
    perror(schema_file_tmp);
283
295
 
284
296
    if (unlink(schema_file_tmp))
285
 
      sql_perror(schema_file_tmp);
 
297
      perror(schema_file_tmp);
286
298
 
287
299
    return false;
288
300
  }
290
302
  if (rename(schema_file_tmp, schema_file.c_str()) == -1)
291
303
  {
292
304
    if (unlink(schema_file_tmp))
293
 
      sql_perror(schema_file_tmp);
 
305
      perror(schema_file_tmp);
294
306
 
295
307
    return false;
296
308
  }
299
311
}
300
312
 
301
313
 
302
 
bool Schema::readSchemaFile(const drizzled::identifier::Schema &schema_identifier, drizzled::message::Schema &schema)
 
314
bool Schema::readSchemaFile(const std::string &schema_file_name, drizzled::message::Schema &schema_message)
303
315
{
304
 
  return readSchemaFile(schema_identifier.getPath(), schema); 
305
 
}
 
316
  string db_opt_path(schema_file_name);
306
317
 
307
 
bool Schema::readSchemaFile(std::string db_opt_path, drizzled::message::Schema &schema)
308
 
{
309
318
  /*
310
319
    Pass an empty file name, and the database options file name as extension
311
320
    to avoid table name to file name encoding.
322
331
  */
323
332
  if (input.good())
324
333
  {
325
 
    if (schema.ParseFromIstream(&input))
 
334
    if (schema_message.ParseFromIstream(&input))
326
335
    {
327
336
      return true;
328
337
    }
329
338
 
330
 
    my_error(ER_CORRUPT_SCHEMA_DEFINITION, MYF(0), db_opt_path.c_str(),
331
 
             schema.InitializationErrorString().empty() ? "unknown" :  schema.InitializationErrorString().c_str());
 
339
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
 
340
             schema_message.InitializationErrorString().c_str());
332
341
  }
333
342
  else
334
343
  {
335
 
    sql_perror(db_opt_path.c_str());
 
344
    perror(db_opt_path.c_str());
336
345
  }
337
346
 
338
347
  return false;
339
348
}
340
349
 
 
350
bool Schema::doCanCreateTable(drizzled::TableIdentifier &identifier)
 
351
{
 
352
  if (static_cast<SchemaIdentifier&>(identifier) == TEMPORARY_IDENTIFIER)
 
353
  {
 
354
    return false;
 
355
  }
 
356
 
 
357
  return true;
 
358
}
 
359
 
341
360
void Schema::doGetTableIdentifiers(drizzled::CachedDirectory&,
342
 
                                   const drizzled::identifier::Schema&,
343
 
                                   drizzled::identifier::Table::vector&)
 
361
                                   drizzled::SchemaIdentifier&,
 
362
                                   drizzled::TableIdentifiers&)
344
363
{
345
364
}