home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 January / Chip_2001-01_cd1.bin / tema / mysql / mysql-3.23.28g-win-source.exe / isam / _cache.c next >
C/C++ Source or Header  |  2000-08-31  |  3KB  |  93 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 for read record cacheing with nisam */
  18. /* Used instead of my_b_read() to allow for no-cacheed seeks */
  19.  
  20. #include "isamdef.h"
  21.  
  22. #define READING_NEXT    1
  23. #define READING_HEADER    2
  24.  
  25.     /* Copy block from cache if it`s in it. If re_read_if_possibly is */
  26.     /* set read to cache (if after current file-position) else read to */
  27.     /* buff                                  */
  28.  
  29. int _nisam_read_cache(IO_CACHE *info, byte *buff, ulong pos, uint length,
  30.               int flag)
  31. {
  32.   uint read_length,in_buff_length;
  33.   ulong offset;
  34.   char *in_buff_pos;
  35.  
  36.   if (pos < info->pos_in_file)
  37.   {
  38.     read_length= (uint) min((ulong) length,(ulong) (info->pos_in_file-pos));
  39.     info->seek_not_done=1;
  40.     VOID(my_seek(info->file,pos,MY_SEEK_SET,MYF(0)));
  41.     if (my_read(info->file,buff,read_length,MYF(MY_NABP)))
  42.       return 1;
  43.     if (!(length-=read_length))
  44.       return 0;
  45.     pos+=read_length;
  46.     buff+=read_length;
  47.   }
  48.   if ((offset=pos - (ulong) info->pos_in_file) <
  49.       (ulong) (info->rc_end - info->rc_request_pos))
  50.   {
  51.     in_buff_pos=info->rc_request_pos+(uint) offset;
  52.     in_buff_length= min(length,(uint) (info->rc_end-in_buff_pos));
  53.     memcpy(buff,info->rc_request_pos+(uint) offset,(size_t) in_buff_length);
  54.     if (!(length-=in_buff_length))
  55.       return 0;
  56.     pos+=in_buff_length;
  57.     buff+=in_buff_length;
  58.   }
  59.   else
  60.     in_buff_length=0;
  61.   if (flag & READING_NEXT)
  62.   {
  63.     if (pos != ((info)->pos_in_file +
  64.         (uint) ((info)->rc_end - (info)->rc_request_pos)))
  65.     {
  66.       info->pos_in_file=pos;                /* Force start here */
  67.       info->rc_pos=info->rc_end=info->rc_request_pos;    /* Everything used */
  68.       info->seek_not_done=1;
  69.     }
  70.     else
  71.       info->rc_pos=info->rc_end;            /* All block used */
  72.     if (!(*info->read_function)(info,buff,length))
  73.       return 0;
  74.     if (!(flag & READING_HEADER) || info->error == -1 ||
  75.     (uint) info->error+in_buff_length < 3)
  76.       return 1;
  77.     if (BLOCK_INFO_HEADER_LENGTH < in_buff_length + (uint) info->error)
  78.       bzero(buff+info->error,BLOCK_INFO_HEADER_LENGTH - in_buff_length -
  79.         (uint) info->error);
  80.     return 0;
  81.   }
  82.   info->seek_not_done=1;
  83.   VOID(my_seek(info->file,pos,MY_SEEK_SET,MYF(0)));
  84.   if ((read_length=my_read(info->file,buff,length,MYF(0))) == length)
  85.     return 0;
  86.   if (!(flag & READING_HEADER) || (int) read_length == -1 ||
  87.       read_length+in_buff_length < 3)
  88.     return 1;
  89.   bzero(buff+read_length,BLOCK_INFO_HEADER_LENGTH - in_buff_length -
  90.     read_length);
  91.   return 0;
  92. } /* _nisam_read_cache */
  93.