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( ...@@ -1132,63 +1132,71 @@ std::unique_ptr<IOBuf> ZSTDCodec::doUncompress(
} // namespace } // namespace
std::unique_ptr<Codec> getCodec(CodecType type, int level) { typedef std::unique_ptr<Codec> (*CodecFactory)(int, CodecType);
typedef std::unique_ptr<Codec> (*CodecFactory)(int, CodecType); static CodecFactory
codecFactories[static_cast<size_t>(CodecType::NUM_CODEC_TYPES)] = {
static CodecFactory codecFactories[ nullptr, // USER_DEFINED
static_cast<size_t>(CodecType::NUM_CODEC_TYPES)] = { NoCompressionCodec::create,
nullptr, // USER_DEFINED
NoCompressionCodec::create,
#if FOLLY_HAVE_LIBLZ4 #if FOLLY_HAVE_LIBLZ4
LZ4Codec::create, LZ4Codec::create,
#else #else
nullptr, nullptr,
#endif #endif
#if FOLLY_HAVE_LIBSNAPPY #if FOLLY_HAVE_LIBSNAPPY
SnappyCodec::create, SnappyCodec::create,
#else #else
nullptr, nullptr,
#endif #endif
#if FOLLY_HAVE_LIBZ #if FOLLY_HAVE_LIBZ
ZlibCodec::create, ZlibCodec::create,
#else #else
nullptr, nullptr,
#endif #endif
#if FOLLY_HAVE_LIBLZ4 #if FOLLY_HAVE_LIBLZ4
LZ4Codec::create, LZ4Codec::create,
#else #else
nullptr, nullptr,
#endif #endif
#if FOLLY_HAVE_LIBLZMA #if FOLLY_HAVE_LIBLZMA
LZMA2Codec::create, LZMA2Codec::create,
LZMA2Codec::create, LZMA2Codec::create,
#else #else
nullptr, nullptr,
nullptr, nullptr,
#endif #endif
#if FOLLY_HAVE_LIBZSTD #if FOLLY_HAVE_LIBZSTD
ZSTDCodec::create, ZSTDCodec::create,
#else #else
nullptr, nullptr,
#endif #endif
#if FOLLY_HAVE_LIBZ #if FOLLY_HAVE_LIBZ
ZlibCodec::create, ZlibCodec::create,
#else #else
nullptr, nullptr,
#endif #endif
}; };
bool hasCodec(CodecType type) {
size_t idx = static_cast<size_t>(type); size_t idx = static_cast<size_t>(type);
if (idx >= static_cast<size_t>(CodecType::NUM_CODEC_TYPES)) { if (idx >= static_cast<size_t>(CodecType::NUM_CODEC_TYPES)) {
throw std::invalid_argument(to<std::string>( throw std::invalid_argument(
"Compression type ", idx, " not supported")); 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]; auto factory = codecFactories[idx];
if (!factory) { if (!factory) {
......
...@@ -176,4 +176,9 @@ constexpr int COMPRESSION_LEVEL_BEST = -3; ...@@ -176,4 +176,9 @@ constexpr int COMPRESSION_LEVEL_BEST = -3;
std::unique_ptr<Codec> getCodec(CodecType type, std::unique_ptr<Codec> getCodec(CodecType type,
int level = COMPRESSION_LEVEL_DEFAULT); int level = COMPRESSION_LEVEL_DEFAULT);
/**
* Check if a specified codec is supported.
*/
bool hasCodec(CodecType type);
}} // namespaces }} // 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