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
267
268
269
270
271
272
273
274
275
276
277
278
|
/* Copyright (C) 2000-2004 MySQL AB
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; version 2 of the License.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/* HANDLER ... commands - direct access to ISAM */
/* TODO:
HANDLER blabla OPEN [ AS foobar ] [ (column-list) ]
the most natural (easiest, fastest) way to do it is to
compute List<Item> field_list not in mysql_ha_read
but in mysql_ha_open, and then store it in Table structure.
The problem here is that mysql_parse calls free_item to free all the
items allocated at the end of every query. The workaround would to
keep two item lists per Session - normal free_list and handler_items.
The second is to be freeed only on thread end. mysql_ha_open should
then do { handler_items=concat(handler_items, free_list); free_list=0; }
But !!! do_command calls free_root at the end of every query and frees up
all the sql_alloc'ed memory. It's harder to work around...
*/
/*
There are two containers holding information about open handler tables.
The first is 'session->handler_tables'. It is a linked list of Table objects.
It is used like 'session->open_tables' in the table cache. The trick is to
exchange these two lists during open and lock of tables. Thus the normal
table cache code can be used.
The second container is a HASH. It holds objects of the type TableList.
Despite its name, no lists of tables but only single structs are hashed
(the 'next' pointer is always NULL). The reason for theis second container
is, that we want handler tables to survive FLUSH Table commands. A table
affected by FLUSH Table must be closed so that other threads are not
blocked by handler tables still in use. Since we use the normal table cache
functions with 'session->handler_tables', the closed tables are removed from
this list. Hence we need the original open information for the handler
table in the case that it is used again. This information is handed over
to mysql_ha_open() as a TableList. So we store this information in the
second container, where it is not affected by FLUSH Table. The second
container is implemented as a hash for performance reasons. Consequently,
we use it not only for re-opening a handler table, but also for the
HANDLER ... READ commands. For this purpose, we store a pointer to the
Table structure (in the first container) in the TBALE_LIST object in the
second container. When the table is flushed, the pointer is cleared.
*/
#include <drizzled/server_includes.h>
#include <drizzled/sql_select.h>
#include <drizzled/error.h>
#include <drizzled/sql_base.h>
#include <drizzled/lock.h>
/**
Close a HANDLER table.
@param session Thread identifier.
@param tables A list of tables with the first entry to close.
@param is_locked If LOCK_open is locked.
@note Though this function takes a list of tables, only the first list entry
will be closed.
@note Broadcasts refresh if it closed a table with old version.
*/
static void mysql_ha_close_table(Session *session, TableList *tables,
bool is_locked)
{
Table **table_ptr;
/*
Though we could take the table pointer from hash_tables->table,
we must follow the session->handler_tables chain anyway, as we need the
address of the 'next' pointer referencing this table
for close_thread_table().
*/
for (table_ptr= &(session->handler_tables);
*table_ptr && (*table_ptr != tables->table);
table_ptr= &(*table_ptr)->next)
;
if (*table_ptr)
{
(*table_ptr)->file->ha_index_or_rnd_end();
if (! is_locked)
pthread_mutex_lock(&LOCK_open);
if (close_thread_table(session, table_ptr))
{
/* Tell threads waiting for refresh that something has happened */
broadcast_refresh();
}
if (! is_locked)
pthread_mutex_unlock(&LOCK_open);
}
else if (tables->table)
{
/* Must be a temporary table */
Table *table= tables->table;
table->file->ha_index_or_rnd_end();
table->query_id= session->query_id;
table->open_by_handler= 0;
}
}
/*
Close a HANDLER table by alias or table name
SYNOPSIS
mysql_ha_close()
session Thread identifier.
tables A list of tables with the first entry to close.
DESCRIPTION
Closes the table that is associated (on the handler tables hash) with the
name (table->alias) of the specified table.
RETURN
false ok
true error
*/
bool mysql_ha_close(Session *session, TableList *tables)
{
TableList *hash_tables;
if ((hash_tables= (TableList*) hash_search(&session->handler_tables_hash,
(unsigned char*) tables->alias,
strlen(tables->alias) + 1)))
{
mysql_ha_close_table(session, hash_tables, false);
hash_delete(&session->handler_tables_hash, (unsigned char*) hash_tables);
}
else
{
my_error(ER_UNKNOWN_TABLE, MYF(0), tables->alias, "HANDLER");
return(true);
}
session->my_ok();
return(false);
}
/**
Scan the handler tables hash for matching tables.
@param session Thread identifier.
@param tables The list of tables to remove.
@return Pointer to head of linked list (TableList::next_local) of matching
TableList elements from handler_tables_hash. Otherwise, NULL if no
table was matched.
*/
static TableList *mysql_ha_find(Session *session, TableList *tables)
{
TableList *hash_tables, *head= NULL, *first= tables;
/* search for all handlers with matching table names */
for (uint32_t i= 0; i < session->handler_tables_hash.records; i++)
{
hash_tables= (TableList*) hash_element(&session->handler_tables_hash, i);
for (tables= first; tables; tables= tables->next_local)
{
if ((! *tables->db ||
! my_strcasecmp(&my_charset_utf8_general_ci, hash_tables->db, tables->db)) &&
! my_strcasecmp(&my_charset_utf8_general_ci, hash_tables->table_name,
tables->table_name))
break;
}
if (tables)
{
hash_tables->next_local= head;
head= hash_tables;
}
}
return(head);
}
/**
Remove matching tables from the HANDLER's hash table.
@param session Thread identifier.
@param tables The list of tables to remove.
@param is_locked If LOCK_open is locked.
@note Broadcasts refresh if it closed a table with old version.
*/
void mysql_ha_rm_tables(Session *session, TableList *tables, bool is_locked)
{
TableList *hash_tables, *next;
assert(tables);
hash_tables= mysql_ha_find(session, tables);
while (hash_tables)
{
next= hash_tables->next_local;
if (hash_tables->table)
mysql_ha_close_table(session, hash_tables, is_locked);
hash_delete(&session->handler_tables_hash, (unsigned char*) hash_tables);
hash_tables= next;
}
return;
}
/**
Flush (close and mark for re-open) all tables that should be should
be reopen.
@param session Thread identifier.
@note Broadcasts refresh if it closed a table with old version.
*/
void mysql_ha_flush(Session *session)
{
TableList *hash_tables;
safe_mutex_assert_owner(&LOCK_open);
for (uint32_t i= 0; i < session->handler_tables_hash.records; i++)
{
hash_tables= (TableList*) hash_element(&session->handler_tables_hash, i);
if (hash_tables->table && hash_tables->table->needs_reopen_or_name_lock())
{
mysql_ha_close_table(session, hash_tables, true);
/* Mark table as closed, ready for re-open. */
hash_tables->table= NULL;
}
}
return;
}
/**
Close all HANDLER's tables.
@param session Thread identifier.
@note Broadcasts refresh if it closed a table with old version.
*/
void mysql_ha_cleanup(Session *session)
{
TableList *hash_tables;
for (uint32_t i= 0; i < session->handler_tables_hash.records; i++)
{
hash_tables= (TableList*) hash_element(&session->handler_tables_hash, i);
if (hash_tables->table)
mysql_ha_close_table(session, hash_tables, false);
}
hash_free(&session->handler_tables_hash);
return;
}
|