~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/schema_engine/schema.cc

  • Committer: Monty Taylor
  • Date: 2011-02-14 20:25:28 UTC
  • mto: (2168.1.3 build)
  • mto: This revision was merged to the branch mainline in revision 2169.
  • Revision ID: mordred@inaugust.com-20110214202528-wfb2a5h51ntbl5ct
Fix daemonize c++ patch.

Show diffs side-by-side

added added

removed removed

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