~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/heap/ha_heap.cc

  • Committer: Siddharth Prakash Singh
  • Date: 2010-03-26 17:24:57 UTC
  • mto: This revision was merged to the branch mainline in revision 1425.
  • Revision ID: spsneo@spsneo-laptop-20100326172457-vni09y22ktvvefmn
some more sprintf --> snprintf

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