From ce56a16aab5a5f39246b26cb145e822a15436b5a Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Mon, 13 Mar 2017 14:47:40 -0700 Subject: [PATCH] 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 --- folly/io/Compression.cpp | 60 +++++++++++++++++++++++----------------- folly/io/Compression.h | 5 ++++ 2 files changed, 39 insertions(+), 26 deletions(-) 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 ZSTDCodec::doUncompress( } // namespace -std::unique_ptr getCodec(CodecType type, int level) { - typedef std::unique_ptr (*CodecFactory)(int, CodecType); - - static CodecFactory codecFactories[ - static_cast(CodecType::NUM_CODEC_TYPES)] = { - nullptr, // USER_DEFINED - NoCompressionCodec::create, +typedef std::unique_ptr (*CodecFactory)(int, CodecType); +static CodecFactory + codecFactories[static_cast(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(type); if (idx >= static_cast(CodecType::NUM_CODEC_TYPES)) { - throw std::invalid_argument(to( - "Compression type ", idx, " not supported")); + throw std::invalid_argument( + to("Compression type ", idx, " invalid")); + } + return codecFactories[idx] != nullptr; +} + +std::unique_ptr getCodec(CodecType type, int level) { + size_t idx = static_cast(type); + if (idx >= static_cast(CodecType::NUM_CODEC_TYPES)) { + throw std::invalid_argument( + to("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 getCodec(CodecType type, int level = COMPRESSION_LEVEL_DEFAULT); +/** + * Check if a specified codec is supported. + */ +bool hasCodec(CodecType type); + }} // namespaces -- 2.34.1