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

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. /* Functions to handle fixed-length-records */
  17. #include "isamdef.h"
  18. int _nisam_write_static_record(N_INFO *info, const byte *record)
  19. {
  20.   uchar temp[4]; /* Not sizeof(long) */
  21.   if (info->s->state.dellink != NI_POS_ERROR)
  22.   {
  23.     ulong filepos=info->s->state.dellink;
  24.     info->rec_cache.seek_not_done=1; /* We have done a seek */
  25.     VOID(my_seek(info->dfile,info->s->state.dellink+1,MY_SEEK_SET,MYF(0)));
  26.     if (my_read(info->dfile,(char*) &temp[0],sizeof(temp), MYF(MY_NABP)))
  27.       goto err;
  28.     info->s->state.dellink=uint4korr(temp);
  29.     if (info->s->state.dellink == (uint32) ~0) /* Fix for 64 bit long */
  30.       info->s->state.dellink=NI_POS_ERROR;
  31.     info->s->state.del--;
  32.     info->s->state.empty-=info->s->base.reclength;
  33.     VOID(my_seek(info->dfile,filepos,MY_SEEK_SET,MYF(0)));
  34.     if (my_write(info->dfile, (char*) record, info->s->base.reclength,
  35.  MYF(MY_NABP)))
  36.       goto err;
  37.   }
  38.   else
  39.   {
  40.     if (info->s->state.data_file_length > info->s->base.max_data_file_length)
  41.     {
  42.       my_errno=HA_ERR_RECORD_FILE_FULL;
  43.       return(2);
  44.     }
  45.     if (info->opt_flag & WRITE_CACHE_USED)
  46.     { /* Cash in use */
  47.       if (my_b_write(&info->rec_cache, (byte*) record, info->s->base.reclength))
  48. goto err;
  49.     }
  50.     else
  51.     {
  52.       info->rec_cache.seek_not_done=1; /* We have done a seek */
  53.       VOID(my_seek(info->dfile,info->s->state.data_file_length,
  54.    MY_SEEK_SET,MYF(0)));
  55.       if (my_write(info->dfile,(char*) record,info->s->base.reclength,
  56.    MYF(MY_NABP | MY_WAIT_IF_FULL)))
  57. goto err;
  58.     }
  59.     info->s->state.data_file_length+=info->s->base.reclength;
  60.     info->s->state.splitt++;
  61.   }
  62.   return 0;
  63.  err:
  64.   return 1;
  65. }
  66. int _nisam_update_static_record(N_INFO *info, ulong pos, const byte *record)
  67. {
  68.   info->rec_cache.seek_not_done=1; /* We have done a seek */
  69.   VOID(my_seek(info->dfile,pos,MY_SEEK_SET,MYF(0)));
  70.   return (my_write(info->dfile,(char*) record,info->s->base.reclength,
  71.    MYF(MY_NABP)) != 0);
  72. }
  73. int _nisam_delete_static_record(N_INFO *info)
  74. {
  75.   uchar temp[5]; /* 1+sizeof(uint32) */
  76.   info->s->state.del++;
  77.   info->s->state.empty+=info->s->base.reclength;
  78.   temp[0]= ''; /* Mark that record is deleted */
  79.   int4store(temp+1,info->s->state.dellink);
  80.   info->s->state.dellink = info->lastpos;
  81.   info->rec_cache.seek_not_done=1;
  82.   VOID(my_seek(info->dfile,info->lastpos,MY_SEEK_SET,MYF(0)));
  83.   return (my_write(info->dfile,(byte*) temp,(uint) sizeof(temp),
  84.    MYF(MY_NABP)) != 0);
  85. }
  86. int _nisam_cmp_static_record(register N_INFO *info, register const byte *old)
  87. {
  88.   DBUG_ENTER("_nisam_rectest");
  89.   /* We are going to do changes; dont let anybody disturb */
  90.   dont_break(); /* Dont allow SIGHUP or SIGINT */
  91.   if (info->opt_flag & WRITE_CACHE_USED)
  92.   {
  93.     if (flush_io_cache(&info->rec_cache))
  94.     {
  95.       DBUG_RETURN(-1);
  96.     }
  97.     info->rec_cache.seek_not_done=1; /* We have done a seek */
  98.   }
  99.   if ((info->opt_flag & READ_CHECK_USED))
  100.   { /* If check isn't disabled  */
  101.     info->rec_cache.seek_not_done=1; /* We have done a seek */
  102.     VOID(my_seek(info->dfile,info->lastpos,MY_SEEK_SET,MYF(0)));
  103.     if (my_read(info->dfile, (char*) info->rec_buff, info->s->base.reclength,
  104. MYF(MY_NABP)))
  105.       DBUG_RETURN(-1);
  106.     if (memcmp((byte*) info->rec_buff, (byte*) old,
  107.        (uint) info->s->base.reclength))
  108.     {
  109.       DBUG_DUMP("read",old,info->s->base.reclength);
  110.       DBUG_DUMP("disk",info->rec_buff,info->s->base.reclength);
  111.       my_errno=HA_ERR_RECORD_CHANGED; /* Record have changed */
  112.       DBUG_RETURN(1);
  113.     }
  114.   }
  115.   DBUG_RETURN(0);
  116. }
  117. /* Read a fixed-length-record */
  118. /* Returns 0 if Ok. */
  119. /*    1 if record is deleted */
  120. /*   MY_FILE_ERROR on read-error or locking-error */
  121. int _nisam_read_static_record(register N_INFO *info, register ulong pos,
  122.    register byte *record)
  123. {
  124.   int error;
  125.   if (pos != NI_POS_ERROR)
  126.   {
  127.     if (info->opt_flag & WRITE_CACHE_USED &&
  128. info->rec_cache.pos_in_file <= pos &&
  129. flush_io_cache(&info->rec_cache))
  130.       return(-1);
  131.     info->rec_cache.seek_not_done=1; /* We have done a seek */
  132.     error=my_pread(info->dfile,(char*) record,info->s->base.reclength,
  133.    pos,MYF(MY_NABP)) != 0;
  134.     if (info->s->r_locks == 0 && info->s->w_locks == 0)
  135.       VOID(_nisam_writeinfo(info,0));
  136.     if (! error)
  137.     {
  138.       if (!*record) return(1); /* Record is deleted */
  139.       info->update|= HA_STATE_AKTIV; /* Record is read */
  140.       my_errno=HA_ERR_RECORD_DELETED;
  141.       return(0);
  142.     }
  143.     return(-1); /* Error on read */
  144.   }
  145.   VOID(_nisam_writeinfo(info,0)); /* No such record */
  146.   return(-1);
  147. } /* _nisam_read_record */
  148. int _nisam_read_rnd_static_record(N_INFO *info, byte *buf,
  149.   register ulong filepos,
  150.   int skipp_deleted_blocks)
  151. {
  152.   int locked,error,cache_read;
  153.   uint cache_length;
  154.   ISAM_SHARE *share=info->s;
  155.   DBUG_ENTER("_nisam_read_rnd_static_record");
  156.   cache_read=0;
  157.   LINT_INIT(cache_length);
  158.   if (info->opt_flag & WRITE_CACHE_USED &&
  159.       (info->rec_cache.pos_in_file <= filepos || skipp_deleted_blocks) &&
  160.       flush_io_cache(&info->rec_cache))
  161.     DBUG_RETURN(-1);
  162.   if (info->opt_flag & READ_CACHE_USED)
  163.   { /* Cash in use */
  164.     if (filepos == my_b_tell(&info->rec_cache) &&
  165. (skipp_deleted_blocks || !filepos))
  166.     {
  167.       cache_read=1; /* Read record using cache */
  168.       cache_length=(uint) (info->rec_cache.rc_end - info->rec_cache.rc_pos);
  169.     }
  170.     else
  171.       info->rec_cache.seek_not_done=1; /* Filepos is changed */
  172.   }
  173. #ifndef NO_LOCKING
  174.   locked=0;
  175.   if (info->lock_type == F_UNLCK)
  176.   {
  177.     if (filepos >= share->state.data_file_length)
  178.     { /* Test if new records */
  179.       if (_nisam_readinfo(info,F_RDLCK,0))
  180. DBUG_RETURN(-1);
  181.       locked=1;
  182.     }
  183.     else
  184.     { /* We don't nead new info */
  185. #ifndef UNSAFE_LOCKING
  186.       if ((! cache_read || share->base.reclength > cache_length) &&
  187.   share->r_locks == 0 && share->w_locks == 0)
  188.       { /* record not in cache */
  189. if (my_lock(share->kfile,F_RDLCK,0L,F_TO_EOF,
  190.     MYF(MY_SEEK_NOT_DONE) | info->lock_wait))
  191.   DBUG_RETURN(-1);
  192. locked=1;
  193.       }
  194. #else
  195.       info->tmp_lock_type=F_RDLCK;
  196. #endif
  197.     }
  198.   }
  199. #endif
  200.   if (filepos >= share->state.data_file_length)
  201.   {
  202. #ifndef NO_LOCKING
  203.     DBUG_PRINT("test",("filepos: %ld (%ld)  records: %ld  del: %ld",
  204.        filepos/share->base.reclength,filepos,
  205.        share->state.records, share->state.del));
  206.     VOID(_nisam_writeinfo(info,0));
  207. #endif
  208.     my_errno=HA_ERR_END_OF_FILE;
  209.     DBUG_RETURN(-1);
  210.   }
  211.   info->lastpos= filepos;
  212.   info->nextpos= filepos+share->base.reclength;
  213.   if (! cache_read) /* No cacheing */
  214.   {
  215.     error=_nisam_read_static_record(info,filepos,buf);
  216.     if (error > 0)
  217.       my_errno=HA_ERR_RECORD_DELETED;
  218.     DBUG_RETURN(error);
  219.   }
  220. /* Read record with cacheing */
  221.   error=my_b_read(&info->rec_cache,(byte*) buf,share->base.reclength);
  222. #ifndef NO_LOCKING
  223.   if (locked)
  224.     VOID(_nisam_writeinfo(info,0)); /* Unlock keyfile */
  225. #endif
  226.   if (!error)
  227.   {
  228.     if (!buf[0])
  229.     { /* Record is removed */
  230.       my_errno=HA_ERR_RECORD_DELETED;
  231.       DBUG_RETURN(1);
  232.     }
  233. /* Found and may be updated */
  234.     info->update|= HA_STATE_AKTIV | HA_STATE_KEY_CHANGED;
  235.     DBUG_RETURN(0);
  236.   }
  237.   if (info->rec_cache.error != -1 || my_errno == 0)
  238.     my_errno=HA_ERR_WRONG_IN_RECORD;
  239.   DBUG_RETURN(-1); /* Something wrong (EOF?) */
  240. }