* End compression stage. Rewind and prepare the BufFile for decompression. */
| 1370 | * End compression stage. Rewind and prepare the BufFile for decompression. |
| 1371 | */ |
| 1372 | static void |
| 1373 | BufFileEndCompression(BufFile *file) |
| 1374 | { |
| 1375 | ZSTD_outBuffer output; |
| 1376 | size_t ret; |
| 1377 | int wrote; |
| 1378 | off_t pos = 0; |
| 1379 | |
| 1380 | Assert(file->state == BFS_COMPRESSED_WRITING); |
| 1381 | |
| 1382 | do { |
| 1383 | output.dst = compression_buffer; |
| 1384 | output.size = BLCKSZ; |
| 1385 | output.pos = 0; |
| 1386 | |
| 1387 | ret = ZSTD_endStream(file->zstd_context->cctx, &output); |
| 1388 | if (ZSTD_isError(ret)) |
| 1389 | elog(ERROR, "%s", ZSTD_getErrorName(ret)); |
| 1390 | |
| 1391 | wrote = FileWrite(file->files[0], output.dst, output.pos, file->curOffset + file->pos + pos, WAIT_EVENT_BUFFILE_WRITE); |
| 1392 | if (wrote != output.pos) |
| 1393 | elog(ERROR, "could not write %d bytes to compressed temporary file: %m", (int) output.pos); |
| 1394 | pos += wrote; |
| 1395 | } while (ret > 0); |
| 1396 | |
| 1397 | ZSTD_freeCCtx(file->zstd_context->cctx); |
| 1398 | file->zstd_context->cctx = NULL; |
| 1399 | |
| 1400 | elog(DEBUG1, "BufFile compressed from %ld to %ld bytes", |
| 1401 | file->uncompressed_bytes, BufFileSize(file)); |
| 1402 | |
| 1403 | /* Done writing. Initialize for reading */ |
| 1404 | file->zstd_context->dctx = ZSTD_createDStream(); |
| 1405 | if (!file->zstd_context->dctx) |
| 1406 | elog(ERROR, "out of memory"); |
| 1407 | ret = ZSTD_initDStream(file->zstd_context->dctx); |
| 1408 | if (ZSTD_isError(ret)) |
| 1409 | elog(ERROR, "failed to initialize zstd dstream: %s", ZSTD_getErrorName(ret)); |
| 1410 | |
| 1411 | file->compressed_buffer.src = palloc(BLCKSZ); |
| 1412 | file->compressed_buffer.size = 0; |
| 1413 | file->compressed_buffer.pos = 0; |
| 1414 | file->state = BFS_RANDOM_ACCESS; |
| 1415 | |
| 1416 | if (BufFileSeek(file, 0, 0, SEEK_SET) != 0) |
| 1417 | elog(ERROR, "could not seek in temporary file: %m"); |
| 1418 | |
| 1419 | file->state = BFS_COMPRESSED_READING; |
| 1420 | } |
| 1421 | |
| 1422 | static int |
| 1423 | BufFileLoadCompressedBuffer(BufFile *file, void *buffer, size_t bufsize) |
no test coverage detected