PostgreSQL Source Code  git master
pg_lzcompress.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PGLZ_Strategy
 

Macros

#define PGLZ_MAX_OUTPUT(_dlen)   ((_dlen) + 4)
 

Typedefs

typedef struct PGLZ_Strategy PGLZ_Strategy
 

Functions

int32 pglz_compress (const char *source, int32 slen, char *dest, const PGLZ_Strategy *strategy)
 
int32 pglz_decompress (const char *source, int32 slen, char *dest, int32 rawsize, bool check_complete)
 
int32 pglz_maximum_compressed_size (int32 rawsize, int32 total_compressed_size)
 

Variables

const PGLZ_Strategy *const PGLZ_strategy_default
 
const PGLZ_Strategy *const PGLZ_strategy_always
 

Macro Definition Documentation

◆ PGLZ_MAX_OUTPUT

#define PGLZ_MAX_OUTPUT (   _dlen)    ((_dlen) + 4)

Definition at line 21 of file pg_lzcompress.h.

Referenced by toast_compress_datum().

Typedef Documentation

◆ PGLZ_Strategy

typedef struct PGLZ_Strategy PGLZ_Strategy

Function Documentation

◆ pglz_compress()

int32 pglz_compress ( const char *  source,
int32  slen,
char *  dest,
const PGLZ_Strategy strategy 
)

Definition at line 509 of file pg_lzcompress.c.

References PGLZ_Strategy::first_success_by, hist_start, PGLZ_Strategy::match_size_drop, PGLZ_Strategy::match_size_good, PGLZ_Strategy::max_input_size, PGLZ_Strategy::min_comp_rate, pglz_find_match(), pglz_hist_add, PGLZ_MAX_MATCH, pglz_out_literal, pglz_out_tag, and PGLZ_strategy_default.

Referenced by toast_compress_datum(), and XLogCompressBackupBlock().

511 {
512  unsigned char *bp = (unsigned char *) dest;
513  unsigned char *bstart = bp;
514  int hist_next = 1;
515  bool hist_recycle = false;
516  const char *dp = source;
517  const char *dend = source + slen;
518  unsigned char ctrl_dummy = 0;
519  unsigned char *ctrlp = &ctrl_dummy;
520  unsigned char ctrlb = 0;
521  unsigned char ctrl = 0;
522  bool found_match = false;
523  int32 match_len;
524  int32 match_off;
525  int32 good_match;
526  int32 good_drop;
527  int32 result_size;
528  int32 result_max;
529  int32 need_rate;
530  int hashsz;
531  int mask;
532 
533  /*
534  * Our fallback strategy is the default.
535  */
536  if (strategy == NULL)
537  strategy = PGLZ_strategy_default;
538 
539  /*
540  * If the strategy forbids compression (at all or if source chunk size out
541  * of range), fail.
542  */
543  if (strategy->match_size_good <= 0 ||
544  slen < strategy->min_input_size ||
545  slen > strategy->max_input_size)
546  return -1;
547 
548  /*
549  * Limit the match parameters to the supported range.
550  */
551  good_match = strategy->match_size_good;
552  if (good_match > PGLZ_MAX_MATCH)
553  good_match = PGLZ_MAX_MATCH;
554  else if (good_match < 17)
555  good_match = 17;
556 
557  good_drop = strategy->match_size_drop;
558  if (good_drop < 0)
559  good_drop = 0;
560  else if (good_drop > 100)
561  good_drop = 100;
562 
563  need_rate = strategy->min_comp_rate;
564  if (need_rate < 0)
565  need_rate = 0;
566  else if (need_rate > 99)
567  need_rate = 99;
568 
569  /*
570  * Compute the maximum result size allowed by the strategy, namely the
571  * input size minus the minimum wanted compression rate. This had better
572  * be <= slen, else we might overrun the provided output buffer.
573  */
574  if (slen > (INT_MAX / 100))
575  {
576  /* Approximate to avoid overflow */
577  result_max = (slen / 100) * (100 - need_rate);
578  }
579  else
580  result_max = (slen * (100 - need_rate)) / 100;
581 
582  /*
583  * Experiments suggest that these hash sizes work pretty well. A large
584  * hash table minimizes collision, but has a higher startup cost. For a
585  * small input, the startup cost dominates. The table size must be a power
586  * of two.
587  */
588  if (slen < 128)
589  hashsz = 512;
590  else if (slen < 256)
591  hashsz = 1024;
592  else if (slen < 512)
593  hashsz = 2048;
594  else if (slen < 1024)
595  hashsz = 4096;
596  else
597  hashsz = 8192;
598  mask = hashsz - 1;
599 
600  /*
601  * Initialize the history lists to empty. We do not need to zero the
602  * hist_entries[] array; its entries are initialized as they are used.
603  */
604  memset(hist_start, 0, hashsz * sizeof(int16));
605 
606  /*
607  * Compress the source directly into the output buffer.
608  */
609  while (dp < dend)
610  {
611  /*
612  * If we already exceeded the maximum result size, fail.
613  *
614  * We check once per loop; since the loop body could emit as many as 4
615  * bytes (a control byte and 3-byte tag), PGLZ_MAX_OUTPUT() had better
616  * allow 4 slop bytes.
617  */
618  if (bp - bstart >= result_max)
619  return -1;
620 
621  /*
622  * If we've emitted more than first_success_by bytes without finding
623  * anything compressible at all, fail. This lets us fall out
624  * reasonably quickly when looking at incompressible input (such as
625  * pre-compressed data).
626  */
627  if (!found_match && bp - bstart >= strategy->first_success_by)
628  return -1;
629 
630  /*
631  * Try to find a match in the history
632  */
633  if (pglz_find_match(hist_start, dp, dend, &match_len,
634  &match_off, good_match, good_drop, mask))
635  {
636  /*
637  * Create the tag and add history entries for all matched
638  * characters.
639  */
640  pglz_out_tag(ctrlp, ctrlb, ctrl, bp, match_len, match_off);
641  while (match_len--)
642  {
644  hist_next, hist_recycle,
645  dp, dend, mask);
646  dp++; /* Do not do this ++ in the line above! */
647  /* The macro would do it four times - Jan. */
648  }
649  found_match = true;
650  }
651  else
652  {
653  /*
654  * No match found. Copy one literal byte.
655  */
656  pglz_out_literal(ctrlp, ctrlb, ctrl, bp, *dp);
658  hist_next, hist_recycle,
659  dp, dend, mask);
660  dp++; /* Do not do this ++ in the line above! */
661  /* The macro would do it four times - Jan. */
662  }
663  }
664 
665  /*
666  * Write out the last control byte and check that we haven't overrun the
667  * output size allowed by the strategy.
668  */
669  *ctrlp = ctrlb;
670  result_size = bp - bstart;
671  if (result_size >= result_max)
672  return -1;
673 
674  /* success */
675  return result_size;
676 }
signed short int16
Definition: c.h:346
#define pglz_hist_add(_hs, _he, _hn, _recycle, _s, _e, _mask)
static PGLZ_HistEntry hist_entries[PGLZ_HISTORY_SIZE+1]
signed int int32
Definition: c.h:347
int32 first_success_by
Definition: pg_lzcompress.h:62
int32 match_size_good
Definition: pg_lzcompress.h:63
#define pglz_out_tag(_ctrlp, _ctrlb, _ctrl, _buf, _len, _off)
int32 match_size_drop
Definition: pg_lzcompress.h:64
static int16 hist_start[PGLZ_MAX_HISTORY_LISTS]
static int pglz_find_match(int16 *hstart, const char *input, const char *end, int *lenp, int *offp, int good_match, int good_drop, int mask)
#define pglz_out_literal(_ctrlp, _ctrlb, _ctrl, _buf, _byte)
#define PGLZ_MAX_MATCH
int32 max_input_size
Definition: pg_lzcompress.h:60
const PGLZ_Strategy *const PGLZ_strategy_default
int32 min_comp_rate
Definition: pg_lzcompress.h:61

◆ pglz_decompress()

int32 pglz_decompress ( const char *  source,
int32  slen,
char *  dest,
int32  rawsize,
bool  check_complete 
)

Definition at line 689 of file pg_lzcompress.c.

References generate_unaccent_rules::dest, and Min.

Referenced by RestoreBlockImage(), toast_decompress_datum(), and toast_decompress_datum_slice().

691 {
692  const unsigned char *sp;
693  const unsigned char *srcend;
694  unsigned char *dp;
695  unsigned char *destend;
696 
697  sp = (const unsigned char *) source;
698  srcend = ((const unsigned char *) source) + slen;
699  dp = (unsigned char *) dest;
700  destend = dp + rawsize;
701 
702  while (sp < srcend && dp < destend)
703  {
704  /*
705  * Read one control byte and process the next 8 items (or as many as
706  * remain in the compressed input).
707  */
708  unsigned char ctrl = *sp++;
709  int ctrlc;
710 
711  for (ctrlc = 0; ctrlc < 8 && sp < srcend && dp < destend; ctrlc++)
712  {
713 
714  if (ctrl & 1)
715  {
716  /*
717  * Otherwise it contains the match length minus 3 and the
718  * upper 4 bits of the offset. The next following byte
719  * contains the lower 8 bits of the offset. If the length is
720  * coded as 18, another extension tag byte tells how much
721  * longer the match really was (0-255).
722  */
723  int32 len;
724  int32 off;
725 
726  len = (sp[0] & 0x0f) + 3;
727  off = ((sp[0] & 0xf0) << 4) | sp[1];
728  sp += 2;
729  if (len == 18)
730  len += *sp++;
731 
732  /*
733  * Now we copy the bytes specified by the tag from OUTPUT to
734  * OUTPUT. It is dangerous and platform dependent to use
735  * memcpy() here, because the copied areas could overlap
736  * extremely!
737  */
738  len = Min(len, destend - dp);
739  while (len--)
740  {
741  *dp = dp[-off];
742  dp++;
743  }
744  }
745  else
746  {
747  /*
748  * An unset control bit means LITERAL BYTE. So we just copy
749  * one from INPUT to OUTPUT.
750  */
751  *dp++ = *sp++;
752  }
753 
754  /*
755  * Advance the control bit
756  */
757  ctrl >>= 1;
758  }
759  }
760 
761  /*
762  * Check we decompressed the right amount. If we are slicing, then we
763  * won't necessarily be at the end of the source or dest buffers when we
764  * hit a stop, so we don't test them.
765  */
766  if (check_complete && (dp != destend || sp != srcend))
767  return -1;
768 
769  /*
770  * That's it.
771  */
772  return (char *) dp - dest;
773 }
#define Min(x, y)
Definition: c.h:905
signed int int32
Definition: c.h:347

◆ pglz_maximum_compressed_size()

int32 pglz_maximum_compressed_size ( int32  rawsize,
int32  total_compressed_size 
)

Definition at line 791 of file pg_lzcompress.c.

References Min.

Referenced by detoast_attr_slice().

792 {
793  int32 compressed_size;
794 
795  /*
796  * pglz uses one control bit per byte, so we need (rawsize * 9) bits. We
797  * care about bytes though, so we add 7 to make sure we include the last
798  * incomplete byte (integer division rounds down).
799  *
800  * XXX Use int64 to prevent overflow during calculation.
801  */
802  compressed_size = (int32) ((int64) rawsize * 9 + 7) / 8;
803 
804  /*
805  * Maximum compressed size can't be larger than total compressed size.
806  */
807  compressed_size = Min(compressed_size, total_compressed_size);
808 
809  return compressed_size;
810 }
#define Min(x, y)
Definition: c.h:905
signed int int32
Definition: c.h:347

Variable Documentation

◆ PGLZ_strategy_always

const PGLZ_Strategy* const PGLZ_strategy_always

Definition at line 248 of file pg_lzcompress.c.

◆ PGLZ_strategy_default

const PGLZ_Strategy* const PGLZ_strategy_default

Definition at line 236 of file pg_lzcompress.c.

Referenced by pglz_compress(), toast_compress_datum(), and XLogCompressBackupBlock().