2018-10-11 02:57:48 +00:00
|
|
|
#include <Compression/CompressionCodecZSTD.h>
|
2018-12-21 13:25:39 +00:00
|
|
|
#include <Compression/CompressionInfo.h>
|
2018-12-14 13:27:35 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
2018-10-11 02:57:48 +00:00
|
|
|
#include <Compression/CompressionFactory.h>
|
|
|
|
#include <zstd.h>
|
|
|
|
#include <Core/Field.h>
|
|
|
|
#include <Parsers/IAST.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
2018-12-18 15:00:51 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2018-10-11 02:57:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_COMPRESS;
|
|
|
|
extern const int CANNOT_DECOMPRESS;
|
2018-12-14 13:27:35 +00:00
|
|
|
extern const int ILLEGAL_SYNTAX_FOR_CODEC_TYPE;
|
2018-12-18 15:00:51 +00:00
|
|
|
extern const int ILLEGAL_CODEC_PARAMETER;
|
2018-10-11 02:57:48 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 17:20:18 +00:00
|
|
|
UInt8 CompressionCodecZSTD::getMethodByte() const
|
2018-10-11 02:57:48 +00:00
|
|
|
{
|
2018-12-19 17:20:18 +00:00
|
|
|
return static_cast<UInt8>(CompressionMethodByte::ZSTD);
|
2018-10-11 02:57:48 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 17:20:18 +00:00
|
|
|
String CompressionCodecZSTD::getCodecDesc() const
|
2018-10-11 02:57:48 +00:00
|
|
|
{
|
2018-12-19 17:20:18 +00:00
|
|
|
return "ZSTD";
|
2018-10-11 02:57:48 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 17:20:18 +00:00
|
|
|
UInt32 CompressionCodecZSTD::getCompressedDataSize(UInt32 uncompressed_size) const
|
2018-10-11 02:57:48 +00:00
|
|
|
{
|
|
|
|
return ZSTD_compressBound(uncompressed_size);
|
|
|
|
}
|
|
|
|
|
2018-12-19 17:20:18 +00:00
|
|
|
|
|
|
|
UInt32 CompressionCodecZSTD::doCompressData(const char * source, UInt32 source_size, char * dest) const
|
2018-10-11 02:57:48 +00:00
|
|
|
{
|
|
|
|
size_t compressed_size = ZSTD_compress(dest, ZSTD_compressBound(source_size), source, source_size, level);
|
|
|
|
|
|
|
|
if (ZSTD_isError(compressed_size))
|
|
|
|
throw Exception("Cannot compress block with ZSTD: " + std::string(ZSTD_getErrorName(compressed_size)), ErrorCodes::CANNOT_COMPRESS);
|
|
|
|
|
|
|
|
return compressed_size;
|
|
|
|
}
|
|
|
|
|
2018-12-19 17:20:18 +00:00
|
|
|
|
|
|
|
void CompressionCodecZSTD::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
|
|
|
size_t res = ZSTD_decompress(dest, uncompressed_size, source, source_size);
|
2018-10-11 02:57:48 +00:00
|
|
|
|
|
|
|
if (ZSTD_isError(res))
|
|
|
|
throw Exception("Cannot ZSTD_decompress: " + std::string(ZSTD_getErrorName(res)), ErrorCodes::CANNOT_DECOMPRESS);
|
|
|
|
}
|
|
|
|
|
2018-12-19 17:20:18 +00:00
|
|
|
CompressionCodecZSTD::CompressionCodecZSTD(int level_)
|
|
|
|
:level(level_)
|
2018-10-11 02:57:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerCodecZSTD(CompressionCodecFactory & factory)
|
|
|
|
{
|
|
|
|
UInt8 method_code = static_cast<char>(CompressionMethodByte::ZSTD);
|
|
|
|
factory.registerCompressionCodec("ZSTD", method_code, [&](const ASTPtr & arguments) -> CompressionCodecPtr
|
|
|
|
{
|
2018-12-21 12:17:30 +00:00
|
|
|
int level = CompressionCodecZSTD::ZSTD_DEFAULT_LEVEL;
|
2018-10-11 02:57:48 +00:00
|
|
|
if (arguments && !arguments->children.empty())
|
|
|
|
{
|
2018-12-21 12:17:30 +00:00
|
|
|
if (arguments->children.size() > 1)
|
2018-12-14 13:27:35 +00:00
|
|
|
throw Exception("ZSTD codec must have 1 parameter, given " + std::to_string(arguments->children.size()), ErrorCodes::ILLEGAL_SYNTAX_FOR_CODEC_TYPE);
|
|
|
|
|
2018-10-11 02:57:48 +00:00
|
|
|
const auto children = arguments->children;
|
|
|
|
const ASTLiteral * literal = static_cast<const ASTLiteral *>(children[0].get());
|
|
|
|
level = literal->value.safeGet<UInt64>();
|
2018-12-18 15:00:51 +00:00
|
|
|
if (level > ZSTD_maxCLevel())
|
|
|
|
throw Exception("ZSTD codec can't have level more that " + toString(ZSTD_maxCLevel()) + ", given " + toString(level), ErrorCodes::ILLEGAL_CODEC_PARAMETER);
|
2018-10-11 02:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_shared<CompressionCodecZSTD>(level);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|