~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/schema_engine/schema.cc

  • Committer: Andrew Hutchings
  • Date: 2010-11-14 14:46:36 UTC
  • mto: (1931.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1932.
  • Revision ID: andrew@linuxjedi.co.uk-20101114144636-bkttapbfpp20ccd2
Fix broken Global Status test
Fix some stats not reset
Cleanup code a little

Note: local session stats are not reset, they will be added to the global stats after flush if any query is executed on those sessions.

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/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>
 
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/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(SchemaIdentifiers &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
 
drizzled::message::schema::shared_ptr Schema::doGetSchemaDefinition(const identifier::Schema &schema_identifier)
 
116
bool Schema::doGetSchemaDefinition(const SchemaIdentifier &schema_identifier, message::SchemaPtr &schema_message)
120
117
{
121
118
  mutex.lock_shared();
122
119
  SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
123
120
 
124
121
  if (iter != schema_cache.end())
125
122
  {
126
 
    drizzled::message::schema::shared_ptr schema_message;
127
123
    schema_message= (*iter).second;
128
124
    mutex.unlock_shared();
129
 
 
130
 
    return schema_message;
 
125
    return true;
131
126
  }
132
127
  mutex.unlock_shared();
133
128
 
134
 
  return drizzled::message::schema::shared_ptr();
 
129
  return false;
135
130
}
136
131
 
137
132
 
138
133
bool Schema::doCreateSchema(const drizzled::message::Schema &schema_message)
139
134
{
140
 
  identifier::Schema schema_identifier(schema_message.name());
 
135
  SchemaIdentifier schema_identifier(schema_message.name());
141
136
 
142
137
  if (mkdir(schema_identifier.getPath().c_str(), 0777) == -1)
143
 
  {
144
 
    sql_perror(schema_identifier.getPath().c_str());
145
138
    return false;
146
 
  }
147
139
 
148
140
  if (not writeSchemaFile(schema_identifier, schema_message))
149
141
  {
152
144
    return false;
153
145
  }
154
146
 
 
147
  mutex.lock();
155
148
  {
156
 
    boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
157
149
    pair<SchemaCache::iterator, bool> ret=
158
150
      schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
159
151
 
163
155
      abort(); // If this has happened, something really bad is going down.
164
156
    }
165
157
  }
 
158
  mutex.unlock();
 
159
 
 
160
  TransactionServices &transaction_services= TransactionServices::singleton();
 
161
  transaction_services.allocateNewTransactionId();
166
162
 
167
163
  return true;
168
164
}
169
165
 
170
 
bool Schema::doDropSchema(const identifier::Schema &schema_identifier)
 
166
bool Schema::doDropSchema(const SchemaIdentifier &schema_identifier)
171
167
{
 
168
  message::SchemaPtr schema_message;
 
169
 
172
170
  string schema_file(schema_identifier.getPath());
173
171
  schema_file.append(1, FN_LIBCHAR);
174
172
  schema_file.append(MY_DB_OPT_FILE);
175
173
 
176
 
  if (not doGetSchemaDefinition(schema_identifier))
 
174
  if (not doGetSchemaDefinition(schema_identifier, schema_message))
177
175
    return false;
178
176
 
179
177
  // No db.opt file, no love from us.
180
178
  if (access(schema_file.c_str(), F_OK))
181
179
  {
182
 
    sql_perror(schema_file.c_str());
 
180
    perror(schema_file.c_str());
183
181
    return false;
184
182
  }
185
183
 
186
184
  if (unlink(schema_file.c_str()))
187
185
  {
188
 
    sql_perror(schema_file.c_str());
 
186
    perror(schema_file.c_str());
189
187
    return false;
190
188
  }
191
189
 
192
190
  if (rmdir(schema_identifier.getPath().c_str()))
193
191
  {
194
 
    sql_perror(schema_identifier.getPath().c_str());
 
192
    perror(schema_identifier.getPath().c_str());
195
193
    //@todo If this happens, we want a report of it. For the moment I dump
196
194
    //to stderr so I can catch it in Hudson.
197
195
    CachedDirectory dir(schema_identifier.getPath());
198
196
    cerr << dir;
199
197
  }
200
198
 
201
 
  boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
 
199
  mutex.lock();
202
200
  schema_cache.erase(schema_identifier.getPath());
 
201
  mutex.unlock();
 
202
 
 
203
  TransactionServices &transaction_services= TransactionServices::singleton();
 
204
  transaction_services.allocateNewTransactionId();
203
205
 
204
206
  return true;
205
207
}
206
208
 
207
209
bool Schema::doAlterSchema(const drizzled::message::Schema &schema_message)
208
210
{
209
 
  identifier::Schema schema_identifier(schema_message.name());
 
211
  SchemaIdentifier schema_identifier(schema_message.name());
210
212
 
211
213
  if (access(schema_identifier.getPath().c_str(), F_OK))
212
214
    return false;
213
215
 
214
216
  if (writeSchemaFile(schema_identifier, schema_message))
215
217
  {
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)
 
218
    mutex.lock();
223
219
    {
224
 
      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
      }
225
229
    }
 
230
    mutex.unlock();
 
231
 
 
232
    TransactionServices &transaction_services= TransactionServices::singleton();
 
233
    transaction_services.allocateNewTransactionId();
226
234
  }
227
235
 
228
236
  return true;
233
241
 
234
242
  @note we do the rename to make it crash safe.
235
243
*/
236
 
bool Schema::writeSchemaFile(const identifier::Schema &schema_identifier, const message::Schema &db)
 
244
bool Schema::writeSchemaFile(const SchemaIdentifier &schema_identifier, const message::Schema &db)
237
245
{
238
246
  char schema_file_tmp[FN_REFLEN];
239
247
  string schema_file(schema_identifier.getPath());
248
256
 
249
257
  if (fd == -1)
250
258
  {
251
 
    sql_perror(schema_file_tmp);
 
259
    perror(schema_file_tmp);
252
260
 
253
261
    return false;
254
262
  }
269
277
             db.InitializationErrorString().empty() ? "unknown" :  db.InitializationErrorString().c_str());
270
278
 
271
279
    if (close(fd) == -1)
272
 
      sql_perror(schema_file_tmp);
 
280
      perror(schema_file_tmp);
273
281
 
274
282
    if (unlink(schema_file_tmp))
275
 
      sql_perror(schema_file_tmp);
 
283
      perror(schema_file_tmp);
276
284
 
277
285
    return false;
278
286
  }
279
287
 
280
288
  if (close(fd) == -1)
281
289
  {
282
 
    sql_perror(schema_file_tmp);
 
290
    perror(schema_file_tmp);
283
291
 
284
292
    if (unlink(schema_file_tmp))
285
 
      sql_perror(schema_file_tmp);
 
293
      perror(schema_file_tmp);
286
294
 
287
295
    return false;
288
296
  }
290
298
  if (rename(schema_file_tmp, schema_file.c_str()) == -1)
291
299
  {
292
300
    if (unlink(schema_file_tmp))
293
 
      sql_perror(schema_file_tmp);
 
301
      perror(schema_file_tmp);
294
302
 
295
303
    return false;
296
304
  }
299
307
}
300
308
 
301
309
 
302
 
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)
303
311
{
304
 
  return readSchemaFile(schema_identifier.getPath(), schema); 
305
 
}
 
312
  string db_opt_path(schema_identifier.getPath());
306
313
 
307
 
bool Schema::readSchemaFile(std::string db_opt_path, drizzled::message::Schema &schema)
308
 
{
309
314
  /*
310
315
    Pass an empty file name, and the database options file name as extension
311
316
    to avoid table name to file name encoding.
332
337
  }
333
338
  else
334
339
  {
335
 
    sql_perror(db_opt_path.c_str());
 
340
    perror(db_opt_path.c_str());
336
341
  }
337
342
 
338
343
  return false;
339
344
}
340
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
 
341
362
void Schema::doGetTableIdentifiers(drizzled::CachedDirectory&,
342
 
                                   const drizzled::identifier::Schema&,
343
 
                                   drizzled::identifier::Table::vector&)
 
363
                                   const drizzled::SchemaIdentifier&,
 
364
                                   drizzled::TableIdentifiers&)
344
365
{
345
366
}