~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/schema_engine/schema.cc

  • Committer: Brian Aker
  • Date: 2010-12-25 00:28:49 UTC
  • mto: This revision was merged to the branch mainline in revision 2031.
  • Revision ID: brian@tangent.org-20101225002849-g73mg6ihulajis0o
First pass in refactoring of the name of my_decimal.

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