update.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:4k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult 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; either version 2 of the License, or
  6.    (at your option) any later version.
  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  16. /* Uppdaterare nuvarande record i en pisam-databas */
  17. #include "isamdef.h"
  18. #ifdef __WIN__
  19. #include <errno.h>
  20. #endif
  21. /* Updaterar senaste l{sta record i databasen */
  22. int nisam_update(register N_INFO *info, const byte *oldrec, const byte *newrec)
  23. {
  24.   int flag,key_changed,save_errno;
  25.   reg3 ulong pos;
  26.   uint i,length;
  27.   uchar old_key[N_MAX_KEY_BUFF],*new_key;
  28.   DBUG_ENTER("nisam_update");
  29.   LINT_INIT(save_errno);
  30.   if (!(info->update & HA_STATE_AKTIV))
  31.   {
  32.     my_errno=HA_ERR_KEY_NOT_FOUND;
  33.     DBUG_RETURN(-1);
  34.   }
  35.   if (info->s->base.options & HA_OPTION_READ_ONLY_DATA)
  36.   {
  37.     my_errno=EACCES;
  38.     DBUG_RETURN(-1);
  39.   }
  40.   pos=info->lastpos;
  41. #ifndef NO_LOCKING
  42.   if (_nisam_readinfo(info,F_WRLCK,1)) DBUG_RETURN(-1);
  43. #endif
  44.   if ((*info->s->compare_record)(info,oldrec))
  45.   {
  46.     save_errno=my_errno;
  47.     goto err_end; /* Record has changed */
  48.   }
  49.   if (info->s->state.key_file_length >=
  50.       info->s->base.max_key_file_length -
  51.       info->s->blocksize* INDEX_BLOCK_MARGIN *info->s->state.keys)
  52.   {
  53.     save_errno=HA_ERR_INDEX_FILE_FULL;
  54.     goto err_end;
  55.   }
  56. /* Flyttar de element i isamfilen som m}ste flyttas */
  57.   new_key=info->lastkey+info->s->base.max_key_length;
  58.   key_changed=HA_STATE_KEY_CHANGED; /* We changed current database */
  59. /* Remove key that didn't change */
  60.   for (i=0 ; i < info->s->state.keys ; i++)
  61.   {
  62.     length=_nisam_make_key(info,i,new_key,newrec,pos);
  63.     if (length != _nisam_make_key(info,i,old_key,oldrec,pos) ||
  64. memcmp((byte*) old_key,(byte*) new_key,length))
  65.     {
  66.       if ((int) i == info->lastinx)
  67. key_changed|=HA_STATE_WRITTEN; /* Mark that keyfile changed */
  68.       if (_nisam_ck_delete(info,i,old_key)) goto err;
  69.       if (_nisam_ck_write(info,i,new_key)) goto err;
  70.     }
  71.   }
  72.   if ((*info->s->update_record)(info,pos,newrec))
  73.     goto err;
  74.   info->update= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED | HA_STATE_AKTIV |
  75.  key_changed);
  76.   nisam_log_record(LOG_UPDATE,info,newrec,info->lastpos,0);
  77.   VOID(_nisam_writeinfo(info,test(key_changed)));
  78.   allow_break(); /* Allow SIGHUP & SIGINT */
  79.   DBUG_RETURN(0);
  80. err:
  81.   DBUG_PRINT("error",("key: %d  errno: %d",i,my_errno));
  82.   save_errno=my_errno;
  83.   if (my_errno == HA_ERR_FOUND_DUPP_KEY || my_errno == HA_ERR_RECORD_FILE_FULL)
  84.   {
  85.     info->errkey= (int) i;
  86.     flag=0;
  87.     do
  88.     {
  89.       length=_nisam_make_key(info,i,new_key,newrec,pos);
  90.       if (length != _nisam_make_key(info,i,old_key,oldrec,pos) ||
  91.   memcmp((byte*) old_key,(byte*) new_key,length))
  92.       {
  93. if ((flag++ && _nisam_ck_delete(info,i,new_key)) ||
  94.     _nisam_ck_write(info,i,old_key))
  95.   break;
  96.       }
  97.     } while (i-- != 0);
  98.   }
  99.   info->update= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED | HA_STATE_AKTIV |
  100.  key_changed);
  101.  err_end:
  102.   nisam_log_record(LOG_UPDATE,info,newrec,info->lastpos,save_errno);
  103.   VOID(_nisam_writeinfo(info,1));
  104.   allow_break(); /* Allow SIGHUP & SIGINT */
  105.   my_errno=(save_errno == HA_ERR_KEY_NOT_FOUND) ? HA_ERR_CRASHED : save_errno;
  106.   DBUG_RETURN(-1);
  107. } /* nisam_update */