~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table_proto_write.cc

Extracted the LOAD command into its own class and implementation files.
Removed the corresponding case label from the switch statement.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include <drizzled/message/table.pb.h>
26
26
#include <google/protobuf/io/zero_copy_stream.h>
27
27
#include <google/protobuf/io/zero_copy_stream_impl.h>
28
 
 
29
 
#include <drizzled/table_proto.h>
30
28
using namespace std;
31
29
 
32
 
int fill_table_proto(drizzled::message::Table *table_proto,
33
 
                     const char *table_name,
34
 
                     List<CreateField> &create_fields,
35
 
                     HA_CREATE_INFO *create_info,
36
 
                     uint32_t keys,
37
 
                     KEY *key_info)
 
30
int drizzle_read_table_proto(const char* path, drizzled::message::Table* table)
 
31
{
 
32
  int fd= open(path, O_RDONLY);
 
33
 
 
34
  if(fd==-1)
 
35
    return errno;
 
36
 
 
37
  google::protobuf::io::ZeroCopyInputStream* input=
 
38
    new google::protobuf::io::FileInputStream(fd);
 
39
 
 
40
  if (!table->ParseFromZeroCopyStream(input))
 
41
  {
 
42
    delete input;
 
43
    close(fd);
 
44
    return -1;
 
45
  }
 
46
 
 
47
  delete input;
 
48
  close(fd);
 
49
  return 0;
 
50
}
 
51
 
 
52
static int fill_table_proto(drizzled::message::Table *table_proto,
 
53
                            const char *table_name,
 
54
                            List<CreateField> &create_fields,
 
55
                            HA_CREATE_INFO *create_info,
 
56
                            uint32_t keys,
 
57
                            KEY *key_info)
38
58
{
39
59
  CreateField *field_arg;
40
60
  List_iterator<CreateField> it(create_fields);
512
532
  return 0;
513
533
}
514
534
 
 
535
int copy_table_proto_file(const char *from, const char* to)
 
536
{
 
537
  string dfesrc(from);
 
538
  string dfedst(to);
 
539
  string file_ext = ".dfe";
 
540
 
 
541
  dfesrc.append(file_ext);
 
542
  dfedst.append(file_ext);
 
543
 
 
544
  return my_copy(dfesrc.c_str(), dfedst.c_str(),
 
545
                 MYF(MY_DONT_OVERWRITE_FILE));
 
546
}
 
547
 
515
548
int rename_table_proto_file(const char *from, const char* to)
516
549
{
517
550
  string from_path(from);
533
566
  return my_delete(new_path.c_str(), MYF(0));
534
567
}
535
568
 
536
 
int drizzle_write_proto_file(const std::string file_name,
537
 
                             drizzled::message::Table *table_proto)
538
 
{
539
 
  int fd= open(file_name.c_str(), O_RDWR|O_CREAT|O_TRUNC, my_umask);
540
 
 
541
 
  if (fd == -1)
542
 
    return errno;
 
569
int table_proto_exists(const char *path)
 
570
{
 
571
  string proto_path(path);
 
572
  string file_ext(".dfe");
 
573
  proto_path.append(file_ext);
 
574
 
 
575
  int error= access(proto_path.c_str(), F_OK);
 
576
 
 
577
  if (error == 0)
 
578
    return EEXIST;
 
579
  else
 
580
    return errno;
 
581
}
 
582
 
 
583
static int create_table_proto_file(const char *file_name,
 
584
                                   const char *db,
 
585
                                   const char *table_name,
 
586
                                   drizzled::message::Table *table_proto,
 
587
                                   HA_CREATE_INFO *create_info,
 
588
                                   List<CreateField> &create_fields,
 
589
                                   uint32_t keys,
 
590
                                   KEY *key_info)
 
591
{
 
592
  string new_path(file_name);
 
593
  string file_ext = ".dfe";
 
594
 
 
595
  if(fill_table_proto(table_proto, table_name, create_fields, create_info,
 
596
                      keys, key_info))
 
597
    return -1;
 
598
 
 
599
  new_path.append(file_ext);
 
600
 
 
601
  int fd= open(new_path.c_str(), O_RDWR|O_CREAT|O_TRUNC, my_umask);
 
602
 
 
603
  if(fd==-1)
 
604
  {
 
605
    if(errno==ENOENT)
 
606
      my_error(ER_BAD_DB_ERROR,MYF(0),db);
 
607
    else
 
608
      my_error(ER_CANT_CREATE_TABLE,MYF(0),table_name,errno);
 
609
    return errno;
 
610
  }
543
611
 
544
612
  google::protobuf::io::ZeroCopyOutputStream* output=
545
613
    new google::protobuf::io::FileOutputStream(fd);
546
614
 
547
 
  if (table_proto->SerializeToZeroCopyStream(output) == false)
 
615
  if (!table_proto->SerializeToZeroCopyStream(output))
548
616
  {
549
617
    delete output;
550
618
    close(fd);
582
650
                     drizzled::message::Table *table_proto,
583
651
                     HA_CREATE_INFO *create_info,
584
652
                     List<CreateField> &create_fields,
585
 
                     uint32_t keys, KEY *key_info)
 
653
                     uint32_t keys, KEY *key_info,
 
654
                     bool is_like)
586
655
{
587
656
  /* Proto will blow up unless we give a name */
588
657
  assert(table_name);
589
658
 
590
 
  if (fill_table_proto(table_proto, table_name, create_fields, create_info,
591
 
                      keys, key_info))
592
 
    return 1;
593
 
 
594
 
  string new_path(path);
595
 
  string file_ext = ".dfe";
596
 
 
597
 
  new_path.append(file_ext);
598
 
 
599
 
  int err= 0;
600
 
 
601
 
  StorageEngine* engine= ha_resolve_by_name(session,
602
 
                                            table_proto->engine().name());
603
 
  if (engine->check_flag(HTON_BIT_HAS_DATA_DICTIONARY) == false)
604
 
    err= drizzle_write_proto_file(new_path, table_proto);
605
 
 
606
 
  if (err != 0)
607
 
  {
608
 
    if (err == ENOENT)
609
 
      my_error(ER_BAD_DB_ERROR,MYF(0),db);
610
 
    else
611
 
      my_error(ER_CANT_CREATE_TABLE,MYF(0),table_name,err);
612
 
 
613
 
    goto err_handler;
 
659
  /* For is_like we return once the file has been created */
 
660
  if (is_like)
 
661
  {
 
662
    if (create_table_proto_file(path, db, table_name, table_proto,
 
663
                                create_info,
 
664
                                create_fields, keys, key_info)!=0)
 
665
      return 1;
 
666
 
 
667
    return 0;
 
668
  }
 
669
  /* Here we need to build the full frm from the path */
 
670
  else
 
671
  {
 
672
    if (create_table_proto_file(path, db, table_name, table_proto,
 
673
                                create_info,
 
674
                                create_fields, keys, key_info))
 
675
      return 1;
614
676
  }
615
677
 
616
678
  if (ha_create_table(session, path, db, table_name,
617
 
                      create_info,0, table_proto))
 
679
                      create_info,0))
618
680
    goto err_handler;
619
681
  return 0;
620
682
 
621
683
err_handler:
622
 
  if (engine->check_flag(HTON_BIT_HAS_DATA_DICTIONARY) == false)
623
 
    delete_table_proto_file(path);
 
684
  delete_table_proto_file(path);
624
685
 
625
686
  return 1;
626
687
} /* rea_create_table */