~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/schema_engine/schema.cc

  • Committer: lbieber
  • Date: 2010-10-06 16:34:16 UTC
  • mfrom: (1816.1.3 build)
  • Revision ID: lbieber@orisndriz08-20101006163416-ea0sl59qgpglk21y
Merge Monty - Change the requirement from either libinnodb to libhaildb. Also, tied it to version 2.2
Merge Andrew - fix bug 650935: remove --compress from all clients
Merge Andrew - fix bug 653471: Add -A to drizzle client
Merge Travis - 621861 = To change C structs to C++ classes in Drizzle

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