~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/schema_engine/schema.cc

  • Committer: Lee Bieber
  • Date: 2010-12-02 18:56:33 UTC
  • mfrom: (1966.3.1 bug683842)
  • mto: This revision was merged to the branch mainline in revision 1969.
  • Revision ID: kalebral@gmail.com-20101202185633-e27o1zhpev18dlsn
Merge Monty - fix bug 683842: remove generic catch blocks       

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include "drizzled/cursor.h"
30
30
#include "drizzled/data_home.h"
31
31
 
32
 
#include <drizzled/pthread_globals.h>
33
 
 
34
 
#include <drizzled/execute.h>
35
 
 
36
32
#include "drizzled/internal/my_sys.h"
 
33
#include "drizzled/transaction_services.h"
37
34
 
38
35
#include <fcntl.h>
39
36
#include <sys/stat.h>
62
59
  schema_cache_filled(false)
63
60
{
64
61
  table_definition_ext= DEFAULT_FILE_EXTENSION;
 
62
  prime();
65
63
}
66
64
 
67
65
Schema::~Schema()
72
70
{
73
71
  CachedDirectory directory(getDataHomeCatalog().file_string(), CachedDirectory::DIRECTORY);
74
72
  CachedDirectory::Entries files= directory.getEntries();
75
 
  boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
 
73
 
 
74
  mutex.lock();
76
75
 
77
76
  for (CachedDirectory::Entries::iterator fileIter= files.begin();
78
77
       fileIter != files.end(); fileIter++)
83
82
    if (not entry->filename.compare(GLOBAL_TEMPORARY_EXT))
84
83
      continue;
85
84
 
86
 
    if (readSchemaFile(entry->filename, schema_message))
 
85
    SchemaIdentifier filename(entry->filename);
 
86
    if (readSchemaFile(filename, schema_message))
87
87
    {
88
 
      identifier::Schema schema_identifier(schema_message.name());
 
88
      SchemaIdentifier schema_identifier(schema_message.name());
89
89
 
90
90
      pair<SchemaCache::iterator, bool> ret=
91
91
        schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
92
92
 
93
93
      if (ret.second == false)
94
 
      {
 
94
     {
95
95
        abort(); // If this has happened, something really bad is going down.
96
96
      }
97
97
    }
98
98
  }
99
 
}
100
 
 
101
 
void Schema::startup(drizzled::Session &)
102
 
{
103
 
}
104
 
 
105
 
void Schema::doGetSchemaIdentifiers(identifier::Schema::vector &set_of_names)
 
99
  mutex.unlock();
 
100
}
 
101
 
 
102
void Schema::doGetSchemaIdentifiers(SchemaIdentifier::vector &set_of_names)
106
103
{
107
104
  mutex.lock_shared();
108
105
  {
110
107
         iter != schema_cache.end();
111
108
         iter++)
112
109
    {
113
 
      set_of_names.push_back(identifier::Schema((*iter).second->name()));
 
110
      set_of_names.push_back(SchemaIdentifier((*iter).second->name()));
114
111
    }
115
112
  }
116
113
  mutex.unlock_shared();
117
114
}
118
115
 
119
 
bool Schema::doGetSchemaDefinition(const identifier::Schema &schema_identifier, message::schema::shared_ptr &schema_message)
 
116
bool Schema::doGetSchemaDefinition(const SchemaIdentifier &schema_identifier, message::schema::shared_ptr &schema_message)
120
117
{
121
118
  mutex.lock_shared();
122
119
  SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
125
122
  {
126
123
    schema_message= (*iter).second;
127
124
    mutex.unlock_shared();
128
 
 
129
125
    return true;
130
126
  }
131
127
  mutex.unlock_shared();
136
132
 
137
133
bool Schema::doCreateSchema(const drizzled::message::Schema &schema_message)
138
134
{
139
 
  identifier::Schema schema_identifier(schema_message.name());
 
135
  SchemaIdentifier schema_identifier(schema_message.name());
140
136
 
141
137
  if (mkdir(schema_identifier.getPath().c_str(), 0777) == -1)
142
 
  {
143
 
    sql_perror(schema_identifier.getPath().c_str());
144
138
    return false;
145
 
  }
146
139
 
147
140
  if (not writeSchemaFile(schema_identifier, schema_message))
148
141
  {
151
144
    return false;
152
145
  }
153
146
 
 
147
  mutex.lock();
154
148
  {
155
 
    boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
156
149
    pair<SchemaCache::iterator, bool> ret=
157
150
      schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
158
151
 
162
155
      abort(); // If this has happened, something really bad is going down.
163
156
    }
164
157
  }
 
158
  mutex.unlock();
 
159
 
 
160
  TransactionServices &transaction_services= TransactionServices::singleton();
 
161
  transaction_services.allocateNewTransactionId();
165
162
 
166
163
  return true;
167
164
}
168
165
 
169
 
bool Schema::doDropSchema(const identifier::Schema &schema_identifier)
 
166
bool Schema::doDropSchema(const SchemaIdentifier &schema_identifier)
170
167
{
171
168
  message::schema::shared_ptr schema_message;
172
169
 
180
177
  // No db.opt file, no love from us.
181
178
  if (access(schema_file.c_str(), F_OK))
182
179
  {
183
 
    sql_perror(schema_file.c_str());
 
180
    perror(schema_file.c_str());
184
181
    return false;
185
182
  }
186
183
 
187
184
  if (unlink(schema_file.c_str()))
188
185
  {
189
 
    sql_perror(schema_file.c_str());
 
186
    perror(schema_file.c_str());
190
187
    return false;
191
188
  }
192
189
 
193
190
  if (rmdir(schema_identifier.getPath().c_str()))
194
191
  {
195
 
    sql_perror(schema_identifier.getPath().c_str());
 
192
    perror(schema_identifier.getPath().c_str());
196
193
    //@todo If this happens, we want a report of it. For the moment I dump
197
194
    //to stderr so I can catch it in Hudson.
198
195
    CachedDirectory dir(schema_identifier.getPath());
199
196
    cerr << dir;
200
197
  }
201
198
 
202
 
  boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
 
199
  mutex.lock();
203
200
  schema_cache.erase(schema_identifier.getPath());
 
201
  mutex.unlock();
 
202
 
 
203
  TransactionServices &transaction_services= TransactionServices::singleton();
 
204
  transaction_services.allocateNewTransactionId();
204
205
 
205
206
  return true;
206
207
}
207
208
 
208
209
bool Schema::doAlterSchema(const drizzled::message::Schema &schema_message)
209
210
{
210
 
  identifier::Schema schema_identifier(schema_message.name());
 
211
  SchemaIdentifier schema_identifier(schema_message.name());
211
212
 
212
213
  if (access(schema_identifier.getPath().c_str(), F_OK))
213
214
    return false;
214
215
 
215
216
  if (writeSchemaFile(schema_identifier, schema_message))
216
217
  {
217
 
    boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
218
 
    schema_cache.erase(schema_identifier.getPath());
219
 
 
220
 
    pair<SchemaCache::iterator, bool> ret=
221
 
      schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
222
 
 
223
 
    if (ret.second == false)
 
218
    mutex.lock();
224
219
    {
225
 
      abort(); // If this has happened, something really bad is going down.
 
220
      schema_cache.erase(schema_identifier.getPath());
 
221
 
 
222
      pair<SchemaCache::iterator, bool> ret=
 
223
        schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
 
224
 
 
225
      if (ret.second == false)
 
226
      {
 
227
        abort(); // If this has happened, something really bad is going down.
 
228
      }
226
229
    }
 
230
    mutex.unlock();
 
231
 
 
232
    TransactionServices &transaction_services= TransactionServices::singleton();
 
233
    transaction_services.allocateNewTransactionId();
227
234
  }
228
235
 
229
236
  return true;
234
241
 
235
242
  @note we do the rename to make it crash safe.
236
243
*/
237
 
bool Schema::writeSchemaFile(const identifier::Schema &schema_identifier, const message::Schema &db)
 
244
bool Schema::writeSchemaFile(const SchemaIdentifier &schema_identifier, const message::Schema &db)
238
245
{
239
246
  char schema_file_tmp[FN_REFLEN];
240
247
  string schema_file(schema_identifier.getPath());
249
256
 
250
257
  if (fd == -1)
251
258
  {
252
 
    sql_perror(schema_file_tmp);
 
259
    perror(schema_file_tmp);
253
260
 
254
261
    return false;
255
262
  }
270
277
             db.InitializationErrorString().empty() ? "unknown" :  db.InitializationErrorString().c_str());
271
278
 
272
279
    if (close(fd) == -1)
273
 
      sql_perror(schema_file_tmp);
 
280
      perror(schema_file_tmp);
274
281
 
275
282
    if (unlink(schema_file_tmp))
276
 
      sql_perror(schema_file_tmp);
 
283
      perror(schema_file_tmp);
277
284
 
278
285
    return false;
279
286
  }
280
287
 
281
288
  if (close(fd) == -1)
282
289
  {
283
 
    sql_perror(schema_file_tmp);
 
290
    perror(schema_file_tmp);
284
291
 
285
292
    if (unlink(schema_file_tmp))
286
 
      sql_perror(schema_file_tmp);
 
293
      perror(schema_file_tmp);
287
294
 
288
295
    return false;
289
296
  }
291
298
  if (rename(schema_file_tmp, schema_file.c_str()) == -1)
292
299
  {
293
300
    if (unlink(schema_file_tmp))
294
 
      sql_perror(schema_file_tmp);
 
301
      perror(schema_file_tmp);
295
302
 
296
303
    return false;
297
304
  }
300
307
}
301
308
 
302
309
 
303
 
bool Schema::readSchemaFile(const drizzled::identifier::Schema &schema_identifier, drizzled::message::Schema &schema)
 
310
bool Schema::readSchemaFile(const drizzled::SchemaIdentifier &schema_identifier, drizzled::message::Schema &schema)
304
311
{
305
 
  return readSchemaFile(schema_identifier.getPath(), schema); 
306
 
}
 
312
  string db_opt_path(schema_identifier.getPath());
307
313
 
308
 
bool Schema::readSchemaFile(std::string db_opt_path, drizzled::message::Schema &schema)
309
 
{
310
314
  /*
311
315
    Pass an empty file name, and the database options file name as extension
312
316
    to avoid table name to file name encoding.
333
337
  }
334
338
  else
335
339
  {
336
 
    sql_perror(db_opt_path.c_str());
 
340
    perror(db_opt_path.c_str());
337
341
  }
338
342
 
339
343
  return false;
340
344
}
341
345
 
 
346
bool Schema::doCanCreateTable(const drizzled::TableIdentifier &identifier)
 
347
{
 
348
 
 
349
  // This should always be the same value as GLOBAL_TEMPORARY_EXT but be
 
350
  // CASE_UP. --Brian 
 
351
  //
 
352
  // This needs to be done static in here for ordering reasons
 
353
  static SchemaIdentifier TEMPORARY_IDENTIFIER(".TEMPORARY");
 
354
  if (static_cast<const SchemaIdentifier&>(identifier) == TEMPORARY_IDENTIFIER)
 
355
  {
 
356
    return false;
 
357
  }
 
358
 
 
359
  return true;
 
360
}
 
361
 
342
362
void Schema::doGetTableIdentifiers(drizzled::CachedDirectory&,
343
 
                                   const drizzled::identifier::Schema&,
344
 
                                   drizzled::identifier::Table::vector&)
 
363
                                   const drizzled::SchemaIdentifier&,
 
364
                                   drizzled::TableIdentifier::vector&)
345
365
{
346
366
}