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

Go to the source code of this file.

Functions

bbstreamerbbstreamer_zstd_compressor_new (bbstreamer *next, pg_compress_specification *compress)
 
bbstreamerbbstreamer_zstd_decompressor_new (bbstreamer *next)
 

Function Documentation

◆ bbstreamer_zstd_compressor_new()

bbstreamer* bbstreamer_zstd_compressor_new ( bbstreamer next,
pg_compress_specification compress 
)

Definition at line 66 of file bbstreamer_zstd.c.

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

◆ bbstreamer_zstd_decompressor_new()

bbstreamer* bbstreamer_zstd_decompressor_new ( bbstreamer next)

Definition at line 258 of file bbstreamer_zstd.c.

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

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

Referenced by CreateBackupStreamer().