From: Christopher Dykes <cdykes@fb.com> Date: Mon, 13 Mar 2017 21:47:40 +0000 (-0700) Subject: Add a way to determine if a compression codec is supported at runtime X-Git-Tag: v2017.03.20.00~12 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=ce56a16aab5a5f39246b26cb145e822a15436b5a;p=folly.git 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 --- diff --git a/folly/io/Compression.cpp b/folly/io/Compression.cpp index 39ee3b5d..1beea9cc 100644 --- a/folly/io/Compression.cpp +++ b/folly/io/Compression.cpp @@ -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) { diff --git a/folly/io/Compression.h b/folly/io/Compression.h index 06b48af5..78161f31 100644 --- a/folly/io/Compression.h +++ b/folly/io/Compression.h @@ -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