~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/schema_engine/schema.cc

  • Committer: Brian Aker
  • Date: 2011-02-22 06:12:02 UTC
  • mfrom: (2190.1.6 drizzle-build)
  • Revision ID: brian@tangent.org-20110222061202-k03czxykqy4x9hjs
List update, header fixes, multiple symbols, and David deletes some code.

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