22 int mi_update(
register MI_INFO *info,
const uchar *oldrec, uchar *newrec)
24 int flag,key_changed,save_errno;
27 uchar old_key[MI_MAX_KEY_BUFF],*new_key;
28 my_bool auto_key_changed=0;
31 ha_checksum UNINIT_VAR(old_checksum);
32 DBUG_ENTER(
"mi_update");
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))
39 DBUG_RETURN(my_errno=HA_ERR_KEY_NOT_FOUND);
41 if (share->options & HA_OPTION_READ_ONLY_DATA)
43 DBUG_RETURN(my_errno=EACCES);
45 if (info->state->key_file_length >= share->base.margin_key_file_length)
47 DBUG_RETURN(my_errno=HA_ERR_INDEX_FILE_FULL);
50 if (_mi_readinfo(info,F_WRLCK,1))
51 DBUG_RETURN(my_errno);
53 if (share->calc_checksum)
54 old_checksum=info->checksum=(*share->calc_checksum)(info,oldrec);
55 if ((*share->compare_record)(info,oldrec))
64 for (i=0 ; i < share->state.header.uniques ; i++)
67 if (mi_unique_comp(def, newrec, oldrec,1) &&
68 mi_check_unique(info, def, newrec, mi_unique_hash(def, newrec),
75 if (_mi_mark_file_changed(info))
83 new_key=info->lastkey2;
85 for (i=0 ; i < share->base.keys ; i++)
87 if (mi_is_key_active(share->state.key_map, i))
89 if (share->keyinfo[i].flag & HA_FULLTEXT )
91 if (_mi_ft_cmp(info,i,oldrec, newrec))
93 if ((
int) i == info->lastinx)
100 key_changed|=HA_STATE_WRITTEN;
102 changed|=((ulonglong) 1 << i);
103 if (_mi_ft_update(info,i, old_key,oldrec,newrec,pos))
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);
113 info->update&= ~HA_STATE_RNEXT_SAME;
115 if (new_length != old_length ||
116 memcmp((uchar*) old_key,(uchar*) new_key,new_length))
118 if ((
int) i == info->lastinx)
119 key_changed|=HA_STATE_WRITTEN;
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)
134 if (changed || !my_disable_locking)
135 key_changed|= HA_STATE_CHANGED;
137 if (share->calc_checksum)
139 info->checksum=(*share->calc_checksum)(info,newrec);
141 key_changed|= HA_STATE_CHANGED;
150 my_off_t org_delete_link;
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))
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;
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);
169 info->update= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED | HA_STATE_AKTIV |
171 myisam_log_record(MI_LOG_UPDATE,info,newrec,info->lastpos,0);
182 (void) _mi_writeinfo(info, WRITEINFO_UPDATE_KEYFILE);
183 if (info->invalidator != 0)
185 DBUG_PRINT(
"info", (
"invalidator... '%s' (update)", info->filename));
186 (*info->invalidator)(info->filename);
192 DBUG_PRINT(
"error",(
"key: %d errno: %d",i,my_errno));
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)
199 info->errkey= (int) i;
203 if (((ulonglong) 1 << i) & changed)
205 if (share->keyinfo[i].flag & HA_FULLTEXT)
207 if ((flag++ && _mi_ft_del(info,i, new_key,newrec,pos)) ||
208 _mi_ft_add(info,i, old_key,oldrec,pos))
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);
216 share->keyinfo[i].ck_delete(info, i, new_key, new_length)) ||
217 share->keyinfo[i].ck_insert(info, i, old_key, old_length))
225 mi_print_error(info->s, HA_ERR_CRASHED);
226 mi_mark_crashed(info);
228 info->update= (HA_STATE_CHANGED | HA_STATE_AKTIV | HA_STATE_ROW_CHANGED |
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)
236 mi_print_error(info->s, HA_ERR_CRASHED);
237 save_errno=HA_ERR_CRASHED;
239 DBUG_RETURN(my_errno=save_errno);