drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
ha_myisam.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
17
18#include <config.h>
19#include <drizzled/internal/my_bit.h>
20#include "myisampack.h"
21#include "ha_myisam.h"
22#include "myisam_priv.h"
23#include <drizzled/option.h>
24#include <drizzled/internal/my_bit.h>
25#include <drizzled/internal/m_string.h>
26#include <drizzled/util/test.h>
27#include <drizzled/error.h>
28#include <drizzled/errmsg_print.h>
29#include <drizzled/gettext.h>
30#include <drizzled/session.h>
31#include <drizzled/plugin.h>
32#include <drizzled/plugin/client.h>
33#include <drizzled/table.h>
34#include <drizzled/memory/multi_malloc.h>
35#include <drizzled/plugin/daemon.h>
36#include <drizzled/session/table_messages.h>
37#include <drizzled/plugin/storage_engine.h>
38#include <drizzled/key.h>
39#include <drizzled/statistics_variables.h>
40#include <drizzled/system_variables.h>
41#include <drizzled/key_part_info.h>
42
43#include <boost/algorithm/string.hpp>
44#include <boost/scoped_ptr.hpp>
45
46#include <string>
47#include <sstream>
48#include <map>
49#include <algorithm>
50#include <memory>
51#include <boost/program_options.hpp>
53
54namespace po= boost::program_options;
55
56using namespace std;
57using namespace drizzled;
58
59static const string engine_name("MyISAM");
60
61boost::mutex THR_LOCK_myisam;
62
63static uint32_t myisam_key_cache_block_size= KEY_CACHE_BLOCK_SIZE;
64static uint64_t max_sort_file_size;
65typedef constrained_check<size_t, SIZE_MAX, 1024, 1024> sort_buffer_constraint;
66static sort_buffer_constraint sort_buffer_size;
67
68/*****************************************************************************
69** MyISAM tables
70*****************************************************************************/
71
72static const char *ha_myisam_exts[] = {
73 ".MYI",
74 ".MYD",
75 NULL
76};
77
78class MyisamEngine : public plugin::StorageEngine
79{
80public:
81 explicit MyisamEngine(string name_arg) :
82 plugin::StorageEngine(name_arg,
83 HTON_CAN_INDEX_BLOBS |
84 HTON_STATS_RECORDS_IS_EXACT |
85 HTON_TEMPORARY_ONLY |
86 HTON_NULL_IN_KEY |
87 HTON_HAS_RECORDS |
88 HTON_DUPLICATE_POS |
89 HTON_AUTO_PART_KEY |
90 HTON_SKIP_STORE_LOCK)
91 {
92 }
93
94 virtual ~MyisamEngine()
95 {
96 mi_panic(HA_PANIC_CLOSE);
97 }
98
99 virtual Cursor *create(Table &table)
100 {
101 return new ha_myisam(*this, table);
102 }
103
104 const char **bas_ext() const {
105 return ha_myisam_exts;
106 }
107
108 int doCreateTable(Session&,
109 Table& table_arg,
110 const identifier::Table &identifier,
111 const message::Table&);
112
113 int doRenameTable(Session&, const identifier::Table &from, const identifier::Table &to);
114
115 int doDropTable(Session&, const identifier::Table &identifier);
116
117 int doGetTableDefinition(Session& session,
118 const identifier::Table &identifier,
119 message::Table &table_message);
120
121 uint32_t max_supported_keys() const { return MI_MAX_KEY; }
122 uint32_t max_supported_key_length() const { return MI_MAX_KEY_LENGTH; }
123 uint32_t max_supported_key_part_length() const { return MI_MAX_KEY_LENGTH; }
124
125 uint32_t index_flags(drizzled::message::Table::Index::IndexType) const
126 {
127 return (HA_READ_NEXT |
128 HA_READ_PREV |
129 HA_READ_RANGE |
130 HA_READ_ORDER |
131 HA_KEYREAD_ONLY);
132 }
133 bool doDoesTableExist(Session& session, const identifier::Table &identifier);
134
135 void doGetTableIdentifiers(drizzled::CachedDirectory &directory,
136 const drizzled::identifier::Schema &schema_identifier,
137 drizzled::identifier::table::vector &set_of_identifiers);
138 bool validateCreateTableOption(const std::string &key, const std::string &state)
139 {
140 (void)state;
141 if (boost::iequals(key, "ROW_FORMAT"))
142 {
143 return true;
144 }
145
146 return false;
147 }
148};
149
150void MyisamEngine::doGetTableIdentifiers(drizzled::CachedDirectory&,
152 drizzled::identifier::table::vector&)
153{
154}
155
156bool MyisamEngine::doDoesTableExist(Session &session, const identifier::Table &identifier)
157{
158 return session.getMessageCache().doesTableMessageExist(identifier);
159}
160
161int MyisamEngine::doGetTableDefinition(Session &session,
162 const identifier::Table &identifier,
163 message::Table &table_message)
164{
165 if (session.getMessageCache().getTableMessage(identifier, table_message))
166 return EEXIST;
167 return ENOENT;
168}
169
170/*
171 Convert to push_Warnings if you ever care about this, otherwise, it is a no-op.
172*/
173
174static void mi_check_print_msg(MI_CHECK *, const char* ,
175 const char *, va_list )
176{
177}
178
179
180/*
181 Convert Table object to MyISAM key and column definition
182
183 SYNOPSIS
184 table2myisam()
185 table_arg in Table object.
186 keydef_out out MyISAM key definition.
187 recinfo_out out MyISAM column definition.
188 records_out out Number of fields.
189
190 DESCRIPTION
191 This function will allocate and initialize MyISAM key and column
192 definition for further use in mi_create or for a check for underlying
193 table conformance in merge engine.
194
195 The caller needs to free *recinfo_out after use. Since *recinfo_out
196 and *keydef_out are allocated with a multi_malloc, *keydef_out
197 is freed automatically when *recinfo_out is freed.
198
199 RETURN VALUE
200 0 OK
201 !0 error code
202*/
203
204static int table2myisam(Table *table_arg, MI_KEYDEF **keydef_out,
205 MI_COLUMNDEF **recinfo_out, uint32_t *records_out)
206{
207 uint32_t i, j, recpos, minpos, fieldpos, temp_length, length;
208 enum ha_base_keytype type= HA_KEYTYPE_BINARY;
209 unsigned char *record;
210 MI_KEYDEF *keydef;
211 MI_COLUMNDEF *recinfo, *recinfo_pos;
212 HA_KEYSEG *keyseg;
213 TableShare *share= table_arg->getMutableShare();
214 uint32_t options= share->db_options_in_use;
215 if (!(memory::multi_malloc(false,
216 recinfo_out, (share->sizeFields() * 2 + 2) * sizeof(MI_COLUMNDEF),
217 keydef_out, share->sizeKeys() * sizeof(MI_KEYDEF),
218 &keyseg, (share->key_parts + share->sizeKeys()) * sizeof(HA_KEYSEG),
219 NULL)))
220 return(HA_ERR_OUT_OF_MEM);
221 keydef= *keydef_out;
222 recinfo= *recinfo_out;
223 for (i= 0; i < share->sizeKeys(); i++)
224 {
225 KeyInfo *pos= &table_arg->key_info[i];
226 keydef[i].flag= ((uint16_t) pos->flags & (HA_NOSAME));
227 keydef[i].key_alg= message::Table::Index::BTREE;
228 keydef[i].block_length= pos->block_size;
229 keydef[i].seg= keyseg;
230 keydef[i].keysegs= pos->key_parts;
231 for (j= 0; j < pos->key_parts; j++)
232 {
233 Field *field= pos->key_part[j].field;
234 type= field->key_type();
235 keydef[i].seg[j].flag= pos->key_part[j].key_part_flag;
236
237 if (options & HA_OPTION_PACK_KEYS ||
238 (pos->flags & (HA_PACK_KEY | HA_BINARY_PACK_KEY |
239 HA_SPACE_PACK_USED)))
240 {
241 if (pos->key_part[j].length > 8 &&
242 (type == HA_KEYTYPE_TEXT ||
243 (type == HA_KEYTYPE_BINARY && !field->zero_pack())))
244 {
245 /* No blobs here */
246 if (j == 0)
247 keydef[i].flag|= HA_PACK_KEY;
248 if ((((int) (pos->key_part[j].length - field->decimals())) >= 4))
249 keydef[i].seg[j].flag|= HA_SPACE_PACK;
250 }
251 else if (j == 0 && (!(pos->flags & HA_NOSAME) || pos->key_length > 16))
252 keydef[i].flag|= HA_BINARY_PACK_KEY;
253 }
254 keydef[i].seg[j].type= (int) type;
255 keydef[i].seg[j].start= pos->key_part[j].offset;
256 keydef[i].seg[j].length= pos->key_part[j].length;
257 keydef[i].seg[j].bit_start= keydef[i].seg[j].bit_end=
258 keydef[i].seg[j].bit_length= 0;
259 keydef[i].seg[j].bit_pos= 0;
260 keydef[i].seg[j].language= field->charset()->number;
261
262 if (field->null_ptr)
263 {
264 keydef[i].seg[j].null_bit= field->null_bit;
265 keydef[i].seg[j].null_pos= (uint) (field->null_ptr-
266 (unsigned char*) table_arg->getInsertRecord());
267 }
268 else
269 {
270 keydef[i].seg[j].null_bit= 0;
271 keydef[i].seg[j].null_pos= 0;
272 }
273 if (field->type() == DRIZZLE_TYPE_BLOB)
274 {
275 keydef[i].seg[j].flag|= HA_BLOB_PART;
276 /* save number of bytes used to pack length */
277 keydef[i].seg[j].bit_start= (uint) (field->pack_length() -
278 share->sizeBlobPtr());
279 }
280 }
281 keyseg+= pos->key_parts;
282 }
283 if (table_arg->found_next_number_field)
284 keydef[share->next_number_index].flag|= HA_AUTO_KEY;
285 record= table_arg->getInsertRecord();
286 recpos= 0;
287 recinfo_pos= recinfo;
288
289 while (recpos < (uint) share->sizeStoredRecord())
290 {
291 Field **field, *found= 0;
292 minpos= share->getRecordLength();
293 length= 0;
294
295 for (field= table_arg->getFields(); *field; field++)
296 {
297 if ((fieldpos= (*field)->offset(record)) >= recpos &&
298 fieldpos <= minpos)
299 {
300 /* skip null fields */
301 if (!(temp_length= (*field)->pack_length_in_rec()))
302 continue; /* Skip null-fields */
303
304 if (! found || fieldpos < minpos ||
305 (fieldpos == minpos && temp_length < length))
306 {
307 minpos= fieldpos;
308 found= *field;
309 length= temp_length;
310 }
311 }
312 }
313 if (recpos != minpos)
314 { // Reserved space (Null bits?)
315 memset(recinfo_pos, 0, sizeof(*recinfo_pos));
316 recinfo_pos->type= (int) FIELD_NORMAL;
317 recinfo_pos++->length= (uint16_t) (minpos - recpos);
318 }
319 if (!found)
320 break;
321
322 if (found->flags & BLOB_FLAG)
323 recinfo_pos->type= (int) FIELD_BLOB;
324 else if (found->type() == DRIZZLE_TYPE_VARCHAR)
325 recinfo_pos->type= FIELD_VARCHAR;
326 else if (!(options & HA_OPTION_PACK_RECORD))
327 recinfo_pos->type= (int) FIELD_NORMAL;
328 else if (found->zero_pack())
329 recinfo_pos->type= (int) FIELD_SKIP_ZERO;
330 else
331 recinfo_pos->type= (int) ((length <= 3) ? FIELD_NORMAL : FIELD_SKIP_PRESPACE);
332 if (found->null_ptr)
333 {
334 recinfo_pos->null_bit= found->null_bit;
335 recinfo_pos->null_pos= (uint) (found->null_ptr -
336 (unsigned char*) table_arg->getInsertRecord());
337 }
338 else
339 {
340 recinfo_pos->null_bit= 0;
341 recinfo_pos->null_pos= 0;
342 }
343 (recinfo_pos++)->length= (uint16_t) length;
344 recpos= minpos + length;
345 }
346 *records_out= (uint) (recinfo_pos - recinfo);
347 return(0);
348}
349
351{
352 file->s->state.auto_increment= value;
353 return 0;
354}
355
356/*
357 Check for underlying table conformance
358
359 SYNOPSIS
360 check_definition()
361 t1_keyinfo in First table key definition
362 t1_recinfo in First table record definition
363 t1_keys in Number of keys in first table
364 t1_recs in Number of records in first table
365 t2_keyinfo in Second table key definition
366 t2_recinfo in Second table record definition
367 t2_keys in Number of keys in second table
368 t2_recs in Number of records in second table
369 strict in Strict check switch
370
371 DESCRIPTION
372 This function compares two MyISAM definitions. By intention it was done
373 to compare merge table definition against underlying table definition.
374 It may also be used to compare dot-frm and MYI definitions of MyISAM
375 table as well to compare different MyISAM table definitions.
376
377 For merge table it is not required that number of keys in merge table
378 must exactly match number of keys in underlying table. When calling this
379 function for underlying table conformance check, 'strict' flag must be
380 set to false, and converted merge definition must be passed as t1_*.
381
382 Otherwise 'strict' flag must be set to 1 and it is not required to pass
383 converted dot-frm definition as t1_*.
384
385 RETURN VALUE
386 0 - Equal definitions.
387 1 - Different definitions.
388
389 TODO
390 - compare FULLTEXT keys;
391 - compare SPATIAL keys;
392 - compare FIELD_SKIP_ZERO which is converted to FIELD_NORMAL correctly
393 (should be corretly detected in table2myisam).
394*/
395
396static int check_definition(MI_KEYDEF *t1_keyinfo, MI_COLUMNDEF *t1_recinfo,
397 uint32_t t1_keys, uint32_t t1_recs,
398 MI_KEYDEF *t2_keyinfo, MI_COLUMNDEF *t2_recinfo,
399 uint32_t t2_keys, uint32_t t2_recs, bool strict)
400{
401 uint32_t i, j;
402 if ((strict ? t1_keys != t2_keys : t1_keys > t2_keys))
403 {
404 return(1);
405 }
406 if (t1_recs != t2_recs)
407 {
408 return(1);
409 }
410 for (i= 0; i < t1_keys; i++)
411 {
412 HA_KEYSEG *t1_keysegs= t1_keyinfo[i].seg;
413 HA_KEYSEG *t2_keysegs= t2_keyinfo[i].seg;
414 if (t1_keyinfo[i].keysegs != t2_keyinfo[i].keysegs ||
415 t1_keyinfo[i].key_alg != t2_keyinfo[i].key_alg)
416 {
417 return(1);
418 }
419 for (j= t1_keyinfo[i].keysegs; j--;)
420 {
421 uint8_t t1_keysegs_j__type= t1_keysegs[j].type;
422
423 /*
424 Table migration from 4.1 to 5.1. In 5.1 a *TEXT key part is
425 always HA_KEYTYPE_VARTEXT2. In 4.1 we had only the equivalent of
426 HA_KEYTYPE_VARTEXT1. Since we treat both the same on MyISAM
427 level, we can ignore a mismatch between these types.
428 */
429 if ((t1_keysegs[j].flag & HA_BLOB_PART) &&
430 (t2_keysegs[j].flag & HA_BLOB_PART))
431 {
432 if ((t1_keysegs_j__type == HA_KEYTYPE_VARTEXT2) &&
433 (t2_keysegs[j].type == HA_KEYTYPE_VARTEXT1))
434 t1_keysegs_j__type= HA_KEYTYPE_VARTEXT1;
435 else if ((t1_keysegs_j__type == HA_KEYTYPE_VARBINARY2) &&
436 (t2_keysegs[j].type == HA_KEYTYPE_VARBINARY1))
437 t1_keysegs_j__type= HA_KEYTYPE_VARBINARY1;
438 }
439
440 if (t1_keysegs_j__type != t2_keysegs[j].type ||
441 t1_keysegs[j].language != t2_keysegs[j].language ||
442 t1_keysegs[j].null_bit != t2_keysegs[j].null_bit ||
443 t1_keysegs[j].length != t2_keysegs[j].length)
444 {
445 return(1);
446 }
447 }
448 }
449 for (i= 0; i < t1_recs; i++)
450 {
451 MI_COLUMNDEF *t1_rec= &t1_recinfo[i];
452 MI_COLUMNDEF *t2_rec= &t2_recinfo[i];
453 /*
454 FIELD_SKIP_ZERO can be changed to FIELD_NORMAL in mi_create,
455 see NOTE1 in mi_create.c
456 */
457 if ((t1_rec->type != t2_rec->type &&
458 !(t1_rec->type == (int) FIELD_SKIP_ZERO &&
459 t1_rec->length == 1 &&
460 t2_rec->type == (int) FIELD_NORMAL)) ||
461 t1_rec->length != t2_rec->length ||
462 t1_rec->null_bit != t2_rec->null_bit)
463 {
464 return(1);
465 }
466 }
467 return(0);
468}
469
470
471volatile int *killed_ptr(MI_CHECK *param)
472{
473 /* In theory Unsafe conversion, but should be ok for now */
474 return (int*) (((Session *)(param->session))->getKilledPtr());
475}
476
477void mi_check_print_error(MI_CHECK *param, const char *fmt,...)
478{
479 param->error_printed|=1;
480 param->out_flag|= O_DATA_LOST;
481 va_list args;
482 va_start(args, fmt);
483 mi_check_print_msg(param, "error", fmt, args);
484 va_end(args);
485}
486
487void mi_check_print_info(MI_CHECK *param, const char *fmt,...)
488{
489 va_list args;
490 va_start(args, fmt);
491 mi_check_print_msg(param, "info", fmt, args);
492 va_end(args);
493}
494
495void mi_check_print_warning(MI_CHECK *param, const char *fmt,...)
496{
497 param->warning_printed=1;
498 param->out_flag|= O_DATA_LOST;
499 va_list args;
500 va_start(args, fmt);
501 mi_check_print_msg(param, "warning", fmt, args);
502 va_end(args);
503}
504
519
520void _mi_report_crashed(MI_INFO *file, const char *message,
521 const char *sfile, uint32_t sline)
522{
523 Session *cur_session;
524 if ((cur_session= file->in_use))
525 {
526 errmsg_printf(error::ERROR, _("Got an error from thread_id=%" PRIu64 ", %s:%d"),
527 cur_session->thread_id,
528 sfile, sline);
529 }
530 else
531 {
532 errmsg_printf(error::ERROR, _("Got an error from unknown thread, %s:%d"), sfile, sline);
533 }
534
535 if (message)
536 errmsg_printf(error::ERROR, "%s", message);
537
538 list<Session *>::iterator it= file->s->in_use->begin();
539 while (it != file->s->in_use->end())
540 {
541 errmsg_printf(error::ERROR, "%s", _("Unknown thread accessing table"));
542 ++it;
543 }
544}
545
546ha_myisam::ha_myisam(plugin::StorageEngine &engine_arg,
547 Table &table_arg)
548 : Cursor(engine_arg, table_arg),
549 file(0),
550 can_enable_indexes(true),
551 is_ordered(true)
552{ }
553
554Cursor *ha_myisam::clone(memory::Root *mem_root)
555{
556 ha_myisam *new_handler= static_cast <ha_myisam *>(Cursor::clone(mem_root));
557 if (new_handler)
558 new_handler->file->state= file->state;
559 return new_handler;
560}
561
562const char *ha_myisam::index_type(uint32_t )
563{
564 return "BTREE";
565}
566
567/* Name is here without an extension */
568int ha_myisam::doOpen(const drizzled::identifier::Table &identifier, int mode, uint32_t test_if_locked)
569{
570 MI_KEYDEF *keyinfo;
571 MI_COLUMNDEF *recinfo= 0;
572 uint32_t recs;
573 uint32_t i;
574
575 /*
576 If the user wants to have memory mapped data files, add an
577 open_flag. Do not memory map temporary tables because they are
578 expected to be inserted and thus extended a lot. Memory mapping is
579 efficient for files that keep their size, but very inefficient for
580 growing files. Using an open_flag instead of calling mi_extra(...
581 HA_EXTRA_MMAP ...) after mi_open() has the advantage that the
582 mapping is not repeated for every open, but just done on the initial
583 open, when the MyISAM share is created. Everytime the server
584 requires to open a new instance of a table it calls this method. We
585 will always supply HA_OPEN_MMAP for a permanent table. However, the
586 MyISAM storage engine will ignore this flag if this is a secondary
587 open of a table that is in use by other threads already (if the
588 MyISAM share exists already).
589 */
590 if (!(file= mi_open(identifier, mode, test_if_locked)))
591 return (errno ? errno : -1);
592
593 if (getTable()->getShare()->getType() == message::Table::STANDARD)
594 {
595 if ((errno= table2myisam(getTable(), &keyinfo, &recinfo, &recs)))
596 {
597 goto err;
598 }
599 if (check_definition(keyinfo, recinfo, getTable()->getShare()->sizeKeys(), recs,
600 file->s->keyinfo, file->s->rec,
601 file->s->base.keys, file->s->base.fields, true))
602 {
603 errno= HA_ERR_CRASHED;
604 goto err;
605 }
606 }
607
608 assert(test_if_locked);
609 if (test_if_locked & (HA_OPEN_IGNORE_IF_LOCKED | HA_OPEN_TMP_TABLE))
610 mi_extra(file, HA_EXTRA_NO_WAIT_LOCK, 0);
611
612 info(HA_STATUS_NO_LOCK | HA_STATUS_VARIABLE | HA_STATUS_CONST);
613 if (!(test_if_locked & HA_OPEN_WAIT_IF_LOCKED))
614 mi_extra(file, HA_EXTRA_WAIT_LOCK, 0);
615 if (!getTable()->getShare()->db_record_offset)
616 is_ordered= false;
617
618
619 keys_with_parts.reset();
620 for (i= 0; i < getTable()->getShare()->sizeKeys(); i++)
621 {
622 getTable()->key_info[i].block_size= file->s->keyinfo[i].block_length;
623
624 KeyPartInfo *kp= getTable()->key_info[i].key_part;
625 KeyPartInfo *kp_end= kp + getTable()->key_info[i].key_parts;
626 for (; kp != kp_end; kp++)
627 {
628 if (!kp->field->part_of_key.test(i))
629 {
630 keys_with_parts.set(i);
631 break;
632 }
633 }
634 }
635 errno= 0;
636 goto end;
637 err:
638 this->close();
639 end:
640 /*
641 Both recinfo and keydef are allocated by multi_malloc(), thus only
642 recinfo must be freed.
643 */
644 if (recinfo)
645 free((unsigned char*) recinfo);
646 return errno;
647}
648
649int ha_myisam::close(void)
650{
651 MI_INFO *tmp=file;
652 file=0;
653 return mi_close(tmp);
654}
655
656int ha_myisam::doInsertRecord(unsigned char *buf)
657{
658 /*
659 If we have an auto_increment column and we are writing a changed row
660 or a new row, then update the auto_increment value in the record.
661 */
662 if (getTable()->next_number_field && buf == getTable()->getInsertRecord())
663 {
664 int error;
665 if ((error= update_auto_increment()))
666 return error;
667 }
668 return mi_write(file,buf);
669}
670
671
672int ha_myisam::repair(Session *session, MI_CHECK &param, bool do_optimize)
673{
674 int error=0;
675 uint32_t local_testflag= param.testflag;
676 bool optimize_done= !do_optimize, statistics_done=0;
677 const char *old_proc_info= session->get_proc_info();
678 char fixed_name[FN_REFLEN];
679 MYISAM_SHARE* share = file->s;
680 ha_rows rows= file->state->records;
681
682 /*
683 Normally this method is entered with a properly opened table. If the
684 repair fails, it can be repeated with more elaborate options. Under
685 special circumstances it can happen that a repair fails so that it
686 closed the data file and cannot re-open it. In this case file->dfile
687 is set to -1. We must not try another repair without an open data
688 file. (Bug #25289)
689 */
690 if (file->dfile == -1)
691 {
692 errmsg_printf(error::INFO, "Retrying repair of: '%s' failed. "
693 "Please try REPAIR EXTENDED or myisamchk",
694 getTable()->getShare()->getPath());
695 return(HA_ADMIN_FAILED);
696 }
697
698 param.db_name= getTable()->getShare()->getSchemaName();
699 param.table_name= getTable()->getAlias();
700 param.tmpfile_createflag = O_RDWR | O_TRUNC;
701 param.using_global_keycache = 1;
702 param.session= session;
703 param.out_flag= 0;
704 param.sort_buffer_length= static_cast<size_t>(sort_buffer_size);
705 strcpy(fixed_name,file->filename);
706
707 // Don't lock tables if we have used LOCK Table
708 if (mi_lock_database(file, getTable()->getShare()->getType() ? F_EXTRA_LCK : F_WRLCK))
709 {
710 mi_check_print_error(&param,ER(ER_CANT_LOCK),errno);
711 return(HA_ADMIN_FAILED);
712 }
713
714 if (!do_optimize ||
715 ((file->state->del || share->state.split != file->state->records) &&
716 (!(param.testflag & T_QUICK) ||
717 !(share->state.changed & STATE_NOT_OPTIMIZED_KEYS))))
718 {
719 uint64_t key_map= ((local_testflag & T_CREATE_MISSING_KEYS) ?
720 mi_get_mask_all_keys_active(share->base.keys) :
721 share->state.key_map);
722 uint32_t testflag=param.testflag;
723 if (mi_test_if_sort_rep(file,file->state->records,key_map,0) &&
724 (local_testflag & T_REP_BY_SORT))
725 {
726 local_testflag|= T_STATISTICS;
727 param.testflag|= T_STATISTICS; // We get this for free
728 statistics_done=1;
729 {
730 session->set_proc_info("Repair by sorting");
731 error = mi_repair_by_sort(&param, file, fixed_name,
732 param.testflag & T_QUICK);
733 }
734 }
735 else
736 {
737 session->set_proc_info("Repair with keycache");
738 param.testflag &= ~T_REP_BY_SORT;
739 error= mi_repair(&param, file, fixed_name,
740 param.testflag & T_QUICK);
741 }
742 param.testflag=testflag;
743 optimize_done=1;
744 }
745 if (!error)
746 {
747 if ((local_testflag & T_SORT_INDEX) &&
748 (share->state.changed & STATE_NOT_SORTED_PAGES))
749 {
750 optimize_done=1;
751 session->set_proc_info("Sorting index");
752 error=mi_sort_index(&param,file,fixed_name);
753 }
754 if (!statistics_done && (local_testflag & T_STATISTICS))
755 {
756 if (share->state.changed & STATE_NOT_ANALYZED)
757 {
758 optimize_done=1;
759 session->set_proc_info("Analyzing");
760 error = chk_key(&param, file);
761 }
762 else
763 local_testflag&= ~T_STATISTICS; // Don't update statistics
764 }
765 }
766 session->set_proc_info("Saving state");
767 if (!error)
768 {
769 if ((share->state.changed & STATE_CHANGED) || mi_is_crashed(file))
770 {
771 share->state.changed&= ~(STATE_CHANGED | STATE_CRASHED |
772 STATE_CRASHED_ON_REPAIR);
773 file->update|=HA_STATE_CHANGED | HA_STATE_ROW_CHANGED;
774 }
775 /*
776 the following 'if', thought conceptually wrong,
777 is a useful optimization nevertheless.
778 */
779 if (file->state != &file->s->state.state)
780 file->s->state.state = *file->state;
781 if (file->s->base.auto_key)
782 update_auto_increment_key(&param, file, 1);
783 if (optimize_done)
784 error = update_state_info(&param, file,
785 UPDATE_TIME | UPDATE_OPEN_COUNT |
786 (local_testflag &
787 T_STATISTICS ? UPDATE_STAT : 0));
788 info(HA_STATUS_NO_LOCK | HA_STATUS_TIME | HA_STATUS_VARIABLE |
789 HA_STATUS_CONST);
790 if (rows != file->state->records && ! (param.testflag & T_VERY_SILENT))
791 {
792 char llbuff[22],llbuff2[22];
793 mi_check_print_warning(&param,"Number of rows changed from %s to %s",
794 internal::llstr(rows,llbuff),
795 internal::llstr(file->state->records,llbuff2));
796 }
797 }
798 else
799 {
800 mi_mark_crashed_on_repair(file);
801 file->update |= HA_STATE_CHANGED | HA_STATE_ROW_CHANGED;
802 update_state_info(&param, file, 0);
803 }
804 session->set_proc_info(old_proc_info);
805 mi_lock_database(file,F_UNLCK);
806
807 return(error ? HA_ADMIN_FAILED :
808 !optimize_done ? HA_ADMIN_ALREADY_DONE : HA_ADMIN_OK);
809}
810
811
812/*
813 Disable indexes, making it persistent if requested.
814
815 SYNOPSIS
816 disable_indexes()
817 mode mode of operation:
818 HA_KEY_SWITCH_NONUNIQ disable all non-unique keys
819 HA_KEY_SWITCH_ALL disable all keys
820 HA_KEY_SWITCH_NONUNIQ_SAVE dis. non-uni. and make persistent
821 HA_KEY_SWITCH_ALL_SAVE dis. all keys and make persistent
822
823 IMPLEMENTATION
824 HA_KEY_SWITCH_NONUNIQ is not implemented.
825 HA_KEY_SWITCH_ALL_SAVE is not implemented.
826
827 RETURN
828 0 ok
829 HA_ERR_WRONG_COMMAND mode not implemented.
830*/
831
832int ha_myisam::disable_indexes(uint32_t mode)
833{
834 int error;
835
836 if (mode == HA_KEY_SWITCH_ALL)
837 {
838 /* call a storage engine function to switch the key map */
839 error= mi_disable_indexes(file);
840 }
841 else if (mode == HA_KEY_SWITCH_NONUNIQ_SAVE)
842 {
843 mi_extra(file, HA_EXTRA_NO_KEYS, 0);
844 info(HA_STATUS_CONST); // Read new key info
845 error= 0;
846 }
847 else
848 {
849 /* mode not implemented */
850 error= HA_ERR_WRONG_COMMAND;
851 }
852 return error;
853}
854
855
856/*
857 Enable indexes, making it persistent if requested.
858
859 SYNOPSIS
860 enable_indexes()
861 mode mode of operation:
862 HA_KEY_SWITCH_NONUNIQ enable all non-unique keys
863 HA_KEY_SWITCH_ALL enable all keys
864 HA_KEY_SWITCH_NONUNIQ_SAVE en. non-uni. and make persistent
865 HA_KEY_SWITCH_ALL_SAVE en. all keys and make persistent
866
867 DESCRIPTION
868 Enable indexes, which might have been disabled by disable_index() before.
869 The modes without _SAVE work only if both data and indexes are empty,
870 since the MyISAM repair would enable them persistently.
871 To be sure in these cases, call Cursor::delete_all_rows() before.
872
873 IMPLEMENTATION
874 HA_KEY_SWITCH_NONUNIQ is not implemented.
875 HA_KEY_SWITCH_ALL_SAVE is not implemented.
876
877 RETURN
878 0 ok
879 !=0 Error, among others:
880 HA_ERR_CRASHED data or index is non-empty. Delete all rows and retry.
881 HA_ERR_WRONG_COMMAND mode not implemented.
882*/
883
884int ha_myisam::enable_indexes(uint32_t mode)
885{
886 int error;
887
888 if (mi_is_all_keys_active(file->s->state.key_map, file->s->base.keys))
889 {
890 /* All indexes are enabled already. */
891 return 0;
892 }
893
894 if (mode == HA_KEY_SWITCH_ALL)
895 {
896 error= mi_enable_indexes(file);
897 /*
898 Do not try to repair on error,
899 as this could make the enabled state persistent,
900 but mode==HA_KEY_SWITCH_ALL forbids it.
901 */
902 }
903 else if (mode == HA_KEY_SWITCH_NONUNIQ_SAVE)
904 {
905 Session *session= getTable()->in_use;
906 boost::scoped_ptr<MI_CHECK> param_ap(new MI_CHECK);
907 MI_CHECK &param= *param_ap.get();
908 const char *save_proc_info= session->get_proc_info();
909 session->set_proc_info("Creating index");
910 myisamchk_init(&param);
911 param.op_name= "recreating_index";
912 param.testflag= (T_SILENT | T_REP_BY_SORT | T_QUICK |
913 T_CREATE_MISSING_KEYS);
914 param.myf_rw&= ~MY_WAIT_IF_FULL;
915 param.sort_buffer_length= static_cast<size_t>(sort_buffer_size);
916 param.stats_method= MI_STATS_METHOD_NULLS_NOT_EQUAL;
917 if ((error= (repair(session,param,0) != HA_ADMIN_OK)) && param.retry_repair)
918 {
919 errmsg_printf(error::WARN, "Warning: Enabling keys got errno %d on %s.%s, retrying",
920 errno, param.db_name, param.table_name);
921 /* Repairing by sort failed. Now try standard repair method. */
922 param.testflag&= ~(T_REP_BY_SORT | T_QUICK);
923 error= (repair(session,param,0) != HA_ADMIN_OK);
924 /*
925 If the standard repair succeeded, clear all error messages which
926 might have been set by the first repair. They can still be seen
927 with SHOW WARNINGS then.
928 */
929 if (not error)
930 session->clear_error();
931 }
932 info(HA_STATUS_CONST);
933 session->set_proc_info(save_proc_info);
934 }
935 else
936 {
937 /* mode not implemented */
938 error= HA_ERR_WRONG_COMMAND;
939 }
940 return error;
941}
942
943
944/*
945 Test if indexes are disabled.
946
947
948 SYNOPSIS
949 indexes_are_disabled()
950 no parameters
951
952
953 RETURN
954 0 indexes are not disabled
955 1 all indexes are disabled
956 [2 non-unique indexes are disabled - NOT YET IMPLEMENTED]
957*/
958
959int ha_myisam::indexes_are_disabled(void)
960{
961
962 return mi_indexes_are_disabled(file);
963}
964
965
966/*
967 prepare for a many-rows insert operation
968 e.g. - disable indexes (if they can be recreated fast) or
969 activate special bulk-insert optimizations
970
971 SYNOPSIS
972 start_bulk_insert(rows)
973 rows Rows to be inserted
974 0 if we don't know
975
976 NOTICE
977 Do not forget to call end_bulk_insert() later!
978*/
979
980void ha_myisam::start_bulk_insert(ha_rows rows)
981{
982 Session *session= getTable()->in_use;
983 ulong size= session->variables.read_buff_size;
984
985 /* don't enable row cache if too few rows */
986 if (! rows || (rows > MI_MIN_ROWS_TO_USE_WRITE_CACHE))
987 mi_extra(file, HA_EXTRA_WRITE_CACHE, (void*) &size);
988
989 can_enable_indexes= mi_is_all_keys_active(file->s->state.key_map,
990 file->s->base.keys);
991
992 /*
993 Only disable old index if the table was empty and we are inserting
994 a lot of rows.
995 We should not do this for only a few rows as this is slower and
996 we don't want to update the key statistics based of only a few rows.
997 */
998 if (file->state->records == 0 && can_enable_indexes &&
999 (!rows || rows >= MI_MIN_ROWS_TO_DISABLE_INDEXES))
1000 mi_disable_non_unique_index(file,rows);
1001 else
1002 if (!file->bulk_insert &&
1003 (!rows || rows >= MI_MIN_ROWS_TO_USE_BULK_INSERT))
1004 {
1005 mi_init_bulk_insert(file,
1006 (size_t)session->variables.bulk_insert_buff_size,
1007 rows);
1008 }
1009}
1010
1011/*
1012 end special bulk-insert optimizations,
1013 which have been activated by start_bulk_insert().
1014
1015 SYNOPSIS
1016 end_bulk_insert()
1017 no arguments
1018
1019 RETURN
1020 0 OK
1021 != 0 Error
1022*/
1023
1024int ha_myisam::end_bulk_insert()
1025{
1026 mi_end_bulk_insert(file);
1027 int err=mi_extra(file, HA_EXTRA_NO_CACHE, 0);
1028 return err ? err : can_enable_indexes ?
1029 enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE) : 0;
1030}
1031
1032
1033
1034int ha_myisam::doUpdateRecord(const unsigned char *old_data, unsigned char *new_data)
1035{
1036 return mi_update(file,old_data,new_data);
1037}
1038
1039int ha_myisam::doDeleteRecord(const unsigned char *buf)
1040{
1041 return mi_delete(file,buf);
1042}
1043
1044
1045int ha_myisam::doStartIndexScan(uint32_t idx, bool )
1046{
1047 active_index=idx;
1048 //in_range_read= false;
1049 return 0;
1050}
1051
1052
1053int ha_myisam::doEndIndexScan()
1054{
1055 active_index=MAX_KEY;
1056 return 0;
1057}
1058
1059
1060int ha_myisam::index_read_map(unsigned char *buf, const unsigned char *key,
1061 key_part_map keypart_map,
1062 enum ha_rkey_function find_flag)
1063{
1064 assert(inited==INDEX);
1065 ha_statistic_increment(&system_status_var::ha_read_key_count);
1066 int error=mi_rkey(file, buf, active_index, key, keypart_map, find_flag);
1067 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1068 return error;
1069}
1070
1071int ha_myisam::index_read_idx_map(unsigned char *buf, uint32_t index, const unsigned char *key,
1072 key_part_map keypart_map,
1073 enum ha_rkey_function find_flag)
1074{
1075 ha_statistic_increment(&system_status_var::ha_read_key_count);
1076 int error=mi_rkey(file, buf, index, key, keypart_map, find_flag);
1077 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1078 return error;
1079}
1080
1081int ha_myisam::index_read_last_map(unsigned char *buf, const unsigned char *key,
1082 key_part_map keypart_map)
1083{
1084 assert(inited==INDEX);
1085 ha_statistic_increment(&system_status_var::ha_read_key_count);
1086 int error=mi_rkey(file, buf, active_index, key, keypart_map,
1087 HA_READ_PREFIX_LAST);
1088 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1089 return(error);
1090}
1091
1092int ha_myisam::index_next(unsigned char *buf)
1093{
1094 assert(inited==INDEX);
1095 ha_statistic_increment(&system_status_var::ha_read_next_count);
1096 int error=mi_rnext(file,buf,active_index);
1097 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1098 return error;
1099}
1100
1101int ha_myisam::index_prev(unsigned char *buf)
1102{
1103 assert(inited==INDEX);
1104 ha_statistic_increment(&system_status_var::ha_read_prev_count);
1105 int error=mi_rprev(file,buf, active_index);
1106 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1107 return error;
1108}
1109
1110int ha_myisam::index_first(unsigned char *buf)
1111{
1112 assert(inited==INDEX);
1113 ha_statistic_increment(&system_status_var::ha_read_first_count);
1114 int error=mi_rfirst(file, buf, active_index);
1115 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1116 return error;
1117}
1118
1119int ha_myisam::index_last(unsigned char *buf)
1120{
1121 assert(inited==INDEX);
1122 ha_statistic_increment(&system_status_var::ha_read_last_count);
1123 int error=mi_rlast(file, buf, active_index);
1124 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1125 return error;
1126}
1127
1128int ha_myisam::index_next_same(unsigned char *buf,
1129 const unsigned char *,
1130 uint32_t )
1131{
1132 int error;
1133 assert(inited==INDEX);
1134 ha_statistic_increment(&system_status_var::ha_read_next_count);
1135 do
1136 {
1137 error= mi_rnext_same(file,buf);
1138 } while (error == HA_ERR_RECORD_DELETED);
1139 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1140 return error;
1141}
1142
1144 const key_range *end_key,
1145 bool eq_range_arg,
1146 bool sorted /* ignored */)
1147{
1148 int res;
1149 //if (!eq_range_arg)
1150 // in_range_read= true;
1151
1152 res= Cursor::read_range_first(start_key, end_key, eq_range_arg, sorted);
1153
1154 //if (res)
1155 // in_range_read= false;
1156 return res;
1157}
1158
1159
1161{
1162 int res= Cursor::read_range_next();
1163 //if (res)
1164 // in_range_read= false;
1165 return res;
1166}
1167
1168
1170{
1171 if (scan)
1172 return mi_scan_init(file);
1173 return mi_reset(file); // Free buffers
1174}
1175
1176int ha_myisam::rnd_next(unsigned char *buf)
1177{
1178 ha_statistic_increment(&system_status_var::ha_read_rnd_next_count);
1179 int error=mi_scan(file, buf);
1180 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1181 return error;
1182}
1183
1184int ha_myisam::rnd_pos(unsigned char *buf, unsigned char *pos)
1185{
1186 ha_statistic_increment(&system_status_var::ha_read_rnd_count);
1187 int error=mi_rrnd(file, buf, internal::my_get_ptr(pos,ref_length));
1188 getTable()->status=error ? STATUS_NOT_FOUND: 0;
1189 return error;
1190}
1191
1192
1193void ha_myisam::position(const unsigned char *)
1194{
1195 internal::my_off_t row_position= mi_position(file);
1196 internal::my_store_ptr(ref, ref_length, row_position);
1197}
1198
1199int ha_myisam::info(uint32_t flag)
1200{
1201 MI_ISAMINFO misam_info;
1202
1203 (void) mi_status(file,&misam_info,flag);
1204 if (flag & HA_STATUS_VARIABLE)
1205 {
1206 stats.records= misam_info.records;
1207 stats.deleted= misam_info.deleted;
1208 stats.data_file_length= misam_info.data_file_length;
1209 stats.index_file_length= misam_info.index_file_length;
1210 stats.delete_length= misam_info.delete_length;
1211 stats.check_time= misam_info.check_time;
1212 stats.mean_rec_length= misam_info.mean_reclength;
1213 }
1214 if (flag & HA_STATUS_CONST)
1215 {
1216 TableShare *share= getTable()->getMutableShare();
1217 stats.max_data_file_length= misam_info.max_data_file_length;
1218 stats.max_index_file_length= misam_info.max_index_file_length;
1219 stats.create_time= misam_info.create_time;
1220 ref_length= misam_info.reflength;
1221 share->db_options_in_use= misam_info.options;
1222 stats.block_size= myisam_key_cache_block_size; /* record block size */
1223
1224 set_prefix(share->keys_in_use, share->sizeKeys());
1225 /*
1226 * Due to bug 394932 (32-bit solaris build failure), we need
1227 * to convert the uint64_t key_map member of the misam_info
1228 * structure in to a std::bitset so that we can logically and
1229 * it with the share->key_in_use key_map.
1230 */
1231 ostringstream ostr;
1232 string binary_key_map;
1233 uint64_t num= misam_info.key_map;
1234 /*
1235 * Convert the uint64_t to a binary
1236 * string representation of it.
1237 */
1238 while (num > 0)
1239 {
1240 uint64_t bin_digit= num % 2;
1241 ostr << bin_digit;
1242 num/= 2;
1243 }
1244 binary_key_map.append(ostr.str());
1245 /*
1246 * Now we have the binary string representation of the
1247 * flags, we need to fill that string representation out
1248 * with the appropriate number of bits. This is needed
1249 * since key_map is declared as a std::bitset of a certain bit
1250 * width that depends on the MAX_INDEXES variable.
1251 */
1252 if (MAX_INDEXES <= 64)
1253 {
1254 size_t len= 72 - binary_key_map.length();
1255 string all_zeros(len, '0');
1256 binary_key_map.insert(binary_key_map.begin(),
1257 all_zeros.begin(),
1258 all_zeros.end());
1259 }
1260 else
1261 {
1262 size_t len= (MAX_INDEXES + 7) / 8 * 8;
1263 string all_zeros(len, '0');
1264 binary_key_map.insert(binary_key_map.begin(),
1265 all_zeros.begin(),
1266 all_zeros.end());
1267 }
1268 key_map tmp_map(binary_key_map);
1269 share->keys_in_use&= tmp_map;
1270 share->keys_for_keyread&= share->keys_in_use;
1271 share->db_record_offset= misam_info.record_offset;
1272 if (share->key_parts)
1273 memcpy(getTable()->key_info[0].rec_per_key,
1274 misam_info.rec_per_key,
1275 sizeof(getTable()->key_info[0].rec_per_key)*share->key_parts);
1276 assert(share->getType() != message::Table::STANDARD);
1277
1278 }
1279 if (flag & HA_STATUS_ERRKEY)
1280 {
1281 errkey = misam_info.errkey;
1282 internal::my_store_ptr(dup_ref, ref_length, misam_info.dupp_key_pos);
1283 }
1284 if (flag & HA_STATUS_TIME)
1285 stats.update_time = misam_info.update_time;
1286 if (flag & HA_STATUS_AUTO)
1287 stats.auto_increment_value= misam_info.auto_increment;
1288
1289 return 0;
1290}
1291
1292
1293int ha_myisam::extra(enum ha_extra_function operation)
1294{
1295 return mi_extra(file, operation, 0);
1296}
1297
1299{
1300 return mi_reset(file);
1301}
1302
1303/* To be used with WRITE_CACHE and EXTRA_CACHE */
1304
1305int ha_myisam::extra_opt(enum ha_extra_function operation, uint32_t cache_size)
1306{
1307 return mi_extra(file, operation, (void*) &cache_size);
1308}
1309
1311{
1312 return mi_delete_all_rows(file);
1313}
1314
1315int MyisamEngine::doDropTable(Session &session,
1316 const identifier::Table &identifier)
1317{
1318 session.getMessageCache().removeTableMessage(identifier);
1319
1320 return mi_delete_table(identifier.getPath().c_str());
1321}
1322
1323
1324int ha_myisam::external_lock(Session *session, int lock_type)
1325{
1326 file->in_use= session;
1327 return mi_lock_database(file, !getTable()->getShare()->getType() ?
1328 lock_type : ((lock_type == F_UNLCK) ?
1329 F_UNLCK : F_EXTRA_LCK));
1330}
1331
1332int MyisamEngine::doCreateTable(Session &session,
1333 Table& table_arg,
1334 const identifier::Table &identifier,
1335 const message::Table& create_proto)
1336{
1337 int error;
1338 uint32_t create_flags= 0, create_records;
1339 char buff[FN_REFLEN];
1340 MI_KEYDEF *keydef;
1341 MI_COLUMNDEF *recinfo;
1342 MI_CREATE_INFO create_info;
1343 TableShare *share= table_arg.getMutableShare();
1344 uint32_t options= share->db_options_in_use;
1345 if ((error= table2myisam(&table_arg, &keydef, &recinfo, &create_records)))
1346 return(error);
1347 create_info.max_rows= create_proto.options().max_rows();
1348 create_info.reloc_rows= create_proto.options().min_rows();
1349 create_info.with_auto_increment= share->next_number_key_offset == 0;
1350 create_info.auto_increment= (create_proto.options().has_auto_increment_value() ?
1351 create_proto.options().auto_increment_value() -1 :
1352 (uint64_t) 0);
1353 create_info.data_file_length= (create_proto.options().max_rows() *
1354 create_proto.options().avg_row_length());
1355 create_info.data_file_name= NULL;
1356 create_info.index_file_name= NULL;
1357 create_info.language= share->table_charset->number;
1358
1359 if (create_proto.type() == message::Table::TEMPORARY)
1360 create_flags|= HA_CREATE_TMP_TABLE;
1361 if (options & HA_OPTION_PACK_RECORD)
1362 create_flags|= HA_PACK_RECORD;
1363
1364 /* TODO: Check that the following internal::fn_format is really needed */
1365 error= mi_create(internal::fn_format(buff, identifier.getPath().c_str(), "", "",
1366 MY_UNPACK_FILENAME|MY_APPEND_EXT),
1367 share->sizeKeys(), keydef,
1368 create_records, recinfo,
1369 0, (MI_UNIQUEDEF*) 0,
1370 &create_info, create_flags);
1371 free((unsigned char*) recinfo);
1372
1373 session.getMessageCache().storeTableMessage(identifier, create_proto);
1374
1375 return error;
1376}
1377
1378
1379int MyisamEngine::doRenameTable(Session &session, const identifier::Table &from, const identifier::Table &to)
1380{
1381 session.getMessageCache().renameTableMessage(from, to);
1382
1383 return mi_rename(from.getPath().c_str(), to.getPath().c_str());
1384}
1385
1386
1387void ha_myisam::get_auto_increment(uint64_t ,
1388 uint64_t ,
1389 uint64_t ,
1390 uint64_t *first_value,
1391 uint64_t *nb_reserved_values)
1392{
1393 uint64_t nr;
1394 int error;
1395 unsigned char key[MI_MAX_KEY_LENGTH];
1396
1397 if (!getTable()->getShare()->next_number_key_offset)
1398 { // Autoincrement at key-start
1399 ha_myisam::info(HA_STATUS_AUTO);
1400 *first_value= stats.auto_increment_value;
1401 /* MyISAM has only table-level lock, so reserves to +inf */
1402 *nb_reserved_values= UINT64_MAX;
1403 return;
1404 }
1405
1406 /* it's safe to call the following if bulk_insert isn't on */
1407 mi_flush_bulk_insert(file, getTable()->getShare()->next_number_index);
1408
1409 (void) extra(HA_EXTRA_KEYREAD);
1410 key_copy(key, getTable()->getInsertRecord(),
1411 &getTable()->key_info[getTable()->getShare()->next_number_index],
1412 getTable()->getShare()->next_number_key_offset);
1413 error= mi_rkey(file, getTable()->getUpdateRecord(), (int) getTable()->getShare()->next_number_index,
1414 key, make_prev_keypart_map(getTable()->getShare()->next_number_keypart),
1415 HA_READ_PREFIX_LAST);
1416 if (error)
1417 nr= 1;
1418 else
1419 {
1420 /* Get data from getUpdateRecord() */
1421 nr= ((uint64_t) getTable()->next_number_field->
1422 val_int_offset(getTable()->getShare()->rec_buff_length)+1);
1423 }
1424 extra(HA_EXTRA_NO_KEYREAD);
1425 *first_value= nr;
1426 /*
1427 MySQL needs to call us for next row: assume we are inserting ("a",null)
1428 here, we return 3, and next this statement will want to insert ("b",null):
1429 there is no reason why ("b",3+1) would be the good row to insert: maybe it
1430 already exists, maybe 3+1 is too large...
1431 */
1432 *nb_reserved_values= 1;
1433}
1434
1435
1436/*
1437 Find out how many rows there is in the given range
1438
1439 SYNOPSIS
1440 records_in_range()
1441 inx Index to use
1442 min_key Start of range. Null pointer if from first key
1443 max_key End of range. Null pointer if to last key
1444
1445 NOTES
1446 min_key.flag can have one of the following values:
1447 HA_READ_KEY_EXACT Include the key in the range
1448 HA_READ_AFTER_KEY Don't include key in range
1449
1450 max_key.flag can have one of the following values:
1451 HA_READ_BEFORE_KEY Don't include key in range
1452 HA_READ_AFTER_KEY Include all 'end_key' values in the range
1453
1454 RETURN
1455 HA_POS_ERROR Something is wrong with the index tree.
1456 0 There is no matching keys in the given range
1457 number > 0 There is approximately 'number' matching rows in
1458 the range.
1459*/
1460
1461ha_rows ha_myisam::records_in_range(uint32_t inx, key_range *min_key,
1462 key_range *max_key)
1463{
1464 return (ha_rows) mi_records_in_range(file, (int) inx, min_key, max_key);
1465}
1466
1467
1468uint32_t ha_myisam::checksum() const
1469{
1470 return (uint)file->state->checksum;
1471}
1472
1473static int myisam_init(module::Context &context)
1474{
1475 context.add(new MyisamEngine(engine_name));
1476 context.registerVariable(new sys_var_constrained_value<size_t>("sort-buffer-size",
1477 sort_buffer_size));
1478 context.registerVariable(new sys_var_uint64_t_ptr("max_sort_file_size",
1479 &max_sort_file_size,
1480 context.getOptions()["max-sort-file-size"].as<uint64_t>()));
1481
1482 return 0;
1483}
1484
1485
1486static void init_options(drizzled::module::option_context &context)
1487{
1488 context("max-sort-file-size",
1489 po::value<uint64_t>(&max_sort_file_size)->default_value(INT32_MAX),
1490 _("Don't use the fast sort index method to created index if the temporary file would get bigger than this."));
1491 context("sort-buffer-size",
1492 po::value<sort_buffer_constraint>(&sort_buffer_size)->default_value(8192*1024),
1493 _("The buffer that is allocated when sorting the index when doing a REPAIR or when creating indexes with CREATE INDEX or ALTER TABLE."));
1494}
1495
1496
1497DRIZZLE_DECLARE_PLUGIN
1498{
1499 DRIZZLE_VERSION_ID,
1500 "myisam",
1501 "2.0",
1502 "MySQL AB",
1503 N_("MyISAM storage engine: non-transactional, legacy, deprecated"),
1504 PLUGIN_LICENSE_GPL,
1505 myisam_init,
1506 NULL,
1507 init_options
1508}
1509DRIZZLE_DECLARE_PLUGIN_END;
virtual int read_range_first(const key_range *start_key, const key_range *end_key, bool eq_range, bool sorted)
virtual int read_range_next()
unsigned char * null_ptr
Definition field.h:74
unsigned char null_bit
Definition field.h:154
void set_proc_info(const char *info)
Definition session.h:605
drizzle_system_variables & variables
Definition session.h:195
Field * found_next_number_field
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 read_range_first(const drizzled::key_range *start_key, const drizzled::key_range *end_key, bool eq_range_arg, bool sorted)
int doStartTableScan(bool scan)
int delete_all_rows(void)
int read_range_next()
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 reset_auto_increment(uint64_t value)
Definition ha_myisam.cc:350
int reset(void)
int external_lock(drizzled::Session *session, int lock_type)
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...
Convert from decimal to binary representation.
Definition common_fwd.h:215
TODO: Rename this file - func.h is stupid.
STL namespace.
An Proxy Wrapper around boost::program_options::variables_map.