* Initialize the compressor. */
| 1284 | * Initialize the compressor. |
| 1285 | */ |
| 1286 | static void |
| 1287 | BufFileStartCompression(BufFile *file) |
| 1288 | { |
| 1289 | ResourceOwner oldowner; |
| 1290 | size_t ret; |
| 1291 | |
| 1292 | /* |
| 1293 | * When working with compressed files, we rely on libzstd's buffer, |
| 1294 | * and the BufFile's own buffer is unused. It's a bit silly that we |
| 1295 | * allocate it in makeBufFile(), just to free it here again, but it |
| 1296 | * doesn't seem worth the trouble to avoid that either. |
| 1297 | */ |
| 1298 | if (file->buffer.data) |
| 1299 | { |
| 1300 | pfree(file->buffer.data); |
| 1301 | file->buffer.data = NULL; |
| 1302 | } |
| 1303 | |
| 1304 | if (compression_buffer == NULL) |
| 1305 | compression_buffer = MemoryContextAlloc(TopMemoryContext, BLCKSZ); |
| 1306 | |
| 1307 | /* |
| 1308 | * Make sure the zstd handle is kept in the same resource owner as |
| 1309 | * the underlying file. In the typical use, when BufFileCompressOK is |
| 1310 | * called immediately after opening the file, this wouldn't be |
| 1311 | * necessary, but better safe than sorry. |
| 1312 | */ |
| 1313 | oldowner = CurrentResourceOwner; |
| 1314 | CurrentResourceOwner = file->resowner; |
| 1315 | |
| 1316 | file->zstd_context = zstd_alloc_context(); |
| 1317 | file->zstd_context->cctx = ZSTD_createCStream(); |
| 1318 | if (!file->zstd_context->cctx) |
| 1319 | elog(ERROR, "out of memory"); |
| 1320 | ret = ZSTD_initCStream(file->zstd_context->cctx, BUFFILE_ZSTD_COMPRESSION_LEVEL); |
| 1321 | if (ZSTD_isError(ret)) |
| 1322 | elog(ERROR, "failed to initialize zstd stream: %s", ZSTD_getErrorName(ret)); |
| 1323 | |
| 1324 | CurrentResourceOwner = oldowner; |
| 1325 | |
| 1326 | file->state = BFS_COMPRESSED_WRITING; |
| 1327 | } |
| 1328 | |
| 1329 | static void |
| 1330 | BufFileDumpCompressedBuffer(BufFile *file, const void *buffer, Size nbytes) |
no test coverage detected