~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/schema_engine/schema.cc

  • Committer: Brian Aker
  • Date: 2010-10-29 01:13:40 UTC
  • mto: (1890.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 1891.
  • Revision ID: brian@tangent.org-20101029011340-y9ixm180v9daypqr
Remove warnings for c++

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#include "drizzled/charset.h"
28
28
#include "drizzled/charset_info.h"
29
29
#include "drizzled/cursor.h"
 
30
#include "drizzled/data_home.h"
30
31
 
31
32
#include "drizzled/internal/my_sys.h"
32
33
 
44
45
using namespace std;
45
46
using namespace drizzled;
46
47
 
47
 
static SchemaIdentifier TEMPORARY_IDENTIFIER("TEMPORARY");
48
48
 
49
49
#define MY_DB_OPT_FILE "db.opt"
50
50
#define DEFAULT_FILE_EXTENSION ".dfe" // Deep Fried Elephant
58
58
  schema_cache_filled(false)
59
59
{
60
60
  table_definition_ext= DEFAULT_FILE_EXTENSION;
61
 
  pthread_rwlock_init(&schema_lock, NULL);
62
61
  prime();
63
62
}
64
63
 
65
64
Schema::~Schema()
66
65
{
67
 
  pthread_rwlock_destroy(&schema_lock);
68
66
}
69
67
 
70
68
void Schema::prime()
71
69
{
72
 
  CachedDirectory directory(drizzle_data_home, CachedDirectory::DIRECTORY);
 
70
  CachedDirectory directory(getDataHomeCatalog().file_string(), CachedDirectory::DIRECTORY);
73
71
  CachedDirectory::Entries files= directory.getEntries();
74
72
 
75
 
  pthread_rwlock_wrlock(&schema_lock);
 
73
  mutex.lock();
76
74
 
77
75
  for (CachedDirectory::Entries::iterator fileIter= files.begin();
78
76
       fileIter != files.end(); fileIter++)
80
78
    CachedDirectory::Entry *entry= *fileIter;
81
79
    message::Schema schema_message;
82
80
 
83
 
    if (readSchemaFile(entry->filename, schema_message))
 
81
    if (not entry->filename.compare(GLOBAL_TEMPORARY_EXT))
 
82
      continue;
 
83
 
 
84
    SchemaIdentifier filename(entry->filename);
 
85
    if (readSchemaFile(filename, schema_message))
84
86
    {
85
87
      SchemaIdentifier schema_identifier(schema_message.name());
86
88
 
87
89
      pair<SchemaCache::iterator, bool> ret=
88
 
        schema_cache.insert(make_pair(schema_identifier.getPath(), schema_message));
 
90
        schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
89
91
 
90
92
      if (ret.second == false)
91
93
     {
93
95
      }
94
96
    }
95
97
  }
96
 
  pthread_rwlock_unlock(&schema_lock);
 
98
  mutex.unlock();
97
99
}
98
100
 
99
 
void Schema::doGetSchemaIdentifiers(SchemaIdentifierList &set_of_names)
 
101
void Schema::doGetSchemaIdentifiers(SchemaIdentifiers &set_of_names)
100
102
{
101
 
  if (not pthread_rwlock_rdlock(&schema_lock))
 
103
  mutex.lock_shared();
102
104
  {
103
105
    for (SchemaCache::iterator iter= schema_cache.begin();
104
106
         iter != schema_cache.end();
105
107
         iter++)
106
108
    {
107
 
      set_of_names.push_back(SchemaIdentifier((*iter).second.name()));
 
109
      set_of_names.push_back(SchemaIdentifier((*iter).second->name()));
108
110
    }
109
 
    pthread_rwlock_unlock(&schema_lock);
110
 
 
111
 
    return;
112
 
  }
113
 
 
114
 
  // If for some reason getting a lock should fail, we resort to disk
115
 
 
116
 
  CachedDirectory directory(drizzle_data_home, CachedDirectory::DIRECTORY);
117
 
 
118
 
  CachedDirectory::Entries files= directory.getEntries();
119
 
 
120
 
  for (CachedDirectory::Entries::iterator fileIter= files.begin();
121
 
       fileIter != files.end(); fileIter++)
122
 
  {
123
 
    CachedDirectory::Entry *entry= *fileIter;
124
 
    set_of_names.push_back(entry->filename);
125
 
  }
 
111
  }
 
112
  mutex.unlock_shared();
126
113
}
127
114
 
128
 
bool Schema::doGetSchemaDefinition(SchemaIdentifier &schema_identifier, message::Schema &schema_message)
 
115
bool Schema::doGetSchemaDefinition(const SchemaIdentifier &schema_identifier, message::SchemaPtr &schema_message)
129
116
{
130
 
  if (not pthread_rwlock_rdlock(&schema_lock))
 
117
  mutex.lock_shared();
 
118
  SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
 
119
 
 
120
  if (iter != schema_cache.end())
131
121
  {
132
 
    SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
133
 
 
134
 
    if (iter != schema_cache.end())
135
 
    {
136
 
      schema_message.CopyFrom(((*iter).second));
137
 
      pthread_rwlock_unlock(&schema_lock);
138
 
      return true;
139
 
    }
140
 
    pthread_rwlock_unlock(&schema_lock);
141
 
 
142
 
    return false;
 
122
    schema_message= (*iter).second;
 
123
    mutex.unlock_shared();
 
124
    return true;
143
125
  }
 
126
  mutex.unlock_shared();
144
127
 
145
 
  // Fail to disk based means
146
 
  return readSchemaFile(schema_identifier.getPath(), schema_message);
 
128
  return false;
147
129
}
148
130
 
 
131
 
149
132
bool Schema::doCreateSchema(const drizzled::message::Schema &schema_message)
150
133
{
151
134
  SchemaIdentifier schema_identifier(schema_message.name());
160
143
    return false;
161
144
  }
162
145
 
163
 
  if (not pthread_rwlock_wrlock(&schema_lock))
 
146
  mutex.lock();
164
147
  {
165
 
      pair<SchemaCache::iterator, bool> ret=
166
 
        schema_cache.insert(make_pair(schema_identifier.getPath(), schema_message));
167
 
 
168
 
 
169
 
      if (ret.second == false)
170
 
      {
171
 
        abort(); // If this has happened, something really bad is going down.
172
 
      }
173
 
    pthread_rwlock_unlock(&schema_lock);
 
148
    pair<SchemaCache::iterator, bool> ret=
 
149
      schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
 
150
 
 
151
 
 
152
    if (ret.second == false)
 
153
    {
 
154
      abort(); // If this has happened, something really bad is going down.
 
155
    }
174
156
  }
 
157
  mutex.unlock();
175
158
 
176
159
  return true;
177
160
}
178
161
 
179
 
bool Schema::doDropSchema(SchemaIdentifier &schema_identifier)
 
162
bool Schema::doDropSchema(const SchemaIdentifier &schema_identifier)
180
163
{
181
 
  message::Schema schema_message;
 
164
  message::SchemaPtr schema_message;
182
165
 
183
166
  string schema_file(schema_identifier.getPath());
184
167
  schema_file.append(1, FN_LIBCHAR);
209
192
    cerr << dir;
210
193
  }
211
194
 
212
 
  if (not pthread_rwlock_wrlock(&schema_lock))
213
 
  {
214
 
    schema_cache.erase(schema_identifier.getPath());
215
 
    pthread_rwlock_unlock(&schema_lock);
216
 
  }
 
195
  mutex.lock();
 
196
  schema_cache.erase(schema_identifier.getPath());
 
197
  mutex.unlock();
217
198
 
218
199
  return true;
219
200
}
227
208
 
228
209
  if (writeSchemaFile(schema_identifier, schema_message))
229
210
  {
230
 
    if (not pthread_rwlock_wrlock(&schema_lock))
 
211
    mutex.lock();
231
212
    {
232
213
      schema_cache.erase(schema_identifier.getPath());
233
214
 
234
215
      pair<SchemaCache::iterator, bool> ret=
235
 
        schema_cache.insert(make_pair(schema_identifier.getPath(), schema_message));
 
216
        schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
236
217
 
237
218
      if (ret.second == false)
238
219
      {
239
220
        abort(); // If this has happened, something really bad is going down.
240
221
      }
241
 
 
242
 
      pthread_rwlock_unlock(&schema_lock);
243
 
    }
244
 
    else
245
 
    {
246
 
      abort(); // This would leave us out of sync, suck.
247
 
    }
 
222
    }
 
223
    mutex.unlock();
248
224
  }
249
225
 
250
226
  return true;
255
231
 
256
232
  @note we do the rename to make it crash safe.
257
233
*/
258
 
bool Schema::writeSchemaFile(SchemaIdentifier &schema_identifier, const message::Schema &db)
 
234
bool Schema::writeSchemaFile(const SchemaIdentifier &schema_identifier, const message::Schema &db)
259
235
{
260
236
  char schema_file_tmp[FN_REFLEN];
261
237
  string schema_file(schema_identifier.getPath());
275
251
    return false;
276
252
  }
277
253
 
278
 
  if (not db.SerializeToFileDescriptor(fd))
279
 
  {
280
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
281
 
             db.InitializationErrorString().c_str());
 
254
  bool success;
 
255
 
 
256
  try {
 
257
    success= db.SerializeToFileDescriptor(fd);
 
258
  }
 
259
  catch (...)
 
260
  {
 
261
    success= false;
 
262
  }
 
263
 
 
264
  if (not success)
 
265
  {
 
266
    my_error(ER_CORRUPT_SCHEMA_DEFINITION, MYF(0), schema_file.c_str(),
 
267
             db.InitializationErrorString().empty() ? "unknown" :  db.InitializationErrorString().c_str());
282
268
 
283
269
    if (close(fd) == -1)
284
270
      perror(schema_file_tmp);
311
297
}
312
298
 
313
299
 
314
 
bool Schema::readSchemaFile(const std::string &schema_file_name, drizzled::message::Schema &schema_message)
 
300
bool Schema::readSchemaFile(const drizzled::SchemaIdentifier &schema_identifier, drizzled::message::Schema &schema)
315
301
{
316
 
  string db_opt_path(schema_file_name);
 
302
  string db_opt_path(schema_identifier.getPath());
317
303
 
318
304
  /*
319
305
    Pass an empty file name, and the database options file name as extension
331
317
  */
332
318
  if (input.good())
333
319
  {
334
 
    if (schema_message.ParseFromIstream(&input))
 
320
    if (schema.ParseFromIstream(&input))
335
321
    {
336
322
      return true;
337
323
    }
338
324
 
339
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
340
 
             schema_message.InitializationErrorString().c_str());
 
325
    my_error(ER_CORRUPT_SCHEMA_DEFINITION, MYF(0), db_opt_path.c_str(),
 
326
             schema.InitializationErrorString().empty() ? "unknown" :  schema.InitializationErrorString().c_str());
341
327
  }
342
328
  else
343
329
  {
347
333
  return false;
348
334
}
349
335
 
350
 
bool Schema::doCanCreateTable(drizzled::TableIdentifier &identifier)
 
336
bool Schema::doCanCreateTable(const drizzled::TableIdentifier &identifier)
351
337
{
352
 
  if (static_cast<SchemaIdentifier&>(identifier) == TEMPORARY_IDENTIFIER)
 
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)
353
345
  {
354
346
    return false;
355
347
  }
358
350
}
359
351
 
360
352
void Schema::doGetTableIdentifiers(drizzled::CachedDirectory&,
361
 
                                   drizzled::SchemaIdentifier&,
 
353
                                   const drizzled::SchemaIdentifier&,
362
354
                                   drizzled::TableIdentifiers&)
363
355
{
364
356
}