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