PostgreSQL Source Code  git master
astreamer_zstd.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include "common/logging.h"
#include "fe_utils/astreamer.h"
Include dependency graph for astreamer_zstd.c:

Go to the source code of this file.

Functions

astreamerastreamer_zstd_compressor_new (astreamer *next, pg_compress_specification *compress)
 
astreamerastreamer_zstd_decompressor_new (astreamer *next)
 

Function Documentation

◆ astreamer_zstd_compressor_new()

astreamer* astreamer_zstd_compressor_new ( astreamer next,
pg_compress_specification compress 
)

Definition at line 70 of file astreamer_zstd.c.

71 {
72 #ifdef USE_ZSTD
73  astreamer_zstd_frame *streamer;
74  size_t ret;
75 
76  Assert(next != NULL);
77 
78  streamer = palloc0(sizeof(astreamer_zstd_frame));
79 
80  *((const astreamer_ops **) &streamer->base.bbs_ops) =
81  &astreamer_zstd_compressor_ops;
82 
83  streamer->base.bbs_next = next;
84  initStringInfo(&streamer->base.bbs_buffer);
85  enlargeStringInfo(&streamer->base.bbs_buffer, ZSTD_DStreamOutSize());
86 
87  streamer->cctx = ZSTD_createCCtx();
88  if (!streamer->cctx)
89  pg_fatal("could not create zstd compression context");
90 
91  /* Set compression level */
92  ret = ZSTD_CCtx_setParameter(streamer->cctx, ZSTD_c_compressionLevel,
93  compress->level);
94  if (ZSTD_isError(ret))
95  pg_fatal("could not set zstd compression level to %d: %s",
96  compress->level, ZSTD_getErrorName(ret));
97 
98  /* Set # of workers, if specified */
99  if ((compress->options & PG_COMPRESSION_OPTION_WORKERS) != 0)
100  {
101  /*
102  * On older versions of libzstd, this option does not exist, and
103  * trying to set it will fail. Similarly for newer versions if they
104  * are compiled without threading support.
105  */
106  ret = ZSTD_CCtx_setParameter(streamer->cctx, ZSTD_c_nbWorkers,
107  compress->workers);
108  if (ZSTD_isError(ret))
109  pg_fatal("could not set compression worker count to %d: %s",
110  compress->workers, ZSTD_getErrorName(ret));
111  }
112 
113  if ((compress->options & PG_COMPRESSION_OPTION_LONG_DISTANCE) != 0)
114  {
115  ret = ZSTD_CCtx_setParameter(streamer->cctx,
116  ZSTD_c_enableLongDistanceMatching,
117  compress->long_distance);
118  if (ZSTD_isError(ret))
119  {
120  pg_log_error("could not enable long-distance mode: %s",
121  ZSTD_getErrorName(ret));
122  exit(1);
123  }
124  }
125 
126  /* Initialize the ZSTD output buffer. */
127  streamer->zstd_outBuf.dst = streamer->base.bbs_buffer.data;
128  streamer->zstd_outBuf.size = streamer->base.bbs_buffer.maxlen;
129  streamer->zstd_outBuf.pos = 0;
130 
131  return &streamer->base;
132 #else
133  pg_fatal("this build does not support compression with %s", "ZSTD");
134  return NULL; /* keep compiler quiet */
135 #endif
136 }
static int32 next
Definition: blutils.c:222
#define Assert(condition)
Definition: c.h:849
#define PG_COMPRESSION_OPTION_WORKERS
Definition: compression.h:29
#define PG_COMPRESSION_OPTION_LONG_DISTANCE
Definition: compression.h:30
exit(1)
#define pg_log_error(...)
Definition: logging.h:106
void * palloc0(Size size)
Definition: mcxt.c:1347
#define pg_fatal(...)
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:289
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59

References Assert, enlargeStringInfo(), exit(), initStringInfo(), pg_compress_specification::level, pg_compress_specification::long_distance, next, pg_compress_specification::options, palloc0(), PG_COMPRESSION_OPTION_LONG_DISTANCE, PG_COMPRESSION_OPTION_WORKERS, pg_fatal, pg_log_error, and pg_compress_specification::workers.

Referenced by CreateBackupStreamer().

◆ astreamer_zstd_decompressor_new()

astreamer* astreamer_zstd_decompressor_new ( astreamer next)

Definition at line 262 of file astreamer_zstd.c.

263 {
264 #ifdef USE_ZSTD
265  astreamer_zstd_frame *streamer;
266 
267  Assert(next != NULL);
268 
269  streamer = palloc0(sizeof(astreamer_zstd_frame));
270  *((const astreamer_ops **) &streamer->base.bbs_ops) =
271  &astreamer_zstd_decompressor_ops;
272 
273  streamer->base.bbs_next = next;
274  initStringInfo(&streamer->base.bbs_buffer);
275  enlargeStringInfo(&streamer->base.bbs_buffer, ZSTD_DStreamOutSize());
276 
277  streamer->dctx = ZSTD_createDCtx();
278  if (!streamer->dctx)
279  pg_fatal("could not create zstd decompression context");
280 
281  /* Initialize the ZSTD output buffer. */
282  streamer->zstd_outBuf.dst = streamer->base.bbs_buffer.data;
283  streamer->zstd_outBuf.size = streamer->base.bbs_buffer.maxlen;
284  streamer->zstd_outBuf.pos = 0;
285 
286  return &streamer->base;
287 #else
288  pg_fatal("this build does not support compression with %s", "ZSTD");
289  return NULL; /* keep compiler quiet */
290 #endif
291 }

References Assert, enlargeStringInfo(), initStringInfo(), next, palloc0(), and pg_fatal.

Referenced by create_archive_verifier(), and CreateBackupStreamer().