mi_rkey.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:4k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    This program is free software; you can redistribute it and/or modify
  3.    it under the terms of the GNU General Public License as published by
  4.    the Free Software Foundation; either version 2 of the License, or
  5.    (at your option) any later version.
  6.    This program is distributed in the hope that it will be useful,
  7.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9.    GNU General Public License for more details.
  10.    You should have received a copy of the GNU General Public License
  11.    along with this program; if not, write to the Free Software
  12.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  13. /* Read record based on a key */
  14. #include "myisamdef.h"
  15. /* Read a record using key */
  16. /* Ordinary search_flag is 0 ; Give error if no record with key */
  17. int _mi_rkey(MI_INFO *info, byte *buf, int inx, const byte *key, uint key_len,
  18.      enum ha_rkey_function search_flag, bool raw_key)
  19. {
  20.   uchar *key_buff;
  21.   MYISAM_SHARE *share=info->s;
  22.   uint pack_key_length;
  23.   DBUG_ENTER("_mi_rkey");
  24.   DBUG_PRINT("enter",("base: %lx  inx: %d  search_flag: %d",
  25.       info,inx,search_flag));
  26.   if ((inx = _mi_check_index(info,inx)) < 0)
  27.     DBUG_RETURN(my_errno);
  28.   info->update&= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED);
  29.   if (raw_key)
  30.   {
  31.     if (key_len == 0)
  32.       key_len=USE_WHOLE_KEY;
  33.     key_buff=info->lastkey+info->s->base.max_key_length;
  34.     pack_key_length=_mi_pack_key(info,(uint) inx,key_buff,(uchar*) key,key_len);
  35.     info->last_rkey_length=pack_key_length;
  36.     DBUG_EXECUTE("key",_mi_print_key(DBUG_FILE,share->keyinfo[inx].seg,
  37.      key_buff,pack_key_length););
  38.   }
  39.   else
  40.   {
  41.     /* key is already packed! */
  42.     key_buff=info->lastkey+info->s->base.max_key_length;
  43.     info->last_rkey_length=pack_key_length=key_len;
  44.     bmove(key_buff,key,key_len);
  45.   }
  46.   if (_mi_readinfo(info,F_RDLCK,1))
  47.     goto err;
  48.   if (share->concurrent_insert)
  49.     rw_rdlock(&share->key_root_lock[inx]);
  50.   if (!_mi_search(info,info->s->keyinfo+inx,key_buff,pack_key_length,
  51.   myisam_read_vec[search_flag],info->s->state.key_root[inx]))
  52.   {
  53.     while (info->lastpos >= info->state->data_file_length)
  54.     {
  55.       /*
  56. Skip rows that are inserted by other threads since we got a lock
  57. Note that this can only happen if we are not searching after an
  58. exact key, because the keys are sorted according to position
  59.       */
  60.       if  (_mi_search_next(info,info->s->keyinfo+inx,info->lastkey,
  61.    info->lastkey_length,
  62.    myisam_readnext_vec[search_flag],
  63.    info->s->state.key_root[inx]))
  64. break;
  65.     }
  66.   }
  67.   if (share->concurrent_insert)
  68.     rw_unlock(&share->key_root_lock[inx]);
  69.   if (!buf)
  70.     DBUG_RETURN(info->lastpos==HA_OFFSET_ERROR ? my_errno : 0);
  71.   if (!(*info->read_record)(info,info->lastpos,buf))
  72.   {
  73.     info->update|= HA_STATE_AKTIV; /* Record is read */
  74.     DBUG_RETURN(0);
  75.   }
  76.   info->lastpos = HA_OFFSET_ERROR; /* Didn't find key */
  77.   /* Store key for read next */
  78.   memcpy(info->lastkey,key_buff,pack_key_length);
  79.   bzero((char*) info->lastkey+pack_key_length,info->s->base.rec_reflength);
  80.   info->lastkey_length=pack_key_length+info->s->base.rec_reflength;
  81.   if (search_flag == HA_READ_AFTER_KEY)
  82.     info->update|=HA_STATE_NEXT_FOUND; /* Previous gives last row */
  83. err:
  84.   DBUG_RETURN(my_errno);
  85. } /* _mi_rkey */
  86. /* shouldn't forget to do it inline sometime */
  87. int mi_rkey(MI_INFO *info, byte *buf, int inx, const byte *key, uint key_len,
  88.             enum ha_rkey_function search_flag)
  89. {
  90.   return _mi_rkey(info,buf,inx,key,key_len,search_flag,TRUE);
  91. }