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  /* Initialize the ZSTD output buffer. */
110  streamer->zstd_outBuf.dst = streamer->base.bbs_buffer.data;
111  streamer->zstd_outBuf.size = streamer->base.bbs_buffer.maxlen;
112  streamer->zstd_outBuf.pos = 0;
113 
114  return &streamer->base;
115 #else
116  pg_fatal("this build does not support compression with %s", "ZSTD");
117  return NULL; /* keep compiler quiet */
118 #endif
119 }
static int32 next
Definition: blutils.c:219
#define PG_COMPRESSION_OPTION_WORKERS
Definition: compression.h:25
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc0(Size size)
Definition: mcxt.c:1241
#define pg_fatal(...)
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:283
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59

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

Referenced by CreateBackupStreamer().

◆ bbstreamer_zstd_decompressor_new()

bbstreamer* bbstreamer_zstd_decompressor_new ( bbstreamer next)

Definition at line 245 of file bbstreamer_zstd.c.

246 {
247 #ifdef USE_ZSTD
248  bbstreamer_zstd_frame *streamer;
249 
250  Assert(next != NULL);
251 
252  streamer = palloc0(sizeof(bbstreamer_zstd_frame));
253  *((const bbstreamer_ops **) &streamer->base.bbs_ops) =
254  &bbstreamer_zstd_decompressor_ops;
255 
256  streamer->base.bbs_next = next;
257  initStringInfo(&streamer->base.bbs_buffer);
258  enlargeStringInfo(&streamer->base.bbs_buffer, ZSTD_DStreamOutSize());
259 
260  streamer->dctx = ZSTD_createDCtx();
261  if (!streamer->dctx)
262  pg_fatal("could not create zstd decompression context");
263 
264  /* Initialize the ZSTD output buffer. */
265  streamer->zstd_outBuf.dst = streamer->base.bbs_buffer.data;
266  streamer->zstd_outBuf.size = streamer->base.bbs_buffer.maxlen;
267  streamer->zstd_outBuf.pos = 0;
268 
269  return &streamer->base;
270 #else
271  pg_fatal("this build does not support compression with %s", "ZSTD");
272  return NULL; /* keep compiler quiet */
273 #endif
274 }

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

Referenced by CreateBackupStreamer().