~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/memory/ha_heap.cc

  • Committer: Brian Aker
  • Date: 2010-10-20 20:25:52 UTC
  • mto: (1864.2.1 merge)
  • mto: This revision was merged to the branch mainline in revision 1865.
  • Revision ID: brian@tangent.org-20101020202552-51y5sz5ledoxbp7t
Add support for --with-valgrind

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
15
 
16
16
#include "heap_priv.h"
17
17
#include <drizzled/error.h>
18
18
#include <drizzled/table.h>
19
19
#include <drizzled/session.h>
20
 
#include <drizzled/current_session.h>
21
20
#include <drizzled/field/timestamp.h>
22
21
#include <drizzled/field/varstring.h>
23
22
#include "drizzled/plugin/daemon.h"
24
23
 
 
24
#include <boost/thread/mutex.hpp>
 
25
 
25
26
#include "heap.h"
26
27
#include "ha_heap.h"
27
28
 
33
34
 
34
35
static const string engine_name("MEMORY");
35
36
 
36
 
pthread_mutex_t THR_LOCK_heap= PTHREAD_MUTEX_INITIALIZER;
 
37
boost::mutex THR_LOCK_heap;
37
38
 
38
39
static const char *ha_heap_exts[] = {
39
40
  NULL
52
53
                          HTON_SKIP_STORE_LOCK |
53
54
                          HTON_TEMPORARY_ONLY)
54
55
  {
55
 
    pthread_mutex_init(&THR_LOCK_heap, MY_MUTEX_INIT_FAST);
56
56
  }
57
57
 
58
58
  virtual ~HeapEngine()
59
59
  {
60
60
    hp_panic(HA_PANIC_CLOSE);
61
 
 
62
 
    pthread_mutex_destroy(&THR_LOCK_heap);
63
61
  }
64
62
 
65
 
  virtual Cursor *create(TableShare &table,
66
 
                          memory::Root *mem_root)
 
63
  virtual Cursor *create(TableShare &table)
67
64
  {
68
 
    return new (mem_root) ha_heap(*this, table);
 
65
    return new ha_heap(*this, table);
69
66
  }
70
67
 
71
68
  const char **bas_ext() const {
74
71
 
75
72
  int doCreateTable(Session &session,
76
73
                    Table &table_arg,
77
 
                    drizzled::TableIdentifier &identifier,
 
74
                    const TableIdentifier &identifier,
78
75
                    message::Table &create_proto);
79
76
 
80
77
  /* For whatever reason, internal tables can be created by Cursor::open()
88
85
                        message::Table &create_proto,
89
86
                        HP_SHARE **internal_share);
90
87
 
91
 
  int doRenameTable(Session&, TableIdentifier &from, TableIdentifier &to);
 
88
  int doRenameTable(Session&, const TableIdentifier &from, const TableIdentifier &to);
92
89
 
93
 
  int doDropTable(Session&, TableIdentifier &identifier);
 
90
  int doDropTable(Session&, const TableIdentifier &identifier);
94
91
 
95
92
  int doGetTableDefinition(Session& session,
96
 
                           TableIdentifier &identifier,
 
93
                           const TableIdentifier &identifier,
97
94
                           message::Table &table_message);
98
95
 
99
 
  /* Temp only engine, so do not return values. */
100
 
  void doGetTableNames(CachedDirectory &, SchemaIdentifier& , set<string>&) { };
101
 
 
102
96
  uint32_t max_supported_keys()          const { return MAX_KEY; }
103
97
  uint32_t max_supported_key_part_length() const { return MAX_KEY_LENGTH; }
104
98
 
105
 
  uint32_t index_flags(enum  ha_key_alg algorithm) const
 
99
  uint32_t index_flags(enum  ha_key_alg ) const
106
100
  {
107
 
    return ((algorithm == HA_KEY_ALG_BTREE) ?
108
 
            HA_READ_NEXT |
109
 
            HA_READ_PREV |
110
 
            HA_READ_ORDER |
111
 
            HA_READ_RANGE :
112
 
            HA_ONLY_WHOLE_INDEX |
113
 
            HA_KEY_SCAN_NOT_ROR);
 
101
    return ( HA_ONLY_WHOLE_INDEX | HA_KEY_SCAN_NOT_ROR);
114
102
  }
115
103
 
116
 
  bool doDoesTableExist(Session& session, TableIdentifier &identifier);
117
 
  void doGetTableIdentifiers(drizzled::CachedDirectory &directory,
118
 
                             drizzled::SchemaIdentifier &schema_identifier,
119
 
                             drizzled::TableIdentifiers &set_of_identifiers);
 
104
  bool doDoesTableExist(Session& session, const TableIdentifier &identifier);
 
105
  void doGetTableIdentifiers(CachedDirectory &directory,
 
106
                             const SchemaIdentifier &schema_identifier,
 
107
                             TableIdentifiers &set_of_identifiers);
120
108
};
121
109
 
122
 
void HeapEngine::doGetTableIdentifiers(drizzled::CachedDirectory&,
123
 
                                       drizzled::SchemaIdentifier&,
124
 
                                       drizzled::TableIdentifiers&)
 
110
void HeapEngine::doGetTableIdentifiers(CachedDirectory&,
 
111
                                       const SchemaIdentifier&,
 
112
                                       TableIdentifiers&)
125
113
{
126
114
}
127
115
 
128
 
bool HeapEngine::doDoesTableExist(Session& session, TableIdentifier &identifier)
 
116
bool HeapEngine::doDoesTableExist(Session& session, const TableIdentifier &identifier)
129
117
{
130
118
  return session.doesTableMessageExist(identifier);
131
119
}
132
120
 
133
121
int HeapEngine::doGetTableDefinition(Session &session,
134
 
                                     TableIdentifier &identifier,
 
122
                                     const TableIdentifier &identifier,
135
123
                                     message::Table &table_proto)
136
124
{
137
125
  if (session.getTableMessage(identifier, table_proto))
143
131
  We have to ignore ENOENT entries as the MEMORY table is created on open and
144
132
  not when doing a CREATE on the table.
145
133
*/
146
 
int HeapEngine::doDropTable(Session &session, TableIdentifier &identifier)
 
134
int HeapEngine::doDropTable(Session &session, const TableIdentifier &identifier)
147
135
{
148
136
  session.removeTableMessage(identifier);
149
137
 
157
145
 
158
146
static HeapEngine *heap_storage_engine= NULL;
159
147
 
160
 
static int heap_init(plugin::Context &context)
 
148
static int heap_init(module::Context &context)
161
149
{
162
150
  heap_storage_engine= new HeapEngine(engine_name);
163
151
  context.add(heap_storage_engine);
188
176
*/
189
177
#define MEMORY_STATS_UPDATE_THRESHOLD 10
190
178
 
191
 
int ha_heap::open(const char *name, int mode, uint32_t test_if_locked)
 
179
int ha_heap::doOpen(const drizzled::TableIdentifier &identifier, int mode, uint32_t test_if_locked)
192
180
{
193
 
  if ((test_if_locked & HA_OPEN_INTERNAL_TABLE) || (!(file= heap_open(name, mode)) && errno == ENOENT))
 
181
  if ((test_if_locked & HA_OPEN_INTERNAL_TABLE) || (!(file= heap_open(identifier.getPath().c_str(), mode)) && errno == ENOENT))
194
182
  {
195
 
    HA_CREATE_INFO create_info;
196
183
    internal_table= test(test_if_locked & HA_OPEN_INTERNAL_TABLE);
197
 
    memset(&create_info, 0, sizeof(create_info));
198
184
    file= 0;
199
185
    HP_SHARE *internal_share= NULL;
200
186
    message::Table create_proto;
201
187
 
202
 
    if (!heap_storage_engine->heap_create_table(ha_session(), name, table,
203
 
                                                internal_table,
204
 
                                                create_proto,
205
 
                                                &internal_share))
 
188
    if (not heap_storage_engine->heap_create_table(table->in_use,
 
189
                                                   identifier.getPath().c_str(),
 
190
                                                   table,
 
191
                                                   internal_table,
 
192
                                                   create_proto,
 
193
                                                   &internal_share))
206
194
    {
207
195
        file= internal_table ?
208
196
          heap_open_from_share(internal_share, mode) :
210
198
      if (!file)
211
199
      {
212
200
         /* Couldn't open table; Remove the newly created table */
213
 
        pthread_mutex_lock(&THR_LOCK_heap);
 
201
        THR_LOCK_heap.lock();
214
202
        hp_free(internal_share);
215
 
        pthread_mutex_unlock(&THR_LOCK_heap);
 
203
        THR_LOCK_heap.unlock();
216
204
      }
217
 
      implicit_emptied= 1;
218
205
    }
219
206
  }
220
207
  ref_length= sizeof(HEAP_PTR);
230
217
      ha_heap::info(), which is always called before key statistics are
231
218
      used.
232
219
    */
233
 
    key_stat_version= file->s->key_stat_version-1;
 
220
    key_stat_version= file->getShare()->key_stat_version - 1;
234
221
  }
235
222
  return (file ? 0 : 1);
236
223
}
246
233
 
247
234
  DESCRIPTION
248
235
    Do same as default implementation but use file->s->name instead of
249
 
    table->s->path. This is needed by Windows where the clone() call sees
250
 
    '/'-delimited path in table->s->path, while ha_peap::open() was called
 
236
    table->getShare()->path. This is needed by Windows where the clone() call sees
 
237
    '/'-delimited path in table->getShare()->path, while ha_peap::open() was called
251
238
    with '\'-delimited path.
252
239
*/
253
240
 
254
 
Cursor *ha_heap::clone(memory::Root *mem_root)
 
241
Cursor *ha_heap::clone(memory::Root *)
255
242
{
256
 
  Cursor *new_handler= table->s->db_type()->getCursor(*table->s, mem_root);
 
243
  Cursor *new_handler= table->getMutableShare()->db_type()->getCursor(*(table->getMutableShare()));
 
244
  TableIdentifier identifier(table->getShare()->getSchemaName(),
 
245
                             table->getShare()->getTableName(),
 
246
                             table->getShare()->getPath());
257
247
 
258
 
  if (new_handler && !new_handler->ha_open(table, file->s->name, table->db_stat,
 
248
  if (new_handler && !new_handler->ha_open(identifier, table, table->db_stat,
259
249
                                           HA_OPEN_IGNORE_IF_LOCKED))
260
250
    return new_handler;
261
251
  return NULL;
262
252
}
263
253
 
264
254
 
265
 
const char *ha_heap::index_type(uint32_t inx)
 
255
const char *ha_heap::index_type(uint32_t )
266
256
{
267
 
  return ((table_share->key_info[inx].algorithm == HA_KEY_ALG_BTREE) ?
268
 
          "BTREE" : "HASH");
 
257
  return ("HASH");
269
258
}
270
259
 
271
260
 
287
276
 
288
277
void ha_heap::set_keys_for_scanning(void)
289
278
{
290
 
  btree_keys.reset();
291
 
  for (uint32_t i= 0 ; i < table->s->keys ; i++)
292
 
  {
293
 
    if (table->key_info[i].algorithm == HA_KEY_ALG_BTREE)
294
 
      btree_keys.set(i);
295
 
  }
296
279
}
297
280
 
298
281
 
299
282
void ha_heap::update_key_stats()
300
283
{
301
 
  for (uint32_t i= 0; i < table->s->keys; i++)
 
284
  for (uint32_t i= 0; i < table->getShare()->sizeKeys(); i++)
302
285
  {
303
 
    KEY *key=table->key_info+i;
 
286
    KeyInfo *key= &table->key_info[i];
 
287
 
304
288
    if (!key->rec_per_key)
305
289
      continue;
306
 
    if (key->algorithm != HA_KEY_ALG_BTREE)
 
290
 
307
291
    {
308
292
      if (key->flags & HA_NOSAME)
309
293
        key->rec_per_key[key->key_parts-1]= 1;
310
294
      else
311
295
      {
312
 
        ha_rows hash_buckets= file->s->keydef[i].hash_buckets;
313
 
        uint32_t no_records= hash_buckets ? (uint) (file->s->records/hash_buckets) : 2;
 
296
        ha_rows hash_buckets= file->getShare()->keydef[i].hash_buckets;
 
297
        uint32_t no_records= hash_buckets ? (uint) (file->getShare()->records/hash_buckets) : 2;
314
298
        if (no_records < 2)
315
299
          no_records= 2;
316
300
        key->rec_per_key[key->key_parts-1]= no_records;
319
303
  }
320
304
  records_changed= 0;
321
305
  /* At the end of update_key_stats() we can proudly claim they are OK. */
322
 
  key_stat_version= file->s->key_stat_version;
 
306
  key_stat_version= file->getShare()->key_stat_version;
323
307
}
324
308
 
325
309
 
326
 
int ha_heap::write_row(unsigned char * buf)
 
310
int ha_heap::doInsertRecord(unsigned char * buf)
327
311
{
328
312
  int res;
329
 
  ha_statistic_increment(&system_status_var::ha_write_count);
330
 
  if (table->next_number_field && buf == table->record[0])
 
313
  if (table->next_number_field && buf == table->getInsertRecord())
331
314
  {
332
315
    if ((res= update_auto_increment()))
333
316
      return res;
334
317
  }
335
318
  res= heap_write(file,buf);
336
319
  if (!res && (++records_changed*MEMORY_STATS_UPDATE_THRESHOLD >
337
 
               file->s->records))
 
320
               file->getShare()->records))
338
321
  {
339
322
    /*
340
323
       We can perform this safely since only one writer at the time is
341
324
       allowed on the table.
342
325
    */
343
 
    file->s->key_stat_version++;
 
326
    file->getShare()->key_stat_version++;
344
327
  }
345
328
  return res;
346
329
}
347
330
 
348
 
int ha_heap::update_row(const unsigned char * old_data, unsigned char * new_data)
 
331
int ha_heap::doUpdateRecord(const unsigned char * old_data, unsigned char * new_data)
349
332
{
350
333
  int res;
351
 
  ha_statistic_increment(&system_status_var::ha_update_count);
352
 
  if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_UPDATE)
353
 
    table->timestamp_field->set_time();
 
334
 
354
335
  res= heap_update(file,old_data,new_data);
355
336
  if (!res && ++records_changed*MEMORY_STATS_UPDATE_THRESHOLD >
356
 
              file->s->records)
 
337
              file->getShare()->records)
357
338
  {
358
339
    /*
359
340
       We can perform this safely since only one writer at the time is
360
341
       allowed on the table.
361
342
    */
362
 
    file->s->key_stat_version++;
 
343
    file->getShare()->key_stat_version++;
363
344
  }
364
345
  return res;
365
346
}
366
347
 
367
 
int ha_heap::delete_row(const unsigned char * buf)
 
348
int ha_heap::doDeleteRecord(const unsigned char * buf)
368
349
{
369
350
  int res;
370
 
  ha_statistic_increment(&system_status_var::ha_delete_count);
 
351
 
371
352
  res= heap_delete(file,buf);
372
 
  if (!res && table->s->tmp_table == message::Table::STANDARD &&
373
 
      ++records_changed*MEMORY_STATS_UPDATE_THRESHOLD > file->s->records)
 
353
  if (!res && table->getShare()->getType() == message::Table::STANDARD &&
 
354
      ++records_changed*MEMORY_STATS_UPDATE_THRESHOLD > file->getShare()->records)
374
355
  {
375
356
    /*
376
357
       We can perform this safely since only one writer at the time is
377
358
       allowed on the table.
378
359
    */
379
 
    file->s->key_stat_version++;
 
360
    file->getShare()->key_stat_version++;
380
361
  }
381
362
  return res;
382
363
}
449
430
  return error;
450
431
}
451
432
 
452
 
int ha_heap::rnd_init(bool scan)
 
433
int ha_heap::doStartTableScan(bool scan)
453
434
{
454
435
  return scan ? heap_scan_init(file) : 0;
455
436
}
498
479
    have to update the key statistics. Hoping that a table lock is now
499
480
    in place.
500
481
  */
501
 
  if (key_stat_version != file->s->key_stat_version)
 
482
  if (key_stat_version != file->getShare()->key_stat_version)
502
483
    update_key_stats();
503
484
  return 0;
504
485
}
505
486
 
506
 
 
507
 
enum row_type ha_heap::get_row_type() const
508
 
{
509
 
  if (file->s->recordspace.is_variable_size)
510
 
    return ROW_TYPE_DYNAMIC;
511
 
 
512
 
  return ROW_TYPE_FIXED;
513
 
}
514
 
 
515
487
int ha_heap::extra(enum ha_extra_function operation)
516
488
{
517
489
  return heap_extra(file,operation);
527
499
int ha_heap::delete_all_rows()
528
500
{
529
501
  heap_clear(file);
530
 
  if (table->s->tmp_table == message::Table::STANDARD)
 
502
  if (table->getShare()->getType() == message::Table::STANDARD)
531
503
  {
532
504
    /*
533
505
       We can perform this safely since only one writer at the time is
534
506
       allowed on the table.
535
507
    */
536
 
    file->s->key_stat_version++;
 
508
    file->getShare()->key_stat_version++;
537
509
  }
538
510
  return 0;
539
511
}
647
619
 
648
620
void ha_heap::drop_table(const char *)
649
621
{
650
 
  file->s->delete_on_close= 1;
 
622
  file->getShare()->delete_on_close= 1;
651
623
  close();
652
624
}
653
625
 
654
626
 
655
 
int HeapEngine::doRenameTable(Session &session, TableIdentifier &from, TableIdentifier &to)
 
627
int HeapEngine::doRenameTable(Session &session, const TableIdentifier &from, const TableIdentifier &to)
656
628
{
657
629
  session.renameTableMessage(from, to);
658
630
  return heap_rename(from.getPath().c_str(), to.getPath().c_str());
662
634
ha_rows ha_heap::records_in_range(uint32_t inx, key_range *min_key,
663
635
                                  key_range *max_key)
664
636
{
665
 
  KEY *key=table->key_info+inx;
666
 
  if (key->algorithm == HA_KEY_ALG_BTREE)
667
 
    return hp_rb_records_in_range(file, inx, min_key, max_key);
 
637
  KeyInfo *key= &table->key_info[inx];
668
638
 
669
639
  if (!min_key || !max_key ||
670
640
      min_key->length != max_key->length ||
677
647
    return stats.records;
678
648
 
679
649
  /* Assert that info() did run. We need current statistics here. */
680
 
  assert(key_stat_version == file->s->key_stat_version);
 
650
  assert(key_stat_version == file->getShare()->key_stat_version);
681
651
  return key->rec_per_key[key->key_parts-1];
682
652
}
683
653
 
684
654
int HeapEngine::doCreateTable(Session &session,
685
655
                              Table &table_arg,
686
 
                              drizzled::TableIdentifier &identifier,
 
656
                              const TableIdentifier &identifier,
687
657
                              message::Table& create_proto)
688
658
{
689
659
  int error;
710
680
                                  message::Table &create_proto,
711
681
                                  HP_SHARE **internal_share)
712
682
{
713
 
  uint32_t key, parts, mem_per_row_keys= 0, keys= table_arg->s->keys;
 
683
  uint32_t key, parts, mem_per_row_keys= 0;
 
684
  uint32_t keys= table_arg->getShare()->sizeKeys();
714
685
  uint32_t auto_key= 0, auto_key_type= 0;
715
686
  uint32_t max_key_fieldnr = 0, key_part_size = 0, next_field_pos = 0;
716
 
  uint32_t column_idx, column_count= table_arg->s->fields;
717
 
  HP_COLUMNDEF *columndef;
718
 
  HP_KEYDEF *keydef;
719
 
  HA_KEYSEG *seg;
720
 
  char buff[FN_REFLEN];
 
687
  uint32_t column_count= table_arg->getShare()->sizeFields();
 
688
  std::vector<HP_KEYDEF> keydef;
721
689
  int error;
722
 
  TableShare *share= table_arg->s;
723
690
  bool found_real_auto_increment= 0;
724
691
 
725
692
  /* 
728
695
   * can return a number more than that, we trap it here instead of casting
729
696
   * to a truncated integer.
730
697
   */
731
 
  uint64_t num_rows= share->getMaxRows();
 
698
  uint64_t num_rows= table_arg->getShare()->getMaxRows();
732
699
  if (num_rows > UINT32_MAX)
733
700
    return -1;
734
701
 
735
 
  if (!(columndef= (HP_COLUMNDEF*) malloc(column_count * sizeof(HP_COLUMNDEF))))
736
 
    return errno;
737
 
 
738
 
  for (column_idx= 0; column_idx < column_count; column_idx++)
739
 
  {
740
 
    Field* field= *(table_arg->field + column_idx);
741
 
    HP_COLUMNDEF* column= columndef + column_idx;
742
 
    column->type= (uint16_t)field->type();
743
 
    column->length= field->pack_length();
744
 
    column->offset= field->offset(field->table->record[0]);
745
 
 
746
 
    if (field->null_bit)
747
 
    {
748
 
      column->null_bit= field->null_bit;
749
 
      column->null_pos= (uint) (field->null_ptr - (unsigned char*) table_arg->record[0]);
750
 
    }
751
 
    else
752
 
    {
753
 
      column->null_bit= 0;
754
 
      column->null_pos= 0;
755
 
    }
756
 
 
757
 
    if (field->type() == DRIZZLE_TYPE_VARCHAR)
758
 
    {
759
 
      column->length_bytes= (uint8_t)(((Field_varstring*)field)->length_bytes);
760
 
    }
761
 
    else
762
 
    {
763
 
      column->length_bytes= 0;
764
 
    }
765
 
  }
766
 
 
767
702
  for (key= parts= 0; key < keys; key++)
768
703
    parts+= table_arg->key_info[key].key_parts;
769
704
 
770
 
  if (!(keydef= (HP_KEYDEF*) malloc(keys * sizeof(HP_KEYDEF) +
771
 
                                    parts * sizeof(HA_KEYSEG))))
772
 
  {
773
 
    free((void *) columndef);
774
 
    return errno;
775
 
  }
 
705
  keydef.resize(keys);
 
706
  std::vector<HA_KEYSEG> seg_buffer;
 
707
  seg_buffer.resize(parts);
 
708
  HA_KEYSEG *seg= &seg_buffer[0];
776
709
 
777
 
  seg= reinterpret_cast<HA_KEYSEG*> (keydef + keys);
778
710
  for (key= 0; key < keys; key++)
779
711
  {
780
 
    KEY *pos= table_arg->key_info+key;
781
 
    KEY_PART_INFO *key_part=     pos->key_part;
782
 
    KEY_PART_INFO *key_part_end= key_part + pos->key_parts;
 
712
    KeyInfo *pos= &table_arg->key_info[key];
 
713
    KeyPartInfo *key_part=     pos->key_part;
 
714
    KeyPartInfo *key_part_end= key_part + pos->key_parts;
783
715
 
784
716
    keydef[key].keysegs=   (uint) pos->key_parts;
785
717
    keydef[key].flag=      (pos->flags & (HA_NOSAME | HA_NULL_ARE_EQUAL));
786
718
    keydef[key].seg=       seg;
787
719
 
788
 
    switch (pos->algorithm) {
789
 
    case HA_KEY_ALG_UNDEF:
790
 
    case HA_KEY_ALG_HASH:
791
 
      keydef[key].algorithm= HA_KEY_ALG_HASH;
792
 
      mem_per_row_keys+= sizeof(char*) * 2; // = sizeof(HASH_INFO)
793
 
      break;
794
 
    case HA_KEY_ALG_BTREE:
795
 
      keydef[key].algorithm= HA_KEY_ALG_BTREE;
796
 
      mem_per_row_keys+=sizeof(TREE_ELEMENT)+pos->key_length+sizeof(char*);
797
 
      break;
798
 
    default:
799
 
      assert(0); // cannot happen
800
 
    }
 
720
    mem_per_row_keys+= sizeof(char*) * 2; // = sizeof(HASH_INFO)
801
721
 
802
722
    for (; key_part != key_part_end; key_part++, seg++)
803
723
    {
804
724
      Field *field= key_part->field;
805
725
 
806
 
      if (pos->algorithm == HA_KEY_ALG_BTREE)
807
 
        seg->type= field->key_type();
808
 
      else
809
726
      {
810
727
        if ((seg->type = field->key_type()) != (int) HA_KEYTYPE_TEXT &&
811
728
            seg->type != HA_KEYTYPE_VARTEXT1 &&
821
738
      next_field_pos= seg->start + seg->length;
822
739
      if (field->type() == DRIZZLE_TYPE_VARCHAR)
823
740
      {
824
 
        next_field_pos+= (uint8_t)(((Field_varstring*)field)->length_bytes);
 
741
        next_field_pos+= (uint8_t)(((Field_varstring*)field)->pack_length_no_ptr());
825
742
      }
826
743
 
827
744
      if (next_field_pos > key_part_size) {
835
752
      if (field->null_ptr)
836
753
      {
837
754
        seg->null_bit= field->null_bit;
838
 
        seg->null_pos= (uint) (field->null_ptr - (unsigned char*) table_arg->record[0]);
 
755
        seg->null_pos= (uint) (field->null_ptr - (unsigned char*) table_arg->getInsertRecord());
839
756
      }
840
757
      else
841
758
      {
844
761
      }
845
762
      if (field->flags & AUTO_INCREMENT_FLAG &&
846
763
          table_arg->found_next_number_field &&
847
 
          key == share->next_number_index)
 
764
          key == table_arg->getShare()->next_number_index)
848
765
      {
849
766
        /*
850
767
          Store key number and type for found auto_increment key
861
778
    }
862
779
  }
863
780
 
864
 
  if (key_part_size < share->null_bytes + ((share->last_null_bit_pos+7) >> 3))
 
781
  if (key_part_size < table_arg->getShare()->null_bytes + ((table_arg->getShare()->last_null_bit_pos+7) >> 3))
865
782
  {
866
783
    /* Make sure to include null fields regardless of the presense of keys */
867
 
    key_part_size = share->null_bytes + ((share->last_null_bit_pos+7) >> 3);
 
784
    key_part_size = table_arg->getShare()->null_bytes + ((table_arg->getShare()->last_null_bit_pos+7) >> 3);
868
785
  }
869
786
 
870
787
 
871
788
 
872
789
  if (table_arg->found_next_number_field)
873
790
  {
874
 
    keydef[share->next_number_index].flag|= HA_AUTO_KEY;
875
 
    found_real_auto_increment= share->next_number_key_offset == 0;
 
791
    keydef[table_arg->getShare()->next_number_index].flag|= HA_AUTO_KEY;
 
792
    found_real_auto_increment= table_arg->getShare()->next_number_key_offset == 0;
876
793
  }
877
794
  HP_CREATE_INFO hp_create_info;
878
795
  hp_create_info.auto_key= auto_key;
882
799
  hp_create_info.max_table_size=session->variables.max_heap_table_size;
883
800
  hp_create_info.with_auto_increment= found_real_auto_increment;
884
801
  hp_create_info.internal_table= internal_table;
885
 
  hp_create_info.max_chunk_size= share->block_size;
886
 
  hp_create_info.is_dynamic= (share->row_type == ROW_TYPE_DYNAMIC);
887
 
 
888
 
  error= heap_create(internal::fn_format(buff,table_name,"","",
889
 
                              MY_REPLACE_EXT|MY_UNPACK_FILENAME),
890
 
                    keys, keydef,
891
 
                    column_count, columndef,
892
 
                    max_key_fieldnr, key_part_size,
893
 
                    share->reclength, mem_per_row_keys,
894
 
                    static_cast<uint32_t>(num_rows), /* We check for overflow above, so cast is fine here. */
895
 
                    0, // Factor out MIN
896
 
                    &hp_create_info, internal_share);
897
 
 
898
 
  free((unsigned char*) keydef);
899
 
  free((void *) columndef);
 
802
  hp_create_info.max_chunk_size= table_arg->getShare()->block_size;
 
803
 
 
804
  error= heap_create(table_name,
 
805
                     keys, &keydef[0],
 
806
                     column_count,
 
807
                     key_part_size,
 
808
                     table_arg->getShare()->getRecordLength(), mem_per_row_keys,
 
809
                     static_cast<uint32_t>(num_rows), /* We check for overflow above, so cast is fine here. */
 
810
                     0, // Factor out MIN
 
811
                     &hp_create_info, internal_share);
900
812
 
901
813
  return (error);
902
814
}