~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table_share.cc

  • Committer: Brian Aker
  • Date: 2010-09-09 21:45:53 UTC
  • mto: (1756.1.2 build) (1768.2.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 1757.
  • Revision ID: brian@tangent.org-20100909214553-e687rmf5zk9478on
Force unique to just use memory and let the OS handle paging.

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
static TableDefinitionCache table_def_cache;
89
84
 
90
85
/*****************************************************************************
91
86
  Functions to handle table definition cach (TableShare)
92
87
 *****************************************************************************/
93
88
 
 
89
 
 
90
// @todo switch this a boost::thread one only call.
 
91
void TableShare::cacheStart(void)
 
92
{
 
93
  /* 
 
94
   * This is going to overalloc a bit - as rehash sets the number of
 
95
   * buckets, not the number of elements. BUT, it'll allow us to not need
 
96
   * to rehash later on as the unordered_map grows.
 
97
 */
 
98
  table_def_cache.rehash(table_def_size);
 
99
}
 
100
 
 
101
 
 
102
/**
 
103
 * @TODO This should return size_t
 
104
 */
 
105
uint32_t cached_table_definitions(void)
 
106
{
 
107
  return static_cast<uint32_t>(table_def_cache.size());
 
108
}
 
109
 
 
110
 
94
111
/*
95
112
  Mark that we are not using table share anymore.
96
113
 
106
123
void TableShare::release(TableShare *share)
107
124
{
108
125
  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
 
  {
 
126
  safe_mutex_assert_owner(LOCK_open.native_handle);
 
127
 
 
128
  share->lock();
 
129
  if (!--share->ref_count)
 
130
  {
 
131
    to_be_deleted= true;
 
132
  }
 
133
 
 
134
  if (to_be_deleted)
 
135
  {
 
136
    TableIdentifier identifier(share->getSchemaName(), share->getTableName());
 
137
    plugin::EventObserver::deregisterTableEvents(*share);
 
138
   
 
139
    TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
 
140
    if (iter != table_def_cache.end())
 
141
    {
 
142
      table_def_cache.erase(iter);
 
143
      delete share;
 
144
    }
 
145
    return;
 
146
  }
 
147
  share->unlock();
 
148
}
 
149
 
 
150
void TableShare::release(TableIdentifier &identifier)
 
151
{
 
152
  TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
 
153
  if (iter != table_def_cache.end())
 
154
  {
 
155
    TableShare *share= (*iter).second;
147
156
    share->version= 0;                          // Mark for delete
148
157
    if (share->ref_count == 0)
149
158
    {
150
 
      definition::Cache::singleton().erase(identifier.getKey());
 
159
      share->lock();
 
160
      plugin::EventObserver::deregisterTableEvents(*share);
 
161
      table_def_cache.erase(identifier.getKey());
 
162
      delete share;
151
163
    }
152
164
  }
153
165
}
154
166
 
155
167
 
156
 
static TableShare::shared_ptr foundTableShare(TableShare::shared_ptr share)
 
168
static TableShare *foundTableShare(TableShare *share)
157
169
{
158
170
  /*
159
171
    We found an existing table definition. Return it if we didn't get
166
178
    /* Table definition contained an error */
167
179
    share->open_table_error(share->error, share->open_errno, share->errarg);
168
180
 
169
 
    return TableShare::shared_ptr();
 
181
    return NULL;
170
182
  }
171
183
 
172
184
  share->incrementTableCount();
189
201
  If it doesn't exist, create a new from the table definition file.
190
202
 
191
203
  NOTES
192
 
  We must have wrlock on table::Cache::singleton().mutex() when we come here
 
204
  We must have wrlock on LOCK_open when we come here
193
205
  (To be changed later)
194
206
 
195
207
  RETURN
197
209
#  Share for table
198
210
*/
199
211
 
200
 
TableShare::shared_ptr TableShare::getShareCreate(Session *session, 
201
 
                                                  const TableIdentifier &identifier,
202
 
                                                  int &in_error)
 
212
TableShare *TableShare::getShareCreate(Session *session, 
 
213
                                       TableIdentifier &identifier,
 
214
                                       int *error)
203
215
{
204
 
  TableShare::shared_ptr share;
 
216
  TableShare *share= NULL;
205
217
 
206
 
  in_error= 0;
 
218
  *error= 0;
207
219
 
208
220
  /* Read table definition from cache */
209
 
  if ((share= definition::Cache::singleton().find(identifier.getKey())))
 
221
  TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
 
222
  if (iter != table_def_cache.end())
 
223
  {
 
224
    share= (*iter).second;
210
225
    return foundTableShare(share);
211
 
 
212
 
  share.reset(new TableShare(message::Table::STANDARD, identifier));
213
 
  
 
226
  }
 
227
 
 
228
  if (not (share= new TableShare(message::Table::STANDARD, identifier)))
 
229
  {
 
230
    return NULL;
 
231
  }
 
232
 
 
233
  /*
 
234
    Lock mutex to be able to read table definition from file without
 
235
    conflicts
 
236
  */
 
237
  share->lock();
 
238
 
 
239
  /**
 
240
   * @TODO: we need to eject something if we exceed table_def_size
 
241
 */
 
242
  pair<TableDefinitionCache::iterator, bool> ret=
 
243
    table_def_cache.insert(make_pair(identifier.getKey(), share));
 
244
  if (ret.second == false)
 
245
  {
 
246
    delete share;
 
247
 
 
248
    return NULL;
 
249
  }
 
250
 
214
251
  if (share->open_table_def(*session, identifier))
215
252
  {
216
 
    in_error= share->error;
 
253
    *error= share->error;
 
254
    table_def_cache.erase(identifier.getKey());
 
255
    delete share;
217
256
 
218
 
    return TableShare::shared_ptr();
 
257
    return NULL;
219
258
  }
220
259
  share->ref_count++;                           // Mark in use
221
260
  
222
261
  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();
 
262
  
 
263
  share->unlock();
228
264
 
229
265
  return share;
230
266
}
231
267
 
 
268
 
 
269
/*
 
270
  Check if table definition exits in cache
 
271
 
 
272
  SYNOPSIS
 
273
  get_cached_table_share()
 
274
  db                    Database name
 
275
  table_name            Table name
 
276
 
 
277
  RETURN
 
278
  0  Not cached
 
279
#  TableShare for table
 
280
*/
 
281
TableShare *TableShare::getShare(TableIdentifier &identifier)
 
282
{
 
283
  safe_mutex_assert_owner(LOCK_open.native_handle);
 
284
 
 
285
  TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
 
286
  if (iter != table_def_cache.end())
 
287
  {
 
288
    return (*iter).second;
 
289
  }
 
290
  else
 
291
  {
 
292
    return NULL;
 
293
  }
 
294
}
 
295
 
232
296
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
233
297
{
234
298
  enum_field_types field_type;
265
329
  case message::Table::Field::BLOB:
266
330
    field_type= DRIZZLE_TYPE_BLOB;
267
331
    break;
268
 
  case message::Table::Field::UUID:
269
 
    field_type= DRIZZLE_TYPE_UUID;
270
 
    break;
271
332
  default:
272
 
    assert(0);
273
 
    abort(); // Programming error
 
333
    field_type= DRIZZLE_TYPE_LONG; /* Set value to kill GCC warning */
 
334
    assert(1);
274
335
  }
275
336
 
276
337
  return field_type;
304
365
                                 default_value->length());
305
366
    break;
306
367
  case DRIZZLE_TYPE_NULL:
307
 
    assert(0);
308
 
    abort();
 
368
    assert(false);
309
369
  case DRIZZLE_TYPE_TIMESTAMP:
310
370
  case DRIZZLE_TYPE_DATETIME:
311
371
  case DRIZZLE_TYPE_DATE:
312
372
  case DRIZZLE_TYPE_ENUM:
313
 
  case DRIZZLE_TYPE_UUID:
314
373
    default_item= new Item_string(default_value->c_str(),
315
374
                                  default_value->length(),
316
375
                                  system_charset_info);
361
420
      size_t num_parts= index.index_part_size();
362
421
      for (size_t y= 0; y < num_parts; ++y)
363
422
      {
364
 
        if (index.index_part(y).fieldnr() == in_field->position())
 
423
        if (index.index_part(y).fieldnr() == in_field->field_index)
365
424
          return true;
366
425
      }
367
426
    }
369
428
  return false;
370
429
}
371
430
 
372
 
TableShare::TableShare(const TableIdentifier::Type type_arg) :
 
431
const TableDefinitionCache &TableShare::getCache()
 
432
{
 
433
  return table_def_cache;
 
434
}
 
435
 
 
436
TableShare::TableShare(TableIdentifier::Type type_arg) :
373
437
  table_category(TABLE_UNKNOWN_CATEGORY),
 
438
  open_count(0),
374
439
  found_next_number_field(NULL),
375
440
  timestamp_field(NULL),
376
441
  key_info(NULL),
377
442
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
378
 
  all_set(),
379
443
  block_size(0),
380
444
  version(0),
381
445
  timestamp_offset(0),
398
462
  uniques(0),
399
463
  null_fields(0),
400
464
  blob_fields(0),
401
 
  has_variable_width(false),
 
465
  timestamp_field_offset(0),
 
466
  varchar_fields(0),
402
467
  db_create_options(0),
403
468
  db_options_in_use(0),
404
469
  db_record_offset(0),
410
475
  error(0),
411
476
  open_errno(0),
412
477
  errarg(0),
 
478
  column_bitmap_size(0),
413
479
  blob_ptr_size(0),
414
480
  db_low_byte_first(false),
 
481
  name_lock(false),
 
482
  replace_with_name_lock(false),
 
483
  waiting_on_cond(false),
415
484
  keys_in_use(0),
416
485
  keys_for_keyread(0),
417
486
  event_observers(NULL)
425
494
 
426
495
  if (type_arg == message::Table::INTERNAL)
427
496
  {
428
 
    TableIdentifier::build_tmptable_filename(private_key_for_cache.vectorPtr());
429
 
    init(private_key_for_cache.vector(), private_key_for_cache.vector());
 
497
    TableIdentifier::build_tmptable_filename(private_key_for_cache);
 
498
    init(&private_key_for_cache[0], &private_key_for_cache[0]);
430
499
  }
431
500
  else
432
501
  {
434
503
  }
435
504
}
436
505
 
437
 
TableShare::TableShare(const TableIdentifier &identifier, const TableIdentifier::Key &key) :// Used by placeholder
 
506
TableShare::TableShare(TableIdentifier &identifier, const TableIdentifier::Key &key) :// Used by placeholder
438
507
  table_category(TABLE_UNKNOWN_CATEGORY),
 
508
  open_count(0),
439
509
  found_next_number_field(NULL),
440
510
  timestamp_field(NULL),
441
511
  key_info(NULL),
442
512
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
443
 
  all_set(),
444
513
  block_size(0),
445
514
  version(0),
446
515
  timestamp_offset(0),
463
532
  uniques(0),
464
533
  null_fields(0),
465
534
  blob_fields(0),
466
 
  has_variable_width(false),
 
535
  timestamp_field_offset(0),
 
536
  varchar_fields(0),
467
537
  db_create_options(0),
468
538
  db_options_in_use(0),
469
539
  db_record_offset(0),
475
545
  error(0),
476
546
  open_errno(0),
477
547
  errarg(0),
 
548
  column_bitmap_size(0),
478
549
  blob_ptr_size(0),
479
550
  db_low_byte_first(false),
 
551
  name_lock(false),
 
552
  replace_with_name_lock(false),
 
553
  waiting_on_cond(false),
480
554
  keys_in_use(0),
481
555
  keys_for_keyread(0),
482
556
  event_observers(NULL)
492
566
  table_category=         TABLE_CATEGORY_TEMPORARY;
493
567
  tmp_table=              message::Table::INTERNAL;
494
568
 
495
 
  db.str= const_cast<char *>(private_key_for_cache.vector());
496
 
  db.length= strlen(private_key_for_cache.vector());
 
569
  db.str= &private_key_for_cache[0];
 
570
  db.length= strlen(&private_key_for_cache[0]);
497
571
 
498
 
  table_name.str= const_cast<char *>(private_key_for_cache.vector()) + strlen(private_key_for_cache.vector()) + 1;
 
572
  table_name.str= &private_key_for_cache[0] + strlen(&private_key_for_cache[0]) + 1;
499
573
  table_name.length= strlen(table_name.str);
500
574
  path.str= (char *)"";
501
575
  normalized_path.str= path.str;
502
576
  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
 
 
 
577
  assert(strcmp(identifier.getTableName().c_str(), table_name.str) == 0);
508
578
  assert(strcmp(identifier.getSchemaName().c_str(), db.str) == 0);
509
579
}
510
580
 
511
581
 
512
582
TableShare::TableShare(const TableIdentifier &identifier) : // Just used during createTable()
513
583
  table_category(TABLE_UNKNOWN_CATEGORY),
 
584
  open_count(0),
514
585
  found_next_number_field(NULL),
515
586
  timestamp_field(NULL),
516
587
  key_info(NULL),
517
588
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
518
 
  all_set(),
519
589
  block_size(0),
520
590
  version(0),
521
591
  timestamp_offset(0),
538
608
  uniques(0),
539
609
  null_fields(0),
540
610
  blob_fields(0),
541
 
  has_variable_width(false),
 
611
  timestamp_field_offset(0),
 
612
  varchar_fields(0),
542
613
  db_create_options(0),
543
614
  db_options_in_use(0),
544
615
  db_record_offset(0),
550
621
  error(0),
551
622
  open_errno(0),
552
623
  errarg(0),
 
624
  column_bitmap_size(0),
553
625
  blob_ptr_size(0),
554
626
  db_low_byte_first(false),
 
627
  name_lock(false),
 
628
  replace_with_name_lock(false),
 
629
  waiting_on_cond(false),
555
630
  keys_in_use(0),
556
631
  keys_for_keyread(0),
557
632
  event_observers(NULL)
570
645
  {
571
646
    table_category=         TABLE_CATEGORY_TEMPORARY;
572
647
    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());
 
648
    db.str= &private_key_for_cache[0];
 
649
    db.length= strlen(&private_key_for_cache[0]);
575
650
    table_name.str= db.str + 1;
576
651
    table_name.length= strlen(table_name.str);
577
652
    path.str= &private_normalized_path[0];
584
659
/*
585
660
  Used for shares that will go into the cache.
586
661
*/
587
 
TableShare::TableShare(const TableIdentifier::Type type_arg,
588
 
                       const TableIdentifier &identifier,
 
662
TableShare::TableShare(TableIdentifier::Type type_arg,
 
663
                       TableIdentifier &identifier,
589
664
                       char *path_arg,
590
665
                       uint32_t path_length_arg) :
591
666
  table_category(TABLE_UNKNOWN_CATEGORY),
 
667
  open_count(0),
592
668
  found_next_number_field(NULL),
593
669
  timestamp_field(NULL),
594
670
  key_info(NULL),
595
671
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
596
 
  all_set(),
597
672
  block_size(0),
598
673
  version(0),
599
674
  timestamp_offset(0),
616
691
  uniques(0),
617
692
  null_fields(0),
618
693
  blob_fields(0),
619
 
  has_variable_width(false),
 
694
  timestamp_field_offset(0),
 
695
  varchar_fields(0),
620
696
  db_create_options(0),
621
697
  db_options_in_use(0),
622
698
  db_record_offset(0),
628
704
  error(0),
629
705
  open_errno(0),
630
706
  errarg(0),
 
707
  column_bitmap_size(0),
631
708
  blob_ptr_size(0),
632
709
  db_low_byte_first(false),
 
710
  name_lock(false),
 
711
  replace_with_name_lock(false),
 
712
  waiting_on_cond(false),
633
713
  keys_in_use(0),
634
714
  keys_for_keyread(0),
635
715
  event_observers(NULL)
648
728
    Let us use the fact that the key is "db/0/table_name/0" + optional
649
729
    part for temporary tables.
650
730
  */
651
 
  db.str= const_cast<char *>(private_key_for_cache.vector());
 
731
  db.str= &private_key_for_cache[0];
652
732
  db.length=         strlen(db.str);
653
733
  table_name.str=    db.str + db.length + 1;
654
734
  table_name.length= strlen(table_name.str);
662
742
    TableIdentifier::build_table_filename(_path, db.str, table_name.str, false);
663
743
  }
664
744
 
665
 
  if ((path_buff= (char *)mem_root.alloc_root(_path.length() + 1)))
 
745
  if (mem_root.multi_alloc_root(0,
 
746
                                &path_buff, _path.length() + 1,
 
747
                                NULL))
666
748
  {
667
749
    setPath(path_buff, _path.length());
668
750
    strcpy(path_buff, _path.c_str());
669
751
    setNormalizedPath(path_buff, _path.length());
670
752
 
671
753
    version= refresh_version;
 
754
 
 
755
    pthread_mutex_init(&mutex, MY_MUTEX_INIT_FAST);
 
756
    pthread_cond_init(&cond, NULL);
672
757
  }
673
758
  else
674
759
  {
675
760
    assert(0); // We should throw here.
676
 
    abort();
677
761
  }
678
762
}
679
763
 
696
780
{
697
781
  assert(ref_count == 0);
698
782
 
 
783
  /*
 
784
    If someone is waiting for this to be deleted, inform it about this.
 
785
    Don't do a delete until we know that no one is refering to this anymore.
 
786
  */
 
787
  if (tmp_table == message::Table::STANDARD)
 
788
  {
 
789
    /* share->mutex is locked in release_table_share() */
 
790
    while (waiting_on_cond)
 
791
    {
 
792
      pthread_cond_broadcast(&cond);
 
793
      pthread_cond_wait(&cond, &mutex);
 
794
    }
 
795
    /* No thread refers to this anymore */
 
796
    pthread_mutex_unlock(&mutex);
 
797
    pthread_mutex_destroy(&mutex);
 
798
    pthread_cond_destroy(&cond);
 
799
  }
 
800
 
699
801
  storage_engine= NULL;
700
802
 
701
803
  delete table_proto;
702
804
  table_proto= NULL;
703
805
 
704
 
  plugin::EventObserver::deregisterTableEvents(*this);
705
 
 
706
806
  mem_root.free_root(MYF(0));                 // Free's share
707
807
}
708
808
 
709
 
void TableShare::setIdentifier(const TableIdentifier &identifier_arg)
 
809
void TableShare::setIdentifier(TableIdentifier &identifier_arg)
710
810
{
 
811
  private_key_for_cache.clear();
711
812
  private_key_for_cache= identifier_arg.getKey();
712
813
 
713
814
  /*
714
815
    Let us use the fact that the key is "db/0/table_name/0" + optional
715
816
    part for temporary tables.
716
817
  */
717
 
  db.str= const_cast<char *>(private_key_for_cache.vector());
 
818
  db.str= &private_key_for_cache[0];
718
819
  db.length=         strlen(db.str);
719
820
  table_name.str=    db.str + db.length + 1;
720
821
  table_name.length= strlen(table_name.str);
759
860
 
760
861
  table_charset= get_charset(table_options.collation_id());
761
862
 
762
 
  if (! table_charset)
 
863
  if (!table_charset)
763
864
  {
764
865
    char errmsg[100];
765
866
    snprintf(errmsg, sizeof(errmsg),
934
1035
  uint32_t local_null_fields= 0;
935
1036
  reclength= 0;
936
1037
 
937
 
  std::vector<uint32_t> field_offsets;
938
 
  std::vector<uint32_t> field_pack_length;
 
1038
  vector<uint32_t> field_offsets;
 
1039
  vector<uint32_t> field_pack_length;
939
1040
 
940
1041
  field_offsets.resize(fields);
941
1042
  field_pack_length.resize(fields);
981
1082
      {
982
1083
        message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
983
1084
 
984
 
        field_pack_length[fieldnr]= 4;
 
1085
        field_pack_length[fieldnr]=
 
1086
          get_enum_pack_length(field_options.field_value_size());
985
1087
 
986
1088
        interval_count++;
987
1089
        interval_parts+= field_options.field_value_size();
1135
1237
        unireg_type= Field::TIMESTAMP_DN_FIELD;
1136
1238
      }
1137
1239
      else
1138
 
      {
1139
 
        assert(0); // Invalid update value.
1140
 
        abort();
1141
 
      }
 
1240
        assert(1); // Invalid update value.
1142
1241
    }
1143
1242
    else if (pfield.has_options() &&
1144
1243
             pfield.options().has_update_expression() &&
1231
1330
    }
1232
1331
 
1233
1332
 
1234
 
    blob_ptr_size= portable_sizeof_char_ptr;
 
1333
    Table temp_table; /* Use this so that BLOB DEFAULT '' works */
 
1334
    memset(&temp_table, 0, sizeof(temp_table));
 
1335
    temp_table.setShare(this);
 
1336
    temp_table.in_use= &session;
 
1337
    temp_table.getMutableShare()->db_low_byte_first= true; //Cursor->low_byte_first();
 
1338
    temp_table.getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
1235
1339
 
1236
1340
    uint32_t field_length= 0; //Assignment is for compiler complaint.
1237
1341
 
1315
1419
    case DRIZZLE_TYPE_LONGLONG:
1316
1420
      field_length= MAX_BIGINT_WIDTH;
1317
1421
      break;
1318
 
    case DRIZZLE_TYPE_UUID:
1319
 
      field_length= field::Uuid::max_string_length();
1320
 
      break;
1321
1422
    case DRIZZLE_TYPE_NULL:
1322
1423
      abort(); // Programming error
1323
1424
    }
1324
1425
 
1325
 
    assert(enum_field_types_size == 12);
1326
 
 
1327
1426
    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());
 
1427
                                field_length,
 
1428
                                pfield.constraints().is_nullable(),
 
1429
                                null_pos,
 
1430
                                null_bit_pos,
 
1431
                                decimals,
 
1432
                                field_type,
 
1433
                                charset,
 
1434
                                (Field::utype) MTYP_TYPENR(unireg_type),
 
1435
                                ((field_type == DRIZZLE_TYPE_ENUM) ?
 
1436
                                 &intervals[interval_nr++]
 
1437
                                 : (TYPELIB*) 0),
 
1438
                                getTableProto()->field(fieldnr).name().c_str());
1338
1439
 
1339
1440
    field[fieldnr]= f;
1340
1441
 
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;
1363
 
 
1364
1442
    f->init(&temp_table); /* blob default values need table obj */
1365
1443
 
1366
1444
    if (! (f->flags & NOT_NULL_FLAG))
1385
1463
        return local_error;
1386
1464
      }
1387
1465
    }
1388
 
    else if (f->real_type() == DRIZZLE_TYPE_ENUM && (f->flags & NOT_NULL_FLAG))
 
1466
    else if (f->real_type() == DRIZZLE_TYPE_ENUM &&
 
1467
             (f->flags & NOT_NULL_FLAG))
1389
1468
    {
1390
1469
      f->set_notnull();
1391
1470
      f->store((int64_t) 1, true);
1399
1478
    f->setTable(NULL);
1400
1479
    f->orig_table= NULL;
1401
1480
 
1402
 
    f->setPosition(fieldnr);
 
1481
    f->field_index= fieldnr;
1403
1482
    f->comment= comment;
1404
1483
    if (! default_value &&
1405
1484
        ! (f->unireg_check==Field::NEXT_NUMBER) &&
1412
1491
    if (f->unireg_check == Field::NEXT_NUMBER)
1413
1492
      found_next_number_field= &(field[fieldnr]);
1414
1493
 
 
1494
    if (timestamp_field == f)
 
1495
      timestamp_field_offset= fieldnr;
 
1496
 
1415
1497
    if (use_hash) /* supposedly this never fails... but comments lie */
1416
1498
    {
1417
1499
      const char *local_field_name= field[fieldnr]->field_name;
1418
1500
      name_hash.insert(make_pair(local_field_name, &(field[fieldnr])));
1419
1501
    }
 
1502
 
1420
1503
  }
1421
1504
 
1422
1505
  keyinfo= key_info;
1441
1524
    We need to set the unused bits to 1. If the number of bits is a multiple
1442
1525
    of 8 there are no unused bits.
1443
1526
  */
 
1527
 
1444
1528
  if (null_count & 7)
1445
1529
    *(record + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
1446
1530
 
1606
1690
 
1607
1691
  if (blob_fields)
1608
1692
  {
 
1693
    uint32_t k, *save;
 
1694
 
1609
1695
    /* Store offsets to blob fields to find them fast */
1610
1696
    blob_field.resize(blob_fields);
1611
 
    uint32_t *save= &blob_field[0];
1612
 
    uint32_t k= 0;
 
1697
    save= &blob_field[0];
 
1698
    k= 0;
1613
1699
    for (Fields::iterator iter= field.begin(); iter != field.end()-1; iter++, k++)
1614
1700
    {
1615
1701
      if ((*iter)->flags & BLOB_FLAG)
1617
1703
    }
1618
1704
  }
1619
1705
 
1620
 
  all_set.clear();
1621
 
  all_set.resize(fields);
1622
 
  all_set.set();
 
1706
  db_low_byte_first= true; // @todo Question this.
 
1707
  column_bitmap_size= bitmap_buffer_size(fields);
 
1708
 
 
1709
  all_bitmap.resize(column_bitmap_size);
 
1710
  all_set.init(&all_bitmap[0], fields);
 
1711
  all_set.setAll();
1623
1712
 
1624
1713
  return local_error;
1625
1714
}
1650
1739
 
1651
1740
  NOTES
1652
1741
  This function is called when the table definition is not cached in
1653
 
  definition::Cache::singleton().getCache()
 
1742
  table_def_cache
1654
1743
  The data is returned in 'share', which is alloced by
1655
1744
  alloc_table_share().. The code assumes that share is initialized.
1656
1745
 
1664
1753
  6    Unknown .frm version
1665
1754
*/
1666
1755
 
1667
 
int TableShare::open_table_def(Session& session, const TableIdentifier &identifier)
 
1756
int TableShare::open_table_def(Session& session, TableIdentifier &identifier)
1668
1757
{
1669
1758
  int local_error;
1670
1759
  bool error_given;
1672
1761
  local_error= 1;
1673
1762
  error_given= 0;
1674
1763
 
1675
 
  message::table::shared_ptr table;
 
1764
  message::Table table;
1676
1765
 
1677
1766
  local_error= plugin::StorageEngine::getTableDefinition(session, identifier, table);
1678
1767
 
1685
1774
    }
1686
1775
    else
1687
1776
    {
1688
 
      if (not table->IsInitialized())
 
1777
      if (not table.IsInitialized())
1689
1778
      {
1690
1779
        local_error= 4;
1691
1780
      }
1693
1782
    goto err_not_open;
1694
1783
  }
1695
1784
 
1696
 
  local_error= parse_table_proto(session, *table);
 
1785
  local_error= parse_table_proto(session, table);
1697
1786
 
1698
1787
  setTableCategory(TABLE_CATEGORY_USER);
1699
1788
 
1731
1820
  5    Error (see open_table_error: charset unavailable)
1732
1821
  7    Table definition has changed in engine
1733
1822
*/
 
1823
 
1734
1824
int TableShare::open_table_from_share(Session *session,
1735
1825
                                      const TableIdentifier &identifier,
1736
1826
                                      const char *alias,
1737
1827
                                      uint32_t db_stat, uint32_t ha_open_flags,
1738
1828
                                      Table &outparam)
1739
1829
{
 
1830
  int local_error;
 
1831
  uint32_t records, bitmap_size;
1740
1832
  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;
 
1833
  unsigned char *record, *bitmaps;
1769
1834
  Field **field_ptr;
1770
1835
 
1771
1836
  /* Parsing of partitioning information from .frm needs session->lex set up. */
1774
1839
  local_error= 1;
1775
1840
  outparam.resetTable(session, this, db_stat);
1776
1841
 
1777
 
  outparam.setAlias(alias);
 
1842
  if (not (outparam.alias= strdup(alias)))
 
1843
    goto err;
1778
1844
 
1779
1845
  /* Allocate Cursor */
1780
 
  if (not (outparam.cursor= db_type()->getCursor(outparam)))
1781
 
    return local_error;
 
1846
  if (not (outparam.cursor= db_type()->getCursor(*this)))
 
1847
    goto err;
1782
1848
 
1783
1849
  local_error= 4;
1784
1850
  records= 0;
1788
1854
  records++;
1789
1855
 
1790
1856
  if (!(record= (unsigned char*) outparam.alloc_root(rec_buff_length * records)))
1791
 
    return local_error;
 
1857
    goto err;
1792
1858
 
1793
1859
  if (records == 0)
1794
1860
  {
1804
1870
      outparam.record[1]= outparam.getInsertRecord();   // Safety
1805
1871
  }
1806
1872
 
1807
 
#ifdef HAVE_VALGRIND
 
1873
#ifdef HAVE_purify
1808
1874
  /*
1809
1875
    We need this because when we read var-length rows, we are not updating
1810
1876
    bytes after end of varchar
1824
1890
 
1825
1891
  if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((fields+1)* sizeof(Field*)))))
1826
1892
  {
1827
 
    return local_error;
 
1893
    goto err;
1828
1894
  }
1829
1895
 
1830
1896
  outparam.setFields(field_ptr);
1837
1903
  for (uint32_t i= 0 ; i < fields; i++, field_ptr++)
1838
1904
  {
1839
1905
    if (!((*field_ptr)= field[i]->clone(outparam.getMemRoot(), &outparam)))
1840
 
      return local_error;
 
1906
      goto err;
1841
1907
  }
1842
1908
  (*field_ptr)= 0;                              // End marker
1843
1909
 
1845
1911
    outparam.found_next_number_field=
1846
1912
      outparam.getField(positionFields(found_next_number_field));
1847
1913
  if (timestamp_field)
1848
 
    outparam.timestamp_field= (Field_timestamp*) outparam.getField(timestamp_field->position());
 
1914
    outparam.timestamp_field= (Field_timestamp*) outparam.getField(timestamp_field_offset);
 
1915
 
1849
1916
 
1850
1917
  /* Fix key->name and key_part->field */
1851
1918
  if (key_parts)
1855
1922
    uint32_t n_length;
1856
1923
    n_length= keys*sizeof(KeyInfo) + key_parts*sizeof(KeyPartInfo);
1857
1924
    if (!(local_key_info= (KeyInfo*) outparam.alloc_root(n_length)))
1858
 
      return local_error;
 
1925
      goto err;
1859
1926
    outparam.key_info= local_key_info;
1860
1927
    key_part= (reinterpret_cast<KeyPartInfo*> (local_key_info+keys));
1861
1928
 
1894
1961
 
1895
1962
  /* Allocate bitmaps */
1896
1963
 
1897
 
  outparam.def_read_set.resize(fields);
1898
 
  outparam.def_write_set.resize(fields);
1899
 
  outparam.tmp_set.resize(fields);
 
1964
  bitmap_size= column_bitmap_size;
 
1965
  if (!(bitmaps= (unsigned char*) outparam.alloc_root(bitmap_size*3)))
 
1966
  {
 
1967
    goto err;
 
1968
  }
 
1969
  outparam.def_read_set.init((my_bitmap_map*) bitmaps, fields);
 
1970
  outparam.def_write_set.init((my_bitmap_map*) (bitmaps+bitmap_size), fields);
 
1971
  outparam.tmp_set.init((my_bitmap_map*) (bitmaps+bitmap_size*2), fields);
1900
1972
  outparam.default_column_bitmaps();
1901
1973
 
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
1974
  /* The table struct is now initialized;  Open the table */
1911
 
  int local_error= 2;
 
1975
  local_error= 2;
1912
1976
  if (db_stat)
1913
1977
  {
1914
1978
    assert(!(db_stat & HA_WAIT_IF_LOCKED));
1915
1979
    int ha_err;
1916
1980
 
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))))
 
1981
    if ((ha_err= (outparam.cursor->ha_open(identifier, &outparam,
 
1982
                          (db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
 
1983
                          (db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE : HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
1920
1984
    {
1921
1985
      switch (ha_err)
1922
1986
      {
1943
2007
          local_error= 7;
1944
2008
        break;
1945
2009
      }
1946
 
      return local_error;
 
2010
      goto err;
1947
2011
    }
1948
2012
  }
1949
2013
 
 
2014
#if defined(HAVE_purify)
 
2015
  memset(bitmaps, 0, bitmap_size*3);
 
2016
#endif
 
2017
 
1950
2018
  return 0;
 
2019
 
 
2020
err:
 
2021
  if (!error_reported)
 
2022
    open_table_error(local_error, errno, 0);
 
2023
 
 
2024
  delete outparam.cursor;
 
2025
  outparam.cursor= 0;                           // For easier error checking
 
2026
  outparam.db_stat= 0;
 
2027
  outparam.getMemRoot()->free_root(MYF(0));       // Safe to call on zeroed root
 
2028
  free((char*) outparam.alias);
 
2029
 
 
2030
  return (local_error);
1951
2031
}
1952
2032
 
1953
2033
/* error message when opening a form cursor */
2037
2117
  case DRIZZLE_TYPE_DATE:
2038
2118
  case DRIZZLE_TYPE_DATETIME:
2039
2119
  case DRIZZLE_TYPE_TIMESTAMP:
2040
 
  case DRIZZLE_TYPE_UUID:
2041
2120
    field_charset= &my_charset_bin;
2042
2121
  default: break;
2043
2122
  }
2050
2129
                                 null_pos,
2051
2130
                                 null_bit,
2052
2131
                                 field_name,
 
2132
                                 get_enum_pack_length(interval->count),
2053
2133
                                 interval,
2054
2134
                                 field_charset);
2055
2135
  case DRIZZLE_TYPE_VARCHAR:
2056
 
    setVariableWidth();
2057
2136
    return new (&mem_root) Field_varstring(ptr,field_length,
2058
 
                                      ha_varchar_packlength(field_length),
 
2137
                                      HA_VARCHAR_PACKLENGTH(field_length),
2059
2138
                                      null_pos,null_bit,
2060
2139
                                      field_name,
 
2140
                                      this,
2061
2141
                                      field_charset);
2062
2142
  case DRIZZLE_TYPE_BLOB:
2063
2143
    return new (&mem_root) Field_blob(ptr,
2065
2145
                                 null_bit,
2066
2146
                                 field_name,
2067
2147
                                 this,
 
2148
                                 calc_pack_length(DRIZZLE_TYPE_LONG, 0),
2068
2149
                                 field_charset);
2069
2150
  case DRIZZLE_TYPE_DECIMAL:
2070
2151
    return new (&mem_root) Field_decimal(ptr,
2086
2167
                                   decimals,
2087
2168
                                   false,
2088
2169
                                   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
2170
  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);
 
2171
    return new (&mem_root) Field_long(ptr,
 
2172
                                 field_length,
 
2173
                                 null_pos,
 
2174
                                 null_bit,
 
2175
                                 unireg_check,
 
2176
                                 field_name,
 
2177
                                 false,
 
2178
                                 false /* is_unsigned */);
2102
2179
  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);
 
2180
    return new (&mem_root) Field_int64_t(ptr,
 
2181
                                    field_length,
 
2182
                                    null_pos,
 
2183
                                    null_bit,
 
2184
                                    unireg_check,
 
2185
                                    field_name,
 
2186
                                    false,
 
2187
                                    false /* is_unsigned */);
2109
2188
  case DRIZZLE_TYPE_TIMESTAMP:
2110
2189
    return new (&mem_root) Field_timestamp(ptr,
2111
2190
                                      field_length,
2132
2211
                                 field_length,
2133
2212
                                 field_name,
2134
2213
                                 field_charset);
 
2214
  default: // Impossible (Wrong version)
 
2215
    break;
2135
2216
  }
2136
 
  assert(0);
2137
 
  abort();
 
2217
  return 0;
2138
2218
}
2139
2219
 
2140
2220