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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2009 Sun Microsystems, Inc.
*
* 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
*/
#include "config.h"
#include <assert.h>
#include <boost/lexical_cast.hpp>
#include "drizzled/identifier.h"
#include "drizzled/session.h"
#include "drizzled/internal/my_sys.h"
#include "drizzled/table.h"
#include "drizzled/util/string.h"
#include "drizzled/util/tablename_to_filename.h"
#include <algorithm>
#include <sstream>
#include <cstdio>
#include <boost/thread.hpp>
using namespace std;
namespace drizzled
{
class SchemaIdentifier;
extern std::string drizzle_tmpdir;
extern pid_t current_pid;
static const char hexchars[]= "0123456789abcdef";
/*
Translate a cursor name to a table name (WL #1324).
SYNOPSIS
filename_to_tablename()
from The cursor name
to OUT The table name
to_length The size of the table name buffer.
RETURN
Table name length.
*/
uint32_t TableIdentifier::filename_to_tablename(const char *from, char *to, uint32_t to_length)
{
uint32_t length= 0;
if (!memcmp(from, TMP_FILE_PREFIX, TMP_FILE_PREFIX_LENGTH))
{
/* Temporary table name. */
length= strlen(strncpy(to, from, to_length));
}
else
{
for (; *from && length < to_length; length++, from++)
{
if (*from != '@')
{
to[length]= *from;
continue;
}
/* We've found an escaped char - skip the @ */
from++;
to[length]= 0;
/* There will be a two-position hex-char version of the char */
for (int x=1; x >= 0; x--)
{
if (*from >= '0' && *from <= '9')
to[length] += ((*from++ - '0') << (4 * x));
else if (*from >= 'a' && *from <= 'f')
to[length] += ((*from++ - 'a' + 10) << (4 * x));
}
/* Backup because we advanced extra in the inner loop */
from--;
}
}
return length;
}
/*
Creates path to a cursor: drizzle_tmpdir/#sql1234_12_1.ext
SYNOPSIS
build_tmptable_filename()
session The thread handle.
buff Where to write result
bufflen buff size
NOTES
Uses current_pid, thread_id, and tmp_table counter to create
a cursor name in drizzle_tmpdir.
RETURN
path length on success, 0 on failure
*/
#ifdef _GLIBCXX_HAVE_TLS
__thread uint32_t counter= 0;
static uint32_t get_counter()
{
return ++counter;
}
#else
boost::mutex counter_mutex;
static uint32_t counter= 1;
static uint32_t get_counter()
{
boost::mutex::scoped_lock lock(counter_mutex);
uint32_t x;
x= ++counter;
return x;
}
#endif
size_t TableIdentifier::build_tmptable_filename(std::string &buffer)
{
size_t tmpdir_length;
ostringstream post_tmpdir_str;
buffer.append(drizzle_tmpdir);
tmpdir_length= buffer.length();
post_tmpdir_str << "/" << TMP_FILE_PREFIX << current_pid;
post_tmpdir_str << pthread_self() << "-" << get_counter();
buffer.append(post_tmpdir_str.str());
transform(buffer.begin() + tmpdir_length, buffer.end(), buffer.begin() + tmpdir_length, ::tolower);
return buffer.length();
}
size_t TableIdentifier::build_tmptable_filename(std::vector<char> &buffer)
{
ostringstream post_tmpdir_str;
post_tmpdir_str << drizzle_tmpdir << "/" << TMP_FILE_PREFIX << current_pid;
post_tmpdir_str << pthread_self() << "-" << get_counter();
buffer.resize(post_tmpdir_str.str().length() + 1);
memcpy(&buffer[0], post_tmpdir_str.str().c_str(), post_tmpdir_str.str().size());
buffer[post_tmpdir_str.str().size()]= 0;
return buffer.size();
}
/*
Creates path to a cursor: drizzle_data_dir/db/table.ext
SYNOPSIS
build_table_filename()
buff Where to write result
This may be the same as table_name.
bufflen buff size
db Database name
table_name Table name
ext File extension.
flags table_name is temporary, do not change.
NOTES
Uses database and table name, and extension to create
a cursor name in drizzle_data_dir. Database and table
names are converted from system_charset_info into "fscs".
Unless flags indicate a temporary table name.
'db' is always converted.
'ext' is not converted.
The conversion suppression is required for ALTER Table. This
statement creates intermediate tables. These are regular
(non-temporary) tables with a temporary name. Their path names must
be derivable from the table name. So we cannot use
build_tmptable_filename() for them.
RETURN
path length on success, 0 on failure
*/
size_t TableIdentifier::build_table_filename(std::string &in_path, const std::string &in_db, const std::string &in_table_name, bool is_tmp)
{
bool conversion_error= false;
conversion_error= util::tablename_to_filename(in_db, in_path);
if (conversion_error)
{
errmsg_printf(ERRMSG_LVL_ERROR,
_("Schema name cannot be encoded and fit within filesystem "
"name length restrictions."));
return 0;
}
in_path.append(FN_ROOTDIR);
if (is_tmp) // It a conversion tmp
{
in_path.append(in_table_name);
}
else
{
conversion_error= util::tablename_to_filename(in_table_name, in_path);
if (conversion_error)
{
errmsg_printf(ERRMSG_LVL_ERROR,
_("Table name cannot be encoded and fit within filesystem "
"name length restrictions."));
return 0;
}
}
return in_path.length();
}
TableIdentifier::TableIdentifier(const drizzled::Table &table) :
SchemaIdentifier(table.getShare()->getSchemaName()),
type(table.getShare()->getTableType()),
table_name(table.getShare()->getTableName())
{
if (type == message::Table::TEMPORARY)
path= table.getShare()->getPath();
init();
}
void TableIdentifier::init()
{
switch (type) {
case message::Table::FUNCTION:
case message::Table::STANDARD:
assert(path.size() == 0);
build_table_filename(path, getSchemaName(), table_name, false);
break;
case message::Table::INTERNAL:
assert(path.size() == 0);
build_table_filename(path, getSchemaName(), table_name, true);
break;
case message::Table::TEMPORARY:
if (path.empty())
{
build_tmptable_filename(path);
}
break;
}
util::insensitive_hash hasher;
hash_value= hasher(path);
std::string tb_name(getTableName());
std::transform(tb_name.begin(), tb_name.end(), tb_name.begin(), ::tolower);
key.set(getKeySize(), getSchemaName(), tb_name);
}
const std::string &TableIdentifier::getPath() const
{
return path;
}
void TableIdentifier::getSQLPath(std::string &sql_path) const // @todo this is just used for errors, we should find a way to optimize it
{
switch (type) {
case message::Table::FUNCTION:
case message::Table::STANDARD:
sql_path.append(getSchemaName());
sql_path.append(".");
sql_path.append(table_name);
break;
case message::Table::INTERNAL:
sql_path.append("temporary.");
sql_path.append(table_name);
break;
case message::Table::TEMPORARY:
sql_path.append(getSchemaName());
sql_path.append(".#");
sql_path.append(table_name);
break;
}
}
bool TableIdentifier::isValid() const
{
if (not SchemaIdentifier::isValid())
return false;
bool error= false;
do
{
if (table_name.empty())
{
error= true;
break;
}
if (table_name.size() > NAME_LEN)
{
error= true;
break;
}
if (table_name.at(table_name.length() -1) == ' ')
{
error= true;
break;
}
if (table_name.at(0) == '.')
{
error= true;
break;
}
{
const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
int well_formed_error;
uint32_t res= cs->cset->well_formed_len(cs, table_name.c_str(), table_name.c_str() + table_name.length(),
NAME_CHAR_LEN, &well_formed_error);
if (well_formed_error or table_name.length() != res)
{
error= true;
break;
}
}
} while (0);
if (error)
{
std::string name;
getSQLPath(name);
my_error(ER_WRONG_TABLE_NAME, MYF(0), name.c_str());
return false;
}
return true;
}
void TableIdentifier::copyToTableMessage(message::Table &message) const
{
message.set_name(table_name);
message.set_schema(getSchemaName());
}
void TableIdentifier::Key::set(size_t resize_arg, const std::string &a, const std::string &b)
{
key_buffer.resize(resize_arg);
std::copy(a.begin(), a.end(), key_buffer.begin());
std::copy(b.begin(), b.end(), key_buffer.begin() + a.length() + 1);
util::sensitive_hash hasher;
hash_value= hasher(key_buffer);
}
std::size_t hash_value(TableIdentifier const& b)
{
return b.getHashValue();
}
std::size_t hash_value(TableIdentifier::Key const& b)
{
return b.getHashValue();
}
} /* namespace drizzled */
|