~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/archive/azio.h

Extracted the LOAD command into its own class and implementation files.
Removed the corresponding case label from the switch statement.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
  This libary has been modified for use by the MySQL Archive Engine.
3
 
     -Brian Aker
4
 
*/
5
 
 
6
 
/* zlib.h -- interface of the 'zlib' general purpose compression library
7
 
  version 1.2.3, July 18th, 2005
8
 
 
9
 
  Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
10
 
 
11
 
  This software is provided 'as-is', without any express or implied
12
 
  warranty.  In no event will the authors be held liable for any damages
13
 
  arising from the use of this software.
14
 
 
15
 
  Permission is granted to anyone to use this software for any purpose,
16
 
  including commercial applications, and to alter it and redistribute it
17
 
  freely, subject to the following restrictions:
18
 
 
19
 
  1. The origin of this software must not be misrepresented; you must not
20
 
     claim that you wrote the original software. If you use this software
21
 
     in a product, an acknowledgment in the product documentation would be
22
 
     appreciated but is not required.
23
 
  2. Altered source versions must be plainly marked as such, and must not be
24
 
     misrepresented as being the original software.
25
 
  3. This notice may not be removed or altered from any source distribution.
26
 
 
27
 
  Jean-loup Gailly        Mark Adler
28
 
  jloup@gzip.org          madler@alumni.caltech.edu
29
 
 
30
 
 
31
 
  The data format used by the zlib library is described by RFCs (Request for
32
 
  Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
33
 
  (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
34
 
*/
35
 
 
36
 
#ifndef __AZIO_H__
37
 
#define __AZIO_H__
38
 
 
39
 
/* We currently allow this on all platforms */
40
 
#define AZIO_AIO
41
 
 
42
 
#include <drizzled/global.h>
43
 
#include <drizzled/common.h>
44
 
#include <mysys/my_sys.h>
45
 
 
46
 
#include <zlib.h>
47
 
 
48
 
#ifdef  __cplusplus
49
 
extern "C" {
50
 
#endif
51
 
 
52
 
/* Start of MySQL Specific Information */
53
 
 
54
 
/* Some personal debugging functions */
55
 
#define WATCHPOINT fprintf(stderr, "\nWATCHPOINT %s:%d (%s)\n", __FILE__, __LINE__,__func__);fflush(stderr);
56
 
#define WATCHPOINT_STRING(A) fprintf(stderr, "\nWATCHPOINT %s:%d (%s) %s\n", __FILE__, __LINE__,__func__,A);fflush(stderr);
57
 
#define WATCHPOINT_NUMBER(A) fprintf(stderr, "\nWATCHPOINT %s:%d (%s) %d\n", __FILE__, __LINE__,__func__,(int)(A));fflush(stderr);
58
 
#define WATCHPOINT_ERRNO(A) fprintf(stderr, "\nWATCHPOINT %s:%d (%s) %s\n", __FILE__, __LINE__,__func__, strerror(A));A= 0;fflush(stderr);
59
 
 
60
 
/*
61
 
  uint64_t + uint64_t + uint64_t + uint64_t + unsigned char
62
 
*/
63
 
#define AZMETA_BUFFER_SIZE sizeof(uint64_t) \
64
 
  + sizeof(uint64_t) + sizeof(uint64_t) + sizeof(uint64_t) \
65
 
  + sizeof(unsigned int) + sizeof(unsigned int) \
66
 
  + sizeof(unsigned int) + sizeof(unsigned int) \
67
 
  + sizeof(unsigned char)
68
 
 
69
 
#define AZHEADER_SIZE 29
70
 
 
71
 
#define AZ_MAGIC_POS 0
72
 
#define AZ_VERSION_POS 1
73
 
#define AZ_MINOR_VERSION_POS 2
74
 
#define AZ_BLOCK_POS 3
75
 
#define AZ_STRATEGY_POS 4
76
 
#define AZ_FRM_POS 5
77
 
#define AZ_FRM_LENGTH_POS 9
78
 
#define AZ_META_POS 13
79
 
#define AZ_META_LENGTH_POS 17
80
 
#define AZ_START_POS 21
81
 
#define AZ_ROW_POS 29
82
 
#define AZ_FLUSH_POS 37
83
 
#define AZ_CHECK_POS 45
84
 
#define AZ_AUTOINCREMENT_POS 53
85
 
#define AZ_LONGEST_POS 61
86
 
#define AZ_SHORTEST_POS 65
87
 
#define AZ_COMMENT_POS 69
88
 
#define AZ_COMMENT_LENGTH_POS 73
89
 
#define AZ_DIRTY_POS 77
90
 
 
91
 
 
92
 
/*
93
 
  Flags for state
94
 
*/
95
 
#define AZ_STATE_CLEAN 0
96
 
#define AZ_STATE_DIRTY 1
97
 
#define AZ_STATE_SAVED 2
98
 
#define AZ_STATE_CRASHED 3
99
 
 
100
 
/*
101
 
     The 'zlib' compression library provides in-memory compression and
102
 
  decompression functions, including integrity checks of the uncompressed
103
 
  data.  This version of the library supports only one compression method
104
 
  (deflation) but other algorithms will be added later and will have the same
105
 
  stream interface.
106
 
 
107
 
     Compression can be done in a single step if the buffers are large
108
 
  enough (for example if an input file is mmap'ed), or can be done by
109
 
  repeated calls of the compression function.  In the latter case, the
110
 
  application must provide more input and/or consume the output
111
 
  (providing more output space) before each call.
112
 
 
113
 
     The compressed data format used by default by the in-memory functions is
114
 
  the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
115
 
  around a deflate stream, which is itself documented in RFC 1951.
116
 
 
117
 
     The library also supports reading and writing files in gzip (.gz) format
118
 
  with an interface similar to that of stdio using the functions that start
119
 
  with "gz".  The gzip format is different from the zlib format.  gzip is a
120
 
  gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
121
 
 
122
 
     This library can optionally read and write gzip streams in memory as well.
123
 
 
124
 
     The zlib format was designed to be compact and fast for use in memory
125
 
  and on communications channels.  The gzip format was designed for single-
126
 
  file compression on file systems, has a larger header than zlib to maintain
127
 
  directory information, and uses a different, slower check method than zlib.
128
 
 
129
 
     The library does not install any signal handler. The decoder checks
130
 
  the consistency of the compressed data, so the library should never
131
 
  crash even in case of corrupted input.
132
 
*/
133
 
 
134
 
 
135
 
/*
136
 
   The application must update next_in and avail_in when avail_in has
137
 
   dropped to zero. It must update next_out and avail_out when avail_out
138
 
   has dropped to zero. The application must initialize zalloc, zfree and
139
 
   opaque before calling the init function. All other fields are set by the
140
 
   compression library and must not be updated by the application.
141
 
 
142
 
   The opaque value provided by the application will be passed as the first
143
 
   parameter for calls of zalloc and zfree. This can be useful for custom
144
 
   memory management. The compression library attaches no meaning to the
145
 
   opaque value.
146
 
 
147
 
   zalloc must return Z_NULL if there is not enough memory for the object.
148
 
   If zlib is used in a multi-threaded application, zalloc and zfree must be
149
 
   thread safe.
150
 
 
151
 
   On 16-bit systems, the functions zalloc and zfree must be able to allocate
152
 
   exactly 65536 bytes, but will not be required to allocate more than this
153
 
   if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
154
 
   pointers returned by zalloc for objects of exactly 65536 bytes *must*
155
 
   have their offset normalized to zero. The default allocation function
156
 
   provided by this library ensures this (see zutil.c). To reduce memory
157
 
   requirements and avoid any allocation of 64K objects, at the expense of
158
 
   compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
159
 
 
160
 
   The fields total_in and total_out can be used for statistics or
161
 
   progress reports. After compression, total_in holds the total size of
162
 
   the uncompressed data and may be saved for use in the decompressor
163
 
   (particularly if the decompressor wants to decompress everything in
164
 
   a single step).
165
 
*/
166
 
 
167
 
                        /* constants */
168
 
 
169
 
#define Z_NO_FLUSH      0
170
 
#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
171
 
#define Z_SYNC_FLUSH    2
172
 
#define Z_FULL_FLUSH    3
173
 
#define Z_FINISH        4
174
 
#define Z_BLOCK         5
175
 
/* Allowed flush values; see deflate() and inflate() below for details */
176
 
 
177
 
#define Z_OK            0
178
 
#define Z_STREAM_END    1
179
 
#define Z_NEED_DICT     2
180
 
#define Z_ERRNO        (-1)
181
 
#define Z_STREAM_ERROR (-2)
182
 
#define Z_DATA_ERROR   (-3)
183
 
#define Z_MEM_ERROR    (-4)
184
 
#define Z_BUF_ERROR    (-5)
185
 
#define Z_VERSION_ERROR (-6)
186
 
/* Return codes for the compression/decompression functions. Negative
187
 
 * values are errors, positive values are used for special but normal events.
188
 
 */
189
 
 
190
 
#define Z_NO_COMPRESSION         0
191
 
#define Z_BEST_SPEED             1
192
 
#define Z_BEST_COMPRESSION       9
193
 
#define Z_DEFAULT_COMPRESSION  (-1)
194
 
/* compression levels */
195
 
 
196
 
#define Z_FILTERED            1
197
 
#define Z_HUFFMAN_ONLY        2
198
 
#define Z_RLE                 3
199
 
#define Z_FIXED               4
200
 
#define Z_DEFAULT_STRATEGY    0
201
 
/* compression strategy; see deflateInit2() below for details */
202
 
 
203
 
#define Z_BINARY   0
204
 
#define Z_TEXT     1
205
 
#define Z_ASCII    Z_TEXT   /* for compatibility with 1.2.2 and earlier */
206
 
#define Z_UNKNOWN  2
207
 
/* Possible values of the data_type field (though see inflate()) */
208
 
 
209
 
#define Z_DEFLATED   8
210
 
/* The deflate compression method (the only one supported in this version) */
211
 
 
212
 
#define Z_NULL  0  /* for initializing zalloc, zfree, opaque */
213
 
#define AZ_BUFSIZE_READ 32768
214
 
#define AZ_BUFSIZE_WRITE 16384
215
 
 
216
 
typedef enum {
217
 
  AZ_THREAD_FINISHED,
218
 
  AZ_THREAD_ACTIVE,
219
 
  AZ_THREAD_DEAD
220
 
} az_thread_type;
221
 
 
222
 
typedef enum {
223
 
  AZ_METHOD_BLOCK,
224
 
  AZ_METHOD_AIO,
225
 
  AZ_METHOD_MAX
226
 
} az_method;
227
 
 
228
 
typedef struct azio_container_st azio_container_st;
229
 
 
230
 
struct azio_container_st {
231
 
  int fd;
232
 
  az_thread_type ready;
233
 
  size_t offset;
234
 
  size_t read_size;
235
 
  void *buffer;
236
 
  pthread_mutex_t thresh_mutex;
237
 
  pthread_cond_t threshhold;
238
 
  pthread_t mainthread;            /* Thread descriptor */
239
 
};
240
 
 
241
 
 
242
 
typedef struct azio_stream {
243
 
  z_stream stream;
244
 
  int      z_err;   /* error code for last stream operation */
245
 
  int      z_eof;   /* set if end of input file */
246
 
  File     file;   /* .gz file */
247
 
  Byte     *inbuf;  /* input buffer */
248
 
  Byte     buffer1[AZ_BUFSIZE_READ];  /* input buffer */
249
 
  Byte     buffer2[AZ_BUFSIZE_READ];  /* input buffer */
250
 
  Byte     outbuf[AZ_BUFSIZE_WRITE]; /* output buffer */
251
 
  int      aio_inited; /* Are we good to go */
252
 
  uLong    crc;     /* crc32 of uncompressed data */
253
 
  char     *msg;    /* error message */
254
 
  char     mode;    /* 'w' or 'r' */
255
 
  size_t  start;   /* start of compressed data in file (header skipped) */
256
 
  size_t  in;      /* bytes into deflate or inflate */
257
 
  size_t  out;     /* bytes out of deflate or inflate */
258
 
  size_t  pos;     /* bytes out of deflate or inflate */
259
 
  int      back;    /* one character push-back */
260
 
  int      last;    /* true if push-back is last character */
261
 
  unsigned char version;   /* Version */
262
 
  unsigned char minor_version;   /* Version */
263
 
  unsigned int block_size;   /* Block Size */
264
 
  uint64_t check_point;   /* Last position we checked */
265
 
  uint64_t forced_flushes;   /* Forced Flushes */
266
 
  uint64_t rows;   /* rows */
267
 
  uint64_t auto_increment;   /* auto increment field */
268
 
  unsigned int longest_row;   /* Longest row */
269
 
  unsigned int shortest_row;   /* Shortest row */
270
 
  unsigned char dirty;   /* State of file */
271
 
  unsigned int frm_start_pos;   /* Position for start of FRM */
272
 
  unsigned int frm_length;   /* Position for start of FRM */
273
 
  unsigned int comment_start_pos;   /* Position for start of comment */
274
 
  unsigned int comment_length;   /* Position for start of comment */
275
 
#ifdef AZIO_AIO
276
 
  azio_container_st container;
277
 
#endif
278
 
  az_method method;
279
 
  char *row_ptr;
280
 
} azio_stream;
281
 
 
282
 
                        /* basic functions */
283
 
 
284
 
int azopen(azio_stream *s, const char *path, int Flags, az_method method);
285
 
/*
286
 
     Opens a gzip (.gz) file for reading or writing. The mode parameter
287
 
   is as in fopen ("rb" or "wb") but can also include a compression level
288
 
   ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
289
 
   Huffman only compression as in "wb1h", or 'R' for run-length encoding
290
 
   as in "wb1R". (See the description of deflateInit2 for more information
291
 
   about the strategy parameter.)
292
 
 
293
 
     azopen can be used to read a file which is not in gzip format; in this
294
 
   case gzread will directly read from the file without decompression.
295
 
 
296
 
     azopen returns NULL if the file could not be opened or if there was
297
 
   insufficient memory to allocate the (de)compression state; errno
298
 
   can be checked to distinguish the two cases (if errno is zero, the
299
 
   zlib error is Z_MEM_ERROR).  */
300
 
 
301
 
int azdopen(azio_stream *s,File fd, int Flags);
302
 
/*
303
 
     azdopen() associates a azio_stream with the file descriptor fd.  File
304
 
   descriptors are obtained from calls like open, dup, creat, pipe or
305
 
   fileno (in the file has been previously opened with fopen).
306
 
   The mode parameter is as in azopen.
307
 
     The next call of gzclose on the returned azio_stream will also close the
308
 
   file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
309
 
   descriptor fd. If you want to keep fd open, use azdopen(dup(fd), mode).
310
 
     azdopen returns NULL if there was insufficient memory to allocate
311
 
   the (de)compression state.
312
 
*/
313
 
 
314
 
 
315
 
unsigned int azread_internal( azio_stream *s, voidp buf, unsigned int len, int *error);
316
 
/*
317
 
   This function is legacy, do not use.
318
 
 
319
 
     Reads the given number of uncompressed bytes from the compressed file.
320
 
   If the input file was not in gzip format, gzread copies the given number
321
 
   of bytes into the buffer.
322
 
     gzread returns the number of uncompressed bytes actually read (0 for
323
 
   end of file, -1 for error). */
324
 
 
325
 
extern int azflush(azio_stream *file, int flush);
326
 
/*
327
 
     Flushes all pending output into the compressed file. The parameter
328
 
   flush is as in the deflate() function. The return value is the zlib
329
 
   error number (see function gzerror below). gzflush returns Z_OK if
330
 
   the flush parameter is Z_FINISH and all output could be flushed.
331
 
     gzflush should be called only when strictly necessary because it can
332
 
   degrade compression.
333
 
*/
334
 
 
335
 
extern size_t azseek (azio_stream *file,
336
 
                                      size_t offset, int whence);
337
 
/*
338
 
      Sets the starting position for the next gzread or gzwrite on the
339
 
   given compressed file. The offset represents a number of bytes in the
340
 
   uncompressed data stream. The whence parameter is defined as in lseek(2);
341
 
   the value SEEK_END is not supported.
342
 
     If the file is opened for reading, this function is emulated but can be
343
 
   extremely slow. If the file is opened for writing, only forward seeks are
344
 
   supported; gzseek then compresses a sequence of zeroes up to the new
345
 
   starting position.
346
 
 
347
 
      gzseek returns the resulting offset location as measured in bytes from
348
 
   the beginning of the uncompressed stream, or -1 in case of error, in
349
 
   particular if the file is opened for writing and the new starting position
350
 
   would be before the current position.
351
 
*/
352
 
 
353
 
extern size_t aztell(azio_stream *file);
354
 
/*
355
 
     Returns the starting position for the next gzread or gzwrite on the
356
 
   given compressed file. This position represents a number of bytes in the
357
 
   uncompressed data stream.
358
 
 
359
 
   gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
360
 
*/
361
 
 
362
 
extern int azclose(azio_stream *file);
363
 
/*
364
 
     Flushes all pending output if necessary, closes the compressed file
365
 
   and deallocates all the (de)compression state. The return value is the zlib
366
 
   error number (see function gzerror below).
367
 
*/
368
 
 
369
 
int azread_init(azio_stream *s);
370
 
size_t azwrite_row(azio_stream *s, void *buf, unsigned int len);
371
 
size_t azread_row(azio_stream *s, int *error);
372
 
 
373
 
extern int azwrite_frm (azio_stream *s, const char *blob, unsigned int length);
374
 
extern int azread_frm (azio_stream *s, char *blob);
375
 
extern int azwrite_comment (azio_stream *s, char *blob, unsigned int length);
376
 
extern int azread_comment (azio_stream *s, char *blob);
377
 
 
378
 
#ifdef  __cplusplus
379
 
}
380
 
#endif
381
 
 
382
 
#endif /* AZIO_H */