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