~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/schema_engine/schema.cc

edit

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