Eneboo - Documentación para desarrolladores
src/libpq/include/utils/pg_lzcompress.h
Ir a la documentación de este archivo.
00001 /* ----------
00002  * pg_lzcompress.h -
00003  *
00004  * $PostgreSQL: pgsql/src/include/utils/pg_lzcompress.h,v 1.11 2005/05/25 21:40:42 momjian Exp $
00005  *
00006  *      Definitions for the builtin LZ compressor
00007  * ----------
00008  */
00009 
00010 #ifndef _PG_LZCOMPRESS_H_
00011 #define _PG_LZCOMPRESS_H_
00012 
00013 
00014 /* ----------
00015  * PGLZ_Header -
00016  *
00017  *              The information at the top of the compressed data.
00018  *              The varsize must be kept the same data type as the value
00019  *              in front of all variable size data types in PostgreSQL.
00020  * ----------
00021  */
00022 typedef struct PGLZ_Header
00023 {
00024         int32           varsize;
00025         int32           rawsize;
00026 } PGLZ_Header;
00027 
00028 
00029 /* ----------
00030  * PGLZ_MAX_OUTPUT -
00031  *
00032  *              Macro to compute the maximum buffer required for the
00033  *              compression output. It is larger than the input, because
00034  *              in the worst case, we cannot write out one single tag but
00035  *              need one control byte per 8 literal data bytes plus the
00036  *              EOF mark at the end.
00037  * ----------
00038  */
00039 #define PGLZ_MAX_OUTPUT(_dlen)                  ((_dlen) + (((_dlen) | 0x07) >> 3)      \
00040                                                                                                          + sizeof(PGLZ_Header))
00041 
00042 /* ----------
00043  * PGLZ_RAW_SIZE -
00044  *
00045  *              Macro to determine the uncompressed data size contained
00046  *              in the entry.
00047  * ----------
00048  */
00049 #define PGLZ_RAW_SIZE(_lzdata)                  ((_lzdata)->rawsize)
00050 
00051 /* ----------
00052  * PGLZ_IS_COMPRESSED -
00053  *
00054  *              Macro to determine if the data itself is stored as raw
00055  *              uncompressed data.
00056  * ----------
00057  */
00058 #define PGLZ_IS_COMPRESSED(_lzdata)             ((_lzdata)->varsize !=                          \
00059 e                                                                                (_lzdata)->rawsize +                   e       \
00060                                                                                                                 sizeof(PGLZ_Header))
00061 
00062 /* ----------
00063  * PGLZ_RAW_DATA -
00064  *
00065  *              Macro to get access to the plain compressed or uncompressed
00066  *              data. Useful if PGLZ_IS_COMPRESSED returns false.
00067  * ----------
00068  */
00069 #define PGLZ_RAW_DATA(_lzdata)                  (((char *)(_lzdata)) +                          \
00070                                                                                                                 sizeof(PGLZ_Header))
00071 
00072 /* ----------
00073  * PGLZ_Strategy -
00074  *
00075  *              Some values that control the compression algorithm.
00076  *
00077  *              min_input_size          Minimum input data size to start compression.
00078  *
00079  *              force_input_size        Input data size at which compressed storage is
00080  *                                                      forced even if the compression rate drops below
00081  *                                                      min_comp_rate (but not below 0).
00082  *
00083  *              min_comp_rate           Minimum compression rate (0-99%), the output
00084  *                                                      must be smaller than the input. If that isn't
00085  *                                                      the case, the compressor will throw away its
00086  *                                                      output and copy the original, uncompressed data
00087  *                                                      to the output buffer.
00088  *
00089  *              match_size_good         The initial GOOD match size when starting history
00090  *                                                      lookup. When looking up the history to find a
00091  *                                                      match that could be expressed as a tag, the
00092  *                                                      algorithm does not always walk back entirely.
00093  *                                                      A good match fast is usually better than the
00094  *                                                      best possible one very late. For each iteration
00095  *                                                      in the lookup, this value is lowered so the
00096  *                                                      longer the lookup takes, the smaller matches
00097  *                                                      are considered good.
00098  *
00099  *              match_size_drop         The percentage, match_size_good is lowered
00100  *                                                      at each history check. Allowed values are
00101  *                                                      0 (no change until end) to 100 (only check
00102  *                                                      latest history entry at all).
00103  * ----------
00104  */
00105 typedef struct PGLZ_Strategy
00106 {
00107         int32           min_input_size;
00108         int32           force_input_size;
00109         int32           min_comp_rate;
00110         int32           match_size_good;
00111         int32           match_size_drop;
00112 } PGLZ_Strategy;
00113 
00114 
00115 /* ----------
00116  * PGLZ_DecompState -
00117  *
00118  *              Decompression state variable for byte-per-byte decompression
00119  *              using pglz_decomp_getchar() macro.
00120  * ----------
00121  */
00122 typedef struct PGLZ_DecompState
00123 {
00124         unsigned char *temp_buf;
00125         unsigned char *cp_in;
00126         unsigned char *cp_end;
00127         unsigned char *cp_out;
00128         unsigned char *cp_copy;
00129         int                     (*next_char) (struct PGLZ_DecompState *dstate);
00130         int                     tocopy;
00131         int                     ctrl_count;
00132         unsigned char ctrl;
00133 } PGLZ_DecompState;
00134 
00135 
00136 /* ----------
00137  * The standard strategies
00138  *
00139  *              PGLZ_strategy_default           Starts compression only if input is
00140  *                                                                      at least 256 bytes large. Stores output
00141  *                                                                      uncompressed if compression does not
00142  *                                                                      gain at least 20% size reducture but
00143  *                                                                      input does not exceed 6K. Stops history
00144  *                                                                      lookup if at least a 128 byte long
00145  *                                                                      match has been found.
00146  *
00147  *                                                                      This is the default strategy if none
00148  *                                                                      is given to pglz_compress().
00149  *
00150  *              PGLZ_strategy_always            Starts compression on any infinitely
00151  *                                                                      small input and does fallback to
00152  *                                                                      uncompressed storage only if output
00153  *                                                                      would be larger than input.
00154  *
00155  *              PGLZ_strategy_never                     Force pglz_compress to act as a custom
00156  *                                                                      interface for memcpy(). Only useful
00157  *                                                                      for generic interfacing.
00158  * ----------
00159  */
00160 extern PGLZ_Strategy *PGLZ_strategy_default;
00161 extern PGLZ_Strategy *PGLZ_strategy_always;
00162 extern PGLZ_Strategy *PGLZ_strategy_never;
00163 
00164 
00165 /* ----------
00166  * pglz_decomp_getchar -
00167  *
00168  *              Get next character (or EOF) from decompressor.
00169  *              The status variable must be initialized before and deinitialized
00170  *              after compression with the next two macros below.
00171  * ----------
00172  */
00173 #define pglz_decomp_getchar(_ds)                                                                                        \
00174         ((*((_ds)->next_char))((_ds)))
00175 
00176 
00177 /* ----------
00178  * pglz_decomp_init -
00179  *
00180  *              Initialize a decomp state from a compressed input.
00181  * ----------
00182  */
00183 #define pglz_decomp_init(_ds,_lz) do {                                                                          \
00184                 (_ds)->cp_in            = ((unsigned char *)(_lz))                                              \
00185                                                                                         + sizeof(PGLZ_Header);                  \
00186                 (_ds)->cp_end           = (_ds)->cp_in + (_lz)->varsize                                 \
00187                                                                                         - sizeof(PGLZ_Header);                  \
00188                 if (PGLZ_IS_COMPRESSED((_lz))) {                                                                        \
00189                         (_ds)->temp_buf         = (unsigned char *)                                                     \
00190                                                                                 palloc(PGLZ_RAW_SIZE((_lz)));           \
00191                         (_ds)->cp_out           = (_ds)->temp_buf;                                                      \
00192                         (_ds)->next_char        = pglz_get_next_decomp_char_from_lzdata;        \
00193                         (_ds)->tocopy           = 0;                                                                            \
00194                         (_ds)->ctrl_count       = 0;                                                                            \
00195                 } else {                                                                                                                        \
00196                         (_ds)->temp_buf         = NULL;                                                                         \
00197                         (_ds)->next_char        = pglz_get_next_decomp_char_from_plain;         \
00198                 }                                                                                                                                       \
00199         } while (0)
00200 
00201 
00202 /* ----------
00203  * pglz_decomp_end -
00204  *
00205  *              Deallocate resources after decompression.
00206  * ----------
00207  */
00208 #define pglz_decomp_end(_ds) do {                                                                                       \
00209                 if ((_ds)->temp_buf != NULL)                                                                            \
00210                         pfree((void *)((_ds)->temp_buf));                                                               \
00211         } while (0)
00212 
00213 
00214 /* ----------
00215  * Global function declarations
00216  * ----------
00217  */
00218 int pglz_compress(char *source, int32 slen, PGLZ_Header *dest,
00219                           PGLZ_Strategy *strategy);
00220 int                     pglz_decompress(PGLZ_Header *source, char *dest);
00221 
00222 
00223 /* ----------
00224  * Functions used by pglz_decomp_getchar().
00225  * Internal use only.
00226  * ----------
00227  */
00228 extern int      pglz_get_next_decomp_char_from_lzdata(PGLZ_DecompState *dstate);
00229 extern int      pglz_get_next_decomp_char_from_plain(PGLZ_DecompState *dstate);
00230 
00231 #endif   /* _PG_LZCOMPRESS_H_ */
 Todo Clases Namespaces Archivos Funciones Variables 'typedefs' Enumeraciones Valores de enumeraciones Propiedades Amigas 'defines'