~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/myisam/mi_cache.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000-2003 MySQL 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
/*
 
17
  Functions for read record cacheing with myisam
 
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 "myisamdef.h"
 
37
 
 
38
int _mi_read_cache(IO_CACHE *info, uchar *buff, my_off_t pos, uint length,
 
39
                   int flag)
 
40
{
 
41
  uint read_length,in_buff_length;
 
42
  my_off_t offset;
 
43
  uchar *in_buff_pos;
 
44
  DBUG_ENTER("_mi_read_cache");
 
45
 
 
46
  if (pos < info->pos_in_file)
 
47
  {
 
48
    read_length=length;
 
49
    if ((my_off_t) read_length > (my_off_t) (info->pos_in_file-pos))
 
50
      read_length=(uint) (info->pos_in_file-pos);
 
51
    info->seek_not_done=1;
 
52
    if (my_pread(info->file,buff,read_length,pos,MYF(MY_NABP)))
 
53
      DBUG_RETURN(1);
 
54
    if (!(length-=read_length))
 
55
      DBUG_RETURN(0);
 
56
    pos+=read_length;
 
57
    buff+=read_length;
 
58
  }
 
59
  if (pos >= info->pos_in_file &&
 
60
      (offset= (my_off_t) (pos - info->pos_in_file)) <
 
61
      (my_off_t) (info->read_end - info->request_pos))
 
62
  {
 
63
    in_buff_pos=info->request_pos+(uint) offset;
 
64
    in_buff_length= min(length, (size_t) (info->read_end-in_buff_pos));
 
65
    memcpy(buff,info->request_pos+(uint) offset,(size_t) in_buff_length);
 
66
    if (!(length-=in_buff_length))
 
67
      DBUG_RETURN(0);
 
68
    pos+=in_buff_length;
 
69
    buff+=in_buff_length;
 
70
  }
 
71
  else
 
72
    in_buff_length=0;
 
73
  if (flag & READING_NEXT)
 
74
  {
 
75
    if (pos != (info->pos_in_file +
 
76
                (uint) (info->read_end - info->request_pos)))
 
77
    {
 
78
      info->pos_in_file=pos;                            /* Force start here */
 
79
      info->read_pos=info->read_end=info->request_pos;  /* Everything used */
 
80
      info->seek_not_done=1;
 
81
    }
 
82
    else
 
83
      info->read_pos=info->read_end;                    /* All block used */
 
84
    if (!(*info->read_function)(info,buff,length))
 
85
      DBUG_RETURN(0);
 
86
    read_length=info->error;
 
87
  }
 
88
  else
 
89
  {
 
90
    info->seek_not_done=1;
 
91
    if ((read_length=my_pread(info->file,buff,length,pos,MYF(0))) == length)
 
92
      DBUG_RETURN(0);
 
93
  }
 
94
  if (!(flag & READING_HEADER) || (int) read_length == -1 ||
 
95
      read_length+in_buff_length < 3)
 
96
  {
 
97
    DBUG_PRINT("error",
 
98
               ("Error %d reading next-multi-part block (Got %d bytes)",
 
99
                my_errno, (int) read_length));
 
100
    if (!my_errno || my_errno == -1)
 
101
      my_errno=HA_ERR_WRONG_IN_RECORD;
 
102
    DBUG_RETURN(1);
 
103
  }
 
104
  bzero(buff+read_length,MI_BLOCK_INFO_HEADER_LENGTH - in_buff_length -
 
105
        read_length);
 
106
  DBUG_RETURN(0);
 
107
} /* _mi_read_cache */