drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
iocache.h
1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 MySQL
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#pragma once
22
23#include <drizzled/internal/my_sys.h>
24
25namespace drizzled {
26namespace internal {
27
28#define my_b_EOF INT_MIN
29
30class io_cache_st /* Used when cacheing files */
31{
32public:
33 typedef int (*IO_CACHE_CALLBACK)(io_cache_st*);
34
35 /* Offset in file corresponding to the first byte of unsigned char* buffer. */
36 my_off_t pos_in_file;
37 /*
38 The offset of end of file for READ_CACHE and WRITE_CACHE.
39 For SEQ_READ_APPEND it the maximum of the actual end of file and
40 the position represented by read_end.
41 */
42 my_off_t end_of_file;
43 /* Points to current read position in the buffer */
44 unsigned char *read_pos;
45 /* the non-inclusive boundary in the buffer for the currently valid read */
46 unsigned char *read_end;
47 unsigned char *buffer; /* The read buffer */
48 /* Used in ASYNC_IO */
49 unsigned char *request_pos;
50
51 /* Only used in WRITE caches and in SEQ_READ_APPEND to buffer writes */
52 unsigned char *write_buffer;
53 /*
54 Only used in SEQ_READ_APPEND, and points to the current read position
55 in the write buffer. Note that reads in SEQ_READ_APPEND caches can
56 happen from both read buffer (unsigned char* buffer) and write buffer
57 (unsigned char* write_buffer).
58 */
59 unsigned char *append_read_pos;
60 /* Points to current write position in the write buffer */
61 unsigned char *write_pos;
62 /* The non-inclusive boundary of the valid write area */
63 unsigned char *write_end;
64
65 /*
66 Current_pos and current_end are convenience variables used by
67 my_b_tell() and other routines that need to know the current offset
68 current_pos points to &write_pos, and current_end to &write_end in a
69 WRITE_CACHE, and &read_pos and &read_end respectively otherwise
70 */
71 unsigned char **current_pos, **current_end;
72 /*
73 A caller will use my_b_read() macro to read from the cache
74 if the data is already in cache, it will be simply copied with
75 memcpy() and internal variables will be accordinging updated with
76 no functions invoked. However, if the data is not fully in the cache,
77 my_b_read() will call read_function to fetch the data. read_function
78 must never be invoked directly.
79 */
80 int (*read_function)(io_cache_st* ,unsigned char *,size_t);
81 /*
82 Same idea as in the case of read_function, except my_b_write() needs to
83 be replaced with my_b_append() for a SEQ_READ_APPEND cache
84 */
85 int (*write_function)(io_cache_st* ,const unsigned char *,size_t);
86 /*
87 Specifies the type of the cache. Depending on the type of the cache
88 certain operations might not be available and yield unpredicatable
89 results. Details to be documented later
90 */
91 cache_type type;
92 int error;
93 /*
94 Callbacks when the actual read I/O happens. These were added and
95 are currently used for binary logging of LOAD DATA INFILE - when a
96 block is read from the file, we create a block create/append event, and
97 when io_cache_st is closed, we create an end event. These functions could,
98 of course be used for other things
99 */
100 IO_CACHE_CALLBACK pre_read;
101 IO_CACHE_CALLBACK post_read;
102 IO_CACHE_CALLBACK pre_close;
103 void* arg; /* for use by pre/post_read */
104 char *file_name; /* if used with 'open_cached_file' */
105 char *dir,*prefix;
106 int file; /* file descriptor */
107 /*
108 seek_not_done is set by my_b_seek() to inform the upcoming read/write
109 operation that a seek needs to be preformed prior to the actual I/O
110 error is 0 if the cache operation was successful, -1 if there was a
111 "hard" error, and the actual number of I/O-ed bytes if the read/write was
112 partial.
113 */
114 int seek_not_done;
115 /* buffer_length is memory size allocated for buffer or write_buffer */
116 size_t buffer_length;
117 /* read_length is the same as buffer_length except when we use async io */
118 size_t read_length;
119 myf myflags; /* Flags used to my_read/my_write */
120 /*
121 alloced_buffer is 1 if the buffer was allocated by init_io_cache() and
122 0 if it was supplied by the user.
123 Currently READ_NET is the only one that will use a buffer allocated
124 somewhere else
125 */
126 bool alloced_buffer;
127
128 io_cache_st() :
129 pos_in_file(0),
130 end_of_file(0),
131 read_pos(0),
132 read_end(0),
133 buffer(0),
134 request_pos(0),
135 write_buffer(0),
136 append_read_pos(0),
137 write_pos(0),
138 write_end(0),
139 current_pos(0),
140 current_end(0),
141 read_function(0),
142 write_function(0),
143 type(TYPE_NOT_SET),
144 error(0),
145 pre_read(0),
146 post_read(0),
147 pre_close(0),
148 arg(0),
149 file_name(0),
150 dir(0),
151 prefix(0),
152 file(0),
153 seek_not_done(0),
154 buffer_length(0),
155 read_length(0),
156 myflags(0),
157 alloced_buffer(0)
158 { }
159
160 int get();
161 int block_write(const void*, size_t, my_off_t);
162 void close_cached_file();
163 bool real_open_cached_file();
164 int end_io_cache();
165 int init_io_cache(int file, size_t cachesize, cache_type type, my_off_t seek_offset, bool use_async_io, myf cache_myflags);
166 void init_functions();
167
168 bool reinit_io_cache(cache_type type_arg, my_off_t seek_offset, bool use_async_io, bool clear_cache);
169 void setup_io_cache();
170 bool open_cached_file(const char *dir, const char *prefix, size_t cache_size, myf cache_myflags);
171 int flush(int need_append_buffer_lock= 1);
172
173 void clear()
174 {
175 buffer= NULL;
176 }
177
178 bool inited() const
179 {
180 return buffer;
181 }
182
183 my_off_t tell() const
184 {
185 return pos_in_file + *current_pos - request_pos;
186 }
187
188 int read(void* Buffer0, size_t Count)
189 {
190 unsigned char* Buffer= reinterpret_cast<unsigned char*>(Buffer0);
191 if (read_pos + Count > read_end)
192 return read_function(this, Buffer, Count);
193 memcpy(Buffer, read_pos, Count);
194 read_pos += Count;
195 return 0;
196 }
197
198 int write(const void* Buffer0, size_t Count)
199 {
200 const unsigned char* Buffer= reinterpret_cast<const unsigned char*>(Buffer0);
201 if (write_pos + Count > write_end)
202 return write_function(this, Buffer, Count);
203 memcpy(write_pos, Buffer, Count);
204 write_pos += Count;
205 return 0;
206 }
207};
208
209} /* namespace internal */
210} /* namespace drizzled */
bool reinit_io_cache(cache_type type_arg, my_off_t seek_offset, bool use_async_io, bool clear_cache)
Reset the cache.
void setup_io_cache()
Setup internal pointers inside io_cache_st.
int end_io_cache()
Free an io_cache_st object.
int get()
Read one byte when buffer is empty.
int init_io_cache(int file, size_t cachesize, cache_type type, my_off_t seek_offset, bool use_async_io, myf cache_myflags)
Initialize an io_cache_st object.
TODO: Rename this file - func.h is stupid.