MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
transparent_file.cc
1 /* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
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 St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #include "sql_priv.h"
17 #include <mysql/psi/mysql_file.h>
18 #include "transparent_file.h"
19 #include "my_sys.h" // MY_WME, MY_ALLOW_ZERO_PTR, MY_SEEK_SET
20 
21 Transparent_file::Transparent_file() : lower_bound(0), buff_size(IO_SIZE)
22 {
23  buff= (uchar *) my_malloc(buff_size*sizeof(uchar), MYF(MY_WME));
24 }
25 
26 Transparent_file::~Transparent_file()
27 {
28  my_free(buff);
29 }
30 
31 void Transparent_file::init_buff(File filedes_arg)
32 {
33  filedes= filedes_arg;
34  /* read the beginning of the file */
35  lower_bound= 0;
36  mysql_file_seek(filedes, 0, MY_SEEK_SET, MYF(0));
37  if (filedes && buff)
38  upper_bound= mysql_file_read(filedes, buff, buff_size, MYF(0));
39 }
40 
41 uchar *Transparent_file::ptr()
42 {
43  return buff;
44 }
45 
46 my_off_t Transparent_file::start()
47 {
48  return lower_bound;
49 }
50 
51 my_off_t Transparent_file::end()
52 {
53  return upper_bound;
54 }
55 
56 my_off_t Transparent_file::read_next()
57 {
58  size_t bytes_read;
59 
60  /*
61  No need to seek here, as the file managed by Transparent_file class
62  always points to upper_bound byte
63  */
64  if ((bytes_read= mysql_file_read(filedes, buff, buff_size, MYF(0)))
65  == MY_FILE_ERROR)
66  return (my_off_t) -1;
67 
68  /* end of file */
69  if (!bytes_read)
70  return (my_off_t) -1;
71 
72  lower_bound= upper_bound;
73  upper_bound+= bytes_read;
74 
75  return lower_bound;
76 }
77 
78 
79 char Transparent_file::get_value(my_off_t offset)
80 {
81  size_t bytes_read;
82 
83  /* check boundaries */
84  if ((lower_bound <= offset) && (((my_off_t) offset) < upper_bound))
85  return buff[offset - lower_bound];
86 
87  mysql_file_seek(filedes, offset, MY_SEEK_SET, MYF(0));
88  /* read appropriate portion of the file */
89  if ((bytes_read= mysql_file_read(filedes, buff, buff_size,
90  MYF(0))) == MY_FILE_ERROR)
91  return 0;
92 
93  lower_bound= offset;
94  upper_bound= lower_bound + bytes_read;
95 
96  /* end of file */
97  if (upper_bound == (my_off_t) offset)
98  return 0;
99 
100  return buff[0];
101 }