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
|
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2010 Brian Aker
*
* 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 <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "drizzled/session.h"
#include "plugin/myisam/myisam.h"
#include "drizzled/plugin/transactional_storage_engine.h"
#include "drizzled/table.h"
namespace drizzled
{
namespace table
{
Singular::Singular(Session *session, List<CreateField> &field_list) :
_share(message::Table::INTERNAL),
_has_variable_width(false)
{
uint32_t field_count= field_list.elements;
uint32_t blob_count= 0;
Field **field_arg;
CreateField *cdef; /* column definition */
uint32_t record_length= 0;
uint32_t null_count= 0; /* number of columns which may be null */
uint32_t null_pack_length; /* NULL representation array length */
getMutableShare()->setFields(field_count + 1);
setFields(getMutableShare()->getFields(true));
field_arg= getMutableShare()->getFields(true);
getMutableShare()->blob_field.resize(field_count+1);
getMutableShare()->setFieldSize(field_count);
getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
setup_tmp_table_column_bitmaps();
in_use= session; /* field_arg->reset() may access in_use */
/* Create all fields and calculate the total length of record */
List_iterator_fast<CreateField> it(field_list);
message::Table::Field null_field;
while ((cdef= it++))
{
*field_arg= getMutableShare()->make_field(null_field,
NULL,
cdef->length,
(cdef->flags & NOT_NULL_FLAG) ? false : true,
(unsigned char *) ((cdef->flags & NOT_NULL_FLAG) ? 0 : ""),
(cdef->flags & NOT_NULL_FLAG) ? 0 : 1,
cdef->decimals,
cdef->sql_type,
cdef->charset,
cdef->unireg_check,
cdef->interval,
cdef->field_name,
cdef->flags & UNSIGNED_FLAG ? true : false);
if (!*field_arg)
{
throw "Memory allocation failure";
}
(*field_arg)->init(this);
record_length+= (*field_arg)->pack_length();
if (! ((*field_arg)->flags & NOT_NULL_FLAG))
null_count++;
if ((*field_arg)->flags & BLOB_FLAG)
getMutableShare()->blob_field[blob_count++]= (uint32_t) (field_arg - getFields());
field_arg++;
}
*field_arg= NULL; /* mark the end of the list */
getMutableShare()->blob_field[blob_count]= 0; /* mark the end of the list */
getMutableShare()->blob_fields= blob_count;
null_pack_length= (null_count + 7)/8;
getMutableShare()->setRecordLength(record_length + null_pack_length);
getMutableShare()->rec_buff_length= ALIGN_SIZE(getMutableShare()->getRecordLength() + 1);
record[0]= (unsigned char*)session->getMemRoot()->allocate(getMutableShare()->rec_buff_length);
if (not getInsertRecord())
{
throw "Memory allocation failure";
}
if (null_pack_length)
{
null_flags= (unsigned char*) getInsertRecord();
getMutableShare()->null_fields= null_count;
getMutableShare()->null_bytes= null_pack_length;
}
{
/* Set up field pointers */
unsigned char *null_pos= getInsertRecord();
unsigned char *field_pos= null_pos + getMutableShare()->null_bytes;
uint32_t null_bit= 1;
for (field_arg= getFields(); *field_arg; ++field_arg)
{
Field *cur_field= *field_arg;
if ((cur_field->flags & NOT_NULL_FLAG))
cur_field->move_field(field_pos);
else
{
cur_field->move_field(field_pos, (unsigned char*) null_pos, null_bit);
null_bit<<= 1;
if (null_bit == (1 << 8))
{
++null_pos;
null_bit= 1;
}
}
cur_field->reset();
field_pos+= cur_field->pack_length();
}
}
}
bool Singular::open_tmp_table()
{
int error;
identifier::Table identifier(getShare()->getSchemaName(), getShare()->getTableName(), getShare()->getPath());
if ((error=cursor->ha_open(identifier,
O_RDWR,
HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
{
print_error(error, MYF(0));
db_stat= 0;
return true;
}
(void) cursor->extra(HA_EXTRA_QUICK); /* Faster */
return false;
}
/*
Create MyISAM temporary table
SYNOPSIS
create_myisam_tmp_table()
keyinfo Description of the index (there is always one index)
start_recinfo MyISAM's column descriptions
recinfo INOUT End of MyISAM's column descriptions
options Option bits
DESCRIPTION
Create a MyISAM temporary table according to passed description. The is
assumed to have one unique index or constraint.
The passed array or MI_COLUMNDEF structures must have this form:
1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
when there are many nullable columns)
2. Table columns
3. One free MI_COLUMNDEF element (*recinfo points here)
This function may use the free element to create hash column for unique
constraint.
RETURN
false - OK
true - Error
*/
bool Singular::create_myisam_tmp_table(KeyInfo *keyinfo,
MI_COLUMNDEF *start_recinfo,
MI_COLUMNDEF **recinfo,
uint64_t options)
{
int error;
MI_KEYDEF keydef;
MI_UNIQUEDEF uniquedef;
if (getShare()->sizeKeys())
{ // Get keys for ni_create
bool using_unique_constraint= false;
HA_KEYSEG *seg= (HA_KEYSEG*) getMemRoot()->alloc_root(sizeof(*seg) * keyinfo->key_parts);
if (not seg)
return true;
memset(seg, 0, sizeof(*seg) * keyinfo->key_parts);
if (keyinfo->key_length >= cursor->getEngine()->max_key_length() ||
keyinfo->key_parts > cursor->getEngine()->max_key_parts() ||
getShare()->uniques)
{
/* Can't create a key; Make a unique constraint instead of a key */
getMutableShare()->keys= 0;
getMutableShare()->uniques= 1;
using_unique_constraint= true;
memset(&uniquedef, 0, sizeof(uniquedef));
uniquedef.keysegs=keyinfo->key_parts;
uniquedef.seg=seg;
uniquedef.null_are_equal=1;
/* Create extra column for hash value */
memset(*recinfo, 0, sizeof(**recinfo));
(*recinfo)->type= FIELD_CHECK;
(*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
(*recinfo)++;
getMutableShare()->setRecordLength(getShare()->getRecordLength() + MI_UNIQUE_HASH_LENGTH);
}
else
{
/* Create an unique key */
memset(&keydef, 0, sizeof(keydef));
keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
keydef.keysegs= keyinfo->key_parts;
keydef.seg= seg;
}
for (uint32_t i= 0; i < keyinfo->key_parts ; i++,seg++)
{
Field *key_field=keyinfo->key_part[i].field;
seg->flag= 0;
seg->language= key_field->charset()->number;
seg->length= keyinfo->key_part[i].length;
seg->start= keyinfo->key_part[i].offset;
if (key_field->flags & BLOB_FLAG)
{
seg->type= ((keyinfo->key_part[i].key_type & 1 /* binary */) ?
HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
seg->bit_start= (uint8_t)(key_field->pack_length() - getShare()->blob_ptr_size);
seg->flag= HA_BLOB_PART;
seg->length= 0; // Whole blob in unique constraint
}
else
{
seg->type= keyinfo->key_part[i].type;
}
if (!(key_field->flags & NOT_NULL_FLAG))
{
seg->null_bit= key_field->null_bit;
seg->null_pos= (uint32_t) (key_field->null_ptr - (unsigned char*) getInsertRecord());
/*
We are using a GROUP BY on something that contains NULL
In this case we have to tell MyISAM that two NULL should
on INSERT be regarded at the same value
*/
if (! using_unique_constraint)
keydef.flag|= HA_NULL_ARE_EQUAL;
}
}
}
MI_CREATE_INFO create_info;
if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
OPTION_BIG_TABLES)
create_info.data_file_length= ~(uint64_t) 0;
if ((error= mi_create(getShare()->getTableName(), getShare()->sizeKeys(), &keydef,
(uint32_t) (*recinfo-start_recinfo),
start_recinfo,
getShare()->uniques, &uniquedef,
&create_info,
HA_CREATE_TMP_TABLE)))
{
print_error(error, MYF(0));
db_stat= 0;
return true;
}
in_use->status_var.created_tmp_disk_tables++;
getMutableShare()->db_record_offset= 1;
return false;
}
/*
Set up column usage bitmaps for a temporary table
IMPLEMENTATION
For temporary tables, we need one bitmap with all columns set and
a tmp_set bitmap to be used by things like filesort.
*/
void Singular::setup_tmp_table_column_bitmaps()
{
uint32_t field_count= getShare()->sizeFields();
def_read_set.resize(field_count);
def_write_set.resize(field_count);
tmp_set.resize(field_count);
getMutableShare()->all_set.resize(field_count);
getMutableShare()->all_set.set();
def_write_set.set();
def_read_set.set();
default_column_bitmaps();
}
Singular::~Singular()
{
const char *save_proc_info;
save_proc_info= in_use->get_proc_info();
in_use->set_proc_info("removing tmp table");
// Release latches since this can take a long time
plugin::TransactionalStorageEngine::releaseTemporaryLatches(in_use);
if (cursor)
{
if (db_stat)
{
cursor->closeMarkForDelete(getShare()->getTableName());
}
identifier::Table identifier(getShare()->getSchemaName(), getShare()->getTableName(), getShare()->getTableName());
drizzled::error_t ignored;
plugin::StorageEngine::dropTable(*in_use,
*getShare()->getEngine(),
identifier,
ignored);
delete cursor;
}
/* free blobs */
for (Field **ptr= getFields() ; *ptr ; ptr++)
{
(*ptr)->free();
}
free_io_cache();
getMemRoot()->free_root(MYF(0));
in_use->set_proc_info(save_proc_info);
}
} /* namespace table */
} /* namespace drizzled */
|