~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/archive/azio.h

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

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