MySQL 8.4.3
Source Code Documentation
|
#include "sql/key.h"
#include <string.h>
#include <algorithm>
#include "my_bitmap.h"
#include "my_byteorder.h"
#include "my_compare.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_macros.h"
#include "mysql/strings/m_ctype.h"
#include "sql/field.h"
#include "sql/handler.h"
#include "sql/sql_const.h"
#include "sql/sql_error.h"
#include "sql/table.h"
#include "sql_string.h"
#include "string_with_len.h"
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 idx, uint key_length) |
Compare if a key has changed. More... | |
void | field_unpack (String *to, Field *field, uint max_length, bool prefix_key) |
Unpack a field and append it. More... | |
void | key_unpack (String *to, TABLE *table, KEY *key) |
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, uchar *first_rec, uchar *second_rec) |
Compare two records in index order. More... | |
Unpack a field and append it.
[in,out] | to | String to append the field contents to. |
field | Field to unpack. | |
max_length | Maximum length of field to unpack or 0 for unlimited. | |
prefix_key | The field is used as a prefix key. |
int find_ref_key | ( | KEY * | key, |
uint | key_count, | ||
uchar * | record, | ||
Field * | field, | ||
uint * | key_length, | ||
uint * | keypart | ||
) |
int key_cmp | ( | KEY_PART_INFO * | key_part, |
const uchar * | key, | ||
uint | key_length | ||
) |
Compare key in record buffer to a given key.
key_part | Key part handler |
key | Key to compare to value in table->record[0] |
key_length | length 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.
int key_cmp2 | ( | KEY_PART_INFO * | key_part, |
const uchar * | key1, | ||
uint | key1_length, | ||
const uchar * | key2, | ||
uint | key2_length | ||
) |
Compare two given keys.
key_part | Key part handler |
key1 | Key to be compared with key2 |
key1_length | length of 'key1' |
key2 | Key to be compared with key1 |
key2_length | length of 'key2' |
Below comparison code is under the assumption that key1_length and key2_length are same and key1_length, key2_length are non zero value.
Compare if a key has changed.
table | TABLE |
key | key to compare to row |
idx | Index used |
key_length | Length of key |
0 | If key is equal |
1 | Key has changed |
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.
to_key | buffer that will be used as a key |
from_record | full record to be copied from |
key_info | descriptor of the index |
key_length | specifies length of all keyparts that will be copied |
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!
key | Null terminated array of index information |
first_rec | Pointer to record compare with |
second_rec | Pointer to record compare against first_rec |
0 | Keys are equal |
-1 | second_rec is after first_rec |
+1 | first_rec is after second_rec |
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.
to_record | record buffer where the key will be restored to |
from_key | buffer that contains a key |
key_info | descriptor of the index |
key_length | specifies length of all keyparts that will be restored |