~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_base.cc

  • Committer: Brian Aker
  • Date: 2010-10-12 05:21:02 UTC
  • Revision ID: brian@tangent.org-20101012052102-9yrbu1ye7n8n4b6n
Remove dead code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
#include "drizzled/internal/iocache.h"
54
54
#include "drizzled/drizzled.h"
55
55
#include "drizzled/plugin/authorization.h"
56
 
#include "drizzled/table/temporary.h"
57
 
#include "drizzled/table/placeholder.h"
58
 
#include "drizzled/table/unused.h"
 
56
#include "drizzled/table_placeholder.h"
59
57
 
60
58
using namespace std;
61
59
 
64
62
 
65
63
extern bool volatile shutdown_in_progress;
66
64
 
 
65
TableOpenCache &get_open_cache()
 
66
{
 
67
  static TableOpenCache open_cache;                             /* Used by mysql_test */
 
68
 
 
69
  return open_cache;
 
70
}
 
71
 
 
72
static void free_cache_entry(Table *entry);
 
73
 
 
74
void remove_table(Table *arg)
 
75
{
 
76
  TableOpenCacheRange ppp;
 
77
  ppp= get_open_cache().equal_range(arg->getShare()->getCacheKey());
 
78
 
 
79
  for (TableOpenCache::const_iterator iter= ppp.first;
 
80
         iter != ppp.second; ++iter)
 
81
  {
 
82
    Table *found_table= (*iter).second;
 
83
 
 
84
    if (found_table == arg)
 
85
    {
 
86
      free_cache_entry(arg);
 
87
      get_open_cache().erase(iter);
 
88
      return;
 
89
    }
 
90
  }
 
91
}
 
92
 
 
93
static bool add_table(Table *arg)
 
94
{
 
95
  TableOpenCache &open_cache(get_open_cache());
 
96
 
 
97
  TableOpenCache::iterator returnable= open_cache.insert(make_pair(arg->getShare()->getCacheKey(), arg));
 
98
 
 
99
  return not (returnable == open_cache.end());
 
100
}
 
101
 
 
102
class UnusedTables {
 
103
  Table *tables;                                /* Used by mysql_test */
 
104
 
 
105
  Table *getTable() const
 
106
  {
 
107
    return tables;
 
108
  }
 
109
 
 
110
  Table *setTable(Table *arg)
 
111
  {
 
112
    return tables= arg;
 
113
  }
 
114
 
 
115
public:
 
116
 
 
117
  void cull()
 
118
  {
 
119
    /* Free cache if too big */
 
120
    while (cached_open_tables() > table_cache_size && getTable())
 
121
      remove_table(getTable());
 
122
  }
 
123
 
 
124
  void cullByVersion()
 
125
  {
 
126
    while (getTable() && not getTable()->getShare()->getVersion())
 
127
      remove_table(getTable());
 
128
  }
 
129
  
 
130
  void link(Table *table)
 
131
  {
 
132
    if (getTable())
 
133
    {
 
134
      table->setNext(getTable());               /* Link in last */
 
135
      table->setPrev(getTable()->getPrev());
 
136
      getTable()->setPrev(table);
 
137
      table->getPrev()->setNext(table);
 
138
    }
 
139
    else
 
140
    {
 
141
      table->setPrev(setTable(table));
 
142
      table->setNext(table->getPrev());
 
143
      assert(table->getNext() == table && table->getPrev() == table);
 
144
    }
 
145
  }
 
146
 
 
147
 
 
148
  void unlink(Table *table)
 
149
  {
 
150
    table->unlink();
 
151
 
 
152
    /* Unlink the table from "unused_tables" list. */
 
153
    if (table == getTable())
 
154
    {  // First unused
 
155
      setTable(getTable()->getNext()); // Remove from link
 
156
      if (table == getTable())
 
157
        setTable(NULL);
 
158
    }
 
159
  }
 
160
 
 
161
/* move table first in unused links */
 
162
 
 
163
  void relink(Table *table)
 
164
  {
 
165
    if (table != getTable())
 
166
    {
 
167
      table->unlink();
 
168
 
 
169
      table->setNext(getTable());                       /* Link in unused tables */
 
170
      table->setPrev(getTable()->getPrev());
 
171
      getTable()->getPrev()->setNext(table);
 
172
      getTable()->setPrev(table);
 
173
      setTable(table);
 
174
    }
 
175
  }
 
176
 
 
177
 
 
178
  void clear()
 
179
  {
 
180
    while (getTable())
 
181
      remove_table(getTable());
 
182
  }
 
183
 
 
184
  UnusedTables():
 
185
    tables(NULL)
 
186
  { }
 
187
 
 
188
  ~UnusedTables()
 
189
  { 
 
190
  }
 
191
};
 
192
 
 
193
static UnusedTables unused_tables;
 
194
static int open_unireg_entry(Session *session,
 
195
                             Table *entry,
 
196
                             const char *alias,
 
197
                             TableIdentifier &identifier);
 
198
 
 
199
unsigned char *table_cache_key(const unsigned char *record,
 
200
                               size_t *length,
 
201
                               bool );
 
202
 
 
203
unsigned char *table_cache_key(const unsigned char *record,
 
204
                               size_t *length,
 
205
                               bool )
 
206
{
 
207
  Table *entry=(Table*) record;
 
208
  *length= entry->getShare()->getCacheKey().size();
 
209
  return (unsigned char*) &entry->getShare()->getCacheKey()[0];
 
210
}
 
211
 
67
212
bool table_cache_init(void)
68
213
{
69
214
  return false;
71
216
 
72
217
uint32_t cached_open_tables(void)
73
218
{
74
 
  return table::getCache().size();
 
219
  return get_open_cache().size();
75
220
}
76
221
 
77
222
void table_cache_free(void)
78
223
{
79
224
  refresh_version++;                            // Force close of open tables
80
225
 
81
 
  table::getUnused().clear();
82
 
  table::getCache().clear();
 
226
  unused_tables.clear();
 
227
  get_open_cache().clear();
83
228
}
84
229
 
85
230
/*
93
238
  By leaving the table in the table cache, it disallows any other thread
94
239
  to open the table
95
240
 
96
 
  session->getKilled() will be set if we run out of memory
 
241
  session->killed will be set if we run out of memory
97
242
 
98
243
  If closing a MERGE child, the calling function has to take care for
99
244
  closing the parent too, if necessary.
102
247
 
103
248
void close_handle_and_leave_table_as_lock(Table *table)
104
249
{
 
250
  TableShare *share, *old_share= table->getMutableShare();
105
251
  assert(table->db_stat);
106
252
  assert(table->getShare()->getType() == message::Table::STANDARD);
107
253
 
112
258
  */
113
259
  TableIdentifier identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName(), message::Table::INTERNAL);
114
260
  const TableIdentifier::Key &key(identifier.getKey());
115
 
  TableShare *share= new TableShare(identifier.getType(),
116
 
                                    identifier,
117
 
                                    const_cast<char *>(key.vector()),  static_cast<uint32_t>(table->getShare()->getCacheKeySize()));
 
261
  share= new TableShare(identifier.getType(),
 
262
                        identifier,
 
263
                        const_cast<char *>(&key[0]),  static_cast<uint32_t>(old_share->getCacheKeySize()));
118
264
 
119
265
  table->cursor->close();
120
266
  table->db_stat= 0;                            // Mark cursor closed
121
267
  TableShare::release(table->getMutableShare());
122
268
  table->setShare(share);
 
269
  table->cursor->change_table_ptr(table, table->getMutableShare());
123
270
}
124
271
 
125
272
 
137
284
  }
138
285
}
139
286
 
 
287
/*
 
288
  Remove table from the open table cache
 
289
 
 
290
  SYNOPSIS
 
291
  free_cache_entry()
 
292
  entry         Table to remove
 
293
 
 
294
  NOTE
 
295
  We need to have a lock on LOCK_open when calling this
 
296
*/
 
297
 
 
298
void free_cache_entry(Table *table)
 
299
{
 
300
  table->intern_close_table();
 
301
  if (not table->in_use)
 
302
  {
 
303
    unused_tables.unlink(table);
 
304
  }
 
305
 
 
306
  delete table;
 
307
}
 
308
 
140
309
/* Free resources allocated by filesort() and read_record() */
141
310
 
142
311
void Table::free_io_cache()
143
312
{
144
313
  if (sort.io_cache)
145
314
  {
146
 
    sort.io_cache->close_cached_file();
 
315
    close_cached_file(sort.io_cache);
147
316
    delete sort.io_cache;
148
317
    sort.io_cache= 0;
149
318
  }
155
324
 
156
325
  @param session Thread context (may be NULL)
157
326
  @param tables List of tables to remove from the cache
158
 
  @param have_lock If table::Cache::singleton().mutex() is locked
 
327
  @param have_lock If LOCK_open is locked
159
328
  @param wait_for_refresh Wait for a impending flush
160
329
  @param wait_for_placeholders Wait for tables being reopened so that the GRL
161
330
  won't proceed while write-locked tables are being reopened by other
171
340
  Session *session= this;
172
341
 
173
342
  {
174
 
    table::Cache::singleton().mutex().lock(); /* Optionally lock for remove tables from open_cahe if not in use */
 
343
    LOCK_open.lock(); /* Optionally lock for remove tables from open_cahe if not in use */
175
344
 
176
345
    if (tables == NULL)
177
346
    {
178
347
      refresh_version++;                                // Force close of open tables
179
348
 
180
 
      table::getUnused().clear();
 
349
      unused_tables.clear();
181
350
 
182
351
      if (wait_for_refresh)
183
352
      {
208
377
          again. There they will wait until we update all tables version
209
378
          below.
210
379
 
211
 
          Setting some_tables_deleted is done by table::Cache::singleton().removeTable()
 
380
          Setting some_tables_deleted is done by remove_table_from_cache()
212
381
          in the other branch.
213
382
 
214
383
          In other words (reviewer suggestion): You need this setting of
218
387
          after the call to Session::close_old_data_files() i.e. after removal of
219
388
          current thread locks.
220
389
        */
221
 
        for (table::CacheMap::const_iterator iter= table::getCache().begin();
222
 
             iter != table::getCache().end();
 
390
        for (TableOpenCache::const_iterator iter= get_open_cache().begin();
 
391
             iter != get_open_cache().end();
223
392
             iter++)
224
393
        {
225
394
          Table *table= (*iter).second;
233
402
      bool found= false;
234
403
      for (TableList *table= tables; table; table= table->next_local)
235
404
      {
236
 
        TableIdentifier identifier(table->getSchemaName(), table->getTableName());
237
 
        if (table::Cache::singleton().removeTable(session, identifier,
 
405
        TableIdentifier identifier(table->db, table->table_name);
 
406
        if (remove_table_from_cache(session, identifier,
238
407
                                    RTFC_OWNED_BY_Session_FLAG))
239
408
        {
240
409
          found= true;
250
419
        If there is any table that has a lower refresh_version, wait until
251
420
        this is closed (or this thread is killed) before returning
252
421
      */
253
 
      session->mysys_var->current_mutex= &table::Cache::singleton().mutex();
 
422
      session->mysys_var->current_mutex= &LOCK_open;
254
423
      session->mysys_var->current_cond= &COND_refresh;
255
424
      session->set_proc_info("Flushing tables");
256
425
 
258
427
 
259
428
      bool found= true;
260
429
      /* Wait until all threads has closed all the tables we had locked */
261
 
      while (found && ! session->getKilled())
 
430
      while (found && ! session->killed)
262
431
      {
263
432
        found= false;
264
 
        for (table::CacheMap::const_iterator iter= table::getCache().begin();
265
 
             iter != table::getCache().end();
 
433
        for (TableOpenCache::const_iterator iter= get_open_cache().begin();
 
434
             iter != get_open_cache().end();
266
435
             iter++)
267
436
        {
268
437
          Table *table= (*iter).second;
287
456
                                                     (table->open_placeholder && wait_for_placeholders)))
288
457
          {
289
458
            found= true;
290
 
            boost_unique_lock_t scoped(table::Cache::singleton().mutex(), boost::adopt_lock_t());
 
459
            boost_unique_lock_t scoped(LOCK_open, boost::adopt_lock_t());
291
460
            COND_refresh.wait(scoped);
292
461
            scoped.release();
293
462
            break;
313
482
      }
314
483
    }
315
484
 
316
 
    table::Cache::singleton().mutex().unlock();
 
485
    LOCK_open.unlock();
317
486
  }
318
487
 
319
488
  if (wait_for_refresh)
335
504
bool Session::free_cached_table()
336
505
{
337
506
  bool found_old_table= false;
338
 
  table::Concurrent *table= static_cast<table::Concurrent *>(open_tables);
 
507
  Table *table= open_tables;
339
508
 
340
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
 
509
  safe_mutex_assert_owner(LOCK_open.native_handle());
341
510
  assert(table->key_read == 0);
342
511
  assert(!table->cursor || table->cursor->inited == Cursor::NONE);
343
512
 
346
515
  if (table->needs_reopen_or_name_lock() ||
347
516
      version != refresh_version || !table->db_stat)
348
517
  {
349
 
    table::remove_table(table);
 
518
    remove_table(table);
350
519
    found_old_table= true;
351
520
  }
352
521
  else
359
528
 
360
529
    /* Free memory and reset for next loop */
361
530
    table->cursor->ha_reset();
362
 
    table->in_use= NULL;
 
531
    table->in_use= false;
363
532
 
364
 
    table::getUnused().link(table);
 
533
    unused_tables.link(table);
365
534
  }
366
535
 
367
536
  return found_old_table;
380
549
{
381
550
  bool found_old_table= false;
382
551
 
383
 
  safe_mutex_assert_not_owner(table::Cache::singleton().mutex().native_handle());
 
552
  safe_mutex_assert_not_owner(LOCK_open.native_handle());
384
553
 
385
 
  boost_unique_lock_t scoped_lock(table::Cache::singleton().mutex()); /* Close all open tables on Session */
 
554
  boost_unique_lock_t scoped_lock(LOCK_open); /* Close all open tables on Session */
386
555
 
387
556
  while (open_tables)
388
557
  {
393
562
  if (found_old_table)
394
563
  {
395
564
    /* Tell threads waiting for refresh that something has happened */
396
 
    locking::broadcast_refresh();
 
565
    broadcast_refresh();
397
566
  }
398
567
}
399
568
 
423
592
  for (; table; table= table->*link )
424
593
  {
425
594
    if ((table->table == 0 || table->table->getShare()->getType() == message::Table::STANDARD) &&
426
 
        strcasecmp(table->getSchemaName(), db_name) == 0 &&
427
 
        strcasecmp(table->getTableName(), table_name) == 0)
 
595
        strcasecmp(table->db, db_name) == 0 &&
 
596
        strcasecmp(table->table_name, table_name) == 0)
428
597
      break;
429
598
  }
430
599
  return table;
495
664
    */
496
665
    assert(table);
497
666
  }
498
 
  d_name= table->getSchemaName();
499
 
  t_name= table->getTableName();
 
667
  d_name= table->db;
 
668
  t_name= table->table_name;
500
669
  t_alias= table->alias;
501
670
 
502
671
  for (;;)
517
686
}
518
687
 
519
688
 
520
 
void Open_tables_state::doGetTableNames(const SchemaIdentifier &schema_identifier,
521
 
                                        std::set<std::string>& set_of_names)
 
689
void Session::doGetTableNames(const SchemaIdentifier &schema_identifier,
 
690
                              std::set<std::string>& set_of_names)
522
691
{
523
 
  for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
 
692
  for (Table *table= temporary_tables ; table ; table= table->getNext())
524
693
  {
525
694
    if (schema_identifier.compare(table->getShare()->getSchemaName()))
526
695
    {
529
698
  }
530
699
}
531
700
 
532
 
void Open_tables_state::doGetTableNames(CachedDirectory &,
533
 
                                        const SchemaIdentifier &schema_identifier,
534
 
                                        std::set<std::string> &set_of_names)
 
701
void Session::doGetTableNames(CachedDirectory &,
 
702
                              const SchemaIdentifier &schema_identifier,
 
703
                              std::set<std::string> &set_of_names)
535
704
{
536
705
  doGetTableNames(schema_identifier, set_of_names);
537
706
}
538
707
 
539
 
void Open_tables_state::doGetTableIdentifiers(const SchemaIdentifier &schema_identifier,
540
 
                                              TableIdentifier::vector &set_of_identifiers)
 
708
void Session::doGetTableIdentifiers(const SchemaIdentifier &schema_identifier,
 
709
                                    TableIdentifiers &set_of_identifiers)
541
710
{
542
 
  for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
 
711
  for (Table *table= temporary_tables ; table ; table= table->getNext())
543
712
  {
544
713
    if (schema_identifier.compare(table->getShare()->getSchemaName()))
545
714
    {
550
719
  }
551
720
}
552
721
 
553
 
void Open_tables_state::doGetTableIdentifiers(CachedDirectory &,
554
 
                                              const SchemaIdentifier &schema_identifier,
555
 
                                              TableIdentifier::vector &set_of_identifiers)
 
722
void Session::doGetTableIdentifiers(CachedDirectory &,
 
723
                                    const SchemaIdentifier &schema_identifier,
 
724
                                    TableIdentifiers &set_of_identifiers)
556
725
{
557
726
  doGetTableIdentifiers(schema_identifier, set_of_identifiers);
558
727
}
559
728
 
560
 
bool Open_tables_state::doDoesTableExist(const TableIdentifier &identifier)
 
729
bool Session::doDoesTableExist(const TableIdentifier &identifier)
561
730
{
562
 
  for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
 
731
  for (Table *table= temporary_tables ; table ; table= table->getNext())
563
732
  {
564
733
    if (table->getShare()->getType() == message::Table::TEMPORARY)
565
734
    {
573
742
  return false;
574
743
}
575
744
 
576
 
int Open_tables_state::doGetTableDefinition(const TableIdentifier &identifier,
 
745
int Session::doGetTableDefinition(const TableIdentifier &identifier,
577
746
                                  message::Table &table_proto)
578
747
{
579
 
  for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
 
748
  for (Table *table= temporary_tables ; table ; table= table->getNext())
580
749
  {
581
750
    if (table->getShare()->getType() == message::Table::TEMPORARY)
582
751
    {
592
761
  return ENOENT;
593
762
}
594
763
 
595
 
Table *Open_tables_state::find_temporary_table(const TableIdentifier &identifier)
 
764
Table *Session::find_temporary_table(const char *new_db, const char *table_name)
 
765
{
 
766
  char  key[MAX_DBKEY_LENGTH];
 
767
  uint  key_length;
 
768
 
 
769
  key_length= TableIdentifier::createKey(key, new_db, table_name);
 
770
 
 
771
  for (Table *table= temporary_tables ; table ; table= table->getNext())
 
772
  {
 
773
    const TableIdentifier::Key &share_key(table->getShare()->getCacheKey());
 
774
    if (share_key.size() == key_length &&
 
775
        not memcmp(&share_key[0], key, key_length))
 
776
    {
 
777
      return table;
 
778
    }
 
779
  }
 
780
  return NULL;                               // Not a temporary table
 
781
}
 
782
 
 
783
Table *Session::find_temporary_table(TableList *table_list)
 
784
{
 
785
  return find_temporary_table(table_list->db, table_list->table_name);
 
786
}
 
787
 
 
788
Table *Session::find_temporary_table(TableIdentifier &identifier)
596
789
{
597
790
  for (Table *table= temporary_tables ; table ; table= table->getNext())
598
791
  {
630
823
  @retval -1  the table is in use by a outer query
631
824
*/
632
825
 
633
 
int Open_tables_state::drop_temporary_table(const drizzled::TableIdentifier &identifier)
 
826
int Session::drop_temporary_table(TableList *table_list)
634
827
{
635
828
  Table *table;
636
829
 
637
 
  if (not (table= find_temporary_table(identifier)))
 
830
  if (not (table= find_temporary_table(table_list)))
638
831
    return 1;
639
832
 
640
833
  /* Table might be in use by some outer statement. */
641
 
  if (table->query_id && table->query_id != getQueryId())
 
834
  if (table->query_id && table->query_id != query_id)
642
835
  {
643
836
    my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
644
837
    return -1;
656
849
 
657
850
  @param  session     Thread context
658
851
  @param  find    Table to remove
659
 
 
660
 
  @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 TableShare::shared_ptr here.
661
852
*/
662
853
 
663
854
void Session::unlink_open_table(Table *find)
664
855
{
665
 
  const TableIdentifier::Key find_key(find->getShare()->getCacheKey());
666
 
  Table **prev;
667
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
 
856
  char key[MAX_DBKEY_LENGTH];
 
857
  uint32_t key_length= find->getShare()->getCacheKeySize();
 
858
  Table *list, **prev;
 
859
  safe_mutex_assert_owner(LOCK_open.native_handle());
668
860
 
 
861
  memcpy(key, &find->getMutableShare()->getCacheKey()[0], key_length);
669
862
  /*
670
 
    Note that we need to hold table::Cache::singleton().mutex() while changing the
 
863
    Note that we need to hold LOCK_open while changing the
671
864
    open_tables list. Another thread may work on it.
672
 
    (See: table::Cache::singleton().removeTable(), mysql_wait_completed_table())
 
865
    (See: remove_table_from_cache(), mysql_wait_completed_table())
673
866
    Closing a MERGE child before the parent would be fatal if the
674
867
    other thread tries to abort the MERGE lock in between.
675
868
  */
676
869
  for (prev= &open_tables; *prev; )
677
870
  {
678
 
    Table *list= *prev;
 
871
    list= *prev;
679
872
 
680
 
    if (list->getShare()->getCacheKey() == find_key)
 
873
    if (list->getShare()->getCacheKeySize() == key_length &&
 
874
        not memcmp(&list->getShare()->getCacheKey()[0], key, key_length))
681
875
    {
682
876
      /* Remove table from open_tables list. */
683
877
      *prev= list->getNext();
684
878
 
685
879
      /* Close table. */
686
 
      table::remove_table(static_cast<table::Concurrent *>(list));
 
880
      remove_table(list);
687
881
    }
688
882
    else
689
883
    {
693
887
  }
694
888
 
695
889
  // Notify any 'refresh' threads
696
 
  locking::broadcast_refresh();
 
890
  broadcast_refresh();
697
891
}
698
892
 
699
893
 
716
910
  table that was locked with LOCK TABLES.
717
911
*/
718
912
 
719
 
void Session::drop_open_table(Table *table, const TableIdentifier &identifier)
 
913
void Session::drop_open_table(Table *table, TableIdentifier &identifier)
720
914
{
721
915
  if (table->getShare()->getType())
722
916
  {
724
918
  }
725
919
  else
726
920
  {
727
 
    boost_unique_lock_t scoped_lock(table::Cache::singleton().mutex()); /* Close and drop a table (AUX routine) */
 
921
    boost_unique_lock_t scoped_lock(LOCK_open); /* Close and drop a table (AUX routine) */
728
922
    /*
729
923
      unlink_open_table() also tells threads waiting for refresh or close
730
924
      that something has happened.
731
925
    */
732
926
    unlink_open_table(table);
733
 
    plugin::StorageEngine::dropTable(*this, identifier);
 
927
    quick_rm_table(*this, identifier);
734
928
  }
735
929
}
736
930
 
766
960
      mutex is unlocked
767
961
    */
768
962
    boost_unique_lock_t scopedLock(mutex, boost::adopt_lock_t());
769
 
    if (not getKilled())
 
963
    if (not killed)
770
964
    {
771
965
      cond.wait(scopedLock);
772
966
    }
778
972
}
779
973
 
780
974
 
 
975
/*
 
976
  Open table which is already name-locked by this thread.
 
977
 
 
978
  SYNOPSIS
 
979
  reopen_name_locked_table()
 
980
  session         Thread handle
 
981
  table_list  TableList object for table to be open, TableList::table
 
982
  member should point to Table object which was used for
 
983
  name-locking.
 
984
  link_in     true  - if Table object for table to be opened should be
 
985
  linked into Session::open_tables list.
 
986
  false - placeholder used for name-locking is already in
 
987
  this list so we only need to preserve Table::next
 
988
  pointer.
 
989
 
 
990
  NOTE
 
991
  This function assumes that its caller already acquired LOCK_open mutex.
 
992
 
 
993
  RETURN VALUE
 
994
  false - Success
 
995
  true  - Error
 
996
*/
 
997
 
 
998
bool Session::reopen_name_locked_table(TableList* table_list, bool link_in)
 
999
{
 
1000
  Table *table= table_list->table;
 
1001
  TableShare *share;
 
1002
  char *table_name= table_list->table_name;
 
1003
  Table orig_table;
 
1004
 
 
1005
  safe_mutex_assert_owner(LOCK_open.native_handle());
 
1006
 
 
1007
  if (killed || !table)
 
1008
    return true;
 
1009
 
 
1010
  orig_table= *table;
 
1011
 
 
1012
  TableIdentifier identifier(table_list->db, table_list->table_name);
 
1013
  if (open_unireg_entry(this, table, table_name, identifier))
 
1014
  {
 
1015
    table->intern_close_table();
 
1016
    /*
 
1017
      If there was an error during opening of table (for example if it
 
1018
      does not exist) '*table' object can be wiped out. To be able
 
1019
      properly release name-lock in this case we should restore this
 
1020
      object to its original state.
 
1021
    */
 
1022
    *table= orig_table;
 
1023
    return true;
 
1024
  }
 
1025
 
 
1026
  share= table->getMutableShare();
 
1027
  /*
 
1028
    We want to prevent other connections from opening this table until end
 
1029
    of statement as it is likely that modifications of table's metadata are
 
1030
    not yet finished (for example CREATE TRIGGER have to change .TRG cursor,
 
1031
    or we might want to drop table if CREATE TABLE ... SELECT fails).
 
1032
    This also allows us to assume that no other connection will sneak in
 
1033
    before we will get table-level lock on this table.
 
1034
  */
 
1035
  share->resetVersion();
 
1036
  table->in_use = this;
 
1037
 
 
1038
  if (link_in)
 
1039
  {
 
1040
    table->setNext(open_tables);
 
1041
    open_tables= table;
 
1042
  }
 
1043
  else
 
1044
  {
 
1045
    /*
 
1046
      Table object should be already in Session::open_tables list so we just
 
1047
      need to set Table::next correctly.
 
1048
    */
 
1049
    table->setNext(orig_table.getNext());
 
1050
  }
 
1051
 
 
1052
  table->tablenr= current_tablenr++;
 
1053
  table->used_fields= 0;
 
1054
  table->const_table= 0;
 
1055
  table->null_row= false;
 
1056
  table->maybe_null= false;
 
1057
  table->force_index= false;
 
1058
  table->status= STATUS_NO_RECORD;
 
1059
 
 
1060
  return false;
 
1061
}
 
1062
 
 
1063
 
781
1064
/**
782
1065
  Create and insert into table cache placeholder for table
783
1066
  which will prevent its opening (or creation) (a.k.a lock
791
1074
  case of failure.
792
1075
*/
793
1076
 
794
 
table::Placeholder *Session::table_cache_insert_placeholder(const drizzled::TableIdentifier &arg)
 
1077
Table *Session::table_cache_insert_placeholder(const char *db_name, const char *table_name)
795
1078
{
796
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
 
1079
  safe_mutex_assert_owner(LOCK_open.native_handle());
797
1080
 
798
1081
  /*
799
1082
    Create a table entry with the right key and with an old refresh version
800
1083
  */
801
 
  TableIdentifier identifier(arg.getSchemaName(), arg.getTableName(), message::Table::INTERNAL);
802
 
  table::Placeholder *table= new table::Placeholder(this, identifier);
 
1084
  TableIdentifier identifier(db_name, table_name, message::Table::INTERNAL);
 
1085
  TablePlaceholder *table= new TablePlaceholder(this, identifier);
803
1086
 
804
 
  if (not table::Cache::singleton().insert(table))
 
1087
  if (not add_table(table))
805
1088
  {
806
1089
    delete table;
807
1090
 
833
1116
  @retval  true   Error occured (OOM)
834
1117
  @retval  false  Success. 'table' parameter set according to above rules.
835
1118
*/
836
 
bool Session::lock_table_name_if_not_cached(const TableIdentifier &identifier, Table **table)
 
1119
bool Session::lock_table_name_if_not_cached(TableIdentifier &identifier, Table **table)
837
1120
{
838
1121
  const TableIdentifier::Key &key(identifier.getKey());
839
1122
 
840
 
  boost_unique_lock_t scope_lock(table::Cache::singleton().mutex()); /* Obtain a name lock even though table is not in cache (like for create table)  */
841
 
 
842
 
  table::CacheMap::iterator iter;
843
 
 
844
 
  iter= table::getCache().find(key);
845
 
 
846
 
  if (iter != table::getCache().end())
 
1123
  boost_unique_lock_t scope_lock(LOCK_open); /* Obtain a name lock even though table is not in cache (like for create table)  */
 
1124
 
 
1125
  TableOpenCache::iterator iter;
 
1126
 
 
1127
  iter= get_open_cache().find(key);
 
1128
 
 
1129
  if (iter != get_open_cache().end())
847
1130
  {
848
1131
    *table= 0;
849
1132
    return false;
850
1133
  }
851
1134
 
852
 
  if (not (*table= table_cache_insert_placeholder(identifier)))
 
1135
  if (not (*table= table_cache_insert_placeholder(identifier.getSchemaName().c_str(), identifier.getTableName().c_str())))
853
1136
  {
854
1137
    return true;
855
1138
  }
909
1192
  if (check_stack_overrun(this, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
910
1193
    return NULL;
911
1194
 
912
 
  if (getKilled())
 
1195
  if (killed)
913
1196
    return NULL;
914
1197
 
915
 
  TableIdentifier identifier(table_list->getSchemaName(), table_list->getTableName());
 
1198
  TableIdentifier identifier(table_list->db, table_list->table_name);
916
1199
  const TableIdentifier::Key &key(identifier.getKey());
917
 
  table::CacheRange ppp;
 
1200
  TableOpenCacheRange ppp;
918
1201
 
919
1202
  /*
920
1203
    Unless requested otherwise, try to resolve this table in the list
924
1207
    TODO -> move this block into a separate function.
925
1208
  */
926
1209
  bool reset= false;
927
 
  for (table= getTemporaryTables(); table ; table=table->getNext())
 
1210
  for (table= temporary_tables; table ; table=table->getNext())
928
1211
  {
929
1212
    if (table->getShare()->getCacheKey() == key)
930
1213
    {
949
1232
  {
950
1233
    if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
951
1234
    {
952
 
      my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->getSchemaName(), table_list->getTableName());
 
1235
      my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
953
1236
      return NULL;
954
1237
    }
955
1238
 
994
1277
      until no one holds a name lock on the table.
995
1278
      - if there is no such Table in the name cache, read the table definition
996
1279
      and insert it into the cache.
997
 
      We perform all of the above under table::Cache::singleton().mutex() which currently protects
 
1280
      We perform all of the above under LOCK_open which currently protects
998
1281
      the open cache (also known as table cache) and table definitions stored
999
1282
      on disk.
1000
1283
    */
1001
1284
 
1002
1285
    {
1003
 
      table::Cache::singleton().mutex().lock(); /* Lock for FLUSH TABLES for open table */
 
1286
      LOCK_open.lock(); /* Lock for FLUSH TABLES for open table */
1004
1287
 
1005
1288
      /*
1006
1289
        Actually try to find the table in the open_cache.
1012
1295
        an implicit "pending locks queue" - see
1013
1296
        wait_for_locked_table_names for details.
1014
1297
      */
1015
 
      ppp= table::getCache().equal_range(key);
 
1298
      ppp= get_open_cache().equal_range(key);
1016
1299
 
1017
1300
      table= NULL;
1018
 
      for (table::CacheMap::const_iterator iter= ppp.first;
 
1301
      for (TableOpenCache::const_iterator iter= ppp.first;
1019
1302
           iter != ppp.second; ++iter, table= NULL)
1020
1303
      {
1021
1304
        table= (*iter).second;
1052
1335
          /* Avoid self-deadlocks by detecting self-dependencies. */
1053
1336
          if (table->open_placeholder && table->in_use == this)
1054
1337
          {
1055
 
            table::Cache::singleton().mutex().unlock();
1056
 
            my_error(ER_UPDATE_TABLE_USED, MYF(0), table->getShare()->getTableName());
 
1338
            LOCK_open.unlock();
 
1339
            my_error(ER_UPDATE_TABLE_USED, MYF(0), table->getMutableShare()->getTableName());
1057
1340
            return NULL;
1058
1341
          }
1059
1342
 
1085
1368
          */
1086
1369
          if (table->in_use != this)
1087
1370
          {
1088
 
            /* wait_for_conditionwill unlock table::Cache::singleton().mutex() for us */
1089
 
            wait_for_condition(table::Cache::singleton().mutex(), COND_refresh);
 
1371
            /* wait_for_conditionwill unlock LOCK_open for us */
 
1372
            wait_for_condition(LOCK_open, COND_refresh);
1090
1373
          }
1091
1374
          else
1092
1375
          {
1093
 
            table::Cache::singleton().mutex().unlock();
 
1376
            LOCK_open.unlock();
1094
1377
          }
1095
1378
          /*
1096
1379
            There is a refresh in progress for this table.
1103
1386
      }
1104
1387
      if (table)
1105
1388
      {
1106
 
        table::getUnused().unlink(static_cast<table::Concurrent *>(table));
 
1389
        unused_tables.unlink(table);
1107
1390
        table->in_use= this;
1108
1391
      }
1109
1392
      else
1111
1394
        /* Insert a new Table instance into the open cache */
1112
1395
        int error;
1113
1396
        /* Free cache if too big */
1114
 
        table::getUnused().cull();
 
1397
        unused_tables.cull();
1115
1398
 
1116
1399
        if (table_list->isCreate())
1117
1400
        {
1118
 
          TableIdentifier  lock_table_identifier(table_list->getSchemaName(), table_list->getTableName(), message::Table::STANDARD);
 
1401
          TableIdentifier  lock_table_identifier(table_list->db, table_list->table_name, message::Table::STANDARD);
1119
1402
 
1120
1403
          if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
1121
1404
          {
1122
1405
            /*
1123
1406
              Table to be created, so we need to create placeholder in table-cache.
1124
1407
            */
1125
 
            if (!(table= table_cache_insert_placeholder(lock_table_identifier)))
 
1408
            if (!(table= table_cache_insert_placeholder(table_list->db, table_list->table_name)))
1126
1409
            {
1127
 
              table::Cache::singleton().mutex().unlock();
 
1410
              LOCK_open.unlock();
1128
1411
              return NULL;
1129
1412
            }
1130
1413
            /*
1135
1418
            table->open_placeholder= true;
1136
1419
            table->setNext(open_tables);
1137
1420
            open_tables= table;
1138
 
            table::Cache::singleton().mutex().unlock();
 
1421
            LOCK_open.unlock();
1139
1422
 
1140
1423
            return table ;
1141
1424
          }
1143
1426
        }
1144
1427
 
1145
1428
        /* make a new table */
 
1429
        table= new Table;
 
1430
        if (table == NULL)
1146
1431
        {
1147
 
          table::Concurrent *new_table= new table::Concurrent;
1148
 
          table= new_table;
1149
 
          if (new_table == NULL)
1150
 
          {
1151
 
            table::Cache::singleton().mutex().unlock();
1152
 
            return NULL;
1153
 
          }
 
1432
          LOCK_open.unlock();
 
1433
          return NULL;
 
1434
        }
1154
1435
 
1155
 
          error= new_table->open_unireg_entry(this, alias, identifier);
1156
 
          if (error != 0)
1157
 
          {
1158
 
            delete new_table;
1159
 
            table::Cache::singleton().mutex().unlock();
1160
 
            return NULL;
1161
 
          }
1162
 
          (void)table::Cache::singleton().insert(new_table);
 
1436
        error= open_unireg_entry(this, table, alias, identifier);
 
1437
        if (error != 0)
 
1438
        {
 
1439
          delete table;
 
1440
          LOCK_open.unlock();
 
1441
          return NULL;
1163
1442
        }
 
1443
        (void)add_table(table);
1164
1444
      }
1165
1445
 
1166
 
      table::Cache::singleton().mutex().unlock();
 
1446
      LOCK_open.unlock();
1167
1447
    }
1168
1448
    if (refresh)
1169
1449
    {
1178
1458
  /* Fix alias if table name changes */
1179
1459
  if (strcmp(table->getAlias(), alias))
1180
1460
  {
1181
 
    table->setAlias(alias);
 
1461
    uint32_t length=(uint32_t) strlen(alias)+1;
 
1462
    table->alias= (char*) realloc((char*) table->alias, length);
 
1463
    memcpy((void*) table->alias, alias, length);
1182
1464
  }
1183
1465
 
1184
1466
  /* These variables are also set in reopen_table() */
1222
1504
  the strings are used in a loop even after the share may be freed.
1223
1505
*/
1224
1506
 
1225
 
void Session::close_data_files_and_morph_locks(const TableIdentifier &identifier)
 
1507
void Session::close_data_files_and_morph_locks(TableIdentifier &identifier)
1226
1508
{
1227
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle()); /* Adjust locks at the end of ALTER TABLEL */
 
1509
  safe_mutex_assert_owner(LOCK_open.native_handle()); /* Adjust locks at the end of ALTER TABLEL */
1228
1510
 
1229
1511
  if (lock)
1230
1512
  {
1232
1514
      If we are not under LOCK TABLES we should have only one table
1233
1515
      open and locked so it makes sense to remove the lock at once.
1234
1516
    */
1235
 
    unlockTables(lock);
 
1517
    mysql_unlock_tables(this, lock);
1236
1518
    lock= 0;
1237
1519
  }
1238
1520
 
1267
1549
  combination when one needs tables to be reopened (for
1268
1550
  example see openTablesLock()).
1269
1551
 
1270
 
  @note One should have lock on table::Cache::singleton().mutex() when calling this.
 
1552
  @note One should have lock on LOCK_open when calling this.
1271
1553
 
1272
1554
  @return false in case of success, true - otherwise.
1273
1555
*/
1284
1566
  if (open_tables == NULL)
1285
1567
    return false;
1286
1568
 
1287
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
 
1569
  safe_mutex_assert_owner(LOCK_open.native_handle());
1288
1570
  if (get_locks)
1289
1571
  {
1290
1572
    /*
1311
1593
    next= table->getNext();
1312
1594
 
1313
1595
    my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
1314
 
    table::remove_table(static_cast<table::Concurrent *>(table));
 
1596
    remove_table(table);
1315
1597
    error= 1;
1316
1598
  }
1317
1599
  *prev=0;
1320
1602
    DrizzleLock *local_lock;
1321
1603
    /*
1322
1604
      We should always get these locks. Anyway, we must not go into
1323
 
      wait_for_tables() as it tries to acquire table::Cache::singleton().mutex(), which is
 
1605
      wait_for_tables() as it tries to acquire LOCK_open, which is
1324
1606
      already locked.
1325
1607
    */
1326
1608
    some_tables_deleted= false;
1327
1609
 
1328
 
    if ((local_lock= lockTables(tables, (uint32_t) (tables_ptr - tables),
1329
 
                                       flags, &not_used)))
 
1610
    if ((local_lock= mysql_lock_tables(this, tables, (uint32_t) (tables_ptr - tables),
 
1611
                                 flags, &not_used)))
1330
1612
    {
1331
1613
      /* unused */
1332
1614
    }
1345
1627
  if (get_locks && tables)
1346
1628
    delete [] tables;
1347
1629
 
1348
 
  locking::broadcast_refresh();
 
1630
  broadcast_refresh();
1349
1631
 
1350
1632
  return(error);
1351
1633
}
1392
1674
              lock on it. This will also give them a chance to close their
1393
1675
              instances of this table.
1394
1676
            */
1395
 
            abortLock(ulcktbl);
1396
 
            removeLock(ulcktbl);
 
1677
            mysql_lock_abort(this, ulcktbl);
 
1678
            mysql_lock_remove(this, ulcktbl);
1397
1679
            ulcktbl->lock_count= 0;
1398
1680
          }
1399
1681
          if ((ulcktbl != table) && ulcktbl->db_stat)
1433
1715
    }
1434
1716
  }
1435
1717
  if (found)
1436
 
    locking::broadcast_refresh();
 
1718
    broadcast_refresh();
 
1719
}
 
1720
 
 
1721
 
 
1722
/*
 
1723
  Wait until all threads has closed the tables in the list
 
1724
  We have also to wait if there is thread that has a lock on this table even
 
1725
  if the table is closed
 
1726
*/
 
1727
 
 
1728
bool table_is_used(Table *table, bool wait_for_name_lock)
 
1729
{
 
1730
  do
 
1731
  {
 
1732
    const TableIdentifier::Key &key(table->getShare()->getCacheKey());
 
1733
 
 
1734
    TableOpenCacheRange ppp;
 
1735
    ppp= get_open_cache().equal_range(key);
 
1736
 
 
1737
    for (TableOpenCache::const_iterator iter= ppp.first;
 
1738
         iter != ppp.second; ++iter)
 
1739
    {
 
1740
      Table *search= (*iter).second;
 
1741
      if (search->in_use == table->in_use)
 
1742
        continue;                               // Name locked by this thread
 
1743
      /*
 
1744
        We can't use the table under any of the following conditions:
 
1745
        - There is an name lock on it (Table is to be deleted or altered)
 
1746
        - If we are in flush table and we didn't execute the flush
 
1747
        - If the table engine is open and it's an old version
 
1748
        (We must wait until all engines are shut down to use the table)
 
1749
      */
 
1750
      if ( (search->locked_by_name && wait_for_name_lock) ||
 
1751
           (search->is_name_opened() && search->needs_reopen_or_name_lock()))
 
1752
        return 1;
 
1753
    }
 
1754
  } while ((table=table->getNext()));
 
1755
  return 0;
1437
1756
}
1438
1757
 
1439
1758
 
1445
1764
 
1446
1765
  session->set_proc_info("Waiting for tables");
1447
1766
  {
1448
 
    boost_unique_lock_t lock(table::Cache::singleton().mutex());
1449
 
    while (not session->getKilled())
 
1767
    boost_unique_lock_t lock(LOCK_open);
 
1768
    while (!session->killed)
1450
1769
    {
1451
1770
      session->some_tables_deleted= false;
1452
1771
      session->close_old_data_files(false, dropping_tables != 0);
1453
 
      if (not table::Cache::singleton().areTablesUsed(session->open_tables, 1))
1454
 
      {
 
1772
      if (!table_is_used(session->open_tables, 1))
1455
1773
        break;
1456
 
      }
1457
1774
      COND_refresh.wait(lock);
1458
1775
    }
1459
 
    if (session->getKilled())
 
1776
    if (session->killed)
1460
1777
      result= true;                                     // aborted
1461
1778
    else
1462
1779
    {
1502
1819
  prev= &session->open_tables;
1503
1820
 
1504
1821
  /*
1505
 
    Note that we need to hold table::Cache::singleton().mutex() while changing the
 
1822
    Note that we need to hold LOCK_open while changing the
1506
1823
    open_tables list. Another thread may work on it.
1507
 
    (See: table::Cache::singleton().removeTable(), mysql_wait_completed_table())
 
1824
    (See: remove_table_from_cache(), mysql_wait_completed_table())
1508
1825
    Closing a MERGE child before the parent would be fatal if the
1509
1826
    other thread tries to abort the MERGE lock in between.
1510
1827
  */
1513
1830
    next=table->getNext();
1514
1831
    if (table->getShare()->getCacheKey() == identifier.getKey())
1515
1832
    {
1516
 
      session->removeLock(table);
 
1833
      mysql_lock_remove(session, table);
1517
1834
 
1518
1835
      if (!found)
1519
1836
      {
1528
1845
      else
1529
1846
      {
1530
1847
        /* We already have a name lock, remove copy */
1531
 
        table::remove_table(static_cast<table::Concurrent *>(table));
 
1848
        remove_table(table);
1532
1849
      }
1533
1850
    }
1534
1851
    else
1539
1856
  }
1540
1857
  *prev=0;
1541
1858
  if (found)
1542
 
    locking::broadcast_refresh();
 
1859
    broadcast_refresh();
1543
1860
 
1544
1861
  return(found);
1545
1862
}
1559
1876
    if (table->getShare()->getCacheKey() == identifier.getKey())
1560
1877
    {
1561
1878
      /* If MERGE child, forward lock handling to parent. */
1562
 
      session->abortLock(table);
 
1879
      mysql_lock_abort(session, table);
1563
1880
      break;
1564
1881
    }
1565
1882
  }
1566
1883
}
1567
1884
 
 
1885
/*
 
1886
  Load a table definition from cursor and open unireg table
 
1887
 
 
1888
  SYNOPSIS
 
1889
  open_unireg_entry()
 
1890
  session                       Thread handle
 
1891
  entry         Store open table definition here
 
1892
  table_list            TableList with db, table_name
 
1893
  alias         Alias name
 
1894
  cache_key             Key for share_cache
 
1895
  cache_key_length      length of cache_key
 
1896
 
 
1897
  NOTES
 
1898
  Extra argument for open is taken from session->open_options
 
1899
  One must have a lock on LOCK_open when calling this function
 
1900
 
 
1901
  RETURN
 
1902
  0     ok
 
1903
#       Error
 
1904
*/
 
1905
 
 
1906
static int open_unireg_entry(Session *session,
 
1907
                             Table *entry,
 
1908
                             const char *alias,
 
1909
                             TableIdentifier &identifier)
 
1910
{
 
1911
  int error;
 
1912
  TableSharePtr share;
 
1913
  uint32_t discover_retry_count= 0;
 
1914
 
 
1915
  safe_mutex_assert_owner(LOCK_open.native_handle());
 
1916
retry:
 
1917
  if (not (share= TableShare::getShareCreate(session,
 
1918
                                             identifier,
 
1919
                                             &error)))
 
1920
    return 1;
 
1921
 
 
1922
  while ((error= share->open_table_from_share(session,
 
1923
                                              identifier,
 
1924
                                              alias,
 
1925
                                              (uint32_t) (HA_OPEN_KEYFILE |
 
1926
                                                          HA_OPEN_RNDFILE |
 
1927
                                                          HA_GET_INDEX |
 
1928
                                                          HA_TRY_READ_ONLY),
 
1929
                                              session->open_options, *entry)))
 
1930
  {
 
1931
    if (error == 7)                             // Table def changed
 
1932
    {
 
1933
      share->resetVersion();                        // Mark share as old
 
1934
      if (discover_retry_count++)               // Retry once
 
1935
        goto err;
 
1936
 
 
1937
      /*
 
1938
        TODO->
 
1939
        Here we should wait until all threads has released the table.
 
1940
        For now we do one retry. This may cause a deadlock if there
 
1941
        is other threads waiting for other tables used by this thread.
 
1942
 
 
1943
        Proper fix would be to if the second retry failed:
 
1944
        - Mark that table def changed
 
1945
        - Return from open table
 
1946
        - Close all tables used by this thread
 
1947
        - Start waiting that the share is released
 
1948
        - Retry by opening all tables again
 
1949
      */
 
1950
 
 
1951
      /*
 
1952
        TO BE FIXED
 
1953
        To avoid deadlock, only wait for release if no one else is
 
1954
        using the share.
 
1955
      */
 
1956
      if (share->getTableCount() != 1)
 
1957
        goto err;
 
1958
      /* Free share and wait until it's released by all threads */
 
1959
      TableShare::release(share);
 
1960
 
 
1961
      if (!session->killed)
 
1962
      {
 
1963
        drizzle_reset_errors(session, 1);         // Clear warnings
 
1964
        session->clear_error();                 // Clear error message
 
1965
        goto retry;
 
1966
      }
 
1967
      return 1;
 
1968
    }
 
1969
 
 
1970
    goto err;
 
1971
  }
 
1972
 
 
1973
  return 0;
 
1974
 
 
1975
err:
 
1976
  TableShare::release(share);
 
1977
 
 
1978
  return 1;
 
1979
}
 
1980
 
1568
1981
 
1569
1982
/*
1570
1983
  Open all tables in list
1632
2045
     * to see if it exists so that an unauthorized user cannot phish for
1633
2046
     * table/schema information via error messages
1634
2047
     */
1635
 
    TableIdentifier the_table(tables->getSchemaName(), tables->getTableName());
 
2048
    TableIdentifier the_table(tables->db, tables->table_name);
1636
2049
    if (not plugin::Authorization::isAuthorized(getSecurityContext(),
1637
2050
                                                the_table))
1638
2051
    {
1742
2155
 
1743
2156
    assert(lock == 0);  // You must lock everything at once
1744
2157
    if ((table->reginfo.lock_type= lock_type) != TL_UNLOCK)
1745
 
      if (! (lock= lockTables(&table_list->table, 1, 0, &refresh)))
 
2158
      if (! (lock= mysql_lock_tables(this, &table_list->table, 1, 0, &refresh)))
1746
2159
        table= 0;
1747
2160
  }
1748
2161
 
1805
2218
      *(ptr++)= table->table;
1806
2219
  }
1807
2220
 
1808
 
  if (!(session->lock= session->lockTables(start, (uint32_t) (ptr - start), lock_flag, need_reopen)))
 
2221
  if (!(session->lock= mysql_lock_tables(session, start, (uint32_t) (ptr - start),
 
2222
                                         lock_flag, need_reopen)))
1809
2223
  {
1810
2224
    return -1;
1811
2225
  }
1834
2248
#  Table object
1835
2249
*/
1836
2250
 
1837
 
Table *Open_tables_state::open_temporary_table(const TableIdentifier &identifier,
1838
 
                                               bool link_in_list)
 
2251
Table *Session::open_temporary_table(TableIdentifier &identifier,
 
2252
                                     bool link_in_list)
1839
2253
{
 
2254
  TableShare *share;
 
2255
 
1840
2256
  assert(identifier.isTmp());
1841
 
 
1842
 
 
1843
 
  table::Temporary *new_tmp_table= new table::Temporary(identifier.getType(),
1844
 
                                                        identifier,
1845
 
                                                        const_cast<char *>(const_cast<TableIdentifier&>(identifier).getPath().c_str()),
1846
 
                                                        static_cast<uint32_t>(identifier.getPath().length()));
 
2257
  share= new TableShare(identifier.getType(),
 
2258
                        identifier,
 
2259
                        const_cast<char *>(identifier.getPath().c_str()), static_cast<uint32_t>(identifier.getPath().length()));
 
2260
 
 
2261
 
 
2262
  Table *new_tmp_table= new Table;
1847
2263
  if (not new_tmp_table)
1848
2264
    return NULL;
1849
2265
 
1850
2266
  /*
1851
2267
    First open the share, and then open the table from the share we just opened.
1852
2268
  */
1853
 
  if (new_tmp_table->getMutableShare()->open_table_def(*static_cast<Session *>(this), identifier) ||
1854
 
      new_tmp_table->getMutableShare()->open_table_from_share(static_cast<Session *>(this), identifier, identifier.getTableName().c_str(),
1855
 
                                                              (uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
1856
 
                                                                          HA_GET_INDEX),
1857
 
                                                              ha_open_options,
1858
 
                                                              *new_tmp_table))
 
2269
  if (share->open_table_def(*this, identifier) ||
 
2270
      share->open_table_from_share(this, identifier, identifier.getTableName().c_str(),
 
2271
                            (uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
 
2272
                                        HA_GET_INDEX),
 
2273
                            ha_open_options,
 
2274
                            *new_tmp_table))
1859
2275
  {
1860
2276
    /* No need to lock share->mutex as this is not needed for tmp tables */
1861
 
    delete new_tmp_table->getMutableShare();
 
2277
    delete share;
1862
2278
    delete new_tmp_table;
1863
2279
 
1864
2280
    return 0;
1919
2335
      current_bitmap= table->write_set;
1920
2336
    }
1921
2337
 
1922
 
    //if (current_bitmap->testAndSet(field->position()))
1923
 
    if (current_bitmap->test(field->position()))
 
2338
    //if (current_bitmap->testAndSet(field->field_index))
 
2339
    if (current_bitmap->test(field->field_index))
1924
2340
    {
1925
2341
      if (session->mark_used_columns == MARK_COLUMNS_WRITE)
1926
2342
        session->dup_field= field;
2154
2570
      */
2155
2571
      table_name && table_name[0] &&
2156
2572
      (my_strcasecmp(table_alias_charset, table_list->alias, table_name) ||
2157
 
       (db_name && db_name[0] && table_list->getSchemaName() && table_list->getSchemaName()[0] &&
2158
 
        strcmp(db_name, table_list->getSchemaName()))))
 
2573
       (db_name && db_name[0] && table_list->db && table_list->db[0] &&
 
2574
        strcmp(db_name, table_list->db))))
2159
2575
    return 0;
2160
2576
 
2161
2577
  *actual_table= NULL;
2230
2646
      {
2231
2647
        Table *table= field_to_set->getTable();
2232
2648
        if (session->mark_used_columns == MARK_COLUMNS_READ)
2233
 
          table->setReadSet(field_to_set->position());
 
2649
          table->setReadSet(field_to_set->field_index);
2234
2650
        else
2235
 
          table->setWriteSet(field_to_set->position());
 
2651
          table->setWriteSet(field_to_set->field_index);
2236
2652
      }
2237
2653
    }
2238
2654
  }
2788
3204
    /* true if field_name_1 is a member of using_fields */
2789
3205
    bool is_using_column_1;
2790
3206
    if (!(nj_col_1= it_1.get_or_create_column_ref(leaf_1)))
2791
 
      return(result);
 
3207
      goto err;
2792
3208
    field_name_1= nj_col_1->name();
2793
3209
    is_using_column_1= using_fields &&
2794
3210
      test_if_string_in_list(field_name_1, using_fields);
2806
3222
      Natural_join_column *cur_nj_col_2;
2807
3223
      const char *cur_field_name_2;
2808
3224
      if (!(cur_nj_col_2= it_2.get_or_create_column_ref(leaf_2)))
2809
 
        return(result);
 
3225
        goto err;
2810
3226
      cur_field_name_2= cur_nj_col_2->name();
2811
3227
 
2812
3228
      /*
2826
3242
            (found && (!using_fields || is_using_column_1)))
2827
3243
        {
2828
3244
          my_error(ER_NON_UNIQ_ERROR, MYF(0), field_name_1, session->where);
2829
 
          return(result);
 
3245
          goto err;
2830
3246
        }
2831
3247
        nj_col_2= cur_nj_col_2;
2832
3248
        found= true;
2859
3275
      Item_func_eq *eq_cond;
2860
3276
 
2861
3277
      if (!item_1 || !item_2)
2862
 
        return(result); // out of memory
 
3278
        goto err;                               // out of memory
2863
3279
 
2864
3280
      /*
2865
3281
        In the case of no_wrap_view_item == 0, the created items must be
2884
3300
      */
2885
3301
      if (set_new_item_local_context(session, item_ident_1, nj_col_1->table_ref) ||
2886
3302
          set_new_item_local_context(session, item_ident_2, nj_col_2->table_ref))
2887
 
        return(result);
 
3303
        goto err;
2888
3304
 
2889
3305
      if (!(eq_cond= new Item_func_eq(item_ident_1, item_ident_2)))
2890
 
        return(result);                               /* Out of memory. */
 
3306
        goto err;                               /* Out of memory. */
2891
3307
 
2892
3308
      /*
2893
3309
        Add the new equi-join condition to the ON clause. Notice that
2904
3320
      {
2905
3321
        Table *table_1= nj_col_1->table_ref->table;
2906
3322
        /* Mark field_1 used for table cache. */
2907
 
        table_1->setReadSet(field_1->position());
 
3323
        table_1->setReadSet(field_1->field_index);
2908
3324
        table_1->covering_keys&= field_1->part_of_key;
2909
3325
        table_1->merge_keys|= field_1->part_of_key;
2910
3326
      }
2912
3328
      {
2913
3329
        Table *table_2= nj_col_2->table_ref->table;
2914
3330
        /* Mark field_2 used for table cache. */
2915
 
        table_2->setReadSet(field_2->position());
 
3331
        table_2->setReadSet(field_2->field_index);
2916
3332
        table_2->covering_keys&= field_2->part_of_key;
2917
3333
        table_2->merge_keys|= field_2->part_of_key;
2918
3334
      }
2933
3349
  */
2934
3350
  result= false;
2935
3351
 
 
3352
err:
2936
3353
  return(result);
2937
3354
}
2938
3355
 
2990
3407
 
2991
3408
  if (!(non_join_columns= new List<Natural_join_column>) ||
2992
3409
      !(natural_using_join->join_columns= new List<Natural_join_column>))
2993
 
  {
2994
 
    return(result);
2995
 
  }
 
3410
    goto err;
2996
3411
 
2997
3412
  /* Append the columns of the first join operand. */
2998
3413
  for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next())
3031
3446
        {
3032
3447
          my_error(ER_BAD_FIELD_ERROR, MYF(0), using_field_name_ptr,
3033
3448
                   session->where);
3034
 
          return(result);
 
3449
          goto err;
3035
3450
        }
3036
3451
        if (!my_strcasecmp(system_charset_info,
3037
3452
                           common_field->name(), using_field_name_ptr))
3059
3474
 
3060
3475
  result= false;
3061
3476
 
 
3477
err:
3062
3478
  return(result);
3063
3479
}
3064
3480
 
3144
3560
      if (cur_table_ref->getNestedJoin() &&
3145
3561
          store_top_level_join_columns(session, cur_table_ref,
3146
3562
                                       real_left_neighbor, real_right_neighbor))
3147
 
        return(result);
 
3563
        goto err;
3148
3564
      same_level_right_neighbor= cur_table_ref;
3149
3565
    }
3150
3566
  }
3176
3592
      std::swap(table_ref_1, table_ref_2);
3177
3593
    if (mark_common_columns(session, table_ref_1, table_ref_2,
3178
3594
                            using_fields, &found_using_fields))
3179
 
      return(result);
 
3595
      goto err;
3180
3596
 
3181
3597
    /*
3182
3598
      Swap the join operands back, so that we pick the columns of the second
3188
3604
    if (store_natural_using_join_columns(session, table_ref, table_ref_1,
3189
3605
                                         table_ref_2, using_fields,
3190
3606
                                         found_using_fields))
3191
 
      return(result);
 
3607
      goto err;
3192
3608
 
3193
3609
    /*
3194
3610
      Change NATURAL JOIN to JOIN ... ON. We do this for both operands
3221
3637
  }
3222
3638
  result= false; /* All is OK. */
3223
3639
 
 
3640
err:
3224
3641
  return(result);
3225
3642
}
3226
3643
 
3622
4039
    assert(tables->is_leaf_for_name_resolution());
3623
4040
 
3624
4041
    if ((table_name && my_strcasecmp(table_alias_charset, table_name, tables->alias)) ||
3625
 
        (db_name && strcasecmp(tables->getSchemaName(),db_name)))
 
4042
        (db_name && strcasecmp(tables->db,db_name)))
3626
4043
      continue;
3627
4044
 
3628
4045
    /*
3658
4075
      if ((field= field_iterator.field()))
3659
4076
      {
3660
4077
        /* Mark fields as used to allow storage engine to optimze access */
3661
 
        field->getTable()->setReadSet(field->position());
 
4078
        field->getTable()->setReadSet(field->field_index);
3662
4079
        if (table)
3663
4080
        {
3664
4081
          table->covering_keys&= field->part_of_key;
3865
4282
    if ((value->save_in_field(rfield, 0) < 0) && !ignore_errors)
3866
4283
    {
3867
4284
      my_message(ER_UNKNOWN_ERROR, ER(ER_UNKNOWN_ERROR), MYF(0));
3868
 
      if (table)
3869
 
        table->auto_increment_field_not_null= false;
3870
 
 
3871
 
      return true;
 
4285
      goto err;
3872
4286
    }
3873
4287
  }
3874
4288
 
3875
4289
  return session->is_error();
 
4290
 
 
4291
err:
 
4292
  if (table)
 
4293
    table->auto_increment_field_not_null= false;
 
4294
 
 
4295
  return true;
3876
4296
}
3877
4297
 
3878
4298
 
3922
4342
    if (field == table->next_number_field)
3923
4343
      table->auto_increment_field_not_null= true;
3924
4344
    if (value->save_in_field(field, 0) < 0)
3925
 
    {
3926
 
      if (table)
3927
 
        table->auto_increment_field_not_null= false;
3928
 
 
3929
 
      return true;
3930
 
    }
 
4345
      goto err;
3931
4346
  }
3932
4347
 
3933
4348
  return(session->is_error());
 
4349
 
 
4350
err:
 
4351
  if (table)
 
4352
    table->auto_increment_field_not_null= false;
 
4353
 
 
4354
  return true;
3934
4355
}
3935
4356
 
3936
4357
 
3947
4368
 
3948
4369
  plugin::StorageEngine::removeLostTemporaryTables(*session, drizzle_tmpdir.c_str());
3949
4370
 
 
4371
  session->lockForDelete();
3950
4372
  delete session;
3951
4373
 
3952
4374
  return false;
3958
4380
  unireg support functions
3959
4381
 *****************************************************************************/
3960
4382
 
3961
 
 
 
4383
/*
 
4384
  Invalidate any cache entries that are for some DB
 
4385
 
 
4386
  SYNOPSIS
 
4387
  remove_db_from_cache()
 
4388
  db            Database name. This will be in lower case if
 
4389
  lower_case_table_name is set
 
4390
 
 
4391
NOTE:
 
4392
We can't use hash_delete when looping hash_elements. We mark them first
 
4393
and afterwards delete those marked unused.
 
4394
*/
 
4395
 
 
4396
void remove_db_from_cache(const SchemaIdentifier &schema_identifier)
 
4397
{
 
4398
  safe_mutex_assert_owner(LOCK_open.native_handle());
 
4399
 
 
4400
  for (TableOpenCache::const_iterator iter= get_open_cache().begin();
 
4401
       iter != get_open_cache().end();
 
4402
       iter++)
 
4403
  {
 
4404
    Table *table= (*iter).second;
 
4405
 
 
4406
    if (not schema_identifier.getPath().compare(table->getMutableShare()->getSchemaName()))
 
4407
    {
 
4408
      table->getMutableShare()->resetVersion();                 /* Free when thread is ready */
 
4409
      if (not table->in_use)
 
4410
        unused_tables.relink(table);
 
4411
    }
 
4412
  }
 
4413
 
 
4414
  unused_tables.cullByVersion();
 
4415
}
 
4416
 
 
4417
 
 
4418
/*
 
4419
  Mark all entries with the table as deleted to force an reopen of the table
 
4420
 
 
4421
  The table will be closed (not stored in cache) by the current thread when
 
4422
  close_thread_tables() is called.
 
4423
 
 
4424
  PREREQUISITES
 
4425
  Lock on LOCK_open()
 
4426
 
 
4427
  RETURN
 
4428
  0  This thread now have exclusive access to this table and no other thread
 
4429
  can access the table until close_thread_tables() is called.
 
4430
  1  Table is in use by another thread
 
4431
*/
 
4432
 
 
4433
bool remove_table_from_cache(Session *session, TableIdentifier &identifier, uint32_t flags)
 
4434
{
 
4435
  const TableIdentifier::Key &key(identifier.getKey());
 
4436
  bool result= false; 
 
4437
  bool signalled= false;
 
4438
 
 
4439
  for (;;)
 
4440
  {
 
4441
    result= signalled= false;
 
4442
 
 
4443
    TableOpenCacheRange ppp;
 
4444
    ppp= get_open_cache().equal_range(key);
 
4445
 
 
4446
    for (TableOpenCache::const_iterator iter= ppp.first;
 
4447
         iter != ppp.second; ++iter)
 
4448
    {
 
4449
      Table *table= (*iter).second;
 
4450
      Session *in_use;
 
4451
 
 
4452
      table->getMutableShare()->resetVersion();         /* Free when thread is ready */
 
4453
      if (!(in_use=table->in_use))
 
4454
      {
 
4455
        unused_tables.relink(table);
 
4456
      }
 
4457
      else if (in_use != session)
 
4458
      {
 
4459
        /*
 
4460
          Mark that table is going to be deleted from cache. This will
 
4461
          force threads that are in mysql_lock_tables() (but not yet
 
4462
          in thr_multi_lock()) to abort it's locks, close all tables and retry
 
4463
        */
 
4464
        in_use->some_tables_deleted= true;
 
4465
        if (table->is_name_opened())
 
4466
        {
 
4467
          result= true;
 
4468
        }
 
4469
        /*
 
4470
          Now we must abort all tables locks used by this thread
 
4471
          as the thread may be waiting to get a lock for another table.
 
4472
          Note that we need to hold LOCK_open while going through the
 
4473
          list. So that the other thread cannot change it. The other
 
4474
          thread must also hold LOCK_open whenever changing the
 
4475
          open_tables list. Aborting the MERGE lock after a child was
 
4476
          closed and before the parent is closed would be fatal.
 
4477
        */
 
4478
        for (Table *session_table= in_use->open_tables;
 
4479
             session_table ;
 
4480
             session_table= session_table->getNext())
 
4481
        {
 
4482
          /* Do not handle locks of MERGE children. */
 
4483
          if (session_table->db_stat)   // If table is open
 
4484
            signalled|= mysql_lock_abort_for_thread(session, session_table);
 
4485
        }
 
4486
      }
 
4487
      else
 
4488
        result= result || (flags & RTFC_OWNED_BY_Session_FLAG);
 
4489
    }
 
4490
 
 
4491
    unused_tables.cullByVersion();
 
4492
 
 
4493
    /* Remove table from table definition cache if it's not in use */
 
4494
    TableShare::release(identifier);
 
4495
 
 
4496
    if (result && (flags & RTFC_WAIT_OTHER_THREAD_FLAG))
 
4497
    {
 
4498
      /*
 
4499
        Signal any thread waiting for tables to be freed to
 
4500
        reopen their tables
 
4501
      */
 
4502
      broadcast_refresh();
 
4503
      if (!(flags & RTFC_CHECK_KILLED_FLAG) || !session->killed)
 
4504
      {
 
4505
        dropping_tables++;
 
4506
        if (likely(signalled))
 
4507
        {
 
4508
          boost_unique_lock_t scoped(LOCK_open, boost::adopt_lock_t());
 
4509
          COND_refresh.wait(scoped);
 
4510
          scoped.release();
 
4511
        }
 
4512
        else
 
4513
        {
 
4514
          /*
 
4515
            It can happen that another thread has opened the
 
4516
            table but has not yet locked any table at all. Since
 
4517
            it can be locked waiting for a table that our thread
 
4518
            has done LOCK Table x WRITE on previously, we need to
 
4519
            ensure that the thread actually hears our signal
 
4520
            before we go to sleep. Thus we wait for a short time
 
4521
            and then we retry another loop in the
 
4522
            remove_table_from_cache routine.
 
4523
          */
 
4524
          boost::xtime xt; 
 
4525
          xtime_get(&xt, boost::TIME_UTC); 
 
4526
          xt.sec += 10; 
 
4527
          boost_unique_lock_t scoped(LOCK_open, boost::adopt_lock_t());
 
4528
          COND_refresh.timed_wait(scoped, xt);
 
4529
          scoped.release();
 
4530
        }
 
4531
        dropping_tables--;
 
4532
        continue;
 
4533
      }
 
4534
    }
 
4535
    break;
 
4536
  }
 
4537
 
 
4538
  return result;
 
4539
}
3962
4540
 
3963
4541
 
3964
4542
/**