~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/filesystem_engine/filesystem_engine.cc

  • Committer: Brian Aker
  • Date: 2010-09-12 17:38:51 UTC
  • mto: (1759.2.2 fix)
  • mto: This revision was merged to the branch mainline in revision 1762.
  • Revision ID: brian@tangent.org-20100912173851-311iq2hluif6pbbf
Fixing another column result.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
17
*/
18
18
 
19
 
#include <config.h>
 
19
#include "config.h"
20
20
#include <drizzled/field.h>
21
21
#include <drizzled/field/blob.h>
 
22
#include <drizzled/field/timestamp.h>
22
23
#include <drizzled/error.h>
23
24
#include <drizzled/table.h>
24
25
#include <drizzled/session.h>
25
 
#include <drizzled/internal/my_sys.h>
 
26
#include "drizzled/internal/my_sys.h"
26
27
#include <google/protobuf/io/zero_copy_stream.h>
27
28
#include <google/protobuf/io/zero_copy_stream_impl.h>
28
29
 
73
74
    pthread_mutex_destroy(&filesystem_mutex);
74
75
  }
75
76
 
76
 
  virtual Cursor *create(Table &table)
 
77
  virtual Cursor *create(TableShare &table)
77
78
  {
78
79
    return new FilesystemCursor(*this, table);
79
80
  }
86
87
 
87
88
  int doCreateTable(Session &,
88
89
                    Table &table_arg,
89
 
                    const drizzled::identifier::Table &identifier,
 
90
                    const drizzled::TableIdentifier &identifier,
90
91
                    drizzled::message::Table&);
91
92
 
92
93
  int doGetTableDefinition(Session& ,
93
 
                           const drizzled::identifier::Table &,
 
94
                           const drizzled::TableIdentifier &,
94
95
                           drizzled::message::Table &);
95
96
 
96
 
  int doDropTable(Session&, const identifier::Table &);
 
97
  /* Temp only engine, so do not return values. */
 
98
  void doGetTableNames(drizzled::CachedDirectory &, const drizzled::SchemaIdentifier &, drizzled::plugin::TableNameList &);
 
99
 
 
100
  int doDropTable(Session&, const TableIdentifier &);
97
101
 
98
102
  /* operations on FilesystemTableShare */
99
103
  FilesystemTableShare *findOpenTable(const string table_name);
103
107
  uint32_t max_keys()          const { return 0; }
104
108
  uint32_t max_key_parts()     const { return 0; }
105
109
  uint32_t max_key_length()    const { return 0; }
106
 
  bool doDoesTableExist(Session& , const identifier::Table &);
107
 
  int doRenameTable(Session&, const identifier::Table &, const identifier::Table &);
 
110
  bool doDoesTableExist(Session& , const TableIdentifier &);
 
111
  int doRenameTable(Session&, const TableIdentifier &, const TableIdentifier &);
108
112
  void doGetTableIdentifiers(drizzled::CachedDirectory &directory,
109
 
                             const drizzled::identifier::Schema &schema_identifier,
110
 
                             drizzled::identifier::Table::vector &set_of_identifiers);
 
113
                             const drizzled::SchemaIdentifier &schema_identifier,
 
114
                             drizzled::TableIdentifiers &set_of_identifiers);
111
115
private:
112
116
  void getTableNamesFromFilesystem(drizzled::CachedDirectory &directory,
113
 
                                   const drizzled::identifier::Schema &schema_identifier,
 
117
                                   const drizzled::SchemaIdentifier &schema_identifier,
114
118
                                   drizzled::plugin::TableNameList *set_of_names,
115
 
                                   drizzled::identifier::Table::vector *set_of_identifiers);
 
119
                                   drizzled::TableIdentifiers *set_of_identifiers);
116
120
};
117
121
 
118
122
void FilesystemEngine::getTableNamesFromFilesystem(drizzled::CachedDirectory &directory,
119
 
                                                   const drizzled::identifier::Schema &schema_identifier,
 
123
                                                   const drizzled::SchemaIdentifier &schema_identifier,
120
124
                                                   drizzled::plugin::TableNameList *set_of_names,
121
 
                                                   drizzled::identifier::Table::vector *set_of_identifiers)
 
125
                                                   drizzled::TableIdentifiers *set_of_identifiers)
122
126
{
123
127
  drizzled::CachedDirectory::Entries entries= directory.getEntries();
124
128
 
140
144
      char uname[NAME_LEN + 1];
141
145
      uint32_t file_name_len;
142
146
 
143
 
      file_name_len= identifier::Table::filename_to_tablename(filename->c_str(), uname, sizeof(uname));
 
147
      file_name_len= TableIdentifier::filename_to_tablename(filename->c_str(), uname, sizeof(uname));
144
148
      uname[file_name_len - sizeof(FILESYSTEM_EXT) + 1]= '\0';
145
149
      if (set_of_names)
146
150
        set_of_names->insert(uname);
147
151
      if (set_of_identifiers)
148
 
        set_of_identifiers->push_back(identifier::Table(schema_identifier, uname));
 
152
        set_of_identifiers->push_back(TableIdentifier(schema_identifier, uname));
149
153
    }
150
154
  }
151
155
}
152
156
 
 
157
void FilesystemEngine::doGetTableNames(drizzled::CachedDirectory &directory,
 
158
                                       const drizzled::SchemaIdentifier &schema_identifier,
 
159
                                       drizzled::plugin::TableNameList &set_of_names)
 
160
{
 
161
  getTableNamesFromFilesystem(directory, schema_identifier, &set_of_names, NULL);
 
162
}
 
163
 
153
164
void FilesystemEngine::doGetTableIdentifiers(drizzled::CachedDirectory &directory,
154
 
                                             const drizzled::identifier::Schema &schema_identifier,
155
 
                                             drizzled::identifier::Table::vector &set_of_identifiers)
 
165
                                             const drizzled::SchemaIdentifier &schema_identifier,
 
166
                                             drizzled::TableIdentifiers &set_of_identifiers)
156
167
{
157
168
  getTableNamesFromFilesystem(directory, schema_identifier, NULL, &set_of_identifiers);
158
169
}
159
170
 
160
 
int FilesystemEngine::doDropTable(Session &, const identifier::Table &identifier)
 
171
int FilesystemEngine::doDropTable(Session &, const TableIdentifier &identifier)
161
172
{
162
173
  string new_path(identifier.getPath());
163
174
  new_path+= FILESYSTEM_EXT;
169
180
  return err;
170
181
}
171
182
 
172
 
bool FilesystemEngine::doDoesTableExist(Session &, const identifier::Table &identifier)
 
183
bool FilesystemEngine::doDoesTableExist(Session &, const TableIdentifier &identifier)
173
184
{
174
185
  string proto_path(identifier.getPath());
175
186
  proto_path.append(FILESYSTEM_EXT);
281
292
}
282
293
 
283
294
int FilesystemEngine::doGetTableDefinition(Session &,
284
 
                                           const drizzled::identifier::Table &identifier,
 
295
                                           const drizzled::TableIdentifier &identifier,
285
296
                                           drizzled::message::Table &table_proto)
286
297
{
287
298
  string new_path(identifier.getPath());
304
315
    if (not table_proto.IsInitialized())
305
316
    {
306
317
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
307
 
               table_proto.name().empty() ? " " : table_proto.name().c_str(),
308
318
               table_proto.InitializationErrorString().c_str());
309
 
 
310
319
      return ER_CORRUPT_TABLE_DEFINITION;
311
320
    }
312
321
 
318
327
  // then columns of this table are added dynamically here.
319
328
  FormatInfo format;
320
329
  format.parseFromTable(&table_proto);
321
 
  if (not format.isTagFormat() || not format.isFileGiven())
322
 
  {
323
 
    close(fd);
 
330
  if (!format.isTagFormat() || !format.isFileGiven())
324
331
    return EEXIST;
325
 
  }
326
332
 
327
 
  std::vector< std::map<std::string, std::string> > vm;
 
333
  vector< map<string, string> > vm;
328
334
  if (parseTaggedFile(format, vm) != 0)
329
 
  {
330
 
    close(fd);
331
 
 
332
 
    return EEXIST;
333
 
  }
334
 
  if (vm.size() == 0) {
335
 
    close(fd);
336
 
    return EEXIST;
337
 
  }
 
335
    return EEXIST;
 
336
  if (vm.size() == 0)
 
337
    return EEXIST;
338
338
 
339
339
  // we don't care what user provides, just clear them all
340
340
  table_proto.clear_field();
341
341
  // we take the first section as sample
342
 
  std::map<string, string> kv= vm[0];
343
 
  for (std::map<string, string>::iterator iter= kv.begin();
 
342
  map<string, string> kv= vm[0];
 
343
  for (map<string, string>::iterator iter= kv.begin();
344
344
       iter != kv.end();
345
345
       ++iter)
346
346
  {
351
351
    message::Table::Field::StringFieldOptions *stringoption= field->mutable_string_options();
352
352
    stringoption->set_length(iter->second.length() + 1);
353
353
  }
354
 
 
355
 
  close(fd);
356
354
  return EEXIST;
357
355
}
358
356
 
372
370
{
373
371
  Guard g(filesystem_mutex);
374
372
 
375
 
  FilesystemEngine *a_engine= static_cast<FilesystemEngine *>(getEngine());
 
373
  FilesystemEngine *a_engine= static_cast<FilesystemEngine *>(engine);
376
374
  share= a_engine->findOpenTable(table_name);
377
375
 
378
376
  /*
387
385
      return NULL;
388
386
    }
389
387
 
390
 
    share->format.parseFromTable(getTable()->getShare()->getTableProto());
 
388
    share->format.parseFromTable(table->getShare()->getTableProto());
391
389
    if (!share->format.isFileGiven())
392
390
    {
393
391
      return NULL;
417
415
  Guard g(filesystem_mutex);
418
416
 
419
417
  if (!--share->use_count){
420
 
    FilesystemEngine *a_engine= static_cast<FilesystemEngine *>(getEngine());
 
418
    FilesystemEngine *a_engine= static_cast<FilesystemEngine *>(engine);
421
419
    a_engine->deleteOpenTable(share->table_name);
422
420
    pthread_mutex_destroy(&share->mutex);
423
421
    delete share;
456
454
  thread_locked = false;
457
455
}
458
456
 
459
 
FilesystemCursor::FilesystemCursor(drizzled::plugin::StorageEngine &engine_arg, Table &table_arg)
 
457
FilesystemCursor::FilesystemCursor(drizzled::plugin::StorageEngine &engine_arg, TableShare &table_arg)
460
458
  : Cursor(engine_arg, table_arg),
461
459
    file_buff(new TransparentFile),
462
460
    thread_locked(false)
463
461
{
464
462
}
465
463
 
466
 
int FilesystemCursor::doOpen(const drizzled::identifier::Table &identifier, int, uint32_t)
 
464
int FilesystemCursor::doOpen(const drizzled::TableIdentifier &identifier, int, uint32_t)
467
465
{
468
466
  if (!(share= get_share(identifier.getPath().c_str())))
469
467
    return ENOENT;
490
488
 
491
489
int FilesystemCursor::doStartTableScan(bool)
492
490
{
493
 
  sql_command_type = getTable()->getSession()->getSqlCommand();
 
491
  sql_command_type = session_sql_command(table->getSession());
494
492
 
495
493
  if (thread_locked)
496
494
    critical_section_exit();
518
516
 
519
517
int FilesystemCursor::find_current_row(unsigned char *buf)
520
518
{
521
 
  ptrdiff_t row_offset= buf - getTable()->record[0];
 
519
  ptrdiff_t row_offset= buf - table->record[0];
522
520
 
523
521
  next_position= current_position;
524
522
 
525
523
  string content;
526
524
  bool line_done= false;
527
525
  bool line_blank= true;
528
 
  Field **field= getTable()->getFields();
 
526
  Field **field= table->getFields();
529
527
  for (; !line_done && *field; ++next_position)
530
528
  {
531
529
    char ch= file_buff->get_value(next_position);
564
562
        if ((*field)->isReadSet() || (*field)->isWriteSet())
565
563
        {
566
564
          (*field)->setWriteSet();
567
 
          (*field)->store_and_check(CHECK_FIELD_WARN,
568
 
                                    content.c_str(),
569
 
                                    (uint32_t)content.length(),
570
 
                                    &my_charset_bin);
 
565
          (*field)->store(content.c_str(),
 
566
                          (uint32_t)content.length(),
 
567
                          &my_charset_bin,
 
568
                          CHECK_FIELD_WARN);
571
569
        }
572
570
        else
573
 
        {
574
571
          (*field)->set_default();
575
 
        }
576
572
      }
577
573
      else
578
574
        (*field)->set_null();
621
617
    if (tag_depth >= share->vm.size())
622
618
      return HA_ERR_END_OF_FILE;
623
619
 
624
 
    ptrdiff_t row_offset= buf - getTable()->record[0];
625
 
    for (Field **field= getTable()->getFields(); *field; field++)
 
620
    ptrdiff_t row_offset= buf - table->record[0];
 
621
    for (Field **field= table->getFields(); *field; field++)
626
622
    {
627
623
      string key((*field)->field_name);
628
624
      string content= share->vm[tag_depth][key];
634
630
        if ((*field)->isReadSet() || (*field)->isWriteSet())
635
631
        {
636
632
          (*field)->setWriteSet();
637
 
          (*field)->store_and_check(CHECK_FIELD_WARN,
638
 
                                    content.c_str(),
639
 
                                    (uint32_t)content.length(),
640
 
                                    &my_charset_bin);
 
633
          (*field)->store(content.c_str(),
 
634
                          (uint32_t)content.length(),
 
635
                          &my_charset_bin,
 
636
                          CHECK_FIELD_WARN);
641
637
        }
642
638
        else
643
639
        {
701
697
 
702
698
int FilesystemCursor::doEndTableScan()
703
699
{
704
 
  sql_command_type = getTable->getSession()->getSqlCommand();
 
700
  sql_command_type = session_sql_command(table->getSession());
705
701
 
706
702
  if (share->format.isTagFormat())
707
703
  {
762
758
      file_buffer_start= file_buff->read_next();
763
759
  }
764
760
  // close update file
765
 
  if (::fsync(update_file_desc) || 
 
761
  if (::fsync(update_file_desc) ||
766
762
      ::close(update_file_desc))
767
763
    goto error;
768
764
  share->update_file_opened= false;
773
769
  if (::rename(update_file_name.c_str(), share->format.getFileName().c_str()))
774
770
    goto error;
775
771
 
 
772
  // reopen the data file
 
773
  file_desc= ::open(share->format.getFileName().c_str(), O_RDONLY);
776
774
  share->needs_reopen= true;
777
 
 
 
775
  if (file_desc < 0)
 
776
    goto error;
 
777
  err= 0;
778
778
error:
779
779
  err= errno;
780
780
  pthread_mutex_unlock(&share->mutex);
781
781
 
782
782
  if (thread_locked)
783
783
    critical_section_exit();
784
 
 
785
784
  return err;
786
785
}
787
786
 
789
788
{
790
789
  bool first= true;
791
790
  drizzled::String attribute;
792
 
  for (Field **field= getTable()->getFields(); *field; ++field)
 
791
  for (Field **field= table->getFields(); *field; ++field)
793
792
  {
794
793
    if (first == true)
795
794
    {
822
821
  if (share->format.isTagFormat())
823
822
    return 0;
824
823
 
825
 
  sql_command_type = getTable()->getSession()->getSqlCommand();
 
824
  sql_command_type = session_sql_command(table->getSession());
826
825
 
827
826
  critical_section_enter();
828
827
 
893
892
  return 0;
894
893
}
895
894
 
896
 
int FilesystemEngine::doRenameTable(Session&, const identifier::Table &from, const identifier::Table &to)
 
895
int FilesystemEngine::doRenameTable(Session&, const TableIdentifier &from, const TableIdentifier &to)
897
896
{
898
897
  if (rename_file_ext(from.getPath().c_str(), to.getPath().c_str(), FILESYSTEM_EXT))
899
898
    return errno;
907
906
}
908
907
 
909
908
int FilesystemEngine::doCreateTable(Session &,
910
 
                                    Table&,
911
 
                                    const drizzled::identifier::Table &identifier,
912
 
                                    drizzled::message::Table &proto)
 
909
                        Table&,
 
910
                        const drizzled::TableIdentifier &identifier,
 
911
                        drizzled::message::Table &proto)
913
912
{
914
913
  FormatInfo format;
915
914
  format.parseFromTable(&proto);
956
955
  "Filesystem Engine",
957
956
  PLUGIN_LICENSE_GPL,
958
957
  filesystem_init_func, /* Plugin Init */
959
 
  NULL,                       /* depends */
 
958
  NULL,                       /* system variables                */
960
959
  NULL                        /* config options                  */
961
960
}
962
961
DRIZZLE_DECLARE_PLUGIN_END;