~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/schema_engine/schema.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

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