~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-07-07 13:41:07 UTC
  • mto: This revision was merged to the branch mainline in revision 2385.
  • Revision ID: olafvdspek@gmail.com-20110707134107-6mi7pauiatxtf4oe
Rename strmake to strdup (standard name)

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 <drizzled/error.h>
 
24
#include <plugin/schema_engine/schema.h>
 
25
#include <drizzled/schema.h>
 
26
#include <drizzled/sql_table.h>
 
27
#include <drizzled/charset.h>
 
28
#include <drizzled/cursor.h>
 
29
#include <drizzled/data_home.h>
 
30
 
 
31
#include <drizzled/pthread_globals.h>
 
32
 
 
33
#include <drizzled/execute.h>
 
34
 
 
35
#include <drizzled/internal/my_sys.h>
 
36
#include <drizzled/cached_directory.h>
33
37
 
34
38
#include <fcntl.h>
35
39
#include <sys/stat.h>
36
40
#include <sys/types.h>
37
41
 
 
42
#include <boost/foreach.hpp>
38
43
#include <google/protobuf/io/zero_copy_stream.h>
39
44
#include <google/protobuf/io/zero_copy_stream_impl.h>
40
45
 
45
50
using namespace std;
46
51
using namespace drizzled;
47
52
 
48
 
 
49
 
#define MY_DB_OPT_FILE "db.opt"
50
 
#define DEFAULT_FILE_EXTENSION ".dfe" // Deep Fried Elephant
51
 
 
52
 
Schema::Schema():
 
53
const char* MY_DB_OPT_FILE= "db.opt";
 
54
const char* DEFAULT_FILE_EXTENSION= ".dfe"; // Deep Fried Elephant
 
55
 
 
56
static const char* g_schema_exts[] = 
 
57
{
 
58
  NULL
 
59
};
 
60
 
 
61
Schema::Schema() :
53
62
  drizzled::plugin::StorageEngine("schema",
54
63
                                  HTON_ALTER_NOT_SUPPORTED |
55
64
                                  HTON_HAS_SCHEMA_DICTIONARY |
60
69
  table_definition_ext= DEFAULT_FILE_EXTENSION;
61
70
}
62
71
 
63
 
Schema::~Schema()
64
 
{
65
 
}
66
 
 
67
72
void Schema::prime()
68
73
{
69
74
  CachedDirectory directory(getDataHomeCatalog().file_string(), CachedDirectory::DIRECTORY);
70
75
  CachedDirectory::Entries files= directory.getEntries();
71
 
 
72
 
  mutex.lock();
73
 
 
74
 
  for (CachedDirectory::Entries::iterator fileIter= files.begin();
75
 
       fileIter != files.end(); fileIter++)
 
76
  boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
 
77
 
 
78
  BOOST_FOREACH(CachedDirectory::Entries::reference entry, files)
76
79
  {
77
 
    CachedDirectory::Entry *entry= *fileIter;
78
 
    message::Schema schema_message;
79
 
 
80
80
    if (not entry->filename.compare(GLOBAL_TEMPORARY_EXT))
81
81
      continue;
82
 
 
83
 
    SchemaIdentifier filename(entry->filename);
84
 
    if (readSchemaFile(filename, schema_message))
 
82
    message::Schema schema_message;
 
83
    if (readSchemaFile(entry->filename, schema_message))
85
84
    {
86
 
      SchemaIdentifier schema_identifier(schema_message.name());
 
85
      identifier::Schema schema_identifier(schema_message.name());
87
86
 
88
87
      pair<SchemaCache::iterator, bool> ret=
89
88
        schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
90
89
 
91
 
      if (ret.second == false)
92
 
     {
93
 
        abort(); // If this has happened, something really bad is going down.
94
 
      }
 
90
      assert(ret.second); // If this has happened, something really bad is going down.
95
91
    }
96
92
  }
97
 
  mutex.unlock();
98
93
}
99
94
 
100
 
void Schema::doGetSchemaIdentifiers(SchemaIdentifier::vector &set_of_names)
 
95
void Schema::doGetSchemaIdentifiers(identifier::schema::vector &set_of_names)
101
96
{
102
97
  mutex.lock_shared();
103
 
  {
104
 
    for (SchemaCache::iterator iter= schema_cache.begin();
105
 
         iter != schema_cache.end();
106
 
         iter++)
107
 
    {
108
 
      set_of_names.push_back(SchemaIdentifier((*iter).second->name()));
109
 
    }
110
 
  }
 
98
  BOOST_FOREACH(SchemaCache::reference iter, schema_cache)
 
99
    set_of_names.push_back(iter.second->name());
111
100
  mutex.unlock_shared();
112
101
}
113
102
 
114
 
bool Schema::doGetSchemaDefinition(const SchemaIdentifier &schema_identifier, message::schema::shared_ptr &schema_message)
 
103
drizzled::message::schema::shared_ptr Schema::doGetSchemaDefinition(const identifier::Schema &schema_identifier)
115
104
{
116
105
  mutex.lock_shared();
117
106
  SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
118
 
 
119
107
  if (iter != schema_cache.end())
120
108
  {
121
 
    schema_message= (*iter).second;
 
109
    drizzled::message::schema::shared_ptr schema_message= iter->second;
122
110
    mutex.unlock_shared();
123
 
    return true;
 
111
    return schema_message;
124
112
  }
125
113
  mutex.unlock_shared();
126
 
 
127
 
  return false;
 
114
  return drizzled::message::schema::shared_ptr();
128
115
}
129
116
 
130
117
 
131
118
bool Schema::doCreateSchema(const drizzled::message::Schema &schema_message)
132
119
{
133
 
  SchemaIdentifier schema_identifier(schema_message.name());
 
120
  identifier::Schema schema_identifier(schema_message.name());
134
121
 
135
122
  if (mkdir(schema_identifier.getPath().c_str(), 0777) == -1)
 
123
  {
 
124
    sql_perror(schema_identifier.getPath().c_str());
136
125
    return false;
 
126
  }
137
127
 
138
128
  if (not writeSchemaFile(schema_identifier, schema_message))
139
129
  {
142
132
    return false;
143
133
  }
144
134
 
145
 
  mutex.lock();
146
 
  {
147
 
    pair<SchemaCache::iterator, bool> ret=
148
 
      schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
149
 
 
150
 
 
151
 
    if (ret.second == false)
152
 
    {
153
 
      abort(); // If this has happened, something really bad is going down.
154
 
    }
155
 
  }
156
 
  mutex.unlock();
157
 
 
 
135
  boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
 
136
  pair<SchemaCache::iterator, bool> ret= 
 
137
    schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
 
138
 
 
139
  assert(ret.second); // If this has happened, something really bad is going down.
158
140
  return true;
159
141
}
160
142
 
161
 
bool Schema::doDropSchema(const SchemaIdentifier &schema_identifier)
 
143
bool Schema::doDropSchema(const identifier::Schema &schema_identifier)
162
144
{
163
 
  message::schema::shared_ptr schema_message;
164
 
 
165
145
  string schema_file(schema_identifier.getPath());
166
146
  schema_file.append(1, FN_LIBCHAR);
167
147
  schema_file.append(MY_DB_OPT_FILE);
168
148
 
169
 
  if (not doGetSchemaDefinition(schema_identifier, schema_message))
 
149
  if (not doGetSchemaDefinition(schema_identifier))
170
150
    return false;
171
151
 
172
152
  // No db.opt file, no love from us.
173
153
  if (access(schema_file.c_str(), F_OK))
174
154
  {
175
 
    perror(schema_file.c_str());
 
155
    sql_perror(schema_file.c_str());
176
156
    return false;
177
157
  }
178
158
 
179
159
  if (unlink(schema_file.c_str()))
180
160
  {
181
 
    perror(schema_file.c_str());
 
161
    sql_perror(schema_file.c_str());
182
162
    return false;
183
163
  }
184
164
 
185
165
  if (rmdir(schema_identifier.getPath().c_str()))
186
166
  {
187
 
    perror(schema_identifier.getPath().c_str());
 
167
    sql_perror(schema_identifier.getPath().c_str());
188
168
    //@todo If this happens, we want a report of it. For the moment I dump
189
169
    //to stderr so I can catch it in Hudson.
190
170
    CachedDirectory dir(schema_identifier.getPath());
191
171
    cerr << dir;
192
172
  }
193
173
 
194
 
  mutex.lock();
 
174
  boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
195
175
  schema_cache.erase(schema_identifier.getPath());
196
 
  mutex.unlock();
197
176
 
198
177
  return true;
199
178
}
200
179
 
201
180
bool Schema::doAlterSchema(const drizzled::message::Schema &schema_message)
202
181
{
203
 
  SchemaIdentifier schema_identifier(schema_message.name());
 
182
  identifier::Schema schema_identifier(schema_message.name());
204
183
 
205
184
  if (access(schema_identifier.getPath().c_str(), F_OK))
206
185
    return false;
207
186
 
208
187
  if (writeSchemaFile(schema_identifier, schema_message))
209
188
  {
210
 
    mutex.lock();
211
 
    {
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
 
      }
221
 
    }
222
 
    mutex.unlock();
 
189
    boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
 
190
    schema_cache.erase(schema_identifier.getPath());
 
191
 
 
192
    pair<SchemaCache::iterator, bool> ret=
 
193
      schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
 
194
 
 
195
    assert(ret.second); // If this has happened, something really bad is going down.
223
196
  }
224
197
 
225
198
  return true;
230
203
 
231
204
  @note we do the rename to make it crash safe.
232
205
*/
233
 
bool Schema::writeSchemaFile(const SchemaIdentifier &schema_identifier, const message::Schema &db)
 
206
bool Schema::writeSchemaFile(const identifier::Schema &schema_identifier, const message::Schema &db)
234
207
{
235
208
  char schema_file_tmp[FN_REFLEN];
236
209
  string schema_file(schema_identifier.getPath());
245
218
 
246
219
  if (fd == -1)
247
220
  {
248
 
    perror(schema_file_tmp);
 
221
    sql_perror(schema_file_tmp);
249
222
 
250
223
    return false;
251
224
  }
266
239
             db.InitializationErrorString().empty() ? "unknown" :  db.InitializationErrorString().c_str());
267
240
 
268
241
    if (close(fd) == -1)
269
 
      perror(schema_file_tmp);
 
242
      sql_perror(schema_file_tmp);
270
243
 
271
244
    if (unlink(schema_file_tmp))
272
 
      perror(schema_file_tmp);
 
245
      sql_perror(schema_file_tmp);
273
246
 
274
247
    return false;
275
248
  }
276
249
 
277
250
  if (close(fd) == -1)
278
251
  {
279
 
    perror(schema_file_tmp);
 
252
    sql_perror(schema_file_tmp);
280
253
 
281
254
    if (unlink(schema_file_tmp))
282
 
      perror(schema_file_tmp);
 
255
      sql_perror(schema_file_tmp);
283
256
 
284
257
    return false;
285
258
  }
287
260
  if (rename(schema_file_tmp, schema_file.c_str()) == -1)
288
261
  {
289
262
    if (unlink(schema_file_tmp))
290
 
      perror(schema_file_tmp);
 
263
      sql_perror(schema_file_tmp);
291
264
 
292
265
    return false;
293
266
  }
296
269
}
297
270
 
298
271
 
299
 
bool Schema::readSchemaFile(const drizzled::SchemaIdentifier &schema_identifier, drizzled::message::Schema &schema)
 
272
bool Schema::readSchemaFile(const drizzled::identifier::Schema &schema_identifier, drizzled::message::Schema &schema)
300
273
{
301
 
  string db_opt_path(schema_identifier.getPath());
 
274
  return readSchemaFile(schema_identifier.getPath(), schema); 
 
275
}
302
276
 
 
277
bool Schema::readSchemaFile(std::string db_opt_path, drizzled::message::Schema &schema)
 
278
{
303
279
  /*
304
280
    Pass an empty file name, and the database options file name as extension
305
281
    to avoid table name to file name encoding.
326
302
  }
327
303
  else
328
304
  {
329
 
    perror(db_opt_path.c_str());
 
305
    sql_perror(db_opt_path.c_str());
330
306
  }
331
307
 
332
308
  return false;
333
309
}
334
310
 
335
311
void Schema::doGetTableIdentifiers(drizzled::CachedDirectory&,
336
 
                                   const drizzled::SchemaIdentifier&,
337
 
                                   drizzled::TableIdentifier::vector&)
338
 
{
 
312
                                   const drizzled::identifier::Schema&,
 
313
                                   drizzled::identifier::table::vector&)
 
314
{
 
315
}
 
316
 
 
317
const char** Schema::bas_ext() const
 
318
{
 
319
  return g_schema_exts;
339
320
}