| 1 | #include "CompressionCodecLZ4.h" |
| 2 | |
| 3 | #include <lz4.h> |
| 4 | #include <lz4hc.h> |
| 5 | #include <Compression/CompressionInfo.h> |
| 6 | #include <Compression/CompressionFactory.h> |
| 7 | #include <Compression/LZ4_decompress_faster.h> |
| 8 | #include <Parsers/IAST.h> |
| 9 | #include <Parsers/ASTLiteral.h> |
| 10 | #include <IO/WriteHelpers.h> |
| 11 | |
| 12 | #pragma GCC diagnostic ignored "-Wold-style-cast" |
| 13 | |
| 14 | |
| 15 | namespace DB |
| 16 | { |
| 17 | |
| 18 | namespace ErrorCodes |
| 19 | { |
| 20 | extern const int CANNOT_COMPRESS; |
| 21 | extern const int ILLEGAL_SYNTAX_FOR_CODEC_TYPE; |
| 22 | extern const int ILLEGAL_CODEC_PARAMETER; |
| 23 | } |
| 24 | |
| 25 | |
| 26 | UInt8 CompressionCodecLZ4::getMethodByte() const |
| 27 | { |
| 28 | return static_cast<UInt8>(CompressionMethodByte::LZ4); |
| 29 | } |
| 30 | |
| 31 | String CompressionCodecLZ4::getCodecDesc() const |
| 32 | { |
| 33 | return "LZ4" ; |
| 34 | } |
| 35 | |
| 36 | UInt32 CompressionCodecLZ4::getMaxCompressedDataSize(UInt32 uncompressed_size) const |
| 37 | { |
| 38 | return LZ4_COMPRESSBOUND(uncompressed_size); |
| 39 | } |
| 40 | |
| 41 | UInt32 CompressionCodecLZ4::doCompressData(const char * source, UInt32 source_size, char * dest) const |
| 42 | { |
| 43 | return LZ4_compress_default(source, dest, source_size, LZ4_COMPRESSBOUND(source_size)); |
| 44 | } |
| 45 | |
| 46 | void CompressionCodecLZ4::doDecompressData(const char * source, UInt32 source_size, char * dest, UInt32 uncompressed_size) const |
| 47 | { |
| 48 | LZ4::decompress(source, dest, source_size, uncompressed_size, lz4_stat); |
| 49 | } |
| 50 | |
| 51 | void registerCodecLZ4(CompressionCodecFactory & factory) |
| 52 | { |
| 53 | factory.registerSimpleCompressionCodec("LZ4" , static_cast<UInt8>(CompressionMethodByte::LZ4), [&] () |
| 54 | { |
| 55 | return std::make_shared<CompressionCodecLZ4>(); |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | String CompressionCodecLZ4HC::getCodecDesc() const |
| 61 | { |
| 62 | return "LZ4HC(" + toString(level) + ")" ; |
| 63 | } |
| 64 | |
| 65 | UInt32 CompressionCodecLZ4HC::doCompressData(const char * source, UInt32 source_size, char * dest) const |
| 66 | { |
| 67 | auto success = LZ4_compress_HC(source, dest, source_size, LZ4_COMPRESSBOUND(source_size), level); |
| 68 | |
| 69 | if (!success) |
| 70 | throw Exception("Cannot LZ4_compress_HC" , ErrorCodes::CANNOT_COMPRESS); |
| 71 | |
| 72 | return success; |
| 73 | } |
| 74 | |
| 75 | void registerCodecLZ4HC(CompressionCodecFactory & factory) |
| 76 | { |
| 77 | factory.registerCompressionCodec("LZ4HC" , {}, [&](const ASTPtr & arguments) -> CompressionCodecPtr |
| 78 | { |
| 79 | int level = 0; |
| 80 | |
| 81 | if (arguments && !arguments->children.empty()) |
| 82 | { |
| 83 | if (arguments->children.size() > 1) |
| 84 | throw Exception("LZ4HC codec must have 1 parameter, given " + std::to_string(arguments->children.size()), ErrorCodes::ILLEGAL_SYNTAX_FOR_CODEC_TYPE); |
| 85 | |
| 86 | const auto children = arguments->children; |
| 87 | const auto * literal = children[0]->as<ASTLiteral>(); |
| 88 | level = literal->value.safeGet<UInt64>(); |
| 89 | } |
| 90 | |
| 91 | return std::make_shared<CompressionCodecLZ4HC>(level); |
| 92 | }); |
| 93 | } |
| 94 | |
| 95 | CompressionCodecLZ4HC::CompressionCodecLZ4HC(int level_) |
| 96 | : level(level_) |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | } |
| 101 | |