~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/schema_engine/schema.cc

  • Committer: Olaf van der Spek
  • Date: 2011-03-24 00:16:14 UTC
  • mto: This revision was merged to the branch mainline in revision 2251.
  • Revision ID: olafvdspek@gmail.com-20110324001614-wvmgc6eg52oq2321
Remove const_reference and reference from Session

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>
 
37
#include <drizzled/cached_directory.h>
33
38
 
34
39
#include <fcntl.h>
35
40
#include <sys/stat.h>
68
73
{
69
74
  CachedDirectory directory(getDataHomeCatalog().file_string(), CachedDirectory::DIRECTORY);
70
75
  CachedDirectory::Entries files= directory.getEntries();
71
 
 
72
 
  mutex.lock();
 
76
  boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
73
77
 
74
78
  for (CachedDirectory::Entries::iterator fileIter= files.begin();
75
79
       fileIter != files.end(); fileIter++)
80
84
    if (not entry->filename.compare(GLOBAL_TEMPORARY_EXT))
81
85
      continue;
82
86
 
83
 
    SchemaIdentifier filename(entry->filename);
84
 
    if (readSchemaFile(filename, schema_message))
 
87
    if (readSchemaFile(entry->filename, schema_message))
85
88
    {
86
 
      SchemaIdentifier schema_identifier(schema_message.name());
 
89
      identifier::Schema schema_identifier(schema_message.name());
87
90
 
88
91
      pair<SchemaCache::iterator, bool> ret=
89
92
        schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
90
93
 
91
94
      if (ret.second == false)
92
 
     {
 
95
      {
93
96
        abort(); // If this has happened, something really bad is going down.
94
97
      }
95
98
    }
96
99
  }
97
 
  mutex.unlock();
98
 
}
99
 
 
100
 
void Schema::doGetSchemaIdentifiers(SchemaIdentifier::vector &set_of_names)
 
100
}
 
101
 
 
102
void Schema::startup(drizzled::Session &)
 
103
{
 
104
}
 
105
 
 
106
void Schema::doGetSchemaIdentifiers(identifier::Schema::vector &set_of_names)
101
107
{
102
108
  mutex.lock_shared();
103
109
  {
105
111
         iter != schema_cache.end();
106
112
         iter++)
107
113
    {
108
 
      set_of_names.push_back(SchemaIdentifier((*iter).second->name()));
 
114
      set_of_names.push_back(identifier::Schema(iter->second->name()));
109
115
    }
110
116
  }
111
117
  mutex.unlock_shared();
112
118
}
113
119
 
114
 
bool Schema::doGetSchemaDefinition(const SchemaIdentifier &schema_identifier, message::schema::shared_ptr &schema_message)
 
120
drizzled::message::schema::shared_ptr Schema::doGetSchemaDefinition(const identifier::Schema &schema_identifier)
115
121
{
116
122
  mutex.lock_shared();
117
123
  SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
118
124
 
119
125
  if (iter != schema_cache.end())
120
126
  {
121
 
    schema_message= (*iter).second;
 
127
    drizzled::message::schema::shared_ptr schema_message;
 
128
    schema_message= iter->second;
122
129
    mutex.unlock_shared();
123
 
    return true;
 
130
 
 
131
    return schema_message;
124
132
  }
125
133
  mutex.unlock_shared();
126
134
 
127
 
  return false;
 
135
  return drizzled::message::schema::shared_ptr();
128
136
}
129
137
 
130
138
 
131
139
bool Schema::doCreateSchema(const drizzled::message::Schema &schema_message)
132
140
{
133
 
  SchemaIdentifier schema_identifier(schema_message.name());
 
141
  identifier::Schema schema_identifier(schema_message.name());
134
142
 
135
143
  if (mkdir(schema_identifier.getPath().c_str(), 0777) == -1)
 
144
  {
 
145
    sql_perror(schema_identifier.getPath().c_str());
136
146
    return false;
 
147
  }
137
148
 
138
149
  if (not writeSchemaFile(schema_identifier, schema_message))
139
150
  {
142
153
    return false;
143
154
  }
144
155
 
145
 
  mutex.lock();
146
156
  {
 
157
    boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
147
158
    pair<SchemaCache::iterator, bool> ret=
148
159
      schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
149
160
 
153
164
      abort(); // If this has happened, something really bad is going down.
154
165
    }
155
166
  }
156
 
  mutex.unlock();
157
167
 
158
168
  return true;
159
169
}
160
170
 
161
 
bool Schema::doDropSchema(const SchemaIdentifier &schema_identifier)
 
171
bool Schema::doDropSchema(const identifier::Schema &schema_identifier)
162
172
{
163
 
  message::schema::shared_ptr schema_message;
164
 
 
165
173
  string schema_file(schema_identifier.getPath());
166
174
  schema_file.append(1, FN_LIBCHAR);
167
175
  schema_file.append(MY_DB_OPT_FILE);
168
176
 
169
 
  if (not doGetSchemaDefinition(schema_identifier, schema_message))
 
177
  if (not doGetSchemaDefinition(schema_identifier))
170
178
    return false;
171
179
 
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
}