1 /* Copyright (c) 2000, 2002-2007 MySQL AB, 2009 Sun Microsystems, Inc.
2    Use is subject to license terms.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */
16 
17 #include "heapdef.h"
18 
heap_rkey(HP_INFO * info,uchar * record,int inx,const uchar * key,key_part_map keypart_map,enum ha_rkey_function find_flag)19 int heap_rkey(HP_INFO *info, uchar *record, int inx, const uchar *key,
20               key_part_map keypart_map, enum ha_rkey_function find_flag)
21 {
22   uchar *pos;
23   HP_SHARE *share= info->s;
24   HP_KEYDEF *keyinfo= share->keydef + inx;
25   DBUG_ENTER("heap_rkey");
26   DBUG_PRINT("enter",("info: %p  inx: %d", info, inx));
27 
28   if ((uint) inx >= share->keys)
29   {
30     DBUG_RETURN(my_errno= HA_ERR_WRONG_INDEX);
31   }
32   info->lastinx= inx;
33   info->current_record= (ulong) ~0L;		/* For heap_rrnd() */
34   info->key_version= info->s->key_version;
35 
36   if (keyinfo->algorithm == HA_KEY_ALG_BTREE)
37   {
38     heap_rb_param custom_arg;
39 
40     custom_arg.keyseg= info->s->keydef[inx].seg;
41     custom_arg.key_length= info->lastkey_len=
42       hp_rb_pack_key(keyinfo, (uchar*) info->lastkey,
43 		     (uchar*) key, keypart_map);
44     custom_arg.search_flag= SEARCH_FIND | SEARCH_SAME;
45     /* for next rkey() after deletion */
46     if (find_flag == HA_READ_AFTER_KEY)
47       info->last_find_flag= HA_READ_KEY_OR_NEXT;
48     else if (find_flag == HA_READ_BEFORE_KEY)
49       info->last_find_flag= HA_READ_KEY_OR_PREV;
50     else
51       info->last_find_flag= find_flag;
52     if (!(pos= tree_search_key(&keyinfo->rb_tree, info->lastkey, info->parents,
53 			       &info->last_pos, find_flag, &custom_arg)))
54     {
55       info->update= HA_STATE_NO_KEY;
56       DBUG_RETURN(my_errno= HA_ERR_KEY_NOT_FOUND);
57     }
58     memcpy(&pos, pos + (*keyinfo->get_key_length)(keyinfo, pos), sizeof(uchar*));
59     info->current_ptr= pos;
60   }
61   else
62   {
63     if (!(pos= hp_search(info, share->keydef + inx, key, 0)))
64     {
65       info->update= HA_STATE_NO_KEY;
66       DBUG_RETURN(my_errno);
67     }
68     if ((keyinfo->flag & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME)
69       memcpy(info->lastkey, key, (size_t) keyinfo->length);
70   }
71   memcpy(record, pos, (size_t) share->reclength);
72   info->update= HA_STATE_AKTIV;
73   DBUG_RETURN(0);
74 }
75 
76 
77 	/* Quick find of record */
78 
heap_find(HP_INFO * info,int inx,const uchar * key)79 uchar* heap_find(HP_INFO *info, int inx, const uchar *key)
80 {
81   return hp_search(info, info->s->keydef + inx, key, 0);
82 }
83