drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
ha_heap.cc
1/* Copyright (C) 2000-2006 MySQL AB
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
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 */
15
16#include "heap_priv.h"
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>
28
29#include <boost/thread/mutex.hpp>
30
31#include "heap.h"
32#include "ha_heap.h"
33
34#include <string>
35
36using namespace drizzled;
37using namespace std;
38
39static const string engine_name("MEMORY");
40
41boost::mutex THR_LOCK_heap;
42
43static const char *ha_heap_exts[] = {
44 NULL
45};
46
47class HeapEngine : public plugin::StorageEngine
48{
49public:
50 explicit HeapEngine(string name_arg) :
51 plugin::StorageEngine(name_arg,
52 HTON_STATS_RECORDS_IS_EXACT |
53 HTON_NULL_IN_KEY |
54 HTON_FAST_KEY_READ |
55 HTON_NO_BLOBS |
56 HTON_HAS_RECORDS |
57 HTON_SKIP_STORE_LOCK |
58 HTON_TEMPORARY_ONLY)
59 {
60 }
61
62 virtual ~HeapEngine()
63 {
64 hp_panic(HA_PANIC_CLOSE);
65 }
66
67 virtual Cursor *create(Table &table)
68 {
69 return new ha_heap(*this, table);
70 }
71
72 const char **bas_ext() const
73 {
74 return ha_heap_exts;
75 }
76
77 drizzled::message::Table::Index::IndexType default_index_type() const
78 {
79 return drizzled::message::Table::Index::HASH;
80 }
81
82 int doCreateTable(Session &session,
83 Table &table_arg,
84 const identifier::Table &identifier,
85 const message::Table &create_proto);
86
87 /* For whatever reason, internal tables can be created by Cursor::open()
88 for MEMORY.
89 Instead of diving down a rat hole, let's just cry ourselves to sleep
90 at night with this odd hackish workaround.
91 */
92 int heap_create_table(Session *session, const char *table_name,
93 Table *table_arg,
94 bool internal_table,
95 const message::Table &create_proto,
96 HP_SHARE **internal_share);
97
98 int doRenameTable(Session&, const identifier::Table &from, const identifier::Table &to);
99
100 int doDropTable(Session&, const identifier::Table &identifier);
101
102 int doGetTableDefinition(Session& session,
103 const identifier::Table &identifier,
104 message::Table &table_message);
105
106 uint32_t max_supported_keys() const { return MAX_KEY; }
107 uint32_t max_supported_key_part_length() const { return MAX_KEY_LENGTH; }
108
109 uint32_t index_flags(drizzled::message::Table::Index::IndexType) const
110 {
111 return ( HA_ONLY_WHOLE_INDEX | HA_KEY_SCAN_NOT_ROR);
112 }
113
114 bool doDoesTableExist(Session& session, const identifier::Table &identifier);
115 void doGetTableIdentifiers(CachedDirectory &directory,
116 const identifier::Schema &schema_identifier,
117 identifier::table::vector &set_of_identifiers);
118};
119
120void HeapEngine::doGetTableIdentifiers(CachedDirectory&,
121 const identifier::Schema&,
122 identifier::table::vector&)
123{
124}
125
126bool HeapEngine::doDoesTableExist(Session& session, const identifier::Table &identifier)
127{
128 return session.getMessageCache().doesTableMessageExist(identifier);
129}
130
131int HeapEngine::doGetTableDefinition(Session &session,
132 const identifier::Table &identifier,
133 message::Table &table_proto)
134{
135 if (session.getMessageCache().getTableMessage(identifier, table_proto))
136 return EEXIST;
137
138 return ENOENT;
139}
140/*
141 We have to ignore ENOENT entries as the MEMORY table is created on open and
142 not when doing a CREATE on the table.
143*/
144int HeapEngine::doDropTable(Session &session, const identifier::Table &identifier)
145{
146 session.getMessageCache().removeTableMessage(identifier);
147
148 int error= heap_delete_table(identifier.getPath().c_str());
149
150 if (error == ENOENT)
151 error= 0;
152
153 return error;
154}
155
156static HeapEngine *heap_storage_engine= NULL;
157
158static int heap_init(module::Context &context)
159{
160 heap_storage_engine= new HeapEngine(engine_name);
161 context.add(heap_storage_engine);
162 return 0;
163}
164
165
166/*****************************************************************************
167** MEMORY tables
168*****************************************************************************/
169
170ha_heap::ha_heap(plugin::StorageEngine &engine_arg,
171 Table &table_arg)
172 :Cursor(engine_arg, table_arg), file(0), records_changed(0), key_stat_version(0),
173 internal_table(0)
174{}
175
176/*
177 Hash index statistics is updated (copied from HP_KEYDEF::hash_buckets to
178 rec_per_key) after 1/MEMORY_STATS_UPDATE_THRESHOLD fraction of table records
179 have been inserted/updated/deleted. delete_all_rows() and table flush cause
180 immediate update.
181
182 NOTE
183 hash index statistics must be updated when number of table records changes
184 from 0 to non-zero value and vice versa. Otherwise records_in_range may
185 erroneously return 0 and 'range' may miss records.
186*/
187#define MEMORY_STATS_UPDATE_THRESHOLD 10
188
189int ha_heap::doOpen(const drizzled::identifier::Table &identifier, int mode, uint32_t test_if_locked)
190{
191 if ((test_if_locked & HA_OPEN_INTERNAL_TABLE) || (!(file= heap_open(identifier.getPath().c_str(), mode)) && errno == ENOENT))
192 {
193 internal_table= test(test_if_locked & HA_OPEN_INTERNAL_TABLE);
194 file= 0;
195 HP_SHARE *internal_share= NULL;
196 message::Table create_proto;
197
198 if (not heap_storage_engine->heap_create_table(getTable()->in_use,
199 identifier.getPath().c_str(),
200 getTable(),
201 internal_table,
202 create_proto,
203 &internal_share))
204 {
205 file= internal_table ?
206 heap_open_from_share(internal_share, mode) :
207 heap_open_from_share_and_register(internal_share, mode);
208 if (!file)
209 {
210 /* Couldn't open table; Remove the newly created table */
211 THR_LOCK_heap.lock();
212 hp_free(internal_share);
213 THR_LOCK_heap.unlock();
214 }
215 }
216 }
217 ref_length= sizeof(HEAP_PTR);
218 if (file)
219 {
220 /* Initialize variables for the opened table */
221 set_keys_for_scanning();
222 /*
223 We cannot run update_key_stats() here because we do not have a
224 lock on the table. The 'records' count might just be changed
225 temporarily at this moment and we might get wrong statistics (Bug
226 #10178). Instead we request for update. This will be done in
227 ha_heap::info(), which is always called before key statistics are
228 used.
229 */
230 key_stat_version= file->getShare()->key_stat_version - 1;
231 }
232 return file ? 0 : 1;
233}
234
235int ha_heap::close(void)
236{
237 return internal_table ? hp_close(file) : heap_close(file);
238}
239
240
241/*
242 Create a copy of this table
243
244 DESCRIPTION
245 Do same as default implementation but use file->s->name instead of
246 table->getShare()->path. This is needed by Windows where the clone() call sees
247 '/'-delimited path in table->getShare()->path, while ha_peap::open() was called
248 with '\'-delimited path.
249*/
250
251Cursor *ha_heap::clone(memory::Root *)
252{
253 Cursor *new_handler= getTable()->getMutableShare()->db_type()->getCursor(*getTable());
254 identifier::Table identifier(*getTable());
255
256 if (new_handler && !new_handler->ha_open(identifier, getTable()->db_stat,
257 HA_OPEN_IGNORE_IF_LOCKED))
258 return new_handler;
259 return NULL;
260}
261
262
263const char *ha_heap::index_type(uint32_t )
264{
265 return ("HASH");
266}
267
268
269/*
270 Compute which keys to use for scanning
271
272 SYNOPSIS
273 set_keys_for_scanning()
274 no parameter
275
276 DESCRIPTION
277 Set the bitmap btree_keys, which is used when the upper layers ask
278 which keys to use for scanning. For each btree index the
279 corresponding bit is set.
280
281 RETURN
282 void
283*/
284
285void ha_heap::set_keys_for_scanning(void)
286{
287}
288
289
290void ha_heap::update_key_stats()
291{
292 for (uint32_t i= 0; i < getTable()->getShare()->sizeKeys(); i++)
293 {
294 KeyInfo *key= &getTable()->key_info[i];
295
296 if (!key->rec_per_key)
297 continue;
298
299 {
300 if (key->flags & HA_NOSAME)
301 key->rec_per_key[key->key_parts-1]= 1;
302 else
303 {
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;
306 if (no_records < 2)
307 no_records= 2;
308 key->rec_per_key[key->key_parts-1]= no_records;
309 }
310 }
311 }
312 records_changed= 0;
313 /* At the end of update_key_stats() we can proudly claim they are OK. */
314 key_stat_version= file->getShare()->key_stat_version;
315}
316
317
318int ha_heap::doInsertRecord(unsigned char * buf)
319{
320 int res;
321 if (getTable()->next_number_field && buf == getTable()->getInsertRecord())
322 {
323 if ((res= update_auto_increment()))
324 return res;
325 }
326 res= heap_write(file,buf);
327 if (!res && (++records_changed*MEMORY_STATS_UPDATE_THRESHOLD >
328 file->getShare()->records))
329 {
330 /*
331 We can perform this safely since only one writer at the time is
332 allowed on the table.
333 */
334 file->getShare()->key_stat_version++;
335 }
336 return res;
337}
338
339int ha_heap::doUpdateRecord(const unsigned char * old_data, unsigned char * new_data)
340{
341 int res;
342
343 res= heap_update(file,old_data,new_data);
344 if (!res && ++records_changed*MEMORY_STATS_UPDATE_THRESHOLD >
345 file->getShare()->records)
346 {
347 /*
348 We can perform this safely since only one writer at the time is
349 allowed on the table.
350 */
351 file->getShare()->key_stat_version++;
352 }
353 return res;
354}
355
356int ha_heap::doDeleteRecord(const unsigned char * buf)
357{
358 int res;
359
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)
363 {
364 /*
365 We can perform this safely since only one writer at the time is
366 allowed on the table.
367 */
368 file->getShare()->key_stat_version++;
369 }
370 return res;
371}
372
373int ha_heap::index_read_map(unsigned char *buf, const unsigned char *key,
374 key_part_map keypart_map,
375 enum ha_rkey_function find_flag)
376{
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;
381 return error;
382}
383
384int ha_heap::index_read_last_map(unsigned char *buf, const unsigned char *key,
385 key_part_map keypart_map)
386{
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;
392 return error;
393}
394
395int ha_heap::index_read_idx_map(unsigned char *buf, uint32_t index, const unsigned char *key,
396 key_part_map keypart_map,
397 enum ha_rkey_function find_flag)
398{
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;
402 return error;
403}
404
405int ha_heap::index_next(unsigned char * buf)
406{
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;
411 return error;
412}
413
414int ha_heap::index_prev(unsigned char * buf)
415{
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;
420 return error;
421}
422
423int ha_heap::index_first(unsigned char * buf)
424{
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;
429 return error;
430}
431
432int ha_heap::index_last(unsigned char * buf)
433{
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;
438 return error;
439}
440
442{
443 return scan ? heap_scan_init(file) : 0;
444}
445
446int ha_heap::rnd_next(unsigned char *buf)
447{
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;
451 return error;
452}
453
454int ha_heap::rnd_pos(unsigned char * buf, unsigned char *pos)
455{
456 int error;
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;
462 return error;
463}
464
465void ha_heap::position(const unsigned char *)
466{
467 *(HEAP_PTR*) ref= heap_position(file); // Ref is aligned
468}
469
470int ha_heap::info(uint32_t flag)
471{
472 HEAPINFO hp_info;
473 (void) heap_info(file,&hp_info,flag);
474
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;
485 /*
486 If info() is called for the first time after open(), we will still
487 have to update the key statistics. Hoping that a table lock is now
488 in place.
489 */
490 if (key_stat_version != file->getShare()->key_stat_version)
491 update_key_stats();
492 return 0;
493}
494
495int ha_heap::extra(enum ha_extra_function operation)
496{
497 return heap_extra(file,operation);
498}
499
500
502{
503 return heap_reset(file);
504}
505
506
508{
509 heap_clear(file);
510 if (getTable()->getShare()->getType() == message::Table::STANDARD)
511 {
512 /*
513 We can perform this safely since only one writer at the time is
514 allowed on the table.
515 */
516 file->getShare()->key_stat_version++;
517 }
518 return 0;
519}
520
521/*
522 Disable indexes.
523
524 SYNOPSIS
525 disable_indexes()
526 mode mode of operation:
527 HA_KEY_SWITCH_NONUNIQ disable all non-unique keys
528 HA_KEY_SWITCH_ALL disable all keys
529 HA_KEY_SWITCH_NONUNIQ_SAVE dis. non-uni. and make persistent
530 HA_KEY_SWITCH_ALL_SAVE dis. all keys and make persistent
531
532 DESCRIPTION
533 Disable indexes and clear keys to use for scanning.
534
535 IMPLEMENTATION
536 HA_KEY_SWITCH_NONUNIQ is not implemented.
537 HA_KEY_SWITCH_NONUNIQ_SAVE is not implemented with HEAP.
538 HA_KEY_SWITCH_ALL_SAVE is not implemented with HEAP.
539
540 RETURN
541 0 ok
542 HA_ERR_WRONG_COMMAND mode not implemented.
543*/
544
545int ha_heap::disable_indexes(uint32_t mode)
546{
547 int error;
548
549 if (mode == HA_KEY_SWITCH_ALL)
550 {
551 if (!(error= heap_disable_indexes(file)))
552 set_keys_for_scanning();
553 }
554 else
555 {
556 /* mode not implemented */
557 error= HA_ERR_WRONG_COMMAND;
558 }
559 return error;
560}
561
562
563/*
564 Enable indexes.
565
566 SYNOPSIS
567 enable_indexes()
568 mode mode of operation:
569 HA_KEY_SWITCH_NONUNIQ enable all non-unique keys
570 HA_KEY_SWITCH_ALL enable all keys
571 HA_KEY_SWITCH_NONUNIQ_SAVE en. non-uni. and make persistent
572 HA_KEY_SWITCH_ALL_SAVE en. all keys and make persistent
573
574 DESCRIPTION
575 Enable indexes and set keys to use for scanning.
576 The indexes might have been disabled by disable_index() before.
577 The function works only if both data and indexes are empty,
578 since the heap storage engine cannot repair the indexes.
579 To be sure, call Cursor::delete_all_rows() before.
580
581 IMPLEMENTATION
582 HA_KEY_SWITCH_NONUNIQ is not implemented.
583 HA_KEY_SWITCH_NONUNIQ_SAVE is not implemented with HEAP.
584 HA_KEY_SWITCH_ALL_SAVE is not implemented with HEAP.
585
586 RETURN
587 0 ok
588 HA_ERR_CRASHED data or index is non-empty. Delete all rows and retry.
589 HA_ERR_WRONG_COMMAND mode not implemented.
590*/
591
592int ha_heap::enable_indexes(uint32_t mode)
593{
594 int error;
595
596 if (mode == HA_KEY_SWITCH_ALL)
597 {
598 if (!(error= heap_enable_indexes(file)))
599 set_keys_for_scanning();
600 }
601 else
602 {
603 /* mode not implemented */
604 error= HA_ERR_WRONG_COMMAND;
605 }
606 return error;
607}
608
609
610/*
611 Test if indexes are disabled.
612
613 SYNOPSIS
614 indexes_are_disabled()
615 no parameters
616
617 RETURN
618 0 indexes are not disabled
619 1 all indexes are disabled
620 [2 non-unique indexes are disabled - NOT YET IMPLEMENTED]
621*/
622
623int ha_heap::indexes_are_disabled()
624{
625 return heap_indexes_are_disabled(file);
626}
627
628void ha_heap::drop_table()
629{
630 file->getShare()->delete_on_close= 1;
631 close();
632}
633
634
635int HeapEngine::doRenameTable(Session &session, const identifier::Table &from, const identifier::Table &to)
636{
637 session.getMessageCache().renameTableMessage(from, to);
638 return heap_rename(from.getPath().c_str(), to.getPath().c_str());
639}
640
641
642ha_rows ha_heap::records_in_range(uint32_t inx, key_range *min_key,
643 key_range *max_key)
644{
645 KeyInfo *key= &getTable()->key_info[inx];
646
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)
652 return HA_POS_ERROR; // Can only use exact keys
653
654 if (stats.records <= 1)
655 return stats.records;
656
657 /* Assert that info() did run. We need current statistics here. */
658 assert(key_stat_version == file->getShare()->key_stat_version);
659 return key->rec_per_key[key->key_parts-1];
660}
661
662int HeapEngine::doCreateTable(Session &session,
663 Table &table_arg,
664 const identifier::Table &identifier,
665 const message::Table& create_proto)
666{
667 int error;
668 HP_SHARE *internal_share;
669 const char *table_name= identifier.getPath().c_str();
670
671 error= heap_create_table(&session, table_name, &table_arg,
672 false,
673 create_proto,
674 &internal_share);
675
676 if (error == 0)
677 {
678 session.getMessageCache().storeTableMessage(identifier, create_proto);
679 }
680
681 return error;
682}
683
684
685int HeapEngine::heap_create_table(Session *session, const char *table_name,
686 Table *table_arg,
687 bool internal_table,
688 const message::Table &create_proto,
689 HP_SHARE **internal_share)
690{
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;
697 int error;
698 bool found_real_auto_increment= 0;
699
700 /*
701 * We cannot create tables with more rows than UINT32_MAX. This is a
702 * limitation of the HEAP engine. Here, since TableShare::getMaxRows()
703 * can return a number more than that, we trap it here instead of casting
704 * to a truncated integer.
705 */
706 uint64_t num_rows= table_arg->getShare()->getMaxRows();
707 if (num_rows > UINT32_MAX)
708 return -1;
709
710 for (key= parts= 0; key < keys; key++)
711 parts+= table_arg->key_info[key].key_parts;
712
713 keydef.resize(keys);
714 std::vector<HA_KEYSEG> seg_buffer;
715 seg_buffer.resize(parts);
716 HA_KEYSEG *seg= &seg_buffer[0];
717
718 for (key= 0; key < keys; key++)
719 {
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;
723
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;
727
728 mem_per_row_keys+= sizeof(char*) * 2; // = sizeof(HASH_INFO)
729
730 for (; key_part != key_part_end; key_part++, seg++)
731 {
732 Field *field= key_part->field;
733
734 {
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;
741 }
742 seg->start= (uint) key_part->offset;
743 seg->length= (uint) key_part->length;
744 seg->flag= key_part->key_part_flag;
745
746 next_field_pos= seg->start + seg->length;
747 if (field->type() == DRIZZLE_TYPE_VARCHAR)
748 {
749 next_field_pos+= (uint8_t)(((Field_varstring*)field)->pack_length_no_ptr());
750 }
751
752 if (next_field_pos > key_part_size) {
753 key_part_size= next_field_pos;
754 }
755
756 if (field->flags & ENUM_FLAG)
757 seg->charset= &my_charset_bin;
758 else
759 seg->charset= field->charset();
760 if (field->null_ptr)
761 {
762 seg->null_bit= field->null_bit;
763 seg->null_pos= (uint) (field->null_ptr - (unsigned char*) table_arg->getInsertRecord());
764 }
765 else
766 {
767 seg->null_bit= 0;
768 seg->null_pos= 0;
769 }
770 if (field->flags & AUTO_INCREMENT_FLAG &&
771 table_arg->found_next_number_field &&
772 key == table_arg->getShare()->next_number_index)
773 {
774 /*
775 Store key number and type for found auto_increment key
776 We have to store type as seg->type can differ from it
777 */
778 auto_key= key+ 1;
779 auto_key_type= field->key_type();
780 }
781 if ((uint)field->position() + 1 > max_key_fieldnr)
782 {
783 /* Do not use seg->fieldnr as it's not reliable in case of temp tables */
784 max_key_fieldnr= field->position() + 1;
785 }
786 }
787 }
788
789 if (key_part_size < table_arg->getShare()->null_bytes + ((table_arg->getShare()->last_null_bit_pos+7) >> 3))
790 {
791 /* Make sure to include null fields regardless of the presense of keys */
792 key_part_size = table_arg->getShare()->null_bytes + ((table_arg->getShare()->last_null_bit_pos+7) >> 3);
793 }
794
795
796
797 if (table_arg->found_next_number_field)
798 {
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;
801 }
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;
811
812 error= heap_create(table_name,
813 keys, &keydef[0],
814 column_count,
815 key_part_size,
816 table_arg->getShare()->getRecordLength(), mem_per_row_keys,
817 static_cast<uint32_t>(num_rows), /* We check for overflow above, so cast is fine here. */
818 0, // Factor out MIN
819 &hp_create_info, internal_share);
820
821 return (error);
822}
823
824
825void ha_heap::get_auto_increment(uint64_t, uint64_t, uint64_t,
826 uint64_t *first_value,
827 uint64_t *nb_reserved_values)
828{
829 ha_heap::info(HA_STATUS_AUTO);
830 *first_value= stats.auto_increment_value;
831 /* such table has only table-level locking so reserves up to +inf */
832 *nb_reserved_values= UINT64_MAX;
833}
834
835
836int ha_heap::cmp_ref(const unsigned char *ref1, const unsigned char *ref2)
837{
838 return memcmp(ref1, ref2, sizeof(HEAP_PTR));
839}
840
841
842DRIZZLE_DECLARE_PLUGIN
843{
844 DRIZZLE_VERSION_ID,
845 "memory",
846 "1.0",
847 "MySQL AB",
848 N_("MEMORY storage engine"),
849 PLUGIN_LICENSE_GPL,
850 heap_init,
851 NULL,
852 NULL
853}
854DRIZZLE_DECLARE_PLUGIN_END;
int ha_open(const identifier::Table &identifier, int mode, int test_if_locked)
unsigned char * null_ptr
Definition field.h:74
unsigned char null_bit
Definition field.h:154
drizzle_system_variables & variables
Definition session.h:195
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...
Definition ha_heap.cc:384
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....
Definition ha_heap.cc:373
int reset()
Definition ha_heap.cc:501
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....
Definition ha_heap.cc:395
int delete_all_rows(void)
Definition ha_heap.cc:507
int doStartTableScan(bool scan)
Definition ha_heap.cc:441
TODO: Rename this file - func.h is stupid.
STL namespace.