1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2009 Sun Microsystems
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
This class is shared between different table objects. There is one
instance of table share per one table in the database.
*/
/* Basic functions needed by many modules */
#include "drizzled/server_includes.h"
#include <assert.h>
#include "drizzled/error.h"
#include "drizzled/gettext.h"
#include "drizzled/sql_base.h"
#include "drizzled/hash.h"
using namespace std;
typedef drizzled::hash_map<string, TableShare *> TableDefCache;
TableDefCache table_def_cache;
static pthread_mutex_t LOCK_table_share;
bool table_def_inited= false;
/*****************************************************************************
Functions to handle table definition cach (TableShare)
*****************************************************************************/
void TableShare::cacheStart(void)
{
pthread_mutex_init(&LOCK_table_share, MY_MUTEX_INIT_FAST);
table_def_inited= true;
/*
* This is going to overalloc a bit - as rehash sets the number of
* buckets, not the number of elements. BUT, it'll allow us to not need
* to rehash later on as the hash_map grows.
*/
table_def_cache.rehash(table_def_size);
}
void TableShare::cacheStop(void)
{
if (table_def_inited)
{
table_def_inited= false;
pthread_mutex_destroy(&LOCK_table_share);
}
}
/**
* @TODO: This should return size_t
*/
uint32_t cached_table_definitions(void)
{
return static_cast<uint32_t>(table_def_cache.size());
}
/*
Mark that we are not using table share anymore.
SYNOPSIS
release()
share Table share
IMPLEMENTATION
If ref_count goes to zero and (we have done a refresh or if we have
already too many open table shares) then delete the definition.
*/
void TableShare::release(TableShare *share)
{
bool to_be_deleted= false;
safe_mutex_assert_owner(&LOCK_open);
pthread_mutex_lock(&share->mutex);
if (!--share->ref_count)
to_be_deleted= true;
if (to_be_deleted)
{
const string key_string(share->table_cache_key.str,
share->table_cache_key.length);
TableDefCache::iterator iter= table_def_cache.find(key_string);
if (iter != table_def_cache.end())
{
(*iter).second->free_table_share();
table_def_cache.erase(iter);
}
return;
}
pthread_mutex_unlock(&share->mutex);
}
void TableShare::release(const char *key, uint32_t key_length)
{
const string key_string(key, key_length);
TableDefCache::iterator iter= table_def_cache.find(key_string);
if (iter != table_def_cache.end())
{
TableShare *share= (*iter).second;
share->version= 0; // Mark for delete
if (share->ref_count == 0)
{
pthread_mutex_lock(&share->mutex);
share->free_table_share();
table_def_cache.erase(key_string);
}
}
}
static TableShare *foundTableShare(TableShare *share)
{
/*
We found an existing table definition. Return it if we didn't get
an error when reading the table definition from file.
*/
/* We must do a lock to ensure that the structure is initialized */
(void) pthread_mutex_lock(&share->mutex);
if (share->error)
{
/* Table definition contained an error */
share->open_table_error(share->error, share->open_errno, share->errarg);
(void) pthread_mutex_unlock(&share->mutex);
return NULL;
}
share->ref_count++;
(void) pthread_mutex_unlock(&share->mutex);
return share;
}
/*
Get TableShare for a table.
get_table_share()
session Thread handle
table_list Table that should be opened
key Table cache key
key_length Length of key
error out: Error code from open_table_def()
IMPLEMENTATION
Get a table definition from the table definition cache.
If it doesn't exist, create a new from the table definition file.
NOTES
We must have wrlock on LOCK_open when we come here
(To be changed later)
RETURN
0 Error
# Share for table
*/
TableShare *TableShare::getShare(Session *session,
TableList *table_list, char *key,
uint32_t key_length, uint32_t, int *error)
{
const string key_string(key, key_length);
TableShare *share= NULL;
*error= 0;
/* Read table definition from cache */
TableDefCache::iterator iter= table_def_cache.find(key_string);
if (iter != table_def_cache.end())
{
share= (*iter).second;
return foundTableShare(share);
}
if (!(share= alloc_table_share(table_list, key, key_length)))
{
return NULL;
}
/*
Lock mutex to be able to read table definition from file without
conflicts
*/
(void) pthread_mutex_lock(&share->mutex);
/**
* @TODO: we need to eject something if we exceed table_def_size
*/
pair<TableDefCache::iterator, bool> ret=
table_def_cache.insert(make_pair(key_string, share));
if (ret.second == false)
{
share->free_table_share();
return NULL;
}
if (open_table_def(*session, share))
{
*error= share->error;
table_def_cache.erase(key_string);
share->free_table_share();
return NULL;
}
share->ref_count++; // Mark in use
(void) pthread_mutex_unlock(&share->mutex);
return share;
}
/*
Check if table definition exits in cache
SYNOPSIS
get_cached_table_share()
db Database name
table_name Table name
RETURN
0 Not cached
# TableShare for table
*/
TableShare *TableShare::getShare(const char *db, const char *table_name)
{
char key[NAME_LEN*2+2];
uint32_t key_length;
safe_mutex_assert_owner(&LOCK_open);
key_length= TableShare::createKey(key, db, table_name);
const string key_string(key, key_length);
TableDefCache::iterator iter= table_def_cache.find(key_string);
if (iter != table_def_cache.end())
{
return (*iter).second;
}
else
{
return NULL;
}
}
|