~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_base.cc

  • Committer: Brian Aker
  • Date: 2009-06-09 00:45:39 UTC
  • mto: This revision was merged to the branch mainline in revision 1058.
  • Revision ID: brian@gaz-20090609004539-t7q42ogivs1heoz4
Move open_table() to session.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1162
1162
  cond  Condition to wait for
1163
1163
*/
1164
1164
 
1165
 
void wait_for_condition(Session *session, pthread_mutex_t *mutex, pthread_cond_t *cond)
 
1165
void Session::wait_for_condition(pthread_mutex_t *mutex, pthread_cond_t *cond)
1166
1166
{
1167
1167
  /* Wait until the current table is up to date */
1168
 
  const char *proc_info;
1169
 
  session->mysys_var->current_mutex= mutex;
1170
 
  session->mysys_var->current_cond= cond;
1171
 
  proc_info=session->get_proc_info();
1172
 
  session->set_proc_info("Waiting for table");
1173
 
  if (!session->killed)
 
1168
  const char *saved_proc_info;
 
1169
  mysys_var->current_mutex= mutex;
 
1170
  mysys_var->current_cond= cond;
 
1171
  saved_proc_info= get_proc_info();
 
1172
  set_proc_info("Waiting for table");
 
1173
  if (!killed)
1174
1174
    (void) pthread_cond_wait(cond, mutex);
1175
1175
 
1176
1176
  /*
1185
1185
  */
1186
1186
 
1187
1187
  pthread_mutex_unlock(mutex);
1188
 
  pthread_mutex_lock(&session->mysys_var->mutex);
1189
 
  session->mysys_var->current_mutex= 0;
1190
 
  session->mysys_var->current_cond= 0;
1191
 
  session->set_proc_info(proc_info);
1192
 
  pthread_mutex_unlock(&session->mysys_var->mutex);
 
1188
  pthread_mutex_lock(&mysys_var->mutex);
 
1189
  mysys_var->current_mutex= 0;
 
1190
  mysys_var->current_cond= 0;
 
1191
  set_proc_info(saved_proc_info);
 
1192
  pthread_mutex_unlock(&mysys_var->mutex);
1193
1193
}
1194
1194
 
1195
1195
 
1452
1452
*/
1453
1453
 
1454
1454
 
1455
 
Table *open_table(Session *session, TableList *table_list, bool *refresh, uint32_t flags)
 
1455
Table *Session::open_table(TableList *table_list, bool *refresh, uint32_t flags)
1456
1456
{
1457
1457
  register Table *table;
1458
1458
  char key[MAX_DBKEY_LENGTH];
1461
1461
  HASH_SEARCH_STATE state;
1462
1462
 
1463
1463
  /* Parsing of partitioning information from .frm needs session->lex set up. */
1464
 
  assert(session->lex->is_lex_started);
 
1464
  assert(lex->is_lex_started);
1465
1465
 
1466
1466
  /* find a unused table in the open table cache */
1467
1467
  if (refresh)
1468
1468
    *refresh= false;
1469
1469
 
1470
1470
  /* an open table operation needs a lot of the stack space */
1471
 
  if (check_stack_overrun(session, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
 
1471
  if (check_stack_overrun(this, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
1472
1472
    return NULL;
1473
1473
 
1474
 
  if (session->killed)
 
1474
  if (killed)
1475
1475
    return NULL;
1476
1476
 
1477
1477
  key_length= table_list->create_table_def_key(key);
1483
1483
    same name. This block implements the behaviour.
1484
1484
    TODO -> move this block into a separate function.
1485
1485
  */
1486
 
  for (table= session->temporary_tables; table ; table=table->next)
 
1486
  for (table= temporary_tables; table ; table=table->next)
1487
1487
  {
1488
1488
    if (table->s->table_cache_key.length == key_length && !memcmp(table->s->table_cache_key.str, key, key_length))
1489
1489
    {
1498
1498
        my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
1499
1499
        return NULL;
1500
1500
      }
1501
 
      table->query_id= session->query_id;
 
1501
      table->query_id= query_id;
1502
1502
      goto reset;
1503
1503
    }
1504
1504
  }
1518
1518
 
1519
1519
    Note-> refresh_version is currently changed only during FLUSH TABLES.
1520
1520
  */
1521
 
  if (!session->open_tables)
1522
 
    session->version=refresh_version;
1523
 
  else if ((session->version != refresh_version) &&
 
1521
  if (!open_tables)
 
1522
    version= refresh_version;
 
1523
  else if ((version != refresh_version) &&
1524
1524
           ! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
1525
1525
  {
1526
1526
    /* Someone did a refresh while thread was opening tables */
1533
1533
  /*
1534
1534
    Before we test the global cache, we test our local session cache.
1535
1535
  */
1536
 
  if (session->cached_table)
 
1536
  if (cached_table)
1537
1537
  {
1538
1538
    assert(false); /* Not implemented yet */
1539
1539
  }
1594
1594
      if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
1595
1595
      {
1596
1596
        /* Force close at once after usage */
1597
 
        session->version= table->s->version;
 
1597
        version= table->s->version;
1598
1598
        continue;
1599
1599
      }
1600
1600
 
1601
1601
      /* Avoid self-deadlocks by detecting self-dependencies. */
1602
 
      if (table->open_placeholder && table->in_use == session)
 
1602
      if (table->open_placeholder && table->in_use == this)
1603
1603
      {
1604
1604
        pthread_mutex_unlock(&LOCK_open);
1605
1605
        my_error(ER_UPDATE_TABLE_USED, MYF(0), table->s->table_name.str);
1615
1615
        that the tables in question are not used any more. See
1616
1616
        table_is_used call for details.
1617
1617
      */
1618
 
      session->close_old_data_files(false, false);
 
1618
      close_old_data_files(false, false);
1619
1619
 
1620
1620
      /*
1621
1621
        Back-off part 2: try to avoid "busy waiting" on the table:
1632
1632
        after we open first instance but before we open second
1633
1633
        instance.
1634
1634
      */
1635
 
      if (table->in_use != session)
 
1635
      if (table->in_use != this)
1636
1636
      {
1637
1637
        /* wait_for_conditionwill unlock LOCK_open for us */
1638
 
        wait_for_condition(session, &LOCK_open, &COND_refresh);
 
1638
        wait_for_condition(&LOCK_open, &COND_refresh);
1639
1639
      }
1640
1640
      else
1641
1641
      {
1661
1661
    }
1662
1662
    table->prev->next=table->next; /* Remove from unused list */
1663
1663
    table->next->prev=table->prev;
1664
 
    table->in_use= session;
 
1664
    table->in_use= this;
1665
1665
  }
1666
1666
  else
1667
1667
  {
1673
1673
 
1674
1674
    if (table_list->create)
1675
1675
    {
1676
 
      if (ha_table_exists_in_engine(session, table_list->db,
 
1676
      if (ha_table_exists_in_engine(this, table_list->db,
1677
1677
                                    table_list->table_name)
1678
1678
          != HA_ERR_TABLE_EXIST)
1679
1679
      {
1680
1680
        /*
1681
1681
          Table to be created, so we need to create placeholder in table-cache.
1682
1682
        */
1683
 
        if (!(table= session->table_cache_insert_placeholder(key, key_length)))
 
1683
        if (!(table= table_cache_insert_placeholder(key, key_length)))
1684
1684
        {
1685
1685
          pthread_mutex_unlock(&LOCK_open);
1686
1686
          return NULL;
1691
1691
          by other trying to take name-lock.
1692
1692
        */
1693
1693
        table->open_placeholder= true;
1694
 
        table->next= session->open_tables;
1695
 
        session->open_tables= table;
 
1694
        table->next= open_tables;
 
1695
        open_tables= table;
1696
1696
        pthread_mutex_unlock(&LOCK_open);
1697
1697
 
1698
1698
        return table ;
1708
1708
      return NULL;
1709
1709
    }
1710
1710
 
1711
 
    error= open_unireg_entry(session, table, table_list, alias, key, key_length);
 
1711
    error= open_unireg_entry(this, table, table_list, alias, key, key_length);
1712
1712
    if (error != 0)
1713
1713
    {
1714
1714
      free(table);
1721
1721
  pthread_mutex_unlock(&LOCK_open);
1722
1722
  if (refresh)
1723
1723
  {
1724
 
    table->next=session->open_tables; /* Link into simple list */
1725
 
    session->open_tables=table;
 
1724
    table->next= open_tables; /* Link into simple list */
 
1725
    open_tables=table;
1726
1726
  }
1727
1727
  table->reginfo.lock_type= TL_READ; /* Assume read */
1728
1728
 
1729
1729
reset:
1730
1730
  assert(table->s->ref_count > 0 || table->s->tmp_table != NO_TMP_TABLE);
1731
1731
 
1732
 
  if (session->lex->need_correct_ident())
 
1732
  if (lex->need_correct_ident())
1733
1733
    table->alias_name_used= my_strcasecmp(table_alias_charset,
1734
1734
                                          table->s->table_name.str, alias);
1735
1735
  /* Fix alias if table name changes */
1741
1741
  }
1742
1742
 
1743
1743
  /* These variables are also set in reopen_table() */
1744
 
  table->tablenr=session->current_tablenr++;
 
1744
  table->tablenr= current_tablenr++;
1745
1745
  table->used_fields= 0;
1746
1746
  table->const_table= 0;
1747
1747
  table->null_row= false;
2531
2531
      not opened yet. Try to open the table.
2532
2532
    */
2533
2533
    if (tables->table == NULL)
2534
 
      tables->table= open_table(this, tables, &refresh, flags);
 
2534
      tables->table= open_table(tables, &refresh, flags);
2535
2535
 
2536
2536
    if (tables->table == NULL)
2537
2537
    {
2616
2616
 
2617
2617
  session->set_proc_info("Opening table");
2618
2618
  session->current_tablenr= 0;
2619
 
  while (!(table= open_table(session, table_list, &refresh, 0)) &&
 
2619
  while (!(table= session->open_table(table_list, &refresh, 0)) &&
2620
2620
         refresh)
2621
2621
    ;
2622
2622