1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
This class is shared between different table objects. There is one
23
instance of table share per one table in the database.
26
/* Basic functions needed by many modules */
27
#include <drizzled/server_includes.h>
30
#include <drizzled/error.h>
31
#include <drizzled/gettext.h>
32
#include <drizzled/sql_base.h>
36
static HASH table_def_cache;
37
static pthread_mutex_t LOCK_table_share;
38
bool table_def_inited= false;
40
/*****************************************************************************
41
Functions to handle table definition cach (TableShare)
42
*****************************************************************************/
46
unsigned char *table_def_key(const unsigned char *record,
51
unsigned char *table_def_key(const unsigned char *record,
55
TableShare *entry=(TableShare*) record;
56
*length= entry->table_cache_key.length;
57
return (unsigned char*) entry->table_cache_key.str;
61
static void table_def_free_entry(TableShare *share)
63
share->free_table_share();
67
bool TableShare::cacheStart(void)
69
table_def_inited= true;
70
pthread_mutex_init(&LOCK_table_share, MY_MUTEX_INIT_FAST);
72
return hash_init(&table_def_cache, &my_charset_bin, (size_t)table_def_size,
74
(hash_free_key) table_def_free_entry, 0);
78
void TableShare::cacheStop(void)
83
pthread_mutex_destroy(&LOCK_table_share);
84
hash_free(&table_def_cache);
89
uint32_t cached_table_definitions(void)
91
return table_def_cache.records;
96
Mark that we are not using table share anymore.
103
If ref_count goes to zero and (we have done a refresh or if we have
104
already too many open table shares) then delete the definition.
107
void TableShare::release(TableShare *share)
109
bool to_be_deleted= false;
110
safe_mutex_assert_owner(&LOCK_open);
112
pthread_mutex_lock(&share->mutex);
113
if (!--share->ref_count)
118
hash_delete(&table_def_cache, (unsigned char*) share);
121
pthread_mutex_unlock(&share->mutex);
124
void TableShare::release(const char *key, uint32_t key_length)
128
if ((share= (TableShare*) hash_search(&table_def_cache,(unsigned char*) key,
131
share->version= 0; // Mark for delete
132
if (share->ref_count == 0)
134
pthread_mutex_lock(&share->mutex);
135
hash_delete(&table_def_cache, (unsigned char*) share);
143
Get TableShare for a table.
146
session Thread handle
147
table_list Table that should be opened
149
key_length Length of key
150
error out: Error code from open_table_def()
153
Get a table definition from the table definition cache.
154
If it doesn't exist, create a new from the table definition file.
157
We must have wrlock on LOCK_open when we come here
158
(To be changed later)
165
TableShare *TableShare::getShare(Session *session,
166
TableList *table_list, char *key,
167
uint32_t key_length, uint32_t, int *error)
173
/* Read table definition from cache */
174
if ((share= (TableShare*) hash_search(&table_def_cache,(unsigned char*) key,
178
if (!(share= alloc_table_share(table_list, key, key_length)))
184
Lock mutex to be able to read table definition from file without
187
(void) pthread_mutex_lock(&share->mutex);
189
if (my_hash_insert(&table_def_cache, (unsigned char*) share))
191
share->free_table_share();
192
return NULL; // return error
194
if (open_table_def(session, share))
196
*error= share->error;
197
(void) hash_delete(&table_def_cache, (unsigned char*) share);
200
share->ref_count++; // Mark in use
201
(void) pthread_mutex_unlock(&share->mutex);
206
We found an existing table definition. Return it if we didn't get
207
an error when reading the table definition from file.
210
/* We must do a lock to ensure that the structure is initialized */
211
(void) pthread_mutex_lock(&share->mutex);
214
/* Table definition contained an error */
215
share->open_table_error(share->error, share->open_errno, share->errarg);
216
(void) pthread_mutex_unlock(&share->mutex);
222
(void) pthread_mutex_unlock(&share->mutex);
229
Check if table definition exits in cache
232
get_cached_table_share()
234
table_name Table name
238
# TableShare for table
241
TableShare *TableShare::getShare(const char *db, const char *table_name)
243
char key[NAME_LEN*2+2];
245
safe_mutex_assert_owner(&LOCK_open);
247
key_length= TableShare::createKey(key, db, table_name);
249
return (TableShare*) hash_search(&table_def_cache,(unsigned char*) key, key_length);