1 | /* Copyright (c) 2000-2003, 2005-2007 MySQL AB, 2009 Sun Microsystems, Inc. |
2 | Use is subject to license terms. |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; version 2 of the License. |
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
16 | |
17 | /* |
18 | Functions for read record cacheing with myisam |
19 | Used for reading dynamic/compressed records from datafile. |
20 | |
21 | Can fetch data directly from file (outside cache), |
22 | if reading a small chunk straight before the cached part (with possible |
23 | overlap). |
24 | |
25 | Can be explicitly asked not to use cache (by not setting READING_NEXT in |
26 | flag) - useful for occasional out-of-cache reads, when the next read is |
27 | expected to hit the cache again. |
28 | |
29 | Allows "partial read" errors in the record header (when READING_HEADER flag |
30 | is set) - unread part is bzero'ed |
31 | |
32 | Note: out-of-cache reads are enabled for shared IO_CACHE's too, |
33 | as these reads will be cached by OS cache (and mysql_file_pread is always atomic) |
34 | */ |
35 | |
36 | #include "myisamdef.h" |
37 | |
38 | int _mi_read_cache(IO_CACHE *info, uchar *buff, my_off_t pos, size_t length, |
39 | int flag) |
40 | { |
41 | size_t read_length,in_buff_length; |
42 | my_off_t offset; |
43 | uchar *in_buff_pos; |
44 | DBUG_ENTER("_mi_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=(size_t)(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, 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, |
93 | MYF(0))) == length) |
94 | DBUG_RETURN(0); |
95 | } |
96 | if (!(flag & READING_HEADER) || (int) read_length == -1 || |
97 | read_length+in_buff_length < 3) |
98 | { |
99 | DBUG_PRINT("error" , |
100 | ("Error %d reading next-multi-part block (Got %d bytes)" , |
101 | my_errno, (int) read_length)); |
102 | if (!my_errno || my_errno == -1 || my_errno == HA_ERR_FILE_TOO_SHORT) |
103 | my_errno= HA_ERR_WRONG_IN_RECORD; |
104 | DBUG_RETURN(1); |
105 | } |
106 | bzero(buff+read_length,MI_BLOCK_INFO_HEADER_LENGTH - in_buff_length - |
107 | read_length); |
108 | DBUG_RETURN(0); |
109 | } /* _mi_read_cache */ |
110 | |