~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/schema_engine/schema.cc

retrieve data from the local cache

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
 
 
31
#include "drizzled/internal/my_sys.h"
37
32
 
38
33
#include <fcntl.h>
39
34
#include <sys/stat.h>
49
44
using namespace std;
50
45
using namespace drizzled;
51
46
 
 
47
// This should always be the same value as GLOBAL_TEMPORARY_EXT but be
 
48
// CASE_UP. --Brian
 
49
static SchemaIdentifier TEMPORARY_IDENTIFIER(".TEMPORARY");
52
50
 
53
51
#define MY_DB_OPT_FILE "db.opt"
54
52
#define DEFAULT_FILE_EXTENSION ".dfe" // Deep Fried Elephant
62
60
  schema_cache_filled(false)
63
61
{
64
62
  table_definition_ext= DEFAULT_FILE_EXTENSION;
 
63
  pthread_rwlock_init(&schema_lock, NULL);
 
64
  prime();
65
65
}
66
66
 
67
67
Schema::~Schema()
68
68
{
 
69
  pthread_rwlock_destroy(&schema_lock);
69
70
}
70
71
 
71
72
void Schema::prime()
72
73
{
73
 
  CachedDirectory directory(getDataHomeCatalog().file_string(), CachedDirectory::DIRECTORY);
 
74
  CachedDirectory directory(data_home, CachedDirectory::DIRECTORY);
74
75
  CachedDirectory::Entries files= directory.getEntries();
75
 
  boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
 
76
 
 
77
  pthread_rwlock_wrlock(&schema_lock);
76
78
 
77
79
  for (CachedDirectory::Entries::iterator fileIter= files.begin();
78
80
       fileIter != files.end(); fileIter++)
85
87
 
86
88
    if (readSchemaFile(entry->filename, schema_message))
87
89
    {
88
 
      identifier::Schema schema_identifier(schema_message.name());
 
90
      SchemaIdentifier schema_identifier(schema_message.name());
89
91
 
90
92
      pair<SchemaCache::iterator, bool> ret=
91
 
        schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
 
93
        schema_cache.insert(make_pair(schema_identifier.getPath(), schema_message));
92
94
 
93
95
      if (ret.second == false)
94
 
      {
 
96
     {
95
97
        abort(); // If this has happened, something really bad is going down.
96
98
      }
97
99
    }
98
100
  }
99
 
}
100
 
 
101
 
void Schema::startup(drizzled::Session &)
102
 
{
103
 
}
104
 
 
105
 
void Schema::doGetSchemaIdentifiers(identifier::Schema::vector &set_of_names)
106
 
{
107
 
  mutex.lock_shared();
 
101
  pthread_rwlock_unlock(&schema_lock);
 
102
}
 
103
 
 
104
void Schema::doGetSchemaIdentifiers(SchemaIdentifierList &set_of_names)
 
105
{
 
106
  if (not pthread_rwlock_rdlock(&schema_lock))
108
107
  {
109
108
    for (SchemaCache::iterator iter= schema_cache.begin();
110
109
         iter != schema_cache.end();
111
110
         iter++)
112
111
    {
113
 
      set_of_names.push_back(identifier::Schema((*iter).second->name()));
 
112
      set_of_names.push_back(SchemaIdentifier((*iter).second.name()));
114
113
    }
115
 
  }
116
 
  mutex.unlock_shared();
 
114
    pthread_rwlock_unlock(&schema_lock);
 
115
 
 
116
    return;
 
117
  }
 
118
 
 
119
  // If for some reason getting a lock should fail, we resort to disk
 
120
 
 
121
  CachedDirectory directory(data_home, CachedDirectory::DIRECTORY);
 
122
 
 
123
  CachedDirectory::Entries files= directory.getEntries();
 
124
 
 
125
  for (CachedDirectory::Entries::iterator fileIter= files.begin();
 
126
       fileIter != files.end(); fileIter++)
 
127
  {
 
128
    CachedDirectory::Entry *entry= *fileIter;
 
129
    set_of_names.push_back(entry->filename);
 
130
  }
117
131
}
118
132
 
119
 
drizzled::message::schema::shared_ptr Schema::doGetSchemaDefinition(const identifier::Schema &schema_identifier)
 
133
bool Schema::doGetSchemaDefinition(const SchemaIdentifier &schema_identifier, message::Schema &schema_message)
120
134
{
121
 
  mutex.lock_shared();
122
 
  SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
123
 
 
124
 
  if (iter != schema_cache.end())
 
135
  if (not pthread_rwlock_rdlock(&schema_lock))
125
136
  {
126
 
    drizzled::message::schema::shared_ptr schema_message;
127
 
    schema_message= (*iter).second;
128
 
    mutex.unlock_shared();
129
 
 
130
 
    return schema_message;
 
137
    SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
 
138
 
 
139
    if (iter != schema_cache.end())
 
140
    {
 
141
      schema_message.CopyFrom(((*iter).second));
 
142
      pthread_rwlock_unlock(&schema_lock);
 
143
      return true;
 
144
    }
 
145
    pthread_rwlock_unlock(&schema_lock);
 
146
 
 
147
    return false;
131
148
  }
132
 
  mutex.unlock_shared();
133
149
 
134
 
  return drizzled::message::schema::shared_ptr();
 
150
  // Fail to disk based means
 
151
  return readSchemaFile(schema_identifier.getPath(), schema_message);
135
152
}
136
153
 
137
 
 
138
154
bool Schema::doCreateSchema(const drizzled::message::Schema &schema_message)
139
155
{
140
 
  identifier::Schema schema_identifier(schema_message.name());
 
156
  SchemaIdentifier schema_identifier(schema_message.name());
141
157
 
142
158
  if (mkdir(schema_identifier.getPath().c_str(), 0777) == -1)
143
 
  {
144
 
    sql_perror(schema_identifier.getPath().c_str());
145
159
    return false;
146
 
  }
147
160
 
148
161
  if (not writeSchemaFile(schema_identifier, schema_message))
149
162
  {
152
165
    return false;
153
166
  }
154
167
 
 
168
  if (not pthread_rwlock_wrlock(&schema_lock))
155
169
  {
156
 
    boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
157
 
    pair<SchemaCache::iterator, bool> ret=
158
 
      schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
159
 
 
160
 
 
161
 
    if (ret.second == false)
162
 
    {
163
 
      abort(); // If this has happened, something really bad is going down.
164
 
    }
 
170
      pair<SchemaCache::iterator, bool> ret=
 
171
        schema_cache.insert(make_pair(schema_identifier.getPath(), schema_message));
 
172
 
 
173
 
 
174
      if (ret.second == false)
 
175
      {
 
176
        abort(); // If this has happened, something really bad is going down.
 
177
      }
 
178
    pthread_rwlock_unlock(&schema_lock);
165
179
  }
166
180
 
167
181
  return true;
168
182
}
169
183
 
170
 
bool Schema::doDropSchema(const identifier::Schema &schema_identifier)
 
184
bool Schema::doDropSchema(const SchemaIdentifier &schema_identifier)
171
185
{
 
186
  message::Schema schema_message;
 
187
 
172
188
  string schema_file(schema_identifier.getPath());
173
189
  schema_file.append(1, FN_LIBCHAR);
174
190
  schema_file.append(MY_DB_OPT_FILE);
175
191
 
176
 
  if (not doGetSchemaDefinition(schema_identifier))
 
192
  if (not doGetSchemaDefinition(schema_identifier, schema_message))
177
193
    return false;
178
194
 
179
195
  // No db.opt file, no love from us.
180
196
  if (access(schema_file.c_str(), F_OK))
181
197
  {
182
 
    sql_perror(schema_file.c_str());
 
198
    perror(schema_file.c_str());
183
199
    return false;
184
200
  }
185
201
 
186
202
  if (unlink(schema_file.c_str()))
187
203
  {
188
 
    sql_perror(schema_file.c_str());
 
204
    perror(schema_file.c_str());
189
205
    return false;
190
206
  }
191
207
 
192
208
  if (rmdir(schema_identifier.getPath().c_str()))
193
209
  {
194
 
    sql_perror(schema_identifier.getPath().c_str());
 
210
    perror(schema_identifier.getPath().c_str());
195
211
    //@todo If this happens, we want a report of it. For the moment I dump
196
212
    //to stderr so I can catch it in Hudson.
197
213
    CachedDirectory dir(schema_identifier.getPath());
198
214
    cerr << dir;
199
215
  }
200
216
 
201
 
  boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
202
 
  schema_cache.erase(schema_identifier.getPath());
 
217
  if (not pthread_rwlock_wrlock(&schema_lock))
 
218
  {
 
219
    schema_cache.erase(schema_identifier.getPath());
 
220
    pthread_rwlock_unlock(&schema_lock);
 
221
  }
203
222
 
204
223
  return true;
205
224
}
206
225
 
207
226
bool Schema::doAlterSchema(const drizzled::message::Schema &schema_message)
208
227
{
209
 
  identifier::Schema schema_identifier(schema_message.name());
 
228
  SchemaIdentifier schema_identifier(schema_message.name());
210
229
 
211
230
  if (access(schema_identifier.getPath().c_str(), F_OK))
212
231
    return false;
213
232
 
214
233
  if (writeSchemaFile(schema_identifier, schema_message))
215
234
  {
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)
223
 
    {
224
 
      abort(); // If this has happened, something really bad is going down.
 
235
    if (not pthread_rwlock_wrlock(&schema_lock))
 
236
    {
 
237
      schema_cache.erase(schema_identifier.getPath());
 
238
 
 
239
      pair<SchemaCache::iterator, bool> ret=
 
240
        schema_cache.insert(make_pair(schema_identifier.getPath(), schema_message));
 
241
 
 
242
      if (ret.second == false)
 
243
      {
 
244
        abort(); // If this has happened, something really bad is going down.
 
245
      }
 
246
 
 
247
      pthread_rwlock_unlock(&schema_lock);
 
248
    }
 
249
    else
 
250
    {
 
251
      abort(); // This would leave us out of sync, suck.
225
252
    }
226
253
  }
227
254
 
233
260
 
234
261
  @note we do the rename to make it crash safe.
235
262
*/
236
 
bool Schema::writeSchemaFile(const identifier::Schema &schema_identifier, const message::Schema &db)
 
263
bool Schema::writeSchemaFile(const SchemaIdentifier &schema_identifier, const message::Schema &db)
237
264
{
238
265
  char schema_file_tmp[FN_REFLEN];
239
266
  string schema_file(schema_identifier.getPath());
248
275
 
249
276
  if (fd == -1)
250
277
  {
251
 
    sql_perror(schema_file_tmp);
 
278
    perror(schema_file_tmp);
252
279
 
253
280
    return false;
254
281
  }
269
296
             db.InitializationErrorString().empty() ? "unknown" :  db.InitializationErrorString().c_str());
270
297
 
271
298
    if (close(fd) == -1)
272
 
      sql_perror(schema_file_tmp);
 
299
      perror(schema_file_tmp);
273
300
 
274
301
    if (unlink(schema_file_tmp))
275
 
      sql_perror(schema_file_tmp);
 
302
      perror(schema_file_tmp);
276
303
 
277
304
    return false;
278
305
  }
279
306
 
280
307
  if (close(fd) == -1)
281
308
  {
282
 
    sql_perror(schema_file_tmp);
 
309
    perror(schema_file_tmp);
283
310
 
284
311
    if (unlink(schema_file_tmp))
285
 
      sql_perror(schema_file_tmp);
 
312
      perror(schema_file_tmp);
286
313
 
287
314
    return false;
288
315
  }
290
317
  if (rename(schema_file_tmp, schema_file.c_str()) == -1)
291
318
  {
292
319
    if (unlink(schema_file_tmp))
293
 
      sql_perror(schema_file_tmp);
 
320
      perror(schema_file_tmp);
294
321
 
295
322
    return false;
296
323
  }
299
326
}
300
327
 
301
328
 
302
 
bool Schema::readSchemaFile(const drizzled::identifier::Schema &schema_identifier, drizzled::message::Schema &schema)
 
329
bool Schema::readSchemaFile(const std::string &schema_file_name, drizzled::message::Schema &schema_message)
303
330
{
304
 
  return readSchemaFile(schema_identifier.getPath(), schema); 
305
 
}
 
331
  string db_opt_path(schema_file_name);
306
332
 
307
 
bool Schema::readSchemaFile(std::string db_opt_path, drizzled::message::Schema &schema)
308
 
{
309
333
  /*
310
334
    Pass an empty file name, and the database options file name as extension
311
335
    to avoid table name to file name encoding.
322
346
  */
323
347
  if (input.good())
324
348
  {
325
 
    if (schema.ParseFromIstream(&input))
 
349
    if (schema_message.ParseFromIstream(&input))
326
350
    {
327
351
      return true;
328
352
    }
329
353
 
330
354
    my_error(ER_CORRUPT_SCHEMA_DEFINITION, MYF(0), db_opt_path.c_str(),
331
 
             schema.InitializationErrorString().empty() ? "unknown" :  schema.InitializationErrorString().c_str());
 
355
             schema_message.InitializationErrorString().empty() ? "unknown" :  schema_message.InitializationErrorString().c_str());
332
356
  }
333
357
  else
334
358
  {
335
 
    sql_perror(db_opt_path.c_str());
 
359
    perror(db_opt_path.c_str());
336
360
  }
337
361
 
338
362
  return false;
339
363
}
340
364
 
 
365
bool Schema::doCanCreateTable(const drizzled::TableIdentifier &identifier)
 
366
{
 
367
  if (static_cast<const SchemaIdentifier&>(identifier) == TEMPORARY_IDENTIFIER)
 
368
  {
 
369
    return false;
 
370
  }
 
371
 
 
372
  return true;
 
373
}
 
374
 
341
375
void Schema::doGetTableIdentifiers(drizzled::CachedDirectory&,
342
 
                                   const drizzled::identifier::Schema&,
343
 
                                   drizzled::identifier::Table::vector&)
 
376
                                   const drizzled::SchemaIdentifier&,
 
377
                                   drizzled::TableIdentifiers&)
344
378
{
345
379
}