~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/heap/ha_heap.cc

Merge Monty

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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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>
20
21
#include <drizzled/field/timestamp.h>
21
22
#include <drizzled/field/varstring.h>
22
23
#include "drizzled/plugin/daemon.h"
23
24
 
24
 
#include <boost/thread/mutex.hpp>
25
 
 
26
25
#include "heap.h"
27
26
#include "ha_heap.h"
28
27
 
34
33
 
35
34
static const string engine_name("MEMORY");
36
35
 
37
 
boost::mutex THR_LOCK_heap;
 
36
pthread_mutex_t THR_LOCK_heap= PTHREAD_MUTEX_INITIALIZER;
38
37
 
39
38
static const char *ha_heap_exts[] = {
40
39
  NULL
53
52
                          HTON_SKIP_STORE_LOCK |
54
53
                          HTON_TEMPORARY_ONLY)
55
54
  {
 
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);
61
63
  }
62
64
 
63
 
  virtual Cursor *create(Table &table)
 
65
  virtual Cursor *create(TableShare &table,
 
66
                          memory::Root *mem_root)
64
67
  {
65
 
    return new ha_heap(*this, table);
 
68
    return new (mem_root) ha_heap(*this, table);
66
69
  }
67
70
 
68
71
  const char **bas_ext() const {
71
74
 
72
75
  int doCreateTable(Session &session,
73
76
                    Table &table_arg,
74
 
                    const TableIdentifier &identifier,
 
77
                    drizzled::TableIdentifier &identifier,
75
78
                    message::Table &create_proto);
76
79
 
77
80
  /* For whatever reason, internal tables can be created by Cursor::open()
85
88
                        message::Table &create_proto,
86
89
                        HP_SHARE **internal_share);
87
90
 
88
 
  int doRenameTable(Session&, const TableIdentifier &from, const TableIdentifier &to);
 
91
  int doRenameTable(Session&, TableIdentifier &from, TableIdentifier &to);
89
92
 
90
 
  int doDropTable(Session&, const TableIdentifier &identifier);
 
93
  int doDropTable(Session&, TableIdentifier &identifier);
91
94
 
92
95
  int doGetTableDefinition(Session& session,
93
 
                           const TableIdentifier &identifier,
 
96
                           TableIdentifier &identifier,
94
97
                           message::Table &table_message);
95
98
 
 
99
  /* Temp only engine, so do not return values. */
 
100
  void doGetTableNames(CachedDirectory &, SchemaIdentifier& , set<string>&) { };
 
101
 
96
102
  uint32_t max_supported_keys()          const { return MAX_KEY; }
97
103
  uint32_t max_supported_key_part_length() const { return MAX_KEY_LENGTH; }
98
104
 
99
 
  uint32_t index_flags(enum  ha_key_alg ) const
 
105
  uint32_t index_flags(enum  ha_key_alg algorithm) const
100
106
  {
101
 
    return ( HA_ONLY_WHOLE_INDEX | HA_KEY_SCAN_NOT_ROR);
 
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);
102
114
  }
103
115
 
104
 
  bool doDoesTableExist(Session& session, const TableIdentifier &identifier);
105
 
  void doGetTableIdentifiers(CachedDirectory &directory,
106
 
                             const SchemaIdentifier &schema_identifier,
107
 
                             TableIdentifier::vector &set_of_identifiers);
 
116
  bool doDoesTableExist(Session& session, TableIdentifier &identifier);
 
117
  void doGetTableIdentifiers(drizzled::CachedDirectory &directory,
 
118
                             drizzled::SchemaIdentifier &schema_identifier,
 
119
                             drizzled::TableIdentifiers &set_of_identifiers);
108
120
};
109
121
 
110
 
void HeapEngine::doGetTableIdentifiers(CachedDirectory&,
111
 
                                       const SchemaIdentifier&,
112
 
                                       TableIdentifier::vector&)
 
122
void HeapEngine::doGetTableIdentifiers(drizzled::CachedDirectory&,
 
123
                                       drizzled::SchemaIdentifier&,
 
124
                                       drizzled::TableIdentifiers&)
113
125
{
114
126
}
115
127
 
116
 
bool HeapEngine::doDoesTableExist(Session& session, const TableIdentifier &identifier)
 
128
bool HeapEngine::doDoesTableExist(Session& session, TableIdentifier &identifier)
117
129
{
118
 
  return session.getMessageCache().doesTableMessageExist(identifier);
 
130
  return session.doesTableMessageExist(identifier);
119
131
}
120
132
 
121
133
int HeapEngine::doGetTableDefinition(Session &session,
122
 
                                     const TableIdentifier &identifier,
 
134
                                     TableIdentifier &identifier,
123
135
                                     message::Table &table_proto)
124
136
{
125
 
  if (session.getMessageCache().getTableMessage(identifier, table_proto))
 
137
  if (session.getTableMessage(identifier, table_proto))
126
138
    return EEXIST;
127
139
 
128
140
  return ENOENT;
131
143
  We have to ignore ENOENT entries as the MEMORY table is created on open and
132
144
  not when doing a CREATE on the table.
133
145
*/
134
 
int HeapEngine::doDropTable(Session &session, const TableIdentifier &identifier)
 
146
int HeapEngine::doDropTable(Session &session, TableIdentifier &identifier)
135
147
{
136
 
  session.getMessageCache().removeTableMessage(identifier);
 
148
  session.removeTableMessage(identifier);
137
149
 
138
150
  int error= heap_delete_table(identifier.getPath().c_str());
139
151
 
145
157
 
146
158
static HeapEngine *heap_storage_engine= NULL;
147
159
 
148
 
static int heap_init(module::Context &context)
 
160
static int heap_init(plugin::Context &context)
149
161
{
150
162
  heap_storage_engine= new HeapEngine(engine_name);
151
163
  context.add(heap_storage_engine);
158
170
*****************************************************************************/
159
171
 
160
172
ha_heap::ha_heap(plugin::StorageEngine &engine_arg,
161
 
                 Table &table_arg)
 
173
                 TableShare &table_arg)
162
174
  :Cursor(engine_arg, table_arg), file(0), records_changed(0), key_stat_version(0),
163
175
  internal_table(0)
164
176
{}
176
188
*/
177
189
#define MEMORY_STATS_UPDATE_THRESHOLD 10
178
190
 
179
 
int ha_heap::doOpen(const drizzled::TableIdentifier &identifier, int mode, uint32_t test_if_locked)
 
191
int ha_heap::open(const char *name, int mode, uint32_t test_if_locked)
180
192
{
181
 
  if ((test_if_locked & HA_OPEN_INTERNAL_TABLE) || (!(file= heap_open(identifier.getPath().c_str(), mode)) && errno == ENOENT))
 
193
  if ((test_if_locked & HA_OPEN_INTERNAL_TABLE) || (!(file= heap_open(name, mode)) && errno == ENOENT))
182
194
  {
 
195
    HA_CREATE_INFO create_info;
183
196
    internal_table= test(test_if_locked & HA_OPEN_INTERNAL_TABLE);
 
197
    memset(&create_info, 0, sizeof(create_info));
184
198
    file= 0;
185
199
    HP_SHARE *internal_share= NULL;
186
200
    message::Table create_proto;
187
201
 
188
 
    if (not heap_storage_engine->heap_create_table(getTable()->in_use,
189
 
                                                   identifier.getPath().c_str(),
190
 
                                                   getTable(),
191
 
                                                   internal_table,
192
 
                                                   create_proto,
193
 
                                                   &internal_share))
 
202
    if (!heap_storage_engine->heap_create_table(ha_session(), name, table,
 
203
                                                internal_table,
 
204
                                                create_proto,
 
205
                                                &internal_share))
194
206
    {
195
207
        file= internal_table ?
196
208
          heap_open_from_share(internal_share, mode) :
198
210
      if (!file)
199
211
      {
200
212
         /* Couldn't open table; Remove the newly created table */
201
 
        THR_LOCK_heap.lock();
 
213
        pthread_mutex_lock(&THR_LOCK_heap);
202
214
        hp_free(internal_share);
203
 
        THR_LOCK_heap.unlock();
 
215
        pthread_mutex_unlock(&THR_LOCK_heap);
204
216
      }
 
217
      implicit_emptied= 1;
205
218
    }
206
219
  }
207
220
  ref_length= sizeof(HEAP_PTR);
217
230
      ha_heap::info(), which is always called before key statistics are
218
231
      used.
219
232
    */
220
 
    key_stat_version= file->getShare()->key_stat_version - 1;
 
233
    key_stat_version= file->s->key_stat_version-1;
221
234
  }
222
235
  return (file ? 0 : 1);
223
236
}
233
246
 
234
247
  DESCRIPTION
235
248
    Do same as default implementation but use file->s->name instead of
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
 
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
238
251
    with '\'-delimited path.
239
252
*/
240
253
 
241
 
Cursor *ha_heap::clone(memory::Root *)
 
254
Cursor *ha_heap::clone(memory::Root *mem_root)
242
255
{
243
 
  Cursor *new_handler= getTable()->getMutableShare()->db_type()->getCursor(*getTable());
244
 
  TableIdentifier identifier(getTable()->getShare()->getSchemaName(),
245
 
                             getTable()->getShare()->getTableName(),
246
 
                             getTable()->getShare()->getPath());
 
256
  Cursor *new_handler= table->s->db_type()->getCursor(*table->s, mem_root);
247
257
 
248
 
  if (new_handler && !new_handler->ha_open(identifier, getTable()->db_stat,
 
258
  if (new_handler && !new_handler->ha_open(table, file->s->name, table->db_stat,
249
259
                                           HA_OPEN_IGNORE_IF_LOCKED))
250
260
    return new_handler;
251
261
  return NULL;
252
262
}
253
263
 
254
264
 
255
 
const char *ha_heap::index_type(uint32_t )
 
265
const char *ha_heap::index_type(uint32_t inx)
256
266
{
257
 
  return ("HASH");
 
267
  return ((table_share->key_info[inx].algorithm == HA_KEY_ALG_BTREE) ?
 
268
          "BTREE" : "HASH");
258
269
}
259
270
 
260
271
 
276
287
 
277
288
void ha_heap::set_keys_for_scanning(void)
278
289
{
 
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
  }
279
296
}
280
297
 
281
298
 
282
299
void ha_heap::update_key_stats()
283
300
{
284
 
  for (uint32_t i= 0; i < getTable()->getShare()->sizeKeys(); i++)
 
301
  for (uint32_t i= 0; i < table->s->keys; i++)
285
302
  {
286
 
    KeyInfo *key= &getTable()->key_info[i];
287
 
 
 
303
    KEY *key=table->key_info+i;
288
304
    if (!key->rec_per_key)
289
305
      continue;
290
 
 
 
306
    if (key->algorithm != HA_KEY_ALG_BTREE)
291
307
    {
292
308
      if (key->flags & HA_NOSAME)
293
309
        key->rec_per_key[key->key_parts-1]= 1;
294
310
      else
295
311
      {
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;
 
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;
298
314
        if (no_records < 2)
299
315
          no_records= 2;
300
316
        key->rec_per_key[key->key_parts-1]= no_records;
303
319
  }
304
320
  records_changed= 0;
305
321
  /* At the end of update_key_stats() we can proudly claim they are OK. */
306
 
  key_stat_version= file->getShare()->key_stat_version;
 
322
  key_stat_version= file->s->key_stat_version;
307
323
}
308
324
 
309
325
 
310
326
int ha_heap::doInsertRecord(unsigned char * buf)
311
327
{
312
328
  int res;
313
 
  if (getTable()->next_number_field && buf == getTable()->getInsertRecord())
 
329
  ha_statistic_increment(&system_status_var::ha_write_count);
 
330
  if (table->next_number_field && buf == table->record[0])
314
331
  {
315
332
    if ((res= update_auto_increment()))
316
333
      return res;
317
334
  }
318
335
  res= heap_write(file,buf);
319
336
  if (!res && (++records_changed*MEMORY_STATS_UPDATE_THRESHOLD >
320
 
               file->getShare()->records))
 
337
               file->s->records))
321
338
  {
322
339
    /*
323
340
       We can perform this safely since only one writer at the time is
324
341
       allowed on the table.
325
342
    */
326
 
    file->getShare()->key_stat_version++;
 
343
    file->s->key_stat_version++;
327
344
  }
328
345
  return res;
329
346
}
331
348
int ha_heap::doUpdateRecord(const unsigned char * old_data, unsigned char * new_data)
332
349
{
333
350
  int res;
334
 
 
 
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();
335
354
  res= heap_update(file,old_data,new_data);
336
355
  if (!res && ++records_changed*MEMORY_STATS_UPDATE_THRESHOLD >
337
 
              file->getShare()->records)
 
356
              file->s->records)
338
357
  {
339
358
    /*
340
359
       We can perform this safely since only one writer at the time is
341
360
       allowed on the table.
342
361
    */
343
 
    file->getShare()->key_stat_version++;
 
362
    file->s->key_stat_version++;
344
363
  }
345
364
  return res;
346
365
}
348
367
int ha_heap::doDeleteRecord(const unsigned char * buf)
349
368
{
350
369
  int res;
351
 
 
 
370
  ha_statistic_increment(&system_status_var::ha_delete_count);
352
371
  res= heap_delete(file,buf);
353
 
  if (!res && getTable()->getShare()->getType() == message::Table::STANDARD &&
354
 
      ++records_changed*MEMORY_STATS_UPDATE_THRESHOLD > file->getShare()->records)
 
372
  if (!res && table->s->tmp_table == message::Table::STANDARD &&
 
373
      ++records_changed*MEMORY_STATS_UPDATE_THRESHOLD > file->s->records)
355
374
  {
356
375
    /*
357
376
       We can perform this safely since only one writer at the time is
358
377
       allowed on the table.
359
378
    */
360
 
    file->getShare()->key_stat_version++;
 
379
    file->s->key_stat_version++;
361
380
  }
362
381
  return res;
363
382
}
369
388
  assert(inited==INDEX);
370
389
  ha_statistic_increment(&system_status_var::ha_read_key_count);
371
390
  int error = heap_rkey(file,buf,active_index, key, keypart_map, find_flag);
372
 
  getTable()->status = error ? STATUS_NOT_FOUND : 0;
 
391
  table->status = error ? STATUS_NOT_FOUND : 0;
373
392
  return error;
374
393
}
375
394
 
380
399
  ha_statistic_increment(&system_status_var::ha_read_key_count);
381
400
  int error= heap_rkey(file, buf, active_index, key, keypart_map,
382
401
                       HA_READ_PREFIX_LAST);
383
 
  getTable()->status= error ? STATUS_NOT_FOUND : 0;
 
402
  table->status= error ? STATUS_NOT_FOUND : 0;
384
403
  return error;
385
404
}
386
405
 
390
409
{
391
410
  ha_statistic_increment(&system_status_var::ha_read_key_count);
392
411
  int error = heap_rkey(file, buf, index, key, keypart_map, find_flag);
393
 
  getTable()->status = error ? STATUS_NOT_FOUND : 0;
 
412
  table->status = error ? STATUS_NOT_FOUND : 0;
394
413
  return error;
395
414
}
396
415
 
399
418
  assert(inited==INDEX);
400
419
  ha_statistic_increment(&system_status_var::ha_read_next_count);
401
420
  int error=heap_rnext(file,buf);
402
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
421
  table->status=error ? STATUS_NOT_FOUND: 0;
403
422
  return error;
404
423
}
405
424
 
408
427
  assert(inited==INDEX);
409
428
  ha_statistic_increment(&system_status_var::ha_read_prev_count);
410
429
  int error=heap_rprev(file,buf);
411
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
430
  table->status=error ? STATUS_NOT_FOUND: 0;
412
431
  return error;
413
432
}
414
433
 
417
436
  assert(inited==INDEX);
418
437
  ha_statistic_increment(&system_status_var::ha_read_first_count);
419
438
  int error=heap_rfirst(file, buf, active_index);
420
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
439
  table->status=error ? STATUS_NOT_FOUND: 0;
421
440
  return error;
422
441
}
423
442
 
426
445
  assert(inited==INDEX);
427
446
  ha_statistic_increment(&system_status_var::ha_read_last_count);
428
447
  int error=heap_rlast(file, buf, active_index);
429
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
448
  table->status=error ? STATUS_NOT_FOUND: 0;
430
449
  return error;
431
450
}
432
451
 
433
 
int ha_heap::doStartTableScan(bool scan)
 
452
int ha_heap::rnd_init(bool scan)
434
453
{
435
454
  return scan ? heap_scan_init(file) : 0;
436
455
}
439
458
{
440
459
  ha_statistic_increment(&system_status_var::ha_read_rnd_next_count);
441
460
  int error=heap_scan(file, buf);
442
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
461
  table->status=error ? STATUS_NOT_FOUND: 0;
443
462
  return error;
444
463
}
445
464
 
450
469
  ha_statistic_increment(&system_status_var::ha_read_rnd_count);
451
470
  memcpy(&heap_position, pos, sizeof(HEAP_PTR));
452
471
  error=heap_rrnd(file, buf, heap_position);
453
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
472
  table->status=error ? STATUS_NOT_FOUND: 0;
454
473
  return error;
455
474
}
456
475
 
479
498
    have to update the key statistics. Hoping that a table lock is now
480
499
    in place.
481
500
  */
482
 
  if (key_stat_version != file->getShare()->key_stat_version)
 
501
  if (key_stat_version != file->s->key_stat_version)
483
502
    update_key_stats();
484
503
  return 0;
485
504
}
486
505
 
 
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
 
487
515
int ha_heap::extra(enum ha_extra_function operation)
488
516
{
489
517
  return heap_extra(file,operation);
499
527
int ha_heap::delete_all_rows()
500
528
{
501
529
  heap_clear(file);
502
 
  if (getTable()->getShare()->getType() == message::Table::STANDARD)
 
530
  if (table->s->tmp_table == message::Table::STANDARD)
503
531
  {
504
532
    /*
505
533
       We can perform this safely since only one writer at the time is
506
534
       allowed on the table.
507
535
    */
508
 
    file->getShare()->key_stat_version++;
 
536
    file->s->key_stat_version++;
509
537
  }
510
538
  return 0;
511
539
}
619
647
 
620
648
void ha_heap::drop_table(const char *)
621
649
{
622
 
  file->getShare()->delete_on_close= 1;
 
650
  file->s->delete_on_close= 1;
623
651
  close();
624
652
}
625
653
 
626
654
 
627
 
int HeapEngine::doRenameTable(Session &session, const TableIdentifier &from, const TableIdentifier &to)
 
655
int HeapEngine::doRenameTable(Session &session, TableIdentifier &from, TableIdentifier &to)
628
656
{
629
 
  session.getMessageCache().renameTableMessage(from, to);
 
657
  session.renameTableMessage(from, to);
630
658
  return heap_rename(from.getPath().c_str(), to.getPath().c_str());
631
659
}
632
660
 
634
662
ha_rows ha_heap::records_in_range(uint32_t inx, key_range *min_key,
635
663
                                  key_range *max_key)
636
664
{
637
 
  KeyInfo *key= &getTable()->key_info[inx];
 
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);
638
668
 
639
669
  if (!min_key || !max_key ||
640
670
      min_key->length != max_key->length ||
647
677
    return stats.records;
648
678
 
649
679
  /* Assert that info() did run. We need current statistics here. */
650
 
  assert(key_stat_version == file->getShare()->key_stat_version);
 
680
  assert(key_stat_version == file->s->key_stat_version);
651
681
  return key->rec_per_key[key->key_parts-1];
652
682
}
653
683
 
654
684
int HeapEngine::doCreateTable(Session &session,
655
685
                              Table &table_arg,
656
 
                              const TableIdentifier &identifier,
 
686
                              drizzled::TableIdentifier &identifier,
657
687
                              message::Table& create_proto)
658
688
{
659
689
  int error;
667
697
 
668
698
  if (error == 0)
669
699
  {
670
 
    session.getMessageCache().storeTableMessage(identifier, create_proto);
 
700
    session.storeTableMessage(identifier, create_proto);
671
701
  }
672
702
 
673
703
  return error;
680
710
                                  message::Table &create_proto,
681
711
                                  HP_SHARE **internal_share)
682
712
{
683
 
  uint32_t key, parts, mem_per_row_keys= 0;
684
 
  uint32_t keys= table_arg->getShare()->sizeKeys();
 
713
  uint32_t key, parts, mem_per_row_keys= 0, keys= table_arg->s->keys;
685
714
  uint32_t auto_key= 0, auto_key_type= 0;
686
715
  uint32_t max_key_fieldnr = 0, key_part_size = 0, next_field_pos = 0;
687
 
  uint32_t column_count= table_arg->getShare()->sizeFields();
688
 
  std::vector<HP_KEYDEF> keydef;
 
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];
689
721
  int error;
 
722
  TableShare *share= table_arg->s;
690
723
  bool found_real_auto_increment= 0;
691
724
 
692
725
  /* 
695
728
   * can return a number more than that, we trap it here instead of casting
696
729
   * to a truncated integer.
697
730
   */
698
 
  uint64_t num_rows= table_arg->getShare()->getMaxRows();
 
731
  uint64_t num_rows= share->getMaxRows();
699
732
  if (num_rows > UINT32_MAX)
700
733
    return -1;
701
734
 
 
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
 
702
767
  for (key= parts= 0; key < keys; key++)
703
768
    parts+= table_arg->key_info[key].key_parts;
704
769
 
705
 
  keydef.resize(keys);
706
 
  std::vector<HA_KEYSEG> seg_buffer;
707
 
  seg_buffer.resize(parts);
708
 
  HA_KEYSEG *seg= &seg_buffer[0];
 
770
  if (!(keydef= (HP_KEYDEF*) malloc(keys * sizeof(HP_KEYDEF) +
 
771
                                    parts * sizeof(HA_KEYSEG))))
 
772
  {
 
773
    free((void *) columndef);
 
774
    return errno;
 
775
  }
709
776
 
 
777
  seg= reinterpret_cast<HA_KEYSEG*> (keydef + keys);
710
778
  for (key= 0; key < keys; key++)
711
779
  {
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;
 
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;
715
783
 
716
784
    keydef[key].keysegs=   (uint) pos->key_parts;
717
785
    keydef[key].flag=      (pos->flags & (HA_NOSAME | HA_NULL_ARE_EQUAL));
718
786
    keydef[key].seg=       seg;
719
787
 
720
 
    mem_per_row_keys+= sizeof(char*) * 2; // = sizeof(HASH_INFO)
 
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
    }
721
801
 
722
802
    for (; key_part != key_part_end; key_part++, seg++)
723
803
    {
724
804
      Field *field= key_part->field;
725
805
 
 
806
      if (pos->algorithm == HA_KEY_ALG_BTREE)
 
807
        seg->type= field->key_type();
 
808
      else
726
809
      {
727
810
        if ((seg->type = field->key_type()) != (int) HA_KEYTYPE_TEXT &&
728
811
            seg->type != HA_KEYTYPE_VARTEXT1 &&
738
821
      next_field_pos= seg->start + seg->length;
739
822
      if (field->type() == DRIZZLE_TYPE_VARCHAR)
740
823
      {
741
 
        next_field_pos+= (uint8_t)(((Field_varstring*)field)->pack_length_no_ptr());
 
824
        next_field_pos+= (uint8_t)(((Field_varstring*)field)->length_bytes);
742
825
      }
743
826
 
744
827
      if (next_field_pos > key_part_size) {
752
835
      if (field->null_ptr)
753
836
      {
754
837
        seg->null_bit= field->null_bit;
755
 
        seg->null_pos= (uint) (field->null_ptr - (unsigned char*) table_arg->getInsertRecord());
 
838
        seg->null_pos= (uint) (field->null_ptr - (unsigned char*) table_arg->record[0]);
756
839
      }
757
840
      else
758
841
      {
761
844
      }
762
845
      if (field->flags & AUTO_INCREMENT_FLAG &&
763
846
          table_arg->found_next_number_field &&
764
 
          key == table_arg->getShare()->next_number_index)
 
847
          key == share->next_number_index)
765
848
      {
766
849
        /*
767
850
          Store key number and type for found auto_increment key
770
853
        auto_key= key+ 1;
771
854
        auto_key_type= field->key_type();
772
855
      }
773
 
      if ((uint)field->position() + 1 > max_key_fieldnr)
 
856
      if ((uint)field->field_index + 1 > max_key_fieldnr)
774
857
      {
775
858
        /* Do not use seg->fieldnr as it's not reliable in case of temp tables */
776
 
        max_key_fieldnr= field->position() + 1;
 
859
        max_key_fieldnr= field->field_index + 1;
777
860
      }
778
861
    }
779
862
  }
780
863
 
781
 
  if (key_part_size < table_arg->getShare()->null_bytes + ((table_arg->getShare()->last_null_bit_pos+7) >> 3))
 
864
  if (key_part_size < share->null_bytes + ((share->last_null_bit_pos+7) >> 3))
782
865
  {
783
866
    /* Make sure to include null fields regardless of the presense of keys */
784
 
    key_part_size = table_arg->getShare()->null_bytes + ((table_arg->getShare()->last_null_bit_pos+7) >> 3);
 
867
    key_part_size = share->null_bytes + ((share->last_null_bit_pos+7) >> 3);
785
868
  }
786
869
 
787
870
 
788
871
 
789
872
  if (table_arg->found_next_number_field)
790
873
  {
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;
 
874
    keydef[share->next_number_index].flag|= HA_AUTO_KEY;
 
875
    found_real_auto_increment= share->next_number_key_offset == 0;
793
876
  }
794
877
  HP_CREATE_INFO hp_create_info;
795
878
  hp_create_info.auto_key= auto_key;
799
882
  hp_create_info.max_table_size=session->variables.max_heap_table_size;
800
883
  hp_create_info.with_auto_increment= found_real_auto_increment;
801
884
  hp_create_info.internal_table= internal_table;
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);
 
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);
812
900
 
813
901
  return (error);
814
902
}