~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/heap/ha_heap.cc

  • Committer: Monty Taylor
  • Date: 2010-03-25 22:03:21 UTC
  • mto: This revision was merged to the branch mainline in revision 1408.
  • Revision ID: mordred@inaugust.com-20100325220321-bf5vs8tryb4a8ssh
Updated the search for libmemcached.

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>
 
21
#include <drizzled/field/timestamp.h>
20
22
#include <drizzled/field/varstring.h>
21
 
#include <drizzled/plugin/daemon.h>
22
 
#include <drizzled/plugin/storage_engine.h>
23
 
 
24
 
#include <boost/thread/mutex.hpp>
 
23
#include "drizzled/plugin/daemon.h"
25
24
 
26
25
#include "heap.h"
27
26
#include "ha_heap.h"
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
50
49
                          HTON_FAST_KEY_READ |
51
50
                          HTON_NO_BLOBS |
52
51
                          HTON_HAS_RECORDS |
 
52
                          HTON_HAS_DATA_DICTIONARY |
53
53
                          HTON_SKIP_STORE_LOCK |
54
54
                          HTON_TEMPORARY_ONLY)
55
55
  {
 
56
    pthread_mutex_init(&THR_LOCK_heap, MY_MUTEX_INIT_FAST);
56
57
  }
57
58
 
58
59
  virtual ~HeapEngine()
59
60
  {
60
61
    hp_panic(HA_PANIC_CLOSE);
 
62
 
 
63
    pthread_mutex_destroy(&THR_LOCK_heap);
61
64
  }
62
65
 
63
 
  virtual Cursor *create(Table &table)
 
66
  virtual Cursor *create(TableShare &table,
 
67
                          memory::Root *mem_root)
64
68
  {
65
 
    return new ha_heap(*this, table);
 
69
    return new (mem_root) ha_heap(*this, table);
66
70
  }
67
71
 
68
72
  const char **bas_ext() const {
69
73
    return ha_heap_exts;
70
74
  }
71
75
 
72
 
  int doCreateTable(Session &session,
73
 
                    Table &table_arg,
74
 
                    const identifier::Table &identifier,
 
76
  int doCreateTable(Session *session,
 
77
                    Table& table_arg,
 
78
                    drizzled::TableIdentifier &identifier,
75
79
                    message::Table &create_proto);
76
80
 
77
81
  /* For whatever reason, internal tables can be created by Cursor::open()
85
89
                        message::Table &create_proto,
86
90
                        HP_SHARE **internal_share);
87
91
 
88
 
  int doRenameTable(Session&, const identifier::Table &from, const identifier::Table &to);
 
92
  int doRenameTable(Session&, TableIdentifier &from, TableIdentifier &to);
89
93
 
90
 
  int doDropTable(Session&, const identifier::Table &identifier);
 
94
  int doDropTable(Session&, TableIdentifier &identifier);
91
95
 
92
96
  int doGetTableDefinition(Session& session,
93
 
                           const identifier::Table &identifier,
 
97
                           TableIdentifier &identifier,
94
98
                           message::Table &table_message);
95
99
 
 
100
  /* Temp only engine, so do not return values. */
 
101
  void doGetTableNames(CachedDirectory &, string& , set<string>&) { };
 
102
 
96
103
  uint32_t max_supported_keys()          const { return MAX_KEY; }
97
104
  uint32_t max_supported_key_part_length() const { return MAX_KEY_LENGTH; }
98
105
 
99
 
  uint32_t index_flags(enum  ha_key_alg ) const
 
106
  uint32_t index_flags(enum  ha_key_alg algorithm) const
100
107
  {
101
 
    return ( HA_ONLY_WHOLE_INDEX | HA_KEY_SCAN_NOT_ROR);
 
108
    return ((algorithm == HA_KEY_ALG_BTREE) ?
 
109
            HA_READ_NEXT |
 
110
            HA_READ_PREV |
 
111
            HA_READ_ORDER |
 
112
            HA_READ_RANGE :
 
113
            HA_ONLY_WHOLE_INDEX |
 
114
            HA_KEY_SCAN_NOT_ROR);
102
115
  }
103
116
 
104
 
  bool doDoesTableExist(Session& session, const identifier::Table &identifier);
105
 
  void doGetTableIdentifiers(CachedDirectory &directory,
106
 
                             const identifier::Schema &schema_identifier,
107
 
                             identifier::Table::vector &set_of_identifiers);
 
117
  bool doDoesTableExist(Session& session, TableIdentifier &identifier);
108
118
};
109
119
 
110
 
void HeapEngine::doGetTableIdentifiers(CachedDirectory&,
111
 
                                       const identifier::Schema&,
112
 
                                       identifier::Table::vector&)
113
 
{
114
 
}
115
 
 
116
 
bool HeapEngine::doDoesTableExist(Session& session, const identifier::Table &identifier)
117
 
{
118
 
  return session.getMessageCache().doesTableMessageExist(identifier);
 
120
bool HeapEngine::doDoesTableExist(Session& session, TableIdentifier &identifier)
 
121
{
 
122
  return session.doesTableMessageExist(identifier);
119
123
}
120
124
 
121
125
int HeapEngine::doGetTableDefinition(Session &session,
122
 
                                     const identifier::Table &identifier,
 
126
                                     TableIdentifier &identifier,
123
127
                                     message::Table &table_proto)
124
128
{
125
 
  if (session.getMessageCache().getTableMessage(identifier, table_proto))
 
129
  if (session.getTableMessage(identifier, table_proto))
126
130
    return EEXIST;
127
131
 
128
132
  return ENOENT;
131
135
  We have to ignore ENOENT entries as the MEMORY table is created on open and
132
136
  not when doing a CREATE on the table.
133
137
*/
134
 
int HeapEngine::doDropTable(Session &session, const identifier::Table &identifier)
 
138
int HeapEngine::doDropTable(Session &session, TableIdentifier &identifier)
135
139
{
136
 
  session.getMessageCache().removeTableMessage(identifier);
137
 
 
138
 
  int error= heap_delete_table(identifier.getPath().c_str());
139
 
 
140
 
  if (error == ENOENT)
141
 
    error= 0;
142
 
 
143
 
  return error;
 
140
  session.removeTableMessage(identifier);
 
141
 
 
142
  return heap_delete_table(identifier.getPath().c_str());
144
143
}
145
144
 
146
145
static HeapEngine *heap_storage_engine= NULL;
147
146
 
148
 
static int heap_init(module::Context &context)
 
147
static int heap_init(plugin::Context &context)
149
148
{
150
149
  heap_storage_engine= new HeapEngine(engine_name);
151
150
  context.add(heap_storage_engine);
158
157
*****************************************************************************/
159
158
 
160
159
ha_heap::ha_heap(plugin::StorageEngine &engine_arg,
161
 
                 Table &table_arg)
 
160
                 TableShare &table_arg)
162
161
  :Cursor(engine_arg, table_arg), file(0), records_changed(0), key_stat_version(0),
163
162
  internal_table(0)
164
163
{}
176
175
*/
177
176
#define MEMORY_STATS_UPDATE_THRESHOLD 10
178
177
 
179
 
int ha_heap::doOpen(const drizzled::identifier::Table &identifier, int mode, uint32_t test_if_locked)
 
178
int ha_heap::open(const char *name, int mode, uint32_t test_if_locked)
180
179
{
181
 
  if ((test_if_locked & HA_OPEN_INTERNAL_TABLE) || (!(file= heap_open(identifier.getPath().c_str(), mode)) && errno == ENOENT))
 
180
  if ((test_if_locked & HA_OPEN_INTERNAL_TABLE) || (!(file= heap_open(name, mode)) && errno == ENOENT))
182
181
  {
 
182
    HA_CREATE_INFO create_info;
183
183
    internal_table= test(test_if_locked & HA_OPEN_INTERNAL_TABLE);
 
184
    memset(&create_info, 0, sizeof(create_info));
184
185
    file= 0;
185
186
    HP_SHARE *internal_share= NULL;
186
187
    message::Table create_proto;
187
188
 
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))
 
189
    if (!heap_storage_engine->heap_create_table(ha_session(), name, table,
 
190
                                                internal_table,
 
191
                                                create_proto,
 
192
                                                &internal_share))
194
193
    {
195
194
        file= internal_table ?
196
195
          heap_open_from_share(internal_share, mode) :
198
197
      if (!file)
199
198
      {
200
199
         /* Couldn't open table; Remove the newly created table */
201
 
        THR_LOCK_heap.lock();
 
200
        pthread_mutex_lock(&THR_LOCK_heap);
202
201
        hp_free(internal_share);
203
 
        THR_LOCK_heap.unlock();
 
202
        pthread_mutex_unlock(&THR_LOCK_heap);
204
203
      }
 
204
      implicit_emptied= 1;
205
205
    }
206
206
  }
207
207
  ref_length= sizeof(HEAP_PTR);
217
217
      ha_heap::info(), which is always called before key statistics are
218
218
      used.
219
219
    */
220
 
    key_stat_version= file->getShare()->key_stat_version - 1;
 
220
    key_stat_version= file->s->key_stat_version-1;
221
221
  }
222
222
  return (file ? 0 : 1);
223
223
}
233
233
 
234
234
  DESCRIPTION
235
235
    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
 
236
    table->s->path. This is needed by Windows where the clone() call sees
 
237
    '/'-delimited path in table->s->path, while ha_peap::open() was called
238
238
    with '\'-delimited path.
239
239
*/
240
240
 
241
 
Cursor *ha_heap::clone(memory::Root *)
 
241
Cursor *ha_heap::clone(memory::Root *mem_root)
242
242
{
243
 
  Cursor *new_handler= getTable()->getMutableShare()->db_type()->getCursor(*getTable());
244
 
  identifier::Table identifier(getTable()->getShare()->getSchemaName(),
245
 
                             getTable()->getShare()->getTableName(),
246
 
                             getTable()->getShare()->getPath());
 
243
  Cursor *new_handler= table->s->db_type()->getCursor(*table->s, mem_root);
247
244
 
248
 
  if (new_handler && !new_handler->ha_open(identifier, getTable()->db_stat,
 
245
  if (new_handler && !new_handler->ha_open(table, file->s->name, table->db_stat,
249
246
                                           HA_OPEN_IGNORE_IF_LOCKED))
250
247
    return new_handler;
251
248
  return NULL;
252
249
}
253
250
 
254
251
 
255
 
const char *ha_heap::index_type(uint32_t )
 
252
const char *ha_heap::index_type(uint32_t inx)
256
253
{
257
 
  return ("HASH");
 
254
  return ((table_share->key_info[inx].algorithm == HA_KEY_ALG_BTREE) ?
 
255
          "BTREE" : "HASH");
258
256
}
259
257
 
260
258
 
276
274
 
277
275
void ha_heap::set_keys_for_scanning(void)
278
276
{
 
277
  btree_keys.reset();
 
278
  for (uint32_t i= 0 ; i < table->s->keys ; i++)
 
279
  {
 
280
    if (table->key_info[i].algorithm == HA_KEY_ALG_BTREE)
 
281
      btree_keys.set(i);
 
282
  }
279
283
}
280
284
 
281
285
 
282
286
void ha_heap::update_key_stats()
283
287
{
284
 
  for (uint32_t i= 0; i < getTable()->getShare()->sizeKeys(); i++)
 
288
  for (uint32_t i= 0; i < table->s->keys; i++)
285
289
  {
286
 
    KeyInfo *key= &getTable()->key_info[i];
287
 
 
 
290
    KEY *key=table->key_info+i;
288
291
    if (!key->rec_per_key)
289
292
      continue;
290
 
 
 
293
    if (key->algorithm != HA_KEY_ALG_BTREE)
291
294
    {
292
295
      if (key->flags & HA_NOSAME)
293
296
        key->rec_per_key[key->key_parts-1]= 1;
294
297
      else
295
298
      {
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;
 
299
        ha_rows hash_buckets= file->s->keydef[i].hash_buckets;
 
300
        uint32_t no_records= hash_buckets ? (uint) (file->s->records/hash_buckets) : 2;
298
301
        if (no_records < 2)
299
302
          no_records= 2;
300
303
        key->rec_per_key[key->key_parts-1]= no_records;
303
306
  }
304
307
  records_changed= 0;
305
308
  /* At the end of update_key_stats() we can proudly claim they are OK. */
306
 
  key_stat_version= file->getShare()->key_stat_version;
 
309
  key_stat_version= file->s->key_stat_version;
307
310
}
308
311
 
309
312
 
310
 
int ha_heap::doInsertRecord(unsigned char * buf)
 
313
int ha_heap::write_row(unsigned char * buf)
311
314
{
312
315
  int res;
313
 
  if (getTable()->next_number_field && buf == getTable()->getInsertRecord())
 
316
  ha_statistic_increment(&system_status_var::ha_write_count);
 
317
  if (table->next_number_field && buf == table->record[0])
314
318
  {
315
319
    if ((res= update_auto_increment()))
316
320
      return res;
317
321
  }
318
322
  res= heap_write(file,buf);
319
323
  if (!res && (++records_changed*MEMORY_STATS_UPDATE_THRESHOLD >
320
 
               file->getShare()->records))
 
324
               file->s->records))
321
325
  {
322
326
    /*
323
327
       We can perform this safely since only one writer at the time is
324
328
       allowed on the table.
325
329
    */
326
 
    file->getShare()->key_stat_version++;
 
330
    file->s->key_stat_version++;
327
331
  }
328
332
  return res;
329
333
}
330
334
 
331
 
int ha_heap::doUpdateRecord(const unsigned char * old_data, unsigned char * new_data)
 
335
int ha_heap::update_row(const unsigned char * old_data, unsigned char * new_data)
332
336
{
333
337
  int res;
334
 
 
 
338
  ha_statistic_increment(&system_status_var::ha_update_count);
 
339
  if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_UPDATE)
 
340
    table->timestamp_field->set_time();
335
341
  res= heap_update(file,old_data,new_data);
336
342
  if (!res && ++records_changed*MEMORY_STATS_UPDATE_THRESHOLD >
337
 
              file->getShare()->records)
 
343
              file->s->records)
338
344
  {
339
345
    /*
340
346
       We can perform this safely since only one writer at the time is
341
347
       allowed on the table.
342
348
    */
343
 
    file->getShare()->key_stat_version++;
 
349
    file->s->key_stat_version++;
344
350
  }
345
351
  return res;
346
352
}
347
353
 
348
 
int ha_heap::doDeleteRecord(const unsigned char * buf)
 
354
int ha_heap::delete_row(const unsigned char * buf)
349
355
{
350
356
  int res;
351
 
 
 
357
  ha_statistic_increment(&system_status_var::ha_delete_count);
352
358
  res= heap_delete(file,buf);
353
 
  if (!res && getTable()->getShare()->getType() == message::Table::STANDARD &&
354
 
      ++records_changed*MEMORY_STATS_UPDATE_THRESHOLD > file->getShare()->records)
 
359
  if (!res && table->s->tmp_table == STANDARD_TABLE &&
 
360
      ++records_changed*MEMORY_STATS_UPDATE_THRESHOLD > file->s->records)
355
361
  {
356
362
    /*
357
363
       We can perform this safely since only one writer at the time is
358
364
       allowed on the table.
359
365
    */
360
 
    file->getShare()->key_stat_version++;
 
366
    file->s->key_stat_version++;
361
367
  }
362
368
  return res;
363
369
}
369
375
  assert(inited==INDEX);
370
376
  ha_statistic_increment(&system_status_var::ha_read_key_count);
371
377
  int error = heap_rkey(file,buf,active_index, key, keypart_map, find_flag);
372
 
  getTable()->status = error ? STATUS_NOT_FOUND : 0;
 
378
  table->status = error ? STATUS_NOT_FOUND : 0;
373
379
  return error;
374
380
}
375
381
 
380
386
  ha_statistic_increment(&system_status_var::ha_read_key_count);
381
387
  int error= heap_rkey(file, buf, active_index, key, keypart_map,
382
388
                       HA_READ_PREFIX_LAST);
383
 
  getTable()->status= error ? STATUS_NOT_FOUND : 0;
 
389
  table->status= error ? STATUS_NOT_FOUND : 0;
384
390
  return error;
385
391
}
386
392
 
390
396
{
391
397
  ha_statistic_increment(&system_status_var::ha_read_key_count);
392
398
  int error = heap_rkey(file, buf, index, key, keypart_map, find_flag);
393
 
  getTable()->status = error ? STATUS_NOT_FOUND : 0;
 
399
  table->status = error ? STATUS_NOT_FOUND : 0;
394
400
  return error;
395
401
}
396
402
 
399
405
  assert(inited==INDEX);
400
406
  ha_statistic_increment(&system_status_var::ha_read_next_count);
401
407
  int error=heap_rnext(file,buf);
402
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
408
  table->status=error ? STATUS_NOT_FOUND: 0;
403
409
  return error;
404
410
}
405
411
 
408
414
  assert(inited==INDEX);
409
415
  ha_statistic_increment(&system_status_var::ha_read_prev_count);
410
416
  int error=heap_rprev(file,buf);
411
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
417
  table->status=error ? STATUS_NOT_FOUND: 0;
412
418
  return error;
413
419
}
414
420
 
417
423
  assert(inited==INDEX);
418
424
  ha_statistic_increment(&system_status_var::ha_read_first_count);
419
425
  int error=heap_rfirst(file, buf, active_index);
420
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
426
  table->status=error ? STATUS_NOT_FOUND: 0;
421
427
  return error;
422
428
}
423
429
 
426
432
  assert(inited==INDEX);
427
433
  ha_statistic_increment(&system_status_var::ha_read_last_count);
428
434
  int error=heap_rlast(file, buf, active_index);
429
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
435
  table->status=error ? STATUS_NOT_FOUND: 0;
430
436
  return error;
431
437
}
432
438
 
433
 
int ha_heap::doStartTableScan(bool scan)
 
439
int ha_heap::rnd_init(bool scan)
434
440
{
435
441
  return scan ? heap_scan_init(file) : 0;
436
442
}
439
445
{
440
446
  ha_statistic_increment(&system_status_var::ha_read_rnd_next_count);
441
447
  int error=heap_scan(file, buf);
442
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
448
  table->status=error ? STATUS_NOT_FOUND: 0;
443
449
  return error;
444
450
}
445
451
 
450
456
  ha_statistic_increment(&system_status_var::ha_read_rnd_count);
451
457
  memcpy(&heap_position, pos, sizeof(HEAP_PTR));
452
458
  error=heap_rrnd(file, buf, heap_position);
453
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
459
  table->status=error ? STATUS_NOT_FOUND: 0;
454
460
  return error;
455
461
}
456
462
 
479
485
    have to update the key statistics. Hoping that a table lock is now
480
486
    in place.
481
487
  */
482
 
  if (key_stat_version != file->getShare()->key_stat_version)
 
488
  if (key_stat_version != file->s->key_stat_version)
483
489
    update_key_stats();
484
490
  return 0;
485
491
}
486
492
 
 
493
 
 
494
enum row_type ha_heap::get_row_type() const
 
495
{
 
496
  if (file->s->recordspace.is_variable_size)
 
497
    return ROW_TYPE_DYNAMIC;
 
498
 
 
499
  return ROW_TYPE_FIXED;
 
500
}
 
501
 
487
502
int ha_heap::extra(enum ha_extra_function operation)
488
503
{
489
504
  return heap_extra(file,operation);
499
514
int ha_heap::delete_all_rows()
500
515
{
501
516
  heap_clear(file);
502
 
  if (getTable()->getShare()->getType() == message::Table::STANDARD)
 
517
  if (table->s->tmp_table == STANDARD_TABLE)
503
518
  {
504
519
    /*
505
520
       We can perform this safely since only one writer at the time is
506
521
       allowed on the table.
507
522
    */
508
 
    file->getShare()->key_stat_version++;
 
523
    file->s->key_stat_version++;
509
524
  }
510
525
  return 0;
511
526
}
619
634
 
620
635
void ha_heap::drop_table(const char *)
621
636
{
622
 
  file->getShare()->delete_on_close= 1;
 
637
  file->s->delete_on_close= 1;
623
638
  close();
624
639
}
625
640
 
626
641
 
627
 
int HeapEngine::doRenameTable(Session &session, const identifier::Table &from, const identifier::Table &to)
 
642
int HeapEngine::doRenameTable(Session &session, TableIdentifier &from, TableIdentifier &to)
628
643
{
629
 
  session.getMessageCache().renameTableMessage(from, to);
 
644
  session.renameTableMessage(from, to);
630
645
  return heap_rename(from.getPath().c_str(), to.getPath().c_str());
631
646
}
632
647
 
634
649
ha_rows ha_heap::records_in_range(uint32_t inx, key_range *min_key,
635
650
                                  key_range *max_key)
636
651
{
637
 
  KeyInfo *key= &getTable()->key_info[inx];
 
652
  KEY *key=table->key_info+inx;
 
653
  if (key->algorithm == HA_KEY_ALG_BTREE)
 
654
    return hp_rb_records_in_range(file, inx, min_key, max_key);
638
655
 
639
656
  if (!min_key || !max_key ||
640
657
      min_key->length != max_key->length ||
647
664
    return stats.records;
648
665
 
649
666
  /* Assert that info() did run. We need current statistics here. */
650
 
  assert(key_stat_version == file->getShare()->key_stat_version);
 
667
  assert(key_stat_version == file->s->key_stat_version);
651
668
  return key->rec_per_key[key->key_parts-1];
652
669
}
653
670
 
654
 
int HeapEngine::doCreateTable(Session &session,
 
671
int HeapEngine::doCreateTable(Session *session,
655
672
                              Table &table_arg,
656
 
                              const identifier::Table &identifier,
 
673
                              drizzled::TableIdentifier &identifier,
657
674
                              message::Table& create_proto)
658
675
{
659
676
  int error;
660
677
  HP_SHARE *internal_share;
661
678
  const char *table_name= identifier.getPath().c_str();
662
679
 
663
 
  error= heap_create_table(&session, table_name, &table_arg,
 
680
  error= heap_create_table(session, table_name, &table_arg,
664
681
                           false, 
665
682
                           create_proto,
666
683
                           &internal_share);
667
684
 
668
685
  if (error == 0)
669
686
  {
670
 
    session.getMessageCache().storeTableMessage(identifier, create_proto);
 
687
    session->storeTableMessage(identifier, create_proto);
671
688
  }
672
689
 
673
690
  return error;
680
697
                                  message::Table &create_proto,
681
698
                                  HP_SHARE **internal_share)
682
699
{
683
 
  uint32_t key, parts, mem_per_row_keys= 0;
684
 
  uint32_t keys= table_arg->getShare()->sizeKeys();
 
700
  uint32_t key, parts, mem_per_row_keys= 0, keys= table_arg->s->keys;
685
701
  uint32_t auto_key= 0, auto_key_type= 0;
686
702
  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;
 
703
  uint32_t column_idx, column_count= table_arg->s->fields;
 
704
  HP_COLUMNDEF *columndef;
 
705
  HP_KEYDEF *keydef;
 
706
  HA_KEYSEG *seg;
 
707
  char buff[FN_REFLEN];
689
708
  int error;
 
709
  TableShare *share= table_arg->s;
690
710
  bool found_real_auto_increment= 0;
691
711
 
692
712
  /* 
695
715
   * can return a number more than that, we trap it here instead of casting
696
716
   * to a truncated integer.
697
717
   */
698
 
  uint64_t num_rows= table_arg->getShare()->getMaxRows();
 
718
  uint64_t num_rows= share->getMaxRows();
699
719
  if (num_rows > UINT32_MAX)
700
720
    return -1;
701
721
 
 
722
  if (!(columndef= (HP_COLUMNDEF*) malloc(column_count * sizeof(HP_COLUMNDEF))))
 
723
    return errno;
 
724
 
 
725
  for (column_idx= 0; column_idx < column_count; column_idx++)
 
726
  {
 
727
    Field* field= *(table_arg->field + column_idx);
 
728
    HP_COLUMNDEF* column= columndef + column_idx;
 
729
    column->type= (uint16_t)field->type();
 
730
    column->length= field->pack_length();
 
731
    column->offset= field->offset(field->table->record[0]);
 
732
 
 
733
    if (field->null_bit)
 
734
    {
 
735
      column->null_bit= field->null_bit;
 
736
      column->null_pos= (uint) (field->null_ptr - (unsigned char*) table_arg->record[0]);
 
737
    }
 
738
    else
 
739
    {
 
740
      column->null_bit= 0;
 
741
      column->null_pos= 0;
 
742
    }
 
743
 
 
744
    if (field->type() == DRIZZLE_TYPE_VARCHAR)
 
745
    {
 
746
      column->length_bytes= (uint8_t)(((Field_varstring*)field)->length_bytes);
 
747
    }
 
748
    else
 
749
    {
 
750
      column->length_bytes= 0;
 
751
    }
 
752
  }
 
753
 
702
754
  for (key= parts= 0; key < keys; key++)
703
755
    parts+= table_arg->key_info[key].key_parts;
704
756
 
705
 
  keydef.resize(keys);
706
 
  std::vector<HA_KEYSEG> seg_buffer;
707
 
  seg_buffer.resize(parts);
708
 
  HA_KEYSEG *seg= &seg_buffer[0];
 
757
  if (!(keydef= (HP_KEYDEF*) malloc(keys * sizeof(HP_KEYDEF) +
 
758
                                    parts * sizeof(HA_KEYSEG))))
 
759
  {
 
760
    free((void *) columndef);
 
761
    return errno;
 
762
  }
709
763
 
 
764
  seg= reinterpret_cast<HA_KEYSEG*> (keydef + keys);
710
765
  for (key= 0; key < keys; key++)
711
766
  {
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;
 
767
    KEY *pos= table_arg->key_info+key;
 
768
    KEY_PART_INFO *key_part=     pos->key_part;
 
769
    KEY_PART_INFO *key_part_end= key_part + pos->key_parts;
715
770
 
716
771
    keydef[key].keysegs=   (uint) pos->key_parts;
717
772
    keydef[key].flag=      (pos->flags & (HA_NOSAME | HA_NULL_ARE_EQUAL));
718
773
    keydef[key].seg=       seg;
719
774
 
720
 
    mem_per_row_keys+= sizeof(char*) * 2; // = sizeof(HASH_INFO)
 
775
    switch (pos->algorithm) {
 
776
    case HA_KEY_ALG_UNDEF:
 
777
    case HA_KEY_ALG_HASH:
 
778
      keydef[key].algorithm= HA_KEY_ALG_HASH;
 
779
      mem_per_row_keys+= sizeof(char*) * 2; // = sizeof(HASH_INFO)
 
780
      break;
 
781
    case HA_KEY_ALG_BTREE:
 
782
      keydef[key].algorithm= HA_KEY_ALG_BTREE;
 
783
      mem_per_row_keys+=sizeof(TREE_ELEMENT)+pos->key_length+sizeof(char*);
 
784
      break;
 
785
    default:
 
786
      assert(0); // cannot happen
 
787
    }
721
788
 
722
789
    for (; key_part != key_part_end; key_part++, seg++)
723
790
    {
724
791
      Field *field= key_part->field;
725
792
 
 
793
      if (pos->algorithm == HA_KEY_ALG_BTREE)
 
794
        seg->type= field->key_type();
 
795
      else
726
796
      {
727
797
        if ((seg->type = field->key_type()) != (int) HA_KEYTYPE_TEXT &&
728
798
            seg->type != HA_KEYTYPE_VARTEXT1 &&
738
808
      next_field_pos= seg->start + seg->length;
739
809
      if (field->type() == DRIZZLE_TYPE_VARCHAR)
740
810
      {
741
 
        next_field_pos+= (uint8_t)(((Field_varstring*)field)->pack_length_no_ptr());
 
811
        next_field_pos+= (uint8_t)(((Field_varstring*)field)->length_bytes);
742
812
      }
743
813
 
744
814
      if (next_field_pos > key_part_size) {
752
822
      if (field->null_ptr)
753
823
      {
754
824
        seg->null_bit= field->null_bit;
755
 
        seg->null_pos= (uint) (field->null_ptr - (unsigned char*) table_arg->getInsertRecord());
 
825
        seg->null_pos= (uint) (field->null_ptr - (unsigned char*) table_arg->record[0]);
756
826
      }
757
827
      else
758
828
      {
761
831
      }
762
832
      if (field->flags & AUTO_INCREMENT_FLAG &&
763
833
          table_arg->found_next_number_field &&
764
 
          key == table_arg->getShare()->next_number_index)
 
834
          key == share->next_number_index)
765
835
      {
766
836
        /*
767
837
          Store key number and type for found auto_increment key
770
840
        auto_key= key+ 1;
771
841
        auto_key_type= field->key_type();
772
842
      }
773
 
      if ((uint)field->position() + 1 > max_key_fieldnr)
 
843
      if ((uint)field->field_index + 1 > max_key_fieldnr)
774
844
      {
775
845
        /* Do not use seg->fieldnr as it's not reliable in case of temp tables */
776
 
        max_key_fieldnr= field->position() + 1;
 
846
        max_key_fieldnr= field->field_index + 1;
777
847
      }
778
848
    }
779
849
  }
780
850
 
781
 
  if (key_part_size < table_arg->getShare()->null_bytes + ((table_arg->getShare()->last_null_bit_pos+7) >> 3))
 
851
  if (key_part_size < share->null_bytes + ((share->last_null_bit_pos+7) >> 3))
782
852
  {
783
853
    /* 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);
 
854
    key_part_size = share->null_bytes + ((share->last_null_bit_pos+7) >> 3);
785
855
  }
786
856
 
787
857
 
788
858
 
789
859
  if (table_arg->found_next_number_field)
790
860
  {
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;
 
861
    keydef[share->next_number_index].flag|= HA_AUTO_KEY;
 
862
    found_real_auto_increment= share->next_number_key_offset == 0;
793
863
  }
794
864
  HP_CREATE_INFO hp_create_info;
795
865
  hp_create_info.auto_key= auto_key;
799
869
  hp_create_info.max_table_size=session->variables.max_heap_table_size;
800
870
  hp_create_info.with_auto_increment= found_real_auto_increment;
801
871
  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);
 
872
  hp_create_info.max_chunk_size= share->block_size;
 
873
  hp_create_info.is_dynamic= (share->row_type == ROW_TYPE_DYNAMIC);
 
874
 
 
875
  error= heap_create(internal::fn_format(buff,table_name,"","",
 
876
                              MY_REPLACE_EXT|MY_UNPACK_FILENAME),
 
877
                    keys, keydef,
 
878
                    column_count, columndef,
 
879
                    max_key_fieldnr, key_part_size,
 
880
                    share->reclength, mem_per_row_keys,
 
881
                    static_cast<uint32_t>(num_rows), /* We check for overflow above, so cast is fine here. */
 
882
                    0, // Factor out MIN
 
883
                    &hp_create_info, internal_share);
 
884
 
 
885
  free((unsigned char*) keydef);
 
886
  free((void *) columndef);
812
887
 
813
888
  return (error);
814
889
}
840
915
  "Hash based, stored in memory, useful for temporary tables",
841
916
  PLUGIN_LICENSE_GPL,
842
917
  heap_init,
843
 
  NULL,                       /* depends */
 
918
  NULL,                       /* system variables                */
844
919
  NULL                        /* config options                  */
845
920
}
846
921
DRIZZLE_DECLARE_PLUGIN_END;