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 */
16
16
#include "heap_priv.h"
17
17
#include <drizzled/error.h>
18
18
#include <drizzled/table.h>
19
19
#include <drizzled/session.h>
20
#include <drizzled/current_session.h>
20
21
#include <drizzled/field/timestamp.h>
21
22
#include <drizzled/field/varstring.h>
22
23
#include "drizzled/plugin/daemon.h"
24
#include <boost/thread/mutex.hpp>
27
26
#include "ha_heap.h"
53
52
HTON_SKIP_STORE_LOCK |
54
53
HTON_TEMPORARY_ONLY)
55
pthread_mutex_init(&THR_LOCK_heap, MY_MUTEX_INIT_FAST);
58
58
virtual ~HeapEngine()
60
60
hp_panic(HA_PANIC_CLOSE);
62
pthread_mutex_destroy(&THR_LOCK_heap);
63
virtual Cursor *create(Table &table)
65
virtual Cursor *create(TableShare &table,
66
memory::Root *mem_root)
65
return new ha_heap(*this, table);
68
return new (mem_root) ha_heap(*this, table);
68
71
const char **bas_ext() const {
85
88
message::Table &create_proto,
86
89
HP_SHARE **internal_share);
88
int doRenameTable(Session&, const TableIdentifier &from, const TableIdentifier &to);
91
int doRenameTable(Session&, TableIdentifier &from, TableIdentifier &to);
90
int doDropTable(Session&, const TableIdentifier &identifier);
93
int doDropTable(Session&, TableIdentifier &identifier);
92
95
int doGetTableDefinition(Session& session,
93
const TableIdentifier &identifier,
96
TableIdentifier &identifier,
94
97
message::Table &table_message);
99
/* Temp only engine, so do not return values. */
100
void doGetTableNames(CachedDirectory &, SchemaIdentifier& , set<string>&) { };
96
102
uint32_t max_supported_keys() const { return MAX_KEY; }
97
103
uint32_t max_supported_key_part_length() const { return MAX_KEY_LENGTH; }
99
uint32_t index_flags(enum ha_key_alg ) const
105
uint32_t index_flags(enum ha_key_alg algorithm) const
101
return ( HA_ONLY_WHOLE_INDEX | HA_KEY_SCAN_NOT_ROR);
107
return ((algorithm == HA_KEY_ALG_BTREE) ?
112
HA_ONLY_WHOLE_INDEX |
113
HA_KEY_SCAN_NOT_ROR);
104
bool doDoesTableExist(Session& session, const TableIdentifier &identifier);
105
void doGetTableIdentifiers(CachedDirectory &directory,
106
const SchemaIdentifier &schema_identifier,
107
TableIdentifier::vector &set_of_identifiers);
116
bool doDoesTableExist(Session& session, TableIdentifier &identifier);
117
void doGetTableIdentifiers(drizzled::CachedDirectory &directory,
118
drizzled::SchemaIdentifier &schema_identifier,
119
drizzled::TableIdentifiers &set_of_identifiers);
110
void HeapEngine::doGetTableIdentifiers(CachedDirectory&,
111
const SchemaIdentifier&,
112
TableIdentifier::vector&)
122
void HeapEngine::doGetTableIdentifiers(drizzled::CachedDirectory&,
123
drizzled::SchemaIdentifier&,
124
drizzled::TableIdentifiers&)
116
bool HeapEngine::doDoesTableExist(Session& session, const TableIdentifier &identifier)
128
bool HeapEngine::doDoesTableExist(Session& session, TableIdentifier &identifier)
118
return session.getMessageCache().doesTableMessageExist(identifier);
130
return session.doesTableMessageExist(identifier);
121
133
int HeapEngine::doGetTableDefinition(Session &session,
122
const TableIdentifier &identifier,
134
TableIdentifier &identifier,
123
135
message::Table &table_proto)
125
if (session.getMessageCache().getTableMessage(identifier, table_proto))
137
if (session.getTableMessage(identifier, table_proto))
131
143
We have to ignore ENOENT entries as the MEMORY table is created on open and
132
144
not when doing a CREATE on the table.
134
int HeapEngine::doDropTable(Session &session, const TableIdentifier &identifier)
146
int HeapEngine::doDropTable(Session &session, TableIdentifier &identifier)
136
session.getMessageCache().removeTableMessage(identifier);
148
session.removeTableMessage(identifier);
138
150
int error= heap_delete_table(identifier.getPath().c_str());
177
189
#define MEMORY_STATS_UPDATE_THRESHOLD 10
179
int ha_heap::doOpen(const drizzled::TableIdentifier &identifier, int mode, uint32_t test_if_locked)
191
int ha_heap::open(const char *name, int mode, uint32_t test_if_locked)
181
if ((test_if_locked & HA_OPEN_INTERNAL_TABLE) || (!(file= heap_open(identifier.getPath().c_str(), mode)) && errno == ENOENT))
193
if ((test_if_locked & HA_OPEN_INTERNAL_TABLE) || (!(file= heap_open(name, mode)) && errno == ENOENT))
195
HA_CREATE_INFO create_info;
183
196
internal_table= test(test_if_locked & HA_OPEN_INTERNAL_TABLE);
197
memset(&create_info, 0, sizeof(create_info));
185
199
HP_SHARE *internal_share= NULL;
186
200
message::Table create_proto;
188
if (not heap_storage_engine->heap_create_table(getTable()->in_use,
189
identifier.getPath().c_str(),
202
if (!heap_storage_engine->heap_create_table(ha_session(), name, table,
195
207
file= internal_table ?
196
208
heap_open_from_share(internal_share, mode) :
238
250
with '\'-delimited path.
241
Cursor *ha_heap::clone(memory::Root *)
253
Cursor *ha_heap::clone(memory::Root *mem_root)
243
Cursor *new_handler= getTable()->getMutableShare()->db_type()->getCursor(*getTable());
244
TableIdentifier identifier(getTable()->getShare()->getSchemaName(),
245
getTable()->getShare()->getTableName(),
246
getTable()->getShare()->getPath());
255
Cursor *new_handler= table->getMutableShare()->db_type()->getCursor(*(table->getMutableShare()), mem_root);
248
if (new_handler && !new_handler->ha_open(identifier, getTable()->db_stat,
257
if (new_handler && !new_handler->ha_open(table, file->s->name, table->db_stat,
249
258
HA_OPEN_IGNORE_IF_LOCKED))
250
259
return new_handler;
255
const char *ha_heap::index_type(uint32_t )
264
const char *ha_heap::index_type(uint32_t inx)
266
return ((table_share->getKeyInfo(inx).algorithm == HA_KEY_ALG_BTREE) ?
277
287
void ha_heap::set_keys_for_scanning(void)
290
for (uint32_t i= 0 ; i < table->getShare()->sizeKeys() ; i++)
292
if (table->key_info[i].algorithm == HA_KEY_ALG_BTREE)
282
298
void ha_heap::update_key_stats()
284
for (uint32_t i= 0; i < getTable()->getShare()->sizeKeys(); i++)
300
for (uint32_t i= 0; i < table->getShare()->sizeKeys(); i++)
286
KeyInfo *key= &getTable()->key_info[i];
302
KeyInfo *key= &table->key_info[i];
288
303
if (!key->rec_per_key)
305
if (key->algorithm != HA_KEY_ALG_BTREE)
292
307
if (key->flags & HA_NOSAME)
293
308
key->rec_per_key[key->key_parts-1]= 1;
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;
311
ha_rows hash_buckets= file->s->keydef[i].hash_buckets;
312
uint32_t no_records= hash_buckets ? (uint) (file->s->records/hash_buckets) : 2;
298
313
if (no_records < 2)
300
315
key->rec_per_key[key->key_parts-1]= no_records;
304
319
records_changed= 0;
305
320
/* At the end of update_key_stats() we can proudly claim they are OK. */
306
key_stat_version= file->getShare()->key_stat_version;
321
key_stat_version= file->s->key_stat_version;
310
325
int ha_heap::doInsertRecord(unsigned char * buf)
313
if (getTable()->next_number_field && buf == getTable()->getInsertRecord())
328
ha_statistic_increment(&system_status_var::ha_write_count);
329
if (table->next_number_field && buf == table->record[0])
315
331
if ((res= update_auto_increment()))
318
334
res= heap_write(file,buf);
319
335
if (!res && (++records_changed*MEMORY_STATS_UPDATE_THRESHOLD >
320
file->getShare()->records))
323
339
We can perform this safely since only one writer at the time is
324
340
allowed on the table.
326
file->getShare()->key_stat_version++;
342
file->s->key_stat_version++;
331
347
int ha_heap::doUpdateRecord(const unsigned char * old_data, unsigned char * new_data)
350
ha_statistic_increment(&system_status_var::ha_update_count);
351
if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_UPDATE)
352
table->timestamp_field->set_time();
335
353
res= heap_update(file,old_data,new_data);
336
354
if (!res && ++records_changed*MEMORY_STATS_UPDATE_THRESHOLD >
337
file->getShare()->records)
340
358
We can perform this safely since only one writer at the time is
341
359
allowed on the table.
343
file->getShare()->key_stat_version++;
361
file->s->key_stat_version++;
348
366
int ha_heap::doDeleteRecord(const unsigned char * buf)
369
ha_statistic_increment(&system_status_var::ha_delete_count);
352
370
res= heap_delete(file,buf);
353
if (!res && getTable()->getShare()->getType() == message::Table::STANDARD &&
354
++records_changed*MEMORY_STATS_UPDATE_THRESHOLD > file->getShare()->records)
371
if (!res && table->getShare()->tmp_table == message::Table::STANDARD &&
372
++records_changed*MEMORY_STATS_UPDATE_THRESHOLD > file->s->records)
357
375
We can perform this safely since only one writer at the time is
358
376
allowed on the table.
360
file->getShare()->key_stat_version++;
378
file->s->key_stat_version++;
479
497
have to update the key statistics. Hoping that a table lock is now
482
if (key_stat_version != file->getShare()->key_stat_version)
500
if (key_stat_version != file->s->key_stat_version)
483
501
update_key_stats();
506
enum row_type ha_heap::get_row_type() const
508
if (file->s->recordspace.is_variable_size)
509
return ROW_TYPE_DYNAMIC;
511
return ROW_TYPE_FIXED;
487
514
int ha_heap::extra(enum ha_extra_function operation)
489
516
return heap_extra(file,operation);
620
647
void ha_heap::drop_table(const char *)
622
file->getShare()->delete_on_close= 1;
649
file->s->delete_on_close= 1;
627
int HeapEngine::doRenameTable(Session &session, const TableIdentifier &from, const TableIdentifier &to)
654
int HeapEngine::doRenameTable(Session &session, TableIdentifier &from, TableIdentifier &to)
629
session.getMessageCache().renameTableMessage(from, to);
656
session.renameTableMessage(from, to);
630
657
return heap_rename(from.getPath().c_str(), to.getPath().c_str());
634
661
ha_rows ha_heap::records_in_range(uint32_t inx, key_range *min_key,
635
662
key_range *max_key)
637
KeyInfo *key= &getTable()->key_info[inx];
664
KeyInfo *key= &table->key_info[inx];
665
if (key->algorithm == HA_KEY_ALG_BTREE)
666
return hp_rb_records_in_range(file, inx, min_key, max_key);
639
668
if (!min_key || !max_key ||
640
669
min_key->length != max_key->length ||
647
676
return stats.records;
649
678
/* Assert that info() did run. We need current statistics here. */
650
assert(key_stat_version == file->getShare()->key_stat_version);
679
assert(key_stat_version == file->s->key_stat_version);
651
680
return key->rec_per_key[key->key_parts-1];
654
683
int HeapEngine::doCreateTable(Session &session,
655
684
Table &table_arg,
656
const TableIdentifier &identifier,
685
drizzled::TableIdentifier &identifier,
657
686
message::Table& create_proto)
680
709
message::Table &create_proto,
681
710
HP_SHARE **internal_share)
683
uint32_t key, parts, mem_per_row_keys= 0;
684
uint32_t keys= table_arg->getShare()->sizeKeys();
712
uint32_t key, parts, mem_per_row_keys= 0, keys= table_arg->getShare()->sizeKeys();
685
713
uint32_t auto_key= 0, auto_key_type= 0;
686
714
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;
715
uint32_t column_idx, column_count= table_arg->getShare()->sizeFields();
716
HP_COLUMNDEF *columndef;
719
char buff[FN_REFLEN];
690
721
bool found_real_auto_increment= 0;
699
730
if (num_rows > UINT32_MAX)
733
if (!(columndef= (HP_COLUMNDEF*) malloc(column_count * sizeof(HP_COLUMNDEF))))
736
for (column_idx= 0; column_idx < column_count; column_idx++)
738
Field* field= *(table_arg->field + column_idx);
739
HP_COLUMNDEF* column= columndef + column_idx;
740
column->type= (uint16_t)field->type();
741
column->length= field->pack_length();
742
column->offset= field->offset(field->table->record[0]);
746
column->null_bit= field->null_bit;
747
column->null_pos= (uint) (field->null_ptr - (unsigned char*) table_arg->record[0]);
755
if (field->type() == DRIZZLE_TYPE_VARCHAR)
757
column->length_bytes= (uint8_t)(((Field_varstring*)field)->length_bytes);
761
column->length_bytes= 0;
702
765
for (key= parts= 0; key < keys; key++)
703
766
parts+= table_arg->key_info[key].key_parts;
706
std::vector<HA_KEYSEG> seg_buffer;
707
seg_buffer.resize(parts);
708
HA_KEYSEG *seg= &seg_buffer[0];
768
if (!(keydef= (HP_KEYDEF*) malloc(keys * sizeof(HP_KEYDEF) +
769
parts * sizeof(HA_KEYSEG))))
771
free((void *) columndef);
775
seg= reinterpret_cast<HA_KEYSEG*> (keydef + keys);
710
776
for (key= 0; key < keys; key++)
712
778
KeyInfo *pos= &table_arg->key_info[key];
717
783
keydef[key].flag= (pos->flags & (HA_NOSAME | HA_NULL_ARE_EQUAL));
718
784
keydef[key].seg= seg;
720
mem_per_row_keys+= sizeof(char*) * 2; // = sizeof(HASH_INFO)
786
switch (pos->algorithm) {
787
case HA_KEY_ALG_UNDEF:
788
case HA_KEY_ALG_HASH:
789
keydef[key].algorithm= HA_KEY_ALG_HASH;
790
mem_per_row_keys+= sizeof(char*) * 2; // = sizeof(HASH_INFO)
792
case HA_KEY_ALG_BTREE:
793
keydef[key].algorithm= HA_KEY_ALG_BTREE;
794
mem_per_row_keys+=sizeof(TREE_ELEMENT)+pos->key_length+sizeof(char*);
797
assert(0); // cannot happen
722
800
for (; key_part != key_part_end; key_part++, seg++)
724
802
Field *field= key_part->field;
804
if (pos->algorithm == HA_KEY_ALG_BTREE)
805
seg->type= field->key_type();
727
808
if ((seg->type = field->key_type()) != (int) HA_KEYTYPE_TEXT &&
728
809
seg->type != HA_KEYTYPE_VARTEXT1 &&
800
881
hp_create_info.with_auto_increment= found_real_auto_increment;
801
882
hp_create_info.internal_table= internal_table;
802
883
hp_create_info.max_chunk_size= table_arg->getShare()->block_size;
804
error= heap_create(table_name,
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. */
811
&hp_create_info, internal_share);
884
hp_create_info.is_dynamic= (table_arg->getShare()->row_type == ROW_TYPE_DYNAMIC);
886
error= heap_create(internal::fn_format(buff,table_name,"","",
887
MY_REPLACE_EXT|MY_UNPACK_FILENAME),
889
column_count, columndef,
890
max_key_fieldnr, key_part_size,
891
table_arg->getShare()->getRecordLength(), mem_per_row_keys,
892
static_cast<uint32_t>(num_rows), /* We check for overflow above, so cast is fine here. */
894
&hp_create_info, internal_share);
896
free((unsigned char*) keydef);
897
free((void *) columndef);