92
92
SETMSG(HA_ERR_WRONG_INDEX, "Wrong index given to function");
93
93
SETMSG(HA_ERR_CRASHED, ER(ER_NOT_KEYFILE));
94
94
SETMSG(HA_ERR_WRONG_IN_RECORD, ER(ER_CRASHED_ON_USAGE));
95
SETMSG(HA_ERR_OUT_OF_MEM, "Table handler out of memory");
95
SETMSG(HA_ERR_OUT_OF_MEM, "Table Cursor out of memory");
96
96
SETMSG(HA_ERR_NOT_A_TABLE, "Incorrect file format '%.64s'");
97
97
SETMSG(HA_ERR_WRONG_COMMAND, "Command not supported");
98
98
SETMSG(HA_ERR_OLD_FILE, ER(ER_OLD_KEYFILE));
391
391
in it unless the engine says so. Thus, in order to be
392
392
a part of a transaction, the engine must "register" itself.
393
393
This is done by invoking trans_register_ha() server call.
394
Normally the engine registers itself whenever handler::external_lock()
394
Normally the engine registers itself whenever Cursor::external_lock()
395
395
is called. trans_register_ha() can be invoked many times: if
396
396
an engine is already registered, the call does nothing.
397
397
In case autocommit is not set, the engine must register itself
402
402
Note, that although the registration interface in itself is
403
403
fairly clear, the current usage practice often leads to undesired
404
404
effects. E.g. since a call to trans_register_ha() in most engines
405
is embedded into implementation of handler::external_lock(), some
405
is embedded into implementation of Cursor::external_lock(), some
406
406
DDL statements start a transaction (at least from the server
407
407
point of view) even though they are not expected to. E.g.
408
408
CREATE TABLE does not start a transaction, since
409
handler::external_lock() is never called during CREATE TABLE. But
410
CREATE TABLE ... SELECT does, since handler::external_lock() is
409
Cursor::external_lock() is never called during CREATE TABLE. But
410
CREATE TABLE ... SELECT does, since Cursor::external_lock() is
411
411
called for the table that is being selected from. This has no
412
412
practical effects currently, but must be kept in mind
418
418
During statement execution, whenever any of data-modifying
419
PSEA API methods is used, e.g. handler::write_row() or
420
handler::update_row(), the read-write flag is raised in the
419
PSEA API methods is used, e.g. Cursor::write_row() or
420
Cursor::update_row(), the read-write flag is raised in the
421
421
statement transaction for the involved engine.
422
422
Currently All PSEA calls are "traced", and the data can not be
423
423
changed in a way other than issuing a PSEA call. Important:
945
945
/****************************************************************************
946
** General handler functions
946
** General Cursor functions
947
947
****************************************************************************/
948
handler::~handler(void)
948
Cursor::~Cursor(void)
950
950
assert(locked == false);
951
951
/* TODO: assert(inited == NONE); */
955
handler *handler::clone(MEM_ROOT *mem_root)
955
Cursor *Cursor::clone(MEM_ROOT *mem_root)
957
handler *new_handler= plugin::StorageEngine::getNewHandler(table->s, mem_root, table->s->db_type());
957
Cursor *new_handler= table->s->db_type()->getCursor(table->s, mem_root);
959
Allocate handler->ref here because otherwise ha_open will allocate it
960
Allocate Cursor->ref here because otherwise ha_open will allocate it
960
961
on this->table->mem_root and we will not be able to reclaim that memory
961
when the clone handler object is destroyed.
962
when the clone Cursor object is destroyed.
963
964
if (!(new_handler->ref= (unsigned char*) alloc_root(mem_root, ALIGN_SIZE(ref_length)*2)))
1000
int handler::ha_rnd_end()
1001
int Cursor::ha_rnd_end()
1002
1003
assert(inited==RND);
1004
1005
return(rnd_end());
1007
int handler::ha_index_or_rnd_end()
1008
int Cursor::ha_index_or_rnd_end()
1009
1010
return inited == INDEX ? ha_index_end() : inited == RND ? ha_rnd_end() : 0;
1012
handler::Table_flags handler::ha_table_flags() const
1013
Cursor::Table_flags Cursor::ha_table_flags() const
1014
1015
return cached_table_flags;
1017
void handler::ha_start_bulk_insert(ha_rows rows)
1018
void Cursor::ha_start_bulk_insert(ha_rows rows)
1019
1020
estimation_rows_to_insert= rows;
1020
1021
start_bulk_insert(rows);
1023
int handler::ha_end_bulk_insert()
1024
int Cursor::ha_end_bulk_insert()
1025
1026
estimation_rows_to_insert= 0;
1026
1027
return end_bulk_insert();
1029
void handler::change_table_ptr(Table *table_arg, TableShare *share)
1030
void Cursor::change_table_ptr(Table *table_arg, TableShare *share)
1031
1032
table= table_arg;
1032
1033
table_share= share;
1035
const key_map *handler::keys_to_use_for_scanning()
1036
const key_map *Cursor::keys_to_use_for_scanning()
1037
1038
return &key_map_empty;
1040
bool handler::has_transactions()
1041
bool Cursor::has_transactions()
1042
1043
return (ha_table_flags() & HA_NO_TRANSACTIONS) == 0;
1045
void handler::ha_statistic_increment(ulong SSV::*offset) const
1046
void Cursor::ha_statistic_increment(ulong SSV::*offset) const
1047
1048
status_var_increment(table->in_use->status_var.*offset);
1050
void **handler::ha_data(Session *session) const
1051
void **Cursor::ha_data(Session *session) const
1052
1053
return session_ha_data(session, engine);
1055
Session *handler::ha_session(void) const
1056
Session *Cursor::ha_session(void) const
1057
1058
assert(!table || !table->in_use || table->in_use == current_session);
1058
1059
return (table && table->in_use) ? table->in_use : current_session;
1062
bool handler::is_fatal_error(int error, uint32_t flags)
1063
bool Cursor::is_fatal_error(int error, uint32_t flags)
1065
1066
((flags & HA_CHECK_DUP_KEY) &&
1073
ha_rows handler::records() { return stats.records; }
1074
ha_rows Cursor::records() { return stats.records; }
1076
Open database-handler.
1077
Open database-Cursor.
1078
1079
Try O_RDONLY if cannot open as O_RDWR
1079
1080
Don't wait for locks if not HA_OPEN_WAIT_IF_LOCKED is set
1081
int handler::ha_open(Table *table_arg, const char *name, int mode,
1082
int Cursor::ha_open(Table *table_arg, const char *name, int mode,
1082
1083
int test_if_locked)
1106
1107
table->db_stat|=HA_READ_ONLY;
1107
1108
(void) extra(HA_EXTRA_NO_READCHECK); // Not needed in SQL
1109
/* ref is already allocated for us if we're called from handler::clone() */
1110
/* ref is already allocated for us if we're called from Cursor::clone() */
1110
1111
if (!ref && !(ref= (unsigned char*) alloc_root(&table->mem_root,
1111
1112
ALIGN_SIZE(ref_length)*2)))
1272
1273
reserved for us.
1274
1275
- In both cases, for the following rows we use those reserved values without
1275
calling the handler again (we just progress in the interval, computing
1276
calling the Cursor again (we just progress in the interval, computing
1276
1277
each new value from the previous one). Until we have exhausted them, then
1277
1278
we either take the next provided interval or call get_auto_increment()
1278
1279
again to reserve a new interval.
1367
handler::estimation_rows_to_insert was set by
1368
handler::ha_start_bulk_insert(); if 0 it means "unknown".
1368
Cursor::estimation_rows_to_insert was set by
1369
Cursor::ha_start_bulk_insert(); if 0 it means "unknown".
1370
1371
uint32_t nb_already_reserved_intervals=
1371
1372
session->auto_inc_intervals_in_cur_stmt_for_binlog.nb_elements();
1479
1480
@param increment
1480
1481
@param nb_desired_values how many values we want
1481
@param first_value (OUT) the first value reserved by the handler
1482
@param nb_reserved_values (OUT) how many values the handler reserved
1482
@param first_value (OUT) the first value reserved by the Cursor
1483
@param nb_reserved_values (OUT) how many values the Cursor reserved
1484
void handler::get_auto_increment(uint64_t ,
1485
void Cursor::get_auto_increment(uint64_t ,
1487
1488
uint64_t *first_value,
1498
1499
error=index_last(table->record[1]);
1500
1501
MySQL implicitely assumes such method does locking (as MySQL decides to
1501
use nr+increment without checking again with the handler, in
1502
handler::update_auto_increment()), so reserves to infinite.
1502
use nr+increment without checking again with the Cursor, in
1503
Cursor::update_auto_increment()), so reserves to infinite.
1504
1505
*nb_reserved_values= UINT64_MAX;
1775
Return an error message specific to this handler.
1776
Return an error message specific to this Cursor.
1777
@param error error code previously returned by handler
1778
@param error error code previously returned by Cursor
1778
1779
@param buf pointer to String where to add error message
1781
1782
Returns true if this is a temporary error
1783
bool handler::get_error_message(int , String* )
1784
bool Cursor::get_error_message(int , String* )
1789
1790
/* Code left, but Drizzle has no legacy yet (while MySQL did) */
1790
int handler::check_old_types()
1791
int Cursor::check_old_types()
1860
1861
Bulk update row: public interface.
1862
@sa handler::bulk_update_row()
1863
@sa Cursor::bulk_update_row()
1866
handler::ha_bulk_update_row(const unsigned char *old_data, unsigned char *new_data,
1867
Cursor::ha_bulk_update_row(const unsigned char *old_data, unsigned char *new_data,
1867
1868
uint32_t *dup_key_found)
1869
1870
mark_trx_read_write();
1993
1994
Tell the storage engine that it is allowed to "disable transaction" in the
1994
handler. It is a hint that ACID is not required - it is used in NDB for
1995
Cursor. It is a hint that ACID is not required - it is used in NDB for
1995
1996
ALTER Table, for example, when data are copied to temporary table.
1996
1997
A storage engine may treat this hint any way it likes. NDB for example
1997
1998
starts to commit every now and then automatically.
2071
2072
/****************************************************************************
2072
** Some general functions that isn't in the handler class
2073
** Some general functions that isn't in the Cursor class
2073
2074
****************************************************************************/
2076
void st_ha_check_opt::init()
2083
2077
Calculate cost of 'index only' scan for given index and number of records
2089
2083
It is assumed that we will read trough the whole key range and that all
2090
2084
key blocks are half full (normally things are much better). It is also
2091
assumed that each time we read the next key from the index, the handler
2085
assumed that each time we read the next key from the index, the Cursor
2092
2086
performs a random seek, thus the cost is proportional to the number of
2096
Consider joining this function and handler::read_time() into one
2097
handler::read_time(keynr, records, ranges, bool index_only) function.
2090
Consider joining this function and Cursor::read_time() into one
2091
Cursor::read_time(keynr, records, ranges, bool index_only) function.
2100
2094
Estimated cost of 'index only' scan
2103
double handler::index_only_read_time(uint32_t keynr, double key_records)
2097
double Cursor::index_only_read_time(uint32_t keynr, double key_records)
2105
2099
uint32_t keys_per_block= (stats.block_size/2/
2106
2100
(table->key_info[keynr].key_length + ref_length) + 1);
2236
2230
other Error or can't perform the requested scan
2239
int handler::multi_range_read_info(uint32_t keyno, uint32_t n_ranges, uint32_t n_rows,
2233
int Cursor::multi_range_read_info(uint32_t keyno, uint32_t n_ranges, uint32_t n_rows,
2240
2234
uint32_t *bufsz, uint32_t *flags, COST_VECT *cost)
2242
2236
*bufsz= 0; /* Default implementation doesn't need a buffer */
2282
2276
The caller will guarantee that if "seq->init == mrr_ranges_array_init"
2283
2277
then seq_init_param is an array of n_ranges KEY_MULTI_RANGE structures.
2284
This property will only be used by NDB handler until WL#2623 is done.
2278
This property will only be used by NDB Cursor until WL#2623 is done.
2286
2280
Buffer memory management is done according to the following scenario:
2287
2281
The caller allocates the buffer and provides it to the callee by filling
2380
Get cost of reading nrows table records in a "disk sweep"
2382
A disk sweep read is a sequence of handler->rnd_pos(rowid) calls that made
2383
for an ordered sequence of rowids.
2385
We assume hard disk IO. The read is performed as follows:
2387
1. The disk head is moved to the needed cylinder
2388
2. The controller waits for the plate to rotate
2389
3. The data is transferred
2391
Time to do #3 is insignificant compared to #2+#1.
2393
Time to move the disk head is proportional to head travel distance.
2395
Time to wait for the plate to rotate depends on whether the disk head
2398
If disk head wasn't moved, the wait time is proportional to distance
2399
between the previous block and the block we're reading.
2401
If the head was moved, we don't know how much we'll need to wait for the
2402
plate to rotate. We assume the wait time to be a variate with a mean of
2403
0.5 of full rotation time.
2405
Our cost units are "random disk seeks". The cost of random disk seek is
2406
actually not a constant, it depends one range of cylinders we're going
2407
to access. We make it constant by introducing a fuzzy concept of "typical
2408
datafile length" (it's fuzzy as it's hard to tell whether it should
2409
include index file, temp.tables etc). Then random seek cost is:
2411
1 = half_rotation_cost + move_cost * 1/3 * typical_data_file_length
2413
We define half_rotation_cost as DISK_SEEK_BASE_COST=0.9.
2415
@param table Table to be accessed
2416
@param nrows Number of rows to retrieve
2417
@param interrupted true <=> Assume that the disk sweep will be
2418
interrupted by other disk IO. false - otherwise.
2419
@param cost OUT The cost.
2422
void get_sweep_read_cost(Table *table, ha_rows nrows, bool interrupted,
2426
if (table->file->primary_key_is_clustered())
2428
cost->io_count= table->file->read_time(table->s->primary_key,
2429
(uint32_t) nrows, nrows);
2434
ceil(uint64_t2double(table->file->stats.data_file_length) / IO_SIZE);
2436
n_blocks * (1.0 - pow(1.0 - 1.0/n_blocks, rows2double(nrows)));
2437
if (busy_blocks < 1.0)
2440
cost->io_count= busy_blocks;
2444
/* Assume reading is done in one 'sweep' */
2445
cost->avg_io_cost= (DISK_SEEK_BASE_COST +
2446
DISK_SEEK_PROP_COST*n_blocks/busy_blocks);
2452
2373
/* **************************************************************************
2453
2374
* DS-MRR implementation ends
2454
2375
***************************************************************************/
2582
int handler::index_read_idx_map(unsigned char * buf, uint32_t index,
2503
int Cursor::index_read_idx_map(unsigned char * buf, uint32_t index,
2583
2504
const unsigned char * key,
2584
2505
key_part_map keypart_map,
2585
2506
enum ha_rkey_function find_flag)
2692
2613
* If we have a timestamp column, update it to the current time
2694
2615
* @TODO Technically, the below two lines can be take even further out of the
2695
* handler interface and into the fill_record() method.
2616
* Cursor interface and into the fill_record() method.
2697
2618
if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_INSERT)
2698
2619
table->timestamp_field->set_time();