home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 January / Chip_2001-01_cd1.bin / tema / mysql / mysql-3.23.28g-win-source.exe / myisam / mi_statrec.c < prev    next >
C/C++ Source or Header  |  2000-08-31  |  9KB  |  303 lines

  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.  
  17.     /* Functions to handle fixed-length-records */
  18.  
  19. #include "myisamdef.h"
  20.  
  21.  
  22. int _mi_write_static_record(MI_INFO *info, const byte *record)
  23. {
  24.   uchar temp[8];                /* max pointer length */
  25.  
  26.   if (info->s->state.dellink != HA_OFFSET_ERROR)
  27.   {
  28.     my_off_t filepos=info->s->state.dellink;
  29.     info->rec_cache.seek_not_done=1;        /* We have done a seek */
  30.     VOID(my_seek(info->dfile,info->s->state.dellink+1,MY_SEEK_SET,MYF(0)));
  31.  
  32.     if (my_read(info->dfile,(char*) &temp[0],info->s->base.rec_reflength,
  33.         MYF(MY_NABP)))
  34.       goto err;
  35.     info->s->state.dellink= _mi_rec_pos(info->s,temp);
  36.     info->state->del--;
  37.     info->state->empty-=info->s->base.pack_reclength;
  38.     VOID(my_seek(info->dfile,filepos,MY_SEEK_SET,MYF(0)));
  39.     if (my_write(info->dfile, (char*) record, info->s->base.reclength,
  40.          MYF(MY_NABP)))
  41.       goto err;
  42.   }
  43.   else
  44.   {
  45.     if (info->state->data_file_length > info->s->base.max_data_file_length-
  46.     info->s->base.pack_reclength)
  47.     {
  48.       my_errno=HA_ERR_RECORD_FILE_FULL;
  49.       return(2);
  50.     }
  51.     if (info->opt_flag & WRITE_CACHE_USED)
  52.     {                /* Cash in use */
  53.       if (my_b_write(&info->rec_cache, (byte*) record,
  54.              info->s->base.reclength))
  55.     goto err;
  56.       if (info->s->base.pack_reclength != info->s->base.reclength)
  57.       {
  58.     uint length=info->s->base.pack_reclength - info->s->base.reclength;
  59.     bzero((char*) temp,length);
  60.     if (my_b_write(&info->rec_cache, (byte*) temp,length))
  61.       goto err;
  62.       }
  63.     }
  64.     else
  65.     {
  66.       info->rec_cache.seek_not_done=1;        /* We have done a seek */
  67.       VOID(my_seek(info->dfile,info->state->data_file_length,
  68.            MY_SEEK_SET,MYF(0)));
  69.       if (my_write(info->dfile,(char*) record,info->s->base.reclength,
  70.            info->s->write_flag))
  71.     goto err;
  72.       if (info->s->base.pack_reclength != info->s->base.reclength)
  73.       {
  74.     uint length=info->s->base.pack_reclength - info->s->base.reclength;
  75.     bzero((char*) temp,length);
  76.     if (my_write(info->dfile, (byte*) temp,length, info->s->write_flag))
  77.       goto err;
  78.       }
  79.     }
  80.     info->state->data_file_length+=info->s->base.pack_reclength;
  81.     info->s->state.split++;
  82.   }
  83.   return 0;
  84.  err:
  85.   return 1;
  86. }
  87.  
  88. int _mi_update_static_record(MI_INFO *info, my_off_t pos, const byte *record)
  89. {
  90.   info->rec_cache.seek_not_done=1;        /* We have done a seek */
  91.   VOID(my_seek(info->dfile,pos,MY_SEEK_SET,MYF(0)));
  92.   return (my_write(info->dfile,(char*) record,info->s->base.reclength,
  93.            MYF(MY_NABP)) != 0);
  94. }
  95.  
  96.  
  97. int _mi_delete_static_record(MI_INFO *info)
  98. {
  99.   uchar temp[9];                /* 1+sizeof(uint32) */
  100.  
  101.   info->state->del++;
  102.   info->state->empty+=info->s->base.pack_reclength;
  103.   temp[0]= '\0';            /* Mark that record is deleted */
  104.   _mi_dpointer(info,temp+1,info->s->state.dellink);
  105.   info->s->state.dellink = info->lastpos;
  106.   info->rec_cache.seek_not_done=1;
  107.   VOID(my_seek(info->dfile,info->lastpos,MY_SEEK_SET,MYF(0)));
  108.   return (my_write(info->dfile,(byte*) temp, 1+info->s->rec_reflength,
  109.            MYF(MY_NABP)) != 0);
  110. }
  111.  
  112.  
  113. int _mi_cmp_static_record(register MI_INFO *info, register const byte *old)
  114. {
  115.   DBUG_ENTER("_mi_cmp_static_record");
  116.  
  117.   /* We are going to do changes; dont let anybody disturb */
  118.   dont_break();                /* Dont allow SIGHUP or SIGINT */
  119.  
  120.   if (info->opt_flag & WRITE_CACHE_USED)
  121.   {
  122.     if (flush_io_cache(&info->rec_cache))
  123.     {
  124.       DBUG_RETURN(-1);
  125.     }
  126.     info->rec_cache.seek_not_done=1;        /* We have done a seek */
  127.   }
  128.  
  129.   if ((info->opt_flag & READ_CHECK_USED))
  130.   {                        /* If check isn't disabled  */
  131.     info->rec_cache.seek_not_done=1;        /* We have done a seek */
  132.     VOID(my_seek(info->dfile,info->lastpos,MY_SEEK_SET,MYF(0)));
  133.     if (my_read(info->dfile, (char*) info->rec_buff, info->s->base.reclength,
  134.         MYF(MY_NABP)))
  135.       DBUG_RETURN(-1);
  136.     if (memcmp((byte*) info->rec_buff, (byte*) old,
  137.            (uint) info->s->base.reclength))
  138.     {
  139.       DBUG_DUMP("read",old,info->s->base.reclength);
  140.       DBUG_DUMP("disk",info->rec_buff,info->s->base.reclength);
  141.       my_errno=HA_ERR_RECORD_CHANGED;        /* Record have changed */
  142.       DBUG_RETURN(1);
  143.     }
  144.   }
  145.   DBUG_RETURN(0);
  146. }
  147.  
  148.  
  149. int _mi_cmp_static_unique(MI_INFO *info, MI_UNIQUEDEF *def,
  150.               const byte *record, my_off_t pos)
  151. {
  152.   DBUG_ENTER("_mi_cmp_static_unique");
  153.  
  154.   info->rec_cache.seek_not_done=1;        /* We have done a seek */
  155.   VOID(my_seek(info->dfile,pos,MY_SEEK_SET,MYF(0)));
  156.   if (my_read(info->dfile, (char*) info->rec_buff, info->s->base.reclength,
  157.           MYF(MY_NABP)))
  158.     DBUG_RETURN(-1);
  159.   DBUG_RETURN(mi_unique_comp(def, record, info->rec_buff,
  160.                  def->null_are_equal));
  161. }
  162.  
  163.  
  164.     /* Read a fixed-length-record */
  165.     /* Returns 0 if Ok. */
  166.     /*       1 if record is deleted */
  167.     /*      MY_FILE_ERROR on read-error or locking-error */
  168.  
  169. int _mi_read_static_record(register MI_INFO *info, register my_off_t pos,
  170.                register byte *record)
  171. {
  172.   int error;
  173.  
  174.   if (pos != HA_OFFSET_ERROR)
  175.   {
  176.     if (info->opt_flag & WRITE_CACHE_USED &&
  177.     info->rec_cache.pos_in_file <= pos &&
  178.     flush_io_cache(&info->rec_cache))
  179.       return(-1);
  180.     info->rec_cache.seek_not_done=1;        /* We have done a seek */
  181.  
  182.     error=my_pread(info->dfile,(char*) record,info->s->base.reclength,
  183.            pos,MYF(MY_NABP)) != 0;
  184.     if (info->s->r_locks == 0 && info->s->w_locks == 0)
  185.       VOID(_mi_writeinfo(info,0));
  186.     if (! error)
  187.     {
  188.       if (!*record)
  189.       {
  190.     my_errno=HA_ERR_RECORD_DELETED;
  191.     return(1);                /* Record is deleted */
  192.       }
  193.       info->update|= HA_STATE_AKTIV;        /* Record is read */
  194.       return(0);
  195.     }
  196.     return(-1);                    /* Error on read */
  197.   }
  198.   VOID(_mi_writeinfo(info,0));            /* No such record */
  199.   return(-1);
  200. }
  201.  
  202.  
  203.  
  204. int _mi_read_rnd_static_record(MI_INFO *info, byte *buf,
  205.                    register my_off_t filepos,
  206.                    my_bool skipp_deleted_blocks)
  207. {
  208.   int locked,error,cache_read;
  209.   uint cache_length;
  210.   MYISAM_SHARE *share=info->s;
  211.   DBUG_ENTER("_mi_read_rnd_static_record");
  212.  
  213.   cache_read=0;
  214.   cache_length=0;
  215.   if (info->opt_flag & WRITE_CACHE_USED &&
  216.       (info->rec_cache.pos_in_file <= filepos || skipp_deleted_blocks) &&
  217.       flush_io_cache(&info->rec_cache))
  218.     DBUG_RETURN(my_errno);
  219.   if (info->opt_flag & READ_CACHE_USED)
  220.   {                        /* Cache in use */
  221.     if (filepos == my_b_tell(&info->rec_cache) &&
  222.     (skipp_deleted_blocks || !filepos))
  223.     {
  224.       cache_read=1;                /* Read record using cache */
  225.       cache_length=(uint) (info->rec_cache.rc_end - info->rec_cache.rc_pos);
  226.     }
  227.     else
  228.       info->rec_cache.seek_not_done=1;        /* Filepos is changed */
  229.   }
  230.   locked=0;
  231.   if (info->lock_type == F_UNLCK)
  232.   {
  233.     if (filepos >= info->state->data_file_length)
  234.     {                        /* Test if new records */
  235.       if (_mi_readinfo(info,F_RDLCK,0))
  236.     DBUG_RETURN(my_errno);
  237.       locked=1;
  238.     }
  239.     else
  240.     {                        /* We don't nead new info */
  241. #ifndef UNSAFE_LOCKING
  242.       if ((! cache_read || share->base.reclength > cache_length) &&
  243.       share->r_locks == 0 && share->w_locks == 0)
  244.       {                        /* record not in cache */
  245.     if (my_lock(share->kfile,F_RDLCK,0L,F_TO_EOF,
  246.             MYF(MY_SEEK_NOT_DONE) | info->lock_wait))
  247.       DBUG_RETURN(my_errno);
  248.     locked=1;
  249.       }
  250. #else
  251.       info->tmp_lock_type=F_RDLCK;
  252. #endif
  253.     }
  254.   }
  255.   if (filepos >= info->state->data_file_length)
  256.   {
  257.     DBUG_PRINT("test",("filepos: %ld (%ld)  records: %ld  del: %ld",
  258.                filepos/share->base.reclength,filepos,
  259.                info->state->records, info->state->del));
  260.     VOID(_mi_writeinfo(info,0));
  261.     DBUG_RETURN(my_errno=HA_ERR_END_OF_FILE);
  262.   }
  263.   info->lastpos= filepos;
  264.   info->nextpos= filepos+share->base.pack_reclength;
  265.  
  266.   if (! cache_read)            /* No cacheing */
  267.   {
  268.     if ((error=_mi_read_static_record(info,filepos,buf)))
  269.     {
  270.       if (error > 0)
  271.     error=my_errno=HA_ERR_RECORD_DELETED;
  272.       else
  273.     error=my_errno;
  274.     }
  275.     DBUG_RETURN(error);
  276.   }
  277.  
  278.     /* Read record with cacheing */
  279.   error=my_b_read(&info->rec_cache,(byte*) buf,share->base.reclength);
  280.   if (info->s->base.pack_reclength != info->s->base.reclength && !error)
  281.   {
  282.     char tmp[8];                /* Skill fill bytes */
  283.     error=my_b_read(&info->rec_cache,(byte*) tmp,
  284.             info->s->base.pack_reclength - info->s->base.reclength);
  285.   }
  286.   if (locked)
  287.     VOID(_mi_writeinfo(info,0));        /* Unlock keyfile */
  288.   if (!error)
  289.   {
  290.     if (!buf[0])
  291.     {                        /* Record is removed */
  292.       DBUG_RETURN(my_errno=HA_ERR_RECORD_DELETED);
  293.     }
  294.                         /* Found and may be updated */
  295.     info->update|= HA_STATE_AKTIV | HA_STATE_KEY_CHANGED;
  296.     DBUG_RETURN(0);
  297.   }
  298.   /* my_errno should be set if rec_cache.error == -1 */
  299.   if (info->rec_cache.error != -1 || my_errno == 0)
  300.     my_errno=HA_ERR_WRONG_IN_RECORD;
  301.   DBUG_RETURN(my_errno);            /* Something wrong (EOF?) */
  302. }
  303.