MySQL  8.0.19
Source Code Documentation
key.h File Reference
#include <stddef.h>
#include <sys/types.h>
#include "lex_string.h"
#include "my_base.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "sql/key_spec.h"
#include "sql/sql_plugin_ref.h"

Go to the source code of this file.

Classes

class  FOREIGN_KEY
 
class  KEY_PART_INFO
 
class  KEY
 

Macros

#define REC_PER_KEY_UNKNOWN   -1.0f
 If an entry for a key part in KEY::rec_per_key_float[] has this value, then the storage engine has not provided a value for it and the rec_per_key value for this key part is unknown. More...
 
#define IN_MEMORY_ESTIMATE_UNKNOWN   -1.0
 If the "in memory estimate" for a table (in ha_statistics.table_in_mem_estimate) or index (in KEY::m_in_memory_estimate) is not known or not set by the storage engine, then it should have the following value. More...
 

Typedefs

typedef float rec_per_key_t
 Data type for records per key estimates that are stored in the KEY::rec_per_key_float[] array. More...
 

Functions

int find_ref_key (KEY *key, uint key_count, uchar *record, Field *field, uint *key_length, uint *keypart)
 
void key_copy (uchar *to_key, const uchar *from_record, const KEY *key_info, uint key_length)
 Copy part of a record that forms a key or key prefix to a buffer. More...
 
void key_restore (uchar *to_record, const uchar *from_key, const KEY *key_info, uint key_length)
 Restore a key from some buffer to record. More...
 
bool key_cmp_if_same (const TABLE *table, const uchar *key, uint index, uint key_length)
 Compare if a key has changed. More...
 
void key_unpack (String *to, TABLE *table, KEY *key)
 
void field_unpack (String *to, Field *field, uint max_length, bool prefix_key)
 Unpack a field and append it. More...
 
bool is_key_used (TABLE *table, uint idx, const MY_BITMAP *fields)
 
int key_cmp (KEY_PART_INFO *key_part, const uchar *key, uint key_length)
 Compare key in record buffer to a given key. More...
 
int key_cmp2 (KEY_PART_INFO *key_part, const uchar *key1, uint key1_length, const uchar *key2, uint key2_length)
 Compare two given keys. More...
 
int key_rec_cmp (KEY **key_info, uchar *a, uchar *b)
 Compare two records in index order. More...
 

Macro Definition Documentation

◆ IN_MEMORY_ESTIMATE_UNKNOWN

#define IN_MEMORY_ESTIMATE_UNKNOWN   -1.0

If the "in memory estimate" for a table (in ha_statistics.table_in_mem_estimate) or index (in KEY::m_in_memory_estimate) is not known or not set by the storage engine, then it should have the following value.

◆ REC_PER_KEY_UNKNOWN

#define REC_PER_KEY_UNKNOWN   -1.0f

If an entry for a key part in KEY::rec_per_key_float[] has this value, then the storage engine has not provided a value for it and the rec_per_key value for this key part is unknown.

Typedef Documentation

◆ rec_per_key_t

typedef float rec_per_key_t

Data type for records per key estimates that are stored in the KEY::rec_per_key_float[] array.

Function Documentation

◆ field_unpack()

void field_unpack ( String to,
Field field,
uint  max_length,
bool  prefix_key 
)

Unpack a field and append it.

Parameters
[in,out]toString to append the field contents to.
fieldField to unpack.
max_lengthMaximum length of field to unpack or 0 for unlimited.
prefix_keyThe field is used as a prefix key.

◆ find_ref_key()

int find_ref_key ( KEY key,
uint  key_count,
uchar record,
Field field,
uint key_length,
uint keypart 
)

◆ is_key_used()

bool is_key_used ( TABLE table,
uint  idx,
const MY_BITMAP fields 
)

◆ key_cmp()

int key_cmp ( KEY_PART_INFO key_part,
const uchar key,
uint  key_length 
)

Compare key in record buffer to a given key.

Parameters
key_partKey part handler
keyKey to compare to value in table->record[0]
key_lengthlength of 'key'

The function compares given key and key in record buffer, part by part, using info from key_part arg. Since callers expect before/after rather than lesser/greater, result depends on the HA_REVERSE_SORT flag of the key part. E.g. For ASC key part and two keys, 'A' and 'Z', -1 will be returned. For same keys, but DESC key part, 1 will be returned.

Returns
The return value is SIGN(key_in_row - range_key):
  • 0 Key is equal to record's key
  • -1 Key is before record's key
  • 1 Key is after record's key
Note
: keep this function and key_cmp2() in sync

◆ key_cmp2()

int key_cmp2 ( KEY_PART_INFO key_part,
const uchar key1,
uint  key1_length,
const uchar key2,
uint  key2_length 
)

Compare two given keys.

Parameters
key_partKey part handler
key1Key to be compared with key2
key1_lengthlength of 'key1'
key2Key to be compared with key1
key2_lengthlength of 'key2'
Returns
The return value is an integral value that takes into account ASC/DESC order of keyparts and indicates the relationship between the two keys:
  • 0 key1 equal to key2
  • -1 Key1 before Key2
  • 1 Key1 after Key2
Note
: keep this function and key_cmp() in sync

Below comparison code is under the assumption that key1_length and key2_length are same and key1_length, key2_length are non zero value.

See also
key_cmp()

◆ key_cmp_if_same()

bool key_cmp_if_same ( const TABLE table,
const uchar key,
uint  idx,
uint  key_length 
)

Compare if a key has changed.

Parameters
tableTABLE
keykey to compare to row
idxIndex used
key_lengthLength of key
Note
In theory we could just call field->cmp() for all field types, but as we are only interested if a key has changed (not if the key is larger or smaller than the previous value) we can do things a bit faster by using memcmp() instead.
Return values
0If key is equal
1Key has changed

◆ key_copy()

void key_copy ( uchar to_key,
const uchar from_record,
const KEY key_info,
uint  key_length 
)

Copy part of a record that forms a key or key prefix to a buffer.

The function takes a complete table record (as e.g. retrieved by handler::index_read()), and a description of an index on the same table, and extracts the first key_length bytes of the record which are part of a key into to_key. If length == 0 then copy all bytes from the record that form a key.

Parameters
to_keybuffer that will be used as a key
from_recordfull record to be copied from
key_infodescriptor of the index
key_lengthspecifies length of all keyparts that will be copied

◆ key_rec_cmp()

int key_rec_cmp ( KEY **  key,
uchar first_rec,
uchar second_rec 
)

Compare two records in index order.

This method is set-up such that it can be called directly from the priority queue and it is attempted to be optimised as much as possible since this will be called O(N * log N) times while performing a merge sort in various places in the code.

We retrieve the pointer to table->record[0] using the fact that key_parts have an offset making it possible to calculate the start of the record. We need to get the diff to the compared record since none of the records being compared are stored in table->record[0].

We first check for NULL values, if there are no NULL values we use a compare method that gets two field pointers and a max length and return the result of the comparison.

key is a null terminated array, since in some cases (clustered primary key) it must compare more than one index. We only compare the fields that are specified in table->read_set and stop at the first non set field. The first must be set!

Parameters
keyNull terminated array of index information
first_recPointer to record compare with
second_recPointer to record compare against first_rec
Returns
Return value is less, equal or greater than 0 if first rec is sorted before, same or after second rec.
Return values
0Keys are equal
-1second_rec is after first_rec
+1first_rec is after second_rec

◆ key_restore()

void key_restore ( uchar to_record,
const uchar from_key,
const KEY key_info,
uint  key_length 
)

Restore a key from some buffer to record.

This function converts a key into record format. It can be used in cases when we want to return a key as a result row.

Parameters
to_recordrecord buffer where the key will be restored to
from_keybuffer that contains a key
key_infodescriptor of the index
key_lengthspecifies length of all keyparts that will be restored

◆ key_unpack()

void key_unpack ( String to,
TABLE table,
KEY key 
)