Commit ce56a16a authored by Christopher Dykes's avatar Christopher Dykes Committed by Facebook Github Bot

Add a way to determine if a compression codec is supported at runtime

Summary: Folly supports being compiled without all of the compression libraries, so we need a way to determine at runtime what libraries Folly has been compiled to support.

Reviewed By: yfeldblum

Differential Revision: D4692556

fbshipit-source-id: 0ec459db70a4b1d64f1e07c87a1f51ae584bccd0
parent 03d240dc
......@@ -1132,63 +1132,71 @@ std::unique_ptr<IOBuf> ZSTDCodec::doUncompress(
} // namespace
std::unique_ptr<Codec> getCodec(CodecType type, int level) {
typedef std::unique_ptr<Codec> (*CodecFactory)(int, CodecType);
static CodecFactory codecFactories[
static_cast<size_t>(CodecType::NUM_CODEC_TYPES)] = {
nullptr, // USER_DEFINED
NoCompressionCodec::create,
typedef std::unique_ptr<Codec> (*CodecFactory)(int, CodecType);
static CodecFactory
codecFactories[static_cast<size_t>(CodecType::NUM_CODEC_TYPES)] = {
nullptr, // USER_DEFINED
NoCompressionCodec::create,
#if FOLLY_HAVE_LIBLZ4
LZ4Codec::create,
LZ4Codec::create,
#else
nullptr,
nullptr,
#endif
#if FOLLY_HAVE_LIBSNAPPY
SnappyCodec::create,
SnappyCodec::create,
#else
nullptr,
nullptr,
#endif
#if FOLLY_HAVE_LIBZ
ZlibCodec::create,
ZlibCodec::create,
#else
nullptr,
nullptr,
#endif
#if FOLLY_HAVE_LIBLZ4
LZ4Codec::create,
LZ4Codec::create,
#else
nullptr,
nullptr,
#endif
#if FOLLY_HAVE_LIBLZMA
LZMA2Codec::create,
LZMA2Codec::create,
LZMA2Codec::create,
LZMA2Codec::create,
#else
nullptr,
nullptr,
nullptr,
nullptr,
#endif
#if FOLLY_HAVE_LIBZSTD
ZSTDCodec::create,
ZSTDCodec::create,
#else
nullptr,
nullptr,
#endif
#if FOLLY_HAVE_LIBZ
ZlibCodec::create,
ZlibCodec::create,
#else
nullptr,
nullptr,
#endif
};
};
bool hasCodec(CodecType type) {
size_t idx = static_cast<size_t>(type);
if (idx >= static_cast<size_t>(CodecType::NUM_CODEC_TYPES)) {
throw std::invalid_argument(to<std::string>(
"Compression type ", idx, " not supported"));
throw std::invalid_argument(
to<std::string>("Compression type ", idx, " invalid"));
}
return codecFactories[idx] != nullptr;
}
std::unique_ptr<Codec> getCodec(CodecType type, int level) {
size_t idx = static_cast<size_t>(type);
if (idx >= static_cast<size_t>(CodecType::NUM_CODEC_TYPES)) {
throw std::invalid_argument(
to<std::string>("Compression type ", idx, " invalid"));
}
auto factory = codecFactories[idx];
if (!factory) {
......
......@@ -176,4 +176,9 @@ constexpr int COMPRESSION_LEVEL_BEST = -3;
std::unique_ptr<Codec> getCodec(CodecType type,
int level = COMPRESSION_LEVEL_DEFAULT);
/**
* Check if a specified codec is supported.
*/
bool hasCodec(CodecType type);
}} // namespaces
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment