17#include <drizzled/error.h>
18#include <drizzled/table.h>
19#include <drizzled/session.h>
20#include <drizzled/field/varstring.h>
21#include <drizzled/plugin/daemon.h>
22#include <drizzled/plugin/storage_engine.h>
23#include <drizzled/util/test.h>
24#include <drizzled/session/table_messages.h>
25#include <drizzled/statistics_variables.h>
26#include <drizzled/system_variables.h>
27#include <drizzled/key_part_info.h>
29#include <boost/thread/mutex.hpp>
39static const string engine_name(
"MEMORY");
41boost::mutex THR_LOCK_heap;
43static const char *ha_heap_exts[] = {
47class HeapEngine :
public plugin::StorageEngine
50 explicit HeapEngine(
string name_arg) :
51 plugin::StorageEngine(name_arg,
52 HTON_STATS_RECORDS_IS_EXACT |
57 HTON_SKIP_STORE_LOCK |
64 hp_panic(HA_PANIC_CLOSE);
69 return new ha_heap(*
this, table);
72 const char **bas_ext()
const
77 drizzled::message::Table::Index::IndexType default_index_type()
const
79 return drizzled::message::Table::Index::HASH;
82 int doCreateTable(
Session &session,
85 const message::Table &create_proto);
92 int heap_create_table(
Session *session,
const char *table_name,
95 const message::Table &create_proto,
96 HP_SHARE **internal_share);
102 int doGetTableDefinition(
Session& session,
104 message::Table &table_message);
106 uint32_t max_supported_keys()
const {
return MAX_KEY; }
107 uint32_t max_supported_key_part_length()
const {
return MAX_KEY_LENGTH; }
109 uint32_t index_flags(drizzled::message::Table::Index::IndexType)
const
111 return ( HA_ONLY_WHOLE_INDEX | HA_KEY_SCAN_NOT_ROR);
117 identifier::table::vector &set_of_identifiers);
122 identifier::table::vector&)
128 return session.getMessageCache().doesTableMessageExist(identifier);
131int HeapEngine::doGetTableDefinition(
Session &session,
133 message::Table &table_proto)
135 if (session.getMessageCache().getTableMessage(identifier, table_proto))
146 session.getMessageCache().removeTableMessage(identifier);
148 int error= heap_delete_table(identifier.getPath().c_str());
160 heap_storage_engine=
new HeapEngine(engine_name);
161 context.add(heap_storage_engine);
170ha_heap::ha_heap(plugin::StorageEngine &engine_arg,
172 :
Cursor(engine_arg, table_arg), file(0), records_changed(0), key_stat_version(0),
187#define MEMORY_STATS_UPDATE_THRESHOLD 10
191 if ((test_if_locked & HA_OPEN_INTERNAL_TABLE) || (!(file= heap_open(identifier.getPath().c_str(), mode)) && errno == ENOENT))
193 internal_table= test(test_if_locked & HA_OPEN_INTERNAL_TABLE);
195 HP_SHARE *internal_share= NULL;
196 message::Table create_proto;
198 if (not heap_storage_engine->heap_create_table(getTable()->in_use,
199 identifier.getPath().c_str(),
205 file= internal_table ?
206 heap_open_from_share(internal_share, mode) :
207 heap_open_from_share_and_register(internal_share, mode);
211 THR_LOCK_heap.lock();
212 hp_free(internal_share);
213 THR_LOCK_heap.unlock();
221 set_keys_for_scanning();
230 key_stat_version= file->getShare()->key_stat_version - 1;
235int ha_heap::close(
void)
237 return internal_table ? hp_close(file) : heap_close(file);
253 Cursor *new_handler= getTable()->getMutableShare()->db_type()->getCursor(*getTable());
254 identifier::Table identifier(*getTable());
256 if (new_handler && !new_handler->
ha_open(identifier, getTable()->db_stat,
257 HA_OPEN_IGNORE_IF_LOCKED))
263const char *ha_heap::index_type(uint32_t )
285void ha_heap::set_keys_for_scanning(
void)
290void ha_heap::update_key_stats()
292 for (uint32_t i= 0; i < getTable()->getShare()->sizeKeys(); i++)
294 KeyInfo *key= &getTable()->key_info[i];
296 if (!key->rec_per_key)
300 if (key->flags & HA_NOSAME)
301 key->rec_per_key[key->key_parts-1]= 1;
304 ha_rows hash_buckets= file->getShare()->keydef[i].hash_buckets;
305 uint32_t no_records= hash_buckets ? (uint) (file->getShare()->records/hash_buckets) : 2;
308 key->rec_per_key[key->key_parts-1]= no_records;
314 key_stat_version= file->getShare()->key_stat_version;
318int ha_heap::doInsertRecord(
unsigned char * buf)
321 if (getTable()->next_number_field && buf == getTable()->getInsertRecord())
323 if ((res= update_auto_increment()))
326 res= heap_write(file,buf);
327 if (!res && (++records_changed*MEMORY_STATS_UPDATE_THRESHOLD >
328 file->getShare()->records))
334 file->getShare()->key_stat_version++;
339int ha_heap::doUpdateRecord(
const unsigned char * old_data,
unsigned char * new_data)
343 res= heap_update(file,old_data,new_data);
344 if (!res && ++records_changed*MEMORY_STATS_UPDATE_THRESHOLD >
345 file->getShare()->records)
351 file->getShare()->key_stat_version++;
356int ha_heap::doDeleteRecord(
const unsigned char * buf)
360 res= heap_delete(file,buf);
361 if (!res && getTable()->getShare()->getType() == message::Table::STANDARD &&
362 ++records_changed*MEMORY_STATS_UPDATE_THRESHOLD > file->getShare()->records)
368 file->getShare()->key_stat_version++;
374 key_part_map keypart_map,
375 enum ha_rkey_function find_flag)
377 assert(inited==INDEX);
378 ha_statistic_increment(&system_status_var::ha_read_key_count);
379 int error = heap_rkey(file,buf,active_index, key, keypart_map, find_flag);
380 getTable()->status = error ? STATUS_NOT_FOUND : 0;
385 key_part_map keypart_map)
387 assert(inited==INDEX);
388 ha_statistic_increment(&system_status_var::ha_read_key_count);
389 int error= heap_rkey(file, buf, active_index, key, keypart_map,
390 HA_READ_PREFIX_LAST);
391 getTable()->status= error ? STATUS_NOT_FOUND : 0;
396 key_part_map keypart_map,
397 enum ha_rkey_function find_flag)
399 ha_statistic_increment(&system_status_var::ha_read_key_count);
400 int error = heap_rkey(file, buf, index, key, keypart_map, find_flag);
401 getTable()->status = error ? STATUS_NOT_FOUND : 0;
405int ha_heap::index_next(
unsigned char * buf)
407 assert(inited==INDEX);
408 ha_statistic_increment(&system_status_var::ha_read_next_count);
409 int error=heap_rnext(file,buf);
410 getTable()->status=error ? STATUS_NOT_FOUND: 0;
414int ha_heap::index_prev(
unsigned char * buf)
416 assert(inited==INDEX);
417 ha_statistic_increment(&system_status_var::ha_read_prev_count);
418 int error=heap_rprev(file,buf);
419 getTable()->status=error ? STATUS_NOT_FOUND: 0;
423int ha_heap::index_first(
unsigned char * buf)
425 assert(inited==INDEX);
426 ha_statistic_increment(&system_status_var::ha_read_first_count);
427 int error=heap_rfirst(file, buf, active_index);
428 getTable()->status=error ? STATUS_NOT_FOUND: 0;
432int ha_heap::index_last(
unsigned char * buf)
434 assert(inited==INDEX);
435 ha_statistic_increment(&system_status_var::ha_read_last_count);
436 int error=heap_rlast(file, buf, active_index);
437 getTable()->status=error ? STATUS_NOT_FOUND: 0;
443 return scan ? heap_scan_init(file) : 0;
446int ha_heap::rnd_next(
unsigned char *buf)
448 ha_statistic_increment(&system_status_var::ha_read_rnd_next_count);
449 int error=heap_scan(file, buf);
450 getTable()->status=error ? STATUS_NOT_FOUND: 0;
454int ha_heap::rnd_pos(
unsigned char * buf,
unsigned char *pos)
457 HEAP_PTR heap_position;
458 ha_statistic_increment(&system_status_var::ha_read_rnd_count);
459 memcpy(&heap_position, pos,
sizeof(HEAP_PTR));
460 error=heap_rrnd(file, buf, heap_position);
461 getTable()->status=error ? STATUS_NOT_FOUND: 0;
465void ha_heap::position(
const unsigned char *)
467 *(HEAP_PTR*) ref= heap_position(file);
470int ha_heap::info(uint32_t flag)
473 (void) heap_info(file,&hp_info,flag);
475 errkey= hp_info.errkey;
476 stats.records= hp_info.records;
477 stats.deleted= hp_info.deleted;
478 stats.mean_rec_length= hp_info.reclength;
479 stats.data_file_length= hp_info.data_length;
480 stats.index_file_length= hp_info.index_length;
481 stats.max_data_file_length= hp_info.max_records * hp_info.reclength;
482 stats.delete_length= hp_info.deleted * hp_info.reclength;
483 if (flag & HA_STATUS_AUTO)
484 stats.auto_increment_value= hp_info.auto_increment;
490 if (key_stat_version != file->getShare()->key_stat_version)
495int ha_heap::extra(
enum ha_extra_function operation)
497 return heap_extra(file,operation);
503 return heap_reset(file);
510 if (getTable()->getShare()->getType() == message::Table::STANDARD)
516 file->getShare()->key_stat_version++;
545int ha_heap::disable_indexes(uint32_t mode)
549 if (mode == HA_KEY_SWITCH_ALL)
551 if (!(error= heap_disable_indexes(file)))
552 set_keys_for_scanning();
557 error= HA_ERR_WRONG_COMMAND;
592int ha_heap::enable_indexes(uint32_t mode)
596 if (mode == HA_KEY_SWITCH_ALL)
598 if (!(error= heap_enable_indexes(file)))
599 set_keys_for_scanning();
604 error= HA_ERR_WRONG_COMMAND;
623int ha_heap::indexes_are_disabled()
625 return heap_indexes_are_disabled(file);
628void ha_heap::drop_table()
630 file->getShare()->delete_on_close= 1;
637 session.getMessageCache().renameTableMessage(from, to);
638 return heap_rename(from.getPath().c_str(), to.getPath().c_str());
642ha_rows ha_heap::records_in_range(uint32_t inx,
key_range *min_key,
645 KeyInfo *key= &getTable()->key_info[inx];
647 if (!min_key || !max_key ||
648 min_key->length != max_key->length ||
649 min_key->length != key->key_length ||
650 min_key->flag != HA_READ_KEY_EXACT ||
651 max_key->flag != HA_READ_AFTER_KEY)
654 if (stats.records <= 1)
655 return stats.records;
658 assert(key_stat_version == file->getShare()->key_stat_version);
659 return key->rec_per_key[key->key_parts-1];
662int HeapEngine::doCreateTable(
Session &session,
665 const message::Table& create_proto)
668 HP_SHARE *internal_share;
669 const char *table_name= identifier.getPath().c_str();
671 error= heap_create_table(&session, table_name, &table_arg,
678 session.getMessageCache().storeTableMessage(identifier, create_proto);
685int HeapEngine::heap_create_table(
Session *session,
const char *table_name,
688 const message::Table &create_proto,
689 HP_SHARE **internal_share)
691 uint32_t key, parts, mem_per_row_keys= 0;
692 uint32_t keys= table_arg->getShare()->sizeKeys();
693 uint32_t auto_key= 0, auto_key_type= 0;
694 uint32_t max_key_fieldnr = 0, key_part_size = 0, next_field_pos = 0;
695 uint32_t column_count= table_arg->getShare()->sizeFields();
696 std::vector<HP_KEYDEF> keydef;
698 bool found_real_auto_increment= 0;
706 uint64_t num_rows= table_arg->getShare()->getMaxRows();
707 if (num_rows > UINT32_MAX)
710 for (key= parts= 0; key < keys; key++)
711 parts+= table_arg->
key_info[key].key_parts;
714 std::vector<HA_KEYSEG> seg_buffer;
715 seg_buffer.resize(parts);
716 HA_KEYSEG *seg= &seg_buffer[0];
718 for (key= 0; key < keys; key++)
720 KeyInfo *pos= &table_arg->
key_info[key];
721 KeyPartInfo *key_part= pos->key_part;
722 KeyPartInfo *key_part_end= key_part + pos->key_parts;
724 keydef[key].keysegs= (uint) pos->key_parts;
725 keydef[key].flag= (pos->flags & (HA_NOSAME | HA_NULL_ARE_EQUAL));
726 keydef[key].seg= seg;
728 mem_per_row_keys+=
sizeof(
char*) * 2;
730 for (; key_part != key_part_end; key_part++, seg++)
732 Field *field= key_part->field;
735 if ((seg->type = field->key_type()) != (
int) HA_KEYTYPE_TEXT &&
736 seg->type != HA_KEYTYPE_VARTEXT1 &&
737 seg->type != HA_KEYTYPE_VARTEXT2 &&
738 seg->type != HA_KEYTYPE_VARBINARY1 &&
739 seg->type != HA_KEYTYPE_VARBINARY2)
740 seg->type= HA_KEYTYPE_BINARY;
742 seg->start= (uint) key_part->offset;
743 seg->length= (uint) key_part->length;
744 seg->flag= key_part->key_part_flag;
746 next_field_pos= seg->start + seg->length;
747 if (field->type() == DRIZZLE_TYPE_VARCHAR)
749 next_field_pos+= (uint8_t)(((Field_varstring*)field)->pack_length_no_ptr());
752 if (next_field_pos > key_part_size) {
753 key_part_size= next_field_pos;
756 if (field->flags & ENUM_FLAG)
757 seg->charset= &my_charset_bin;
759 seg->charset= field->charset();
763 seg->null_pos= (uint) (field->
null_ptr - (
unsigned char*) table_arg->getInsertRecord());
770 if (field->flags & AUTO_INCREMENT_FLAG &&
772 key == table_arg->getShare()->next_number_index)
779 auto_key_type= field->key_type();
781 if ((uint)field->position() + 1 > max_key_fieldnr)
784 max_key_fieldnr= field->position() + 1;
789 if (key_part_size < table_arg->getShare()->null_bytes + ((table_arg->getShare()->last_null_bit_pos+7) >> 3))
792 key_part_size = table_arg->getShare()->null_bytes + ((table_arg->getShare()->last_null_bit_pos+7) >> 3);
799 keydef[table_arg->getShare()->next_number_index].flag|= HA_AUTO_KEY;
800 found_real_auto_increment= table_arg->getShare()->next_number_key_offset == 0;
802 HP_CREATE_INFO hp_create_info;
803 hp_create_info.auto_key= auto_key;
804 hp_create_info.auto_key_type= auto_key_type;
805 hp_create_info.auto_increment= (create_proto.options().has_auto_increment_value() ?
806 create_proto.options().auto_increment_value() - 1 : 0);
807 hp_create_info.max_table_size=session->
variables.max_heap_table_size;
808 hp_create_info.with_auto_increment= found_real_auto_increment;
809 hp_create_info.internal_table= internal_table;
810 hp_create_info.max_chunk_size= table_arg->getShare()->block_size;
812 error= heap_create(table_name,
816 table_arg->getShare()->getRecordLength(), mem_per_row_keys,
817 static_cast<uint32_t
>(num_rows),
819 &hp_create_info, internal_share);
825void ha_heap::get_auto_increment(uint64_t, uint64_t, uint64_t,
826 uint64_t *first_value,
827 uint64_t *nb_reserved_values)
829 ha_heap::info(HA_STATUS_AUTO);
830 *first_value= stats.auto_increment_value;
832 *nb_reserved_values= UINT64_MAX;
836int ha_heap::cmp_ref(
const unsigned char *ref1,
const unsigned char *ref2)
838 return memcmp(ref1, ref2,
sizeof(HEAP_PTR));
842DRIZZLE_DECLARE_PLUGIN
848 N_(
"MEMORY storage engine"),
854DRIZZLE_DECLARE_PLUGIN_END;
int ha_open(const identifier::Table &identifier, int mode, int test_if_locked)
drizzle_system_variables & variables
Field * found_next_number_field
int index_read_last_map(unsigned char *buf, const unsigned char *key, drizzled::key_part_map keypart_map)
The following functions works like index_read, but it find the last row with the current key value or...
int index_read_map(unsigned char *buf, const unsigned char *key, drizzled::key_part_map keypart_map, enum drizzled::ha_rkey_function find_flag)
Positions an index cursor to the index specified in the handle. Fetches the row if available....
int index_read_idx_map(unsigned char *buf, uint32_t index, const unsigned char *key, drizzled::key_part_map keypart_map, enum drizzled::ha_rkey_function find_flag)
Positions an index cursor to the index specified in the handle. Fetches the row if available....
int delete_all_rows(void)
int doStartTableScan(bool scan)
TODO: Rename this file - func.h is stupid.