hp_rkey.c 2.6 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
unknown's avatar
unknown committed
2

unknown's avatar
unknown committed
3 4 5 6
   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
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
unknown's avatar
unknown committed
7

unknown's avatar
unknown committed
8 9 10 11
   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.
unknown's avatar
unknown committed
12

unknown's avatar
unknown committed
13 14 15 16 17 18
   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 */

#include "heapdef.h"

19 20
int heap_rkey(HP_INFO *info, byte *record, int inx, const byte *key, 
              uint key_len, enum ha_rkey_function find_flag)
unknown's avatar
unknown committed
21 22
{
  byte *pos;
unknown's avatar
unknown committed
23 24
  HP_SHARE *share= info->s;
  HP_KEYDEF *keyinfo= share->keydef + inx;
25
  DBUG_ENTER("heap_rkey");
unknown's avatar
unknown committed
26 27 28 29
  DBUG_PRINT("enter",("base: %lx  inx: %d",info,inx));

  if ((uint) inx >= share->keys)
  {
unknown's avatar
unknown committed
30
    DBUG_RETURN(my_errno= HA_ERR_WRONG_INDEX);
unknown's avatar
unknown committed
31
  }
unknown's avatar
unknown committed
32 33
  info->lastinx= inx;
  info->current_record= (ulong) ~0L;		/* For heap_rrnd() */
unknown's avatar
unknown committed
34

35
  if (keyinfo->algorithm == HA_KEY_ALG_BTREE)
unknown's avatar
unknown committed
36
  {
37 38
    heap_rb_param custom_arg;

unknown's avatar
unknown committed
39
    custom_arg.keyseg= info->s->keydef[inx].seg;
unknown's avatar
unknown committed
40 41
    custom_arg.key_length= info->lastkey_len= 
			hp_rb_pack_key(keyinfo, info->recbuf, key, key_len);
unknown's avatar
unknown committed
42
    custom_arg.search_flag= SEARCH_FIND | SEARCH_SAME;
43 44
    /* for next rkey() after deletion */
    if (find_flag == HA_READ_AFTER_KEY)
unknown's avatar
unknown committed
45
      info->last_find_flag= HA_READ_KEY_OR_NEXT;
46
    else if (find_flag == HA_READ_BEFORE_KEY)
unknown's avatar
unknown committed
47
      info->last_find_flag= HA_READ_KEY_OR_PREV;
48
    else
unknown's avatar
unknown committed
49 50 51
      info->last_find_flag= find_flag;
    if (!(pos= tree_search_key(&keyinfo->rb_tree, info->recbuf, info->parents,
			       &info->last_pos, find_flag, &custom_arg)))
52
    {
unknown's avatar
unknown committed
53 54
      info->update= 0;
      DBUG_RETURN(my_errno= HA_ERR_KEY_NOT_FOUND);
55
    }
unknown's avatar
unknown committed
56 57
    memcpy(&pos, pos + (*keyinfo->get_key_length)(keyinfo, pos), sizeof(byte*));
    info->current_ptr= pos;
58 59 60
  }
  else
  {
unknown's avatar
unknown committed
61
    if (!(pos= hp_search(info, share->keydef + inx, key, 0)))
62
    {
unknown's avatar
unknown committed
63
      info->update= 0;
64 65 66
      DBUG_RETURN(my_errno);
    }
    if (!(keyinfo->flag & HA_NOSAME))
unknown's avatar
unknown committed
67
      memcpy(info->lastkey, key, (size_t) keyinfo->length);
unknown's avatar
unknown committed
68
  }
unknown's avatar
unknown committed
69 70
  memcpy(record, pos, (size_t) share->reclength);
  info->update= HA_STATE_AKTIV;
unknown's avatar
unknown committed
71 72 73 74 75 76 77 78
  DBUG_RETURN(0);
}


	/* Quick find of record */

gptr heap_find(HP_INFO *info, int inx, const byte *key)
{
79
  return hp_search(info, info->s->keydef + inx, key, 0);
unknown's avatar
unknown committed
80
}