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