~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table/instance.cc

  • Committer: Barry.Leslie at PrimeBase
  • Date: 2010-10-20 20:41:00 UTC
  • mfrom: (1863 staging)
  • mto: This revision was merged to the branch mainline in revision 1871.
  • Revision ID: barry.leslie@primebase.com-20101020204100-oyj6p5cfssjw3p62
Merged with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include <config.h>
 
21
#include "config.h"
22
22
 
23
23
#include <sys/types.h>
24
24
#include <sys/stat.h>
25
25
#include <fcntl.h>
26
26
 
27
 
#include <drizzled/session.h>
28
 
#include <plugin/myisam/myisam.h>
29
 
#include <drizzled/plugin/transactional_storage_engine.h>
 
27
#include "drizzled/session.h"
 
28
#include "plugin/myisam/myisam.h"
 
29
#include "drizzled/plugin/transactional_storage_engine.h"
30
30
 
31
 
#include <drizzled/table.h>
 
31
#include "drizzled/table.h"
32
32
 
33
33
namespace drizzled
34
34
{
36
36
namespace table
37
37
{
38
38
 
39
 
Singular::Singular(Session *session, List<CreateField> &field_list) :
40
 
  _share(message::Table::INTERNAL),
41
 
  _has_variable_width(false)
42
 
{
43
 
  uint32_t field_count= field_list.elements;
44
 
  uint32_t blob_count= 0;
45
 
  Field **field_arg;
46
 
  CreateField *cdef;                           /* column definition */
47
 
  uint32_t record_length= 0;
48
 
  uint32_t null_count= 0;                 /* number of columns which may be null */
49
 
  uint32_t null_pack_length;              /* NULL representation array length */
50
 
 
51
 
  getMutableShare()->setFields(field_count + 1);
52
 
  setFields(getMutableShare()->getFields(true));
53
 
  field_arg= getMutableShare()->getFields(true);
54
 
  getMutableShare()->blob_field.resize(field_count+1);
55
 
  getMutableShare()->setFieldSize(field_count);
56
 
  getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
57
 
  setup_tmp_table_column_bitmaps();
58
 
 
59
 
  in_use= session;           /* field_arg->reset() may access in_use */
60
 
 
61
 
  /* Create all fields and calculate the total length of record */
62
 
  List<CreateField>::iterator it(field_list.begin());
63
 
  message::Table::Field null_field;
64
 
  while ((cdef= it++))
65
 
  {
66
 
    *field_arg= getMutableShare()->make_field(null_field,
67
 
                                              NULL,
68
 
                                              cdef->length,
69
 
                                              (cdef->flags & NOT_NULL_FLAG) ? false : true,
70
 
                                              (unsigned char *) ((cdef->flags & NOT_NULL_FLAG) ? 0 : ""),
71
 
                                              (cdef->flags & NOT_NULL_FLAG) ? 0 : 1,
72
 
                                              cdef->decimals,
73
 
                                              cdef->sql_type,
74
 
                                              cdef->charset,
75
 
                                              cdef->unireg_check,
76
 
                                              cdef->interval,
77
 
                                              cdef->field_name,
78
 
                                              cdef->flags & UNSIGNED_FLAG ? true : false);
79
 
    if (!*field_arg)
80
 
    {
81
 
      throw "Memory allocation failure";
82
 
    }
83
 
 
84
 
    (*field_arg)->init(this);
85
 
    record_length+= (*field_arg)->pack_length();
86
 
    if (! ((*field_arg)->flags & NOT_NULL_FLAG))
87
 
      null_count++;
88
 
 
89
 
    if ((*field_arg)->flags & BLOB_FLAG)
90
 
      getMutableShare()->blob_field[blob_count++]= (uint32_t) (field_arg - getFields());
91
 
 
92
 
    field_arg++;
93
 
  }
94
 
  *field_arg= NULL;                             /* mark the end of the list */
95
 
  getMutableShare()->blob_field[blob_count]= 0;            /* mark the end of the list */
96
 
  getMutableShare()->blob_fields= blob_count;
97
 
 
98
 
  null_pack_length= (null_count + 7)/8;
99
 
  getMutableShare()->setRecordLength(record_length + null_pack_length);
100
 
  getMutableShare()->rec_buff_length= ALIGN_SIZE(getMutableShare()->getRecordLength() + 1);
101
 
  record[0]= (unsigned char*)session->getMemRoot()->allocate(getMutableShare()->rec_buff_length);
102
 
  if (not getInsertRecord())
103
 
  {
104
 
    throw "Memory allocation failure";
105
 
  }
106
 
 
107
 
  if (null_pack_length)
108
 
  {
109
 
    null_flags= (unsigned char*) getInsertRecord();
110
 
    getMutableShare()->null_fields= null_count;
111
 
    getMutableShare()->null_bytes= null_pack_length;
112
 
  }
113
 
  {
114
 
    /* Set up field pointers */
115
 
    unsigned char *null_pos= getInsertRecord();
116
 
    unsigned char *field_pos= null_pos + getMutableShare()->null_bytes;
117
 
    uint32_t null_bit= 1;
118
 
 
119
 
    for (field_arg= getFields(); *field_arg; ++field_arg)
120
 
    {
121
 
      Field *cur_field= *field_arg;
122
 
      if ((cur_field->flags & NOT_NULL_FLAG))
123
 
        cur_field->move_field(field_pos);
124
 
      else
125
 
      {
126
 
        cur_field->move_field(field_pos, (unsigned char*) null_pos, null_bit);
127
 
        null_bit<<= 1;
128
 
        if (null_bit == (1 << 8))
129
 
        {
130
 
          ++null_pos;
131
 
          null_bit= 1;
132
 
        }
133
 
      }
134
 
      cur_field->reset();
135
 
 
136
 
      field_pos+= cur_field->pack_length();
137
 
    }
138
 
  }
139
 
}
140
 
 
141
 
bool Singular::open_tmp_table()
 
39
bool Instance::open_tmp_table()
142
40
{
143
41
  int error;
144
42
  
145
 
  identifier::Table identifier(getShare()->getSchemaName(), getShare()->getTableName(), getShare()->getPath());
 
43
  TableIdentifier identifier(getShare()->getSchemaName(), getShare()->getTableName(), getShare()->getPath());
146
44
  if ((error=cursor->ha_open(identifier,
 
45
                             this,
147
46
                             O_RDWR,
148
47
                             HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
149
48
  {
185
84
     true  - Error
186
85
*/
187
86
 
188
 
bool Singular::create_myisam_tmp_table(KeyInfo *keyinfo,
 
87
bool Instance::create_myisam_tmp_table(KeyInfo *keyinfo,
189
88
                                                 MI_COLUMNDEF *start_recinfo,
190
89
                                                 MI_COLUMNDEF **recinfo,
191
90
                                                 uint64_t options)
286
185
  return false;
287
186
}
288
187
 
 
188
 
 
189
void Instance::free_tmp_table(Session *session)
 
190
{
 
191
  const char *save_proc_info;
 
192
 
 
193
  save_proc_info= session->get_proc_info();
 
194
  session->set_proc_info("removing tmp table");
 
195
 
 
196
  // Release latches since this can take a long time
 
197
  plugin::TransactionalStorageEngine::releaseTemporaryLatches(session);
 
198
 
 
199
  if (cursor)
 
200
  {
 
201
    if (db_stat)
 
202
    {
 
203
      cursor->closeMarkForDelete(getShare()->getTableName());
 
204
    }
 
205
 
 
206
    TableIdentifier identifier(getShare()->getSchemaName(), getShare()->getTableName(), getShare()->getTableName());
 
207
    getShare()->getEngine()->doDropTable(*session, identifier);
 
208
 
 
209
    delete cursor;
 
210
  }
 
211
 
 
212
  /* free blobs */
 
213
  for (Field **ptr= getFields() ; *ptr ; ptr++)
 
214
  {
 
215
    (*ptr)->free();
 
216
  }
 
217
  free_io_cache();
 
218
 
 
219
  getMemRoot()->free_root(MYF(0));
 
220
  session->set_proc_info(save_proc_info);
 
221
}
 
222
 
289
223
/*
290
224
  Set up column usage bitmaps for a temporary table
291
225
 
294
228
    a tmp_set bitmap to be used by things like filesort.
295
229
*/
296
230
 
297
 
void Singular::setup_tmp_table_column_bitmaps()
 
231
void Instance::setup_tmp_table_column_bitmaps()
298
232
{
299
233
  uint32_t field_count= getShare()->sizeFields();
300
234
 
308
242
  default_column_bitmaps();
309
243
}
310
244
 
311
 
Singular::~Singular()
 
245
Instance::~Instance()
312
246
{
313
 
  const char *save_proc_info;
314
 
 
315
 
  save_proc_info= in_use->get_proc_info();
316
 
  in_use->set_proc_info("removing tmp table");
317
 
 
318
 
  // Release latches since this can take a long time
319
 
  plugin::TransactionalStorageEngine::releaseTemporaryLatches(in_use);
320
 
 
321
 
  if (cursor)
322
 
  {
323
 
    if (db_stat)
324
 
    {
325
 
      cursor->closeMarkForDelete(getShare()->getTableName());
326
 
    }
327
 
 
328
 
    identifier::Table identifier(getShare()->getSchemaName(), getShare()->getTableName(), getShare()->getTableName());
329
 
    drizzled::error_t ignored;
330
 
    plugin::StorageEngine::dropTable(*in_use,
331
 
                                     *getShare()->getEngine(),
332
 
                                     identifier,
333
 
                                     ignored);
334
 
 
335
 
    delete cursor;
336
 
  }
337
 
 
338
 
  /* free blobs */
339
 
  for (Field **ptr= getFields() ; *ptr ; ptr++)
340
 
  {
341
 
    (*ptr)->free();
342
 
  }
343
 
  free_io_cache();
344
 
 
345
 
  getMemRoot()->free_root(MYF(0));
346
 
  in_use->set_proc_info(save_proc_info);
 
247
  free_tmp_table(in_use);
347
248
}
348
249
 
349
250