~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/definition/table.cc

merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
#include "drizzled/internal/my_pthread.h"
45
45
#include "drizzled/plugin/event_observer.h"
46
46
 
 
47
#include "drizzled/table.h"
 
48
#include "drizzled/table/shell.h"
 
49
 
47
50
#include "drizzled/session.h"
48
51
 
49
52
#include "drizzled/charset.h"
80
83
{
81
84
 
82
85
extern size_t table_def_size;
83
 
static TableDefinitionCache table_def_cache;
84
86
 
85
87
/*****************************************************************************
86
88
  Functions to handle table definition cach (TableShare)
87
89
 *****************************************************************************/
88
90
 
89
 
 
90
 
// @todo switch this a boost::thread one only call.
91
 
void TableShare::cacheStart(void)
92
 
{
93
 
  /* 
94
 
   * This is going to overalloc a bit - as rehash sets the number of
95
 
   * buckets, not the number of elements. BUT, it'll allow us to not need
96
 
   * to rehash later on as the unordered_map grows.
97
 
 */
98
 
  table_def_cache.rehash(table_def_size);
99
 
}
100
 
 
101
 
 
102
 
/**
103
 
 * @TODO This should return size_t
104
 
 */
105
 
uint32_t cached_table_definitions(void)
106
 
{
107
 
  return static_cast<uint32_t>(table_def_cache.size());
108
 
}
109
 
 
110
 
 
111
91
/*
112
92
  Mark that we are not using table share anymore.
113
93
 
136
116
    TableIdentifier identifier(share->getSchemaName(), share->getTableName());
137
117
    plugin::EventObserver::deregisterTableEvents(*share);
138
118
   
139
 
    TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
140
 
    if (iter != table_def_cache.end())
141
 
    {
142
 
      table_def_cache.erase(iter);
143
 
    }
 
119
    definition::Cache::singleton().erase(identifier);
144
120
    return;
145
121
  }
146
122
  share->unlock();
162
138
    TableIdentifier identifier(share->getSchemaName(), share->getTableName());
163
139
    plugin::EventObserver::deregisterTableEvents(*share);
164
140
   
165
 
    TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
166
 
    if (iter != table_def_cache.end())
167
 
    {
168
 
      table_def_cache.erase(iter);
169
 
    }
 
141
    definition::Cache::singleton().erase(identifier);
170
142
    return;
171
143
  }
172
144
  share->unlock();
174
146
 
175
147
void TableShare::release(TableIdentifier &identifier)
176
148
{
177
 
  TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
178
 
  if (iter != table_def_cache.end())
 
149
  TableSharePtr share= definition::Cache::singleton().find(identifier);
 
150
  if (share)
179
151
  {
180
 
    TableSharePtr share= (*iter).second;
181
152
    share->version= 0;                          // Mark for delete
182
153
    if (share->ref_count == 0)
183
154
    {
184
155
      share->lock();
185
156
      plugin::EventObserver::deregisterTableEvents(*share);
186
 
      table_def_cache.erase(identifier.getKey());
 
157
      definition::Cache::singleton().erase(identifier);
187
158
    }
188
159
  }
189
160
}
235
206
 
236
207
TableSharePtr TableShare::getShareCreate(Session *session, 
237
208
                                         TableIdentifier &identifier,
238
 
                                         int *error)
 
209
                                         int *in_error)
239
210
{
240
211
  TableSharePtr share;
241
212
 
242
 
  *error= 0;
 
213
  *in_error= 0;
243
214
 
244
215
  /* Read table definition from cache */
245
 
  TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
246
 
  if (iter != table_def_cache.end())
247
 
  {
248
 
    share= (*iter).second;
 
216
  if ((share= definition::Cache::singleton().find(identifier)))
249
217
    return foundTableShare(share);
250
 
  }
251
218
 
252
219
  share.reset(new TableShare(message::Table::STANDARD, identifier));
253
220
  
257
224
  */
258
225
  share->lock();
259
226
 
260
 
  pair<TableDefinitionCache::iterator, bool> ret=
261
 
    table_def_cache.insert(make_pair(identifier.getKey(), share));
262
 
  if (ret.second == false)
263
 
  {
 
227
  bool ret= definition::Cache::singleton().insert(identifier, share);
 
228
 
 
229
  if (not ret)
264
230
    return TableSharePtr();
265
 
  }
266
231
 
267
232
  if (share->open_table_def(*session, identifier))
268
233
  {
269
 
    *error= share->error;
270
 
    table_def_cache.erase(identifier.getKey());
 
234
    *in_error= share->error;
 
235
    definition::Cache::singleton().erase(identifier);
271
236
 
272
237
    return TableSharePtr();
273
238
  }
297
262
{
298
263
  safe_mutex_assert_owner(LOCK_open.native_handle);
299
264
 
300
 
  TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
301
 
  if (iter != table_def_cache.end())
302
 
  {
303
 
    return (*iter).second;
304
 
  }
305
 
 
306
 
  return TableSharePtr();
 
265
  return definition::Cache::singleton().find(identifier);
307
266
}
308
267
 
309
268
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
441
400
  return false;
442
401
}
443
402
 
444
 
const TableDefinitionCache &TableShare::getCache()
445
 
{
446
 
  return table_def_cache;
447
 
}
448
 
 
449
403
TableShare::TableShare(TableIdentifier::Type type_arg) :
450
404
  table_category(TABLE_UNKNOWN_CATEGORY),
451
 
  open_count(0),
452
405
  found_next_number_field(NULL),
453
406
  timestamp_field(NULL),
454
407
  key_info(NULL),
507
460
 
508
461
  if (type_arg == message::Table::INTERNAL)
509
462
  {
510
 
    TableIdentifier::build_tmptable_filename(private_key_for_cache);
511
 
    init(&private_key_for_cache[0], &private_key_for_cache[0]);
 
463
    TableIdentifier::build_tmptable_filename(private_key_for_cache.vectorPtr());
 
464
    init(private_key_for_cache.vector(), private_key_for_cache.vector());
512
465
  }
513
466
  else
514
467
  {
518
471
 
519
472
TableShare::TableShare(TableIdentifier &identifier, const TableIdentifier::Key &key) :// Used by placeholder
520
473
  table_category(TABLE_UNKNOWN_CATEGORY),
521
 
  open_count(0),
522
474
  found_next_number_field(NULL),
523
475
  timestamp_field(NULL),
524
476
  key_info(NULL),
579
531
  table_category=         TABLE_CATEGORY_TEMPORARY;
580
532
  tmp_table=              message::Table::INTERNAL;
581
533
 
582
 
  db.str= &private_key_for_cache[0];
583
 
  db.length= strlen(&private_key_for_cache[0]);
 
534
  db.str= const_cast<char *>(private_key_for_cache.vector());
 
535
  db.length= strlen(private_key_for_cache.vector());
584
536
 
585
 
  table_name.str= &private_key_for_cache[0] + strlen(&private_key_for_cache[0]) + 1;
 
537
  table_name.str= const_cast<char *>(private_key_for_cache.vector()) + strlen(private_key_for_cache.vector()) + 1;
586
538
  table_name.length= strlen(table_name.str);
587
539
  path.str= (char *)"";
588
540
  normalized_path.str= path.str;
594
546
 
595
547
TableShare::TableShare(const TableIdentifier &identifier) : // Just used during createTable()
596
548
  table_category(TABLE_UNKNOWN_CATEGORY),
597
 
  open_count(0),
598
549
  found_next_number_field(NULL),
599
550
  timestamp_field(NULL),
600
551
  key_info(NULL),
658
609
  {
659
610
    table_category=         TABLE_CATEGORY_TEMPORARY;
660
611
    tmp_table=              message::Table::INTERNAL;
661
 
    db.str= &private_key_for_cache[0];
662
 
    db.length= strlen(&private_key_for_cache[0]);
 
612
    db.str= const_cast<char *>(private_key_for_cache.vector());
 
613
    db.length= strlen(private_key_for_cache.vector());
663
614
    table_name.str= db.str + 1;
664
615
    table_name.length= strlen(table_name.str);
665
616
    path.str= &private_normalized_path[0];
677
628
                       char *path_arg,
678
629
                       uint32_t path_length_arg) :
679
630
  table_category(TABLE_UNKNOWN_CATEGORY),
680
 
  open_count(0),
681
631
  found_next_number_field(NULL),
682
632
  timestamp_field(NULL),
683
633
  key_info(NULL),
741
691
    Let us use the fact that the key is "db/0/table_name/0" + optional
742
692
    part for temporary tables.
743
693
  */
744
 
  db.str= &private_key_for_cache[0];
 
694
  db.str= const_cast<char *>(private_key_for_cache.vector());
745
695
  db.length=         strlen(db.str);
746
696
  table_name.str=    db.str + db.length + 1;
747
697
  table_name.length= strlen(table_name.str);
816
766
 
817
767
void TableShare::setIdentifier(TableIdentifier &identifier_arg)
818
768
{
819
 
  private_key_for_cache.clear();
820
769
  private_key_for_cache= identifier_arg.getKey();
821
770
 
822
771
  /*
823
772
    Let us use the fact that the key is "db/0/table_name/0" + optional
824
773
    part for temporary tables.
825
774
  */
826
 
  db.str= &private_key_for_cache[0];
 
775
  db.str= const_cast<char *>(private_key_for_cache.vector());
827
776
  db.length=         strlen(db.str);
828
777
  table_name.str=    db.str + db.length + 1;
829
778
  table_name.length= strlen(table_name.str);
1444
1393
 
1445
1394
    // This needs to go, we should be setting the "use" on the field so that
1446
1395
    // it does not reference the share/table.
1447
 
    Table temp_table; /* Use this so that BLOB DEFAULT '' works */
1448
 
    temp_table.setShare(this);
 
1396
    table::Shell temp_table(*this); /* Use this so that BLOB DEFAULT '' works */
1449
1397
    temp_table.in_use= &session;
1450
1398
 
1451
1399
    f->init(&temp_table); /* blob default values need table obj */
1744
1692
 
1745
1693
  NOTES
1746
1694
  This function is called when the table definition is not cached in
1747
 
  table_def_cache
 
1695
  definition::Cache::singleton().getCache()
1748
1696
  The data is returned in 'share', which is alloced by
1749
1697
  alloc_table_share().. The code assumes that share is initialized.
1750
1698
 
1825
1773
  5    Error (see open_table_error: charset unavailable)
1826
1774
  7    Table definition has changed in engine
1827
1775
*/
1828
 
 
1829
1776
int TableShare::open_table_from_share(Session *session,
1830
1777
                                      const TableIdentifier &identifier,
1831
1778
                                      const char *alias,
1832
1779
                                      uint32_t db_stat, uint32_t ha_open_flags,
1833
1780
                                      Table &outparam)
1834
1781
{
 
1782
  bool error_reported= false;
 
1783
  int ret= open_table_from_share_inner(session, alias, db_stat, outparam);
 
1784
 
 
1785
  if (not ret)
 
1786
    ret= open_table_cursor_inner(identifier, db_stat, ha_open_flags, outparam, error_reported);
 
1787
 
 
1788
  if (not ret)
 
1789
    return ret;
 
1790
 
 
1791
  if (not error_reported)
 
1792
    open_table_error(ret, errno, 0);
 
1793
 
 
1794
  delete outparam.cursor;
 
1795
  outparam.cursor= 0;                           // For easier error checking
 
1796
  outparam.db_stat= 0;
 
1797
  outparam.getMemRoot()->free_root(MYF(0));       // Safe to call on zeroed root
 
1798
  outparam.clearAlias();
 
1799
 
 
1800
  return ret;
 
1801
}
 
1802
 
 
1803
int TableShare::open_table_from_share_inner(Session *session,
 
1804
                                            const char *alias,
 
1805
                                            uint32_t db_stat,
 
1806
                                            Table &outparam)
 
1807
{
1835
1808
  int local_error;
1836
1809
  uint32_t records;
1837
 
  bool error_reported= false;
1838
1810
  unsigned char *record= NULL;
1839
1811
  Field **field_ptr;
1840
1812
 
1844
1816
  local_error= 1;
1845
1817
  outparam.resetTable(session, this, db_stat);
1846
1818
 
1847
 
  if (not (outparam.alias= strdup(alias)))
1848
 
    goto err;
 
1819
  outparam.setAlias(alias);
1849
1820
 
1850
1821
  /* Allocate Cursor */
1851
 
  if (not (outparam.cursor= db_type()->getCursor(*this)))
1852
 
    goto err;
 
1822
  if (not (outparam.cursor= db_type()->getCursor(outparam)))
 
1823
    return local_error;
1853
1824
 
1854
1825
  local_error= 4;
1855
1826
  records= 0;
1859
1830
  records++;
1860
1831
 
1861
1832
  if (!(record= (unsigned char*) outparam.alloc_root(rec_buff_length * records)))
1862
 
    goto err;
 
1833
    return local_error;
1863
1834
 
1864
1835
  if (records == 0)
1865
1836
  {
1875
1846
      outparam.record[1]= outparam.getInsertRecord();   // Safety
1876
1847
  }
1877
1848
 
1878
 
#ifdef HAVE_purify
 
1849
#ifdef HAVE_VALGRIND
1879
1850
  /*
1880
1851
    We need this because when we read var-length rows, we are not updating
1881
1852
    bytes after end of varchar
1895
1866
 
1896
1867
  if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((fields+1)* sizeof(Field*)))))
1897
1868
  {
1898
 
    goto err;
 
1869
    return local_error;
1899
1870
  }
1900
1871
 
1901
1872
  outparam.setFields(field_ptr);
1908
1879
  for (uint32_t i= 0 ; i < fields; i++, field_ptr++)
1909
1880
  {
1910
1881
    if (!((*field_ptr)= field[i]->clone(outparam.getMemRoot(), &outparam)))
1911
 
      goto err;
 
1882
      return local_error;
1912
1883
  }
1913
1884
  (*field_ptr)= 0;                              // End marker
1914
1885
 
1927
1898
    uint32_t n_length;
1928
1899
    n_length= keys*sizeof(KeyInfo) + key_parts*sizeof(KeyPartInfo);
1929
1900
    if (!(local_key_info= (KeyInfo*) outparam.alloc_root(n_length)))
1930
 
      goto err;
 
1901
      return local_error;
1931
1902
    outparam.key_info= local_key_info;
1932
1903
    key_part= (reinterpret_cast<KeyPartInfo*> (local_key_info+keys));
1933
1904
 
1971
1942
  outparam.tmp_set.resize(fields);
1972
1943
  outparam.default_column_bitmaps();
1973
1944
 
 
1945
  return 0;
 
1946
}
 
1947
 
 
1948
int TableShare::open_table_cursor_inner(const TableIdentifier &identifier,
 
1949
                                        uint32_t db_stat, uint32_t ha_open_flags,
 
1950
                                        Table &outparam,
 
1951
                                        bool &error_reported)
 
1952
{
1974
1953
  /* The table struct is now initialized;  Open the table */
1975
 
  local_error= 2;
 
1954
  int local_error= 2;
1976
1955
  if (db_stat)
1977
1956
  {
1978
1957
    assert(!(db_stat & HA_WAIT_IF_LOCKED));
1979
1958
    int ha_err;
1980
1959
 
1981
 
    if ((ha_err= (outparam.cursor->ha_open(identifier, &outparam,
1982
 
                          (db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
1983
 
                          (db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE : HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
 
1960
    if ((ha_err= (outparam.cursor->ha_open(identifier,
 
1961
                                           (db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
 
1962
                                           (db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE : HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
1984
1963
    {
1985
1964
      switch (ha_err)
1986
1965
      {
2007
1986
          local_error= 7;
2008
1987
        break;
2009
1988
      }
2010
 
      goto err;
 
1989
      return local_error;
2011
1990
    }
2012
1991
  }
2013
1992
 
2014
1993
  return 0;
2015
 
 
2016
 
err:
2017
 
  if (!error_reported)
2018
 
    open_table_error(local_error, errno, 0);
2019
 
 
2020
 
  delete outparam.cursor;
2021
 
  outparam.cursor= 0;                           // For easier error checking
2022
 
  outparam.db_stat= 0;
2023
 
  outparam.getMemRoot()->free_root(MYF(0));       // Safe to call on zeroed root
2024
 
  free((char*) outparam.alias);
2025
 
 
2026
 
  return (local_error);
2027
1994
}
2028
1995
 
2029
1996
/* error message when opening a form cursor */