1 /*
2    Copyright (c) 2000, 2011, Oracle and/or its affiliates
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 /* Update an old row in a MyISAM table */
18 
19 #include "fulltext.h"
20 #include "rt_index.h"
21 
mi_update(register MI_INFO * info,const uchar * oldrec,const uchar * newrec)22 int mi_update(register MI_INFO *info, const uchar *oldrec,
23               const uchar *newrec)
24 {
25   int flag,key_changed,save_errno;
26   reg3 my_off_t pos;
27   uint i;
28   uchar old_key[HA_MAX_KEY_BUFF],*new_key;
29   my_bool auto_key_changed=0;
30   ulonglong changed;
31   MYISAM_SHARE *share=info->s;
32   ha_checksum UNINIT_VAR(old_checksum);
33   DBUG_ENTER("mi_update");
34 
35   DBUG_EXECUTE_IF("myisam_pretend_crashed_table_on_usage",
36                   mi_print_error(info->s, HA_ERR_CRASHED);
37                   DBUG_RETURN(my_errno= HA_ERR_CRASHED););
38   if (!(info->update & HA_STATE_AKTIV))
39   {
40     DBUG_RETURN(my_errno=HA_ERR_KEY_NOT_FOUND);
41   }
42   if (share->options & HA_OPTION_READ_ONLY_DATA)
43   {
44     DBUG_RETURN(my_errno=EACCES);
45   }
46   if (info->state->key_file_length >= share->base.margin_key_file_length)
47   {
48     DBUG_RETURN(my_errno=HA_ERR_INDEX_FILE_FULL);
49   }
50   pos=info->lastpos;
51   if (_mi_readinfo(info,F_WRLCK,1))
52     DBUG_RETURN(my_errno);
53 
54   if (share->calc_checksum)
55     old_checksum=info->checksum=(*share->calc_checksum)(info,oldrec);
56   if ((*share->compare_record)(info,oldrec))
57   {
58     save_errno=my_errno;
59     goto err_end;			/* Record has changed */
60   }
61 
62 
63   /* Calculate and check all unique constraints */
64   key_changed=0;
65   for (i=0 ; i < share->state.header.uniques ; i++)
66   {
67     MI_UNIQUEDEF *def=share->uniqueinfo+i;
68     if (mi_unique_comp(def, newrec, oldrec,1) &&
69 	mi_check_unique(info, def, newrec, mi_unique_hash(def, newrec),
70 			info->lastpos))
71     {
72       save_errno=my_errno;
73       goto err_end;
74     }
75   }
76   if (_mi_mark_file_changed(info))
77   {
78     save_errno=my_errno;
79     goto err_end;
80   }
81 
82   /* Check which keys changed from the original row */
83 
84   new_key=info->lastkey2;
85   changed=0;
86   for (i=0 ; i < share->base.keys ; i++)
87   {
88     if (mi_is_key_active(share->state.key_map, i))
89     {
90       if (share->keyinfo[i].flag & HA_FULLTEXT )
91       {
92 	if (_mi_ft_cmp(info,i,oldrec, newrec))
93 	{
94 	  if ((int) i == info->lastinx)
95 	  {
96 	  /*
97 	    We are changeing the index we are reading on.  Mark that
98 	    the index data has changed and we need to do a full search
99 	    when doing read-next
100 	  */
101 	    key_changed|=HA_STATE_WRITTEN;
102 	  }
103 	  changed|=((ulonglong) 1 << i);
104 	  if (_mi_ft_update(info,i, old_key,oldrec,newrec,pos))
105 	    goto err;
106 	}
107       }
108       else
109       {
110 	uint new_length=_mi_make_key(info,i,new_key,newrec,pos);
111 	uint old_length=_mi_make_key(info,i,old_key,oldrec,pos);
112 
113         /* The above changed info->lastkey2. Inform mi_rnext_same(). */
114         info->update&= ~HA_STATE_RNEXT_SAME;
115 
116 	if (new_length != old_length ||
117 	    memcmp((uchar*) old_key,(uchar*) new_key,new_length))
118 	{
119 	  if ((int) i == info->lastinx)
120 	    key_changed|=HA_STATE_WRITTEN;	/* Mark that keyfile changed */
121 	  changed|=((ulonglong) 1 << i);
122 	  share->keyinfo[i].version++;
123 	  if (share->keyinfo[i].ck_delete(info,i,old_key,old_length)) goto err;
124 	  if (share->keyinfo[i].ck_insert(info,i,new_key,new_length)) goto err;
125 	  if (share->base.auto_key == i+1)
126 	    auto_key_changed=1;
127 	}
128       }
129     }
130   }
131   /*
132     If we are running with external locking, we must update the index file
133     that something has changed.
134   */
135   if (changed || !my_disable_locking)
136     key_changed|= HA_STATE_CHANGED;
137 
138   if (share->calc_checksum)
139   {
140     info->checksum=(*share->calc_checksum)(info,newrec);
141     /* Store new checksum in index file header */
142     key_changed|= HA_STATE_CHANGED;
143   }
144   {
145     /*
146       Don't update index file if data file is not extended and no status
147       information changed
148     */
149     MI_STATUS_INFO state;
150     ha_rows org_split;
151     my_off_t org_delete_link;
152 
153     memcpy((char*) &state, (char*) info->state, sizeof(state));
154     org_split=	     share->state.split;
155     org_delete_link= share->state.dellink;
156     if ((*share->update_record)(info,pos,newrec))
157       goto err;
158     if (!key_changed &&
159 	(memcmp((char*) &state, (char*) info->state, sizeof(state)) ||
160 	 org_split != share->state.split ||
161 	 org_delete_link != share->state.dellink))
162       key_changed|= HA_STATE_CHANGED;		/* Must update index file */
163   }
164   if (auto_key_changed)
165     set_if_bigger(info->s->state.auto_increment,
166                   retrieve_auto_increment(info, newrec));
167   if (share->calc_checksum)
168     info->state->checksum+=(info->checksum - old_checksum);
169 
170   info->update= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED | HA_STATE_AKTIV |
171 		 key_changed);
172   myisam_log_record(MI_LOG_UPDATE,info,newrec,info->lastpos,0);
173   /*
174     Every myisam function that updates myisam table must end with
175     call to _mi_writeinfo(). If operation (second param of
176     _mi_writeinfo()) is not 0 it sets share->changed to 1, that is
177     flags that data has changed. If operation is 0, this function
178     equals to no-op in this case.
179 
180     mi_update() must always pass !0 value as operation, since even if
181     there is no index change there could be data change.
182   */
183   (void) _mi_writeinfo(info, WRITEINFO_UPDATE_KEYFILE);
184   if (info->invalidator != 0)
185   {
186     DBUG_PRINT("info", ("invalidator... '%s' (update)", info->filename));
187     (*info->invalidator)(info->filename);
188     info->invalidator=0;
189   }
190   DBUG_RETURN(0);
191 
192 err:
193   DBUG_PRINT("error",("key: %d  errno: %d",i,my_errno));
194   save_errno=my_errno;
195   if (changed)
196     key_changed|= HA_STATE_CHANGED;
197   if (my_errno == HA_ERR_FOUND_DUPP_KEY || my_errno == HA_ERR_RECORD_FILE_FULL ||
198       my_errno == HA_ERR_NULL_IN_SPATIAL || my_errno == HA_ERR_OUT_OF_MEM)
199   {
200     info->errkey= (int) i;
201     flag=0;
202     do
203     {
204       if (((ulonglong) 1 << i) & changed)
205       {
206 	if (share->keyinfo[i].flag & HA_FULLTEXT)
207 	{
208 	  if ((flag++ && _mi_ft_del(info,i, new_key,newrec,pos)) ||
209 	      _mi_ft_add(info,i, old_key,oldrec,pos))
210 	    break;
211 	}
212 	else
213 	{
214 	  uint new_length=_mi_make_key(info,i,new_key,newrec,pos);
215 	  uint old_length= _mi_make_key(info,i,old_key,oldrec,pos);
216 	  if ((flag++ &&
217 	       share->keyinfo[i].ck_delete(info, i, new_key, new_length)) ||
218 	      share->keyinfo[i].ck_insert(info, i, old_key, old_length))
219 	    break;
220 	}
221       }
222     } while (i-- != 0);
223   }
224   else
225   {
226     mi_print_error(info->s, HA_ERR_CRASHED);
227     mi_mark_crashed(info);
228   }
229   info->update= (HA_STATE_CHANGED | HA_STATE_AKTIV | HA_STATE_ROW_CHANGED |
230 		 key_changed);
231 
232  err_end:
233   myisam_log_record(MI_LOG_UPDATE,info,newrec,info->lastpos,my_errno);
234   (void) _mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
235   if (save_errno == HA_ERR_KEY_NOT_FOUND)
236   {
237     mi_print_error(info->s, HA_ERR_CRASHED);
238     save_errno=HA_ERR_CRASHED;
239   }
240   DBUG_RETURN(my_errno=save_errno);
241 } /* mi_update */
242