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, if specified */
88  if ((compress->options & PG_COMPRESSION_OPTION_LEVEL) != 0)
89  {
90  ret = ZSTD_CCtx_setParameter(streamer->cctx, ZSTD_c_compressionLevel,
91  compress->level);
92  if (ZSTD_isError(ret))
93  pg_fatal("could not set zstd compression level to %d: %s",
94  compress->level, ZSTD_getErrorName(ret));
95  }
96 
97  /* Set # of workers, if specified */
98  if ((compress->options & PG_COMPRESSION_OPTION_WORKERS) != 0)
99  {
100  /*
101  * On older versions of libzstd, this option does not exist, and
102  * trying to set it will fail. Similarly for newer versions if they
103  * are compiled without threading support.
104  */
105  ret = ZSTD_CCtx_setParameter(streamer->cctx, ZSTD_c_nbWorkers,
106  compress->workers);
107  if (ZSTD_isError(ret))
108  pg_fatal("could not set compression worker count to %d: %s",
109  compress->workers, ZSTD_getErrorName(ret));
110  }
111 
112  /* Initialize the ZSTD output buffer. */
113  streamer->zstd_outBuf.dst = streamer->base.bbs_buffer.data;
114  streamer->zstd_outBuf.size = streamer->base.bbs_buffer.maxlen;
115  streamer->zstd_outBuf.pos = 0;
116 
117  return &streamer->base;
118 #else
119  pg_fatal("this build does not support zstd compression");
120  return NULL; /* keep compiler quiet */
121 #endif
122 }
static int32 next
Definition: blutils.c:219
#define PG_COMPRESSION_OPTION_LEVEL
Definition: compression.h:25
#define PG_COMPRESSION_OPTION_WORKERS
Definition: compression.h:26
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc0(Size size)
Definition: mcxt.c:1099
#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_LEVEL, 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 248 of file bbstreamer_zstd.c.

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

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

Referenced by CreateBackupStreamer().