2016-09-01 22:22:19 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2016-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "ErrorHolder.h"
|
2016-10-13 02:02:27 +00:00
|
|
|
#include "Logging.h"
|
2016-09-01 22:22:19 +00:00
|
|
|
#include "Options.h"
|
|
|
|
#include "utils/Buffer.h"
|
|
|
|
#include "utils/Range.h"
|
2016-10-12 22:18:16 +00:00
|
|
|
#include "utils/ResourcePool.h"
|
2016-09-01 22:22:19 +00:00
|
|
|
#include "utils/ThreadPool.h"
|
|
|
|
#include "utils/WorkQueue.h"
|
|
|
|
#define ZSTD_STATIC_LINKING_ONLY
|
|
|
|
#include "zstd.h"
|
|
|
|
#undef ZSTD_STATIC_LINKING_ONLY
|
|
|
|
|
|
|
|
#include <cstddef>
|
2016-09-07 03:11:02 +00:00
|
|
|
#include <cstdint>
|
2016-09-01 22:22:19 +00:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace pzstd {
|
|
|
|
/**
|
|
|
|
* Runs pzstd with `options` and returns the number of bytes written.
|
|
|
|
* An error occurred if `errorHandler.hasError()`.
|
|
|
|
*
|
|
|
|
* @param options The pzstd options to use for (de)compression
|
2016-09-21 21:29:47 +00:00
|
|
|
* @returns 0 upon success and non-zero on failure.
|
2016-09-01 22:22:19 +00:00
|
|
|
*/
|
2016-09-21 21:29:47 +00:00
|
|
|
int pzstdMain(const Options& options);
|
2016-09-01 22:22:19 +00:00
|
|
|
|
2016-10-12 22:18:16 +00:00
|
|
|
class SharedState {
|
|
|
|
public:
|
2016-10-13 02:02:27 +00:00
|
|
|
SharedState(const Options& options) : log(options.verbosity) {
|
|
|
|
if (!options.decompress) {
|
|
|
|
auto parameters = options.determineParameters();
|
2016-10-13 00:23:38 +00:00
|
|
|
cStreamPool.reset(new ResourcePool<ZSTD_CStream>{
|
2016-11-16 00:39:09 +00:00
|
|
|
[this, parameters]() -> ZSTD_CStream* {
|
2017-03-06 03:36:56 +00:00
|
|
|
this->log(VERBOSE, "%s\n", "Creating new ZSTD_CStream");
|
2016-10-13 00:23:38 +00:00
|
|
|
auto zcs = ZSTD_createCStream();
|
|
|
|
if (zcs) {
|
|
|
|
auto err = ZSTD_initCStream_advanced(
|
|
|
|
zcs, nullptr, 0, parameters, 0);
|
|
|
|
if (ZSTD_isError(err)) {
|
|
|
|
ZSTD_freeCStream(zcs);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return zcs;
|
|
|
|
},
|
|
|
|
[](ZSTD_CStream *zcs) {
|
|
|
|
ZSTD_freeCStream(zcs);
|
|
|
|
}});
|
|
|
|
} else {
|
|
|
|
dStreamPool.reset(new ResourcePool<ZSTD_DStream>{
|
2016-11-16 00:39:09 +00:00
|
|
|
[this]() -> ZSTD_DStream* {
|
2017-03-06 03:36:56 +00:00
|
|
|
this->log(VERBOSE, "%s\n", "Creating new ZSTD_DStream");
|
2016-10-13 00:23:38 +00:00
|
|
|
auto zds = ZSTD_createDStream();
|
|
|
|
if (zds) {
|
|
|
|
auto err = ZSTD_initDStream(zds);
|
|
|
|
if (ZSTD_isError(err)) {
|
|
|
|
ZSTD_freeDStream(zds);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return zds;
|
|
|
|
},
|
|
|
|
[](ZSTD_DStream *zds) {
|
|
|
|
ZSTD_freeDStream(zds);
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 00:39:09 +00:00
|
|
|
~SharedState() {
|
|
|
|
// The resource pools have references to this, so destroy them first.
|
|
|
|
cStreamPool.reset();
|
|
|
|
dStreamPool.reset();
|
|
|
|
}
|
|
|
|
|
2016-10-13 02:02:27 +00:00
|
|
|
Logger log;
|
2016-10-12 22:18:16 +00:00
|
|
|
ErrorHolder errorHolder;
|
2016-10-13 00:23:38 +00:00
|
|
|
std::unique_ptr<ResourcePool<ZSTD_CStream>> cStreamPool;
|
|
|
|
std::unique_ptr<ResourcePool<ZSTD_DStream>> dStreamPool;
|
2016-10-12 22:18:16 +00:00
|
|
|
};
|
|
|
|
|
2016-09-01 22:22:19 +00:00
|
|
|
/**
|
|
|
|
* Streams input from `fd`, breaks input up into chunks, and compresses each
|
|
|
|
* chunk independently. Output of each chunk gets streamed to a queue, and
|
|
|
|
* the output queues get put into `chunks` in order.
|
|
|
|
*
|
2016-10-12 22:18:16 +00:00
|
|
|
* @param state The shared state
|
2016-09-01 22:22:19 +00:00
|
|
|
* @param chunks Each compression jobs output queue gets `pushed()` here
|
|
|
|
* as soon as it is available
|
|
|
|
* @param executor The thread pool to run compression jobs in
|
|
|
|
* @param fd The input file descriptor
|
|
|
|
* @param size The size of the input file if known, 0 otherwise
|
|
|
|
* @param numThreads The number of threads in the thread pool
|
|
|
|
* @param parameters The zstd parameters to use for compression
|
2016-09-23 19:55:21 +00:00
|
|
|
* @returns The number of bytes read from the file
|
2016-09-01 22:22:19 +00:00
|
|
|
*/
|
2016-09-23 19:55:21 +00:00
|
|
|
std::uint64_t asyncCompressChunks(
|
2016-10-12 22:18:16 +00:00
|
|
|
SharedState& state,
|
2016-09-01 22:22:19 +00:00
|
|
|
WorkQueue<std::shared_ptr<BufferWorkQueue>>& chunks,
|
|
|
|
ThreadPool& executor,
|
|
|
|
FILE* fd,
|
2016-09-07 03:11:02 +00:00
|
|
|
std::uintmax_t size,
|
2016-09-01 22:22:19 +00:00
|
|
|
std::size_t numThreads,
|
|
|
|
ZSTD_parameters parameters);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Streams input from `fd`. If pzstd headers are available it breaks the input
|
|
|
|
* up into independent frames. It sends each frame to an independent
|
|
|
|
* decompression job. Output of each frame gets streamed to a queue, and
|
|
|
|
* the output queues get put into `frames` in order.
|
|
|
|
*
|
2016-10-12 22:18:16 +00:00
|
|
|
* @param state The shared state
|
2016-09-01 22:22:19 +00:00
|
|
|
* @param frames Each decompression jobs output queue gets `pushed()` here
|
|
|
|
* as soon as it is available
|
|
|
|
* @param executor The thread pool to run compression jobs in
|
|
|
|
* @param fd The input file descriptor
|
2016-09-23 19:55:21 +00:00
|
|
|
* @returns The number of bytes read from the file
|
2016-09-01 22:22:19 +00:00
|
|
|
*/
|
2016-09-23 19:55:21 +00:00
|
|
|
std::uint64_t asyncDecompressFrames(
|
2016-10-12 22:18:16 +00:00
|
|
|
SharedState& state,
|
2016-09-01 22:22:19 +00:00
|
|
|
WorkQueue<std::shared_ptr<BufferWorkQueue>>& frames,
|
|
|
|
ThreadPool& executor,
|
|
|
|
FILE* fd);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Streams input in from each queue in `outs` in order, and writes the data to
|
|
|
|
* `outputFd`.
|
|
|
|
*
|
2016-10-12 22:18:16 +00:00
|
|
|
* @param state The shared state
|
2016-09-21 21:29:47 +00:00
|
|
|
* @param outs A queue of output queues, one for each
|
|
|
|
* (de)compression job.
|
|
|
|
* @param outputFd The file descriptor to write to
|
|
|
|
* @param decompress Are we decompressing?
|
|
|
|
* @returns The number of bytes written
|
2016-09-01 22:22:19 +00:00
|
|
|
*/
|
2016-09-23 19:55:21 +00:00
|
|
|
std::uint64_t writeFile(
|
2016-10-12 22:18:16 +00:00
|
|
|
SharedState& state,
|
2016-09-01 22:22:19 +00:00
|
|
|
WorkQueue<std::shared_ptr<BufferWorkQueue>>& outs,
|
|
|
|
FILE* outputFd,
|
2016-10-13 02:02:27 +00:00
|
|
|
bool decompress);
|
2016-09-01 22:22:19 +00:00
|
|
|
}
|