28#include <drizzled/error.h>
29#include <drizzled/field/epoch.h>
30#include <drizzled/gettext.h>
31#include <drizzled/internal/my_sys.h>
32#include <drizzled/item/empty_string.h>
33#include <drizzled/item/int.h>
34#include <drizzled/lock.h>
35#include <drizzled/message/table.h>
36#include <drizzled/optimizer/cost_vector.h>
37#include <drizzled/plugin/client.h>
38#include <drizzled/plugin/event_observer.h>
39#include <drizzled/plugin/storage_engine.h>
40#include <drizzled/probes.h>
41#include <drizzled/session.h>
42#include <drizzled/sql_base.h>
43#include <drizzled/sql_parse.h>
44#include <drizzled/transaction_services.h>
45#include <drizzled/key.h>
46#include <drizzled/sql_lex.h>
47#include <drizzled/resource_context.h>
48#include <drizzled/statistics_variables.h>
49#include <drizzled/system_variables.h>
50#include "drizzled/probes.h"
51#include <drizzled/key_part_info.h>
64 estimation_rows_to_insert(0),
66 key_used_on_scan(MAX_KEY), active_index(MAX_KEY),
67 ref_length(sizeof(internal::my_off_t)),
70 next_insert_id(0), insert_id_for_cur_row(0)
85Cursor *Cursor::clone(memory::Root *mem_root)
87 Cursor *new_handler= getTable()->getMutableShare()->db_type()->getCursor(*getTable());
93 new_handler->ref= mem_root->alloc(ALIGN_SIZE(ref_length)*2);
94 identifier::Table identifier(*getTable());
95 return new_handler->ha_open(identifier, getTable()->getDBStat(), HA_OPEN_IGNORE_IF_LOCKED) ? NULL : new_handler;
103uint32_t Cursor::calculate_key_len(uint32_t key_position, key_part_map keypart_map_arg)
106 assert(((keypart_map_arg + 1) & keypart_map_arg) == 0);
108 const KeyPartInfo *key_part_found= getTable()->getShare()->getKeyInfo(key_position).key_part;
109 const KeyPartInfo *end_key_part_found= key_part_found + getTable()->getShare()->getKeyInfo(key_position).key_parts;
112 while (key_part_found < end_key_part_found && keypart_map_arg)
114 length+= key_part_found->store_length;
115 keypart_map_arg >>= 1;
121int Cursor::startIndexScan(uint32_t idx,
bool sorted)
124 assert(inited == NONE);
125 if (!(result= doStartIndexScan(idx, sorted)))
131int Cursor::endIndexScan()
133 assert(inited==INDEX);
136 return(doEndIndexScan());
139int Cursor::startTableScan(
bool scan)
142 assert(inited==NONE || (inited==RND && scan));
143 inited= (result= doStartTableScan(scan)) ? NONE: RND;
148int Cursor::endTableScan()
152 return(doEndTableScan());
155int Cursor::ha_index_or_rnd_end()
157 return inited == INDEX ? endIndexScan() : inited == RND ? endTableScan() : 0;
160void Cursor::ha_start_bulk_insert(ha_rows rows)
162 estimation_rows_to_insert= rows;
163 start_bulk_insert(rows);
166int Cursor::ha_end_bulk_insert()
168 estimation_rows_to_insert= 0;
169 return end_bulk_insert();
172const key_map *Cursor::keys_to_use_for_scanning()
174 return &key_map_empty;
177bool Cursor::has_transactions()
179 return (getEngine()->participatesInSqlTransaction());
182void Cursor::ha_statistic_increment(uint64_t system_status_var::*offset)
const
184 (getTable()->in_use->status_var.*offset)++;
187void **Cursor::ha_data(Session *session)
const
189 return session->getEngineData(getEngine());
195 ((flags & HA_CHECK_DUP_KEY) &&
196 (error == HA_ERR_FOUND_DUPP_KEY ||
197 error == HA_ERR_FOUND_DUPP_UNIQUE)))
204uint64_t Cursor::tableSize() {
return stats.index_file_length + stats.data_file_length; }
205uint64_t Cursor::rowSize() {
return getTable()->getRecordLength() + getTable()->sizeFields(); }
207int Cursor::doOpen(
const identifier::Table &identifier,
int mode, uint32_t test_if_locked)
209 return open(identifier.getPath().c_str(), mode, test_if_locked);
224 if ((error= doOpen(identifier, mode, test_if_locked)))
226 if ((error == EACCES || error == EROFS) && mode == O_RDWR &&
227 (getTable()->db_stat & HA_TRY_READ_ONLY))
229 getTable()->db_stat|=HA_READ_ONLY;
230 error= doOpen(identifier, O_RDONLY,test_if_locked);
239 if (getTable()->getShare()->db_options_in_use & HA_OPTION_READ_ONLY_DATA)
240 getTable()->db_stat|=HA_READ_ONLY;
241 (void) extra(HA_EXTRA_NO_READCHECK);
245 ref= getTable()->alloc(ALIGN_SIZE(
ref_length)*2);
261 ha_statistic_increment(&system_status_var::ha_read_first_count);
268 if (stats.deleted < 10 || primary_key >= MAX_KEY ||
269 !(getTable()->index_flags(primary_key) & HA_READ_ORDER))
271 error= startTableScan(1);
274 while ((error= rnd_next(buf)) == HA_ERR_RECORD_DELETED) ;
275 (void) endTableScan();
281 error= startIndexScan(primary_key, 0);
284 error=index_first(buf);
285 (void) endIndexScan();
305 if (variables->auto_increment_increment == 1)
307 nr= (((nr+ variables->auto_increment_increment -
308 variables->auto_increment_offset)) /
309 (uint64_t) variables->auto_increment_increment);
310 return (nr* (uint64_t) variables->auto_increment_increment +
311 variables->auto_increment_offset);
315void Cursor::adjust_next_insert_id_after_explicit_value(uint64_t nr)
322 if ((next_insert_id > 0) && (nr >= next_insert_id))
323 set_next_insert_id(compute_next_insert_id(nr, &getTable()->in_use->variables));
345 if (DRIZZLE_UNLIKELY(nr < variables->auto_increment_offset))
354 if (variables->auto_increment_increment == 1)
356 nr= (((nr - variables->auto_increment_offset)) /
357 (uint64_t) variables->auto_increment_increment);
358 return (nr * (uint64_t) variables->auto_increment_increment +
359 variables->auto_increment_offset);
424#define AUTO_INC_DEFAULT_NB_ROWS 1
425#define AUTO_INC_DEFAULT_NB_MAX_BITS 16
426#define AUTO_INC_DEFAULT_NB_MAX ((1 << AUTO_INC_DEFAULT_NB_MAX_BITS) - 1)
428int Cursor::update_auto_increment()
430 uint64_t nr, nb_reserved_values;
432 Session *session= getTable()->in_use;
433 drizzle_system_variables *variables= &session->variables;
439 assert(next_insert_id >= auto_inc_interval_for_cur_row.minimum());
445 if ((nr= getTable()->next_number_field->val_int()) != 0
446 || getTable()->auto_increment_field_not_null)
454 adjust_next_insert_id_after_explicit_value(nr);
455 insert_id_for_cur_row= 0;
460 if ((nr= next_insert_id) >= auto_inc_interval_for_cur_row.maximum())
467 uint32_t nb_already_reserved_intervals= 0;
468 uint64_t nb_desired_values;
480 if (nb_already_reserved_intervals == 0 &&
481 (estimation_rows_to_insert > 0))
482 nb_desired_values= estimation_rows_to_insert;
486 if (nb_already_reserved_intervals <= AUTO_INC_DEFAULT_NB_MAX_BITS)
488 nb_desired_values= AUTO_INC_DEFAULT_NB_ROWS *
489 (1 << nb_already_reserved_intervals);
490 set_if_smaller(nb_desired_values, (uint64_t)AUTO_INC_DEFAULT_NB_MAX);
493 nb_desired_values= AUTO_INC_DEFAULT_NB_MAX;
496 get_auto_increment(variables->auto_increment_offset,
497 variables->auto_increment_increment,
498 nb_desired_values, &nr,
499 &nb_reserved_values);
500 if (nr == ~(uint64_t) 0)
501 return HA_ERR_AUTOINC_READ_FAILED;
514 if (getTable()->getShare()->next_number_keypart == 0)
521 if (DRIZZLE_UNLIKELY(getTable()->next_number_field->store((int64_t) nr,
true)))
526 if (session->getKilled() == Session::KILL_BAD_DATA)
527 return HA_ERR_AUTOINC_ERANGE;
537 nr=
prev_insert_id(getTable()->next_number_field->val_int(), variables);
538 if (DRIZZLE_UNLIKELY(getTable()->next_number_field->store((int64_t) nr,
true)))
539 nr= getTable()->next_number_field->val_int();
542 auto_inc_interval_for_cur_row.replace(nr, nb_reserved_values, variables->auto_increment_increment);
551 insert_id_for_cur_row= nr;
580 release_auto_increment();
586void Cursor::drop_table()
591int Cursor::ha_check(Session*)
610 if (not getTable()->in_use)
621 ResourceContext& resource_context= getTable()->in_use->getResourceContext(*getEngine());
652 Session& session= *getTable()->in_use;
686 return analyze(session);
700 return disable_indexes(mode);
715 return enable_indexes(mode);
728 return discard_or_import_tablespace(discard);
743int Cursor::index_next_same(
unsigned char *buf,
const unsigned char *key, uint32_t keylen)
745 int error= index_next(buf);
749 ptrdiff_t ptrdiff= buf - getTable()->getInsertRecord();
750 unsigned char *save_record_0= NULL;
765 save_record_0= getTable()->getInsertRecord();
766 getTable()->record[0]= buf;
767 key_info= getTable()->key_info + active_index;
768 key_part= key_info->key_part;
769 key_part_end= key_part + key_info->key_parts;
770 for (; key_part < key_part_end; key_part++)
772 assert(key_part->field);
773 key_part->field->move_field_offset(ptrdiff);
779 getTable()->status=STATUS_NOT_FOUND;
780 error= HA_ERR_END_OF_FILE;
786 getTable()->record[0]= save_record_0;
787 for (key_part= key_info->key_part; key_part < key_part_end; key_part++)
788 key_part->field->move_field_offset(-ptrdiff);
821 uint32_t keys_per_block= (stats.block_size/2/
822 (getTable()->key_info[keynr].key_length +
ref_length) + 1);
823 return ((
double) (key_records + keys_per_block-1) /
824 (
double) keys_per_block);
865 void *seq_init_param,
871 ha_rows rows, total_rows= 0;
877 seq_it= seq->init(seq_init_param, n_ranges, *flags);
878 while (!seq->next(seq_it, &range))
883 min_endp= range.start_key.length? &range.start_key : NULL;
884 max_endp= range.end_key.length? &range.end_key : NULL;
886 if ((range.range_flag & UNIQUE_RANGE) && !(range.range_flag & NULL_RANGE))
890 if (HA_POS_ERROR == (rows= this->records_in_range(keyno, min_endp,
894 total_rows= HA_POS_ERROR;
901 if (total_rows != HA_POS_ERROR)
904 *flags |= HA_MRR_USE_DEFAULT_IMPL;
906 cost->setAvgIOCost(1);
907 if ((*flags & HA_MRR_INDEX_ONLY) && total_rows > 2)
910 cost->setIOCount(read_time(keyno, n_ranges, total_rows));
956 *flags |= HA_MRR_USE_DEFAULT_IMPL;
959 cost->setAvgIOCost(1);
962 if (*flags & HA_MRR_INDEX_ONLY)
965 cost->setIOCount(read_time(keyno, n_ranges, n_rows));
1013 uint32_t n_ranges, uint32_t mode)
1015 mrr_iter= seq_funcs->init(seq_init_param, n_ranges, mode);
1016 mrr_funcs= *seq_funcs;
1017 mrr_is_output_sorted= test(mode & HA_MRR_SORTED);
1055 if (result != HA_ERR_END_OF_FILE)
1066 result= HA_ERR_END_OF_FILE;
1079 mrr_is_output_sorted);
1080 if (result != HA_ERR_END_OF_FILE)
1084 while ((result == HA_ERR_END_OF_FILE) && !range_res);
1120 eq_range= eq_range_arg;
1126 key_compare_result_on_equal= ((end_key->flag == HA_READ_BEFORE_KEY) ? 1 :
1127 (end_key->flag == HA_READ_AFTER_KEY) ? -1 : 0);
1129 range_key_part= getTable()->key_info[active_index].key_part;
1132 result= index_first(getTable()->getInsertRecord());
1136 start_key->keypart_map,
1139 return((result == HA_ERR_KEY_NOT_FOUND)
1140 ? HA_ERR_END_OF_FILE
1143 return (
compare_key(end_range) <= 0 ? 0 : HA_ERR_END_OF_FILE);
1167 return(index_next_same(getTable()->getInsertRecord(),
1169 end_range->length));
1171 result= index_next(getTable()->getInsertRecord());
1174 return(
compare_key(end_range) <= 0 ? 0 : HA_ERR_END_OF_FILE);
1198 cmp=
key_cmp(range_key_part, range->key, range->length);
1200 cmp= key_compare_result_on_equal;
1205 const unsigned char * key,
1206 key_part_map keypart_map,
1207 enum ha_rkey_function find_flag)
1210 error= doStartIndexScan(index, 0);
1214 error1= doEndIndexScan();
1216 return error ? error : error1;
1219static bool log_row_for_replication(
Table* table,
1220 const unsigned char *before_record,
1221 const unsigned char *after_record)
1223 Session *
const session= table->in_use;
1230 switch (session->lex().sql_command)
1232 case SQLCOM_CREATE_TABLE:
1244 case SQLCOM_REPLACE:
1245 case SQLCOM_REPLACE_SELECT:
1264 if (after_record == NULL)
1281 if (before_record == NULL)
1282 result= TransactionServices::insertRecord(*session, *table);
1284 TransactionServices::updateRecord(*session, *table, before_record, after_record);
1288 case SQLCOM_INSERT_SELECT:
1296 if (before_record == NULL)
1297 result= TransactionServices::insertRecord(*session, *table);
1299 TransactionServices::updateRecord(*session, *table, before_record, after_record);
1303 TransactionServices::updateRecord(*session, *table, before_record, after_record);
1307 TransactionServices::deleteRecord(*session, *table);
1325 if (DRIZZLE_CURSOR_RDLOCK_START_ENABLED() ||
1326 DRIZZLE_CURSOR_WRLOCK_START_ENABLED() ||
1327 DRIZZLE_CURSOR_UNLOCK_START_ENABLED())
1329 if (lock_type == F_RDLCK)
1331 DRIZZLE_CURSOR_RDLOCK_START(getTable()->getShare()->getSchemaName(),
1332 getTable()->getShare()->getTableName());
1334 else if (lock_type == F_WRLCK)
1336 DRIZZLE_CURSOR_WRLOCK_START(getTable()->getShare()->getSchemaName(),
1337 getTable()->getShare()->getTableName());
1339 else if (lock_type == F_UNLCK)
1341 DRIZZLE_CURSOR_UNLOCK_START(getTable()->getShare()->getSchemaName(),
1342 getTable()->getShare()->getTableName());
1353 if (DRIZZLE_CURSOR_RDLOCK_DONE_ENABLED() ||
1354 DRIZZLE_CURSOR_WRLOCK_DONE_ENABLED() ||
1355 DRIZZLE_CURSOR_UNLOCK_DONE_ENABLED())
1357 if (lock_type == F_RDLCK)
1359 DRIZZLE_CURSOR_RDLOCK_DONE(error);
1361 else if (lock_type == F_WRLCK)
1363 DRIZZLE_CURSOR_WRLOCK_DONE(error);
1365 else if (lock_type == F_UNLCK)
1367 DRIZZLE_CURSOR_UNLOCK_DONE(error);
1381 assert(! getTable()->getShare()->all_set.none());
1382 assert(getTable()->key_read == 0);
1384 assert(inited == NONE);
1386 getTable()->free_io_cache();
1388 getTable()->default_column_bitmaps();
1393int Cursor::insertRecord(
unsigned char *buf)
1403 if (getTable()->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_INSERT)
1405 getTable()->timestamp_field->set_time();
1408 DRIZZLE_INSERT_ROW_START(getTable()->getShare()->getSchemaName(), getTable()->getShare()->getTableName());
1409 setTransactionReadWrite();
1411 if (DRIZZLE_UNLIKELY(plugin::EventObserver::beforeInsertRecord(*getTable(), buf)))
1413 error= ER_EVENT_OBSERVER_PLUGIN;
1417 error= doInsertRecord(buf);
1418 if (DRIZZLE_UNLIKELY(plugin::EventObserver::afterInsertRecord(*getTable(), buf, error)))
1420 error= ER_EVENT_OBSERVER_PLUGIN;
1424 ha_statistic_increment(&system_status_var::ha_write_count);
1426 DRIZZLE_INSERT_ROW_DONE(error);
1428 if (DRIZZLE_UNLIKELY(error))
1433 if (DRIZZLE_UNLIKELY(log_row_for_replication(getTable(), NULL, buf)))
1434 return HA_ERR_LOG_ROW_FOR_REPLICATION_FAILED;
1440int Cursor::updateRecord(
const unsigned char *old_data,
unsigned char *new_data)
1448 assert(new_data == getTable()->getInsertRecord());
1450 DRIZZLE_UPDATE_ROW_START(getTable()->getShare()->getSchemaName(), getTable()->getShare()->getTableName());
1451 setTransactionReadWrite();
1452 if (DRIZZLE_UNLIKELY(plugin::EventObserver::beforeUpdateRecord(*getTable(), old_data, new_data)))
1454 error= ER_EVENT_OBSERVER_PLUGIN;
1458 if (getTable()->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_UPDATE)
1460 getTable()->timestamp_field->set_time();
1463 error= doUpdateRecord(old_data, new_data);
1464 if (DRIZZLE_UNLIKELY(plugin::EventObserver::afterUpdateRecord(*getTable(), old_data, new_data, error)))
1466 error= ER_EVENT_OBSERVER_PLUGIN;
1470 ha_statistic_increment(&system_status_var::ha_update_count);
1472 DRIZZLE_UPDATE_ROW_DONE(error);
1474 if (DRIZZLE_UNLIKELY(error))
1479 if (DRIZZLE_UNLIKELY(log_row_for_replication(getTable(), old_data, new_data)))
1480 return HA_ERR_LOG_ROW_FOR_REPLICATION_FAILED;
1484TableShare *Cursor::getShare()
1486 return getTable()->getMutableShare();
1489int Cursor::deleteRecord(
const unsigned char *buf)
1493 DRIZZLE_DELETE_ROW_START(getTable()->getShare()->getSchemaName(), getTable()->getShare()->getTableName());
1494 setTransactionReadWrite();
1495 if (DRIZZLE_UNLIKELY(plugin::EventObserver::beforeDeleteRecord(*getTable(), buf)))
1497 error= ER_EVENT_OBSERVER_PLUGIN;
1501 error= doDeleteRecord(buf);
1502 if (DRIZZLE_UNLIKELY(plugin::EventObserver::afterDeleteRecord(*getTable(), buf, error)))
1504 error= ER_EVENT_OBSERVER_PLUGIN;
1508 ha_statistic_increment(&system_status_var::ha_delete_count);
1510 DRIZZLE_DELETE_ROW_DONE(error);
1512 if (DRIZZLE_UNLIKELY(error))
1515 if (DRIZZLE_UNLIKELY(log_row_for_replication(getTable(), buf, NULL)))
1516 return HA_ERR_LOG_ROW_FOR_REPLICATION_FAILED;
virtual int multi_range_read_init(RANGE_SEQ_IF *seq, void *seq_init_param, uint32_t n_ranges, uint32_t mode)
KEY_MULTI_RANGE mrr_cur_range
virtual bool is_fatal_error(int error, uint32_t flags)
virtual ha_rows records()
uint64_t insert_id_for_cur_row
int ha_enable_indexes(uint32_t mode)
int compare_key(key_range *range)
virtual int read_range_first(const key_range *start_key, const key_range *end_key, bool eq_range, bool sorted)
void ha_release_auto_increment()
int ha_open(const identifier::Table &identifier, int mode, int test_if_locked)
void setTransactionReadWrite()
virtual double index_only_read_time(uint32_t keynr, double records)
virtual int delete_all_rows(void)
virtual int index_read_map(unsigned char *buf, const unsigned char *key, key_part_map keypart_map, enum ha_rkey_function find_flag)
Positions an index cursor to the index specified in the handle. Fetches the row if available....
void closeMarkForDelete()
virtual bool was_semi_consistent_read()
int ha_reset_auto_increment(uint64_t value)
int ha_external_lock(Session *session, int lock_type)
int ha_discard_or_import_tablespace(bool discard)
int ha_disable_indexes(uint32_t mode)
virtual ha_rows multi_range_read_info_const(uint32_t keyno, RANGE_SEQ_IF *seq, void *seq_init_param, uint32_t n_ranges, uint32_t *bufsz, uint32_t *flags, optimizer::CostVector *cost)
virtual int multi_range_read_info(uint32_t keyno, uint32_t n_ranges, uint32_t keys, uint32_t *bufsz, uint32_t *flags, optimizer::CostVector *cost)
virtual int reset_auto_increment(uint64_t)
virtual int external_lock(Session *, int)
virtual int index_read_idx_map(unsigned char *buf, uint32_t index, const unsigned char *key, key_part_map keypart_map, enum ha_rkey_function find_flag)
Positions an index cursor to the index specified in the handle. Fetches the row if available....
virtual int read_first_row(unsigned char *buf, uint32_t primary_key)
Discrete_interval auto_inc_interval_for_cur_row
int ha_analyze(Session *)
virtual int read_range_next()
virtual int multi_range_read_next(char **range_info)
static bool insertRecord(Session &, Table &in_table)
static bool shouldConstructMessages()
static void finalizeStatementMessage(message::Statement &, Session &)
static void deleteRecord(Session &, Table &, bool use_update_record=false)
static void truncateTable(Session &, Table &)
TODO: Rename this file - func.h is stupid.
uint64_t compute_next_insert_id(uint64_t nr, drizzle_system_variables *variables)
uint64_t prev_insert_id(uint64_t nr, drizzle_system_variables *variables)
int key_cmp(KeyPartInfo *key_part, const unsigned char *key, uint32_t key_length)
bool key_cmp_if_same(Table *table, const unsigned char *key, uint32_t idx, uint32_t key_length)