ClickHouse/dbms/Compression/CompressionCodecLZ4.cpp

100 lines
2.6 KiB
C++
Raw Normal View History

2018-12-28 18:15:26 +00:00
#include "CompressionCodecLZ4.h"
2018-10-11 02:57:48 +00:00
#include <lz4.h>
#include <lz4hc.h>
#include <Compression/CompressionInfo.h>
2018-10-11 02:57:48 +00:00
#include <Compression/CompressionFactory.h>
2018-12-28 18:15:26 +00:00
#include <Compression/LZ4_decompress_faster.h>
2018-12-21 14:03:53 +00:00
#include <Parsers/IAST.h>
#include <Parsers/ASTLiteral.h>
#include <IO/WriteHelpers.h>
2018-12-21 14:03:53 +00:00
2019-12-18 05:12:03 +00:00
#pragma GCC diagnostic ignored "-Wold-style-cast"
2018-10-11 02:57:48 +00:00
namespace DB
{
2018-12-21 14:03:53 +00:00
namespace ErrorCodes
{
extern const int CANNOT_COMPRESS;
extern const int ILLEGAL_SYNTAX_FOR_CODEC_TYPE;
}
2020-01-03 14:39:24 +00:00
uint8_t CompressionCodecLZ4::getMethodByte() const
2018-10-11 02:57:48 +00:00
{
2020-01-03 14:39:24 +00:00
return static_cast<uint8_t>(CompressionMethodByte::LZ4);
2018-10-11 02:57:48 +00:00
}
2018-12-19 17:20:18 +00:00
String CompressionCodecLZ4::getCodecDesc() const
2018-10-11 02:57:48 +00:00
{
2018-12-19 17:20:18 +00:00
return "LZ4";
2018-10-11 02:57:48 +00:00
}
UInt32 CompressionCodecLZ4::getMaxCompressedDataSize(UInt32 uncompressed_size) const
2018-10-11 02:57:48 +00:00
{
return LZ4_COMPRESSBOUND(uncompressed_size);
}
2018-12-19 17:20:18 +00:00
UInt32 CompressionCodecLZ4::doCompressData(const char * source, UInt32 source_size, char * dest) const
2018-10-11 02:57:48 +00:00
{
return LZ4_compress_default(source, dest, source_size, LZ4_COMPRESSBOUND(source_size));
}
2018-12-19 17:20:18 +00:00
void CompressionCodecLZ4::doDecompressData(const char * source, UInt32 source_size, char * dest, UInt32 uncompressed_size) const
2018-10-11 02:57:48 +00:00
{
2018-12-19 17:20:18 +00:00
LZ4::decompress(source, dest, source_size, uncompressed_size, lz4_stat);
2018-10-11 02:57:48 +00:00
}
void registerCodecLZ4(CompressionCodecFactory & factory)
{
2018-12-24 14:10:37 +00:00
factory.registerSimpleCompressionCodec("LZ4", static_cast<UInt8>(CompressionMethodByte::LZ4), [&] ()
{
2018-10-11 02:57:48 +00:00
return std::make_shared<CompressionCodecLZ4>();
});
}
2018-12-21 14:03:53 +00:00
String CompressionCodecLZ4HC::getCodecDesc() const
{
return "LZ4HC(" + toString(level) + ")";
2018-10-11 02:57:48 +00:00
}
2018-12-21 14:03:53 +00:00
UInt32 CompressionCodecLZ4HC::doCompressData(const char * source, UInt32 source_size, char * dest) const
{
auto success = LZ4_compress_HC(source, dest, source_size, LZ4_COMPRESSBOUND(source_size), level);
if (!success)
throw Exception("Cannot LZ4_compress_HC", ErrorCodes::CANNOT_COMPRESS);
return success;
}
void registerCodecLZ4HC(CompressionCodecFactory & factory)
{
factory.registerCompressionCodec("LZ4HC", {}, [&](const ASTPtr & arguments) -> CompressionCodecPtr
{
2018-12-24 14:10:37 +00:00
int level = 0;
2018-12-21 14:03:53 +00:00
if (arguments && !arguments->children.empty())
{
if (arguments->children.size() > 1)
throw Exception("LZ4HC codec must have 1 parameter, given " + std::to_string(arguments->children.size()), ErrorCodes::ILLEGAL_SYNTAX_FOR_CODEC_TYPE);
const auto children = arguments->children;
2019-03-11 13:22:51 +00:00
const auto * literal = children[0]->as<ASTLiteral>();
2018-12-21 14:03:53 +00:00
level = literal->value.safeGet<UInt64>();
}
return std::make_shared<CompressionCodecLZ4HC>(level);
});
}
CompressionCodecLZ4HC::CompressionCodecLZ4HC(int level_)
: level(level_)
{
}
}