~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_base.cc

  • Committer: Brian Aker
  • Date: 2010-05-09 23:33:20 UTC
  • mfrom: (1502.1.22 fix-myisam)
  • Revision ID: brian@gaz-20100509233320-2ejod14ukaiqoqjk
Merge in modifications to make TableShare and actual object. 

Crazy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
*/
68
68
Table *unused_tables;                           /* Used by mysql_test */
69
69
HASH open_cache;                                /* Used by mysql_test */
70
 
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
 
70
static int open_unireg_entry(Session *session,
 
71
                             Table *entry,
71
72
                             const char *alias,
72
73
                             char *cache_key, uint32_t cache_key_length);
73
74
void free_cache_entry(void *entry);
84
85
                               bool )
85
86
{
86
87
  Table *entry=(Table*) record;
87
 
  *length= entry->s->table_cache_key.length;
88
 
  return (unsigned char*) entry->s->table_cache_key.str;
 
88
  *length= entry->s->getCacheKeySize();
 
89
  return (unsigned char*) entry->s->getCacheKey();
89
90
}
90
91
 
91
92
HASH *get_open_cache()
140
141
void close_handle_and_leave_table_as_lock(Table *table)
141
142
{
142
143
  TableShare *share, *old_share= table->s;
143
 
  char *key_buff;
144
 
  memory::Root *mem_root= &table->mem_root;
145
144
 
146
145
  assert(table->db_stat);
147
146
 
150
149
    This has to be done to ensure that the table share is removed from
151
150
    the table defintion cache as soon as the last instance is removed
152
151
  */
153
 
  if (multi_alloc_root(mem_root,
154
 
                       &share, sizeof(*share),
155
 
                       &key_buff, old_share->table_cache_key.length,
156
 
                       NULL))
157
 
  {
158
 
    memset(share, 0, sizeof(*share));
159
 
    share->set_table_cache_key(key_buff, old_share->table_cache_key.str,
160
 
                               old_share->table_cache_key.length);
161
 
    share->tmp_table= message::Table::INTERNAL;       // for intern_close_table()
162
 
  }
 
152
  share= new TableShare(const_cast<char *>(old_share->getCacheKey()),  static_cast<uint32_t>(old_share->getCacheKeySize()));
 
153
  share->tmp_table= message::Table::INTERNAL;       // for intern_close_table()
163
154
 
164
155
  table->cursor->close();
165
156
  table->db_stat= 0;                            // Mark cursor closed
178
169
{                                               // Free all structures
179
170
  free_io_cache();
180
171
  if (cursor)                              // Not true if name lock
181
 
    closefrm(true);                     // close cursor
 
172
  {
 
173
    delete_table(true);                 // close cursor
 
174
  }
182
175
}
183
176
 
184
177
/*
595
588
  {
596
589
    if (schema_identifier.compare(table->s->getSchemaName()))
597
590
    {
598
 
      set_of_names.insert(table->s->table_name.str);
 
591
      set_of_names.insert(table->s->getTableName());
599
592
    }
600
593
  }
601
594
}
634
627
  {
635
628
    if (table->s->tmp_table == message::Table::TEMPORARY)
636
629
    {
637
 
      if (identifier.compare(table->s->getSchemaName(), table->s->table_name.str))
 
630
      if (identifier.compare(table->s->getSchemaName(), table->s->getTableName()))
638
631
      {
639
632
        return true;
640
633
      }
651
644
  {
652
645
    if (table->s->tmp_table == message::Table::TEMPORARY)
653
646
    {
654
 
      if (identifier.compare(table->s->getSchemaName(), table->s->table_name.str))
 
647
      if (identifier.compare(table->s->getSchemaName(), table->s->getTableName()))
655
648
      {
656
649
        table_proto.CopyFrom(*(table->s->getTableProto()));
657
650
 
672
665
 
673
666
  for (Table *table= temporary_tables ; table ; table= table->next)
674
667
  {
675
 
    if (table->s->table_cache_key.length == key_length &&
676
 
        not memcmp(table->s->table_cache_key.str, key, key_length))
 
668
    if (table->s->getCacheKeySize() == key_length &&
 
669
        not memcmp(table->s->getCacheKey(), key, key_length))
677
670
    {
678
671
      return table;
679
672
    }
695
688
 
696
689
  for (Table *table= temporary_tables ; table ; table= table->next)
697
690
  {
698
 
    if (table->s->table_cache_key.length == key_length &&
699
 
        not memcmp(table->s->table_cache_key.str, key, key_length))
 
691
    if (table->s->getCacheKeySize() == key_length &&
 
692
        not memcmp(table->s->getCacheKey(), key, key_length))
700
693
 
701
694
      return table;
702
695
  }
779
772
void Session::unlink_open_table(Table *find)
780
773
{
781
774
  char key[MAX_DBKEY_LENGTH];
782
 
  uint32_t key_length= find->s->table_cache_key.length;
 
775
  uint32_t key_length= find->s->getCacheKeySize();
783
776
  Table *list, **prev;
784
777
 
785
778
  safe_mutex_assert_owner(&LOCK_open);
786
779
 
787
 
  memcpy(key, find->s->table_cache_key.str, key_length);
 
780
  memcpy(key, find->s->getCacheKey(), key_length);
788
781
  /*
789
782
    Note that we need to hold LOCK_open while changing the
790
783
    open_tables list. Another thread may work on it.
796
789
  {
797
790
    list= *prev;
798
791
 
799
 
    if (list->s->table_cache_key.length == key_length &&
800
 
        not memcmp(list->s->table_cache_key.str, key, key_length))
 
792
    if (list->s->getCacheKeySize() == key_length &&
 
793
        not memcmp(list->s->getCacheKey(), key, key_length))
801
794
    {
802
795
      /* Remove table from open_tables list. */
803
796
      *prev= list->next;
934
927
 
935
928
  orig_table= *table;
936
929
 
937
 
  if (open_unireg_entry(this, table, table_list, table_name,
938
 
                        table->s->table_cache_key.str,
939
 
                        table->s->table_cache_key.length))
 
930
  if (open_unireg_entry(this, table, table_name,
 
931
                        const_cast<char *>(table->s->getCacheKey()),
 
932
                        table->s->getCacheKeySize()))
940
933
  {
941
934
    table->intern_close_table();
942
935
    /*
1160
1153
  */
1161
1154
  for (table= temporary_tables; table ; table=table->next)
1162
1155
  {
1163
 
    if (table->s->table_cache_key.length == key_length && !memcmp(table->s->table_cache_key.str, key, key_length))
 
1156
    if (table->s->getCacheKeySize() == key_length && !memcmp(table->s->getCacheKey(), key, key_length))
1164
1157
    {
1165
1158
      /*
1166
1159
        We're trying to use the same temporary table twice in a query.
1277
1270
      if (table->open_placeholder && table->in_use == this)
1278
1271
      {
1279
1272
        pthread_mutex_unlock(&LOCK_open);
1280
 
        my_error(ER_UPDATE_TABLE_USED, MYF(0), table->s->table_name.str);
 
1273
        my_error(ER_UPDATE_TABLE_USED, MYF(0), table->s->getTableName());
1281
1274
        return NULL;
1282
1275
      }
1283
1276
 
1383
1376
      return NULL;
1384
1377
    }
1385
1378
 
1386
 
    error= open_unireg_entry(this, table, table_list, alias, key, key_length);
 
1379
    error= open_unireg_entry(this, table, alias, key, key_length);
1387
1380
    if (error != 0)
1388
1381
    {
1389
1382
      free(table);
1397
1390
  if (refresh)
1398
1391
  {
1399
1392
    table->next= open_tables; /* Link into simple list */
1400
 
    open_tables=table;
 
1393
    open_tables= table;
1401
1394
  }
1402
1395
  table->reginfo.lock_type= TL_READ; /* Assume read */
1403
1396
 
1406
1399
 
1407
1400
  if (lex->need_correct_ident())
1408
1401
    table->alias_name_used= my_strcasecmp(table_alias_charset,
1409
 
                                          table->s->table_name.str, alias);
 
1402
                                          table->s->getTableName(), alias);
1410
1403
  /* Fix alias if table name changes */
1411
1404
  if (strcmp(table->alias, alias))
1412
1405
  {
1472
1465
                  table->alias);
1473
1466
#endif
1474
1467
  table_list.db=         const_cast<char *>(table->s->getSchemaName());
1475
 
  table_list.table_name= table->s->table_name.str;
 
1468
  table_list.table_name= table->s->getTableName();
1476
1469
  table_list.table=      table;
1477
1470
 
1478
1471
  if (wait_for_locked_table_names(session, &table_list))
1480
1473
 
1481
1474
  if (open_unireg_entry(session, &tmp, &table_list,
1482
1475
                        table->alias,
1483
 
                        table->s->table_cache_key.str,
1484
 
                        table->s->table_cache_key.length))
 
1476
                        table->s->getCacheKey(),
 
1477
                        table->s->getCacheKeySize()))
1485
1478
    goto end;
1486
1479
 
1487
1480
  /* This list copies variables set by open_table */
1501
1494
  tmp.prev=             table->prev;
1502
1495
 
1503
1496
  if (table->cursor)
1504
 
    table->closefrm(true);              // close cursor, free everything
 
1497
    table->delete_table(true);          // close cursor, free everything
1505
1498
 
1506
1499
  *table= tmp;
1507
1500
  table->default_column_bitmaps();
1568
1561
  */
1569
1562
  for (table= open_tables; table ; table=table->next)
1570
1563
  {
1571
 
    if (!strcmp(table->s->table_name.str, identifier.getTableName().c_str()) &&
 
1564
    if (!strcmp(table->s->getTableName(), identifier.getTableName().c_str()) &&
1572
1565
        !strcasecmp(table->s->getSchemaName(), identifier.getSchemaName().c_str()))
1573
1566
    {
1574
1567
      table->open_placeholder= true;
1773
1766
{
1774
1767
  do
1775
1768
  {
1776
 
    char *key= table->s->table_cache_key.str;
1777
 
    uint32_t key_length= table->s->table_cache_key.length;
 
1769
    char *key= const_cast<char *>(table->s->getCacheKey());
 
1770
    uint32_t key_length= table->s->getCacheKeySize();
1778
1771
 
1779
1772
    HASH_SEARCH_STATE state;
1780
1773
    for (Table *search= (Table*) hash_first(&open_cache, (unsigned char*) key,
1872
1865
  for (table= session->open_tables; table ; table=next)
1873
1866
  {
1874
1867
    next=table->next;
1875
 
    if (!strcmp(table->s->table_name.str, table_name) &&
 
1868
    if (!strcmp(table->s->getTableName(), table_name) &&
1876
1869
        !strcasecmp(table->s->getSchemaName(), db))
1877
1870
    {
1878
1871
      mysql_lock_remove(session, table);
1918
1911
  Table *table;
1919
1912
  for (table= session->open_tables; table ; table= table->next)
1920
1913
  {
1921
 
    if (!strcmp(table->s->table_name.str, table_name) &&
 
1914
    if (!strcmp(table->s->getTableName(), table_name) &&
1922
1915
        !strcmp(table->s->getSchemaName(), db))
1923
1916
    {
1924
1917
      /* If MERGE child, forward lock handling to parent. */
1949
1942
#       Error
1950
1943
*/
1951
1944
 
1952
 
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
 
1945
static int open_unireg_entry(Session *session,
 
1946
                             Table *entry,
1953
1947
                             const char *alias,
1954
1948
                             char *cache_key, uint32_t cache_key_length)
1955
1949
{
1959
1953
 
1960
1954
  safe_mutex_assert_owner(&LOCK_open);
1961
1955
retry:
1962
 
  if (not (share= TableShare::getShare(session, table_list, cache_key,
 
1956
  if (not (share= TableShare::getShare(session, cache_key,
1963
1957
                                       cache_key_length,
1964
 
                                       table_list->i_s_requested_object,
1965
1958
                                       &error)))
1966
1959
    return 1;
1967
1960
 
1970
1963
                                                   HA_OPEN_RNDFILE |
1971
1964
                                                   HA_GET_INDEX |
1972
1965
                                                   HA_TRY_READ_ONLY),
1973
 
                                       session->open_options, entry)))
 
1966
                                       session->open_options, *entry)))
1974
1967
  {
1975
1968
    if (error == 7)                             // Table def changed
1976
1969
    {
2297
2290
{
2298
2291
  Table *new_tmp_table;
2299
2292
  TableShare *share;
2300
 
  char cache_key[MAX_DBKEY_LENGTH], *saved_cache_key, *tmp_path;
2301
 
  uint32_t key_length, path_length;
2302
 
  TableList table_list;
 
2293
  char cache_key[MAX_DBKEY_LENGTH];
 
2294
  uint32_t key_length;
2303
2295
 
2304
 
  table_list.db=         const_cast<char*>(identifier.getSchemaName().c_str());
2305
 
  table_list.table_name= const_cast<char*>(identifier.getTableName().c_str());
2306
2296
  /* Create the cache_key for temporary tables */
2307
 
  key_length= table_list.create_table_def_key(cache_key);
2308
 
  path_length= identifier.getPath().length();
2309
 
 
2310
 
  if (!(new_tmp_table= (Table*) malloc(sizeof(*new_tmp_table) + sizeof(*share) +
2311
 
                                       path_length + 1 + key_length)))
 
2297
  key_length= TableShare::createKey(cache_key, const_cast<char*>(identifier.getSchemaName().c_str()),
 
2298
                                    const_cast<char*>(identifier.getTableName().c_str()));
 
2299
 
 
2300
  share= new TableShare(cache_key, key_length,
 
2301
                        const_cast<char *>(identifier.getPath().c_str()), static_cast<uint32_t>(identifier.getPath().length()));
 
2302
 
 
2303
  if (!(new_tmp_table= (Table*) malloc(sizeof(*new_tmp_table))))
2312
2304
    return NULL;
2313
2305
 
2314
 
  share= (TableShare*) (new_tmp_table+1);
2315
 
  tmp_path= (char*) (share+1);
2316
 
  saved_cache_key= strcpy(tmp_path, identifier.getPath().c_str())+path_length+1;
2317
 
  memcpy(saved_cache_key, cache_key, key_length);
2318
 
 
2319
 
  share->init(saved_cache_key, key_length, strchr(saved_cache_key, '\0')+1, tmp_path);
2320
2306
 
2321
2307
  /*
2322
2308
    First open the share, and then open the table from the share we just opened.
2326
2312
                            (uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
2327
2313
                                        HA_GET_INDEX),
2328
2314
                            ha_open_options,
2329
 
                            new_tmp_table))
 
2315
                            *new_tmp_table))
2330
2316
  {
2331
2317
    /* No need to lock share->mutex as this is not needed for tmp tables */
2332
 
    share->free_table_share();
 
2318
    delete share;
2333
2319
    free((char*) new_tmp_table);
 
2320
 
2334
2321
    return 0;
2335
2322
  }
2336
2323