MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hp_update.c
1 /* Copyright (C) 2000-2002, 2004-2005 MySQL AB
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 Street, Fifth Floor, Boston, MA 02110-1301, USA */
15 
16 /* Update current record in heap-database */
17 
18 #include "heapdef.h"
19 
20 int heap_update(HP_INFO *info, const uchar *old, const uchar *heap_new)
21 {
22  HP_KEYDEF *keydef, *end, *p_lastinx;
23  uchar *pos;
24  my_bool auto_key_changed= 0;
25  HP_SHARE *share= info->s;
26  DBUG_ENTER("heap_update");
27 
28  test_active(info);
29  pos=info->current_ptr;
30 
31  if (info->opt_flag & READ_CHECK_USED && hp_rectest(info,old))
32  DBUG_RETURN(my_errno); /* Record changed */
33  if (--(share->records) < share->blength >> 1) share->blength>>= 1;
34  share->changed=1;
35 
36  p_lastinx= share->keydef + info->lastinx;
37  for (keydef= share->keydef, end= keydef + share->keys; keydef < end; keydef++)
38  {
39  if (hp_rec_key_cmp(keydef, old, heap_new, 0))
40  {
41  if ((*keydef->delete_key)(info, keydef, old, pos, keydef == p_lastinx) ||
42  (*keydef->write_key)(info, keydef, heap_new, pos))
43  goto err;
44  if (share->auto_key == (uint) (keydef - share->keydef + 1))
45  auto_key_changed= 1;
46  }
47  }
48 
49  memcpy(pos,heap_new,(size_t) share->reclength);
50  if (++(share->records) == share->blength) share->blength+= share->blength;
51 
52 #if !defined(DBUG_OFF) && defined(EXTRA_HEAP_DEBUG)
53  DBUG_EXECUTE("check_heap",heap_check_heap(info, 0););
54 #endif
55  if (auto_key_changed)
56  heap_update_auto_increment(info, heap_new);
57  DBUG_RETURN(0);
58 
59  err:
60  if (my_errno == HA_ERR_FOUND_DUPP_KEY)
61  {
62  info->errkey = (int) (keydef - share->keydef);
63  if (keydef->algorithm == HA_KEY_ALG_BTREE)
64  {
65  /* we don't need to delete non-inserted key from rb-tree */
66  if ((*keydef->write_key)(info, keydef, old, pos))
67  {
68  if (++(share->records) == share->blength)
69  share->blength+= share->blength;
70  DBUG_RETURN(my_errno);
71  }
72  keydef--;
73  }
74  while (keydef >= share->keydef)
75  {
76  if (hp_rec_key_cmp(keydef, old, heap_new, 0))
77  {
78  if ((*keydef->delete_key)(info, keydef, heap_new, pos, 0) ||
79  (*keydef->write_key)(info, keydef, old, pos))
80  break;
81  }
82  keydef--;
83  }
84  }
85  if (++(share->records) == share->blength)
86  share->blength+= share->blength;
87  DBUG_RETURN(my_errno);
88 } /* heap_update */