~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table.cc

  • Committer: Brian Aker
  • Date: 2010-05-18 23:58:31 UTC
  • mfrom: (1532.1.9 fix-table)
  • Revision ID: brian@gaz-20100518235831-nji9mm9ju2eekelw
Merge of overall patch for TableShare new() work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
#include <drizzled/item/null.h>
55
55
#include <drizzled/temporal.h>
56
56
 
57
 
#include <drizzled/table_instance.h>
 
57
#include "drizzled/table_share_instance.h"
58
58
 
59
59
#include "drizzled/table_proto.h"
60
60
 
696
696
/****************************************************************************
697
697
 Functions for creating temporary tables.
698
698
****************************************************************************/
699
 
 
700
 
 
701
 
/* Prototypes */
702
 
void free_tmp_table(Session *session, Table *entry);
703
 
 
704
699
/**
705
700
  Create field for temporary table from given field.
706
701
 
806
801
                 uint64_t select_options, ha_rows rows_limit,
807
802
                 const char *table_alias)
808
803
{
809
 
  memory::Root *mem_root_save, own_root(TABLE_ALLOC_BLOCK_SIZE);
810
 
  TableInstance *table;
811
 
  TableShare *share;
 
804
  memory::Root *mem_root_save;
 
805
  Table *table;
812
806
  uint  i,field_count,null_count,null_pack_length;
813
807
  uint32_t  copy_func_count= param->func_count;
814
808
  uint32_t  hidden_null_count, hidden_null_pack_length, hidden_field_count;
872
866
    these items are stored in the temporary table.
873
867
  */
874
868
  if (param->precomputed_group_by)
 
869
  {
875
870
    copy_func_count+= param->sum_func_count;
876
 
 
877
 
  if (!multi_alloc_root(&own_root,
878
 
                        &table, sizeof(*table),
879
 
                        &reg_field, sizeof(Field*) * (field_count+1),
880
 
                        &default_field, sizeof(Field*) * (field_count),
881
 
                        &blob_field, sizeof(uint32_t)*(field_count+1),
882
 
                        &from_field, sizeof(Field*)*field_count,
883
 
                        &copy_func, sizeof(*copy_func)*(copy_func_count+1),
884
 
                        &param->keyinfo, sizeof(*param->keyinfo),
885
 
                        &key_part_info,
886
 
                        sizeof(*key_part_info)*(param->group_parts+1),
887
 
                        &param->start_recinfo,
888
 
                        sizeof(*param->recinfo)*(field_count*2+4),
889
 
                        &tmpname, (uint32_t) strlen(path)+1,
890
 
                        &group_buff, (group && ! using_unique_constraint ?
891
 
                                      param->group_length : 0),
892
 
                        &bitmaps, bitmap_buffer_size(field_count)*2,
893
 
                        NULL))
 
871
  }
 
872
 
 
873
  memory::Root own_root(TABLE_ALLOC_BLOCK_SIZE);
 
874
 
 
875
  if (not own_root.multi_alloc_root(0, &tmpname, (uint32_t) strlen(path)+1, NULL))
 
876
  {
 
877
    return NULL;
 
878
  }
 
879
 
 
880
  strcpy(tmpname, path);
 
881
 
 
882
  TableShareInstance *share= session->getTemporaryShare(tmpname); // This will not go into the tableshare cache, so no key is used.
 
883
 
 
884
  if (not share->getMemRoot()->multi_alloc_root(0, &reg_field, sizeof(Field*) * (field_count+1),
 
885
                                                &default_field, sizeof(Field*) * (field_count),
 
886
                                                &blob_field, sizeof(uint32_t)*(field_count+1),
 
887
                                                &from_field, sizeof(Field*)*field_count,
 
888
                                                &copy_func, sizeof(*copy_func)*(copy_func_count+1),
 
889
                                                &param->keyinfo, sizeof(*param->keyinfo),
 
890
                                                &key_part_info, sizeof(*key_part_info)*(param->group_parts+1),
 
891
                                                &param->start_recinfo, sizeof(*param->recinfo)*(field_count*2+4),
 
892
                                                &group_buff, (group && ! using_unique_constraint ?
 
893
                                                              param->group_length : 0),
 
894
                                                &bitmaps, bitmap_buffer_size(field_count)*2,
 
895
                                                NULL))
894
896
  {
895
897
    return NULL;
896
898
  }
897
899
  /* CopyField belongs to Tmp_Table_Param, allocate it in Session mem_root */
898
900
  if (!(param->copy_field= copy= new (session->mem_root) CopyField[field_count]))
899
901
  {
900
 
    own_root.free_root(MYF(0));
901
902
    return NULL;
902
903
  }
903
904
  param->items_to_copy= copy_func;
904
 
  strcpy(tmpname,path);
905
905
  /* make table according to fields */
906
906
 
907
 
  memset(table, 0, sizeof(*table));
 
907
  table= share->getTable();
 
908
 
908
909
  memset(reg_field, 0, sizeof(Field*)*(field_count+1));
909
910
  memset(default_field, 0, sizeof(Field*) * (field_count));
910
911
  memset(from_field, 0, sizeof(Field*)*field_count);
911
912
 
912
 
  share=  &table->_table_share;
913
 
 
914
913
  table->mem_root= own_root;
915
914
  mem_root_save= session->mem_root;
916
915
  session->mem_root= &table->mem_root;
927
926
  table->keys_in_use_for_query.reset();
928
927
 
929
928
  table->setShare(share);
930
 
  share->init(tmpname, tmpname);
931
929
  share->blob_field= blob_field;
932
930
  share->blob_ptr_size= portable_sizeof_char_ptr;
933
931
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
1091
1089
      (select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) == OPTION_BIG_TABLES)
1092
1090
  {
1093
1091
    share->storage_engine= myisam_engine;
1094
 
    table->cursor= share->db_type()->getCursor(*share, &table->mem_root);
 
1092
    table->cursor= share->db_type()->getCursor(*share, table->getMemRoot());
1095
1093
    if (group &&
1096
1094
        (param->group_parts > table->cursor->getEngine()->max_key_parts() ||
1097
1095
         param->group_length > table->cursor->getEngine()->max_key_length()))
1102
1100
  else
1103
1101
  {
1104
1102
    share->storage_engine= heap_engine;
1105
 
    table->cursor= share->db_type()->getCursor(*share, &table->mem_root);
 
1103
    table->cursor= share->db_type()->getCursor(*share, table->getMemRoot());
1106
1104
  }
1107
1105
  if (! table->cursor)
1108
1106
    goto err;
1135
1133
    uint32_t alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
1136
1134
    share->rec_buff_length= alloc_length;
1137
1135
    if (!(table->record[0]= (unsigned char*)
1138
 
                            table->mem_root.alloc_root(alloc_length*3)))
 
1136
                            table->alloc_root(alloc_length*3)))
1139
1137
    {
1140
1138
      goto err;
1141
1139
    }
1355
1353
    table->distinct= 1;
1356
1354
    share->keys= 1;
1357
1355
    if (!(key_part_info= (KeyPartInfo*)
1358
 
         table->mem_root.alloc_root(keyinfo->key_parts * sizeof(KeyPartInfo))))
 
1356
         table->alloc_root(keyinfo->key_parts * sizeof(KeyPartInfo))))
1359
1357
      goto err;
1360
1358
    memset(key_part_info, 0, keyinfo->key_parts * sizeof(KeyPartInfo));
1361
1359
    table->key_info=keyinfo;
1443
1441
 
1444
1442
err:
1445
1443
  session->mem_root= mem_root_save;
1446
 
  table->free_tmp_table(session);
 
1444
  table= NULL;
 
1445
 
1447
1446
  return NULL;
1448
1447
}
1449
1448
 
1470
1469
 
1471
1470
Table *Session::create_virtual_tmp_table(List<CreateField> &field_list)
1472
1471
{
1473
 
  Session *session= this;
1474
1472
  uint32_t field_count= field_list.elements;
1475
1473
  uint32_t blob_count= 0;
1476
1474
  Field **field;
1480
1478
  uint32_t null_pack_length;              /* NULL representation array length */
1481
1479
  uint32_t *blob_field;
1482
1480
  unsigned char *bitmaps;
1483
 
  TableInstance *table;
1484
 
  TableShare *share;
1485
 
 
1486
 
  if (!multi_alloc_root(session->mem_root,
1487
 
                        &table, sizeof(*table),
1488
 
                        &field, (field_count + 1) * sizeof(Field*),
1489
 
                        &blob_field, (field_count+1) *sizeof(uint32_t),
1490
 
                        &bitmaps, bitmap_buffer_size(field_count)*2,
1491
 
                        NULL))
 
1481
  Table *table;
 
1482
 
 
1483
  TableShareInstance *share= getTemporaryShare(); // This will not go into the tableshare cache, so no key is used.
 
1484
 
 
1485
  if (! share->getMemRoot()->multi_alloc_root(0, &field, (field_count + 1) * sizeof(Field*),
 
1486
                                              &blob_field, (field_count+1) *sizeof(uint32_t),
 
1487
                                              &bitmaps, bitmap_buffer_size(field_count)*2,
 
1488
                                              NULL))
 
1489
  {
1492
1490
    return NULL;
 
1491
  }
1493
1492
 
1494
 
  memset(table, 0, sizeof(*table));
1495
 
  share= &table->_table_share;
 
1493
  table= share->getTable();
1496
1494
  table->field= field;
1497
 
  table->s= share;
1498
1495
  share->blob_field= blob_field;
1499
1496
  share->fields= field_count;
1500
1497
  share->blob_ptr_size= portable_sizeof_char_ptr;
1504
1501
  List_iterator_fast<CreateField> it(field_list);
1505
1502
  while ((cdef= it++))
1506
1503
  {
1507
 
    *field= share->make_field(session->mem_root,
1508
 
                              NULL,
 
1504
    *field= share->make_field(NULL,
1509
1505
                              cdef->length,
1510
1506
                              (cdef->flags & NOT_NULL_FLAG) ? false : true,
1511
1507
                              (unsigned char *) ((cdef->flags & NOT_NULL_FLAG) ? 0 : ""),
1535
1531
  null_pack_length= (null_count + 7)/8;
1536
1532
  share->reclength= record_length + null_pack_length;
1537
1533
  share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
1538
 
  table->record[0]= (unsigned char*) session->alloc(share->rec_buff_length);
 
1534
  table->record[0]= (unsigned char*)alloc(share->rec_buff_length);
1539
1535
  if (!table->record[0])
1540
1536
    goto error;
1541
1537
 
1546
1542
    share->null_bytes= null_pack_length;
1547
1543
  }
1548
1544
 
1549
 
  table->in_use= session;           /* field->reset() may access table->in_use */
 
1545
  table->in_use= this;           /* field->reset() may access table->in_use */
1550
1546
  {
1551
1547
    /* Set up field pointers */
1552
1548
    unsigned char *null_pos= table->record[0];
1735
1731
  memory::Root own_root= mem_root;
1736
1732
  const char *save_proc_info;
1737
1733
 
1738
 
  save_proc_info=session->get_proc_info();
 
1734
  save_proc_info= session->get_proc_info();
1739
1735
  session->set_proc_info("removing tmp table");
1740
1736
 
1741
1737
  // Release latches since this can take a long time
1744
1740
  if (cursor)
1745
1741
  {
1746
1742
    if (db_stat)
 
1743
    {
1747
1744
      cursor->closeMarkForDelete(s->getTableName());
 
1745
    }
1748
1746
 
1749
1747
    TableIdentifier identifier(s->getSchemaName(), s->getTableName(), s->getTableName());
1750
1748
    s->db_type()->doDropTable(*session, identifier);
1888
1886
  memset(null_flags, 255, s->null_bytes);
1889
1887
}
1890
1888
 
1891
 
Table::Table()
1892
 
  : s(NULL),
1893
 
    field(NULL),
1894
 
    cursor(NULL),
1895
 
    next(NULL),
1896
 
    prev(NULL),
1897
 
    read_set(NULL),
1898
 
    write_set(NULL),
1899
 
    tablenr(0),
1900
 
    db_stat(0),
1901
 
    in_use(NULL),
1902
 
    insert_values(NULL),
1903
 
    key_info(NULL),
1904
 
    next_number_field(NULL),
1905
 
    found_next_number_field(NULL),
1906
 
    timestamp_field(NULL),
1907
 
    pos_in_table_list(NULL),
1908
 
    group(NULL),
1909
 
    alias(NULL),
1910
 
    null_flags(NULL),
1911
 
    lock_position(0),
1912
 
    lock_data_start(0),
1913
 
    lock_count(0),
1914
 
    used_fields(0),
1915
 
    status(0),
1916
 
    derived_select_number(0),
1917
 
    current_lock(F_UNLCK),
1918
 
    copy_blobs(false),
1919
 
    maybe_null(false),
1920
 
    null_row(false),
1921
 
    force_index(false),
1922
 
    distinct(false),
1923
 
    const_table(false),
1924
 
    no_rows(false),
1925
 
    key_read(false),
1926
 
    no_keyread(false),
1927
 
    open_placeholder(false),
1928
 
    locked_by_name(false),
1929
 
    no_cache(false),
1930
 
    auto_increment_field_not_null(false),
1931
 
    alias_name_used(false),
1932
 
    query_id(0),
1933
 
    quick_condition_rows(0),
1934
 
    timestamp_field_type(TIMESTAMP_NO_AUTO_SET),
1935
 
    map(0)
 
1889
Table::Table() : 
 
1890
  s(NULL),
 
1891
  field(NULL),
 
1892
  cursor(NULL),
 
1893
  next(NULL),
 
1894
  prev(NULL),
 
1895
  read_set(NULL),
 
1896
  write_set(NULL),
 
1897
  tablenr(0),
 
1898
  db_stat(0),
 
1899
  in_use(NULL),
 
1900
  insert_values(NULL),
 
1901
  key_info(NULL),
 
1902
  next_number_field(NULL),
 
1903
  found_next_number_field(NULL),
 
1904
  timestamp_field(NULL),
 
1905
  pos_in_table_list(NULL),
 
1906
  group(NULL),
 
1907
  alias(NULL),
 
1908
  null_flags(NULL),
 
1909
  lock_position(0),
 
1910
  lock_data_start(0),
 
1911
  lock_count(0),
 
1912
  used_fields(0),
 
1913
  status(0),
 
1914
  derived_select_number(0),
 
1915
  current_lock(F_UNLCK),
 
1916
  copy_blobs(false),
 
1917
  maybe_null(false),
 
1918
  null_row(false),
 
1919
  force_index(false),
 
1920
  distinct(false),
 
1921
  const_table(false),
 
1922
  no_rows(false),
 
1923
  key_read(false),
 
1924
  no_keyread(false),
 
1925
  open_placeholder(false),
 
1926
  locked_by_name(false),
 
1927
  no_cache(false),
 
1928
  auto_increment_field_not_null(false),
 
1929
  alias_name_used(false),
 
1930
  query_id(0),
 
1931
  quick_condition_rows(0),
 
1932
  timestamp_field_type(TIMESTAMP_NO_AUTO_SET),
 
1933
  map(0)
1936
1934
{
 
1935
  memset(&def_read_set, 0, sizeof(MyBitmap)); /**< Default read set of columns */
 
1936
  memset(&def_write_set, 0, sizeof(MyBitmap)); /**< Default write set of columns */
 
1937
  memset(&tmp_set, 0, sizeof(MyBitmap)); /* Not sure about this... */
 
1938
 
1937
1939
  record[0]= (unsigned char *) 0;
1938
1940
  record[1]= (unsigned char *) 0;
1939
1941
 
 
1942
  reginfo.reset();
1940
1943
  covering_keys.reset();
1941
 
 
1942
1944
  quick_keys.reset();
1943
1945
  merge_keys.reset();
1944
1946
 
1952
1954
  memset(quick_key_parts, 0, sizeof(unsigned int) * MAX_KEY);
1953
1955
  memset(quick_n_ranges, 0, sizeof(unsigned int) * MAX_KEY);
1954
1956
 
1955
 
  memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
 
1957
  memset(&mem_root, 0, sizeof(memory::Root));
1956
1958
  memset(&sort, 0, sizeof(filesort_info_st));
1957
1959
}
1958
1960