44
44
#include "drizzled/internal/my_pthread.h"
45
45
#include "drizzled/plugin/event_observer.h"
47
#include "drizzled/table.h"
48
#include "drizzled/table/shell.h"
50
47
#include "drizzled/session.h"
52
49
#include "drizzled/charset.h"
70
67
#include "drizzled/field/decimal.h"
71
68
#include "drizzled/field/real.h"
72
69
#include "drizzled/field/double.h"
73
#include "drizzled/field/int32.h"
74
#include "drizzled/field/int64.h"
70
#include "drizzled/field/long.h"
71
#include "drizzled/field/int64_t.h"
75
72
#include "drizzled/field/num.h"
76
73
#include "drizzled/field/timestamp.h"
77
74
#include "drizzled/field/datetime.h"
78
75
#include "drizzled/field/varstring.h"
79
#include "drizzled/field/uuid.h"
81
#include "drizzled/definition/cache.h"
83
77
using namespace std;
88
82
extern size_t table_def_size;
83
static TableDefinitionCache table_def_cache;
90
85
/*****************************************************************************
91
86
Functions to handle table definition cach (TableShare)
92
87
*****************************************************************************/
90
// @todo switch this a boost::thread one only call.
91
void TableShare::cacheStart(void)
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.
98
table_def_cache.rehash(table_def_size);
103
* @TODO This should return size_t
105
uint32_t cached_table_definitions(void)
107
return static_cast<uint32_t>(table_def_cache.size());
95
112
Mark that we are not using table share anymore.
106
123
void TableShare::release(TableShare *share)
108
125
bool to_be_deleted= false;
109
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
126
safe_mutex_assert_owner(LOCK_open.native_handle);
112
129
if (!--share->ref_count)
114
131
to_be_deleted= true;
136
TableIdentifier identifier(share->getSchemaName(), share->getTableName());
137
plugin::EventObserver::deregisterTableEvents(*share);
139
TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
140
if (iter != table_def_cache.end())
142
table_def_cache.erase(iter);
120
definition::Cache::singleton().erase(share->getCacheKey());
124
void TableShare::release(TableShare::shared_ptr &share)
149
void TableShare::release(TableSharePtr &share)
126
151
bool to_be_deleted= false;
127
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
152
safe_mutex_assert_owner(LOCK_open.native_handle);
130
155
if (!--share->ref_count)
132
157
to_be_deleted= true;
162
TableIdentifier identifier(share->getSchemaName(), share->getTableName());
163
plugin::EventObserver::deregisterTableEvents(*share);
165
TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
166
if (iter != table_def_cache.end())
168
table_def_cache.erase(iter);
138
definition::Cache::singleton().erase(share->getCacheKey());
142
void TableShare::release(const TableIdentifier &identifier)
175
void TableShare::release(TableIdentifier &identifier)
144
TableShare::shared_ptr share= definition::Cache::singleton().find(identifier.getKey());
177
TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
178
if (iter != table_def_cache.end())
180
TableSharePtr share= (*iter).second;
147
181
share->version= 0; // Mark for delete
148
182
if (share->ref_count == 0)
150
definition::Cache::singleton().erase(identifier.getKey());
185
plugin::EventObserver::deregisterTableEvents(*share);
186
table_def_cache.erase(identifier.getKey());
156
static TableShare::shared_ptr foundTableShare(TableShare::shared_ptr share)
192
static TableSharePtr foundTableShare(TableSharePtr share)
159
195
We found an existing table definition. Return it if we didn't get
166
202
/* Table definition contained an error */
167
203
share->open_table_error(share->error, share->open_errno, share->errarg);
169
return TableShare::shared_ptr();
205
return TableSharePtr();
172
208
share->incrementTableCount();
197
233
# Share for table
200
TableShare::shared_ptr TableShare::getShareCreate(Session *session,
201
const TableIdentifier &identifier,
236
TableSharePtr TableShare::getShareCreate(Session *session,
237
TableIdentifier &identifier,
204
TableShare::shared_ptr share;
208
244
/* Read table definition from cache */
209
if ((share= definition::Cache::singleton().find(identifier.getKey())))
245
TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
246
if (iter != table_def_cache.end())
248
share= (*iter).second;
210
249
return foundTableShare(share);
212
252
share.reset(new TableShare(message::Table::STANDARD, identifier));
255
Lock mutex to be able to read table definition from file without
260
pair<TableDefinitionCache::iterator, bool> ret=
261
table_def_cache.insert(make_pair(identifier.getKey(), share));
262
if (ret.second == false)
264
return TableSharePtr();
214
267
if (share->open_table_def(*session, identifier))
216
in_error= share->error;
269
*error= share->error;
270
table_def_cache.erase(identifier.getKey());
218
return TableShare::shared_ptr();
272
return TableSharePtr();
220
274
share->ref_count++; // Mark in use
222
276
plugin::EventObserver::registerTableEvents(*share);
224
bool ret= definition::Cache::singleton().insert(identifier.getKey(), share);
227
return TableShare::shared_ptr();
285
Check if table definition exits in cache
288
get_cached_table_share()
290
table_name Table name
294
# TableShare for table
296
TableSharePtr TableShare::getShare(TableIdentifier &identifier)
298
safe_mutex_assert_owner(LOCK_open.native_handle);
300
TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
301
if (iter != table_def_cache.end())
303
return (*iter).second;
306
return TableSharePtr();
232
309
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
234
311
enum_field_types field_type;
265
342
case message::Table::Field::BLOB:
266
343
field_type= DRIZZLE_TYPE_BLOB;
268
case message::Table::Field::UUID:
269
field_type= DRIZZLE_TYPE_UUID;
273
abort(); // Programming error
346
field_type= DRIZZLE_TYPE_LONG; /* Set value to kill GCC warning */
276
350
return field_type;
304
378
default_value->length());
306
380
case DRIZZLE_TYPE_NULL:
309
382
case DRIZZLE_TYPE_TIMESTAMP:
310
383
case DRIZZLE_TYPE_DATETIME:
311
384
case DRIZZLE_TYPE_DATE:
312
385
case DRIZZLE_TYPE_ENUM:
313
case DRIZZLE_TYPE_UUID:
314
386
default_item= new Item_string(default_value->c_str(),
315
387
default_value->length(),
316
388
system_charset_info);
372
TableShare::TableShare(const TableIdentifier::Type type_arg) :
444
const TableDefinitionCache &TableShare::getCache()
446
return table_def_cache;
449
TableShare::TableShare(TableIdentifier::Type type_arg) :
373
450
table_category(TABLE_UNKNOWN_CATEGORY),
374
452
found_next_number_field(NULL),
375
453
timestamp_field(NULL),
426
508
if (type_arg == message::Table::INTERNAL)
428
TableIdentifier::build_tmptable_filename(private_key_for_cache.vectorPtr());
429
init(private_key_for_cache.vector(), private_key_for_cache.vector());
510
TableIdentifier::build_tmptable_filename(private_key_for_cache);
511
init(&private_key_for_cache[0], &private_key_for_cache[0]);
437
TableShare::TableShare(const TableIdentifier &identifier, const TableIdentifier::Key &key) :// Used by placeholder
519
TableShare::TableShare(TableIdentifier &identifier, const TableIdentifier::Key &key) :// Used by placeholder
438
520
table_category(TABLE_UNKNOWN_CATEGORY),
439
522
found_next_number_field(NULL),
440
523
timestamp_field(NULL),
492
579
table_category= TABLE_CATEGORY_TEMPORARY;
493
580
tmp_table= message::Table::INTERNAL;
495
db.str= const_cast<char *>(private_key_for_cache.vector());
496
db.length= strlen(private_key_for_cache.vector());
582
db.str= &private_key_for_cache[0];
583
db.length= strlen(&private_key_for_cache[0]);
498
table_name.str= const_cast<char *>(private_key_for_cache.vector()) + strlen(private_key_for_cache.vector()) + 1;
585
table_name.str= &private_key_for_cache[0] + strlen(&private_key_for_cache[0]) + 1;
499
586
table_name.length= strlen(table_name.str);
500
587
path.str= (char *)"";
501
588
normalized_path.str= path.str;
502
589
path.length= normalized_path.length= 0;
504
std::string tb_name(identifier.getTableName());
505
std::transform(tb_name.begin(), tb_name.end(), tb_name.begin(), ::tolower);
506
assert(strcmp(tb_name.c_str(), table_name.str) == 0);
590
assert(strcmp(identifier.getTableName().c_str(), table_name.str) == 0);
508
591
assert(strcmp(identifier.getSchemaName().c_str(), db.str) == 0);
512
595
TableShare::TableShare(const TableIdentifier &identifier) : // Just used during createTable()
513
596
table_category(TABLE_UNKNOWN_CATEGORY),
514
598
found_next_number_field(NULL),
515
599
timestamp_field(NULL),
571
659
table_category= TABLE_CATEGORY_TEMPORARY;
572
660
tmp_table= message::Table::INTERNAL;
573
db.str= const_cast<char *>(private_key_for_cache.vector());
574
db.length= strlen(private_key_for_cache.vector());
661
db.str= &private_key_for_cache[0];
662
db.length= strlen(&private_key_for_cache[0]);
575
663
table_name.str= db.str + 1;
576
664
table_name.length= strlen(table_name.str);
577
665
path.str= &private_normalized_path[0];
585
673
Used for shares that will go into the cache.
587
TableShare::TableShare(const TableIdentifier::Type type_arg,
588
const TableIdentifier &identifier,
675
TableShare::TableShare(TableIdentifier::Type type_arg,
676
TableIdentifier &identifier,
590
678
uint32_t path_length_arg) :
591
679
table_category(TABLE_UNKNOWN_CATEGORY),
592
681
found_next_number_field(NULL),
593
682
timestamp_field(NULL),
648
741
Let us use the fact that the key is "db/0/table_name/0" + optional
649
742
part for temporary tables.
651
db.str= const_cast<char *>(private_key_for_cache.vector());
744
db.str= &private_key_for_cache[0];
652
745
db.length= strlen(db.str);
653
746
table_name.str= db.str + db.length + 1;
654
747
table_name.length= strlen(table_name.str);
697
789
assert(ref_count == 0);
792
If someone is waiting for this to be deleted, inform it about this.
793
Don't do a delete until we know that no one is refering to this anymore.
795
if (tmp_table == message::Table::STANDARD)
797
/* share->mutex is locked in release_table_share() */
798
while (waiting_on_cond)
801
boost::mutex::scoped_lock scoped(mutex, boost::adopt_lock_t());
805
/* No thread refers to this anymore */
699
809
storage_engine= NULL;
701
811
delete table_proto;
702
812
table_proto= NULL;
704
plugin::EventObserver::deregisterTableEvents(*this);
706
814
mem_root.free_root(MYF(0)); // Free's share
709
void TableShare::setIdentifier(const TableIdentifier &identifier_arg)
817
void TableShare::setIdentifier(TableIdentifier &identifier_arg)
819
private_key_for_cache.clear();
711
820
private_key_for_cache= identifier_arg.getKey();
714
823
Let us use the fact that the key is "db/0/table_name/0" + optional
715
824
part for temporary tables.
717
db.str= const_cast<char *>(private_key_for_cache.vector());
826
db.str= &private_key_for_cache[0];
718
827
db.length= strlen(db.str);
719
828
table_name.str= db.str + db.length + 1;
720
829
table_name.length= strlen(table_name.str);
934
1043
uint32_t local_null_fields= 0;
937
std::vector<uint32_t> field_offsets;
938
std::vector<uint32_t> field_pack_length;
1046
vector<uint32_t> field_offsets;
1047
vector<uint32_t> field_pack_length;
940
1049
field_offsets.resize(fields);
941
1050
field_pack_length.resize(fields);
1135
1244
unireg_type= Field::TIMESTAMP_DN_FIELD;
1139
assert(0); // Invalid update value.
1247
assert(1); // Invalid update value.
1143
1249
else if (pfield.has_options() &&
1144
1250
pfield.options().has_update_expression() &&
1315
1422
case DRIZZLE_TYPE_LONGLONG:
1316
1423
field_length= MAX_BIGINT_WIDTH;
1318
case DRIZZLE_TYPE_UUID:
1319
field_length= field::Uuid::max_string_length();
1321
1425
case DRIZZLE_TYPE_NULL:
1322
1426
abort(); // Programming error
1325
assert(enum_field_types_size == 12);
1327
1429
Field* f= make_field(record + field_offsets[fieldnr] + data_offset,
1329
pfield.constraints().is_nullable(),
1335
MTYP_TYPENR(unireg_type),
1336
((field_type == DRIZZLE_TYPE_ENUM) ? &intervals[interval_nr++] : (TYPELIB*) 0),
1337
getTableProto()->field(fieldnr).name().c_str());
1431
pfield.constraints().is_nullable(),
1437
(Field::utype) MTYP_TYPENR(unireg_type),
1438
((field_type == DRIZZLE_TYPE_ENUM) ?
1439
&intervals[interval_nr++]
1441
getTableProto()->field(fieldnr).name().c_str());
1339
1443
field[fieldnr]= f;
1341
// Insert post make_field code here.
1344
case DRIZZLE_TYPE_BLOB:
1345
case DRIZZLE_TYPE_VARCHAR:
1346
case DRIZZLE_TYPE_DOUBLE:
1347
case DRIZZLE_TYPE_DECIMAL:
1348
case DRIZZLE_TYPE_TIMESTAMP:
1349
case DRIZZLE_TYPE_DATETIME:
1350
case DRIZZLE_TYPE_DATE:
1351
case DRIZZLE_TYPE_ENUM:
1352
case DRIZZLE_TYPE_LONG:
1353
case DRIZZLE_TYPE_LONGLONG:
1354
case DRIZZLE_TYPE_NULL:
1355
case DRIZZLE_TYPE_UUID:
1359
1445
// This needs to go, we should be setting the "use" on the field so that
1360
1446
// it does not reference the share/table.
1361
table::Shell temp_table(*this); /* Use this so that BLOB DEFAULT '' works */
1447
Table temp_table; /* Use this so that BLOB DEFAULT '' works */
1448
temp_table.setShare(this);
1362
1449
temp_table.in_use= &session;
1364
1451
f->init(&temp_table); /* blob default values need table obj */
1385
1472
return local_error;
1388
else if (f->real_type() == DRIZZLE_TYPE_ENUM && (f->flags & NOT_NULL_FLAG))
1475
else if (f->real_type() == DRIZZLE_TYPE_ENUM &&
1476
(f->flags & NOT_NULL_FLAG))
1390
1478
f->set_notnull();
1391
1479
f->store((int64_t) 1, true);
1412
1500
if (f->unireg_check == Field::NEXT_NUMBER)
1413
1501
found_next_number_field= &(field[fieldnr]);
1503
if (timestamp_field == f)
1504
timestamp_field_offset= fieldnr;
1415
1506
if (use_hash) /* supposedly this never fails... but comments lie */
1417
1508
const char *local_field_name= field[fieldnr]->field_name;
1418
1509
name_hash.insert(make_pair(local_field_name, &(field[fieldnr])));
1422
1514
keyinfo= key_info;
1652
1746
This function is called when the table definition is not cached in
1653
definition::Cache::singleton().getCache()
1654
1748
The data is returned in 'share', which is alloced by
1655
1749
alloc_table_share().. The code assumes that share is initialized.
1731
1825
5 Error (see open_table_error: charset unavailable)
1732
1826
7 Table definition has changed in engine
1734
1829
int TableShare::open_table_from_share(Session *session,
1735
1830
const TableIdentifier &identifier,
1736
1831
const char *alias,
1737
1832
uint32_t db_stat, uint32_t ha_open_flags,
1738
1833
Table &outparam)
1740
1837
bool error_reported= false;
1741
int ret= open_table_from_share_inner(session, alias, db_stat, outparam);
1744
ret= open_table_cursor_inner(identifier, db_stat, ha_open_flags, outparam, error_reported);
1749
if (not error_reported)
1750
open_table_error(ret, errno, 0);
1752
delete outparam.cursor;
1753
outparam.cursor= 0; // For easier error checking
1754
outparam.db_stat= 0;
1755
outparam.getMemRoot()->free_root(MYF(0)); // Safe to call on zeroed root
1756
outparam.clearAlias();
1761
int TableShare::open_table_from_share_inner(Session *session,
1768
1838
unsigned char *record= NULL;
1769
1839
Field **field_ptr;
1774
1844
local_error= 1;
1775
1845
outparam.resetTable(session, this, db_stat);
1777
outparam.setAlias(alias);
1847
if (not (outparam.alias= strdup(alias)))
1779
1850
/* Allocate Cursor */
1780
if (not (outparam.cursor= db_type()->getCursor(outparam)))
1851
if (not (outparam.cursor= db_type()->getCursor(*this)))
1783
1854
local_error= 4;
1845
1916
outparam.found_next_number_field=
1846
1917
outparam.getField(positionFields(found_next_number_field));
1847
1918
if (timestamp_field)
1848
outparam.timestamp_field= (Field_timestamp*) outparam.getField(timestamp_field->position());
1919
outparam.timestamp_field= (Field_timestamp*) outparam.getField(timestamp_field_offset);
1850
1922
/* Fix key->name and key_part->field */
1855
1927
uint32_t n_length;
1856
1928
n_length= keys*sizeof(KeyInfo) + key_parts*sizeof(KeyPartInfo);
1857
1929
if (!(local_key_info= (KeyInfo*) outparam.alloc_root(n_length)))
1859
1931
outparam.key_info= local_key_info;
1860
1932
key_part= (reinterpret_cast<KeyPartInfo*> (local_key_info+keys));
1899
1971
outparam.tmp_set.resize(fields);
1900
1972
outparam.default_column_bitmaps();
1905
int TableShare::open_table_cursor_inner(const TableIdentifier &identifier,
1906
uint32_t db_stat, uint32_t ha_open_flags,
1908
bool &error_reported)
1910
1974
/* The table struct is now initialized; Open the table */
1914
1978
assert(!(db_stat & HA_WAIT_IF_LOCKED));
1917
if ((ha_err= (outparam.cursor->ha_open(identifier,
1918
(db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
1919
(db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE : HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
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))))
1921
1985
switch (ha_err)
1943
2007
local_error= 7;
2017
if (!error_reported)
2018
open_table_error(local_error, errno, 0);
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);
2026
return (local_error);
1953
2029
/* error message when opening a form cursor */
2055
2130
case DRIZZLE_TYPE_VARCHAR:
2056
2131
setVariableWidth();
2057
2132
return new (&mem_root) Field_varstring(ptr,field_length,
2058
ha_varchar_packlength(field_length),
2133
HA_VARCHAR_PACKLENGTH(field_length),
2059
2134
null_pos,null_bit,
2061
2136
field_charset);
2088
2164
false /* is_unsigned */);
2089
case DRIZZLE_TYPE_UUID:
2090
return new (&mem_root) field::Uuid(ptr,
2095
2165
case DRIZZLE_TYPE_LONG:
2096
return new (&mem_root) field::Int32(ptr,
2166
return new (&mem_root) Field_long(ptr,
2173
false /* is_unsigned */);
2102
2174
case DRIZZLE_TYPE_LONGLONG:
2103
return new (&mem_root) field::Int64(ptr,
2175
return new (&mem_root) Field_int64_t(ptr,
2182
false /* is_unsigned */);
2109
2183
case DRIZZLE_TYPE_TIMESTAMP:
2110
2184
return new (&mem_root) Field_timestamp(ptr,