~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_base.cc

  • Committer: Stewart Smith
  • Date: 2010-11-03 03:27:09 UTC
  • mto: (1902.1.1 build) (1910.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1903.
  • Revision ID: stewart@flamingspork.com-20101103032709-oyvfrc6eb8fzj0mr
fix docs warning: docs/unlock.rst:2: (WARNING/2) Title underline too short.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
  You should have received a copy of the GNU General Public License
13
13
  along with this program; if not, write to the Free Software
14
 
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
14
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
15
 
16
16
 
17
17
/* Basic functions needed by many modules */
31
31
# endif
32
32
#endif
33
33
#include "drizzled/internal/my_pthread.h"
 
34
#include "drizzled/internal/thread_var.h"
34
35
 
35
36
#include <drizzled/sql_select.h>
36
37
#include <drizzled/error.h>
46
47
#include "drizzled/cached_directory.h"
47
48
#include <drizzled/field/timestamp.h>
48
49
#include <drizzled/field/null.h>
49
 
#include "drizzled/memory/multi_malloc.h"
50
50
#include "drizzled/sql_table.h"
51
51
#include "drizzled/global_charset_info.h"
52
52
#include "drizzled/pthread_globals.h"
53
53
#include "drizzled/internal/iocache.h"
 
54
#include "drizzled/drizzled.h"
54
55
#include "drizzled/plugin/authorization.h"
 
56
#include "drizzled/table/temporary.h"
 
57
#include "drizzled/table/placeholder.h"
 
58
#include "drizzled/table/unused.h"
55
59
 
56
60
using namespace std;
57
61
 
60
64
 
61
65
extern bool volatile shutdown_in_progress;
62
66
 
63
 
bool drizzle_rm_tmp_tables();
64
 
 
65
 
/**
66
 
  @defgroup Data_Dictionary Data Dictionary
67
 
  @{
68
 
*/
69
 
Table *unused_tables;                           /* Used by mysql_test */
70
 
HASH open_cache;                                /* Used by mysql_test */
71
 
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
72
 
                             const char *alias,
73
 
                             char *cache_key, uint32_t cache_key_length);
74
 
void free_cache_entry(void *entry);
75
 
unsigned char *table_cache_key(const unsigned char *record,
76
 
                               size_t *length,
77
 
                               bool );
78
 
 
79
 
 
80
 
unsigned char *table_cache_key(const unsigned char *record,
81
 
                               size_t *length,
82
 
                               bool )
83
 
{
84
 
  Table *entry=(Table*) record;
85
 
  *length= entry->s->table_cache_key.length;
86
 
  return (unsigned char*) entry->s->table_cache_key.str;
87
 
}
88
 
 
89
 
HASH *get_open_cache()
90
 
{
91
 
  return &open_cache;
92
 
}
93
 
 
 
67
static bool add_table(table::Concurrent *arg)
 
68
{
 
69
  table::CacheMap &open_cache(table::getCache());
 
70
 
 
71
  table::CacheMap::iterator returnable= open_cache.insert(make_pair(arg->getShare()->getCacheKey(), arg));
 
72
 
 
73
  return not (returnable == open_cache.end());
 
74
}
94
75
 
95
76
bool table_cache_init(void)
96
77
{
97
 
  return hash_init(&open_cache, &my_charset_bin,
98
 
                   (size_t) table_cache_size+16,
99
 
                   0, 0, table_cache_key,
100
 
                   free_cache_entry, 0);
 
78
  return false;
 
79
}
 
80
 
 
81
uint32_t cached_open_tables(void)
 
82
{
 
83
  return table::getCache().size();
101
84
}
102
85
 
103
86
void table_cache_free(void)
104
87
{
105
88
  refresh_version++;                            // Force close of open tables
106
89
 
107
 
  while (unused_tables)
108
 
    hash_delete(&open_cache, (unsigned char*) unused_tables);
109
 
 
110
 
  if (not open_cache.records)                   // Safety first
111
 
    hash_free(&open_cache);
112
 
}
113
 
 
114
 
uint32_t cached_open_tables(void)
115
 
{
116
 
  return open_cache.records;
117
 
}
118
 
 
 
90
  table::getUnused().clear();
 
91
  table::getCache().clear();
 
92
}
119
93
 
120
94
/*
121
95
  Close cursor handle, but leave the table in the table cache
137
111
 
138
112
void close_handle_and_leave_table_as_lock(Table *table)
139
113
{
140
 
  TableShare *share, *old_share= table->s;
141
 
  char *key_buff;
142
 
  memory::Root *mem_root= &table->mem_root;
143
 
 
144
114
  assert(table->db_stat);
 
115
  assert(table->getShare()->getType() == message::Table::STANDARD);
145
116
 
146
117
  /*
147
118
    Make a local copy of the table share and free the current one.
148
119
    This has to be done to ensure that the table share is removed from
149
120
    the table defintion cache as soon as the last instance is removed
150
121
  */
151
 
  if (multi_alloc_root(mem_root,
152
 
                       &share, sizeof(*share),
153
 
                       &key_buff, old_share->table_cache_key.length,
154
 
                       NULL))
155
 
  {
156
 
    memset(share, 0, sizeof(*share));
157
 
    share->set_table_cache_key(key_buff, old_share->table_cache_key.str,
158
 
                               old_share->table_cache_key.length);
159
 
    share->tmp_table= message::Table::INTERNAL;       // for intern_close_table()
160
 
  }
 
122
  TableIdentifier identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName(), message::Table::INTERNAL);
 
123
  const TableIdentifier::Key &key(identifier.getKey());
 
124
  TableShare *share= new TableShare(identifier.getType(),
 
125
                                    identifier,
 
126
                                    const_cast<char *>(key.vector()),  static_cast<uint32_t>(table->getShare()->getCacheKeySize()));
161
127
 
162
128
  table->cursor->close();
163
129
  table->db_stat= 0;                            // Mark cursor closed
164
 
  TableShare::release(table->s);
165
 
  table->s= share;
166
 
  table->cursor->change_table_ptr(table, table->s);
 
130
  TableShare::release(table->getMutableShare());
 
131
  table->setShare(share);
167
132
}
168
133
 
169
134
 
176
141
{                                               // Free all structures
177
142
  free_io_cache();
178
143
  if (cursor)                              // Not true if name lock
179
 
    closefrm(true);                     // close cursor
180
 
}
181
 
 
182
 
/*
183
 
  Remove table from the open table cache
184
 
 
185
 
  SYNOPSIS
186
 
  free_cache_entry()
187
 
  entry         Table to remove
188
 
 
189
 
  NOTE
190
 
  We need to have a lock on LOCK_open when calling this
191
 
*/
192
 
 
193
 
void free_cache_entry(void *entry)
194
 
{
195
 
  Table *table= static_cast<Table *>(entry);
196
 
  table->intern_close_table();
197
 
  if (not table->in_use)
198
144
  {
199
 
    table->next->prev=table->prev;              /* remove from used chain */
200
 
    table->prev->next=table->next;
201
 
    if (table == unused_tables)
202
 
    {
203
 
      unused_tables=unused_tables->next;
204
 
      if (table == unused_tables)
205
 
        unused_tables= NULL;
206
 
    }
 
145
    delete_table(true);                 // close cursor
207
146
  }
208
 
  free(table);
209
147
}
210
148
 
211
149
/* Free resources allocated by filesort() and read_record() */
241
179
  bool result= false;
242
180
  Session *session= this;
243
181
 
244
 
  pthread_mutex_lock(&LOCK_open); /* Optionally lock for remove tables from open_cahe if not in use */
245
 
 
246
 
  if (tables == NULL)
247
182
  {
248
 
    refresh_version++;                          // Force close of open tables
249
 
    while (unused_tables)
250
 
      hash_delete(&open_cache,(unsigned char*) unused_tables);
 
183
    LOCK_open.lock(); /* Optionally lock for remove tables from open_cahe if not in use */
 
184
 
 
185
    if (tables == NULL)
 
186
    {
 
187
      refresh_version++;                                // Force close of open tables
 
188
 
 
189
      table::getUnused().clear();
 
190
 
 
191
      if (wait_for_refresh)
 
192
      {
 
193
        /*
 
194
          Other threads could wait in a loop in open_and_lock_tables(),
 
195
          trying to lock one or more of our tables.
 
196
 
 
197
          If they wait for the locks in thr_multi_lock(), their lock
 
198
          request is aborted. They loop in open_and_lock_tables() and
 
199
          enter open_table(). Here they notice the table is refreshed and
 
200
          wait for COND_refresh. Then they loop again in
 
201
          openTablesLock() and this time open_table() succeeds. At
 
202
          this moment, if we (the FLUSH TABLES thread) are scheduled and
 
203
          on another FLUSH TABLES enter close_cached_tables(), they could
 
204
          awake while we sleep below, waiting for others threads (us) to
 
205
          close their open tables. If this happens, the other threads
 
206
          would find the tables unlocked. They would get the locks, one
 
207
          after the other, and could do their destructive work. This is an
 
208
          issue if we have LOCK TABLES in effect.
 
209
 
 
210
          The problem is that the other threads passed all checks in
 
211
          open_table() before we refresh the table.
 
212
 
 
213
          The fix for this problem is to set some_tables_deleted for all
 
214
          threads with open tables. These threads can still get their
 
215
          locks, but will immediately release them again after checking
 
216
          this variable. They will then loop in openTablesLock()
 
217
          again. There they will wait until we update all tables version
 
218
          below.
 
219
 
 
220
          Setting some_tables_deleted is done by table::Cache::singleton().removeTable()
 
221
          in the other branch.
 
222
 
 
223
          In other words (reviewer suggestion): You need this setting of
 
224
          some_tables_deleted for the case when table was opened and all
 
225
          related checks were passed before incrementing refresh_version
 
226
          (which you already have) but attempt to lock the table happened
 
227
          after the call to Session::close_old_data_files() i.e. after removal of
 
228
          current thread locks.
 
229
        */
 
230
        for (table::CacheMap::const_iterator iter= table::getCache().begin();
 
231
             iter != table::getCache().end();
 
232
             iter++)
 
233
        {
 
234
          Table *table= (*iter).second;
 
235
          if (table->in_use)
 
236
            table->in_use->some_tables_deleted= false;
 
237
        }
 
238
      }
 
239
    }
 
240
    else
 
241
    {
 
242
      bool found= false;
 
243
      for (TableList *table= tables; table; table= table->next_local)
 
244
      {
 
245
        TableIdentifier identifier(table->getSchemaName(), table->getTableName());
 
246
        if (table::Cache::singleton().removeTable(session, identifier,
 
247
                                    RTFC_OWNED_BY_Session_FLAG))
 
248
        {
 
249
          found= true;
 
250
        }
 
251
      }
 
252
      if (!found)
 
253
        wait_for_refresh= false;                        // Nothing to wait for
 
254
    }
251
255
 
252
256
    if (wait_for_refresh)
253
257
    {
254
258
      /*
255
 
        Other threads could wait in a loop in open_and_lock_tables(),
256
 
        trying to lock one or more of our tables.
257
 
 
258
 
        If they wait for the locks in thr_multi_lock(), their lock
259
 
        request is aborted. They loop in open_and_lock_tables() and
260
 
        enter open_table(). Here they notice the table is refreshed and
261
 
        wait for COND_refresh. Then they loop again in
262
 
        openTablesLock() and this time open_table() succeeds. At
263
 
        this moment, if we (the FLUSH TABLES thread) are scheduled and
264
 
        on another FLUSH TABLES enter close_cached_tables(), they could
265
 
        awake while we sleep below, waiting for others threads (us) to
266
 
        close their open tables. If this happens, the other threads
267
 
        would find the tables unlocked. They would get the locks, one
268
 
        after the other, and could do their destructive work. This is an
269
 
        issue if we have LOCK TABLES in effect.
270
 
 
271
 
        The problem is that the other threads passed all checks in
272
 
        open_table() before we refresh the table.
273
 
 
274
 
        The fix for this problem is to set some_tables_deleted for all
275
 
        threads with open tables. These threads can still get their
276
 
        locks, but will immediately release them again after checking
277
 
        this variable. They will then loop in openTablesLock()
278
 
        again. There they will wait until we update all tables version
279
 
        below.
280
 
 
281
 
        Setting some_tables_deleted is done by remove_table_from_cache()
282
 
        in the other branch.
283
 
 
284
 
        In other words (reviewer suggestion): You need this setting of
285
 
        some_tables_deleted for the case when table was opened and all
286
 
        related checks were passed before incrementing refresh_version
287
 
        (which you already have) but attempt to lock the table happened
288
 
        after the call to Session::close_old_data_files() i.e. after removal of
289
 
        current thread locks.
 
259
        If there is any table that has a lower refresh_version, wait until
 
260
        this is closed (or this thread is killed) before returning
290
261
      */
291
 
      for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
 
262
      session->mysys_var->current_mutex= &LOCK_open;
 
263
      session->mysys_var->current_cond= &COND_refresh;
 
264
      session->set_proc_info("Flushing tables");
 
265
 
 
266
      session->close_old_data_files();
 
267
 
 
268
      bool found= true;
 
269
      /* Wait until all threads has closed all the tables we had locked */
 
270
      while (found && ! session->killed)
292
271
      {
293
 
        Table *table=(Table*) hash_element(&open_cache,idx);
294
 
        if (table->in_use)
295
 
          table->in_use->some_tables_deleted= false;
 
272
        found= false;
 
273
        for (table::CacheMap::const_iterator iter= table::getCache().begin();
 
274
             iter != table::getCache().end();
 
275
             iter++)
 
276
        {
 
277
          Table *table= (*iter).second;
 
278
          /* Avoid a self-deadlock. */
 
279
          if (table->in_use == session)
 
280
            continue;
 
281
          /*
 
282
            Note that we wait here only for tables which are actually open, and
 
283
            not for placeholders with Table::open_placeholder set. Waiting for
 
284
            latter will cause deadlock in the following scenario, for example:
 
285
 
 
286
            conn1-> lock table t1 write;
 
287
            conn2-> lock table t2 write;
 
288
            conn1-> flush tables;
 
289
            conn2-> flush tables;
 
290
 
 
291
            It also does not make sense to wait for those of placeholders that
 
292
            are employed by CREATE TABLE as in this case table simply does not
 
293
            exist yet.
 
294
          */
 
295
          if (table->needs_reopen_or_name_lock() && (table->db_stat ||
 
296
                                                     (table->open_placeholder && wait_for_placeholders)))
 
297
          {
 
298
            found= true;
 
299
            boost_unique_lock_t scoped(LOCK_open, boost::adopt_lock_t());
 
300
            COND_refresh.wait(scoped);
 
301
            scoped.release();
 
302
            break;
 
303
          }
 
304
        }
296
305
      }
297
 
    }
298
 
  }
299
 
  else
300
 
  {
301
 
    bool found= false;
302
 
    for (TableList *table= tables; table; table= table->next_local)
303
 
    {
304
 
      if (remove_table_from_cache(session, table->db, table->table_name,
305
 
                                  RTFC_OWNED_BY_Session_FLAG))
306
 
        found= true;
307
 
    }
308
 
    if (!found)
309
 
      wait_for_refresh= false;                  // Nothing to wait for
310
 
  }
311
 
 
312
 
  if (wait_for_refresh)
313
 
  {
314
 
    /*
315
 
      If there is any table that has a lower refresh_version, wait until
316
 
      this is closed (or this thread is killed) before returning
317
 
    */
318
 
    session->mysys_var->current_mutex= &LOCK_open;
319
 
    session->mysys_var->current_cond= &COND_refresh;
320
 
    session->set_proc_info("Flushing tables");
321
 
 
322
 
    session->close_old_data_files();
323
 
 
324
 
    bool found= true;
325
 
    /* Wait until all threads has closed all the tables we had locked */
326
 
    while (found && ! session->killed)
327
 
    {
328
 
      found= false;
329
 
      for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
 
306
      /*
 
307
        No other thread has the locked tables open; reopen them and get the
 
308
        old locks. This should always succeed (unless some external process
 
309
        has removed the tables)
 
310
      */
 
311
      result= session->reopen_tables(true, true);
 
312
 
 
313
      /* Set version for table */
 
314
      for (Table *table= session->open_tables; table ; table= table->getNext())
330
315
      {
331
 
        Table *table=(Table*) hash_element(&open_cache,idx);
332
 
        /* Avoid a self-deadlock. */
333
 
        if (table->in_use == session)
334
 
          continue;
335
316
        /*
336
 
          Note that we wait here only for tables which are actually open, and
337
 
          not for placeholders with Table::open_placeholder set. Waiting for
338
 
          latter will cause deadlock in the following scenario, for example:
339
 
 
340
 
conn1: lock table t1 write;
341
 
conn2: lock table t2 write;
342
 
conn1: flush tables;
343
 
conn2: flush tables;
344
 
 
345
 
It also does not make sense to wait for those of placeholders that
346
 
are employed by CREATE TABLE as in this case table simply does not
347
 
exist yet.
 
317
          Preserve the version (0) of write locked tables so that a impending
 
318
          global read lock won't sneak in.
348
319
        */
349
 
        if (table->needs_reopen_or_name_lock() && (table->db_stat ||
350
 
                                                   (table->open_placeholder && wait_for_placeholders)))
351
 
        {
352
 
          found= true;
353
 
          pthread_cond_wait(&COND_refresh,&LOCK_open);
354
 
          break;
355
 
        }
 
320
        if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
 
321
          table->getMutableShare()->refreshVersion();
356
322
      }
357
323
    }
358
 
    /*
359
 
      No other thread has the locked tables open; reopen them and get the
360
 
      old locks. This should always succeed (unless some external process
361
 
      has removed the tables)
362
 
    */
363
 
    result= session->reopen_tables(true, true);
364
324
 
365
 
    /* Set version for table */
366
 
    for (Table *table= session->open_tables; table ; table= table->next)
367
 
    {
368
 
      /*
369
 
        Preserve the version (0) of write locked tables so that a impending
370
 
        global read lock won't sneak in.
371
 
      */
372
 
      if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
373
 
        table->s->version= refresh_version;
374
 
    }
 
325
    LOCK_open.unlock();
375
326
  }
376
327
 
377
 
  pthread_mutex_unlock(&LOCK_open);
378
 
 
379
328
  if (wait_for_refresh)
380
329
  {
381
 
    pthread_mutex_lock(&session->mysys_var->mutex);
 
330
    boost_unique_lock_t scopedLock(session->mysys_var->mutex);
382
331
    session->mysys_var->current_mutex= 0;
383
332
    session->mysys_var->current_cond= 0;
384
333
    session->set_proc_info(0);
385
 
    pthread_mutex_unlock(&session->mysys_var->mutex);
386
334
  }
387
335
 
388
336
  return result;
396
344
bool Session::free_cached_table()
397
345
{
398
346
  bool found_old_table= false;
399
 
  Table *table= open_tables;
 
347
  table::Concurrent *table= static_cast<table::Concurrent *>(open_tables);
400
348
 
401
 
  safe_mutex_assert_owner(&LOCK_open);
 
349
  safe_mutex_assert_owner(LOCK_open.native_handle());
402
350
  assert(table->key_read == 0);
403
351
  assert(!table->cursor || table->cursor->inited == Cursor::NONE);
404
352
 
405
 
  open_tables= table->next;
 
353
  open_tables= table->getNext();
406
354
 
407
355
  if (table->needs_reopen_or_name_lock() ||
408
356
      version != refresh_version || !table->db_stat)
409
357
  {
410
 
    hash_delete(&open_cache,(unsigned char*) table);
 
358
    table::remove_table(table);
411
359
    found_old_table= true;
412
360
  }
413
361
  else
420
368
 
421
369
    /* Free memory and reset for next loop */
422
370
    table->cursor->ha_reset();
423
 
    table->in_use= false;
 
371
    table->in_use= NULL;
424
372
 
425
 
    if (unused_tables)
426
 
    {
427
 
      table->next= unused_tables;               /* Link in last */
428
 
      table->prev= unused_tables->prev;
429
 
      unused_tables->prev= table;
430
 
      table->prev->next= table;
431
 
    }
432
 
    else
433
 
      unused_tables= table->next=table->prev=table;
 
373
    table::getUnused().link(table);
434
374
  }
435
375
 
436
376
  return found_old_table;
449
389
{
450
390
  bool found_old_table= false;
451
391
 
452
 
  safe_mutex_assert_not_owner(&LOCK_open);
 
392
  safe_mutex_assert_not_owner(LOCK_open.native_handle());
453
393
 
454
 
  pthread_mutex_lock(&LOCK_open); /* Close all open tables on Session */
 
394
  boost_unique_lock_t scoped_lock(LOCK_open); /* Close all open tables on Session */
455
395
 
456
396
  while (open_tables)
 
397
  {
457
398
    found_old_table|= free_cached_table();
 
399
  }
458
400
  some_tables_deleted= false;
459
401
 
460
402
  if (found_old_table)
462
404
    /* Tell threads waiting for refresh that something has happened */
463
405
    broadcast_refresh();
464
406
  }
465
 
 
466
 
  pthread_mutex_unlock(&LOCK_open);
467
407
}
468
408
 
469
409
/*
491
431
{
492
432
  for (; table; table= table->*link )
493
433
  {
494
 
    if ((table->table == 0 || table->table->s->tmp_table == message::Table::STANDARD) &&
495
 
        strcasecmp(table->db, db_name) == 0 &&
496
 
        strcasecmp(table->table_name, table_name) == 0)
 
434
    if ((table->table == 0 || table->table->getShare()->getType() == message::Table::STANDARD) &&
 
435
        strcasecmp(table->getSchemaName(), db_name) == 0 &&
 
436
        strcasecmp(table->getTableName(), table_name) == 0)
497
437
      break;
498
438
  }
499
439
  return table;
555
495
  if (table->table)
556
496
  {
557
497
    /* temporary table is always unique */
558
 
    if (table->table && table->table->s->tmp_table != message::Table::STANDARD)
 
498
    if (table->table && table->table->getShare()->getType() != message::Table::STANDARD)
559
499
      return 0;
560
500
    table= table->find_underlying_table(table->table);
561
501
    /*
564
504
    */
565
505
    assert(table);
566
506
  }
567
 
  d_name= table->db;
568
 
  t_name= table->table_name;
 
507
  d_name= table->getSchemaName();
 
508
  t_name= table->getTableName();
569
509
  t_alias= table->alias;
570
510
 
571
511
  for (;;)
586
526
}
587
527
 
588
528
 
589
 
void Session::doGetTableNames(SchemaIdentifier &schema_identifier,
 
529
void Session::doGetTableNames(const SchemaIdentifier &schema_identifier,
590
530
                              std::set<std::string>& set_of_names)
591
531
{
592
 
  for (Table *table= temporary_tables ; table ; table= table->next)
 
532
  for (Table *table= temporary_tables ; table ; table= table->getNext())
593
533
  {
594
 
    if (schema_identifier.compare(table->s->getSchemaName()))
 
534
    if (schema_identifier.compare(table->getShare()->getSchemaName()))
595
535
    {
596
 
      set_of_names.insert(table->s->table_name.str);
 
536
      set_of_names.insert(table->getShare()->getTableName());
597
537
    }
598
538
  }
599
539
}
600
540
 
601
541
void Session::doGetTableNames(CachedDirectory &,
602
 
                              SchemaIdentifier &schema_identifier,
 
542
                              const SchemaIdentifier &schema_identifier,
603
543
                              std::set<std::string> &set_of_names)
604
544
{
605
545
  doGetTableNames(schema_identifier, set_of_names);
606
546
}
607
547
 
608
 
void Session::doGetTableIdentifiers(SchemaIdentifier &schema_identifier,
 
548
void Session::doGetTableIdentifiers(const SchemaIdentifier &schema_identifier,
609
549
                                    TableIdentifiers &set_of_identifiers)
610
550
{
611
 
  for (Table *table= temporary_tables ; table ; table= table->next)
 
551
  for (Table *table= temporary_tables ; table ; table= table->getNext())
612
552
  {
613
 
    if (schema_identifier.compare(table->s->getSchemaName()))
 
553
    if (schema_identifier.compare(table->getShare()->getSchemaName()))
614
554
    {
615
555
      set_of_identifiers.push_back(TableIdentifier(table->getShare()->getSchemaName(),
616
556
                                                   table->getShare()->getTableName(),
620
560
}
621
561
 
622
562
void Session::doGetTableIdentifiers(CachedDirectory &,
623
 
                                    SchemaIdentifier &schema_identifier,
 
563
                                    const SchemaIdentifier &schema_identifier,
624
564
                                    TableIdentifiers &set_of_identifiers)
625
565
{
626
566
  doGetTableIdentifiers(schema_identifier, set_of_identifiers);
627
567
}
628
568
 
629
 
bool Session::doDoesTableExist(TableIdentifier &identifier)
 
569
bool Session::doDoesTableExist(const TableIdentifier &identifier)
630
570
{
631
 
  for (Table *table= temporary_tables ; table ; table= table->next)
 
571
  for (Table *table= temporary_tables ; table ; table= table->getNext())
632
572
  {
633
 
    if (table->s->tmp_table == message::Table::TEMPORARY)
 
573
    if (table->getShare()->getType() == message::Table::TEMPORARY)
634
574
    {
635
 
      if (identifier.compare(table->s->getSchemaName(), table->s->table_name.str))
 
575
      if (identifier.getKey() == table->getShare()->getCacheKey())
636
576
      {
637
577
        return true;
638
578
      }
642
582
  return false;
643
583
}
644
584
 
645
 
int Session::doGetTableDefinition(TableIdentifier &identifier,
 
585
int Session::doGetTableDefinition(const TableIdentifier &identifier,
646
586
                                  message::Table &table_proto)
647
587
{
648
 
  for (Table *table= temporary_tables ; table ; table= table->next)
 
588
  for (Table *table= temporary_tables ; table ; table= table->getNext())
649
589
  {
650
 
    if (table->s->tmp_table == message::Table::TEMPORARY)
 
590
    if (table->getShare()->getType() == message::Table::TEMPORARY)
651
591
    {
652
 
      if (identifier.compare(table->s->getSchemaName(), table->s->table_name.str))
 
592
      if (identifier.getKey() == table->getShare()->getCacheKey())
653
593
      {
654
 
        table_proto.CopyFrom(*(table->s->getTableProto()));
 
594
        table_proto.CopyFrom(*(table->getShare()->getTableProto()));
655
595
 
656
596
        return EEXIST;
657
597
      }
661
601
  return ENOENT;
662
602
}
663
603
 
664
 
Table *Session::find_temporary_table(const char *new_db, const char *table_name)
665
 
{
666
 
  char  key[MAX_DBKEY_LENGTH];
667
 
  uint  key_length;
668
 
 
669
 
  key_length= TableShare::createKey(key, new_db, table_name);
670
 
 
671
 
  for (Table *table= temporary_tables ; table ; table= table->next)
672
 
  {
673
 
    if (table->s->table_cache_key.length == key_length &&
674
 
        not memcmp(table->s->table_cache_key.str, key, key_length))
675
 
    {
676
 
      return table;
677
 
    }
678
 
  }
679
 
  return NULL;                               // Not a temporary table
680
 
}
681
 
 
682
 
Table *Session::find_temporary_table(TableList *table_list)
683
 
{
684
 
  return find_temporary_table(table_list->db, table_list->table_name);
685
 
}
686
 
 
687
 
Table *Session::find_temporary_table(TableIdentifier &identifier)
688
 
{
689
 
  char  key[MAX_DBKEY_LENGTH];
690
 
  uint  key_length;
691
 
 
692
 
  key_length= TableShare::createKey(key, identifier);
693
 
 
694
 
  for (Table *table= temporary_tables ; table ; table= table->next)
695
 
  {
696
 
    if (table->s->table_cache_key.length == key_length &&
697
 
        not memcmp(table->s->table_cache_key.str, key, key_length))
698
 
 
 
604
Table *Session::find_temporary_table(const TableIdentifier &identifier)
 
605
{
 
606
  for (Table *table= temporary_tables ; table ; table= table->getNext())
 
607
  {
 
608
    if (identifier.getKey() == table->getShare()->getCacheKey())
699
609
      return table;
700
610
  }
701
611
 
729
639
  @retval -1  the table is in use by a outer query
730
640
*/
731
641
 
732
 
int Session::drop_temporary_table(TableList *table_list)
 
642
int Session::drop_temporary_table(const drizzled::TableIdentifier &identifier)
733
643
{
734
644
  Table *table;
735
645
 
736
 
  if (not (table= find_temporary_table(table_list)))
 
646
  if (not (table= find_temporary_table(identifier)))
737
647
    return 1;
738
648
 
739
649
  /* Table might be in use by some outer statement. */
740
650
  if (table->query_id && table->query_id != query_id)
741
651
  {
742
 
    my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
 
652
    my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
743
653
    return -1;
744
654
  }
745
655
 
749
659
}
750
660
 
751
661
 
752
 
/* move table first in unused links */
753
 
 
754
 
static void relink_unused(Table *table)
755
 
{
756
 
  if (table != unused_tables)
757
 
  {
758
 
    table->prev->next=table->next;              /* Remove from unused list */
759
 
    table->next->prev=table->prev;
760
 
    table->next=unused_tables;                  /* Link in unused tables */
761
 
    table->prev=unused_tables->prev;
762
 
    unused_tables->prev->next=table;
763
 
    unused_tables->prev=table;
764
 
    unused_tables=table;
765
 
  }
766
 
}
767
 
 
768
 
 
769
662
/**
770
663
  Remove all instances of table from thread's open list and
771
664
  table cache.
772
665
 
773
666
  @param  session     Thread context
774
667
  @param  find    Table to remove
 
668
 
 
669
  @note because we risk the chance of deleting the share, we can't assume that it will exist past, this should be modified once we can use a TableSharePtr here.
775
670
*/
776
671
 
777
672
void Session::unlink_open_table(Table *find)
778
673
{
779
 
  char key[MAX_DBKEY_LENGTH];
780
 
  uint32_t key_length= find->s->table_cache_key.length;
781
 
  Table *list, **prev;
782
 
 
783
 
  safe_mutex_assert_owner(&LOCK_open);
784
 
 
785
 
  memcpy(key, find->s->table_cache_key.str, key_length);
 
674
  const TableIdentifier::Key find_key(find->getShare()->getCacheKey());
 
675
  Table **prev;
 
676
  safe_mutex_assert_owner(LOCK_open.native_handle());
 
677
 
786
678
  /*
787
679
    Note that we need to hold LOCK_open while changing the
788
680
    open_tables list. Another thread may work on it.
789
 
    (See: remove_table_from_cache(), mysql_wait_completed_table())
 
681
    (See: table::Cache::singleton().removeTable(), mysql_wait_completed_table())
790
682
    Closing a MERGE child before the parent would be fatal if the
791
683
    other thread tries to abort the MERGE lock in between.
792
684
  */
793
685
  for (prev= &open_tables; *prev; )
794
686
  {
795
 
    list= *prev;
 
687
    Table *list= *prev;
796
688
 
797
 
    if (list->s->table_cache_key.length == key_length &&
798
 
        not memcmp(list->s->table_cache_key.str, key, key_length))
 
689
    if (list->getShare()->getCacheKey() == find_key)
799
690
    {
800
691
      /* Remove table from open_tables list. */
801
 
      *prev= list->next;
 
692
      *prev= list->getNext();
 
693
 
802
694
      /* Close table. */
803
 
      hash_delete(&open_cache,(unsigned char*) list); // Close table
 
695
      table::remove_table(static_cast<table::Concurrent *>(list));
804
696
    }
805
697
    else
806
698
    {
807
699
      /* Step to next entry in open_tables list. */
808
 
      prev= &list->next;
 
700
      prev= list->getNextPtr();
809
701
    }
810
702
  }
811
703
 
835
727
 
836
728
void Session::drop_open_table(Table *table, TableIdentifier &identifier)
837
729
{
838
 
  if (table->s->tmp_table)
 
730
  if (table->getShare()->getType())
839
731
  {
840
732
    close_temporary_table(table);
841
733
  }
842
734
  else
843
735
  {
844
 
    pthread_mutex_lock(&LOCK_open); /* Close and drop a table (AUX routine) */
 
736
    boost_unique_lock_t scoped_lock(LOCK_open); /* Close and drop a table (AUX routine) */
845
737
    /*
846
738
      unlink_open_table() also tells threads waiting for refresh or close
847
739
      that something has happened.
848
740
    */
849
741
    unlink_open_table(table);
850
742
    quick_rm_table(*this, identifier);
851
 
    pthread_mutex_unlock(&LOCK_open);
852
743
  }
853
744
}
854
745
 
864
755
  cond  Condition to wait for
865
756
*/
866
757
 
867
 
void Session::wait_for_condition(pthread_mutex_t *mutex, pthread_cond_t *cond)
 
758
void Session::wait_for_condition(boost::mutex &mutex, boost::condition_variable_any &cond)
868
759
{
869
760
  /* Wait until the current table is up to date */
870
761
  const char *saved_proc_info;
871
 
  mysys_var->current_mutex= mutex;
872
 
  mysys_var->current_cond= cond;
 
762
  mysys_var->current_mutex= &mutex;
 
763
  mysys_var->current_cond= &cond;
873
764
  saved_proc_info= get_proc_info();
874
765
  set_proc_info("Waiting for table");
875
 
  if (!killed)
876
 
    (void) pthread_cond_wait(cond, mutex);
877
 
 
878
 
  /*
879
 
    We must unlock mutex first to avoid deadlock becasue conditions are
880
 
    sent to this thread by doing locks in the following order:
881
 
    lock(mysys_var->mutex)
882
 
    lock(mysys_var->current_mutex)
883
 
 
884
 
    One by effect of this that one can only use wait_for_condition with
885
 
    condition variables that are guranteed to not disapper (freed) even if this
886
 
    mutex is unlocked
887
 
  */
888
 
 
889
 
  pthread_mutex_unlock(mutex);
890
 
  pthread_mutex_lock(&mysys_var->mutex);
 
766
  {
 
767
    /*
 
768
      We must unlock mutex first to avoid deadlock becasue conditions are
 
769
      sent to this thread by doing locks in the following order:
 
770
      lock(mysys_var->mutex)
 
771
      lock(mysys_var->current_mutex)
 
772
 
 
773
      One by effect of this that one can only use wait_for_condition with
 
774
      condition variables that are guranteed to not disapper (freed) even if this
 
775
      mutex is unlocked
 
776
    */
 
777
    boost_unique_lock_t scopedLock(mutex, boost::adopt_lock_t());
 
778
    if (not killed)
 
779
    {
 
780
      cond.wait(scopedLock);
 
781
    }
 
782
  }
 
783
  boost_unique_lock_t mysys_scopedLock(mysys_var->mutex);
891
784
  mysys_var->current_mutex= 0;
892
785
  mysys_var->current_cond= 0;
893
786
  set_proc_info(saved_proc_info);
894
 
  pthread_mutex_unlock(&mysys_var->mutex);
895
 
}
896
 
 
897
 
 
898
 
/*
899
 
  Open table which is already name-locked by this thread.
900
 
 
901
 
  SYNOPSIS
902
 
  reopen_name_locked_table()
903
 
  session         Thread handle
904
 
  table_list  TableList object for table to be open, TableList::table
905
 
  member should point to Table object which was used for
906
 
  name-locking.
907
 
  link_in     true  - if Table object for table to be opened should be
908
 
  linked into Session::open_tables list.
909
 
  false - placeholder used for name-locking is already in
910
 
  this list so we only need to preserve Table::next
911
 
  pointer.
912
 
 
913
 
  NOTE
914
 
  This function assumes that its caller already acquired LOCK_open mutex.
915
 
 
916
 
  RETURN VALUE
917
 
  false - Success
918
 
  true  - Error
919
 
*/
920
 
 
921
 
bool Session::reopen_name_locked_table(TableList* table_list, bool link_in)
922
 
{
923
 
  Table *table= table_list->table;
924
 
  TableShare *share;
925
 
  char *table_name= table_list->table_name;
926
 
  Table orig_table;
927
 
 
928
 
  safe_mutex_assert_owner(&LOCK_open);
929
 
 
930
 
  if (killed || !table)
931
 
    return true;
932
 
 
933
 
  orig_table= *table;
934
 
 
935
 
  if (open_unireg_entry(this, table, table_list, table_name,
936
 
                        table->s->table_cache_key.str,
937
 
                        table->s->table_cache_key.length))
938
 
  {
939
 
    table->intern_close_table();
940
 
    /*
941
 
      If there was an error during opening of table (for example if it
942
 
      does not exist) '*table' object can be wiped out. To be able
943
 
      properly release name-lock in this case we should restore this
944
 
      object to its original state.
945
 
    */
946
 
    *table= orig_table;
947
 
    return true;
948
 
  }
949
 
 
950
 
  share= table->s;
951
 
  /*
952
 
    We want to prevent other connections from opening this table until end
953
 
    of statement as it is likely that modifications of table's metadata are
954
 
    not yet finished (for example CREATE TRIGGER have to change .TRG cursor,
955
 
    or we might want to drop table if CREATE TABLE ... SELECT fails).
956
 
    This also allows us to assume that no other connection will sneak in
957
 
    before we will get table-level lock on this table.
958
 
  */
959
 
  share->version=0;
960
 
  table->in_use = this;
961
 
 
962
 
  if (link_in)
963
 
  {
964
 
    table->next= open_tables;
965
 
    open_tables= table;
966
 
  }
967
 
  else
968
 
  {
969
 
    /*
970
 
      Table object should be already in Session::open_tables list so we just
971
 
      need to set Table::next correctly.
972
 
    */
973
 
    table->next= orig_table.next;
974
 
  }
975
 
 
976
 
  table->tablenr= current_tablenr++;
977
 
  table->used_fields= 0;
978
 
  table->const_table= 0;
979
 
  table->null_row= false;
980
 
  table->maybe_null= false;
981
 
  table->force_index= false;
982
 
  table->status= STATUS_NO_RECORD;
983
 
 
984
 
  return false;
985
787
}
986
788
 
987
789
 
998
800
  case of failure.
999
801
*/
1000
802
 
1001
 
Table *Session::table_cache_insert_placeholder(const char *key, uint32_t key_length)
 
803
table::Placeholder *Session::table_cache_insert_placeholder(const drizzled::TableIdentifier &arg)
1002
804
{
1003
 
  Table *table;
1004
 
  TableShare *share;
1005
 
  char *key_buff;
1006
 
 
1007
 
  safe_mutex_assert_owner(&LOCK_open);
 
805
  safe_mutex_assert_owner(LOCK_open.native_handle());
1008
806
 
1009
807
  /*
1010
808
    Create a table entry with the right key and with an old refresh version
1011
 
    Note that we must use multi_malloc() here as this is freed by the
1012
 
    table cache
1013
809
  */
1014
 
  if (! memory::multi_malloc(true,
1015
 
                             &table, sizeof(*table),
1016
 
                             &share, sizeof(*share),
1017
 
                             &key_buff, key_length,
1018
 
                             NULL))
1019
 
    return NULL;
1020
 
 
1021
 
  table->s= share;
1022
 
  share->set_table_cache_key(key_buff, key, key_length);
1023
 
  share->tmp_table= message::Table::INTERNAL;  // for intern_close_table
1024
 
  table->in_use= this;
1025
 
  table->locked_by_name=1;
1026
 
 
1027
 
  if (my_hash_insert(&open_cache, (unsigned char*)table))
 
810
  TableIdentifier identifier(arg.getSchemaName(), arg.getTableName(), message::Table::INTERNAL);
 
811
  table::Placeholder *table= new table::Placeholder(this, identifier);
 
812
 
 
813
  if (not add_table(table))
1028
814
  {
1029
 
    free((unsigned char*) table);
 
815
    delete table;
 
816
 
1030
817
    return NULL;
1031
818
  }
1032
819
 
1057
844
*/
1058
845
bool Session::lock_table_name_if_not_cached(TableIdentifier &identifier, Table **table)
1059
846
{
1060
 
  return lock_table_name_if_not_cached(identifier.getSchemaName().c_str(), identifier.getTableName().c_str(), table);
1061
 
}
1062
 
 
1063
 
bool Session::lock_table_name_if_not_cached(const char *new_db,
1064
 
                                            const char *table_name, Table **table)
1065
 
{
1066
 
  char key[MAX_DBKEY_LENGTH];
1067
 
  char *key_pos= key;
1068
 
  uint32_t key_length;
1069
 
 
1070
 
  key_pos= strcpy(key_pos, new_db) + strlen(new_db);
1071
 
  key_pos= strcpy(key_pos+1, table_name) + strlen(table_name);
1072
 
  key_length= (uint32_t) (key_pos-key)+1;
1073
 
 
1074
 
  pthread_mutex_lock(&LOCK_open); /* Obtain a name lock even though table is not in cache (like for create table)  */
1075
 
 
1076
 
  if (hash_search(&open_cache, (unsigned char *)key, key_length))
 
847
  const TableIdentifier::Key &key(identifier.getKey());
 
848
 
 
849
  boost_unique_lock_t scope_lock(LOCK_open); /* Obtain a name lock even though table is not in cache (like for create table)  */
 
850
 
 
851
  table::CacheMap::iterator iter;
 
852
 
 
853
  iter= table::getCache().find(key);
 
854
 
 
855
  if (iter != table::getCache().end())
1077
856
  {
1078
 
    pthread_mutex_unlock(&LOCK_open);
1079
857
    *table= 0;
1080
858
    return false;
1081
859
  }
1082
 
  if (not (*table= table_cache_insert_placeholder(key, key_length)))
 
860
 
 
861
  if (not (*table= table_cache_insert_placeholder(identifier)))
1083
862
  {
1084
 
    pthread_mutex_unlock(&LOCK_open);
1085
863
    return true;
1086
864
  }
1087
865
  (*table)->open_placeholder= true;
1088
 
  (*table)->next= open_tables;
 
866
  (*table)->setNext(open_tables);
1089
867
  open_tables= *table;
1090
 
  pthread_mutex_unlock(&LOCK_open);
1091
868
 
1092
869
  return false;
1093
870
}
1128
905
Table *Session::openTable(TableList *table_list, bool *refresh, uint32_t flags)
1129
906
{
1130
907
  Table *table;
1131
 
  char key[MAX_DBKEY_LENGTH];
1132
 
  unsigned int key_length;
1133
908
  const char *alias= table_list->alias;
1134
 
  HASH_SEARCH_STATE state;
1135
909
 
1136
910
  /* Parsing of partitioning information from .frm needs session->lex set up. */
1137
911
  assert(lex->is_lex_started);
1147
921
  if (killed)
1148
922
    return NULL;
1149
923
 
1150
 
  key_length= table_list->create_table_def_key(key);
 
924
  TableIdentifier identifier(table_list->getSchemaName(), table_list->getTableName());
 
925
  const TableIdentifier::Key &key(identifier.getKey());
 
926
  table::CacheRange ppp;
1151
927
 
1152
928
  /*
1153
929
    Unless requested otherwise, try to resolve this table in the list
1156
932
    same name. This block implements the behaviour.
1157
933
    TODO -> move this block into a separate function.
1158
934
  */
1159
 
  for (table= temporary_tables; table ; table=table->next)
 
935
  bool reset= false;
 
936
  for (table= temporary_tables; table ; table=table->getNext())
1160
937
  {
1161
 
    if (table->s->table_cache_key.length == key_length && !memcmp(table->s->table_cache_key.str, key, key_length))
 
938
    if (table->getShare()->getCacheKey() == key)
1162
939
    {
1163
940
      /*
1164
941
        We're trying to use the same temporary table twice in a query.
1168
945
      */
1169
946
      if (table->query_id)
1170
947
      {
1171
 
        my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
 
948
        my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
1172
949
        return NULL;
1173
950
      }
1174
951
      table->query_id= getQueryId();
1175
 
      goto reset;
1176
 
    }
1177
 
  }
1178
 
 
1179
 
  if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
1180
 
  {
1181
 
    my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
1182
 
    return NULL;
1183
 
  }
1184
 
 
1185
 
  /*
1186
 
    If it's the first table from a list of tables used in a query,
1187
 
    remember refresh_version (the version of open_cache state).
1188
 
    If the version changes while we're opening the remaining tables,
1189
 
    we will have to back off, close all the tables opened-so-far,
1190
 
    and try to reopen them.
1191
 
 
1192
 
    Note-> refresh_version is currently changed only during FLUSH TABLES.
1193
 
  */
1194
 
  if (!open_tables)
1195
 
    version= refresh_version;
1196
 
  else if ((version != refresh_version) &&
1197
 
           ! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
1198
 
  {
1199
 
    /* Someone did a refresh while thread was opening tables */
1200
 
    if (refresh)
1201
 
      *refresh= true;
1202
 
 
1203
 
    return NULL;
1204
 
  }
1205
 
 
1206
 
  /*
1207
 
    Before we test the global cache, we test our local session cache.
1208
 
  */
1209
 
  if (cached_table)
1210
 
  {
1211
 
    assert(false); /* Not implemented yet */
1212
 
  }
1213
 
 
1214
 
  /*
1215
 
    Non pre-locked/LOCK TABLES mode, and the table is not temporary:
1216
 
    this is the normal use case.
1217
 
    Now we should:
1218
 
    - try to find the table in the table cache.
1219
 
    - if one of the discovered Table instances is name-locked
1220
 
    (table->s->version == 0) back off -- we have to wait
1221
 
    until no one holds a name lock on the table.
1222
 
    - if there is no such Table in the name cache, read the table definition
1223
 
    and insert it into the cache.
1224
 
    We perform all of the above under LOCK_open which currently protects
1225
 
    the open cache (also known as table cache) and table definitions stored
1226
 
    on disk.
1227
 
  */
1228
 
 
1229
 
  pthread_mutex_lock(&LOCK_open); /* Lock for FLUSH TABLES for open table */
1230
 
 
1231
 
  /*
1232
 
    Actually try to find the table in the open_cache.
1233
 
    The cache may contain several "Table" instances for the same
1234
 
    physical table. The instances that are currently "in use" by
1235
 
    some thread have their "in_use" member != NULL.
1236
 
    There is no good reason for having more than one entry in the
1237
 
    hash for the same physical table, except that we use this as
1238
 
    an implicit "pending locks queue" - see
1239
 
    wait_for_locked_table_names for details.
1240
 
  */
1241
 
  for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
1242
 
                                  &state);
1243
 
       table && table->in_use ;
1244
 
       table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
1245
 
                                 &state))
1246
 
  {
 
952
      reset= true;
 
953
      break;
 
954
    }
 
955
  }
 
956
 
 
957
  if (not reset)
 
958
  {
 
959
    if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
 
960
    {
 
961
      my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->getSchemaName(), table_list->getTableName());
 
962
      return NULL;
 
963
    }
 
964
 
1247
965
    /*
1248
 
      Here we flush tables marked for flush.
1249
 
      Normally, table->s->version contains the value of
1250
 
      refresh_version from the moment when this table was
1251
 
      (re-)opened and added to the cache.
1252
 
      If since then we did (or just started) FLUSH TABLES
1253
 
      statement, refresh_version has been increased.
1254
 
      For "name-locked" Table instances, table->s->version is set
1255
 
      to 0 (see lock_table_name for details).
1256
 
      In case there is a pending FLUSH TABLES or a name lock, we
1257
 
      need to back off and re-start opening tables.
1258
 
      If we do not back off now, we may dead lock in case of lock
1259
 
      order mismatch with some other thread:
1260
 
c1: name lock t1; -- sort of exclusive lock
1261
 
c2: open t2;      -- sort of shared lock
1262
 
c1: name lock t2; -- blocks
1263
 
c2: open t1; -- blocks
 
966
      If it's the first table from a list of tables used in a query,
 
967
      remember refresh_version (the version of open_cache state).
 
968
      If the version changes while we're opening the remaining tables,
 
969
      we will have to back off, close all the tables opened-so-far,
 
970
      and try to reopen them.
 
971
 
 
972
      Note-> refresh_version is currently changed only during FLUSH TABLES.
1264
973
    */
1265
 
    if (table->needs_reopen_or_name_lock())
1266
 
    {
1267
 
      if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
1268
 
      {
1269
 
        /* Force close at once after usage */
1270
 
        version= table->s->version;
1271
 
        continue;
1272
 
      }
1273
 
 
1274
 
      /* Avoid self-deadlocks by detecting self-dependencies. */
1275
 
      if (table->open_placeholder && table->in_use == this)
1276
 
      {
1277
 
        pthread_mutex_unlock(&LOCK_open);
1278
 
        my_error(ER_UPDATE_TABLE_USED, MYF(0), table->s->table_name.str);
1279
 
        return NULL;
1280
 
      }
1281
 
 
1282
 
      /*
1283
 
        Back off, part 1: mark the table as "unused" for the
1284
 
        purpose of name-locking by setting table->db_stat to 0. Do
1285
 
        that only for the tables in this thread that have an old
1286
 
        table->s->version (this is an optimization (?)).
1287
 
        table->db_stat == 0 signals wait_for_locked_table_names
1288
 
        that the tables in question are not used any more. See
1289
 
        table_is_used call for details.
1290
 
      */
1291
 
      close_old_data_files(false, false);
1292
 
 
1293
 
      /*
1294
 
        Back-off part 2: try to avoid "busy waiting" on the table:
1295
 
        if the table is in use by some other thread, we suspend
1296
 
        and wait till the operation is complete: when any
1297
 
        operation that juggles with table->s->version completes,
1298
 
        it broadcasts COND_refresh condition variable.
1299
 
        If 'old' table we met is in use by current thread we return
1300
 
        without waiting since in this situation it's this thread
1301
 
        which is responsible for broadcasting on COND_refresh
1302
 
        (and this was done already in Session::close_old_data_files()).
1303
 
        Good example of such situation is when we have statement
1304
 
        that needs two instances of table and FLUSH TABLES comes
1305
 
        after we open first instance but before we open second
1306
 
        instance.
1307
 
      */
1308
 
      if (table->in_use != this)
1309
 
      {
1310
 
        /* wait_for_conditionwill unlock LOCK_open for us */
1311
 
        wait_for_condition(&LOCK_open, &COND_refresh);
1312
 
      }
1313
 
      else
1314
 
      {
1315
 
        pthread_mutex_unlock(&LOCK_open);
1316
 
      }
1317
 
      /*
1318
 
        There is a refresh in progress for this table.
1319
 
        Signal the caller that it has to try again.
1320
 
      */
 
974
    if (!open_tables)
 
975
    {
 
976
      version= refresh_version;
 
977
    }
 
978
    else if ((version != refresh_version) &&
 
979
             ! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
 
980
    {
 
981
      /* Someone did a refresh while thread was opening tables */
1321
982
      if (refresh)
1322
983
        *refresh= true;
 
984
 
1323
985
      return NULL;
1324
986
    }
1325
 
  }
1326
 
  if (table)
1327
 
  {
1328
 
    /* Unlink the table from "unused_tables" list. */
1329
 
    if (table == unused_tables)
1330
 
    {  // First unused
1331
 
      unused_tables=unused_tables->next; // Remove from link
1332
 
      if (table == unused_tables)
1333
 
        unused_tables= NULL;
 
987
 
 
988
    /*
 
989
      Before we test the global cache, we test our local session cache.
 
990
    */
 
991
    if (cached_table)
 
992
    {
 
993
      assert(false); /* Not implemented yet */
1334
994
    }
1335
 
    table->prev->next=table->next; /* Remove from unused list */
1336
 
    table->next->prev=table->prev;
1337
 
    table->in_use= this;
1338
 
  }
1339
 
  else
1340
 
  {
1341
 
    /* Insert a new Table instance into the open cache */
1342
 
    int error;
1343
 
    /* Free cache if too big */
1344
 
    while (open_cache.records > table_cache_size && unused_tables)
1345
 
      hash_delete(&open_cache,(unsigned char*) unused_tables);
1346
 
 
1347
 
    if (table_list->create)
 
995
 
 
996
    /*
 
997
      Non pre-locked/LOCK TABLES mode, and the table is not temporary:
 
998
      this is the normal use case.
 
999
      Now we should:
 
1000
      - try to find the table in the table cache.
 
1001
      - if one of the discovered Table instances is name-locked
 
1002
      (table->getShare()->version == 0) back off -- we have to wait
 
1003
      until no one holds a name lock on the table.
 
1004
      - if there is no such Table in the name cache, read the table definition
 
1005
      and insert it into the cache.
 
1006
      We perform all of the above under LOCK_open which currently protects
 
1007
      the open cache (also known as table cache) and table definitions stored
 
1008
      on disk.
 
1009
    */
 
1010
 
1348
1011
    {
1349
 
      TableIdentifier  lock_table_identifier(table_list->db, table_list->table_name, message::Table::STANDARD);
1350
 
 
1351
 
      if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
 
1012
      LOCK_open.lock(); /* Lock for FLUSH TABLES for open table */
 
1013
 
 
1014
      /*
 
1015
        Actually try to find the table in the open_cache.
 
1016
        The cache may contain several "Table" instances for the same
 
1017
        physical table. The instances that are currently "in use" by
 
1018
        some thread have their "in_use" member != NULL.
 
1019
        There is no good reason for having more than one entry in the
 
1020
        hash for the same physical table, except that we use this as
 
1021
        an implicit "pending locks queue" - see
 
1022
        wait_for_locked_table_names for details.
 
1023
      */
 
1024
      ppp= table::getCache().equal_range(key);
 
1025
 
 
1026
      table= NULL;
 
1027
      for (table::CacheMap::const_iterator iter= ppp.first;
 
1028
           iter != ppp.second; ++iter, table= NULL)
1352
1029
      {
 
1030
        table= (*iter).second;
 
1031
 
 
1032
        if (not table->in_use)
 
1033
          break;
1353
1034
        /*
1354
 
          Table to be created, so we need to create placeholder in table-cache.
 
1035
          Here we flush tables marked for flush.
 
1036
          Normally, table->getShare()->version contains the value of
 
1037
          refresh_version from the moment when this table was
 
1038
          (re-)opened and added to the cache.
 
1039
          If since then we did (or just started) FLUSH TABLES
 
1040
          statement, refresh_version has been increased.
 
1041
          For "name-locked" Table instances, table->getShare()->version is set
 
1042
          to 0 (see lock_table_name for details).
 
1043
          In case there is a pending FLUSH TABLES or a name lock, we
 
1044
          need to back off and re-start opening tables.
 
1045
          If we do not back off now, we may dead lock in case of lock
 
1046
          order mismatch with some other thread:
 
1047
          c1-> name lock t1; -- sort of exclusive lock
 
1048
          c2-> open t2;      -- sort of shared lock
 
1049
          c1-> name lock t2; -- blocks
 
1050
          c2-> open t1; -- blocks
1355
1051
        */
1356
 
        if (!(table= table_cache_insert_placeholder(key, key_length)))
 
1052
        if (table->needs_reopen_or_name_lock())
1357
1053
        {
1358
 
          pthread_mutex_unlock(&LOCK_open);
 
1054
          if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
 
1055
          {
 
1056
            /* Force close at once after usage */
 
1057
            version= table->getShare()->getVersion();
 
1058
            continue;
 
1059
          }
 
1060
 
 
1061
          /* Avoid self-deadlocks by detecting self-dependencies. */
 
1062
          if (table->open_placeholder && table->in_use == this)
 
1063
          {
 
1064
            LOCK_open.unlock();
 
1065
            my_error(ER_UPDATE_TABLE_USED, MYF(0), table->getShare()->getTableName());
 
1066
            return NULL;
 
1067
          }
 
1068
 
 
1069
          /*
 
1070
            Back off, part 1: mark the table as "unused" for the
 
1071
            purpose of name-locking by setting table->db_stat to 0. Do
 
1072
            that only for the tables in this thread that have an old
 
1073
            table->getShare()->version (this is an optimization (?)).
 
1074
            table->db_stat == 0 signals wait_for_locked_table_names
 
1075
            that the tables in question are not used any more. See
 
1076
            table_is_used call for details.
 
1077
          */
 
1078
          close_old_data_files(false, false);
 
1079
 
 
1080
          /*
 
1081
            Back-off part 2: try to avoid "busy waiting" on the table:
 
1082
            if the table is in use by some other thread, we suspend
 
1083
            and wait till the operation is complete: when any
 
1084
            operation that juggles with table->getShare()->version completes,
 
1085
            it broadcasts COND_refresh condition variable.
 
1086
            If 'old' table we met is in use by current thread we return
 
1087
            without waiting since in this situation it's this thread
 
1088
            which is responsible for broadcasting on COND_refresh
 
1089
            (and this was done already in Session::close_old_data_files()).
 
1090
            Good example of such situation is when we have statement
 
1091
            that needs two instances of table and FLUSH TABLES comes
 
1092
            after we open first instance but before we open second
 
1093
            instance.
 
1094
          */
 
1095
          if (table->in_use != this)
 
1096
          {
 
1097
            /* wait_for_conditionwill unlock LOCK_open for us */
 
1098
            wait_for_condition(LOCK_open, COND_refresh);
 
1099
          }
 
1100
          else
 
1101
          {
 
1102
            LOCK_open.unlock();
 
1103
          }
 
1104
          /*
 
1105
            There is a refresh in progress for this table.
 
1106
            Signal the caller that it has to try again.
 
1107
          */
 
1108
          if (refresh)
 
1109
            *refresh= true;
1359
1110
          return NULL;
1360
1111
        }
1361
 
        /*
1362
 
          Link placeholder to the open tables list so it will be automatically
1363
 
          removed once tables are closed. Also mark it so it won't be ignored
1364
 
          by other trying to take name-lock.
1365
 
        */
1366
 
        table->open_placeholder= true;
1367
 
        table->next= open_tables;
1368
 
        open_tables= table;
1369
 
        pthread_mutex_unlock(&LOCK_open);
1370
 
 
1371
 
        return table ;
1372
 
      }
1373
 
      /* Table exists. Let us try to open it. */
1374
 
    }
1375
 
 
1376
 
    /* make a new table */
1377
 
    table= (Table *)malloc(sizeof(Table));
1378
 
    if (table == NULL)
1379
 
    {
1380
 
      pthread_mutex_unlock(&LOCK_open);
1381
 
      return NULL;
1382
 
    }
1383
 
 
1384
 
    error= open_unireg_entry(this, table, table_list, alias, key, key_length);
1385
 
    if (error != 0)
1386
 
    {
1387
 
      free(table);
1388
 
      pthread_mutex_unlock(&LOCK_open);
1389
 
      return NULL;
1390
 
    }
1391
 
    my_hash_insert(&open_cache, (unsigned char*) table);
1392
 
  }
1393
 
 
1394
 
  pthread_mutex_unlock(&LOCK_open);
1395
 
  if (refresh)
1396
 
  {
1397
 
    table->next= open_tables; /* Link into simple list */
1398
 
    open_tables=table;
1399
 
  }
1400
 
  table->reginfo.lock_type= TL_READ; /* Assume read */
1401
 
 
1402
 
reset:
1403
 
  assert(table->s->ref_count > 0 || table->s->tmp_table != message::Table::STANDARD);
1404
 
 
1405
 
  if (lex->need_correct_ident())
1406
 
    table->alias_name_used= my_strcasecmp(table_alias_charset,
1407
 
                                          table->s->table_name.str, alias);
 
1112
      }
 
1113
      if (table)
 
1114
      {
 
1115
        table::getUnused().unlink(static_cast<table::Concurrent *>(table));
 
1116
        table->in_use= this;
 
1117
      }
 
1118
      else
 
1119
      {
 
1120
        /* Insert a new Table instance into the open cache */
 
1121
        int error;
 
1122
        /* Free cache if too big */
 
1123
        table::getUnused().cull();
 
1124
 
 
1125
        if (table_list->isCreate())
 
1126
        {
 
1127
          TableIdentifier  lock_table_identifier(table_list->getSchemaName(), table_list->getTableName(), message::Table::STANDARD);
 
1128
 
 
1129
          if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
 
1130
          {
 
1131
            /*
 
1132
              Table to be created, so we need to create placeholder in table-cache.
 
1133
            */
 
1134
            if (!(table= table_cache_insert_placeholder(lock_table_identifier)))
 
1135
            {
 
1136
              LOCK_open.unlock();
 
1137
              return NULL;
 
1138
            }
 
1139
            /*
 
1140
              Link placeholder to the open tables list so it will be automatically
 
1141
              removed once tables are closed. Also mark it so it won't be ignored
 
1142
              by other trying to take name-lock.
 
1143
            */
 
1144
            table->open_placeholder= true;
 
1145
            table->setNext(open_tables);
 
1146
            open_tables= table;
 
1147
            LOCK_open.unlock();
 
1148
 
 
1149
            return table ;
 
1150
          }
 
1151
          /* Table exists. Let us try to open it. */
 
1152
        }
 
1153
 
 
1154
        /* make a new table */
 
1155
        {
 
1156
          table::Concurrent *new_table= new table::Concurrent;
 
1157
          table= new_table;
 
1158
          if (new_table == NULL)
 
1159
          {
 
1160
            LOCK_open.unlock();
 
1161
            return NULL;
 
1162
          }
 
1163
 
 
1164
          error= new_table->open_unireg_entry(this, alias, identifier);
 
1165
          if (error != 0)
 
1166
          {
 
1167
            delete new_table;
 
1168
            LOCK_open.unlock();
 
1169
            return NULL;
 
1170
          }
 
1171
          (void)add_table(new_table);
 
1172
        }
 
1173
      }
 
1174
 
 
1175
      LOCK_open.unlock();
 
1176
    }
 
1177
    if (refresh)
 
1178
    {
 
1179
      table->setNext(open_tables); /* Link into simple list */
 
1180
      open_tables= table;
 
1181
    }
 
1182
    table->reginfo.lock_type= TL_READ; /* Assume read */
 
1183
 
 
1184
  }
 
1185
  assert(table->getShare()->getTableCount() > 0 || table->getShare()->getType() != message::Table::STANDARD);
 
1186
 
1408
1187
  /* Fix alias if table name changes */
1409
 
  if (strcmp(table->alias, alias))
 
1188
  if (strcmp(table->getAlias(), alias))
1410
1189
  {
1411
 
    uint32_t length=(uint32_t) strlen(alias)+1;
1412
 
    table->alias= (char*) realloc((char*) table->alias, length);
1413
 
    memcpy((void*) table->alias, alias, length);
 
1190
    table->setAlias(alias);
1414
1191
  }
1415
1192
 
1416
1193
  /* These variables are also set in reopen_table() */
1421
1198
  table->maybe_null= false;
1422
1199
  table->force_index= false;
1423
1200
  table->status=STATUS_NO_RECORD;
1424
 
  table->insert_values= 0;
 
1201
  table->insert_values.clear();
1425
1202
  /* Catch wrong handling of the auto_increment_field_not_null. */
1426
1203
  assert(!table->auto_increment_field_not_null);
1427
1204
  table->auto_increment_field_not_null= false;
1428
1205
  if (table->timestamp_field)
 
1206
  {
1429
1207
    table->timestamp_field_type= table->timestamp_field->get_auto_set_type();
 
1208
  }
1430
1209
  table->pos_in_table_list= table_list;
1431
1210
  table->clear_column_bitmaps();
1432
1211
  assert(table->key_read == 0);
1435
1214
}
1436
1215
 
1437
1216
 
1438
 
/*
1439
 
  Reopen an table because the definition has changed.
1440
 
 
1441
 
  SYNOPSIS
1442
 
  reopen_table()
1443
 
  table Table object
1444
 
 
1445
 
  NOTES
1446
 
  The data cursor for the table is already closed and the share is released
1447
 
  The table has a 'dummy' share that mainly contains database and table name.
1448
 
 
1449
 
  RETURN
1450
 
  0  ok
1451
 
  1  error. The old table object is not changed.
1452
 
*/
1453
 
 
1454
 
bool reopen_table(Table *table)
1455
 
{
1456
 
  Table tmp;
1457
 
  bool error= 1;
1458
 
  Field **field;
1459
 
  uint32_t key,part;
1460
 
  TableList table_list;
1461
 
  Session *session= table->in_use;
1462
 
 
1463
 
  assert(table->s->ref_count == 0);
1464
 
  assert(!table->sort.io_cache);
1465
 
 
1466
 
#ifdef EXTRA_DEBUG
1467
 
  if (table->db_stat)
1468
 
    errmsg_printf(ERRMSG_LVL_ERROR, _("Table %s had a open data Cursor in reopen_table"),
1469
 
                  table->alias);
1470
 
#endif
1471
 
  table_list.db=         const_cast<char *>(table->s->getSchemaName());
1472
 
  table_list.table_name= table->s->table_name.str;
1473
 
  table_list.table=      table;
1474
 
 
1475
 
  if (wait_for_locked_table_names(session, &table_list))
1476
 
    return true;                             // Thread was killed
1477
 
 
1478
 
  if (open_unireg_entry(session, &tmp, &table_list,
1479
 
                        table->alias,
1480
 
                        table->s->table_cache_key.str,
1481
 
                        table->s->table_cache_key.length))
1482
 
    goto end;
1483
 
 
1484
 
  /* This list copies variables set by open_table */
1485
 
  tmp.tablenr=          table->tablenr;
1486
 
  tmp.used_fields=      table->used_fields;
1487
 
  tmp.const_table=      table->const_table;
1488
 
  tmp.null_row=         table->null_row;
1489
 
  tmp.maybe_null=       table->maybe_null;
1490
 
  tmp.status=           table->status;
1491
 
 
1492
 
  /* Get state */
1493
 
  tmp.in_use=           session;
1494
 
  tmp.reginfo.lock_type=table->reginfo.lock_type;
1495
 
 
1496
 
  /* Replace table in open list */
1497
 
  tmp.next=             table->next;
1498
 
  tmp.prev=             table->prev;
1499
 
 
1500
 
  if (table->cursor)
1501
 
    table->closefrm(true);              // close cursor, free everything
1502
 
 
1503
 
  *table= tmp;
1504
 
  table->default_column_bitmaps();
1505
 
  table->cursor->change_table_ptr(table, table->s);
1506
 
 
1507
 
  assert(table->alias != 0);
1508
 
  for (field=table->field ; *field ; field++)
1509
 
  {
1510
 
    (*field)->table= (*field)->orig_table= table;
1511
 
    (*field)->table_name= &table->alias;
1512
 
  }
1513
 
  for (key=0 ; key < table->s->keys ; key++)
1514
 
  {
1515
 
    for (part=0 ; part < table->key_info[key].usable_key_parts ; part++)
1516
 
      table->key_info[key].key_part[part].field->table= table;
1517
 
  }
1518
 
 
1519
 
  broadcast_refresh();
1520
 
  error= false;
1521
 
 
1522
 
end:
1523
 
  return(error);
1524
 
}
1525
 
 
1526
 
 
1527
1217
/**
1528
1218
  Close all instances of a table open by this thread and replace
1529
1219
  them with exclusive name-locks.
1543
1233
 
1544
1234
void Session::close_data_files_and_morph_locks(TableIdentifier &identifier)
1545
1235
{
1546
 
  close_data_files_and_morph_locks(identifier.getSchemaName().c_str(), identifier.getTableName().c_str());
1547
 
}
1548
 
 
1549
 
void Session::close_data_files_and_morph_locks(const char *new_db, const char *new_table_name)
1550
 
{
1551
 
  Table *table;
1552
 
 
1553
 
  safe_mutex_assert_owner(&LOCK_open); /* Adjust locks at the end of ALTER TABLEL */
 
1236
  safe_mutex_assert_owner(LOCK_open.native_handle()); /* Adjust locks at the end of ALTER TABLEL */
1554
1237
 
1555
1238
  if (lock)
1556
1239
  {
1567
1250
    for target table name if we process ALTER Table ... RENAME.
1568
1251
    So loop below makes sense even if we are not under LOCK TABLES.
1569
1252
  */
1570
 
  for (table= open_tables; table ; table=table->next)
 
1253
  for (Table *table= open_tables; table ; table=table->getNext())
1571
1254
  {
1572
 
    if (!strcmp(table->s->table_name.str, new_table_name) &&
1573
 
        !strcasecmp(table->s->getSchemaName(), new_db))
 
1255
    if (table->getShare()->getCacheKey() == identifier.getKey())
1574
1256
    {
1575
1257
      table->open_placeholder= true;
1576
1258
      close_handle_and_leave_table_as_lock(table);
1599
1281
  @return false in case of success, true - otherwise.
1600
1282
*/
1601
1283
 
1602
 
bool Session::reopen_tables(bool get_locks, bool mark_share_as_old)
 
1284
bool Session::reopen_tables(bool get_locks, bool)
1603
1285
{
1604
1286
  Table *table,*next,**prev;
1605
1287
  Table **tables,**tables_ptr;                  // For locks
1611
1293
  if (open_tables == NULL)
1612
1294
    return false;
1613
1295
 
1614
 
  safe_mutex_assert_owner(&LOCK_open);
 
1296
  safe_mutex_assert_owner(LOCK_open.native_handle());
1615
1297
  if (get_locks)
1616
1298
  {
1617
1299
    /*
1620
1302
    */
1621
1303
    uint32_t opens= 0;
1622
1304
 
1623
 
    for (table= open_tables; table ; table=table->next)
 
1305
    for (table= open_tables; table ; table=table->getNext())
 
1306
    {
1624
1307
      opens++;
 
1308
    }
1625
1309
    tables= new Table *[opens];
1626
1310
  }
1627
1311
  else
 
1312
  {
1628
1313
    tables= &open_tables;
 
1314
  }
1629
1315
  tables_ptr =tables;
1630
1316
 
1631
1317
  prev= &open_tables;
1632
1318
  for (table= open_tables; table ; table=next)
1633
1319
  {
1634
 
    uint32_t db_stat= table->db_stat;
1635
 
    next= table->next;
1636
 
    if (!tables || (!db_stat && reopen_table(table)))
1637
 
    {
1638
 
      my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
1639
 
      hash_delete(&open_cache,(unsigned char*) table);
1640
 
      error= 1;
1641
 
    }
1642
 
    else
1643
 
    {
1644
 
      *prev= table;
1645
 
      prev= &table->next;
1646
 
      /* Do not handle locks of MERGE children. */
1647
 
      if (get_locks && !db_stat)
1648
 
        *tables_ptr++= table;                   // need new lock on this
1649
 
      if (mark_share_as_old)
1650
 
      {
1651
 
        table->s->version= 0;
1652
 
        table->open_placeholder= false;
1653
 
      }
1654
 
    }
 
1320
    next= table->getNext();
 
1321
 
 
1322
    my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
 
1323
    table::remove_table(static_cast<table::Concurrent *>(table));
 
1324
    error= 1;
1655
1325
  }
1656
1326
  *prev=0;
1657
1327
  if (tables != tables_ptr)                     // Should we get back old locks
1658
1328
  {
1659
 
    DRIZZLE_LOCK *local_lock;
 
1329
    DrizzleLock *local_lock;
1660
1330
    /*
1661
1331
      We should always get these locks. Anyway, we must not go into
1662
1332
      wait_for_tables() as it tries to acquire LOCK_open, which is
1710
1380
 
1711
1381
  Table *table= open_tables;
1712
1382
 
1713
 
  for (; table ; table=table->next)
 
1383
  for (; table ; table=table->getNext())
1714
1384
  {
1715
1385
    /*
1716
1386
      Reopen marked for flush.
1776
1446
}
1777
1447
 
1778
1448
 
1779
 
/*
1780
 
  Wait until all threads has closed the tables in the list
1781
 
  We have also to wait if there is thread that has a lock on this table even
1782
 
  if the table is closed
1783
 
*/
1784
 
 
1785
 
bool table_is_used(Table *table, bool wait_for_name_lock)
1786
 
{
1787
 
  do
1788
 
  {
1789
 
    char *key= table->s->table_cache_key.str;
1790
 
    uint32_t key_length= table->s->table_cache_key.length;
1791
 
 
1792
 
    HASH_SEARCH_STATE state;
1793
 
    for (Table *search= (Table*) hash_first(&open_cache, (unsigned char*) key,
1794
 
                                            key_length, &state);
1795
 
         search ;
1796
 
         search= (Table*) hash_next(&open_cache, (unsigned char*) key,
1797
 
                                    key_length, &state))
1798
 
    {
1799
 
      if (search->in_use == table->in_use)
1800
 
        continue;                               // Name locked by this thread
1801
 
      /*
1802
 
        We can't use the table under any of the following conditions:
1803
 
        - There is an name lock on it (Table is to be deleted or altered)
1804
 
        - If we are in flush table and we didn't execute the flush
1805
 
        - If the table engine is open and it's an old version
1806
 
        (We must wait until all engines are shut down to use the table)
1807
 
      */
1808
 
      if ( (search->locked_by_name && wait_for_name_lock) ||
1809
 
           (search->is_name_opened() && search->needs_reopen_or_name_lock()))
1810
 
        return 1;
1811
 
    }
1812
 
  } while ((table=table->next));
1813
 
  return 0;
1814
 
}
1815
 
 
1816
 
 
1817
1449
/* Wait until all used tables are refreshed */
1818
1450
 
1819
1451
bool wait_for_tables(Session *session)
1821
1453
  bool result;
1822
1454
 
1823
1455
  session->set_proc_info("Waiting for tables");
1824
 
  pthread_mutex_lock(&LOCK_open); /* Lock for all tables to be refreshed */
1825
 
  while (!session->killed)
1826
 
  {
1827
 
    session->some_tables_deleted= false;
1828
 
    session->close_old_data_files(false, dropping_tables != 0);
1829
 
    if (!table_is_used(session->open_tables, 1))
1830
 
      break;
1831
 
    (void) pthread_cond_wait(&COND_refresh,&LOCK_open);
1832
 
  }
1833
 
  if (session->killed)
1834
 
    result= true;                                       // aborted
1835
 
  else
1836
 
  {
1837
 
    /* Now we can open all tables without any interference */
1838
 
    session->set_proc_info("Reopen tables");
1839
 
    session->version= refresh_version;
1840
 
    result= session->reopen_tables(false, false);
1841
 
  }
1842
 
  pthread_mutex_unlock(&LOCK_open);
 
1456
  {
 
1457
    boost_unique_lock_t lock(LOCK_open);
 
1458
    while (!session->killed)
 
1459
    {
 
1460
      session->some_tables_deleted= false;
 
1461
      session->close_old_data_files(false, dropping_tables != 0);
 
1462
      if (not table::Cache::singleton().areTablesUsed(session->open_tables, 1))
 
1463
      {
 
1464
        break;
 
1465
      }
 
1466
      COND_refresh.wait(lock);
 
1467
    }
 
1468
    if (session->killed)
 
1469
      result= true;                                     // aborted
 
1470
    else
 
1471
    {
 
1472
      /* Now we can open all tables without any interference */
 
1473
      session->set_proc_info("Reopen tables");
 
1474
      session->version= refresh_version;
 
1475
      result= session->reopen_tables(false, false);
 
1476
    }
 
1477
  }
1843
1478
  session->set_proc_info(0);
1844
1479
 
1845
1480
  return result;
1870
1505
*/
1871
1506
 
1872
1507
 
1873
 
Table *drop_locked_tables(Session *session,const char *db, const char *table_name)
 
1508
Table *drop_locked_tables(Session *session, const drizzled::TableIdentifier &identifier)
1874
1509
{
1875
1510
  Table *table,*next,**prev, *found= 0;
1876
1511
  prev= &session->open_tables;
1878
1513
  /*
1879
1514
    Note that we need to hold LOCK_open while changing the
1880
1515
    open_tables list. Another thread may work on it.
1881
 
    (See: remove_table_from_cache(), mysql_wait_completed_table())
 
1516
    (See: table::Cache::singleton().removeTable(), mysql_wait_completed_table())
1882
1517
    Closing a MERGE child before the parent would be fatal if the
1883
1518
    other thread tries to abort the MERGE lock in between.
1884
1519
  */
1885
1520
  for (table= session->open_tables; table ; table=next)
1886
1521
  {
1887
 
    next=table->next;
1888
 
    if (!strcmp(table->s->table_name.str, table_name) &&
1889
 
        !strcasecmp(table->s->getSchemaName(), db))
 
1522
    next=table->getNext();
 
1523
    if (table->getShare()->getCacheKey() == identifier.getKey())
1890
1524
    {
1891
1525
      mysql_lock_remove(session, table);
1892
1526
 
1903
1537
      else
1904
1538
      {
1905
1539
        /* We already have a name lock, remove copy */
1906
 
        hash_delete(&open_cache,(unsigned char*) table);
 
1540
        table::remove_table(static_cast<table::Concurrent *>(table));
1907
1541
      }
1908
1542
    }
1909
1543
    else
1910
1544
    {
1911
1545
      *prev=table;
1912
 
      prev= &table->next;
 
1546
      prev= table->getNextPtr();
1913
1547
    }
1914
1548
  }
1915
1549
  *prev=0;
1926
1560
  other threads trying to get the lock.
1927
1561
*/
1928
1562
 
1929
 
void abort_locked_tables(Session *session,const char *db, const char *table_name)
 
1563
void abort_locked_tables(Session *session, const drizzled::TableIdentifier &identifier)
1930
1564
{
1931
1565
  Table *table;
1932
 
  for (table= session->open_tables; table ; table= table->next)
 
1566
  for (table= session->open_tables; table ; table= table->getNext())
1933
1567
  {
1934
 
    if (!strcmp(table->s->table_name.str, table_name) &&
1935
 
        !strcmp(table->s->getSchemaName(), db))
 
1568
    if (table->getShare()->getCacheKey() == identifier.getKey())
1936
1569
    {
1937
1570
      /* If MERGE child, forward lock handling to parent. */
1938
1571
      mysql_lock_abort(session, table);
1941
1574
  }
1942
1575
}
1943
1576
 
1944
 
/*
1945
 
  Load a table definition from cursor and open unireg table
1946
 
 
1947
 
  SYNOPSIS
1948
 
  open_unireg_entry()
1949
 
  session                       Thread handle
1950
 
  entry         Store open table definition here
1951
 
  table_list            TableList with db, table_name
1952
 
  alias         Alias name
1953
 
  cache_key             Key for share_cache
1954
 
  cache_key_length      length of cache_key
1955
 
 
1956
 
  NOTES
1957
 
  Extra argument for open is taken from session->open_options
1958
 
  One must have a lock on LOCK_open when calling this function
1959
 
 
1960
 
  RETURN
1961
 
  0     ok
1962
 
#       Error
1963
 
*/
1964
 
 
1965
 
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
1966
 
                             const char *alias,
1967
 
                             char *cache_key, uint32_t cache_key_length)
1968
 
{
1969
 
  int error;
1970
 
  TableShare *share;
1971
 
  uint32_t discover_retry_count= 0;
1972
 
 
1973
 
  safe_mutex_assert_owner(&LOCK_open);
1974
 
retry:
1975
 
  if (not (share= TableShare::getShare(session, table_list, cache_key,
1976
 
                                       cache_key_length,
1977
 
                                       table_list->i_s_requested_object,
1978
 
                                       &error)))
1979
 
    return 1;
1980
 
 
1981
 
  while ((error= open_table_from_share(session, share, alias,
1982
 
                                       (uint32_t) (HA_OPEN_KEYFILE |
1983
 
                                                   HA_OPEN_RNDFILE |
1984
 
                                                   HA_GET_INDEX |
1985
 
                                                   HA_TRY_READ_ONLY),
1986
 
                                       session->open_options, entry)))
1987
 
  {
1988
 
    if (error == 7)                             // Table def changed
1989
 
    {
1990
 
      share->version= 0;                        // Mark share as old
1991
 
      if (discover_retry_count++)               // Retry once
1992
 
        goto err;
1993
 
 
1994
 
      /*
1995
 
        TODO->
1996
 
        Here we should wait until all threads has released the table.
1997
 
        For now we do one retry. This may cause a deadlock if there
1998
 
        is other threads waiting for other tables used by this thread.
1999
 
 
2000
 
        Proper fix would be to if the second retry failed:
2001
 
        - Mark that table def changed
2002
 
        - Return from open table
2003
 
        - Close all tables used by this thread
2004
 
        - Start waiting that the share is released
2005
 
        - Retry by opening all tables again
2006
 
      */
2007
 
 
2008
 
      /*
2009
 
        TO BE FIXED
2010
 
        To avoid deadlock, only wait for release if no one else is
2011
 
        using the share.
2012
 
      */
2013
 
      if (share->ref_count != 1)
2014
 
        goto err;
2015
 
      /* Free share and wait until it's released by all threads */
2016
 
      TableShare::release(share);
2017
 
 
2018
 
      if (!session->killed)
2019
 
      {
2020
 
        drizzle_reset_errors(session, 1);         // Clear warnings
2021
 
        session->clear_error();                 // Clear error message
2022
 
        goto retry;
2023
 
      }
2024
 
      return 1;
2025
 
    }
2026
 
 
2027
 
    goto err;
2028
 
  }
2029
 
 
2030
 
  return 0;
2031
 
 
2032
 
err:
2033
 
  TableShare::release(share);
2034
 
 
2035
 
  return 1;
2036
 
}
2037
 
 
2038
1577
 
2039
1578
/*
2040
1579
  Open all tables in list
2102
1641
     * to see if it exists so that an unauthorized user cannot phish for
2103
1642
     * table/schema information via error messages
2104
1643
     */
 
1644
    TableIdentifier the_table(tables->getSchemaName(), tables->getTableName());
2105
1645
    if (not plugin::Authorization::isAuthorized(getSecurityContext(),
2106
 
                                                string(tables->db),
2107
 
                                                string(tables->table_name)))
 
1646
                                                the_table))
2108
1647
    {
2109
1648
      result= -1;                               // Fatal error
2110
1649
      break;
2150
1689
    {
2151
1690
      if (tables->lock_type == TL_WRITE_DEFAULT)
2152
1691
        tables->table->reginfo.lock_type= update_lock_default;
2153
 
      else if (tables->table->s->tmp_table == message::Table::STANDARD)
 
1692
      else if (tables->table->getShare()->getType() == message::Table::STANDARD)
2154
1693
        tables->table->reginfo.lock_type= tables->lock_type;
2155
1694
    }
2156
1695
  }
2308
1847
Table *Session::open_temporary_table(TableIdentifier &identifier,
2309
1848
                                     bool link_in_list)
2310
1849
{
2311
 
  Table *new_tmp_table;
2312
 
  TableShare *share;
2313
 
  char cache_key[MAX_DBKEY_LENGTH], *saved_cache_key, *tmp_path;
2314
 
  uint32_t key_length, path_length;
2315
 
  TableList table_list;
2316
 
 
2317
 
  table_list.db=         const_cast<char*>(identifier.getSchemaName().c_str());
2318
 
  table_list.table_name= const_cast<char*>(identifier.getTableName().c_str());
2319
 
  /* Create the cache_key for temporary tables */
2320
 
  key_length= table_list.create_table_def_key(cache_key);
2321
 
  path_length= identifier.getPath().length();
2322
 
 
2323
 
  if (!(new_tmp_table= (Table*) malloc(sizeof(*new_tmp_table) + sizeof(*share) +
2324
 
                                       path_length + 1 + key_length)))
 
1850
  assert(identifier.isTmp());
 
1851
 
 
1852
 
 
1853
  table::Temporary *new_tmp_table= new table::Temporary(identifier.getType(),
 
1854
                                                        identifier,
 
1855
                                                        const_cast<char *>(identifier.getPath().c_str()),
 
1856
                                                        static_cast<uint32_t>(identifier.getPath().length()));
 
1857
  if (not new_tmp_table)
2325
1858
    return NULL;
2326
1859
 
2327
 
  share= (TableShare*) (new_tmp_table+1);
2328
 
  tmp_path= (char*) (share+1);
2329
 
  saved_cache_key= strcpy(tmp_path, identifier.getPath().c_str())+path_length+1;
2330
 
  memcpy(saved_cache_key, cache_key, key_length);
2331
 
 
2332
 
  share->init(saved_cache_key, key_length, strchr(saved_cache_key, '\0')+1, tmp_path);
2333
 
 
2334
1860
  /*
2335
1861
    First open the share, and then open the table from the share we just opened.
2336
1862
  */
2337
 
  if (open_table_def(*this, identifier, share) ||
2338
 
      open_table_from_share(this, share, identifier.getTableName().c_str(),
2339
 
                            (uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
2340
 
                                        HA_GET_INDEX),
2341
 
                            ha_open_options,
2342
 
                            new_tmp_table))
 
1863
  if (new_tmp_table->getMutableShare()->open_table_def(*this, identifier) ||
 
1864
      new_tmp_table->getMutableShare()->open_table_from_share(this, identifier, identifier.getTableName().c_str(),
 
1865
                                                              (uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
 
1866
                                                                          HA_GET_INDEX),
 
1867
                                                              ha_open_options,
 
1868
                                                              *new_tmp_table))
2343
1869
  {
2344
1870
    /* No need to lock share->mutex as this is not needed for tmp tables */
2345
 
    share->free_table_share();
2346
 
    free((char*) new_tmp_table);
 
1871
    delete new_tmp_table->getMutableShare();
 
1872
    delete new_tmp_table;
 
1873
 
2347
1874
    return 0;
2348
1875
  }
2349
1876
 
2350
1877
  new_tmp_table->reginfo.lock_type= TL_WRITE;    // Simulate locked
2351
 
  share->tmp_table= message::Table::TEMPORARY;
2352
1878
 
2353
1879
  if (link_in_list)
2354
1880
  {
2355
1881
    /* growing temp list at the head */
2356
 
    new_tmp_table->next= this->temporary_tables;
2357
 
    if (new_tmp_table->next)
2358
 
      new_tmp_table->next->prev= new_tmp_table;
 
1882
    new_tmp_table->setNext(this->temporary_tables);
 
1883
    if (new_tmp_table->getNext())
 
1884
    {
 
1885
      new_tmp_table->getNext()->setPrev(new_tmp_table);
 
1886
    }
2359
1887
    this->temporary_tables= new_tmp_table;
2360
 
    this->temporary_tables->prev= 0;
 
1888
    this->temporary_tables->setPrev(0);
2361
1889
  }
2362
1890
  new_tmp_table->pos_in_table_list= 0;
2363
1891
 
2382
1910
{
2383
1911
  if (session->mark_used_columns != MARK_COLUMNS_NONE)
2384
1912
  {
2385
 
    MyBitmap *current_bitmap, *other_bitmap;
 
1913
    boost::dynamic_bitset<> *current_bitmap= NULL;
2386
1914
 
2387
1915
    /*
2388
1916
      We always want to register the used keys, as the column bitmap may have
2395
1923
    if (session->mark_used_columns == MARK_COLUMNS_READ)
2396
1924
    {
2397
1925
      current_bitmap= table->read_set;
2398
 
      other_bitmap=   table->write_set;
2399
1926
    }
2400
1927
    else
2401
1928
    {
2402
1929
      current_bitmap= table->write_set;
2403
 
      other_bitmap=   table->read_set;
2404
1930
    }
2405
1931
 
2406
 
    if (current_bitmap->testAndSet(field->field_index))
 
1932
    //if (current_bitmap->testAndSet(field->field_index))
 
1933
    if (current_bitmap->test(field->field_index))
2407
1934
    {
2408
1935
      if (session->mark_used_columns == MARK_COLUMNS_WRITE)
2409
1936
        session->dup_field= field;
2472
1999
    return NULL;
2473
2000
  {
2474
2001
    /* This is a base table. */
2475
 
    assert(nj_col->table_ref->table == nj_col->table_field->table);
 
2002
    assert(nj_col->table_ref->table == nj_col->table_field->getTable());
2476
2003
    found_field= nj_col->table_field;
2477
2004
    update_field_dependencies(session, found_field, nj_col->table_ref->table);
2478
2005
  }
2509
2036
  uint32_t cached_field_index= *cached_field_index_ptr;
2510
2037
 
2511
2038
  /* We assume here that table->field < NO_CACHED_FIELD_INDEX = UINT_MAX */
2512
 
  if (cached_field_index < table->s->fields &&
 
2039
  if (cached_field_index < table->getShare()->sizeFields() &&
2513
2040
      !my_strcasecmp(system_charset_info,
2514
 
                     table->field[cached_field_index]->field_name, name))
2515
 
    field_ptr= table->field + cached_field_index;
2516
 
  else if (table->s->name_hash.records)
2517
 
  {
2518
 
    field_ptr= (Field**) hash_search(&table->s->name_hash, (unsigned char*) name,
2519
 
                                     length);
 
2041
                     table->getField(cached_field_index)->field_name, name))
 
2042
  {
 
2043
    field_ptr= table->getFields() + cached_field_index;
 
2044
  }
 
2045
  else if (table->getShare()->getNamedFieldSize())
 
2046
  {
 
2047
    field_ptr= table->getMutableShare()->getNamedField(std::string(name, length));
2520
2048
    if (field_ptr)
2521
2049
    {
2522
2050
      /*
2523
2051
        field_ptr points to field in TableShare. Convert it to the matching
2524
2052
        field in table
2525
2053
      */
2526
 
      field_ptr= (table->field + (field_ptr - table->s->field));
 
2054
      field_ptr= (table->getFields() + table->getShare()->positionFields(field_ptr));
2527
2055
    }
2528
2056
  }
2529
2057
  else
2530
2058
  {
2531
 
    if (!(field_ptr= table->field))
 
2059
    if (!(field_ptr= table->getFields()))
2532
2060
      return((Field *)0);
2533
2061
    for (; *field_ptr; ++field_ptr)
2534
2062
      if (!my_strcasecmp(system_charset_info, (*field_ptr)->field_name, name))
2537
2065
 
2538
2066
  if (field_ptr && *field_ptr)
2539
2067
  {
2540
 
    *cached_field_index_ptr= field_ptr - table->field;
 
2068
    *cached_field_index_ptr= field_ptr - table->getFields();
2541
2069
    field= *field_ptr;
2542
2070
  }
2543
2071
  else
2544
2072
  {
2545
2073
    if (!allow_rowid ||
2546
2074
        my_strcasecmp(system_charset_info, name, "_rowid") ||
2547
 
        table->s->rowid_field_offset == 0)
 
2075
        table->getShare()->rowid_field_offset == 0)
2548
2076
      return((Field*) 0);
2549
 
    field= table->field[table->s->rowid_field_offset-1];
 
2077
    field= table->getField(table->getShare()->rowid_field_offset-1);
2550
2078
  }
2551
2079
 
2552
2080
  update_field_dependencies(session, field, table);
2625
2153
    inside the view, but we want to search directly in the view columns
2626
2154
    which are represented as a 'field_translation'.
2627
2155
 
2628
 
TODO: Ensure that table_name, db_name and tables->db always points to
2629
 
something !
 
2156
    TODO-> Ensure that table_name, db_name and tables->db always points to something !
2630
2157
  */
2631
2158
  if (/* Exclude nested joins. */
2632
 
      (!table_list->nested_join) &&
 
2159
      (!table_list->getNestedJoin()) &&
2633
2160
      /* Include merge views and information schema tables. */
2634
2161
      /*
2635
2162
        Test if the field qualifiers match the table reference we plan
2637
2164
      */
2638
2165
      table_name && table_name[0] &&
2639
2166
      (my_strcasecmp(table_alias_charset, table_list->alias, table_name) ||
2640
 
       (db_name && db_name[0] && table_list->db && table_list->db[0] &&
2641
 
        strcmp(db_name, table_list->db))))
 
2167
       (db_name && db_name[0] && table_list->getSchemaName() && table_list->getSchemaName()[0] &&
 
2168
        strcmp(db_name, table_list->getSchemaName()))))
2642
2169
    return 0;
2643
2170
 
2644
2171
  *actual_table= NULL;
2645
2172
 
2646
 
  if (!table_list->nested_join)
 
2173
  if (!table_list->getNestedJoin())
2647
2174
  {
2648
2175
    /* 'table_list' is a stored table. */
2649
2176
    assert(table_list->table);
2663
2190
    */
2664
2191
    if (table_name && table_name[0])
2665
2192
    {
2666
 
      List_iterator<TableList> it(table_list->nested_join->join_list);
 
2193
      List_iterator<TableList> it(table_list->getNestedJoin()->join_list);
2667
2194
      TableList *table;
2668
2195
      while ((table= it++))
2669
2196
      {
2711
2238
        field_to_set= fld;
2712
2239
      if (field_to_set)
2713
2240
      {
2714
 
        Table *table= field_to_set->table;
 
2241
        Table *table= field_to_set->getTable();
2715
2242
        if (session->mark_used_columns == MARK_COLUMNS_READ)
2716
2243
          table->setReadSet(field_to_set->field_index);
2717
2244
        else
2957
2484
 
2958
2485
 
2959
2486
Item **
2960
 
find_item_in_list(Item *find, List<Item> &items, uint32_t *counter,
 
2487
find_item_in_list(Session *session,
 
2488
                  Item *find, List<Item> &items, uint32_t *counter,
2961
2489
                  find_item_error_report_type report_error,
2962
2490
                  enum_resolution_type *resolution)
2963
2491
{
3037
2565
            */
3038
2566
            if (report_error != IGNORE_ERRORS)
3039
2567
              my_error(ER_NON_UNIQ_ERROR, MYF(0),
3040
 
                       find->full_name(), current_session->where);
 
2568
                       find->full_name(), session->where);
3041
2569
            return (Item**) 0;
3042
2570
          }
3043
2571
          found_unaliased= li.ref();
3068
2596
              continue;                           // Same field twice
3069
2597
            if (report_error != IGNORE_ERRORS)
3070
2598
              my_error(ER_NON_UNIQ_ERROR, MYF(0),
3071
 
                       find->full_name(), current_session->where);
 
2599
                       find->full_name(), session->where);
3072
2600
            return (Item**) 0;
3073
2601
          }
3074
2602
          found= li.ref();
3120
2648
    {
3121
2649
      if (report_error != IGNORE_ERRORS)
3122
2650
        my_error(ER_NON_UNIQ_ERROR, MYF(0),
3123
 
                 find->full_name(), current_session->where);
 
2651
                 find->full_name(), session->where);
3124
2652
      return (Item **) 0;
3125
2653
    }
3126
2654
    if (found_unaliased)
3136
2664
  {
3137
2665
    if (report_error == REPORT_ALL_ERRORS)
3138
2666
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
3139
 
               find->full_name(), current_session->where);
 
2667
               find->full_name(), session->where);
3140
2668
    return (Item **) 0;
3141
2669
  }
3142
2670
  else
3254
2782
    Leaf table references to which new natural join columns are added
3255
2783
    if the leaves are != NULL.
3256
2784
  */
3257
 
  TableList *leaf_1= (table_ref_1->nested_join &&
3258
 
                      !table_ref_1->is_natural_join) ?
 
2785
  TableList *leaf_1= (table_ref_1->getNestedJoin() &&
 
2786
                      ! table_ref_1->is_natural_join) ?
3259
2787
    NULL : table_ref_1;
3260
 
  TableList *leaf_2= (table_ref_2->nested_join &&
3261
 
                      !table_ref_2->is_natural_join) ?
 
2788
  TableList *leaf_2= (table_ref_2->getNestedJoin() &&
 
2789
                      ! table_ref_2->is_natural_join) ?
3262
2790
    NULL : table_ref_2;
3263
2791
 
3264
2792
  *found_using_fields= 0;
3270
2798
    /* true if field_name_1 is a member of using_fields */
3271
2799
    bool is_using_column_1;
3272
2800
    if (!(nj_col_1= it_1.get_or_create_column_ref(leaf_1)))
3273
 
      goto err;
 
2801
      return(result);
3274
2802
    field_name_1= nj_col_1->name();
3275
2803
    is_using_column_1= using_fields &&
3276
2804
      test_if_string_in_list(field_name_1, using_fields);
3288
2816
      Natural_join_column *cur_nj_col_2;
3289
2817
      const char *cur_field_name_2;
3290
2818
      if (!(cur_nj_col_2= it_2.get_or_create_column_ref(leaf_2)))
3291
 
        goto err;
 
2819
        return(result);
3292
2820
      cur_field_name_2= cur_nj_col_2->name();
3293
2821
 
3294
2822
      /*
3308
2836
            (found && (!using_fields || is_using_column_1)))
3309
2837
        {
3310
2838
          my_error(ER_NON_UNIQ_ERROR, MYF(0), field_name_1, session->where);
3311
 
          goto err;
 
2839
          return(result);
3312
2840
        }
3313
2841
        nj_col_2= cur_nj_col_2;
3314
2842
        found= true;
3341
2869
      Item_func_eq *eq_cond;
3342
2870
 
3343
2871
      if (!item_1 || !item_2)
3344
 
        goto err;                               // out of memory
 
2872
        return(result); // out of memory
3345
2873
 
3346
2874
      /*
3347
2875
        In the case of no_wrap_view_item == 0, the created items must be
3366
2894
      */
3367
2895
      if (set_new_item_local_context(session, item_ident_1, nj_col_1->table_ref) ||
3368
2896
          set_new_item_local_context(session, item_ident_2, nj_col_2->table_ref))
3369
 
        goto err;
 
2897
        return(result);
3370
2898
 
3371
2899
      if (!(eq_cond= new Item_func_eq(item_ident_1, item_ident_2)))
3372
 
        goto err;                               /* Out of memory. */
 
2900
        return(result);                               /* Out of memory. */
3373
2901
 
3374
2902
      /*
3375
2903
        Add the new equi-join condition to the ON clause. Notice that
3415
2943
  */
3416
2944
  result= false;
3417
2945
 
3418
 
err:
3419
2946
  return(result);
3420
2947
}
3421
2948
 
3457
2984
*/
3458
2985
 
3459
2986
static bool
3460
 
store_natural_using_join_columns(Session *,
 
2987
store_natural_using_join_columns(Session *session,
3461
2988
                                 TableList *natural_using_join,
3462
2989
                                 TableList *table_ref_1,
3463
2990
                                 TableList *table_ref_2,
3473
3000
 
3474
3001
  if (!(non_join_columns= new List<Natural_join_column>) ||
3475
3002
      !(natural_using_join->join_columns= new List<Natural_join_column>))
3476
 
    goto err;
 
3003
  {
 
3004
    return(result);
 
3005
  }
3477
3006
 
3478
3007
  /* Append the columns of the first join operand. */
3479
3008
  for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next())
3511
3040
        if (!(common_field= it++))
3512
3041
        {
3513
3042
          my_error(ER_BAD_FIELD_ERROR, MYF(0), using_field_name_ptr,
3514
 
                   current_session->where);
3515
 
          goto err;
 
3043
                   session->where);
 
3044
          return(result);
3516
3045
        }
3517
3046
        if (!my_strcasecmp(system_charset_info,
3518
3047
                           common_field->name(), using_field_name_ptr))
3540
3069
 
3541
3070
  result= false;
3542
3071
 
3543
 
err:
3544
3072
  return(result);
3545
3073
}
3546
3074
 
3583
3111
  bool result= true;
3584
3112
 
3585
3113
  /* Call the procedure recursively for each nested table reference. */
3586
 
  if (table_ref->nested_join)
 
3114
  if (table_ref->getNestedJoin())
3587
3115
  {
3588
 
    List_iterator_fast<TableList> nested_it(table_ref->nested_join->join_list);
 
3116
    List_iterator_fast<TableList> nested_it(table_ref->getNestedJoin()->join_list);
3589
3117
    TableList *same_level_left_neighbor= nested_it++;
3590
3118
    TableList *same_level_right_neighbor= NULL;
3591
3119
    /* Left/right-most neighbors, possibly at higher levels in the join tree. */
3610
3138
          cur_table_ref->outer_join & JOIN_TYPE_RIGHT)
3611
3139
      {
3612
3140
        /* This can happen only for JOIN ... ON. */
3613
 
        assert(table_ref->nested_join->join_list.elements == 2);
 
3141
        assert(table_ref->getNestedJoin()->join_list.elements == 2);
3614
3142
        std::swap(same_level_left_neighbor, cur_table_ref);
3615
3143
      }
3616
3144
 
3623
3151
      real_right_neighbor= (same_level_right_neighbor) ?
3624
3152
        same_level_right_neighbor : right_neighbor;
3625
3153
 
3626
 
      if (cur_table_ref->nested_join &&
 
3154
      if (cur_table_ref->getNestedJoin() &&
3627
3155
          store_top_level_join_columns(session, cur_table_ref,
3628
3156
                                       real_left_neighbor, real_right_neighbor))
3629
 
        goto err;
 
3157
        return(result);
3630
3158
      same_level_right_neighbor= cur_table_ref;
3631
3159
    }
3632
3160
  }
3637
3165
  */
3638
3166
  if (table_ref->is_natural_join)
3639
3167
  {
3640
 
    assert(table_ref->nested_join &&
3641
 
           table_ref->nested_join->join_list.elements == 2);
3642
 
    List_iterator_fast<TableList> operand_it(table_ref->nested_join->join_list);
 
3168
    assert(table_ref->getNestedJoin() &&
 
3169
           table_ref->getNestedJoin()->join_list.elements == 2);
 
3170
    List_iterator_fast<TableList> operand_it(table_ref->getNestedJoin()->join_list);
3643
3171
    /*
3644
3172
      Notice that the order of join operands depends on whether table_ref
3645
3173
      represents a LEFT or a RIGHT join. In a RIGHT join, the operands are
3658
3186
      std::swap(table_ref_1, table_ref_2);
3659
3187
    if (mark_common_columns(session, table_ref_1, table_ref_2,
3660
3188
                            using_fields, &found_using_fields))
3661
 
      goto err;
 
3189
      return(result);
3662
3190
 
3663
3191
    /*
3664
3192
      Swap the join operands back, so that we pick the columns of the second
3670
3198
    if (store_natural_using_join_columns(session, table_ref, table_ref_1,
3671
3199
                                         table_ref_2, using_fields,
3672
3200
                                         found_using_fields))
3673
 
      goto err;
 
3201
      return(result);
3674
3202
 
3675
3203
    /*
3676
3204
      Change NATURAL JOIN to JOIN ... ON. We do this for both operands
3703
3231
  }
3704
3232
  result= false; /* All is OK. */
3705
3233
 
3706
 
err:
3707
3234
  return(result);
3708
3235
}
3709
3236
 
4105
3632
    assert(tables->is_leaf_for_name_resolution());
4106
3633
 
4107
3634
    if ((table_name && my_strcasecmp(table_alias_charset, table_name, tables->alias)) ||
4108
 
        (db_name && strcasecmp(tables->db,db_name)))
 
3635
        (db_name && strcasecmp(tables->getSchemaName(),db_name)))
4109
3636
      continue;
4110
3637
 
4111
3638
    /*
4141
3668
      if ((field= field_iterator.field()))
4142
3669
      {
4143
3670
        /* Mark fields as used to allow storage engine to optimze access */
4144
 
        field->table->setReadSet(field->field_index);
 
3671
        field->getTable()->setReadSet(field->field_index);
4145
3672
        if (table)
4146
3673
        {
4147
3674
          table->covering_keys&= field->part_of_key;
4179
3706
      For NATURAL joins, used_tables is updated in the IF above.
4180
3707
    */
4181
3708
    if (table)
4182
 
      table->used_fields= table->s->fields;
 
3709
      table->used_fields= table->getShare()->sizeFields();
4183
3710
  }
4184
3711
  if (found)
4185
3712
    return false;
4269
3796
          goto err_no_arena;
4270
3797
        select_lex->cond_count++;
4271
3798
      }
4272
 
      embedding= embedded->embedding;
 
3799
      embedding= embedded->getEmbedding();
4273
3800
    }
4274
3801
    while (embedding &&
4275
 
           embedding->nested_join->join_list.head() == embedded);
 
3802
           embedding->getNestedJoin()->join_list.head() == embedded);
4276
3803
 
4277
3804
  }
4278
3805
  session->session_marker= save_session_marker;
4331
3858
      thus we safely can take table from the first field.
4332
3859
    */
4333
3860
    field= static_cast<Item_field *>(f++);
4334
 
    table= field->field->table;
 
3861
    table= field->field->getTable();
4335
3862
    table->auto_increment_field_not_null= false;
4336
3863
    f.rewind();
4337
3864
  }
4341
3868
    value= v++;
4342
3869
 
4343
3870
    Field *rfield= field->field;
4344
 
    table= rfield->table;
 
3871
    table= rfield->getTable();
4345
3872
 
4346
3873
    if (rfield == table->next_number_field)
4347
3874
      table->auto_increment_field_not_null= true;
4348
3875
    if ((value->save_in_field(rfield, 0) < 0) && !ignore_errors)
4349
3876
    {
4350
3877
      my_message(ER_UNKNOWN_ERROR, ER(ER_UNKNOWN_ERROR), MYF(0));
4351
 
      goto err;
 
3878
      if (table)
 
3879
        table->auto_increment_field_not_null= false;
 
3880
 
 
3881
      return true;
4352
3882
    }
4353
3883
  }
4354
3884
 
4355
3885
  return session->is_error();
4356
 
 
4357
 
err:
4358
 
  if (table)
4359
 
    table->auto_increment_field_not_null= false;
4360
 
 
4361
 
  return true;
4362
3886
}
4363
3887
 
4364
3888
 
4398
3922
      On INSERT or UPDATE fields are checked to be from the same table,
4399
3923
      thus we safely can take table from the first field.
4400
3924
    */
4401
 
    table= (*ptr)->table;
 
3925
    table= (*ptr)->getTable();
4402
3926
    table->auto_increment_field_not_null= false;
4403
3927
  }
4404
3928
  while ((field = *ptr++) && ! session->is_error())
4405
3929
  {
4406
3930
    value=v++;
4407
 
    table= field->table;
 
3931
    table= field->getTable();
4408
3932
    if (field == table->next_number_field)
4409
3933
      table->auto_increment_field_not_null= true;
4410
3934
    if (value->save_in_field(field, 0) < 0)
4411
 
      goto err;
 
3935
    {
 
3936
      if (table)
 
3937
        table->auto_increment_field_not_null= false;
 
3938
 
 
3939
      return true;
 
3940
    }
4412
3941
  }
4413
3942
 
4414
3943
  return(session->is_error());
4415
 
 
4416
 
err:
4417
 
  if (table)
4418
 
    table->auto_increment_field_not_null= false;
4419
 
 
4420
 
  return true;
4421
3944
}
4422
3945
 
4423
3946
 
4425
3948
{
4426
3949
  Session *session;
4427
3950
 
4428
 
  assert(drizzle_tmpdir);
 
3951
  assert(drizzle_tmpdir.size());
4429
3952
 
4430
3953
  if (!(session= new Session(plugin::Listen::getNullClient())))
4431
3954
    return true;
4432
3955
  session->thread_stack= (char*) &session;
4433
3956
  session->storeGlobals();
4434
3957
 
4435
 
  plugin::StorageEngine::removeLostTemporaryTables(*session, drizzle_tmpdir);
 
3958
  plugin::StorageEngine::removeLostTemporaryTables(*session, drizzle_tmpdir.c_str());
4436
3959
 
 
3960
  session->lockForDelete();
4437
3961
  delete session;
4438
3962
 
4439
3963
  return false;
4445
3969
  unireg support functions
4446
3970
 *****************************************************************************/
4447
3971
 
4448
 
/*
4449
 
  Invalidate any cache entries that are for some DB
4450
 
 
4451
 
  SYNOPSIS
4452
 
  remove_db_from_cache()
4453
 
  db            Database name. This will be in lower case if
4454
 
  lower_case_table_name is set
4455
 
 
4456
 
NOTE:
4457
 
We can't use hash_delete when looping hash_elements. We mark them first
4458
 
and afterwards delete those marked unused.
4459
 
*/
4460
 
 
4461
 
void remove_db_from_cache(SchemaIdentifier &schema_identifier)
4462
 
{
4463
 
  safe_mutex_assert_owner(&LOCK_open);
4464
 
 
4465
 
  for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
4466
 
  {
4467
 
    Table *table=(Table*) hash_element(&open_cache,idx);
4468
 
    if (not schema_identifier.getPath().compare(table->s->getSchemaName()))
4469
 
    {
4470
 
      table->s->version= 0L;                    /* Free when thread is ready */
4471
 
      if (not table->in_use)
4472
 
        relink_unused(table);
4473
 
    }
4474
 
  }
4475
 
  while (unused_tables && !unused_tables->s->version)
4476
 
    hash_delete(&open_cache,(unsigned char*) unused_tables);
4477
 
}
4478
 
 
4479
 
 
4480
 
/*
4481
 
  Mark all entries with the table as deleted to force an reopen of the table
4482
 
 
4483
 
  The table will be closed (not stored in cache) by the current thread when
4484
 
  close_thread_tables() is called.
4485
 
 
4486
 
  PREREQUISITES
4487
 
  Lock on LOCK_open()
4488
 
 
4489
 
  RETURN
4490
 
  0  This thread now have exclusive access to this table and no other thread
4491
 
  can access the table until close_thread_tables() is called.
4492
 
  1  Table is in use by another thread
4493
 
*/
4494
 
 
4495
 
bool remove_table_from_cache(Session *session, const char *db, const char *table_name,
4496
 
                             uint32_t flags)
4497
 
{
4498
 
  char key[MAX_DBKEY_LENGTH];
4499
 
  char *key_pos= key;
4500
 
  uint32_t key_length;
4501
 
  Table *table;
4502
 
  bool result= false; 
4503
 
  bool signalled= false;
4504
 
 
4505
 
  key_pos= strcpy(key_pos, db) + strlen(db);
4506
 
  key_pos= strcpy(key_pos+1, table_name) + strlen(table_name);
4507
 
  key_length= (uint32_t) (key_pos-key)+1;
4508
 
 
4509
 
  for (;;)
4510
 
  {
4511
 
    HASH_SEARCH_STATE state;
4512
 
    result= signalled= false;
4513
 
 
4514
 
    for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
4515
 
                                    &state);
4516
 
         table;
4517
 
         table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
4518
 
                                   &state))
4519
 
    {
4520
 
      Session *in_use;
4521
 
 
4522
 
      table->s->version=0L;             /* Free when thread is ready */
4523
 
      if (!(in_use=table->in_use))
4524
 
      {
4525
 
        relink_unused(table);
4526
 
      }
4527
 
      else if (in_use != session)
4528
 
      {
4529
 
        /*
4530
 
          Mark that table is going to be deleted from cache. This will
4531
 
          force threads that are in mysql_lock_tables() (but not yet
4532
 
          in thr_multi_lock()) to abort it's locks, close all tables and retry
4533
 
        */
4534
 
        in_use->some_tables_deleted= true;
4535
 
        if (table->is_name_opened())
4536
 
        {
4537
 
          result= true;
4538
 
        }
4539
 
        /*
4540
 
          Now we must abort all tables locks used by this thread
4541
 
          as the thread may be waiting to get a lock for another table.
4542
 
          Note that we need to hold LOCK_open while going through the
4543
 
          list. So that the other thread cannot change it. The other
4544
 
          thread must also hold LOCK_open whenever changing the
4545
 
          open_tables list. Aborting the MERGE lock after a child was
4546
 
          closed and before the parent is closed would be fatal.
4547
 
        */
4548
 
        for (Table *session_table= in_use->open_tables;
4549
 
             session_table ;
4550
 
             session_table= session_table->next)
4551
 
        {
4552
 
          /* Do not handle locks of MERGE children. */
4553
 
          if (session_table->db_stat)   // If table is open
4554
 
            signalled|= mysql_lock_abort_for_thread(session, session_table);
4555
 
        }
4556
 
      }
4557
 
      else
4558
 
        result= result || (flags & RTFC_OWNED_BY_Session_FLAG);
4559
 
    }
4560
 
    while (unused_tables && !unused_tables->s->version)
4561
 
      hash_delete(&open_cache,(unsigned char*) unused_tables);
4562
 
 
4563
 
    /* Remove table from table definition cache if it's not in use */
4564
 
    TableShare::release(key, key_length);
4565
 
 
4566
 
    if (result && (flags & RTFC_WAIT_OTHER_THREAD_FLAG))
4567
 
    {
4568
 
      /*
4569
 
        Signal any thread waiting for tables to be freed to
4570
 
        reopen their tables
4571
 
      */
4572
 
      broadcast_refresh();
4573
 
      if (!(flags & RTFC_CHECK_KILLED_FLAG) || !session->killed)
4574
 
      {
4575
 
        dropping_tables++;
4576
 
        if (likely(signalled))
4577
 
          (void) pthread_cond_wait(&COND_refresh, &LOCK_open);
4578
 
        else
4579
 
        {
4580
 
          struct timespec abstime;
4581
 
          /*
4582
 
            It can happen that another thread has opened the
4583
 
            table but has not yet locked any table at all. Since
4584
 
            it can be locked waiting for a table that our thread
4585
 
            has done LOCK Table x WRITE on previously, we need to
4586
 
            ensure that the thread actually hears our signal
4587
 
            before we go to sleep. Thus we wait for a short time
4588
 
            and then we retry another loop in the
4589
 
            remove_table_from_cache routine.
4590
 
          */
4591
 
          set_timespec(abstime, 10);
4592
 
          pthread_cond_timedwait(&COND_refresh, &LOCK_open, &abstime);
4593
 
        }
4594
 
        dropping_tables--;
4595
 
        continue;
4596
 
      }
4597
 
    }
4598
 
    break;
4599
 
  }
4600
 
  return result;
4601
 
}
 
3972
 
4602
3973
 
4603
3974
 
4604
3975
/**