1 /* Copyright (C) 2006 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; version 2 of the License.
6 
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11 
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
15 
16 /*
17   Functions for read record cacheing with maria
18   Used for reading dynamic/compressed records from datafile.
19 
20   Can fetch data directly from file (outside cache),
21   if reading a small chunk straight before the cached part (with possible
22   overlap).
23 
24   Can be explicitly asked not to use cache (by not setting READING_NEXT in
25   flag) - useful for occasional out-of-cache reads, when the next read is
26   expected to hit the cache again.
27 
28   Allows "partial read" errors in the record header (when READING_HEADER flag
29   is set) - unread part is bzero'ed
30 
31   Note: out-of-cache reads are enabled for shared IO_CACHE's too,
32   as these reads will be cached by OS cache (and my_pread is always atomic)
33 */
34 
35 
36 #include "maria_def.h"
37 
_ma_read_cache(MARIA_HA * handler,IO_CACHE * info,uchar * buff,my_off_t pos,size_t length,uint flag)38 my_bool _ma_read_cache(MARIA_HA *handler, IO_CACHE *info, uchar *buff,
39                        my_off_t pos, size_t length, uint flag)
40 {
41   size_t read_length,in_buff_length;
42   my_off_t offset;
43   uchar *in_buff_pos;
44   DBUG_ENTER("_ma_read_cache");
45   DBUG_ASSERT(!(info->myflags & MY_ENCRYPT));
46 
47   if (pos < info->pos_in_file)
48   {
49     read_length=length;
50     if ((my_off_t) read_length > (my_off_t) (info->pos_in_file-pos))
51       read_length=(uint) (info->pos_in_file-pos);
52     info->seek_not_done=1;
53     if (mysql_file_pread(info->file,buff,read_length,pos,MYF(MY_NABP)))
54       DBUG_RETURN(1);
55     if (!(length-=read_length))
56       DBUG_RETURN(0);
57     pos+=read_length;
58     buff+=read_length;
59   }
60   if (pos >= info->pos_in_file &&
61       (offset= (my_off_t) (pos - info->pos_in_file)) <
62       (my_off_t) (info->read_end - info->request_pos))
63   {
64     in_buff_pos=info->request_pos+(uint) offset;
65     in_buff_length= MY_MIN(length,(size_t) (info->read_end-in_buff_pos));
66     memcpy(buff,info->request_pos+(uint) offset,(size_t) in_buff_length);
67     if (!(length-=in_buff_length))
68       DBUG_RETURN(0);
69     pos+=in_buff_length;
70     buff+=in_buff_length;
71   }
72   else
73     in_buff_length=0;
74   if (flag & READING_NEXT)
75   {
76     if (pos != (info->pos_in_file +
77 		(uint) (info->read_end - info->request_pos)))
78     {
79       info->pos_in_file=pos;				/* Force start here */
80       info->read_pos=info->read_end=info->request_pos;	/* Everything used */
81       info->seek_not_done=1;
82     }
83     else
84       info->read_pos=info->read_end;			/* All block used */
85     if (!_my_b_read(info,buff,length))
86       DBUG_RETURN(0);
87     read_length=info->error;
88   }
89   else
90   {
91     info->seek_not_done=1;
92     if ((read_length=mysql_file_pread(info->file,buff,length,pos,MYF(0))) == length)
93       DBUG_RETURN(0);
94   }
95   if (!(flag & READING_HEADER) || (int) read_length == -1 ||
96       read_length+in_buff_length < 3)
97   {
98     DBUG_PRINT("error",
99                ("Error %d reading next-multi-part block (Got %d bytes)",
100                 my_errno, (int) read_length));
101     if (!my_errno || my_errno == HA_ERR_FILE_TOO_SHORT)
102     {
103       if (!handler->in_check_table)
104         _ma_set_fatal_error(handler->s, HA_ERR_WRONG_IN_RECORD);
105       else
106         my_errno= HA_ERR_WRONG_IN_RECORD;
107     }
108     DBUG_RETURN(1);
109   }
110   bzero(buff+read_length,MARIA_BLOCK_INFO_HEADER_LENGTH - in_buff_length -
111         read_length);
112   DBUG_RETURN(0);
113 } /* _ma_read_cache */
114