2009-12-05 02:26:15 +01:00
|
|
|
/* Copyright (C) 2000-2006 MySQL AB, 2008-2009 Sun Microsystems, Inc
|
2000-08-14 13:27:19 +02:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2006-12-23 20:17:15 +01:00
|
|
|
the Free Software Foundation; version 2 of the License.
|
2000-08-14 13:27:19 +02:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2000-08-14 13:27:19 +02:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
|
|
|
/* Read record based on a key */
|
|
|
|
|
|
|
|
#include "myisamdef.h"
|
2002-02-20 11:11:21 +01:00
|
|
|
#include "rt_index.h"
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
/* Read a record using key */
|
|
|
|
/* Ordinary search_flag is 0 ; Give error if no record with key */
|
|
|
|
|
WL#3817: Simplify string / memory area types and make things more consistent (first part)
The following type conversions was done:
- Changed byte to uchar
- Changed gptr to uchar*
- Change my_string to char *
- Change my_size_t to size_t
- Change size_s to size_t
Removed declaration of byte, gptr, my_string, my_size_t and size_s.
Following function parameter changes was done:
- All string functions in mysys/strings was changed to use size_t
instead of uint for string lengths.
- All read()/write() functions changed to use size_t (including vio).
- All protocoll functions changed to use size_t instead of uint
- Functions that used a pointer to a string length was changed to use size_t*
- Changed malloc(), free() and related functions from using gptr to use void *
as this requires fewer casts in the code and is more in line with how the
standard functions work.
- Added extra length argument to dirname_part() to return the length of the
created string.
- Changed (at least) following functions to take uchar* as argument:
- db_dump()
- my_net_write()
- net_write_command()
- net_store_data()
- DBUG_DUMP()
- decimal2bin() & bin2decimal()
- Changed my_compress() and my_uncompress() to use size_t. Changed one
argument to my_uncompress() from a pointer to a value as we only return
one value (makes function easier to use).
- Changed type of 'pack_data' argument to packfrm() to avoid casts.
- Changed in readfrm() and writefrom(), ha_discover and handler::discover()
the type for argument 'frmdata' to uchar** to avoid casts.
- Changed most Field functions to use uchar* instead of char* (reduced a lot of
casts).
- Changed field->val_xxx(xxx, new_ptr) to take const pointers.
Other changes:
- Removed a lot of not needed casts
- Added a few new cast required by other changes
- Added some cast to my_multi_malloc() arguments for safety (as string lengths
needs to be uint, not size_t).
- Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done
explicitely as this conflict was often hided by casting the function to
hash_get_key).
- Changed some buffers to memory regions to uchar* to avoid casts.
- Changed some string lengths from uint to size_t.
- Changed field->ptr to be uchar* instead of char*. This allowed us to
get rid of a lot of casts.
- Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar
- Include zlib.h in some files as we needed declaration of crc32()
- Changed MY_FILE_ERROR to be (size_t) -1.
- Changed many variables to hold the result of my_read() / my_write() to be
size_t. This was needed to properly detect errors (which are
returned as (size_t) -1).
- Removed some very old VMS code
- Changed packfrm()/unpackfrm() to not be depending on uint size
(portability fix)
- Removed windows specific code to restore cursor position as this
causes slowdown on windows and we should not mix read() and pread()
calls anyway as this is not thread safe. Updated function comment to
reflect this. Changed function that depended on original behavior of
my_pwrite() to itself restore the cursor position (one such case).
- Added some missing checking of return value of malloc().
- Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow.
- Changed type of table_def::m_size from my_size_t to ulong to reflect that
m_size is the number of elements in the array, not a string/memory
length.
- Moved THD::max_row_length() to table.cc (as it's not depending on THD).
Inlined max_row_length_blob() into this function.
- More function comments
- Fixed some compiler warnings when compiled without partitions.
- Removed setting of LEX_STRING() arguments in declaration (portability fix).
- Some trivial indentation/variable name changes.
- Some trivial code simplifications:
- Replaced some calls to alloc_root + memcpy to use
strmake_root()/strdup_root().
- Changed some calls from memdup() to strmake() (Safety fix)
- Simpler loops in client-simple.c
2007-05-10 11:59:39 +02:00
|
|
|
int mi_rkey(MI_INFO *info, uchar *buf, int inx, const uchar *key,
|
2007-03-17 00:13:25 +01:00
|
|
|
key_part_map keypart_map, enum ha_rkey_function search_flag)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
uchar *key_buff;
|
|
|
|
MYISAM_SHARE *share=info->s;
|
2003-04-27 21:12:08 +02:00
|
|
|
MI_KEYDEF *keyinfo;
|
2003-05-21 20:39:58 +02:00
|
|
|
HA_KEYSEG *last_used_keyseg;
|
2001-10-02 04:53:00 +02:00
|
|
|
uint pack_key_length, use_key_length, nextflag;
|
2001-05-16 23:46:50 +02:00
|
|
|
DBUG_ENTER("mi_rkey");
|
2007-01-29 00:47:35 +01:00
|
|
|
DBUG_PRINT("enter", ("base: 0x%lx buf: 0x%lx inx: %d search_flag: %d",
|
2005-10-07 02:12:15 +02:00
|
|
|
(long) info, (long) buf, inx, search_flag));
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
if ((inx = _mi_check_index(info,inx)) < 0)
|
|
|
|
DBUG_RETURN(my_errno);
|
|
|
|
|
|
|
|
info->update&= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED);
|
2003-05-19 15:35:49 +02:00
|
|
|
info->last_key_func= search_flag;
|
2003-04-27 21:12:08 +02:00
|
|
|
keyinfo= share->keyinfo + inx;
|
2000-08-14 13:27:19 +02:00
|
|
|
|
2002-11-25 22:18:44 +01:00
|
|
|
if (info->once_flags & USE_PACKED_KEYS)
|
|
|
|
{
|
2003-05-19 15:35:49 +02:00
|
|
|
info->once_flags&= ~USE_PACKED_KEYS; /* Reset flag */
|
2003-04-27 21:12:08 +02:00
|
|
|
/*
|
|
|
|
key is already packed!; This happens when we are using a MERGE TABLE
|
2007-08-13 15:11:25 +02:00
|
|
|
In this key 'key_part_map' is the length of the key !
|
2003-04-27 21:12:08 +02:00
|
|
|
*/
|
2002-11-25 22:18:44 +01:00
|
|
|
key_buff=info->lastkey+info->s->base.max_key_length;
|
2007-01-29 10:40:26 +01:00
|
|
|
pack_key_length= keypart_map;
|
|
|
|
bmove(key_buff, key, pack_key_length);
|
2007-04-10 22:40:35 +02:00
|
|
|
last_used_keyseg= info->s->keyinfo[inx].seg + info->last_used_keyseg;
|
2002-11-25 22:18:44 +01:00
|
|
|
}
|
|
|
|
else
|
2000-08-14 13:27:19 +02:00
|
|
|
{
|
2007-01-29 10:40:26 +01:00
|
|
|
DBUG_ASSERT(keypart_map);
|
2005-09-23 10:15:11 +02:00
|
|
|
/* Save the packed key for later use in the second buffer of lastkey. */
|
2000-09-28 23:58:16 +02:00
|
|
|
key_buff=info->lastkey+info->s->base.max_key_length;
|
2003-05-19 15:35:49 +02:00
|
|
|
pack_key_length=_mi_pack_key(info,(uint) inx, key_buff, (uchar*) key,
|
2007-01-29 10:40:26 +01:00
|
|
|
keypart_map, &last_used_keyseg);
|
2005-09-23 10:15:11 +02:00
|
|
|
/* Save packed_key_length for use by the MERGE engine. */
|
|
|
|
info->pack_key_length= pack_key_length;
|
2007-04-19 08:53:25 +02:00
|
|
|
info->last_used_keyseg= (uint16) (last_used_keyseg -
|
|
|
|
info->s->keyinfo[inx].seg);
|
2003-05-19 15:35:49 +02:00
|
|
|
DBUG_EXECUTE("key",_mi_print_key(DBUG_FILE, keyinfo->seg,
|
|
|
|
key_buff, pack_key_length););
|
2000-08-14 13:27:19 +02:00
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2001-10-02 04:53:00 +02:00
|
|
|
if (fast_mi_readinfo(info))
|
2000-07-31 21:29:14 +02:00
|
|
|
goto err;
|
2006-06-28 18:55:30 +02:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
if (share->concurrent_insert)
|
2009-12-05 02:26:15 +01:00
|
|
|
mysql_rwlock_rdlock(&share->key_root_lock[inx]);
|
2001-10-02 04:53:00 +02:00
|
|
|
|
|
|
|
nextflag=myisam_read_vec[search_flag];
|
|
|
|
use_key_length=pack_key_length;
|
|
|
|
if (!(nextflag & (SEARCH_FIND | SEARCH_NO_FIND | SEARCH_LAST)))
|
|
|
|
use_key_length=USE_WHOLE_KEY;
|
|
|
|
|
2003-05-19 15:35:49 +02:00
|
|
|
switch (info->s->keyinfo[inx].key_alg) {
|
2004-03-12 15:51:03 +01:00
|
|
|
#ifdef HAVE_RTREE_KEYS
|
2002-02-20 11:11:21 +01:00
|
|
|
case HA_KEY_ALG_RTREE:
|
2003-05-19 15:35:49 +02:00
|
|
|
if (rtree_find_first(info,inx,key_buff,use_key_length,nextflag) < 0)
|
2002-02-20 11:11:21 +01:00
|
|
|
{
|
2004-12-31 02:47:56 +01:00
|
|
|
mi_print_error(info->s, HA_ERR_CRASHED);
|
2002-02-20 11:11:21 +01:00
|
|
|
my_errno=HA_ERR_CRASHED;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
break;
|
2004-03-12 15:51:03 +01:00
|
|
|
#endif
|
2002-02-20 11:11:21 +01:00
|
|
|
case HA_KEY_ALG_BTREE:
|
|
|
|
default:
|
2003-05-19 15:35:49 +02:00
|
|
|
if (!_mi_search(info, keyinfo, key_buff, use_key_length,
|
2006-07-04 19:10:13 +02:00
|
|
|
myisam_read_vec[search_flag], info->s->state.key_root[inx]))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
/*
|
2007-08-01 11:54:24 +02:00
|
|
|
Found a key, but it might not be usable. We cannot use rows that
|
|
|
|
are inserted by other threads after we got our table lock
|
|
|
|
("concurrent inserts"). The record may not even be present yet.
|
|
|
|
Keys are inserted into the index(es) before the record is
|
|
|
|
inserted into the data file. When we got our table lock, we
|
|
|
|
saved the current data_file_length. Concurrent inserts always go
|
|
|
|
to the end of the file. So we can test if the found key
|
|
|
|
references a new record.
|
2000-07-31 21:29:14 +02:00
|
|
|
*/
|
2007-08-01 11:54:24 +02:00
|
|
|
if (info->lastpos >= info->state->data_file_length)
|
2006-07-04 19:10:13 +02:00
|
|
|
{
|
2007-08-01 11:54:24 +02:00
|
|
|
/* The key references a concurrently inserted record. */
|
|
|
|
if (search_flag == HA_READ_KEY_EXACT &&
|
|
|
|
last_used_keyseg == keyinfo->seg + keyinfo->keysegs)
|
|
|
|
{
|
|
|
|
/* Simply ignore the key if it matches exactly. (Bug #29838) */
|
|
|
|
my_errno= HA_ERR_KEY_NOT_FOUND;
|
|
|
|
info->lastpos= HA_OFFSET_ERROR;
|
|
|
|
}
|
|
|
|
else
|
2006-08-29 20:45:04 +02:00
|
|
|
{
|
2006-09-07 16:46:20 +02:00
|
|
|
/*
|
2007-08-01 11:54:24 +02:00
|
|
|
If searching for a partial key (or using >, >=, < or <=) and
|
|
|
|
the data is outside of the data file, we need to continue
|
|
|
|
searching for the first key inside the data file.
|
2006-09-07 16:46:20 +02:00
|
|
|
*/
|
2007-08-01 11:54:24 +02:00
|
|
|
do
|
2006-09-07 16:46:20 +02:00
|
|
|
{
|
2007-08-01 11:54:24 +02:00
|
|
|
uint not_used[2];
|
|
|
|
/*
|
|
|
|
Skip rows that are inserted by other threads since we got
|
|
|
|
a lock. Note that this can only happen if we are not
|
|
|
|
searching after a full length exact key, because the keys
|
|
|
|
are sorted according to position.
|
|
|
|
*/
|
|
|
|
if (_mi_search_next(info, keyinfo, info->lastkey,
|
|
|
|
info->lastkey_length,
|
|
|
|
myisam_readnext_vec[search_flag],
|
|
|
|
info->s->state.key_root[inx]))
|
|
|
|
break; /* purecov: inspected */
|
|
|
|
/*
|
|
|
|
Check that the found key does still match the search.
|
|
|
|
_mi_search_next() delivers the next key regardless of its
|
|
|
|
value.
|
|
|
|
*/
|
|
|
|
if (search_flag == HA_READ_KEY_EXACT &&
|
|
|
|
ha_key_cmp(keyinfo->seg, key_buff, info->lastkey,
|
|
|
|
use_key_length, SEARCH_FIND, not_used))
|
|
|
|
{
|
|
|
|
/* purecov: begin inspected */
|
|
|
|
my_errno= HA_ERR_KEY_NOT_FOUND;
|
|
|
|
info->lastpos= HA_OFFSET_ERROR;
|
|
|
|
break;
|
|
|
|
/* purecov: end */
|
|
|
|
}
|
|
|
|
} while (info->lastpos >= info->state->data_file_length);
|
|
|
|
}
|
2002-02-20 11:11:21 +01:00
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (share->concurrent_insert)
|
2009-12-05 02:26:15 +01:00
|
|
|
mysql_rwlock_unlock(&share->key_root_lock[inx]);
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2003-04-27 21:12:08 +02:00
|
|
|
/* Calculate length of the found key; Used by mi_rnext_same */
|
2003-04-30 09:15:09 +02:00
|
|
|
if ((keyinfo->flag & HA_VAR_LENGTH_KEY) && last_used_keyseg &&
|
|
|
|
info->lastpos != HA_OFFSET_ERROR)
|
2003-04-27 21:12:08 +02:00
|
|
|
info->last_rkey_length= _mi_keylength_part(keyinfo, info->lastkey,
|
|
|
|
last_used_keyseg);
|
|
|
|
else
|
|
|
|
info->last_rkey_length= pack_key_length;
|
2003-04-30 09:15:09 +02:00
|
|
|
|
|
|
|
/* Check if we don't want to have record back, only error message */
|
2000-08-14 13:27:19 +02:00
|
|
|
if (!buf)
|
2003-04-30 09:15:09 +02:00
|
|
|
DBUG_RETURN(info->lastpos == HA_OFFSET_ERROR ? my_errno : 0);
|
2000-08-14 13:27:19 +02:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
if (!(*info->read_record)(info,info->lastpos,buf))
|
|
|
|
{
|
|
|
|
info->update|= HA_STATE_AKTIV; /* Record is read */
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
info->lastpos = HA_OFFSET_ERROR; /* Didn't find key */
|
|
|
|
|
2003-04-30 09:15:09 +02:00
|
|
|
/* Store last used key as a base for read next */
|
2000-07-31 21:29:14 +02:00
|
|
|
memcpy(info->lastkey,key_buff,pack_key_length);
|
2003-04-27 21:12:08 +02:00
|
|
|
info->last_rkey_length= pack_key_length;
|
2000-07-31 21:29:14 +02:00
|
|
|
bzero((char*) info->lastkey+pack_key_length,info->s->base.rec_reflength);
|
|
|
|
info->lastkey_length=pack_key_length+info->s->base.rec_reflength;
|
|
|
|
|
|
|
|
if (search_flag == HA_READ_AFTER_KEY)
|
|
|
|
info->update|=HA_STATE_NEXT_FOUND; /* Previous gives last row */
|
|
|
|
err:
|
|
|
|
DBUG_RETURN(my_errno);
|
2000-08-14 13:27:19 +02:00
|
|
|
} /* _mi_rkey */
|