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 */
28
28
#include "drizzled/errmsg_print.h"
29
29
#include "drizzled/gettext.h"
30
30
#include "drizzled/session.h"
31
#include "drizzled/plugin.h"
31
#include "drizzled/set_var.h"
32
#include <drizzled/plugin.h>
32
33
#include "drizzled/plugin/client.h"
33
34
#include "drizzled/table.h"
35
#include "drizzled/field/timestamp.h"
34
36
#include "drizzled/memory/multi_malloc.h"
35
37
#include "drizzled/plugin/daemon.h"
37
#include <drizzled/plugin/storage_engine.h>
39
39
#include <boost/algorithm/string.hpp>
40
#include <boost/scoped_ptr.hpp>
45
44
#include <algorithm>
47
45
#include <boost/program_options.hpp>
48
46
#include <drizzled/module/option_map.h>
61
59
static uint32_t myisam_key_cache_division_limit;
62
60
static uint32_t myisam_key_cache_age_threshold;
63
61
static uint64_t max_sort_file_size;
64
typedef constrained_check<size_t, SIZE_MAX, 1024, 1024> sort_buffer_constraint;
65
static sort_buffer_constraint sort_buffer_size;
62
static uint64_t sort_buffer_size;
67
64
void st_mi_isam_share::setKeyCache()
119
116
int doCreateTable(Session&,
120
117
Table& table_arg,
121
const identifier::Table &identifier,
118
const TableIdentifier &identifier,
122
119
message::Table&);
124
int doRenameTable(Session&, const identifier::Table &from, const identifier::Table &to);
121
int doRenameTable(Session&, const TableIdentifier &from, const TableIdentifier &to);
126
int doDropTable(Session&, const identifier::Table &identifier);
123
int doDropTable(Session&, const TableIdentifier &identifier);
128
125
int doGetTableDefinition(Session& session,
129
const identifier::Table &identifier,
126
const TableIdentifier &identifier,
130
127
message::Table &table_message);
132
129
uint32_t max_supported_keys() const { return MI_MAX_KEY; }
142
139
HA_KEYREAD_ONLY);
144
bool doDoesTableExist(Session& session, const identifier::Table &identifier);
141
bool doDoesTableExist(Session& session, const TableIdentifier &identifier);
146
143
void doGetTableIdentifiers(drizzled::CachedDirectory &directory,
147
const drizzled::identifier::Schema &schema_identifier,
148
drizzled::identifier::Table::vector &set_of_identifiers);
144
const drizzled::SchemaIdentifier &schema_identifier,
145
drizzled::TableIdentifiers &set_of_identifiers);
149
146
bool validateCreateTableOption(const std::string &key, const std::string &state)
161
158
void MyisamEngine::doGetTableIdentifiers(drizzled::CachedDirectory&,
162
const drizzled::identifier::Schema&,
163
drizzled::identifier::Table::vector&)
159
const drizzled::SchemaIdentifier&,
160
drizzled::TableIdentifiers&)
167
bool MyisamEngine::doDoesTableExist(Session &session, const identifier::Table &identifier)
164
bool MyisamEngine::doDoesTableExist(Session &session, const TableIdentifier &identifier)
169
return session.getMessageCache().doesTableMessageExist(identifier);
166
return session.doesTableMessageExist(identifier);
172
169
int MyisamEngine::doGetTableDefinition(Session &session,
173
const identifier::Table &identifier,
170
const TableIdentifier &identifier,
174
171
message::Table &table_message)
176
if (session.getMessageCache().getTableMessage(identifier, table_message))
173
if (session.getTableMessage(identifier, table_message))
482
477
volatile int *killed_ptr(MI_CHECK *param)
484
479
/* In theory Unsafe conversion, but should be ok for now */
485
return (int*) (((Session *)(param->session))->getKilledPtr());
480
return (int*) &(((Session *)(param->session))->killed);
488
483
void mi_check_print_error(MI_CHECK *param, const char *fmt,...)
534
529
Session *cur_session;
535
530
if ((cur_session= file->in_use))
537
errmsg_printf(error::ERROR, _("Got an error from thread_id=%"PRIu64", %s:%d"),
531
errmsg_printf(ERRMSG_LVL_ERROR, _("Got an error from thread_id=%"PRIu64", %s:%d"),
538
532
cur_session->thread_id,
543
errmsg_printf(error::ERROR, _("Got an error from unknown thread, %s:%d"), sfile, sline);
535
errmsg_printf(ERRMSG_LVL_ERROR, _("Got an error from unknown thread, %s:%d"), sfile, sline);
547
errmsg_printf(error::ERROR, "%s", message);
537
errmsg_printf(ERRMSG_LVL_ERROR, "%s", message);
549
538
list<Session *>::iterator it= file->s->in_use->begin();
550
539
while (it != file->s->in_use->end())
552
errmsg_printf(error::ERROR, "%s", _("Unknown thread accessing table"));
541
errmsg_printf(ERRMSG_LVL_ERROR, "%s", _("Unknown thread accessing table"));
557
546
ha_myisam::ha_myisam(plugin::StorageEngine &engine_arg,
547
TableShare &table_arg)
559
548
: Cursor(engine_arg, table_arg),
561
550
can_enable_indexes(true),
601
590
if (!(file= mi_open(identifier, mode, test_if_locked)))
602
591
return (errno ? errno : -1);
604
if (!getTable()->getShare()->getType()) /* No need to perform a check for tmp table */
593
if (!table->getShare()->getType()) /* No need to perform a check for tmp table */
606
if ((errno= table2myisam(getTable(), &keyinfo, &recinfo, &recs)))
595
if ((errno= table2myisam(table, &keyinfo, &recinfo, &recs)))
610
if (check_definition(keyinfo, recinfo, getTable()->getShare()->sizeKeys(), recs,
599
if (check_definition(keyinfo, recinfo, table->getShare()->sizeKeys(), recs,
611
600
file->s->keyinfo, file->s->rec,
612
601
file->s->base.keys, file->s->base.fields, true))
623
612
info(HA_STATUS_NO_LOCK | HA_STATUS_VARIABLE | HA_STATUS_CONST);
624
613
if (!(test_if_locked & HA_OPEN_WAIT_IF_LOCKED))
625
614
mi_extra(file, HA_EXTRA_WAIT_LOCK, 0);
626
if (!getTable()->getShare()->db_record_offset)
615
if (!table->getShare()->db_record_offset)
627
616
is_ordered= false;
630
619
keys_with_parts.reset();
631
for (i= 0; i < getTable()->getShare()->sizeKeys(); i++)
620
for (i= 0; i < table->getShare()->sizeKeys(); i++)
633
getTable()->key_info[i].block_size= file->s->keyinfo[i].block_length;
622
table->key_info[i].block_size= file->s->keyinfo[i].block_length;
635
KeyPartInfo *kp= getTable()->key_info[i].key_part;
636
KeyPartInfo *kp_end= kp + getTable()->key_info[i].key_parts;
624
KeyPartInfo *kp= table->key_info[i].key_part;
625
KeyPartInfo *kp_end= kp + table->key_info[i].key_parts;
637
626
for (; kp != kp_end; kp++)
639
628
if (!kp->field->part_of_key.test(i))
701
690
if (file->dfile == -1)
703
errmsg_printf(error::INFO, "Retrying repair of: '%s' failed. "
704
"Please try REPAIR EXTENDED or myisamchk",
705
getTable()->getShare()->getPath());
692
errmsg_printf(ERRMSG_LVL_INFO, "Retrying repair of: '%s' failed. "
693
"Please try REPAIR EXTENDED or myisamchk",
694
table->getShare()->getPath());
706
695
return(HA_ADMIN_FAILED);
709
param.db_name= getTable()->getShare()->getSchemaName();
710
param.table_name= getTable()->getAlias();
698
param.db_name= table->getShare()->getSchemaName();
699
param.table_name= table->getAlias();
711
700
param.tmpfile_createflag = O_RDWR | O_TRUNC;
712
701
param.using_global_keycache = 1;
713
702
param.session= session;
714
703
param.out_flag= 0;
715
param.sort_buffer_length= static_cast<size_t>(sort_buffer_size);
704
param.sort_buffer_length= (size_t)sort_buffer_size;
716
705
strcpy(fixed_name,file->filename);
718
707
// Don't lock tables if we have used LOCK Table
719
if (mi_lock_database(file, getTable()->getShare()->getType() ? F_EXTRA_LCK : F_WRLCK))
708
if (mi_lock_database(file, table->getShare()->getType() ? F_EXTRA_LCK : F_WRLCK))
721
710
mi_check_print_error(¶m,ER(ER_CANT_LOCK),errno);
722
711
return(HA_ADMIN_FAILED);
914
903
else if (mode == HA_KEY_SWITCH_NONUNIQ_SAVE)
916
Session *session= getTable()->in_use;
917
boost::scoped_ptr<MI_CHECK> param_ap(new MI_CHECK);
918
MI_CHECK ¶m= *param_ap.get();
905
Session *session= table->in_use;
919
907
const char *save_proc_info= session->get_proc_info();
920
908
session->set_proc_info("Creating index");
921
909
myisamchk_init(¶m);
923
911
param.testflag= (T_SILENT | T_REP_BY_SORT | T_QUICK |
924
912
T_CREATE_MISSING_KEYS);
925
913
param.myf_rw&= ~MY_WAIT_IF_FULL;
926
param.sort_buffer_length= static_cast<size_t>(sort_buffer_size);
914
param.sort_buffer_length= (size_t)sort_buffer_size;
927
915
param.stats_method= MI_STATS_METHOD_NULLS_NOT_EQUAL;
928
916
if ((error= (repair(session,param,0) != HA_ADMIN_OK)) && param.retry_repair)
930
errmsg_printf(error::WARN, "Warning: Enabling keys got errno %d on %s.%s, retrying",
918
errmsg_printf(ERRMSG_LVL_WARN, "Warning: Enabling keys got errno %d on %s.%s, retrying",
931
919
errno, param.db_name, param.table_name);
932
920
/* Repairing by sort failed. Now try standard repair method. */
933
921
param.testflag&= ~(T_REP_BY_SORT | T_QUICK);
1282
1270
share->keys_for_keyread&= share->keys_in_use;
1283
1271
share->db_record_offset= misam_info.record_offset;
1284
1272
if (share->key_parts)
1285
memcpy(getTable()->key_info[0].rec_per_key,
1273
memcpy(table->key_info[0].rec_per_key,
1286
1274
misam_info.rec_per_key,
1287
sizeof(getTable()->key_info[0].rec_per_key)*share->key_parts);
1275
sizeof(table->key_info[0].rec_per_key)*share->key_parts);
1288
1276
assert(share->getType() != message::Table::STANDARD);
1340
1328
int MyisamEngine::doDropTable(Session &session,
1341
const identifier::Table &identifier)
1329
const TableIdentifier &identifier)
1343
session.getMessageCache().removeTableMessage(identifier);
1331
session.removeTableMessage(identifier);
1345
1333
return mi_delete_table(identifier.getPath().c_str());
1349
1337
int ha_myisam::external_lock(Session *session, int lock_type)
1351
1339
file->in_use= session;
1352
return mi_lock_database(file, !getTable()->getShare()->getType() ?
1340
return mi_lock_database(file, !table->getShare()->getType() ?
1353
1341
lock_type : ((lock_type == F_UNLCK) ?
1354
1342
F_UNLCK : F_EXTRA_LCK));
1357
1345
int MyisamEngine::doCreateTable(Session &session,
1358
1346
Table& table_arg,
1359
const identifier::Table &identifier,
1347
const TableIdentifier &identifier,
1360
1348
message::Table& create_proto)
1396
1384
&create_info, create_flags);
1397
1385
free((unsigned char*) recinfo);
1399
session.getMessageCache().storeTableMessage(identifier, create_proto);
1387
session.storeTableMessage(identifier, create_proto);
1405
int MyisamEngine::doRenameTable(Session &session, const identifier::Table &from, const identifier::Table &to)
1393
int MyisamEngine::doRenameTable(Session &session, const TableIdentifier &from, const TableIdentifier &to)
1407
session.getMessageCache().renameTableMessage(from, to);
1395
session.renameTableMessage(from, to);
1409
1397
return mi_rename(from.getPath().c_str(), to.getPath().c_str());
1432
1420
/* it's safe to call the following if bulk_insert isn't on */
1433
mi_flush_bulk_insert(file, getTable()->getShare()->next_number_index);
1421
mi_flush_bulk_insert(file, table->getShare()->next_number_index);
1435
1423
(void) extra(HA_EXTRA_KEYREAD);
1436
key_copy(key, getTable()->getInsertRecord(),
1437
&getTable()->key_info[getTable()->getShare()->next_number_index],
1438
getTable()->getShare()->next_number_key_offset);
1439
error= mi_rkey(file, getTable()->getUpdateRecord(), (int) getTable()->getShare()->next_number_index,
1440
key, make_prev_keypart_map(getTable()->getShare()->next_number_keypart),
1424
key_copy(key, table->getInsertRecord(),
1425
&table->key_info[table->getShare()->next_number_index],
1426
table->getShare()->next_number_key_offset);
1427
error= mi_rkey(file, table->getUpdateRecord(), (int) table->getShare()->next_number_index,
1428
key, make_prev_keypart_map(table->getShare()->next_number_keypart),
1441
1429
HA_READ_PREFIX_LAST);
1446
1434
/* Get data from getUpdateRecord() */
1447
nr= ((uint64_t) getTable()->next_number_field->
1448
val_int_offset(getTable()->getShare()->rec_buff_length)+1);
1435
nr= ((uint64_t) table->next_number_field->
1436
val_int_offset(table->getShare()->rec_buff_length)+1);
1450
1438
extra(HA_EXTRA_NO_KEYREAD);
1451
1439
*first_value= nr;
1496
1484
return (uint)file->state->checksum;
1487
static MyisamEngine *engine= NULL;
1499
1489
static int myisam_init(module::Context &context)
1501
context.add(new MyisamEngine(engine_name));
1502
context.registerVariable(new sys_var_constrained_value<size_t>("sort-buffer-size",
1504
context.registerVariable(new sys_var_uint64_t_ptr("max_sort_file_size",
1505
&max_sort_file_size,
1506
context.getOptions()["max-sort-file-size"].as<uint64_t>()));
1491
const module::option_map &vm= context.getOptions();
1493
if (vm.count("max-sort-file-size"))
1495
if (max_sort_file_size > UINT64_MAX)
1497
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for max-sort-file-size\n"));
1502
if (vm.count("sort-buffer-size"))
1504
if (sort_buffer_size < 1024 || sort_buffer_size > SIZE_MAX)
1506
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for sort-buffer-size\n"));
1511
engine= new MyisamEngine(engine_name);
1512
context.add(engine);
1518
static DRIZZLE_SYSVAR_ULONGLONG(max_sort_file_size, max_sort_file_size,
1519
PLUGIN_VAR_RQCMDARG,
1520
N_("Don't use the fast sort index method to created index if the temporary file would get bigger than this."),
1521
NULL, NULL, INT32_MAX, 0, UINT64_MAX, 0);
1523
static DRIZZLE_SYSVAR_ULONGLONG(sort_buffer_size, sort_buffer_size,
1524
PLUGIN_VAR_RQCMDARG,
1525
N_("The buffer that is allocated when sorting the index when doing a REPAIR or when creating indexes with CREATE INDEX or ALTER TABLE."),
1526
NULL, NULL, 8192*1024, 1024, SIZE_MAX, 0);
1512
1528
static void init_options(drizzled::module::option_context &context)
1514
1530
context("max-sort-file-size",
1515
1531
po::value<uint64_t>(&max_sort_file_size)->default_value(INT32_MAX),
1516
_("Don't use the fast sort index method to created index if the temporary file would get bigger than this."));
1532
N_("Don't use the fast sort index method to created index if the temporary file would get bigger than this."));
1517
1533
context("sort-buffer-size",
1518
po::value<sort_buffer_constraint>(&sort_buffer_size)->default_value(8192*1024),
1519
_("The buffer that is allocated when sorting the index when doing a REPAIR or when creating indexes with CREATE INDEX or ALTER TABLE."));
1534
po::value<uint64_t>(&sort_buffer_size)->default_value(8192*1024),
1535
N_("The buffer that is allocated when sorting the index when doing a REPAIR or when creating indexes with CREATE INDEX or ALTER TABLE."));
1538
static drizzle_sys_var* sys_variables[]= {
1539
DRIZZLE_SYSVAR(max_sort_file_size),
1540
DRIZZLE_SYSVAR(sort_buffer_size),
1523
1545
DRIZZLE_DECLARE_PLUGIN