25 pg_fatal(
"this build does not support compression with %s",
"ZSTD");
31 pg_fatal(
"this build does not support compression with %s",
"ZSTD");
38 typedef struct ZstdCompressorState
43 ZSTD_CStream *cstream;
44 ZSTD_DStream *dstream;
49 const char *zstderror;
50 } ZstdCompressorState;
55 const void *
data,
size_t dLen);
59 _Zstd_CCtx_setParam_or_die(ZSTD_CStream *cstream,
60 ZSTD_cParameter param,
int value,
char *paramname)
64 res = ZSTD_CCtx_setParameter(cstream, param,
value);
65 if (ZSTD_isError(
res))
66 pg_fatal(
"could not set compression parameter \"%s\": %s",
67 paramname, ZSTD_getErrorName(
res));
74 ZSTD_CStream *cstream;
76 cstream = ZSTD_createCStream();
78 pg_fatal(
"could not initialize compression library");
80 _Zstd_CCtx_setParam_or_die(cstream, ZSTD_c_compressionLevel,
81 compress.
level,
"level");
84 _Zstd_CCtx_setParam_or_die(cstream,
85 ZSTD_c_enableLongDistanceMatching,
95 ZstdCompressorState *zstdcs = (ZstdCompressorState *) cs->
private_data;
96 ZSTD_inBuffer *
input = &zstdcs->input;
97 ZSTD_outBuffer *
output = &zstdcs->output;
105 res = ZSTD_compressStream2(zstdcs->cstream,
output,
106 input, flush ? ZSTD_e_end : ZSTD_e_continue);
108 if (ZSTD_isError(
res))
109 pg_fatal(
"could not compress data: %s", ZSTD_getErrorName(
res));
127 ZstdCompressorState *zstdcs = (ZstdCompressorState *) cs->
private_data;
131 Assert(zstdcs->cstream == NULL);
132 ZSTD_freeDStream(zstdcs->dstream);
135 else if (cs->
writeF != NULL)
137 Assert(zstdcs->dstream == NULL);
138 _ZstdWriteCommon(AH, cs,
true);
139 ZSTD_freeCStream(zstdcs->cstream);
148 const void *
data,
size_t dLen)
150 ZstdCompressorState *zstdcs = (ZstdCompressorState *) cs->
private_data;
152 zstdcs->input.src =
data;
153 zstdcs->input.size = dLen;
154 zstdcs->input.pos = 0;
156 _ZstdWriteCommon(AH, cs,
false);
162 ZstdCompressorState *zstdcs = (ZstdCompressorState *) cs->
private_data;
163 ZSTD_outBuffer *
output = &zstdcs->output;
164 ZSTD_inBuffer *
input = &zstdcs->input;
165 size_t input_allocated_size = ZSTD_DStreamInSize();
176 input->size = input_allocated_size;
181 input_allocated_size =
input->size;
193 if (ZSTD_isError(
res))
194 pg_fatal(
"could not decompress data: %s", ZSTD_getErrorName(
res));
213 ZstdCompressorState *zstdcs;
215 cs->
readData = ReadDataFromArchiveZstd;
217 cs->
end = EndCompressorZstd;
221 zstdcs = (ZstdCompressorState *)
pg_malloc0(
sizeof(*zstdcs));
227 if (cs->
readF != NULL)
229 zstdcs->dstream = ZSTD_createDStream();
230 if (zstdcs->dstream == NULL)
231 pg_fatal(
"could not initialize compression library");
233 zstdcs->input.size = ZSTD_DStreamInSize();
234 zstdcs->input.src =
pg_malloc(zstdcs->input.size);
242 zstdcs->output.size = ZSTD_DStreamOutSize();
243 zstdcs->output.dst =
pg_malloc(zstdcs->output.size + 1);
245 else if (cs->
writeF != NULL)
249 zstdcs->output.size = ZSTD_CStreamOutSize();
250 zstdcs->output.dst =
pg_malloc(zstdcs->output.size);
251 zstdcs->output.pos = 0;
262 ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->
private_data;
263 ZSTD_inBuffer *
input = &zstdcs->input;
264 ZSTD_outBuffer *
output = &zstdcs->output;
265 size_t input_allocated_size = ZSTD_DStreamInSize();
292 cnt = fread(
unconstify(
void *,
input->src), 1, input_allocated_size, zstdcs->fp);
295 Assert(cnt <= input_allocated_size);
307 if (ZSTD_isError(
res))
308 pg_fatal(
"could not decompress data: %s", ZSTD_getErrorName(
res));
330 ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->
private_data;
331 ZSTD_inBuffer *
input = &zstdcs->input;
332 ZSTD_outBuffer *
output = &zstdcs->output;
344 res = ZSTD_compressStream2(zstdcs->cstream,
output,
input, ZSTD_e_continue);
345 if (ZSTD_isError(
res))
347 zstdcs->zstderror = ZSTD_getErrorName(
res);
354 zstdcs->zstderror =
strerror(errno);
365 ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->
private_data;
370 if (feof(zstdcs->fp))
371 pg_fatal(
"could not read from input file: end of file");
373 pg_fatal(
"could not read from input file: %m");
389 for (
i = 0;
i <
len - 1; ++
i)
404 return i > 0 ?
buf : NULL;
410 ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->
private_data;
416 ZSTD_inBuffer *
input = &zstdcs->input;
417 ZSTD_outBuffer *
output = &zstdcs->output;
423 res = ZSTD_compressStream2(zstdcs->cstream,
output,
input, ZSTD_e_end);
424 if (ZSTD_isError(
res))
426 zstdcs->zstderror = ZSTD_getErrorName(
res);
433 zstdcs->zstderror =
strerror(errno);
441 ZSTD_freeCStream(zstdcs->cstream);
447 ZSTD_freeDStream(zstdcs->dstream);
451 if (fclose(zstdcs->fp) != 0)
461 ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->
private_data;
463 return feof(zstdcs->fp);
467 Zstd_open(
const char *path,
int fd,
const char *
mode,
471 ZstdCompressorState *zstdcs;
476 fp = fopen(path,
mode);
481 zstdcs = (ZstdCompressorState *)
pg_malloc0(
sizeof(*zstdcs));
487 zstdcs->input.src =
pg_malloc0(ZSTD_DStreamInSize());
488 zstdcs->dstream = ZSTD_createDStream();
489 if (zstdcs->dstream == NULL)
490 pg_fatal(
"could not initialize compression library");
492 else if (
mode[0] ==
'w' ||
mode[0] ==
'a')
494 zstdcs->output.size = ZSTD_CStreamOutSize();
495 zstdcs->output.dst =
pg_malloc0(zstdcs->output.size);
497 if (zstdcs->cstream == NULL)
498 pg_fatal(
"could not initialize compression library");
511 sprintf(fname,
"%s.zst", path);
518 ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->
private_data;
520 return zstdcs->zstderror;
#define unconstify(underlying_type, expr)
#define Assert(condition)
void InitCompressorZstd(CompressorState *cs, const pg_compress_specification compression_spec)
void InitCompressFileHandleZstd(CompressFileHandle *CFH, const pg_compress_specification compression_spec)
#define PG_COMPRESSION_OPTION_LONG_DISTANCE
static void PGresult * res
void * pg_malloc0(size_t size)
void * pg_malloc(size_t size)
if(TABLE==NULL||TABLE_index==NULL)
void ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
static PgChecksumMode mode
static int fd(const char *x, int i)
static pg_noinline void Size size
bool(* open_write_func)(const char *path, const char *mode, CompressFileHandle *CFH)
const char *(* get_error_func)(CompressFileHandle *CFH)
bool(* write_func)(const void *ptr, size_t size, struct CompressFileHandle *CFH)
int(* getc_func)(CompressFileHandle *CFH)
char *(* gets_func)(char *s, int size, CompressFileHandle *CFH)
bool(* eof_func)(CompressFileHandle *CFH)
bool(* open_func)(const char *path, int fd, const char *mode, CompressFileHandle *CFH)
pg_compress_specification compression_spec
bool(* close_func)(CompressFileHandle *CFH)
bool(* read_func)(void *ptr, size_t size, size_t *rsize, CompressFileHandle *CFH)
void(* readData)(ArchiveHandle *AH, CompressorState *cs)
pg_compress_specification compression_spec
void(* end)(ArchiveHandle *AH, CompressorState *cs)
void(* writeData)(ArchiveHandle *AH, CompressorState *cs, const void *data, size_t dLen)