~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table_share.cc

  • Committer: Paul McCullagh
  • Date: 2010-05-26 10:17:56 UTC
  • mto: (1567.1.3 new-staging)
  • mto: This revision was merged to the branch mainline in revision 1568.
  • Revision ID: paul.mccullagh@primebase.org-20100526101756-3qdwb9id3qhog0z1
Adjust for namespace changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; i/dent-tabs-mode: nil; -*-
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
4
 *  Copyright (C) 2010 Brian Aker
44
44
#include "drizzled/internal/my_pthread.h"
45
45
#include "drizzled/plugin/event_observer.h"
46
46
 
47
 
#include "drizzled/table.h"
48
 
#include "drizzled/table/shell.h"
49
 
 
50
47
#include "drizzled/session.h"
51
48
 
52
49
#include "drizzled/charset.h"
70
67
#include "drizzled/field/decimal.h"
71
68
#include "drizzled/field/real.h"
72
69
#include "drizzled/field/double.h"
73
 
#include "drizzled/field/int32.h"
74
 
#include "drizzled/field/int64.h"
 
70
#include "drizzled/field/long.h"
 
71
#include "drizzled/field/int64_t.h"
75
72
#include "drizzled/field/num.h"
76
73
#include "drizzled/field/timestamp.h"
77
74
#include "drizzled/field/datetime.h"
78
75
#include "drizzled/field/varstring.h"
79
 
#include "drizzled/field/uuid.h"
80
 
 
81
 
#include "drizzled/definition/cache.h"
82
76
 
83
77
using namespace std;
84
78
 
86
80
{
87
81
 
88
82
extern size_t table_def_size;
 
83
TableDefinitionCache table_def_cache;
 
84
static pthread_mutex_t LOCK_table_share;
 
85
bool table_def_inited= false;
89
86
 
90
87
/*****************************************************************************
91
88
  Functions to handle table definition cach (TableShare)
92
89
 *****************************************************************************/
93
90
 
 
91
 
 
92
void TableShare::cacheStart(void)
 
93
{
 
94
  pthread_mutex_init(&LOCK_table_share, MY_MUTEX_INIT_FAST);
 
95
  table_def_inited= true;
 
96
  /* 
 
97
   * This is going to overalloc a bit - as rehash sets the number of
 
98
   * buckets, not the number of elements. BUT, it'll allow us to not need
 
99
   * to rehash later on as the unordered_map grows.
 
100
 */
 
101
  table_def_cache.rehash(table_def_size);
 
102
}
 
103
 
 
104
 
 
105
void TableShare::cacheStop(void)
 
106
{
 
107
  if (table_def_inited)
 
108
  {
 
109
    table_def_inited= false;
 
110
    pthread_mutex_destroy(&LOCK_table_share);
 
111
  }
 
112
}
 
113
 
 
114
 
 
115
/**
 
116
 * @TODO: This should return size_t
 
117
 */
 
118
uint32_t cached_table_definitions(void)
 
119
{
 
120
  return static_cast<uint32_t>(table_def_cache.size());
 
121
}
 
122
 
 
123
 
94
124
/*
95
125
  Mark that we are not using table share anymore.
96
126
 
106
136
void TableShare::release(TableShare *share)
107
137
{
108
138
  bool to_be_deleted= false;
109
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
110
 
 
111
 
  share->lock();
112
 
  if (!--share->ref_count)
113
 
  {
114
 
    to_be_deleted= true;
115
 
  }
116
 
  share->unlock();
117
 
 
118
 
  if (to_be_deleted)
119
 
  {
120
 
    definition::Cache::singleton().erase(share->getCacheKey());
121
 
  }
122
 
}
123
 
 
124
 
void TableShare::release(TableShare::shared_ptr &share)
125
 
{
126
 
  bool to_be_deleted= false;
127
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
128
 
 
129
 
  share->lock();
130
 
  if (!--share->ref_count)
131
 
  {
132
 
    to_be_deleted= true;
133
 
  }
134
 
  share->unlock();
135
 
 
136
 
  if (to_be_deleted)
137
 
  {
138
 
    definition::Cache::singleton().erase(share->getCacheKey());
139
 
  }
140
 
}
141
 
 
142
 
void TableShare::release(const TableIdentifier &identifier)
143
 
{
144
 
  TableShare::shared_ptr share= definition::Cache::singleton().find(identifier.getKey());
145
 
  if (share)
146
 
  {
 
139
  safe_mutex_assert_owner(&LOCK_open);
 
140
 
 
141
  pthread_mutex_lock(&share->mutex);
 
142
  if (!--share->ref_count)
 
143
  {
 
144
    to_be_deleted= true;
 
145
  }
 
146
 
 
147
  if (to_be_deleted)
 
148
  {
 
149
    const string key_string(share->getCacheKey(),
 
150
                            share->getCacheKeySize());
 
151
    plugin::EventObserver::deregisterTableEvents(*share);
 
152
   
 
153
    TableDefinitionCache::iterator iter= table_def_cache.find(key_string);
 
154
    if (iter != table_def_cache.end())
 
155
    {
 
156
      table_def_cache.erase(iter);
 
157
      delete share;
 
158
    }
 
159
    return;
 
160
  }
 
161
  pthread_mutex_unlock(&share->mutex);
 
162
}
 
163
 
 
164
void TableShare::release(const char *key, uint32_t key_length)
 
165
{
 
166
  const string key_string(key, key_length);
 
167
 
 
168
  TableDefinitionCache::iterator iter= table_def_cache.find(key_string);
 
169
  if (iter != table_def_cache.end())
 
170
  {
 
171
    TableShare *share= (*iter).second;
147
172
    share->version= 0;                          // Mark for delete
148
173
    if (share->ref_count == 0)
149
174
    {
150
 
      definition::Cache::singleton().erase(identifier.getKey());
 
175
      pthread_mutex_lock(&share->mutex);
 
176
      plugin::EventObserver::deregisterTableEvents(*share);
 
177
      table_def_cache.erase(key_string);
 
178
      delete share;
151
179
    }
152
180
  }
153
181
}
154
182
 
155
183
 
156
 
static TableShare::shared_ptr foundTableShare(TableShare::shared_ptr share)
 
184
static TableShare *foundTableShare(TableShare *share)
157
185
{
158
186
  /*
159
187
    We found an existing table definition. Return it if we didn't get
161
189
  */
162
190
 
163
191
  /* We must do a lock to ensure that the structure is initialized */
 
192
  (void) pthread_mutex_lock(&share->mutex);
164
193
  if (share->error)
165
194
  {
166
195
    /* Table definition contained an error */
167
196
    share->open_table_error(share->error, share->open_errno, share->errarg);
 
197
    (void) pthread_mutex_unlock(&share->mutex);
168
198
 
169
 
    return TableShare::shared_ptr();
 
199
    return NULL;
170
200
  }
171
201
 
172
 
  share->incrementTableCount();
 
202
  share->ref_count++;
 
203
  (void) pthread_mutex_unlock(&share->mutex);
173
204
 
174
205
  return share;
175
206
}
189
220
  If it doesn't exist, create a new from the table definition file.
190
221
 
191
222
  NOTES
192
 
  We must have wrlock on table::Cache::singleton().mutex() when we come here
 
223
  We must have wrlock on LOCK_open when we come here
193
224
  (To be changed later)
194
225
 
195
226
  RETURN
197
228
#  Share for table
198
229
*/
199
230
 
200
 
TableShare::shared_ptr TableShare::getShareCreate(Session *session, 
201
 
                                                  const TableIdentifier &identifier,
202
 
                                                  int &in_error)
 
231
TableShare *TableShare::getShare(Session *session, 
 
232
                                 char *key,
 
233
                                 uint32_t key_length, int *error)
203
234
{
204
 
  TableShare::shared_ptr share;
 
235
  const string key_string(key, key_length);
 
236
  TableShare *share= NULL;
205
237
 
206
 
  in_error= 0;
 
238
  *error= 0;
207
239
 
208
240
  /* Read table definition from cache */
209
 
  if ((share= definition::Cache::singleton().find(identifier.getKey())))
 
241
  TableDefinitionCache::iterator iter= table_def_cache.find(key_string);
 
242
  if (iter != table_def_cache.end())
 
243
  {
 
244
    share= (*iter).second;
210
245
    return foundTableShare(share);
211
 
 
212
 
  share.reset(new TableShare(message::Table::STANDARD, identifier));
213
 
  
 
246
  }
 
247
 
 
248
  if (not (share= new TableShare(key, key_length)))
 
249
  {
 
250
    return NULL;
 
251
  }
 
252
 
 
253
  /*
 
254
    Lock mutex to be able to read table definition from file without
 
255
    conflicts
 
256
  */
 
257
  (void) pthread_mutex_lock(&share->mutex);
 
258
 
 
259
  /**
 
260
   * @TODO: we need to eject something if we exceed table_def_size
 
261
 */
 
262
  pair<TableDefinitionCache::iterator, bool> ret=
 
263
    table_def_cache.insert(make_pair(key_string, share));
 
264
  if (ret.second == false)
 
265
  {
 
266
    delete share;
 
267
 
 
268
    return NULL;
 
269
  }
 
270
 
 
271
  TableIdentifier identifier(share->getSchemaName(), share->getTableName());
214
272
  if (share->open_table_def(*session, identifier))
215
273
  {
216
 
    in_error= share->error;
 
274
    *error= share->error;
 
275
    table_def_cache.erase(key_string);
 
276
    delete share;
217
277
 
218
 
    return TableShare::shared_ptr();
 
278
    return NULL;
219
279
  }
220
280
  share->ref_count++;                           // Mark in use
221
281
  
222
282
  plugin::EventObserver::registerTableEvents(*share);
223
 
 
224
 
  bool ret= definition::Cache::singleton().insert(identifier.getKey(), share);
225
 
 
226
 
  if (not ret)
227
 
    return TableShare::shared_ptr();
 
283
  
 
284
  (void) pthread_mutex_unlock(&share->mutex);
228
285
 
229
286
  return share;
230
287
}
231
288
 
 
289
 
 
290
/*
 
291
  Check if table definition exits in cache
 
292
 
 
293
  SYNOPSIS
 
294
  get_cached_table_share()
 
295
  db                    Database name
 
296
  table_name            Table name
 
297
 
 
298
  RETURN
 
299
  0  Not cached
 
300
#  TableShare for table
 
301
*/
 
302
TableShare *TableShare::getShare(TableIdentifier &identifier)
 
303
{
 
304
  char key[MAX_DBKEY_LENGTH];
 
305
  uint32_t key_length;
 
306
  safe_mutex_assert_owner(&LOCK_open);
 
307
 
 
308
  key_length= TableShare::createKey(key, identifier);
 
309
 
 
310
  const string key_string(key, key_length);
 
311
 
 
312
  TableDefinitionCache::iterator iter= table_def_cache.find(key_string);
 
313
  if (iter != table_def_cache.end())
 
314
  {
 
315
    return (*iter).second;
 
316
  }
 
317
  else
 
318
  {
 
319
    return NULL;
 
320
  }
 
321
}
 
322
 
 
323
/* Get column name from column hash */
 
324
 
 
325
static unsigned char *get_field_name(Field **buff, size_t *length, bool)
 
326
{
 
327
  *length= (uint32_t) strlen((*buff)->field_name);
 
328
  return (unsigned char*) (*buff)->field_name;
 
329
}
 
330
 
232
331
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
233
332
{
234
333
  enum_field_types field_type;
265
364
  case message::Table::Field::BLOB:
266
365
    field_type= DRIZZLE_TYPE_BLOB;
267
366
    break;
268
 
  case message::Table::Field::UUID:
269
 
    field_type= DRIZZLE_TYPE_UUID;
270
 
    break;
271
367
  default:
272
 
    assert(0);
273
 
    abort(); // Programming error
 
368
    field_type= DRIZZLE_TYPE_LONG; /* Set value to kill GCC warning */
 
369
    assert(1);
274
370
  }
275
371
 
276
372
  return field_type;
304
400
                                 default_value->length());
305
401
    break;
306
402
  case DRIZZLE_TYPE_NULL:
307
 
    assert(0);
308
 
    abort();
 
403
    assert(false);
309
404
  case DRIZZLE_TYPE_TIMESTAMP:
310
405
  case DRIZZLE_TYPE_DATETIME:
311
406
  case DRIZZLE_TYPE_DATE:
 
407
    if (default_value->compare("NOW()") == 0)
 
408
      break;
312
409
  case DRIZZLE_TYPE_ENUM:
313
 
  case DRIZZLE_TYPE_UUID:
314
410
    default_item= new Item_string(default_value->c_str(),
315
411
                                  default_value->length(),
316
412
                                  system_charset_info);
361
457
      size_t num_parts= index.index_part_size();
362
458
      for (size_t y= 0; y < num_parts; ++y)
363
459
      {
364
 
        if (index.index_part(y).fieldnr() == in_field->position())
 
460
        if (index.index_part(y).fieldnr() == in_field->field_index)
365
461
          return true;
366
462
      }
367
463
    }
369
465
  return false;
370
466
}
371
467
 
372
 
TableShare::TableShare(const TableIdentifier::Type type_arg) :
373
 
  table_category(TABLE_UNKNOWN_CATEGORY),
374
 
  found_next_number_field(NULL),
375
 
  timestamp_field(NULL),
376
 
  key_info(NULL),
377
 
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
378
 
  all_set(),
379
 
  block_size(0),
380
 
  version(0),
381
 
  timestamp_offset(0),
382
 
  reclength(0),
383
 
  stored_rec_length(0),
384
 
  max_rows(0),
385
 
  table_proto(NULL),
386
 
  storage_engine(NULL),
387
 
  tmp_table(type_arg),
388
 
  ref_count(0),
389
 
  null_bytes(0),
390
 
  last_null_bit_pos(0),
391
 
  fields(0),
392
 
  rec_buff_length(0),
393
 
  keys(0),
394
 
  key_parts(0),
395
 
  max_key_length(0),
396
 
  max_unique_length(0),
397
 
  total_key_length(0),
398
 
  uniques(0),
399
 
  null_fields(0),
400
 
  blob_fields(0),
401
 
  has_variable_width(false),
402
 
  db_create_options(0),
403
 
  db_options_in_use(0),
404
 
  db_record_offset(0),
405
 
  rowid_field_offset(0),
406
 
  primary_key(MAX_KEY),
407
 
  next_number_index(0),
408
 
  next_number_key_offset(0),
409
 
  next_number_keypart(0),
410
 
  error(0),
411
 
  open_errno(0),
412
 
  errarg(0),
413
 
  blob_ptr_size(0),
414
 
  db_low_byte_first(false),
415
 
  keys_in_use(0),
416
 
  keys_for_keyread(0),
417
 
  event_observers(NULL)
418
 
{
419
 
 
420
 
  table_charset= 0;
421
 
  memset(&db, 0, sizeof(LEX_STRING));
422
 
  memset(&table_name, 0, sizeof(LEX_STRING));
423
 
  memset(&path, 0, sizeof(LEX_STRING));
424
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
425
 
 
426
 
  if (type_arg == message::Table::INTERNAL)
427
 
  {
428
 
    TableIdentifier::build_tmptable_filename(private_key_for_cache.vectorPtr());
429
 
    init(private_key_for_cache.vector(), private_key_for_cache.vector());
430
 
  }
431
 
  else
432
 
  {
433
 
    init("", "");
434
 
  }
435
 
}
436
 
 
437
 
TableShare::TableShare(const TableIdentifier &identifier, const TableIdentifier::Key &key) :// Used by placeholder
438
 
  table_category(TABLE_UNKNOWN_CATEGORY),
439
 
  found_next_number_field(NULL),
440
 
  timestamp_field(NULL),
441
 
  key_info(NULL),
442
 
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
443
 
  all_set(),
444
 
  block_size(0),
445
 
  version(0),
446
 
  timestamp_offset(0),
447
 
  reclength(0),
448
 
  stored_rec_length(0),
449
 
  max_rows(0),
450
 
  table_proto(NULL),
451
 
  storage_engine(NULL),
452
 
  tmp_table(message::Table::INTERNAL),
453
 
  ref_count(0),
454
 
  null_bytes(0),
455
 
  last_null_bit_pos(0),
456
 
  fields(0),
457
 
  rec_buff_length(0),
458
 
  keys(0),
459
 
  key_parts(0),
460
 
  max_key_length(0),
461
 
  max_unique_length(0),
462
 
  total_key_length(0),
463
 
  uniques(0),
464
 
  null_fields(0),
465
 
  blob_fields(0),
466
 
  has_variable_width(false),
467
 
  db_create_options(0),
468
 
  db_options_in_use(0),
469
 
  db_record_offset(0),
470
 
  rowid_field_offset(0),
471
 
  primary_key(MAX_KEY),
472
 
  next_number_index(0),
473
 
  next_number_key_offset(0),
474
 
  next_number_keypart(0),
475
 
  error(0),
476
 
  open_errno(0),
477
 
  errarg(0),
478
 
  blob_ptr_size(0),
479
 
  db_low_byte_first(false),
480
 
  keys_in_use(0),
481
 
  keys_for_keyread(0),
482
 
  event_observers(NULL)
483
 
{
484
 
  assert(identifier.getKey() == key);
485
 
 
486
 
  table_charset= 0;
487
 
  memset(&path, 0, sizeof(LEX_STRING));
488
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
489
 
 
490
 
  private_key_for_cache= key;
491
 
 
492
 
  table_category=         TABLE_CATEGORY_TEMPORARY;
493
 
  tmp_table=              message::Table::INTERNAL;
494
 
 
495
 
  db.str= const_cast<char *>(private_key_for_cache.vector());
496
 
  db.length= strlen(private_key_for_cache.vector());
497
 
 
498
 
  table_name.str= const_cast<char *>(private_key_for_cache.vector()) + strlen(private_key_for_cache.vector()) + 1;
499
 
  table_name.length= strlen(table_name.str);
500
 
  path.str= (char *)"";
501
 
  normalized_path.str= path.str;
502
 
  path.length= normalized_path.length= 0;
503
 
 
504
 
  std::string tb_name(identifier.getTableName());
505
 
  std::transform(tb_name.begin(), tb_name.end(), tb_name.begin(), ::tolower);
506
 
  assert(strcmp(tb_name.c_str(), table_name.str) == 0);
507
 
 
508
 
  assert(strcmp(identifier.getSchemaName().c_str(), db.str) == 0);
509
 
}
510
 
 
511
 
 
512
 
TableShare::TableShare(const TableIdentifier &identifier) : // Just used during createTable()
513
 
  table_category(TABLE_UNKNOWN_CATEGORY),
514
 
  found_next_number_field(NULL),
515
 
  timestamp_field(NULL),
516
 
  key_info(NULL),
517
 
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
518
 
  all_set(),
519
 
  block_size(0),
520
 
  version(0),
521
 
  timestamp_offset(0),
522
 
  reclength(0),
523
 
  stored_rec_length(0),
524
 
  max_rows(0),
525
 
  table_proto(NULL),
526
 
  storage_engine(NULL),
527
 
  tmp_table(identifier.getType()),
528
 
  ref_count(0),
529
 
  null_bytes(0),
530
 
  last_null_bit_pos(0),
531
 
  fields(0),
532
 
  rec_buff_length(0),
533
 
  keys(0),
534
 
  key_parts(0),
535
 
  max_key_length(0),
536
 
  max_unique_length(0),
537
 
  total_key_length(0),
538
 
  uniques(0),
539
 
  null_fields(0),
540
 
  blob_fields(0),
541
 
  has_variable_width(false),
542
 
  db_create_options(0),
543
 
  db_options_in_use(0),
544
 
  db_record_offset(0),
545
 
  rowid_field_offset(0),
546
 
  primary_key(MAX_KEY),
547
 
  next_number_index(0),
548
 
  next_number_key_offset(0),
549
 
  next_number_keypart(0),
550
 
  error(0),
551
 
  open_errno(0),
552
 
  errarg(0),
553
 
  blob_ptr_size(0),
554
 
  db_low_byte_first(false),
555
 
  keys_in_use(0),
556
 
  keys_for_keyread(0),
557
 
  event_observers(NULL)
558
 
{
559
 
  table_charset= 0;
560
 
  memset(&db, 0, sizeof(LEX_STRING));
561
 
  memset(&table_name, 0, sizeof(LEX_STRING));
562
 
  memset(&path, 0, sizeof(LEX_STRING));
563
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
564
 
 
565
 
  private_key_for_cache= identifier.getKey();
566
 
  assert(identifier.getPath().size()); // Since we are doing a create table, this should be a positive value
567
 
  private_normalized_path.resize(identifier.getPath().size() + 1);
568
 
  memcpy(&private_normalized_path[0], identifier.getPath().c_str(), identifier.getPath().size());
569
 
 
570
 
  {
571
 
    table_category=         TABLE_CATEGORY_TEMPORARY;
572
 
    tmp_table=              message::Table::INTERNAL;
573
 
    db.str= const_cast<char *>(private_key_for_cache.vector());
574
 
    db.length= strlen(private_key_for_cache.vector());
575
 
    table_name.str= db.str + 1;
576
 
    table_name.length= strlen(table_name.str);
577
 
    path.str= &private_normalized_path[0];
578
 
    normalized_path.str= path.str;
579
 
    path.length= normalized_path.length= private_normalized_path.size();
580
 
  }
581
 
}
582
 
 
583
 
 
584
 
/*
585
 
  Used for shares that will go into the cache.
586
 
*/
587
 
TableShare::TableShare(const TableIdentifier::Type type_arg,
588
 
                       const TableIdentifier &identifier,
589
 
                       char *path_arg,
590
 
                       uint32_t path_length_arg) :
591
 
  table_category(TABLE_UNKNOWN_CATEGORY),
592
 
  found_next_number_field(NULL),
593
 
  timestamp_field(NULL),
594
 
  key_info(NULL),
595
 
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
596
 
  all_set(),
597
 
  block_size(0),
598
 
  version(0),
599
 
  timestamp_offset(0),
600
 
  reclength(0),
601
 
  stored_rec_length(0),
602
 
  max_rows(0),
603
 
  table_proto(NULL),
604
 
  storage_engine(NULL),
605
 
  tmp_table(type_arg),
606
 
  ref_count(0),
607
 
  null_bytes(0),
608
 
  last_null_bit_pos(0),
609
 
  fields(0),
610
 
  rec_buff_length(0),
611
 
  keys(0),
612
 
  key_parts(0),
613
 
  max_key_length(0),
614
 
  max_unique_length(0),
615
 
  total_key_length(0),
616
 
  uniques(0),
617
 
  null_fields(0),
618
 
  blob_fields(0),
619
 
  has_variable_width(false),
620
 
  db_create_options(0),
621
 
  db_options_in_use(0),
622
 
  db_record_offset(0),
623
 
  rowid_field_offset(0),
624
 
  primary_key(MAX_KEY),
625
 
  next_number_index(0),
626
 
  next_number_key_offset(0),
627
 
  next_number_keypart(0),
628
 
  error(0),
629
 
  open_errno(0),
630
 
  errarg(0),
631
 
  blob_ptr_size(0),
632
 
  db_low_byte_first(false),
633
 
  keys_in_use(0),
634
 
  keys_for_keyread(0),
635
 
  event_observers(NULL)
636
 
{
637
 
  table_charset= 0;
638
 
  memset(&db, 0, sizeof(LEX_STRING));
639
 
  memset(&table_name, 0, sizeof(LEX_STRING));
640
 
  memset(&path, 0, sizeof(LEX_STRING));
641
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
642
 
 
643
 
  char *path_buff;
 
468
TableDefinitionCache &TableShare::getCache()
 
469
{
 
470
  return table_def_cache;
 
471
}
 
472
 
 
473
TableShare::TableShare(char *key, uint32_t key_length, char *path_arg, uint32_t path_length_arg) :
 
474
  table_category(TABLE_UNKNOWN_CATEGORY),
 
475
  open_count(0),
 
476
  field(NULL),
 
477
  found_next_number_field(NULL),
 
478
  timestamp_field(NULL),
 
479
  key_info(NULL),
 
480
  blob_field(NULL),
 
481
  intervals(NULL),
 
482
  default_values(NULL),
 
483
  block_size(0),
 
484
  version(0),
 
485
  timestamp_offset(0),
 
486
  reclength(0),
 
487
  stored_rec_length(0),
 
488
  row_type(ROW_TYPE_DEFAULT),
 
489
  max_rows(0),
 
490
  table_proto(NULL),
 
491
  storage_engine(NULL),
 
492
  tmp_table(message::Table::STANDARD),
 
493
  ref_count(0),
 
494
  null_bytes(0),
 
495
  last_null_bit_pos(0),
 
496
  fields(0),
 
497
  rec_buff_length(0),
 
498
  keys(0),
 
499
  key_parts(0),
 
500
  max_key_length(0),
 
501
  max_unique_length(0),
 
502
  total_key_length(0),
 
503
  uniques(0),
 
504
  null_fields(0),
 
505
  blob_fields(0),
 
506
  timestamp_field_offset(0),
 
507
  varchar_fields(0),
 
508
  db_create_options(0),
 
509
  db_options_in_use(0),
 
510
  db_record_offset(0),
 
511
  rowid_field_offset(0),
 
512
  primary_key(0),
 
513
  next_number_index(0),
 
514
  next_number_key_offset(0),
 
515
  next_number_keypart(0),
 
516
  error(0),
 
517
  open_errno(0),
 
518
  errarg(0),
 
519
  column_bitmap_size(0),
 
520
  blob_ptr_size(0),
 
521
  db_low_byte_first(false),
 
522
  name_lock(false),
 
523
  replace_with_name_lock(false),
 
524
  waiting_on_cond(false),
 
525
  keys_in_use(0),
 
526
  keys_for_keyread(0),
 
527
  newed(true)
 
528
{
 
529
  memset(&name_hash, 0, sizeof(HASH));
 
530
 
 
531
  table_charset= 0;
 
532
  memset(&all_set, 0, sizeof (MyBitmap));
 
533
  memset(&table_cache_key, 0, sizeof(LEX_STRING));
 
534
  memset(&db, 0, sizeof(LEX_STRING));
 
535
  memset(&table_name, 0, sizeof(LEX_STRING));
 
536
  memset(&path, 0, sizeof(LEX_STRING));
 
537
  memset(&normalized_path, 0, sizeof(LEX_STRING));
 
538
 
 
539
  mem_root.init_alloc_root(TABLE_ALLOC_BLOCK_SIZE);
 
540
  char *key_buff, *path_buff;
644
541
  std::string _path;
645
542
 
646
 
  private_key_for_cache= identifier.getKey();
647
 
  /*
648
 
    Let us use the fact that the key is "db/0/table_name/0" + optional
649
 
    part for temporary tables.
650
 
  */
651
 
  db.str= const_cast<char *>(private_key_for_cache.vector());
652
 
  db.length=         strlen(db.str);
653
 
  table_name.str=    db.str + db.length + 1;
 
543
  db.str= key;
 
544
  db.length= strlen(db.str);
 
545
  table_name.str= db.str + db.length + 1;
654
546
  table_name.length= strlen(table_name.str);
655
547
 
656
548
  if (path_arg)
659
551
  }
660
552
  else
661
553
  {
662
 
    TableIdentifier::build_table_filename(_path, db.str, table_name.str, false);
 
554
    build_table_filename(_path, db.str, table_name.str, false);
663
555
  }
664
556
 
665
 
  if ((path_buff= (char *)mem_root.alloc_root(_path.length() + 1)))
 
557
  if (mem_root.multi_alloc_root(0, &key_buff, key_length,
 
558
                                &path_buff, _path.length() + 1,
 
559
                                NULL))
666
560
  {
 
561
    memcpy(key_buff, key, key_length);
 
562
    set_table_cache_key(key_buff, key_length, db.length, table_name.length);
 
563
 
667
564
    setPath(path_buff, _path.length());
668
565
    strcpy(path_buff, _path.c_str());
669
566
    setNormalizedPath(path_buff, _path.length());
670
567
 
671
568
    version= refresh_version;
 
569
 
 
570
    pthread_mutex_init(&mutex, MY_MUTEX_INIT_FAST);
 
571
    pthread_cond_init(&cond, NULL);
672
572
  }
673
573
  else
674
574
  {
675
575
    assert(0); // We should throw here.
676
 
    abort();
677
576
  }
678
 
}
679
 
 
680
 
void TableShare::init(const char *new_table_name,
681
 
                      const char *new_path)
682
 
{
683
 
 
684
 
  table_category=         TABLE_CATEGORY_TEMPORARY;
685
 
  tmp_table=              message::Table::INTERNAL;
686
 
  db.str= (char *)"";
687
 
  db.length= 0;
688
 
  table_name.str=         (char*) new_table_name;
689
 
  table_name.length=      strlen(new_table_name);
690
 
  path.str=               (char*) new_path;
691
 
  normalized_path.str=    (char*) new_path;
692
 
  path.length= normalized_path.length= strlen(new_path);
693
 
}
694
 
 
695
 
TableShare::~TableShare() 
696
 
{
697
 
  assert(ref_count == 0);
698
 
 
699
 
  storage_engine= NULL;
700
 
 
701
 
  delete table_proto;
702
 
  table_proto= NULL;
703
 
 
704
 
  plugin::EventObserver::deregisterTableEvents(*this);
705
 
 
706
 
  mem_root.free_root(MYF(0));                 // Free's share
707
 
}
708
 
 
709
 
void TableShare::setIdentifier(const TableIdentifier &identifier_arg)
710
 
{
711
 
  private_key_for_cache= identifier_arg.getKey();
712
 
 
713
 
  /*
714
 
    Let us use the fact that the key is "db/0/table_name/0" + optional
715
 
    part for temporary tables.
716
 
  */
717
 
  db.str= const_cast<char *>(private_key_for_cache.vector());
718
 
  db.length=         strlen(db.str);
719
 
  table_name.str=    db.str + db.length + 1;
720
 
  table_name.length= strlen(table_name.str);
721
 
 
722
 
  table_proto->set_name(identifier_arg.getTableName());
723
 
  table_proto->set_schema(identifier_arg.getSchemaName());
 
577
 
 
578
  newed= true;
724
579
}
725
580
 
726
581
int TableShare::inner_parse_table_proto(Session& session, message::Table &table)
727
582
{
 
583
  TableShare *share= this;
728
584
  int local_error= 0;
729
585
 
730
586
  if (! table.IsInitialized())
733
589
    return ER_CORRUPT_TABLE_DEFINITION;
734
590
  }
735
591
 
736
 
  setTableProto(new(nothrow) message::Table(table));
 
592
  share->setTableProto(new(nothrow) message::Table(table));
737
593
 
738
 
  storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
739
 
  assert(storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
 
594
  share->storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
 
595
  assert(share->storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
740
596
 
741
597
  message::Table::TableOptions table_options;
742
598
 
751
607
  /* local_db_create_options was stored as 2 bytes in FRM
752
608
    Any HA_OPTION_ that doesn't fit into 2 bytes was silently truncated away.
753
609
  */
754
 
  db_create_options= (local_db_create_options & 0x0000FFFF);
755
 
  db_options_in_use= db_create_options;
756
 
 
757
 
  block_size= table_options.has_block_size() ?
 
610
  share->db_create_options= (local_db_create_options & 0x0000FFFF);
 
611
  share->db_options_in_use= share->db_create_options;
 
612
 
 
613
  share->row_type= table_options.has_row_type() ?
 
614
    (enum row_type) table_options.row_type() : ROW_TYPE_DEFAULT;
 
615
 
 
616
  share->block_size= table_options.has_block_size() ?
758
617
    table_options.block_size() : 0;
759
618
 
760
 
  table_charset= get_charset(table_options.collation_id());
 
619
  share->table_charset= get_charset(table_options.has_collation_id()?
 
620
                                    table_options.collation_id() : 0);
761
621
 
762
 
  if (! table_charset)
 
622
  if (!share->table_charset)
763
623
  {
764
 
    char errmsg[100];
765
 
    snprintf(errmsg, sizeof(errmsg),
766
 
             _("Table %s has invalid/unknown collation: %d,%s"),
767
 
             getPath(),
768
 
             table_options.collation_id(),
769
 
             table_options.collation().c_str());
770
 
    errmsg[99]='\0';
771
 
 
772
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
773
 
    return ER_CORRUPT_TABLE_DEFINITION;
 
624
    /* unknown charset in head[38] or pre-3.23 frm */
 
625
    if (use_mb(default_charset_info))
 
626
    {
 
627
      /* Warn that we may be changing the size of character columns */
 
628
      errmsg_printf(ERRMSG_LVL_WARN,
 
629
                    _("'%s' had no or invalid character set, "
 
630
                      "and default character set is multi-byte, "
 
631
                      "so character column sizes may have changed"),
 
632
                    share->getPath());
 
633
    }
 
634
    share->table_charset= default_charset_info;
774
635
  }
775
636
 
776
 
  db_record_offset= 1;
777
 
 
778
 
  blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
779
 
 
780
 
  keys= table.indexes_size();
781
 
 
782
 
  key_parts= 0;
 
637
  share->db_record_offset= 1;
 
638
 
 
639
  share->blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
 
640
 
 
641
  share->keys= table.indexes_size();
 
642
 
 
643
  share->key_parts= 0;
783
644
  for (int indx= 0; indx < table.indexes_size(); indx++)
784
 
    key_parts+= table.indexes(indx).index_part_size();
 
645
    share->key_parts+= table.indexes(indx).index_part_size();
785
646
 
786
 
  key_info= (KeyInfo*) alloc_root( table.indexes_size() * sizeof(KeyInfo) +key_parts*sizeof(KeyPartInfo));
 
647
  share->key_info= (KeyInfo*) share->alloc_root( table.indexes_size() * sizeof(KeyInfo) +share->key_parts*sizeof(KeyPartInfo));
787
648
 
788
649
  KeyPartInfo *key_part;
789
650
 
790
651
  key_part= reinterpret_cast<KeyPartInfo*>
791
 
    (key_info+table.indexes_size());
792
 
 
793
 
 
794
 
  ulong *rec_per_key= (ulong*) alloc_root(sizeof(ulong*)*key_parts);
795
 
 
796
 
  KeyInfo* keyinfo= key_info;
 
652
    (share->key_info+table.indexes_size());
 
653
 
 
654
 
 
655
  ulong *rec_per_key= (ulong*) share->alloc_root(sizeof(ulong*)*share->key_parts);
 
656
 
 
657
  KeyInfo* keyinfo= share->key_info;
797
658
  for (int keynr= 0; keynr < table.indexes_size(); keynr++, keyinfo++)
798
659
  {
799
660
    message::Table::Index indx= table.indexes(keynr);
882
743
 
883
744
      key_part->length= part.compare_length();
884
745
 
885
 
      int mbmaxlen= 1;
886
 
 
887
 
      if (table.field(part.fieldnr()).type() == message::Table::Field::VARCHAR
888
 
          || table.field(part.fieldnr()).type() == message::Table::Field::BLOB)
889
 
      {
890
 
        uint32_t collation_id;
891
 
 
892
 
        if (table.field(part.fieldnr()).string_options().has_collation_id())
893
 
          collation_id= table.field(part.fieldnr()).string_options().collation_id();
894
 
        else
895
 
          collation_id= table.options().collation_id();
896
 
 
897
 
        const CHARSET_INFO *cs= get_charset(collation_id);
898
 
 
899
 
        mbmaxlen= cs->mbmaxlen;
900
 
      }
901
 
      key_part->length*= mbmaxlen;
902
 
 
903
746
      key_part->store_length= key_part->length;
904
747
 
905
748
      /* key_part->offset is set later */
906
 
      key_part->key_type= 0;
 
749
      key_part->key_type= part.key_type();
907
750
    }
908
751
 
909
752
    if (! indx.has_comment())
915
758
    {
916
759
      keyinfo->flags|= HA_USES_COMMENT;
917
760
      keyinfo->comment.length= indx.comment().length();
918
 
      keyinfo->comment.str= strmake_root(indx.comment().c_str(), keyinfo->comment.length);
 
761
      keyinfo->comment.str= share->strmake_root(indx.comment().c_str(), keyinfo->comment.length);
919
762
    }
920
763
 
921
 
    keyinfo->name= strmake_root(indx.name().c_str(), indx.name().length());
 
764
    keyinfo->name= share->strmake_root(indx.name().c_str(), indx.name().length());
922
765
 
923
766
    addKeyName(string(keyinfo->name, indx.name().length()));
924
767
  }
925
768
 
926
 
  keys_for_keyread.reset();
927
 
  set_prefix(keys_in_use, keys);
928
 
 
929
 
  fields= table.field_size();
930
 
 
931
 
  setFields(fields + 1);
932
 
  field[fields]= NULL;
 
769
  share->keys_for_keyread.reset();
 
770
  set_prefix(share->keys_in_use, share->keys);
 
771
 
 
772
  share->fields= table.field_size();
 
773
 
 
774
  share->field= (Field**) share->alloc_root(((share->fields+1) * sizeof(Field*)));
 
775
  share->field[share->fields]= NULL;
933
776
 
934
777
  uint32_t local_null_fields= 0;
935
 
  reclength= 0;
936
 
 
937
 
  std::vector<uint32_t> field_offsets;
938
 
  std::vector<uint32_t> field_pack_length;
939
 
 
940
 
  field_offsets.resize(fields);
941
 
  field_pack_length.resize(fields);
 
778
  share->reclength= 0;
 
779
 
 
780
  vector<uint32_t> field_offsets;
 
781
  vector<uint32_t> field_pack_length;
 
782
 
 
783
  field_offsets.resize(share->fields);
 
784
  field_pack_length.resize(share->fields);
942
785
 
943
786
  uint32_t interval_count= 0;
944
787
  uint32_t interval_parts= 0;
945
788
 
946
789
  uint32_t stored_columns_reclength= 0;
947
790
 
948
 
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
 
791
  for (unsigned int fieldnr= 0; fieldnr < share->fields; fieldnr++)
949
792
  {
950
793
    message::Table::Field pfield= table.field(fieldnr);
951
794
    if (pfield.constraints().is_nullable())
981
824
      {
982
825
        message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
983
826
 
984
 
        field_pack_length[fieldnr]= 4;
 
827
        field_pack_length[fieldnr]=
 
828
          get_enum_pack_length(field_options.field_value_size());
985
829
 
986
830
        interval_count++;
987
831
        interval_parts+= field_options.field_value_size();
999
843
      field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type, 0);
1000
844
    }
1001
845
 
1002
 
    reclength+= field_pack_length[fieldnr];
 
846
    share->reclength+= field_pack_length[fieldnr];
1003
847
    stored_columns_reclength+= field_pack_length[fieldnr];
1004
848
  }
1005
849
 
1006
850
  /* data_offset added to stored_rec_length later */
1007
 
  stored_rec_length= stored_columns_reclength;
 
851
  share->stored_rec_length= stored_columns_reclength;
1008
852
 
1009
 
  null_fields= local_null_fields;
 
853
  share->null_fields= local_null_fields;
1010
854
 
1011
855
  ulong null_bits= local_null_fields;
1012
856
  if (! table_options.pack_record())
1014
858
  ulong data_offset= (null_bits + 7)/8;
1015
859
 
1016
860
 
1017
 
  reclength+= data_offset;
1018
 
  stored_rec_length+= data_offset;
 
861
  share->reclength+= data_offset;
 
862
  share->stored_rec_length+= data_offset;
1019
863
 
1020
864
  ulong local_rec_buff_length;
1021
865
 
1022
 
  local_rec_buff_length= ALIGN_SIZE(reclength + 1);
1023
 
  rec_buff_length= local_rec_buff_length;
1024
 
 
1025
 
  resizeDefaultValues(local_rec_buff_length);
1026
 
  unsigned char* record= getDefaultValues();
 
866
  local_rec_buff_length= ALIGN_SIZE(share->reclength + 1);
 
867
  share->rec_buff_length= local_rec_buff_length;
 
868
 
 
869
  unsigned char* record= NULL;
 
870
 
 
871
  if (! (record= (unsigned char *) share->alloc_root(local_rec_buff_length)))
 
872
    abort();
 
873
 
 
874
  memset(record, 0, local_rec_buff_length);
 
875
 
1027
876
  int null_count= 0;
1028
877
 
1029
878
  if (! table_options.pack_record())
1032
881
    *record|= 1;
1033
882
  }
1034
883
 
 
884
  share->default_values= record;
1035
885
 
1036
 
  intervals.resize(interval_count);
 
886
  if (interval_count)
 
887
  {
 
888
    share->intervals= (TYPELIB *) share->alloc_root(interval_count*sizeof(TYPELIB));
 
889
  }
 
890
  else
 
891
  {
 
892
    share->intervals= NULL;
 
893
  }
1037
894
 
1038
895
  /* Now fix the TYPELIBs for the intervals (enum values)
1039
896
    and field names.
1041
898
 
1042
899
  uint32_t interval_nr= 0;
1043
900
 
1044
 
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
 
901
  for (unsigned int fieldnr= 0; fieldnr < share->fields; fieldnr++)
1045
902
  {
1046
903
    message::Table::Field pfield= table.field(fieldnr);
1047
904
 
1051
908
 
1052
909
    message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
1053
910
 
1054
 
    if (field_options.field_value_size() > Field_enum::max_supported_elements)
1055
 
    {
1056
 
      char errmsg[100];
1057
 
      snprintf(errmsg, sizeof(errmsg),
1058
 
               _("ENUM column %s has greater than %d possible values"),
1059
 
               pfield.name().c_str(),
1060
 
               Field_enum::max_supported_elements);
1061
 
      errmsg[99]='\0';
1062
 
 
1063
 
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
1064
 
      return ER_CORRUPT_TABLE_DEFINITION;
1065
 
    }
1066
 
 
1067
 
 
1068
911
    const CHARSET_INFO *charset= get_charset(field_options.has_collation_id() ?
1069
912
                                             field_options.collation_id() : 0);
1070
913
 
1071
914
    if (! charset)
1072
915
      charset= default_charset_info;
1073
916
 
1074
 
    TYPELIB *t= (&intervals[interval_nr]);
1075
 
 
1076
 
    t->type_names= (const char**)alloc_root((field_options.field_value_size() + 1) * sizeof(char*));
1077
 
 
1078
 
    t->type_lengths= (unsigned int*) alloc_root((field_options.field_value_size() + 1) * sizeof(unsigned int));
 
917
    TYPELIB *t= &(share->intervals[interval_nr]);
 
918
 
 
919
    t->type_names= (const char**)share->alloc_root((field_options.field_value_size() + 1) * sizeof(char*));
 
920
 
 
921
    t->type_lengths= (unsigned int*) share->alloc_root((field_options.field_value_size() + 1) * sizeof(unsigned int));
1079
922
 
1080
923
    t->type_names[field_options.field_value_size()]= NULL;
1081
924
    t->type_lengths[field_options.field_value_size()]= 0;
1085
928
 
1086
929
    for (int n= 0; n < field_options.field_value_size(); n++)
1087
930
    {
1088
 
      t->type_names[n]= strmake_root(field_options.field_value(n).c_str(), field_options.field_value(n).length());
 
931
      t->type_names[n]= share->strmake_root(field_options.field_value(n).c_str(), field_options.field_value(n).length());
1089
932
 
1090
933
      /* 
1091
934
       * Go ask the charset what the length is as for "" length=1
1104
947
  /* and read the fields */
1105
948
  interval_nr= 0;
1106
949
 
1107
 
  bool use_hash= fields >= MAX_FIELDS_BEFORE_HASH;
1108
 
 
1109
 
  unsigned char* null_pos= getDefaultValues();
 
950
  bool use_hash= share->fields >= MAX_FIELDS_BEFORE_HASH;
 
951
 
 
952
  if (use_hash)
 
953
    use_hash= ! hash_init(&share->name_hash,
 
954
                          system_charset_info,
 
955
                          share->fields,
 
956
                          0,
 
957
                          0,
 
958
                          (hash_get_key) get_field_name,
 
959
                          0,
 
960
                          0);
 
961
 
 
962
  unsigned char* null_pos= record;;
1110
963
  int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
1111
964
 
1112
 
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
 
965
  for (unsigned int fieldnr= 0; fieldnr < share->fields; fieldnr++)
1113
966
  {
1114
967
    message::Table::Field pfield= table.field(fieldnr);
1115
968
 
1122
975
    }
1123
976
 
1124
977
    if (pfield.has_options() &&
1125
 
        pfield.options().has_default_expression() &&
1126
 
        pfield.options().default_expression().compare("CURRENT_TIMESTAMP") == 0)
 
978
        pfield.options().has_default_value() &&
 
979
        pfield.options().default_value().compare("NOW()") == 0)
1127
980
    {
1128
 
      if (pfield.options().has_update_expression() &&
1129
 
          pfield.options().update_expression().compare("CURRENT_TIMESTAMP") == 0)
 
981
      if (pfield.options().has_update_value() &&
 
982
          pfield.options().update_value().compare("NOW()") == 0)
1130
983
      {
1131
984
        unireg_type= Field::TIMESTAMP_DNUN_FIELD;
1132
985
      }
1133
 
      else if (! pfield.options().has_update_expression())
 
986
      else if (! pfield.options().has_update_value())
1134
987
      {
1135
988
        unireg_type= Field::TIMESTAMP_DN_FIELD;
1136
989
      }
1137
990
      else
1138
 
      {
1139
 
        assert(0); // Invalid update value.
1140
 
        abort();
1141
 
      }
 
991
        assert(1); // Invalid update value.
1142
992
    }
1143
993
    else if (pfield.has_options() &&
1144
 
             pfield.options().has_update_expression() &&
1145
 
             pfield.options().update_expression().compare("CURRENT_TIMESTAMP") == 0)
 
994
             pfield.options().has_update_value() &&
 
995
             pfield.options().update_value().compare("NOW()") == 0)
1146
996
    {
1147
997
      unireg_type= Field::TIMESTAMP_UN_FIELD;
1148
998
    }
1158
1008
      size_t len= pfield.comment().length();
1159
1009
      const char* str= pfield.comment().c_str();
1160
1010
 
1161
 
      comment.str= strmake_root(str, len);
 
1011
      comment.str= share->strmake_root(str, len);
1162
1012
      comment.length= len;
1163
1013
    }
1164
1014
 
1220
1070
    Item *default_value= NULL;
1221
1071
 
1222
1072
    if (pfield.options().has_default_value() ||
1223
 
        pfield.options().default_null()  ||
 
1073
        pfield.options().has_default_null()  ||
1224
1074
        pfield.options().has_default_bin_value())
1225
1075
    {
1226
1076
      default_value= default_value_item(field_type,
1231
1081
    }
1232
1082
 
1233
1083
 
1234
 
    blob_ptr_size= portable_sizeof_char_ptr;
 
1084
    Table temp_table; /* Use this so that BLOB DEFAULT '' works */
 
1085
    memset(&temp_table, 0, sizeof(temp_table));
 
1086
    temp_table.s= share;
 
1087
    temp_table.in_use= &session;
 
1088
    temp_table.s->db_low_byte_first= true; //Cursor->low_byte_first();
 
1089
    temp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
1235
1090
 
1236
1091
    uint32_t field_length= 0; //Assignment is for compiler complaint.
1237
1092
 
1315
1170
    case DRIZZLE_TYPE_LONGLONG:
1316
1171
      field_length= MAX_BIGINT_WIDTH;
1317
1172
      break;
1318
 
    case DRIZZLE_TYPE_UUID:
1319
 
      field_length= field::Uuid::max_string_length();
1320
 
      break;
1321
1173
    case DRIZZLE_TYPE_NULL:
1322
1174
      abort(); // Programming error
1323
1175
    }
1324
1176
 
1325
 
    assert(enum_field_types_size == 12);
1326
 
 
1327
 
    Field* f= make_field(record + field_offsets[fieldnr] + data_offset,
1328
 
                         field_length,
1329
 
                         pfield.constraints().is_nullable(),
1330
 
                         null_pos,
1331
 
                         null_bit_pos,
1332
 
                         decimals,
1333
 
                         field_type,
1334
 
                         charset,
1335
 
                         MTYP_TYPENR(unireg_type),
1336
 
                         ((field_type == DRIZZLE_TYPE_ENUM) ?  &intervals[interval_nr++] : (TYPELIB*) 0),
1337
 
                         getTableProto()->field(fieldnr).name().c_str());
1338
 
 
1339
 
    field[fieldnr]= f;
1340
 
 
1341
 
    // Insert post make_field code here.
1342
 
    switch (field_type)
1343
 
    {
1344
 
    case DRIZZLE_TYPE_BLOB:
1345
 
    case DRIZZLE_TYPE_VARCHAR:
1346
 
    case DRIZZLE_TYPE_DOUBLE:
1347
 
    case DRIZZLE_TYPE_DECIMAL:
1348
 
    case DRIZZLE_TYPE_TIMESTAMP:
1349
 
    case DRIZZLE_TYPE_DATETIME:
1350
 
    case DRIZZLE_TYPE_DATE:
1351
 
    case DRIZZLE_TYPE_ENUM:
1352
 
    case DRIZZLE_TYPE_LONG:
1353
 
    case DRIZZLE_TYPE_LONGLONG:
1354
 
    case DRIZZLE_TYPE_NULL:
1355
 
    case DRIZZLE_TYPE_UUID:
1356
 
      break;
1357
 
    }
1358
 
 
1359
 
    // This needs to go, we should be setting the "use" on the field so that
1360
 
    // it does not reference the share/table.
1361
 
    table::Shell temp_table(*this); /* Use this so that BLOB DEFAULT '' works */
1362
 
    temp_table.in_use= &session;
 
1177
    Field* f= share->make_field(record + field_offsets[fieldnr] + data_offset,
 
1178
                                field_length,
 
1179
                                pfield.constraints().is_nullable(),
 
1180
                                null_pos,
 
1181
                                null_bit_pos,
 
1182
                                decimals,
 
1183
                                field_type,
 
1184
                                charset,
 
1185
                                (Field::utype) MTYP_TYPENR(unireg_type),
 
1186
                                ((field_type == DRIZZLE_TYPE_ENUM) ?
 
1187
                                 share->intervals + (interval_nr++)
 
1188
                                 : (TYPELIB*) 0),
 
1189
                                getTableProto()->field(fieldnr).name().c_str());
 
1190
 
 
1191
    share->field[fieldnr]= f;
1363
1192
 
1364
1193
    f->init(&temp_table); /* blob default values need table obj */
1365
1194
 
1374
1203
    if (default_value)
1375
1204
    {
1376
1205
      enum_check_fields old_count_cuted_fields= session.count_cuted_fields;
1377
 
      session.count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
 
1206
      session.count_cuted_fields= CHECK_FIELD_WARN;
1378
1207
      int res= default_value->save_in_field(f, 1);
1379
1208
      session.count_cuted_fields= old_count_cuted_fields;
1380
1209
      if (res != 0 && res != 3) /* @TODO Huh? */
1385
1214
        return local_error;
1386
1215
      }
1387
1216
    }
1388
 
    else if (f->real_type() == DRIZZLE_TYPE_ENUM && (f->flags & NOT_NULL_FLAG))
 
1217
    else if (f->real_type() == DRIZZLE_TYPE_ENUM &&
 
1218
             (f->flags & NOT_NULL_FLAG))
1389
1219
    {
1390
1220
      f->set_notnull();
1391
1221
      f->store((int64_t) 1, true);
1396
1226
    }
1397
1227
 
1398
1228
    /* hack to undo f->init() */
1399
 
    f->setTable(NULL);
 
1229
    f->table= NULL;
1400
1230
    f->orig_table= NULL;
1401
1231
 
1402
 
    f->setPosition(fieldnr);
 
1232
    f->field_index= fieldnr;
1403
1233
    f->comment= comment;
1404
1234
    if (! default_value &&
1405
1235
        ! (f->unireg_check==Field::NEXT_NUMBER) &&
1410
1240
    }
1411
1241
 
1412
1242
    if (f->unireg_check == Field::NEXT_NUMBER)
1413
 
      found_next_number_field= &(field[fieldnr]);
 
1243
      share->found_next_number_field= &(share->field[fieldnr]);
 
1244
 
 
1245
    if (share->timestamp_field == f)
 
1246
      share->timestamp_field_offset= fieldnr;
1414
1247
 
1415
1248
    if (use_hash) /* supposedly this never fails... but comments lie */
1416
 
    {
1417
 
      const char *local_field_name= field[fieldnr]->field_name;
1418
 
      name_hash.insert(make_pair(local_field_name, &(field[fieldnr])));
1419
 
    }
 
1249
      (void) my_hash_insert(&share->name_hash,
 
1250
                            (unsigned char*)&(share->field[fieldnr]));
 
1251
 
1420
1252
  }
1421
1253
 
1422
 
  keyinfo= key_info;
1423
 
  for (unsigned int keynr= 0; keynr < keys; keynr++, keyinfo++)
 
1254
  keyinfo= share->key_info;
 
1255
  for (unsigned int keynr= 0; keynr < share->keys; keynr++, keyinfo++)
1424
1256
  {
1425
1257
    key_part= keyinfo->key_part;
1426
1258
 
1441
1273
    We need to set the unused bits to 1. If the number of bits is a multiple
1442
1274
    of 8 there are no unused bits.
1443
1275
  */
 
1276
 
1444
1277
  if (null_count & 7)
1445
1278
    *(record + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
1446
1279
 
1447
 
  null_bytes= (null_pos - (unsigned char*) record + (null_bit_pos + 7) / 8);
 
1280
  share->null_bytes= (null_pos - (unsigned char*) record + (null_bit_pos + 7) / 8);
1448
1281
 
1449
 
  last_null_bit_pos= null_bit_pos;
 
1282
  share->last_null_bit_pos= null_bit_pos;
1450
1283
 
1451
1284
  /* Fix key stuff */
1452
 
  if (key_parts)
 
1285
  if (share->key_parts)
1453
1286
  {
1454
1287
    uint32_t local_primary_key= 0;
1455
1288
    doesKeyNameExist("PRIMARY", local_primary_key);
1456
1289
 
1457
 
    keyinfo= key_info;
 
1290
    keyinfo= share->key_info;
1458
1291
    key_part= keyinfo->key_part;
1459
1292
 
1460
 
    for (uint32_t key= 0; key < keys; key++,keyinfo++)
 
1293
    for (uint32_t key= 0; key < share->keys; key++,keyinfo++)
1461
1294
    {
1462
1295
      uint32_t usable_parts= 0;
1463
1296
 
1472
1305
        {
1473
1306
          uint32_t fieldnr= key_part[i].fieldnr;
1474
1307
          if (! fieldnr ||
1475
 
              field[fieldnr-1]->null_ptr ||
1476
 
              field[fieldnr-1]->key_length() != key_part[i].length)
 
1308
              share->field[fieldnr-1]->null_ptr ||
 
1309
              share->field[fieldnr-1]->key_length() != key_part[i].length)
1477
1310
          {
1478
1311
            local_primary_key= MAX_KEY; // Can't be used
1479
1312
            break;
1488
1321
        {
1489
1322
          return ENOMEM;
1490
1323
        }
1491
 
        local_field= key_part->field= field[key_part->fieldnr-1];
 
1324
        local_field= key_part->field= share->field[key_part->fieldnr-1];
1492
1325
        key_part->type= local_field->key_type();
1493
1326
        if (local_field->null_ptr)
1494
1327
        {
1495
 
          key_part->null_offset=(uint32_t) ((unsigned char*) local_field->null_ptr - getDefaultValues());
 
1328
          key_part->null_offset=(uint32_t) ((unsigned char*) local_field->null_ptr - share->default_values);
1496
1329
          key_part->null_bit= local_field->null_bit;
1497
1330
          key_part->store_length+=HA_KEY_NULL_LENGTH;
1498
1331
          keyinfo->flags|=HA_NULL_PART_KEY;
1519
1352
        if (local_field->key_length() == key_part->length &&
1520
1353
            !(local_field->flags & BLOB_FLAG))
1521
1354
        {
1522
 
          enum ha_key_alg algo= key_info[key].algorithm;
1523
 
          if (db_type()->index_flags(algo) & HA_KEYREAD_ONLY)
 
1355
          enum ha_key_alg algo= share->key_info[key].algorithm;
 
1356
          if (share->db_type()->index_flags(algo) & HA_KEYREAD_ONLY)
1524
1357
          {
1525
 
            keys_for_keyread.set(key);
 
1358
            share->keys_for_keyread.set(key);
1526
1359
            local_field->part_of_key.set(key);
1527
1360
            local_field->part_of_key_not_clustered.set(key);
1528
1361
          }
1529
 
          if (db_type()->index_flags(algo) & HA_READ_ORDER)
 
1362
          if (share->db_type()->index_flags(algo) & HA_READ_ORDER)
1530
1363
            local_field->part_of_sortkey.set(key);
1531
1364
        }
1532
1365
        if (!(key_part->key_part_flag & HA_REVERSE_SORT) &&
1540
1373
            If this field is part of the primary key and all keys contains
1541
1374
            the primary key, then we can use any key to find this column
1542
1375
          */
1543
 
          if (storage_engine->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX))
 
1376
          if (share->storage_engine->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX))
1544
1377
          {
1545
 
            local_field->part_of_key= keys_in_use;
 
1378
            local_field->part_of_key= share->keys_in_use;
1546
1379
            if (local_field->part_of_sortkey.test(key))
1547
 
              local_field->part_of_sortkey= keys_in_use;
 
1380
              local_field->part_of_sortkey= share->keys_in_use;
1548
1381
          }
1549
1382
        }
1550
1383
        if (local_field->key_length() != key_part->length)
1554
1387
      }
1555
1388
      keyinfo->usable_key_parts= usable_parts; // Filesort
1556
1389
 
1557
 
      set_if_bigger(max_key_length,keyinfo->key_length+
 
1390
      set_if_bigger(share->max_key_length,keyinfo->key_length+
1558
1391
                    keyinfo->key_parts);
1559
 
      total_key_length+= keyinfo->key_length;
 
1392
      share->total_key_length+= keyinfo->key_length;
1560
1393
 
1561
1394
      if (keyinfo->flags & HA_NOSAME)
1562
1395
      {
1563
 
        set_if_bigger(max_unique_length,keyinfo->key_length);
 
1396
        set_if_bigger(share->max_unique_length,keyinfo->key_length);
1564
1397
      }
1565
1398
    }
1566
1399
    if (local_primary_key < MAX_KEY &&
1567
 
        (keys_in_use.test(local_primary_key)))
 
1400
        (share->keys_in_use.test(local_primary_key)))
1568
1401
    {
1569
 
      primary_key= local_primary_key;
 
1402
      share->primary_key= local_primary_key;
1570
1403
      /*
1571
1404
        If we are using an integer as the primary key then allow the user to
1572
1405
        refer to it as '_rowid'
1573
1406
      */
1574
 
      if (key_info[local_primary_key].key_parts == 1)
 
1407
      if (share->key_info[local_primary_key].key_parts == 1)
1575
1408
      {
1576
 
        Field *local_field= key_info[local_primary_key].key_part[0].field;
 
1409
        Field *local_field= share->key_info[local_primary_key].key_part[0].field;
1577
1410
        if (local_field && local_field->result_type() == INT_RESULT)
1578
1411
        {
1579
1412
          /* note that fieldnr here (and rowid_field_offset) starts from 1 */
1580
 
          rowid_field_offset= (key_info[local_primary_key].key_part[0].
 
1413
          share->rowid_field_offset= (share->key_info[local_primary_key].key_part[0].
1581
1414
                                      fieldnr);
1582
1415
        }
1583
1416
      }
1584
1417
    }
 
1418
    else
 
1419
    {
 
1420
      share->primary_key = MAX_KEY; // we do not have a primary key
 
1421
    }
 
1422
  }
 
1423
  else
 
1424
  {
 
1425
    share->primary_key= MAX_KEY;
1585
1426
  }
1586
1427
 
1587
 
  if (found_next_number_field)
 
1428
  if (share->found_next_number_field)
1588
1429
  {
1589
 
    Field *reg_field= *found_next_number_field;
1590
 
    if ((int) (next_number_index= (uint32_t)
1591
 
               find_ref_key(key_info, keys,
1592
 
                            getDefaultValues(), reg_field,
1593
 
                            &next_number_key_offset,
1594
 
                            &next_number_keypart)) < 0)
 
1430
    Field *reg_field= *share->found_next_number_field;
 
1431
    if ((int) (share->next_number_index= (uint32_t)
 
1432
               find_ref_key(share->key_info, share->keys,
 
1433
                            share->default_values, reg_field,
 
1434
                            &share->next_number_key_offset,
 
1435
                            &share->next_number_keypart)) < 0)
1595
1436
    {
1596
1437
      /* Wrong field definition */
1597
1438
      local_error= 4;
1604
1445
    }
1605
1446
  }
1606
1447
 
1607
 
  if (blob_fields)
 
1448
  if (share->blob_fields)
1608
1449
  {
 
1450
    Field **ptr;
 
1451
    uint32_t k, *save;
 
1452
 
1609
1453
    /* Store offsets to blob fields to find them fast */
1610
 
    blob_field.resize(blob_fields);
1611
 
    uint32_t *save= &blob_field[0];
1612
 
    uint32_t k= 0;
1613
 
    for (Fields::iterator iter= field.begin(); iter != field.end()-1; iter++, k++)
1614
 
    {
1615
 
      if ((*iter)->flags & BLOB_FLAG)
 
1454
    if (!(share->blob_field= save=
 
1455
          (uint*) share->alloc_root((uint32_t) (share->blob_fields* sizeof(uint32_t)))))
 
1456
    {
 
1457
      return local_error;
 
1458
    }
 
1459
    for (k= 0, ptr= share->field ; *ptr ; ptr++, k++)
 
1460
    {
 
1461
      if ((*ptr)->flags & BLOB_FLAG)
1616
1462
        (*save++)= k;
1617
1463
    }
1618
1464
  }
1619
1465
 
1620
 
  all_set.clear();
1621
 
  all_set.resize(fields);
1622
 
  all_set.set();
 
1466
  share->db_low_byte_first= true; // @todo Question this.
 
1467
  share->column_bitmap_size= bitmap_buffer_size(share->fields);
 
1468
 
 
1469
  my_bitmap_map *bitmaps;
 
1470
 
 
1471
  if (!(bitmaps= (my_bitmap_map*) share->alloc_root(share->column_bitmap_size)))
 
1472
  { }
 
1473
  else
 
1474
  {
 
1475
    share->all_set.init(bitmaps, share->fields);
 
1476
    share->all_set.setAll();
 
1477
 
 
1478
    return (0);
 
1479
  }
1623
1480
 
1624
1481
  return local_error;
1625
1482
}
1634
1491
  error= local_error;
1635
1492
  open_errno= errno;
1636
1493
  errarg= 0;
 
1494
  hash_free(&name_hash);
1637
1495
  open_table_error(local_error, open_errno, 0);
1638
1496
 
1639
1497
  return local_error;
1650
1508
 
1651
1509
  NOTES
1652
1510
  This function is called when the table definition is not cached in
1653
 
  definition::Cache::singleton().getCache()
 
1511
  table_def_cache
1654
1512
  The data is returned in 'share', which is alloced by
1655
1513
  alloc_table_share().. The code assumes that share is initialized.
1656
1514
 
1664
1522
  6    Unknown .frm version
1665
1523
*/
1666
1524
 
1667
 
int TableShare::open_table_def(Session& session, const TableIdentifier &identifier)
 
1525
int TableShare::open_table_def(Session& session, TableIdentifier &identifier)
1668
1526
{
1669
1527
  int local_error;
1670
1528
  bool error_given;
1672
1530
  local_error= 1;
1673
1531
  error_given= 0;
1674
1532
 
1675
 
  message::table::shared_ptr table;
 
1533
  message::Table table;
1676
1534
 
1677
1535
  local_error= plugin::StorageEngine::getTableDefinition(session, identifier, table);
1678
1536
 
1685
1543
    }
1686
1544
    else
1687
1545
    {
1688
 
      if (not table->IsInitialized())
 
1546
      if (not table.IsInitialized())
1689
1547
      {
1690
1548
        local_error= 4;
1691
1549
      }
1693
1551
    goto err_not_open;
1694
1552
  }
1695
1553
 
1696
 
  local_error= parse_table_proto(session, *table);
 
1554
  local_error= parse_table_proto(session, table);
1697
1555
 
1698
1556
  setTableCategory(TABLE_CATEGORY_USER);
1699
1557
 
1731
1589
  5    Error (see open_table_error: charset unavailable)
1732
1590
  7    Table definition has changed in engine
1733
1591
*/
1734
 
int TableShare::open_table_from_share(Session *session,
1735
 
                                      const TableIdentifier &identifier,
1736
 
                                      const char *alias,
 
1592
 
 
1593
int TableShare::open_table_from_share(Session *session, const char *alias,
1737
1594
                                      uint32_t db_stat, uint32_t ha_open_flags,
1738
1595
                                      Table &outparam)
1739
1596
{
 
1597
  int local_error;
 
1598
  uint32_t records, bitmap_size;
1740
1599
  bool error_reported= false;
1741
 
  int ret= open_table_from_share_inner(session, alias, db_stat, outparam);
1742
 
 
1743
 
  if (not ret)
1744
 
    ret= open_table_cursor_inner(identifier, db_stat, ha_open_flags, outparam, error_reported);
1745
 
 
1746
 
  if (not ret)
1747
 
    return ret;
1748
 
 
1749
 
  if (not error_reported)
1750
 
    open_table_error(ret, errno, 0);
1751
 
 
1752
 
  delete outparam.cursor;
1753
 
  outparam.cursor= 0;                           // For easier error checking
1754
 
  outparam.db_stat= 0;
1755
 
  outparam.getMemRoot()->free_root(MYF(0));       // Safe to call on zeroed root
1756
 
  outparam.clearAlias();
1757
 
 
1758
 
  return ret;
1759
 
}
1760
 
 
1761
 
int TableShare::open_table_from_share_inner(Session *session,
1762
 
                                            const char *alias,
1763
 
                                            uint32_t db_stat,
1764
 
                                            Table &outparam)
1765
 
{
1766
 
  int local_error;
1767
 
  uint32_t records;
1768
 
  unsigned char *record= NULL;
 
1600
  unsigned char *record, *bitmaps;
1769
1601
  Field **field_ptr;
1770
1602
 
1771
1603
  /* Parsing of partitioning information from .frm needs session->lex set up. */
1774
1606
  local_error= 1;
1775
1607
  outparam.resetTable(session, this, db_stat);
1776
1608
 
1777
 
  outparam.setAlias(alias);
 
1609
 
 
1610
  if (not (outparam.alias= strdup(alias)))
 
1611
    goto err;
1778
1612
 
1779
1613
  /* Allocate Cursor */
1780
 
  if (not (outparam.cursor= db_type()->getCursor(outparam)))
1781
 
    return local_error;
 
1614
  if (not (outparam.cursor= db_type()->getCursor(*this, outparam.getMemRoot())))
 
1615
    goto err;
1782
1616
 
1783
1617
  local_error= 4;
1784
1618
  records= 0;
1788
1622
  records++;
1789
1623
 
1790
1624
  if (!(record= (unsigned char*) outparam.alloc_root(rec_buff_length * records)))
1791
 
    return local_error;
 
1625
    goto err;
1792
1626
 
1793
1627
  if (records == 0)
1794
1628
  {
1795
1629
    /* We are probably in hard repair, and the buffers should not be used */
1796
 
    outparam.record[0]= outparam.record[1]= getDefaultValues();
 
1630
    outparam.record[0]= outparam.record[1]= default_values;
1797
1631
  }
1798
1632
  else
1799
1633
  {
1801
1635
    if (records > 1)
1802
1636
      outparam.record[1]= record+ rec_buff_length;
1803
1637
    else
1804
 
      outparam.record[1]= outparam.getInsertRecord();   // Safety
 
1638
      outparam.record[1]= outparam.record[0];   // Safety
1805
1639
  }
1806
1640
 
1807
 
#ifdef HAVE_VALGRIND
 
1641
#ifdef HAVE_purify
1808
1642
  /*
1809
1643
    We need this because when we read var-length rows, we are not updating
1810
1644
    bytes after end of varchar
1811
1645
  */
1812
1646
  if (records > 1)
1813
1647
  {
1814
 
    memcpy(outparam.getInsertRecord(), getDefaultValues(), rec_buff_length);
1815
 
    memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
 
1648
    memcpy(outparam.record[0], default_values, rec_buff_length);
 
1649
    memcpy(outparam.record[1], default_values, null_bytes);
1816
1650
    if (records > 2)
1817
 
      memcpy(outparam.getUpdateRecord(), getDefaultValues(), rec_buff_length);
 
1651
      memcpy(outparam.record[1], default_values, rec_buff_length);
1818
1652
  }
1819
1653
#endif
1820
1654
  if (records > 1)
1821
1655
  {
1822
 
    memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
 
1656
    memcpy(outparam.record[1], default_values, null_bytes);
1823
1657
  }
1824
1658
 
1825
1659
  if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((fields+1)* sizeof(Field*)))))
1826
1660
  {
1827
 
    return local_error;
 
1661
    goto err;
1828
1662
  }
1829
1663
 
1830
 
  outparam.setFields(field_ptr);
 
1664
  outparam.field= field_ptr;
1831
1665
 
1832
 
  record= (unsigned char*) outparam.getInsertRecord()-1;        /* Fieldstart = 1 */
 
1666
  record= (unsigned char*) outparam.record[0]-1;        /* Fieldstart = 1 */
1833
1667
 
1834
1668
  outparam.null_flags= (unsigned char*) record+1;
1835
1669
 
1837
1671
  for (uint32_t i= 0 ; i < fields; i++, field_ptr++)
1838
1672
  {
1839
1673
    if (!((*field_ptr)= field[i]->clone(outparam.getMemRoot(), &outparam)))
1840
 
      return local_error;
 
1674
      goto err;
1841
1675
  }
1842
1676
  (*field_ptr)= 0;                              // End marker
1843
1677
 
1844
1678
  if (found_next_number_field)
1845
1679
    outparam.found_next_number_field=
1846
 
      outparam.getField(positionFields(found_next_number_field));
 
1680
      outparam.field[(uint32_t) (found_next_number_field - field)];
1847
1681
  if (timestamp_field)
1848
 
    outparam.timestamp_field= (Field_timestamp*) outparam.getField(timestamp_field->position());
 
1682
    outparam.timestamp_field= (Field_timestamp*) outparam.field[timestamp_field_offset];
 
1683
 
1849
1684
 
1850
1685
  /* Fix key->name and key_part->field */
1851
1686
  if (key_parts)
1855
1690
    uint32_t n_length;
1856
1691
    n_length= keys*sizeof(KeyInfo) + key_parts*sizeof(KeyPartInfo);
1857
1692
    if (!(local_key_info= (KeyInfo*) outparam.alloc_root(n_length)))
1858
 
      return local_error;
 
1693
      goto err;
1859
1694
    outparam.key_info= local_key_info;
1860
1695
    key_part= (reinterpret_cast<KeyPartInfo*> (local_key_info+keys));
1861
1696
 
1876
1711
           key_part < key_part_end ;
1877
1712
           key_part++)
1878
1713
      {
1879
 
        Field *local_field= key_part->field= outparam.getField(key_part->fieldnr-1);
 
1714
        Field *local_field= key_part->field= outparam.field[key_part->fieldnr-1];
1880
1715
 
1881
1716
        if (local_field->key_length() != key_part->length &&
1882
1717
            !(local_field->flags & BLOB_FLAG))
1894
1729
 
1895
1730
  /* Allocate bitmaps */
1896
1731
 
1897
 
  outparam.def_read_set.resize(fields);
1898
 
  outparam.def_write_set.resize(fields);
1899
 
  outparam.tmp_set.resize(fields);
 
1732
  bitmap_size= column_bitmap_size;
 
1733
  if (!(bitmaps= (unsigned char*) outparam.alloc_root(bitmap_size*3)))
 
1734
  {
 
1735
    goto err;
 
1736
  }
 
1737
  outparam.def_read_set.init((my_bitmap_map*) bitmaps, fields);
 
1738
  outparam.def_write_set.init((my_bitmap_map*) (bitmaps+bitmap_size), fields);
 
1739
  outparam.tmp_set.init((my_bitmap_map*) (bitmaps+bitmap_size*2), fields);
1900
1740
  outparam.default_column_bitmaps();
1901
1741
 
1902
 
  return 0;
1903
 
}
1904
 
 
1905
 
int TableShare::open_table_cursor_inner(const TableIdentifier &identifier,
1906
 
                                        uint32_t db_stat, uint32_t ha_open_flags,
1907
 
                                        Table &outparam,
1908
 
                                        bool &error_reported)
1909
 
{
1910
1742
  /* The table struct is now initialized;  Open the table */
1911
 
  int local_error= 2;
 
1743
  local_error= 2;
1912
1744
  if (db_stat)
1913
1745
  {
1914
 
    assert(!(db_stat & HA_WAIT_IF_LOCKED));
1915
1746
    int ha_err;
1916
 
 
1917
 
    if ((ha_err= (outparam.cursor->ha_open(identifier,
1918
 
                                           (db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
1919
 
                                           (db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE : HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
 
1747
    if ((ha_err= (outparam.cursor->
 
1748
                  ha_open(&outparam, getNormalizedPath(),
 
1749
                          (db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
 
1750
                          (db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE :
 
1751
                           (db_stat & HA_WAIT_IF_LOCKED) ?  HA_OPEN_WAIT_IF_LOCKED :
 
1752
                           (db_stat & (HA_ABORT_IF_LOCKED | HA_GET_INFO)) ?
 
1753
                           HA_OPEN_ABORT_IF_LOCKED :
 
1754
                           HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
1920
1755
    {
1921
1756
      switch (ha_err)
1922
1757
      {
1943
1778
          local_error= 7;
1944
1779
        break;
1945
1780
      }
1946
 
      return local_error;
 
1781
      goto err;
1947
1782
    }
1948
1783
  }
1949
1784
 
 
1785
#if defined(HAVE_purify)
 
1786
  memset(bitmaps, 0, bitmap_size*3);
 
1787
#endif
 
1788
 
1950
1789
  return 0;
 
1790
 
 
1791
err:
 
1792
  if (!error_reported)
 
1793
    open_table_error(local_error, errno, 0);
 
1794
 
 
1795
  delete outparam.cursor;
 
1796
  outparam.cursor= 0;                           // For easier error checking
 
1797
  outparam.db_stat= 0;
 
1798
  outparam.getMemRoot()->free_root(MYF(0));       // Safe to call on zeroed root
 
1799
  free((char*) outparam.alias);
 
1800
  return (local_error);
1951
1801
}
1952
1802
 
1953
1803
/* error message when opening a form cursor */
1973
1823
    break;
1974
1824
  case 2:
1975
1825
    {
 
1826
      Cursor *cursor= 0;
 
1827
      const char *datext= "";
 
1828
 
 
1829
      if (db_type() != NULL)
 
1830
      {
 
1831
        if ((cursor= db_type()->getCursor(*this, current_session->mem_root)))
 
1832
        {
 
1833
          if (!(datext= *db_type()->bas_ext()))
 
1834
            datext= "";
 
1835
        }
 
1836
      }
1976
1837
      err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ?
1977
1838
        ER_FILE_USED : ER_CANT_OPEN_FILE;
1978
 
      my_error(err_no, errortype, normalized_path.str, db_errno);
 
1839
      snprintf(buff, sizeof(buff), "%s%s", normalized_path.str,datext);
 
1840
      my_error(err_no,errortype, buff, db_errno);
 
1841
      delete cursor;
1979
1842
      break;
1980
1843
    }
1981
1844
  case 5:
2037
1900
  case DRIZZLE_TYPE_DATE:
2038
1901
  case DRIZZLE_TYPE_DATETIME:
2039
1902
  case DRIZZLE_TYPE_TIMESTAMP:
2040
 
  case DRIZZLE_TYPE_UUID:
2041
1903
    field_charset= &my_charset_bin;
2042
1904
  default: break;
2043
1905
  }
2050
1912
                                 null_pos,
2051
1913
                                 null_bit,
2052
1914
                                 field_name,
 
1915
                                 get_enum_pack_length(interval->count),
2053
1916
                                 interval,
2054
1917
                                 field_charset);
2055
1918
  case DRIZZLE_TYPE_VARCHAR:
2056
 
    setVariableWidth();
2057
1919
    return new (&mem_root) Field_varstring(ptr,field_length,
2058
 
                                      ha_varchar_packlength(field_length),
 
1920
                                      HA_VARCHAR_PACKLENGTH(field_length),
2059
1921
                                      null_pos,null_bit,
2060
1922
                                      field_name,
 
1923
                                      this,
2061
1924
                                      field_charset);
2062
1925
  case DRIZZLE_TYPE_BLOB:
2063
1926
    return new (&mem_root) Field_blob(ptr,
2065
1928
                                 null_bit,
2066
1929
                                 field_name,
2067
1930
                                 this,
 
1931
                                 calc_pack_length(DRIZZLE_TYPE_LONG, 0),
2068
1932
                                 field_charset);
2069
1933
  case DRIZZLE_TYPE_DECIMAL:
2070
1934
    return new (&mem_root) Field_decimal(ptr,
2086
1950
                                   decimals,
2087
1951
                                   false,
2088
1952
                                   false /* is_unsigned */);
2089
 
  case DRIZZLE_TYPE_UUID:
2090
 
    return new (&mem_root) field::Uuid(ptr,
2091
 
                                       field_length,
2092
 
                                       null_pos,
2093
 
                                       null_bit,
2094
 
                                       field_name);
2095
1953
  case DRIZZLE_TYPE_LONG:
2096
 
    return new (&mem_root) field::Int32(ptr,
2097
 
                                        field_length,
2098
 
                                        null_pos,
2099
 
                                        null_bit,
2100
 
                                        unireg_check,
2101
 
                                        field_name);
 
1954
    return new (&mem_root) Field_long(ptr,
 
1955
                                 field_length,
 
1956
                                 null_pos,
 
1957
                                 null_bit,
 
1958
                                 unireg_check,
 
1959
                                 field_name,
 
1960
                                 false,
 
1961
                                 false /* is_unsigned */);
2102
1962
  case DRIZZLE_TYPE_LONGLONG:
2103
 
    return new (&mem_root) field::Int64(ptr,
2104
 
                                        field_length,
2105
 
                                        null_pos,
2106
 
                                        null_bit,
2107
 
                                        unireg_check,
2108
 
                                        field_name);
 
1963
    return new (&mem_root) Field_int64_t(ptr,
 
1964
                                    field_length,
 
1965
                                    null_pos,
 
1966
                                    null_bit,
 
1967
                                    unireg_check,
 
1968
                                    field_name,
 
1969
                                    false,
 
1970
                                    false /* is_unsigned */);
2109
1971
  case DRIZZLE_TYPE_TIMESTAMP:
2110
1972
    return new (&mem_root) Field_timestamp(ptr,
2111
1973
                                      field_length,
2132
1994
                                 field_length,
2133
1995
                                 field_name,
2134
1996
                                 field_charset);
 
1997
  default: // Impossible (Wrong version)
 
1998
    break;
2135
1999
  }
2136
 
  assert(0);
2137
 
  abort();
 
2000
  return 0;
2138
2001
}
2139
2002
 
2140
2003