~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/heap/ha_heap.cc

move functions from item.cc/item.h to item directory

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
 
#include "heap_priv.h"
 
16
#define DRIZZLE_SERVER 1
 
17
#include <drizzled/server_includes.h>
 
18
#include <storage/heap/ha_heap.h>
 
19
#include <storage/heap/heapdef.h>
17
20
#include <drizzled/error.h>
18
21
#include <drizzled/table.h>
19
22
#include <drizzled/session.h>
20
23
#include <drizzled/field/timestamp.h>
21
24
#include <drizzled/field/varstring.h>
22
 
#include "drizzled/plugin/daemon.h"
23
 
 
24
 
#include <boost/thread/mutex.hpp>
25
 
 
26
 
#include "heap.h"
27
 
#include "ha_heap.h"
28
 
 
29
 
#include <string>
30
 
 
31
 
 
32
 
using namespace drizzled;
33
 
using namespace std;
34
 
 
35
 
static const string engine_name("MEMORY");
36
 
 
37
 
boost::mutex THR_LOCK_heap;
38
 
 
39
 
static const char *ha_heap_exts[] = {
40
 
  NULL
41
 
};
42
 
 
43
 
class HeapEngine : public plugin::StorageEngine
44
 
{
45
 
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);
66
 
  }
67
 
 
68
 
  const char **bas_ext() const {
69
 
    return ha_heap_exts;
70
 
  }
71
 
 
72
 
  int doCreateTable(Session &session,
73
 
                    Table &table_arg,
74
 
                    const TableIdentifier &identifier,
75
 
                    message::Table &create_proto);
76
 
 
77
 
  /* For whatever reason, internal tables can be created by Cursor::open()
78
 
     for MEMORY.
79
 
     Instead of diving down a rat hole, let's just cry ourselves to sleep
80
 
     at night with this odd hackish workaround.
81
 
   */
82
 
  int heap_create_table(Session *session, const char *table_name,
83
 
                        Table *table_arg,
84
 
                        bool internal_table,
85
 
                        message::Table &create_proto,
86
 
                        HP_SHARE **internal_share);
87
 
 
88
 
  int doRenameTable(Session&, const TableIdentifier &from, const TableIdentifier &to);
89
 
 
90
 
  int doDropTable(Session&, const TableIdentifier &identifier);
91
 
 
92
 
  int doGetTableDefinition(Session& session,
93
 
                           const TableIdentifier &identifier,
94
 
                           message::Table &table_message);
95
 
 
96
 
  uint32_t max_supported_keys()          const { return MAX_KEY; }
97
 
  uint32_t max_supported_key_part_length() const { return MAX_KEY_LENGTH; }
98
 
 
99
 
  uint32_t index_flags(enum  ha_key_alg ) const
100
 
  {
101
 
    return ( HA_ONLY_WHOLE_INDEX | HA_KEY_SCAN_NOT_ROR);
102
 
  }
103
 
 
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
 
};
109
 
 
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,
123
 
                                     message::Table &table_proto)
124
 
{
125
 
  if (session.getMessageCache().getTableMessage(identifier, table_proto))
126
 
    return EEXIST;
127
 
 
128
 
  return ENOENT;
129
 
}
130
 
/*
131
 
  We have to ignore ENOENT entries as the MEMORY table is created on open and
132
 
  not when doing a CREATE on the table.
133
 
*/
134
 
int HeapEngine::doDropTable(Session &session, const TableIdentifier &identifier)
135
 
{
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;
144
 
}
145
 
 
146
 
static HeapEngine *heap_storage_engine= NULL;
147
 
 
148
 
static int heap_init(module::Context &context)
149
 
{
150
 
  heap_storage_engine= new HeapEngine(engine_name);
151
 
  context.add(heap_storage_engine);
 
25
 
 
26
pthread_mutex_t THR_LOCK_heap= PTHREAD_MUTEX_INITIALIZER;
 
27
 
 
28
static handler *heap_create_handler(handlerton *hton,
 
29
                                    TABLE_SHARE *table,
 
30
                                    MEM_ROOT *mem_root);
 
31
 
 
32
int heap_deinit(void *)
 
33
{
 
34
  pthread_mutex_init(&THR_LOCK_heap,MY_MUTEX_INIT_FAST);
 
35
 
 
36
  return hp_panic(HA_PANIC_CLOSE);
 
37
}
 
38
 
 
39
 
 
40
int heap_init(void *p)
 
41
{
 
42
  handlerton *heap_hton;
 
43
 
 
44
  heap_hton= (handlerton *)p;
 
45
  heap_hton->state=      SHOW_OPTION_YES;
 
46
  heap_hton->create=     heap_create_handler;
 
47
  heap_hton->flags=      HTON_CAN_RECREATE;
 
48
 
152
49
  return 0;
153
50
}
154
51
 
 
52
static handler *heap_create_handler(handlerton *hton,
 
53
                                    TABLE_SHARE *table, 
 
54
                                    MEM_ROOT *mem_root)
 
55
{
 
56
  return new (mem_root) ha_heap(hton, table);
 
57
}
 
58
 
155
59
 
156
60
/*****************************************************************************
157
 
** MEMORY tables
 
61
** HEAP tables
158
62
*****************************************************************************/
159
63
 
160
 
ha_heap::ha_heap(plugin::StorageEngine &engine_arg,
161
 
                 Table &table_arg)
162
 
  :Cursor(engine_arg, table_arg), file(0), records_changed(0), key_stat_version(0),
 
64
ha_heap::ha_heap(handlerton *hton, TABLE_SHARE *table_arg)
 
65
  :handler(hton, table_arg), file(0), records_changed(0), key_stat_version(0), 
163
66
  internal_table(0)
164
67
{}
165
68
 
 
69
 
 
70
static const char *ha_heap_exts[] = {
 
71
  NULL
 
72
};
 
73
 
 
74
const char **ha_heap::bas_ext() const
 
75
{
 
76
  return ha_heap_exts;
 
77
}
 
78
 
166
79
/*
167
 
  Hash index statistics is updated (copied from HP_KEYDEF::hash_buckets to
168
 
  rec_per_key) after 1/MEMORY_STATS_UPDATE_THRESHOLD fraction of table records
169
 
  have been inserted/updated/deleted. delete_all_rows() and table flush cause
 
80
  Hash index statistics is updated (copied from HP_KEYDEF::hash_buckets to 
 
81
  rec_per_key) after 1/HEAP_STATS_UPDATE_THRESHOLD fraction of table records 
 
82
  have been inserted/updated/deleted. delete_all_rows() and table flush cause 
170
83
  immediate update.
171
84
 
172
85
  NOTE
173
86
   hash index statistics must be updated when number of table records changes
174
 
   from 0 to non-zero value and vice versa. Otherwise records_in_range may
 
87
   from 0 to non-zero value and vice versa. Otherwise records_in_range may 
175
88
   erroneously return 0 and 'range' may miss records.
176
89
*/
177
 
#define MEMORY_STATS_UPDATE_THRESHOLD 10
 
90
#define HEAP_STATS_UPDATE_THRESHOLD 10
178
91
 
179
 
int ha_heap::doOpen(const drizzled::TableIdentifier &identifier, int mode, uint32_t test_if_locked)
 
92
int ha_heap::open(const char *name, int mode, uint32_t test_if_locked)
180
93
{
181
 
  if ((test_if_locked & HA_OPEN_INTERNAL_TABLE) || (!(file= heap_open(identifier.getPath().c_str(), mode)) && errno == ENOENT))
 
94
  if ((test_if_locked & HA_OPEN_INTERNAL_TABLE) || (!(file= heap_open(name, mode)) && my_errno == ENOENT))
182
95
  {
 
96
    HA_CREATE_INFO create_info;
183
97
    internal_table= test(test_if_locked & HA_OPEN_INTERNAL_TABLE);
 
98
    memset(&create_info, 0, sizeof(create_info));
184
99
    file= 0;
185
 
    HP_SHARE *internal_share= NULL;
186
 
    message::Table create_proto;
187
 
 
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))
 
100
    if (!create(name, table, &create_info))
194
101
    {
195
102
        file= internal_table ?
196
103
          heap_open_from_share(internal_share, mode) :
198
105
      if (!file)
199
106
      {
200
107
         /* Couldn't open table; Remove the newly created table */
201
 
        THR_LOCK_heap.lock();
 
108
        pthread_mutex_lock(&THR_LOCK_heap);
202
109
        hp_free(internal_share);
203
 
        THR_LOCK_heap.unlock();
 
110
        pthread_mutex_unlock(&THR_LOCK_heap);
204
111
      }
 
112
      implicit_emptied= 1;
205
113
    }
206
114
  }
207
115
  ref_length= sizeof(HEAP_PTR);
217
125
      ha_heap::info(), which is always called before key statistics are
218
126
      used.
219
127
    */
220
 
    key_stat_version= file->getShare()->key_stat_version - 1;
 
128
    key_stat_version= file->s->key_stat_version-1;
221
129
  }
222
130
  return (file ? 0 : 1);
223
131
}
232
140
  Create a copy of this table
233
141
 
234
142
  DESCRIPTION
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
 
143
    Do same as default implementation but use file->s->name instead of 
 
144
    table->s->path. This is needed by Windows where the clone() call sees
 
145
    '/'-delimited path in table->s->path, while ha_peap::open() was called 
238
146
    with '\'-delimited path.
239
147
*/
240
148
 
241
 
Cursor *ha_heap::clone(memory::Root *)
 
149
handler *ha_heap::clone(MEM_ROOT *mem_root)
242
150
{
243
 
  Cursor *new_handler= getTable()->getMutableShare()->db_type()->getCursor(*getTable());
244
 
  TableIdentifier identifier(getTable()->getShare()->getSchemaName(),
245
 
                             getTable()->getShare()->getTableName(),
246
 
                             getTable()->getShare()->getPath());
247
 
 
248
 
  if (new_handler && !new_handler->ha_open(identifier, getTable()->db_stat,
 
151
  handler *new_handler= get_new_handler(table->s, mem_root, table->s->db_type());
 
152
  if (new_handler && !new_handler->ha_open(table, file->s->name, table->db_stat,
249
153
                                           HA_OPEN_IGNORE_IF_LOCKED))
250
154
    return new_handler;
251
 
  return NULL;
252
 
}
253
 
 
254
 
 
255
 
const char *ha_heap::index_type(uint32_t )
256
 
{
257
 
  return ("HASH");
 
155
  return NULL;  /* purecov: inspected */
 
156
}
 
157
 
 
158
 
 
159
const char *ha_heap::index_type(uint32_t inx)
 
160
{
 
161
  return ((table_share->key_info[inx].algorithm == HA_KEY_ALG_BTREE) ?
 
162
          "BTREE" : "HASH");
 
163
}
 
164
 
 
165
 
 
166
uint32_t ha_heap::index_flags(uint32_t inx, uint32_t, bool) const
 
167
{
 
168
  return ((table_share->key_info[inx].algorithm == HA_KEY_ALG_BTREE) ?
 
169
          HA_READ_NEXT | HA_READ_PREV | HA_READ_ORDER | HA_READ_RANGE :
 
170
          HA_ONLY_WHOLE_INDEX | HA_KEY_SCAN_NOT_ROR);
258
171
}
259
172
 
260
173
 
276
189
 
277
190
void ha_heap::set_keys_for_scanning(void)
278
191
{
 
192
  btree_keys.clear_all();
 
193
  for (uint32_t i= 0 ; i < table->s->keys ; i++)
 
194
  {
 
195
    if (table->key_info[i].algorithm == HA_KEY_ALG_BTREE)
 
196
      btree_keys.set_bit(i);
 
197
  }
279
198
}
280
199
 
281
200
 
282
201
void ha_heap::update_key_stats()
283
202
{
284
 
  for (uint32_t i= 0; i < getTable()->getShare()->sizeKeys(); i++)
 
203
  for (uint32_t i= 0; i < table->s->keys; i++)
285
204
  {
286
 
    KeyInfo *key= &getTable()->key_info[i];
287
 
 
 
205
    KEY *key=table->key_info+i;
288
206
    if (!key->rec_per_key)
289
207
      continue;
290
 
 
 
208
    if (key->algorithm != HA_KEY_ALG_BTREE)
291
209
    {
292
210
      if (key->flags & HA_NOSAME)
293
211
        key->rec_per_key[key->key_parts-1]= 1;
294
212
      else
295
213
      {
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;
 
214
        ha_rows hash_buckets= file->s->keydef[i].hash_buckets;
 
215
        uint32_t no_records= hash_buckets ? (uint) (file->s->records/hash_buckets) : 2;
298
216
        if (no_records < 2)
299
217
          no_records= 2;
300
218
        key->rec_per_key[key->key_parts-1]= no_records;
303
221
  }
304
222
  records_changed= 0;
305
223
  /* At the end of update_key_stats() we can proudly claim they are OK. */
306
 
  key_stat_version= file->getShare()->key_stat_version;
 
224
  key_stat_version= file->s->key_stat_version;
307
225
}
308
226
 
309
227
 
310
 
int ha_heap::doInsertRecord(unsigned char * buf)
 
228
int ha_heap::write_row(unsigned char * buf)
311
229
{
312
230
  int res;
313
 
  if (getTable()->next_number_field && buf == getTable()->getInsertRecord())
 
231
  ha_statistic_increment(&SSV::ha_write_count);
 
232
  if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_INSERT)
 
233
    table->timestamp_field->set_time();
 
234
  if (table->next_number_field && buf == table->record[0])
314
235
  {
315
236
    if ((res= update_auto_increment()))
316
237
      return res;
317
238
  }
318
239
  res= heap_write(file,buf);
319
 
  if (!res && (++records_changed*MEMORY_STATS_UPDATE_THRESHOLD >
320
 
               file->getShare()->records))
 
240
  if (!res && (++records_changed*HEAP_STATS_UPDATE_THRESHOLD >
 
241
               file->s->records))
321
242
  {
322
243
    /*
323
244
       We can perform this safely since only one writer at the time is
324
245
       allowed on the table.
325
246
    */
326
 
    file->getShare()->key_stat_version++;
 
247
    file->s->key_stat_version++;
327
248
  }
328
249
  return res;
329
250
}
330
251
 
331
 
int ha_heap::doUpdateRecord(const unsigned char * old_data, unsigned char * new_data)
 
252
int ha_heap::update_row(const unsigned char * old_data, unsigned char * new_data)
332
253
{
333
254
  int res;
334
 
 
 
255
  ha_statistic_increment(&SSV::ha_update_count);
 
256
  if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_UPDATE)
 
257
    table->timestamp_field->set_time();
335
258
  res= heap_update(file,old_data,new_data);
336
 
  if (!res && ++records_changed*MEMORY_STATS_UPDATE_THRESHOLD >
337
 
              file->getShare()->records)
 
259
  if (!res && ++records_changed*HEAP_STATS_UPDATE_THRESHOLD > 
 
260
              file->s->records)
338
261
  {
339
262
    /*
340
263
       We can perform this safely since only one writer at the time is
341
264
       allowed on the table.
342
265
    */
343
 
    file->getShare()->key_stat_version++;
 
266
    file->s->key_stat_version++;
344
267
  }
345
268
  return res;
346
269
}
347
270
 
348
 
int ha_heap::doDeleteRecord(const unsigned char * buf)
 
271
int ha_heap::delete_row(const unsigned char * buf)
349
272
{
350
273
  int res;
351
 
 
 
274
  ha_statistic_increment(&SSV::ha_delete_count);
352
275
  res= heap_delete(file,buf);
353
 
  if (!res && getTable()->getShare()->getType() == message::Table::STANDARD &&
354
 
      ++records_changed*MEMORY_STATS_UPDATE_THRESHOLD > file->getShare()->records)
 
276
  if (!res && table->s->tmp_table == NO_TMP_TABLE && 
 
277
      ++records_changed*HEAP_STATS_UPDATE_THRESHOLD > file->s->records)
355
278
  {
356
279
    /*
357
280
       We can perform this safely since only one writer at the time is
358
281
       allowed on the table.
359
282
    */
360
 
    file->getShare()->key_stat_version++;
 
283
    file->s->key_stat_version++;
361
284
  }
362
285
  return res;
363
286
}
367
290
                            enum ha_rkey_function find_flag)
368
291
{
369
292
  assert(inited==INDEX);
370
 
  ha_statistic_increment(&system_status_var::ha_read_key_count);
 
293
  ha_statistic_increment(&SSV::ha_read_key_count);
371
294
  int error = heap_rkey(file,buf,active_index, key, keypart_map, find_flag);
372
 
  getTable()->status = error ? STATUS_NOT_FOUND : 0;
 
295
  table->status = error ? STATUS_NOT_FOUND : 0;
373
296
  return error;
374
297
}
375
298
 
377
300
                                 key_part_map keypart_map)
378
301
{
379
302
  assert(inited==INDEX);
380
 
  ha_statistic_increment(&system_status_var::ha_read_key_count);
 
303
  ha_statistic_increment(&SSV::ha_read_key_count);
381
304
  int error= heap_rkey(file, buf, active_index, key, keypart_map,
382
305
                       HA_READ_PREFIX_LAST);
383
 
  getTable()->status= error ? STATUS_NOT_FOUND : 0;
 
306
  table->status= error ? STATUS_NOT_FOUND : 0;
384
307
  return error;
385
308
}
386
309
 
388
311
                                key_part_map keypart_map,
389
312
                                enum ha_rkey_function find_flag)
390
313
{
391
 
  ha_statistic_increment(&system_status_var::ha_read_key_count);
 
314
  ha_statistic_increment(&SSV::ha_read_key_count);
392
315
  int error = heap_rkey(file, buf, index, key, keypart_map, find_flag);
393
 
  getTable()->status = error ? STATUS_NOT_FOUND : 0;
 
316
  table->status = error ? STATUS_NOT_FOUND : 0;
394
317
  return error;
395
318
}
396
319
 
397
320
int ha_heap::index_next(unsigned char * buf)
398
321
{
399
322
  assert(inited==INDEX);
400
 
  ha_statistic_increment(&system_status_var::ha_read_next_count);
 
323
  ha_statistic_increment(&SSV::ha_read_next_count);
401
324
  int error=heap_rnext(file,buf);
402
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
325
  table->status=error ? STATUS_NOT_FOUND: 0;
403
326
  return error;
404
327
}
405
328
 
406
329
int ha_heap::index_prev(unsigned char * buf)
407
330
{
408
331
  assert(inited==INDEX);
409
 
  ha_statistic_increment(&system_status_var::ha_read_prev_count);
 
332
  ha_statistic_increment(&SSV::ha_read_prev_count);
410
333
  int error=heap_rprev(file,buf);
411
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
334
  table->status=error ? STATUS_NOT_FOUND: 0;
412
335
  return error;
413
336
}
414
337
 
415
338
int ha_heap::index_first(unsigned char * buf)
416
339
{
417
340
  assert(inited==INDEX);
418
 
  ha_statistic_increment(&system_status_var::ha_read_first_count);
 
341
  ha_statistic_increment(&SSV::ha_read_first_count);
419
342
  int error=heap_rfirst(file, buf, active_index);
420
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
343
  table->status=error ? STATUS_NOT_FOUND: 0;
421
344
  return error;
422
345
}
423
346
 
424
347
int ha_heap::index_last(unsigned char * buf)
425
348
{
426
349
  assert(inited==INDEX);
427
 
  ha_statistic_increment(&system_status_var::ha_read_last_count);
 
350
  ha_statistic_increment(&SSV::ha_read_last_count);
428
351
  int error=heap_rlast(file, buf, active_index);
429
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
352
  table->status=error ? STATUS_NOT_FOUND: 0;
430
353
  return error;
431
354
}
432
355
 
433
 
int ha_heap::doStartTableScan(bool scan)
 
356
int ha_heap::rnd_init(bool scan)
434
357
{
435
358
  return scan ? heap_scan_init(file) : 0;
436
359
}
437
360
 
438
361
int ha_heap::rnd_next(unsigned char *buf)
439
362
{
440
 
  ha_statistic_increment(&system_status_var::ha_read_rnd_next_count);
 
363
  ha_statistic_increment(&SSV::ha_read_rnd_next_count);
441
364
  int error=heap_scan(file, buf);
442
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
365
  table->status=error ? STATUS_NOT_FOUND: 0;
443
366
  return error;
444
367
}
445
368
 
447
370
{
448
371
  int error;
449
372
  HEAP_PTR heap_position;
450
 
  ha_statistic_increment(&system_status_var::ha_read_rnd_count);
 
373
  ha_statistic_increment(&SSV::ha_read_rnd_count);
451
374
  memcpy(&heap_position, pos, sizeof(HEAP_PTR));
452
375
  error=heap_rrnd(file, buf, heap_position);
453
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
376
  table->status=error ? STATUS_NOT_FOUND: 0;
454
377
  return error;
455
378
}
456
379
 
457
 
void ha_heap::position(const unsigned char *)
 
380
void ha_heap::position(const unsigned char *record __attribute__((unused)))
458
381
{
459
382
  *(HEAP_PTR*) ref= heap_position(file);        // Ref is aligned
460
383
}
479
402
    have to update the key statistics. Hoping that a table lock is now
480
403
    in place.
481
404
  */
482
 
  if (key_stat_version != file->getShare()->key_stat_version)
 
405
  if (key_stat_version != file->s->key_stat_version)
483
406
    update_key_stats();
484
407
  return 0;
485
408
}
486
409
 
 
410
 
 
411
enum row_type ha_heap::get_row_type() const
 
412
{
 
413
  if (file->s->recordspace.is_variable_size)
 
414
    return ROW_TYPE_DYNAMIC;
 
415
 
 
416
  return ROW_TYPE_FIXED;
 
417
}
 
418
 
487
419
int ha_heap::extra(enum ha_extra_function operation)
488
420
{
489
421
  return heap_extra(file,operation);
499
431
int ha_heap::delete_all_rows()
500
432
{
501
433
  heap_clear(file);
502
 
  if (getTable()->getShare()->getType() == message::Table::STANDARD)
 
434
  if (table->s->tmp_table == NO_TMP_TABLE)
503
435
  {
504
436
    /*
505
437
       We can perform this safely since only one writer at the time is
506
438
       allowed on the table.
507
439
    */
508
 
    file->getShare()->key_stat_version++;
 
440
    file->s->key_stat_version++;
509
441
  }
510
442
  return 0;
511
443
}
512
444
 
 
445
int ha_heap::external_lock(Session *session __attribute__((unused)),
 
446
                           int lock_type __attribute__((unused)))
 
447
{
 
448
  return 0;                                     // No external locking
 
449
}
 
450
 
 
451
 
513
452
/*
514
453
  Disable indexes.
515
454
 
568
507
    The indexes might have been disabled by disable_index() before.
569
508
    The function works only if both data and indexes are empty,
570
509
    since the heap storage engine cannot repair the indexes.
571
 
    To be sure, call Cursor::delete_all_rows() before.
 
510
    To be sure, call handler::delete_all_rows() before.
572
511
 
573
512
  IMPLEMENTATION
574
513
    HA_KEY_SWITCH_NONUNIQ       is not implemented.
617
556
  return heap_indexes_are_disabled(file);
618
557
}
619
558
 
620
 
void ha_heap::drop_table(const char *)
621
 
{
622
 
  file->getShare()->delete_on_close= 1;
 
559
THR_LOCK_DATA **ha_heap::store_lock(Session *session __attribute__((unused)),
 
560
                                    THR_LOCK_DATA **to,
 
561
                                    enum thr_lock_type lock_type)
 
562
{
 
563
  if (lock_type != TL_IGNORE && file->lock.type == TL_UNLOCK)
 
564
    file->lock.type=lock_type;
 
565
  *to++= &file->lock;
 
566
  return to;
 
567
}
 
568
 
 
569
/*
 
570
  We have to ignore ENOENT entries as the HEAP table is created on open and
 
571
  not when doing a CREATE on the table.
 
572
*/
 
573
 
 
574
int ha_heap::delete_table(const char *name)
 
575
{
 
576
  return heap_delete_table(name);
 
577
}
 
578
 
 
579
 
 
580
void ha_heap::drop_table(const char *name __attribute__((unused)))
 
581
{
 
582
  file->s->delete_on_close= 1;
623
583
  close();
624
584
}
625
585
 
626
586
 
627
 
int HeapEngine::doRenameTable(Session &session, const TableIdentifier &from, const TableIdentifier &to)
 
587
int ha_heap::rename_table(const char * from, const char * to)
628
588
{
629
 
  session.getMessageCache().renameTableMessage(from, to);
630
 
  return heap_rename(from.getPath().c_str(), to.getPath().c_str());
 
589
  return heap_rename(from,to);
631
590
}
632
591
 
633
592
 
634
593
ha_rows ha_heap::records_in_range(uint32_t inx, key_range *min_key,
635
594
                                  key_range *max_key)
636
595
{
637
 
  KeyInfo *key= &getTable()->key_info[inx];
 
596
  KEY *key=table->key_info+inx;
 
597
  if (key->algorithm == HA_KEY_ALG_BTREE)
 
598
    return hp_rb_records_in_range(file, inx, min_key, max_key);
638
599
 
639
600
  if (!min_key || !max_key ||
640
601
      min_key->length != max_key->length ||
647
608
    return stats.records;
648
609
 
649
610
  /* Assert that info() did run. We need current statistics here. */
650
 
  assert(key_stat_version == file->getShare()->key_stat_version);
 
611
  assert(key_stat_version == file->s->key_stat_version);
651
612
  return key->rec_per_key[key->key_parts-1];
652
613
}
653
614
 
654
 
int HeapEngine::doCreateTable(Session &session,
655
 
                              Table &table_arg,
656
 
                              const TableIdentifier &identifier,
657
 
                              message::Table& create_proto)
658
 
{
659
 
  int error;
660
 
  HP_SHARE *internal_share;
661
 
  const char *table_name= identifier.getPath().c_str();
662
 
 
663
 
  error= heap_create_table(&session, table_name, &table_arg,
664
 
                           false, 
665
 
                           create_proto,
666
 
                           &internal_share);
667
 
 
668
 
  if (error == 0)
669
 
  {
670
 
    session.getMessageCache().storeTableMessage(identifier, create_proto);
671
 
  }
672
 
 
673
 
  return error;
674
 
}
675
 
 
676
 
 
677
 
int HeapEngine::heap_create_table(Session *session, const char *table_name,
678
 
                                  Table *table_arg,
679
 
                                  bool internal_table, 
680
 
                                  message::Table &create_proto,
681
 
                                  HP_SHARE **internal_share)
682
 
{
683
 
  uint32_t key, parts, mem_per_row_keys= 0;
684
 
  uint32_t keys= table_arg->getShare()->sizeKeys();
 
615
 
 
616
int ha_heap::create(const char *name, Table *table_arg,
 
617
                    HA_CREATE_INFO *create_info)
 
618
{
 
619
  uint32_t key, parts, mem_per_row_keys= 0, keys= table_arg->s->keys;
685
620
  uint32_t auto_key= 0, auto_key_type= 0;
686
621
  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;
 
622
  uint32_t column_idx, column_count= table_arg->s->fields;
 
623
  HP_COLUMNDEF *columndef;
 
624
  HP_KEYDEF *keydef;
 
625
  HA_KEYSEG *seg;
 
626
  char buff[FN_REFLEN];
689
627
  int error;
 
628
  TABLE_SHARE *share= table_arg->s;
690
629
  bool found_real_auto_increment= 0;
691
630
 
692
 
  /* 
693
 
   * We cannot create tables with more rows than UINT32_MAX.  This is a
694
 
   * limitation of the HEAP engine.  Here, since TableShare::getMaxRows()
695
 
   * can return a number more than that, we trap it here instead of casting
696
 
   * to a truncated integer.
697
 
   */
698
 
  uint64_t num_rows= table_arg->getShare()->getMaxRows();
699
 
  if (num_rows > UINT32_MAX)
700
 
    return -1;
 
631
  if (!(columndef= (HP_COLUMNDEF*) my_malloc(column_count * sizeof(HP_COLUMNDEF), MYF(MY_WME))))
 
632
    return my_errno;
 
633
 
 
634
  for (column_idx= 0; column_idx < column_count; column_idx++)
 
635
  {
 
636
    Field* field= *(table_arg->field + column_idx);
 
637
    HP_COLUMNDEF* column= columndef + column_idx;
 
638
    column->type= (uint16_t)field->type();
 
639
    column->length= field->pack_length();
 
640
    column->offset= field->offset(field->table->record[0]);
 
641
 
 
642
    if (field->null_bit)
 
643
    {
 
644
      column->null_bit= field->null_bit;
 
645
      column->null_pos= (uint) (field->null_ptr - (unsigned char*) table_arg->record[0]);
 
646
    }
 
647
    else
 
648
    {
 
649
      column->null_bit= 0;
 
650
      column->null_pos= 0;
 
651
    }
 
652
 
 
653
    if (field->type() == DRIZZLE_TYPE_VARCHAR)
 
654
    {
 
655
      column->length_bytes= (uint8_t)(((Field_varstring*)field)->length_bytes);
 
656
    }
 
657
    else
 
658
    {
 
659
      column->length_bytes= 0;
 
660
    }
 
661
  }
701
662
 
702
663
  for (key= parts= 0; key < keys; key++)
703
664
    parts+= table_arg->key_info[key].key_parts;
704
665
 
705
 
  keydef.resize(keys);
706
 
  std::vector<HA_KEYSEG> seg_buffer;
707
 
  seg_buffer.resize(parts);
708
 
  HA_KEYSEG *seg= &seg_buffer[0];
 
666
  if (!(keydef= (HP_KEYDEF*) my_malloc(keys * sizeof(HP_KEYDEF) +
 
667
                                       parts * sizeof(HA_KEYSEG),
 
668
                                       MYF(MY_WME))))
 
669
  {
 
670
    free((void *) columndef);
 
671
    return my_errno;
 
672
  }
709
673
 
 
674
  seg= reinterpret_cast<HA_KEYSEG*> (keydef + keys);
710
675
  for (key= 0; key < keys; key++)
711
676
  {
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;
 
677
    KEY *pos= table_arg->key_info+key;
 
678
    KEY_PART_INFO *key_part=     pos->key_part;
 
679
    KEY_PART_INFO *key_part_end= key_part + pos->key_parts;
715
680
 
716
681
    keydef[key].keysegs=   (uint) pos->key_parts;
717
682
    keydef[key].flag=      (pos->flags & (HA_NOSAME | HA_NULL_ARE_EQUAL));
718
683
    keydef[key].seg=       seg;
719
684
 
720
 
    mem_per_row_keys+= sizeof(char*) * 2; // = sizeof(HASH_INFO)
 
685
    switch (pos->algorithm) {
 
686
    case HA_KEY_ALG_UNDEF:
 
687
    case HA_KEY_ALG_HASH:
 
688
      keydef[key].algorithm= HA_KEY_ALG_HASH;
 
689
      mem_per_row_keys+= sizeof(char*) * 2; // = sizeof(HASH_INFO)
 
690
      break;
 
691
    case HA_KEY_ALG_BTREE:
 
692
      keydef[key].algorithm= HA_KEY_ALG_BTREE;
 
693
      mem_per_row_keys+=sizeof(TREE_ELEMENT)+pos->key_length+sizeof(char*);
 
694
      break;
 
695
    default:
 
696
      assert(0); // cannot happen
 
697
    }
721
698
 
722
699
    for (; key_part != key_part_end; key_part++, seg++)
723
700
    {
724
701
      Field *field= key_part->field;
725
702
 
 
703
      if (pos->algorithm == HA_KEY_ALG_BTREE)
 
704
        seg->type= field->key_type();
 
705
      else
726
706
      {
727
707
        if ((seg->type = field->key_type()) != (int) HA_KEYTYPE_TEXT &&
728
708
            seg->type != HA_KEYTYPE_VARTEXT1 &&
738
718
      next_field_pos= seg->start + seg->length;
739
719
      if (field->type() == DRIZZLE_TYPE_VARCHAR)
740
720
      {
741
 
        next_field_pos+= (uint8_t)(((Field_varstring*)field)->pack_length_no_ptr());
 
721
        next_field_pos+= (uint8_t)(((Field_varstring*)field)->length_bytes);
742
722
      }
743
723
 
744
724
      if (next_field_pos > key_part_size) {
745
725
        key_part_size= next_field_pos;
746
726
      }
747
727
 
748
 
      if (field->flags & ENUM_FLAG)
 
728
      if (field->flags & (ENUM_FLAG | SET_FLAG))
749
729
        seg->charset= &my_charset_bin;
750
730
      else
751
731
        seg->charset= field->charset();
752
732
      if (field->null_ptr)
753
733
      {
754
734
        seg->null_bit= field->null_bit;
755
 
        seg->null_pos= (uint) (field->null_ptr - (unsigned char*) table_arg->getInsertRecord());
 
735
        seg->null_pos= (uint) (field->null_ptr - (unsigned char*) table_arg->record[0]);
756
736
      }
757
737
      else
758
738
      {
761
741
      }
762
742
      if (field->flags & AUTO_INCREMENT_FLAG &&
763
743
          table_arg->found_next_number_field &&
764
 
          key == table_arg->getShare()->next_number_index)
 
744
          key == share->next_number_index)
765
745
      {
766
746
        /*
767
747
          Store key number and type for found auto_increment key
770
750
        auto_key= key+ 1;
771
751
        auto_key_type= field->key_type();
772
752
      }
773
 
      if ((uint)field->position() + 1 > max_key_fieldnr)
 
753
      if ((uint)field->field_index + 1 > max_key_fieldnr)
774
754
      {
775
755
        /* Do not use seg->fieldnr as it's not reliable in case of temp tables */
776
 
        max_key_fieldnr= field->position() + 1;
 
756
        max_key_fieldnr= field->field_index + 1;
777
757
      }
778
758
    }
779
759
  }
780
 
 
781
 
  if (key_part_size < table_arg->getShare()->null_bytes + ((table_arg->getShare()->last_null_bit_pos+7) >> 3))
 
760
  
 
761
  if (key_part_size < share->null_bytes + ((share->last_null_bit_pos+7) >> 3))
782
762
  {
783
763
    /* 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);
 
764
    key_part_size = share->null_bytes + ((share->last_null_bit_pos+7) >> 3);
785
765
  }
786
766
 
787
 
 
788
 
 
 
767
  
 
768
  
789
769
  if (table_arg->found_next_number_field)
790
770
  {
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;
 
771
    keydef[share->next_number_index].flag|= HA_AUTO_KEY;
 
772
    found_real_auto_increment= share->next_number_key_offset == 0;
793
773
  }
794
774
  HP_CREATE_INFO hp_create_info;
795
775
  hp_create_info.auto_key= auto_key;
796
776
  hp_create_info.auto_key_type= auto_key_type;
797
 
  hp_create_info.auto_increment= (create_proto.options().has_auto_increment_value() ?
798
 
                                  create_proto.options().auto_increment_value() - 1 : 0);
799
 
  hp_create_info.max_table_size=session->variables.max_heap_table_size;
 
777
  hp_create_info.auto_increment= (create_info->auto_increment_value ?
 
778
                                  create_info->auto_increment_value - 1 : 0);
 
779
  hp_create_info.max_table_size=current_session->variables.max_heap_table_size;
800
780
  hp_create_info.with_auto_increment= found_real_auto_increment;
801
781
  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);
812
 
 
 
782
  hp_create_info.max_chunk_size= share->block_size;
 
783
  hp_create_info.is_dynamic= (share->row_type == ROW_TYPE_DYNAMIC);
 
784
  error= heap_create(fn_format(buff,name,"","",
 
785
                               MY_REPLACE_EXT|MY_UNPACK_FILENAME),
 
786
                   keys, keydef,
 
787
         column_count, columndef,
 
788
         max_key_fieldnr, key_part_size,
 
789
         share->reclength, mem_per_row_keys,
 
790
         (uint32_t) share->max_rows, (uint32_t) share->min_rows,
 
791
         &hp_create_info, &internal_share);
 
792
  
 
793
  free((unsigned char*) keydef);
 
794
  free((void *) columndef);
 
795
  assert(file == 0);
813
796
  return (error);
814
797
}
815
798
 
816
799
 
817
 
void ha_heap::get_auto_increment(uint64_t, uint64_t, uint64_t,
 
800
void ha_heap::update_create_info(HA_CREATE_INFO *create_info)
 
801
{
 
802
  table->file->info(HA_STATUS_AUTO);
 
803
  if (!(create_info->used_fields & HA_CREATE_USED_AUTO))
 
804
    create_info->auto_increment_value= stats.auto_increment_value;
 
805
  if (!(create_info->used_fields & HA_CREATE_USED_BLOCK_SIZE))
 
806
  {
 
807
    if (file->s->recordspace.is_variable_size)
 
808
      create_info->block_size= file->s->recordspace.chunk_length;
 
809
    else
 
810
      create_info->block_size= 0;
 
811
  }
 
812
}
 
813
 
 
814
void ha_heap::get_auto_increment(uint64_t offset __attribute__((unused)),
 
815
                                 uint64_t increment __attribute__((unused)),
 
816
                                 uint64_t nb_desired_values __attribute__((unused)),
818
817
                                 uint64_t *first_value,
819
818
                                 uint64_t *nb_reserved_values)
820
819
{
825
824
}
826
825
 
827
826
 
828
 
int ha_heap::cmp_ref(const unsigned char *ref1, const unsigned char *ref2)
 
827
bool ha_heap::check_if_incompatible_data(HA_CREATE_INFO *info,
 
828
                                         uint32_t table_changes)
829
829
{
830
 
  return memcmp(ref1, ref2, sizeof(HEAP_PTR));
 
830
  /* Check that auto_increment value was not changed */
 
831
  if ((info->used_fields & HA_CREATE_USED_AUTO &&
 
832
       info->auto_increment_value != 0) ||
 
833
      table_changes == IS_EQUAL_NO ||
 
834
      table_changes & IS_EQUAL_PACK_LENGTH) // Not implemented yet
 
835
    return COMPATIBLE_DATA_NO;
 
836
  return COMPATIBLE_DATA_YES;
831
837
}
832
838
 
833
 
 
834
 
DRIZZLE_DECLARE_PLUGIN
 
839
mysql_declare_plugin(heap)
835
840
{
836
 
  DRIZZLE_VERSION_ID,
 
841
  DRIZZLE_STORAGE_ENGINE_PLUGIN,
837
842
  "MEMORY",
838
843
  "1.0",
839
844
  "MySQL AB",
840
845
  "Hash based, stored in memory, useful for temporary tables",
841
846
  PLUGIN_LICENSE_GPL,
842
847
  heap_init,
 
848
  heap_deinit,
 
849
  NULL,                       /* status variables                */
843
850
  NULL,                       /* system variables                */
844
851
  NULL                        /* config options                  */
845
852
}
846
 
DRIZZLE_DECLARE_PLUGIN_END;
 
853
mysql_declare_plugin_end;