15.15.4. Parsing Key Information
Many of the index methods pass a byte array named
*key
that identifies the index entry to be
read in a standard format. Your storage engine will need to
extract the information stored in the key and translate it into
its internal index format to identify the row associated with
the index.
The information in the key is obtained by iterating through the
key, which is formatted the same as the definition in
table->key_info[index
]->key_part[part_num
]
.
The following example from ha_berkeley.cc
shows how the BerkeleyDB
storage engine takes
a key defined in *key
and converts it to
internal key format:
/*
Create a packed key from a MySQL unpacked key (like the one that is
sent from the index_read()
This key is to be used to read a row
*/
DBT *ha_berkeley::pack_key(DBT *key, uint keynr, char *buff,
const byte *key_ptr, uint key_length)
{
KEY *key_info=table->key_info+keynr;
KEY_PART_INFO *key_part=key_info->key_part;
KEY_PART_INFO *end=key_part+key_info->key_parts;
DBUG_ENTER("bdb:pack_key");
bzero((char*) key,sizeof(*key));
key->data=buff;
key->app_private= (void*) key_info;
for (; key_part != end && (int) key_length > 0 ; key_part++)
{
uint offset=0;
if (key_part->null_bit)
{
if (!(*buff++ = (*key_ptr == 0))) // Store 0 if NULL
{
key_length-= key_part->store_length;
key_ptr+= key_part->store_length;
key->flags|=DB_DBT_DUPOK;
continue;
}
offset=1; // Data is at key_ptr+1
}
buff=key_part->field->pack_key_from_key_image(buff,(char*) key_ptr+offset,
key_part->length);
key_ptr+=key_part->store_length;
key_length-=key_part->store_length;
}
key->size= (buff - (char*) key->data);
DBUG_DUMP("key",(char*) key->data, key->size);
DBUG_RETURN(key);
}