25 pg_fatal(
"this build does not support compression with %s",
"ZSTD");
31 pg_fatal(
"this build does not support compression with %s",
"ZSTD");
38typedef struct ZstdCompressorState
43 ZSTD_CStream *cstream;
44 ZSTD_DStream *dstream;
49 const char *zstderror;
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);
149 const void *
data,
size_t dLen)
151 ZstdCompressorState *zstdcs = (ZstdCompressorState *) cs->
private_data;
153 zstdcs->input.src =
data;
154 zstdcs->input.size = dLen;
155 zstdcs->input.pos = 0;
157 _ZstdWriteCommon(AH, cs,
false);
163 ZstdCompressorState *zstdcs = (ZstdCompressorState *) cs->
private_data;
164 ZSTD_outBuffer *
output = &zstdcs->output;
165 ZSTD_inBuffer *
input = &zstdcs->input;
166 size_t input_allocated_size = ZSTD_DStreamInSize();
177 input->size = input_allocated_size;
182 input_allocated_size =
input->size;
194 if (ZSTD_isError(
res))
195 pg_fatal(
"could not decompress data: %s", ZSTD_getErrorName(
res));
214 ZstdCompressorState *zstdcs;
216 cs->
readData = ReadDataFromArchiveZstd;
218 cs->
end = EndCompressorZstd;
222 zstdcs = (ZstdCompressorState *)
pg_malloc0(
sizeof(*zstdcs));
228 if (cs->
readF != NULL)
230 zstdcs->dstream = ZSTD_createDStream();
231 if (zstdcs->dstream == NULL)
232 pg_fatal(
"could not initialize compression library");
234 zstdcs->input.size = ZSTD_DStreamInSize();
235 zstdcs->input.src =
pg_malloc(zstdcs->input.size);
243 zstdcs->output.size = ZSTD_DStreamOutSize();
244 zstdcs->output.dst =
pg_malloc(zstdcs->output.size + 1);
246 else if (cs->
writeF != NULL)
250 zstdcs->output.size = ZSTD_CStreamOutSize();
251 zstdcs->output.dst =
pg_malloc(zstdcs->output.size);
252 zstdcs->output.pos = 0;
263 ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->
private_data;
264 ZSTD_inBuffer *
input = &zstdcs->input;
265 ZSTD_outBuffer *
output = &zstdcs->output;
266 size_t input_allocated_size = ZSTD_DStreamInSize();
293 cnt = fread(
unconstify(
void *,
input->src), 1, input_allocated_size, zstdcs->fp);
296 Assert(cnt <= input_allocated_size);
308 if (ZSTD_isError(
res))
309 pg_fatal(
"could not decompress data: %s", ZSTD_getErrorName(
res));
331 ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->
private_data;
332 ZSTD_inBuffer *
input = &zstdcs->input;
333 ZSTD_outBuffer *
output = &zstdcs->output;
345 res = ZSTD_compressStream2(zstdcs->cstream,
output,
input, ZSTD_e_continue);
346 if (ZSTD_isError(
res))
348 zstdcs->zstderror = ZSTD_getErrorName(
res);
355 zstdcs->zstderror =
strerror(errno);
366 ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->
private_data;
371 if (feof(zstdcs->fp))
372 pg_fatal(
"could not read from input file: end of file");
374 pg_fatal(
"could not read from input file: %m");
390 for (
i = 0;
i <
len - 1; ++
i)
405 return i > 0 ?
buf : NULL;
411 ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->
private_data;
417 ZSTD_inBuffer *
input = &zstdcs->input;
418 ZSTD_outBuffer *
output = &zstdcs->output;
424 res = ZSTD_compressStream2(zstdcs->cstream,
output,
input, ZSTD_e_end);
425 if (ZSTD_isError(
res))
427 zstdcs->zstderror = ZSTD_getErrorName(
res);
434 zstdcs->zstderror =
strerror(errno);
442 ZSTD_freeCStream(zstdcs->cstream);
448 ZSTD_freeDStream(zstdcs->dstream);
452 if (fclose(zstdcs->fp) != 0)
462 ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->
private_data;
464 return feof(zstdcs->fp);
468Zstd_open(
const char *path,
int fd,
const char *
mode,
472 ZstdCompressorState *zstdcs;
477 fp = fopen(path,
mode);
482 zstdcs = (ZstdCompressorState *)
pg_malloc0(
sizeof(*zstdcs));
488 zstdcs->input.src =
pg_malloc0(ZSTD_DStreamInSize());
489 zstdcs->dstream = ZSTD_createDStream();
490 if (zstdcs->dstream == NULL)
491 pg_fatal(
"could not initialize compression library");
493 else if (
mode[0] ==
'w' ||
mode[0] ==
'a')
495 zstdcs->output.size = ZSTD_CStreamOutSize();
496 zstdcs->output.dst =
pg_malloc0(zstdcs->output.size);
498 if (zstdcs->cstream == NULL)
499 pg_fatal(
"could not initialize compression library");
512 sprintf(fname,
"%s.zst", path);
519 ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->
private_data;
521 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_malloc(size_t size)
void * pg_malloc0(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
char *(* gets_func)(char *s, int size, CompressFileHandle *CFH)
bool(* open_write_func)(const char *path, const char *mode, CompressFileHandle *CFH)
bool(* write_func)(const void *ptr, size_t size, struct CompressFileHandle *CFH)
int(* getc_func)(CompressFileHandle *CFH)
const char *(* get_error_func)(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)