115
/*****************************************************************************
116
Functions to handle table definition cach (TableShare)
117
*****************************************************************************/
119
unsigned char *table_def_key(const unsigned char *record,
123
TableShare *entry=(TableShare*) record;
124
*length= entry->table_cache_key.length;
125
return (unsigned char*) entry->table_cache_key.str;
129
static void table_def_free_entry(TableShare *share)
131
share->free_table_share();
135
bool table_def_init(void)
138
pthread_mutex_init(&LOCK_table_share, MY_MUTEX_INIT_FAST);
140
return hash_init(&table_def_cache, &my_charset_bin, (size_t)table_def_size,
142
(hash_free_key) table_def_free_entry, 0);
146
void table_def_free(void)
148
if (table_def_inited)
151
pthread_mutex_destroy(&LOCK_table_share);
152
hash_free(&table_def_cache);
157
uint32_t cached_table_definitions(void)
159
return table_def_cache.records;
164
Get TableShare for a table.
167
session Thread handle
168
table_list Table that should be opened
170
key_length Length of key
171
error out: Error code from open_table_def()
174
Get a table definition from the table definition cache.
175
If it doesn't exist, create a new from the table definition file.
178
We must have wrlock on LOCK_open when we come here
179
(To be changed later)
186
TableShare *get_table_share(Session *session, TableList *table_list, char *key,
187
uint32_t key_length, uint32_t, int *error)
193
/* Read table definition from cache */
194
if ((share= (TableShare*) hash_search(&table_def_cache,(unsigned char*) key,
198
if (!(share= alloc_table_share(table_list, key, key_length)))
204
Lock mutex to be able to read table definition from file without
207
(void) pthread_mutex_lock(&share->mutex);
209
if (my_hash_insert(&table_def_cache, (unsigned char*) share))
211
share->free_table_share();
212
return NULL; // return error
214
if (open_table_def(session, share))
216
*error= share->error;
217
(void) hash_delete(&table_def_cache, (unsigned char*) share);
220
share->ref_count++; // Mark in use
221
(void) pthread_mutex_unlock(&share->mutex);
226
We found an existing table definition. Return it if we didn't get
227
an error when reading the table definition from file.
230
/* We must do a lock to ensure that the structure is initialized */
231
(void) pthread_mutex_lock(&share->mutex);
234
/* Table definition contained an error */
235
share->open_table_error(share->error, share->open_errno, share->errarg);
236
(void) pthread_mutex_unlock(&share->mutex);
242
(void) pthread_mutex_unlock(&share->mutex);
249
Mark that we are not using table share anymore.
252
release_table_share()
256
If ref_count goes to zero and (we have done a refresh or if we have
257
already too many open table shares) then delete the definition.
259
If type == RELEASE_WAIT_FOR_DROP then don't return until we get a signal
260
that the table is deleted or the thread is killed.
263
void release_table_share(TableShare *share)
265
bool to_be_deleted= false;
267
safe_mutex_assert_owner(&LOCK_open);
269
pthread_mutex_lock(&share->mutex);
270
if (!--share->ref_count)
275
hash_delete(&table_def_cache, (unsigned char*) share);
278
pthread_mutex_unlock(&share->mutex);
283
Check if table definition exits in cache
286
get_cached_table_share()
288
table_name Table name
292
# TableShare for table
295
TableShare *get_cached_table_share(const char *db, const char *table_name)
297
char key[NAME_LEN*2+2];
299
safe_mutex_assert_owner(&LOCK_open);
301
key_length= TableShare::createKey(key, db, table_name);
303
return (TableShare*) hash_search(&table_def_cache,(unsigned char*) key, key_length);
308
108
Close file handle, but leave the table in the table cache
368
168
a lock on LOCK_open when traversing the return list.
371
NULL Error (Probably OOM)
372
# Pointer to list of names of open tables.
375
OPEN_TableList *list_open_tables(const char *db, const char *wild)
174
bool list_open_tables(const char *db, const char *wild, bool(*func)(Table *table, open_table_list_st& open_list), Table *display)
378
OPEN_TableList **start_list, *open_list;
379
TableList table_list;
176
vector<open_table_list_st> open_list;
177
vector<open_table_list_st>::iterator it;
178
open_table_list_st table;
180
/* What we really need is an optimization for knowing unique tables */
182
open_list.reserve(sizeof(open_table_list_st) * (open_cache.records % 2));
184
open_list.reserve(sizeof(open_table_list_st) * open_cache.records);
381
186
pthread_mutex_lock(&LOCK_open); /* List all open tables */
382
memset(&table_list, 0, sizeof(table_list));
383
start_list= &open_list;
386
for (uint32_t idx=0 ; result == 0 && idx < open_cache.records; idx++)
188
for (uint32_t idx= 0; idx < open_cache.records; idx++)
388
OPEN_TableList *table;
389
191
Table *entry=(Table*) hash_element(&open_cache,idx);
390
TableShare *share= entry->s;
392
if (db && my_strcasecmp(system_charset_info, db, share->db.str))
394
if (wild && wild_compare(share->table_name.str, wild, 0))
397
/* Check if user has SELECT privilege for any column in the table */
398
table_list.db= share->db.str;
399
table_list.table_name= share->table_name.str;
401
/* need to check if we haven't already listed it */
402
for (table= open_list ; table ; table=table->next)
193
if (db && my_strcasecmp(system_charset_info, db, entry->s->db.str))
195
if (wild && wild_compare(entry->s->table_name.str, wild, 0))
198
for (it= open_list.begin(); it < open_list.end(); it++)
404
if (!strcmp(table->table, share->table_name.str) &&
405
!strcmp(table->db, share->db.str))
200
if (!(*it).table.compare(entry->s->table_name.str) &&
201
!(*it).db.compare(entry->s->db.str))
407
203
if (entry->in_use)
409
205
if (entry->locked_by_name)
416
if (!(*start_list = (OPEN_TableList *)
417
sql_alloc(sizeof(**start_list)+share->table_cache_key.length)))
419
open_list=0; // Out of memory
422
strcpy((*start_list)->table=
423
strcpy(((*start_list)->db= (char*) ((*start_list)+1)),
424
share->db.str)+share->db.length+1,
425
share->table_name.str);
426
(*start_list)->in_use= entry->in_use ? 1 : 0;
427
(*start_list)->locked= entry->locked_by_name ? 1 : 0;
428
start_list= &(*start_list)->next;
217
table.db= entry->s->db.str;
218
table.table= entry->s->table_name.str;
219
open_list.push_back(table);
431
221
pthread_mutex_unlock(&LOCK_open);
223
for (it= open_list.begin(); it < open_list.end(); it++)
225
if (func(display, *it))
435
232
/*****************************************************************************