~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/archive/ha_archive.cc

  • Committer: Eric Day
  • Date: 2009-10-31 21:53:33 UTC
  • mfrom: (1200 staging)
  • mto: This revision was merged to the branch mainline in revision 1202.
  • Revision ID: eday@oddments.org-20091031215333-j94bjoanwmi68p6f
Merged trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
109
109
static unsigned int global_version;
110
110
 
111
111
/* The file extension */
112
 
#define ARZ ".ARZ"               // The data file
 
112
#define ARZ ".arz"               // The data file
113
113
#define ARN ".ARN"               // Files used during an optimize call
114
114
 
115
115
 
134
134
  NULL
135
135
};
136
136
 
137
 
class ArchiveTableNameIterator: public drizzled::plugin::TableNameIteratorImplementation
138
 
{
139
 
private:
140
 
  MY_DIR *dirp;
141
 
  uint32_t current_entry;
142
 
 
143
 
public:
144
 
  ArchiveTableNameIterator(const std::string &database)
145
 
    : drizzled::plugin::TableNameIteratorImplementation(database), dirp(NULL), current_entry(-1)
146
 
    {};
147
 
 
148
 
  ~ArchiveTableNameIterator();
149
 
 
150
 
  int next(std::string *name);
151
 
 
152
 
};
153
 
 
154
 
ArchiveTableNameIterator::~ArchiveTableNameIterator()
155
 
{
156
 
  if (dirp)
157
 
    my_dirend(dirp);
158
 
}
159
 
 
160
 
int ArchiveTableNameIterator::next(string *name)
161
 
{
162
 
  char uname[NAME_LEN + 1];
163
 
  FILEINFO *file;
164
 
  char *ext;
165
 
  uint32_t file_name_len;
166
 
  const char *wild= NULL;
167
 
 
168
 
  if (dirp == NULL)
169
 
  {
170
 
    bool dir= false;
171
 
    char path[FN_REFLEN];
172
 
 
173
 
    build_table_filename(path, sizeof(path), db.c_str(), "", false);
174
 
    dirp = my_dir(path,MYF(dir ? MY_WANT_STAT : 0));
175
 
    if (dirp == NULL)
176
 
    {
177
 
      if (my_errno == ENOENT)
178
 
        my_error(ER_BAD_DB_ERROR, MYF(ME_BELL+ME_WAITTANG), db.c_str());
179
 
      else
180
 
        my_error(ER_CANT_READ_DIR, MYF(ME_BELL+ME_WAITTANG), path, my_errno);
181
 
      return(ENOENT);
182
 
    }
183
 
    current_entry= -1;
184
 
  }
185
 
 
186
 
  while(true)
187
 
  {
188
 
    current_entry++;
189
 
 
190
 
    if (current_entry == dirp->number_off_files)
191
 
    {
192
 
      my_dirend(dirp);
193
 
      dirp= NULL;
194
 
      return -1;
195
 
    }
196
 
 
197
 
    file= dirp->dir_entry + current_entry;
198
 
 
199
 
    if (my_strcasecmp(system_charset_info, ext=strchr(file->name,'.'), ARZ) ||
200
 
        is_prefix(file->name, TMP_FILE_PREFIX))
201
 
      continue;
202
 
    *ext=0;
203
 
 
204
 
    file_name_len= filename_to_tablename(file->name, uname, sizeof(uname));
205
 
 
206
 
    uname[file_name_len]= '\0';
207
 
 
208
 
    if (wild && wild_compare(uname, wild, 0))
209
 
      continue;
210
 
    if (name)
211
 
      name->assign(uname);
212
 
 
213
 
    return 0;
214
 
  }
215
 
}
216
 
 
217
137
class ArchiveEngine : public drizzled::plugin::StorageEngine
218
138
{
219
139
public:
220
140
  ArchiveEngine(const string &name_arg)
221
141
   : drizzled::plugin::StorageEngine(name_arg,
222
142
                                     HTON_FILE_BASED
223
 
                                      | HTON_HAS_DATA_DICTIONARY) {}
 
143
                                      | HTON_HAS_DATA_DICTIONARY) 
 
144
  {
 
145
    table_definition_ext= ARZ;
 
146
  }
224
147
 
225
 
  virtual handler *create(TableShare *table,
 
148
  virtual Cursor *create(TableShare *table,
226
149
                          MEM_ROOT *mem_root)
227
150
  {
228
151
    return new (mem_root) ha_archive(this, table);
232
155
    return ha_archive_exts;
233
156
  }
234
157
 
235
 
  int createTableImplementation(Session *session, const char *table_name,
236
 
                                Table *table_arg, HA_CREATE_INFO *create_info,
237
 
                                drizzled::message::Table* proto);
238
 
 
239
 
  int getTableProtoImplementation(const char* path,
240
 
                                  drizzled::message::Table *table_proto);
241
 
 
242
 
  drizzled::plugin::TableNameIteratorImplementation* tableNameIterator(const std::string &database)
243
 
  {
244
 
    return new ArchiveTableNameIterator(database);
245
 
  }
 
158
  int doCreateTable(Session *session, const char *table_name,
 
159
                    Table& table_arg, HA_CREATE_INFO& create_info,
 
160
                    drizzled::message::Table& proto);
 
161
 
 
162
  int doGetTableDefinition(Session& session,
 
163
                           const char* path,
 
164
                           const char *db,
 
165
                           const char *table_name,
 
166
                           const bool is_tmp,
 
167
                           drizzled::message::Table *table_proto);
 
168
 
 
169
  void doGetTableNames(CachedDirectory &directory, string& , set<string>& set_of_names);
 
170
 
 
171
  int doDropTable(Session&, const string table_path);
246
172
};
247
173
 
248
 
int ArchiveEngine::getTableProtoImplementation(const char* path,
249
 
                                         drizzled::message::Table *table_proto)
 
174
 
 
175
void ArchiveEngine::doGetTableNames(CachedDirectory &directory, 
 
176
                                    string&, 
 
177
                                    set<string>& set_of_names)
 
178
{
 
179
  CachedDirectory::Entries entries= directory.getEntries();
 
180
 
 
181
  for (CachedDirectory::Entries::iterator entry_iter= entries.begin(); 
 
182
       entry_iter != entries.end(); ++entry_iter)
 
183
  {
 
184
    CachedDirectory::Entry *entry= *entry_iter;
 
185
    string *filename= &entry->filename;
 
186
 
 
187
    assert(filename->size());
 
188
 
 
189
    const char *ext= strchr(filename->c_str(), '.');
 
190
 
 
191
    if (ext == NULL || my_strcasecmp(system_charset_info, ext, ARZ) ||
 
192
        is_prefix(filename->c_str(), TMP_FILE_PREFIX))
 
193
    {  }
 
194
    else
 
195
    {
 
196
      char uname[NAME_LEN + 1];
 
197
      uint32_t file_name_len;
 
198
 
 
199
      file_name_len= filename_to_tablename(filename->c_str(), uname, sizeof(uname));
 
200
      // TODO: Remove need for memory copy here
 
201
      uname[file_name_len - sizeof(ARZ) + 1]= '\0'; // Subtract ending, place NULL 
 
202
      set_of_names.insert(uname);
 
203
    }
 
204
  }
 
205
}
 
206
 
 
207
 
 
208
int ArchiveEngine::doDropTable(Session&,
 
209
                               const string table_path)
 
210
{
 
211
  int error= 0;
 
212
  char buff[FN_REFLEN];
 
213
 
 
214
  fn_format(buff, table_path.c_str(), "", ARZ,
 
215
            MY_UNPACK_FILENAME|MY_APPEND_EXT);
 
216
  if (my_delete_with_symlink(buff, MYF(0)))
 
217
  {
 
218
    if (my_errno != ENOENT)
 
219
    {
 
220
      error= my_errno;
 
221
    }
 
222
  }
 
223
 
 
224
  return error;
 
225
}
 
226
 
 
227
int ArchiveEngine::doGetTableDefinition(Session&,
 
228
                                        const char* path,
 
229
                                        const char *,
 
230
                                        const char *,
 
231
                                        const bool,
 
232
                                        drizzled::message::Table *table_proto)
250
233
{
251
234
  struct stat stat_info;
252
235
  int error= 0;
289
272
static ArchiveEngine *archive_engine= NULL;
290
273
 
291
274
/*
292
 
  Initialize the archive handler.
 
275
  Initialize the archive Cursor.
293
276
 
294
277
  SYNOPSIS
295
278
    archive_db_init()
314
297
}
315
298
 
316
299
/*
317
 
  Release the archive handler.
 
300
  Release the archive Cursor.
318
301
 
319
302
  SYNOPSIS
320
303
    archive_db_done()
337
320
 
338
321
ha_archive::ha_archive(drizzled::plugin::StorageEngine *engine_arg,
339
322
                       TableShare *table_arg)
340
 
  :handler(engine_arg, table_arg), delayed_insert(0), bulk_insert(0)
 
323
  :Cursor(engine_arg, table_arg), delayed_insert(0), bulk_insert(0)
341
324
{
342
325
  /* Set our original buffer from pre-allocated memory */
343
326
  buffer.set((char *)byte_buffer, IO_SIZE, system_charset_info);
516
499
 
517
500
 
518
501
/*
519
 
  No locks are required because it is associated with just one handler instance
 
502
  No locks are required because it is associated with just one Cursor instance
520
503
*/
521
504
int ha_archive::init_archive_reader()
522
505
{
640
623
  of creation.
641
624
*/
642
625
 
643
 
int ArchiveEngine::createTableImplementation(Session *,
644
 
                                             const char *table_name,
645
 
                                             Table *table_arg,
646
 
                                             HA_CREATE_INFO *create_info,
647
 
                                             drizzled::message::Table *proto)
 
626
int ArchiveEngine::doCreateTable(Session *,
 
627
                                 const char *table_name,
 
628
                                 Table& table_arg,
 
629
                                 HA_CREATE_INFO& create_info,
 
630
                                 drizzled::message::Table& proto)
648
631
{
649
632
  char name_buff[FN_REFLEN];
650
633
  int error= 0;
652
635
  uint64_t auto_increment_value;
653
636
  string serialized_proto;
654
637
 
655
 
  auto_increment_value= create_info->auto_increment_value;
 
638
  auto_increment_value= create_info.auto_increment_value;
656
639
 
657
 
  for (uint32_t key= 0; key < table_arg->sizeKeys(); key++)
 
640
  for (uint32_t key= 0; key < table_arg.sizeKeys(); key++)
658
641
  {
659
 
    KEY *pos= table_arg->key_info+key;
 
642
    KEY *pos= table_arg.key_info+key;
660
643
    KEY_PART_INFO *key_part=     pos->key_part;
661
644
    KEY_PART_INFO *key_part_end= key_part + pos->key_parts;
662
645
 
686
669
    goto error2;
687
670
  }
688
671
 
689
 
  proto->SerializeToString(&serialized_proto);
 
672
  proto.SerializeToString(&serialized_proto);
690
673
 
691
674
  if (azwrite_frm(&create_stream, serialized_proto.c_str(),
692
675
                  serialized_proto.length()))
693
676
    goto error2;
694
677
 
695
 
  if (proto->options().has_comment())
 
678
  if (proto.options().has_comment())
696
679
  {
697
680
    int write_length;
698
681
 
699
682
    write_length= azwrite_comment(&create_stream,
700
 
                                  proto->options().comment().c_str(),
701
 
                                  proto->options().comment().length());
 
683
                                  proto.options().comment().c_str(),
 
684
                                  proto.options().comment().length());
702
685
 
703
686
    if (write_length < 0)
704
687
    {