1548.5.2
by Zimin
shamelessly borrow these two files from CSV storage engine. |
1 |
/* Copyright (C) 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
|
|
1802.10.2
by Monty Taylor
Update all of the copyright headers to include the correct address. |
14 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
|
1548.5.2
by Zimin
shamelessly borrow these two files from CSV storage engine. |
15 |
|
16 |
||
17 |
#include "config.h" |
|
18 |
#include <cstdlib> |
|
19 |
#include "drizzled/internal/my_sys.h" |
|
20 |
#include "transparent_file.h" |
|
1548.4.94
by Zimin
remove internal::my_read to use xread instead |
21 |
#include "utility.h" |
1548.5.2
by Zimin
shamelessly borrow these two files from CSV storage engine. |
22 |
|
23 |
using namespace drizzled; |
|
24 |
||
1548.5.3
by Zimin
some name changes to be corresponding with drizzle's naming style. |
25 |
TransparentFile::TransparentFile() : lower_bound(0), buff_size(IO_SIZE) |
1548.5.2
by Zimin
shamelessly borrow these two files from CSV storage engine. |
26 |
{
|
27 |
buff= static_cast<unsigned char *>(malloc(buff_size*sizeof(unsigned char))); |
|
28 |
}
|
|
29 |
||
1548.5.3
by Zimin
some name changes to be corresponding with drizzle's naming style. |
30 |
TransparentFile::~TransparentFile() |
1548.5.2
by Zimin
shamelessly borrow these two files from CSV storage engine. |
31 |
{
|
32 |
free(buff); |
|
33 |
}
|
|
34 |
||
1548.5.3
by Zimin
some name changes to be corresponding with drizzle's naming style. |
35 |
void TransparentFile::init_buff(int filedes_arg) |
1548.5.2
by Zimin
shamelessly borrow these two files from CSV storage engine. |
36 |
{
|
37 |
filedes= filedes_arg; |
|
38 |
/* read the beginning of the file */
|
|
39 |
lower_bound= 0; |
|
40 |
lseek(filedes, 0, SEEK_SET); |
|
41 |
if (filedes && buff) |
|
1548.4.112
by Zimin
fix missing xread |
42 |
upper_bound= ::read(filedes, buff, buff_size); |
1548.5.2
by Zimin
shamelessly borrow these two files from CSV storage engine. |
43 |
}
|
44 |
||
1548.5.3
by Zimin
some name changes to be corresponding with drizzle's naming style. |
45 |
unsigned char *TransparentFile::ptr() |
1548.5.2
by Zimin
shamelessly borrow these two files from CSV storage engine. |
46 |
{
|
47 |
return buff; |
|
48 |
}
|
|
49 |
||
1548.5.3
by Zimin
some name changes to be corresponding with drizzle's naming style. |
50 |
off_t TransparentFile::start() |
1548.5.2
by Zimin
shamelessly borrow these two files from CSV storage engine. |
51 |
{
|
52 |
return lower_bound; |
|
53 |
}
|
|
54 |
||
1548.5.3
by Zimin
some name changes to be corresponding with drizzle's naming style. |
55 |
off_t TransparentFile::end() |
1548.5.2
by Zimin
shamelessly borrow these two files from CSV storage engine. |
56 |
{
|
57 |
return upper_bound; |
|
58 |
}
|
|
59 |
||
1548.5.3
by Zimin
some name changes to be corresponding with drizzle's naming style. |
60 |
off_t TransparentFile::read_next() |
1548.5.2
by Zimin
shamelessly borrow these two files from CSV storage engine. |
61 |
{
|
1548.4.94
by Zimin
remove internal::my_read to use xread instead |
62 |
ssize_t bytes_read; |
1548.5.2
by Zimin
shamelessly borrow these two files from CSV storage engine. |
63 |
|
64 |
/*
|
|
1548.5.3
by Zimin
some name changes to be corresponding with drizzle's naming style. |
65 |
No need to seek here, as the file managed by TransparentFile class
|
1548.5.2
by Zimin
shamelessly borrow these two files from CSV storage engine. |
66 |
always points to upper_bound byte
|
67 |
*/
|
|
1548.4.111
by Zimin
remove xread and xclose, but change xwrite to write_in_all. |
68 |
if ((bytes_read= ::read(filedes, buff, buff_size)) < 0) |
1548.5.2
by Zimin
shamelessly borrow these two files from CSV storage engine. |
69 |
return (off_t) -1; |
70 |
||
71 |
/* end of file */
|
|
72 |
if (!bytes_read) |
|
73 |
return (off_t) -1; |
|
74 |
||
75 |
lower_bound= upper_bound; |
|
76 |
upper_bound+= bytes_read; |
|
77 |
||
78 |
return lower_bound; |
|
79 |
}
|
|
80 |
||
81 |
||
1548.5.3
by Zimin
some name changes to be corresponding with drizzle's naming style. |
82 |
char TransparentFile::get_value(off_t offset) |
1548.5.2
by Zimin
shamelessly borrow these two files from CSV storage engine. |
83 |
{
|
1548.4.94
by Zimin
remove internal::my_read to use xread instead |
84 |
ssize_t bytes_read; |
1548.5.2
by Zimin
shamelessly borrow these two files from CSV storage engine. |
85 |
|
86 |
/* check boundaries */
|
|
87 |
if ((lower_bound <= offset) && (offset < upper_bound)) |
|
88 |
return buff[offset - lower_bound]; |
|
89 |
||
90 |
lseek(filedes, offset, SEEK_SET); |
|
91 |
/* read appropriate portion of the file */
|
|
1548.4.111
by Zimin
remove xread and xclose, but change xwrite to write_in_all. |
92 |
if ((bytes_read= ::read(filedes, buff, buff_size)) < 0) |
1548.5.2
by Zimin
shamelessly borrow these two files from CSV storage engine. |
93 |
return 0; |
94 |
||
95 |
lower_bound= offset; |
|
96 |
upper_bound= lower_bound + bytes_read; |
|
97 |
||
98 |
/* end of file */
|
|
99 |
if (upper_bound == offset) |
|
100 |
return 0; |
|
101 |
||
102 |
return buff[0]; |
|
103 |
}
|