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