2011-05-16 16:39:55 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2014-02-24 02:43:56 +00:00
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <IO/WriteBufferFromFileDescriptor.h>
|
|
|
|
#include <IO/ReadBufferFromFileDescriptor.h>
|
|
|
|
#include <IO/CompressedWriteBuffer.h>
|
|
|
|
#include <IO/CompressedReadBuffer.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/copyData.h>
|
2011-05-16 16:39:55 +00:00
|
|
|
|
|
|
|
|
2016-10-25 06:57:12 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int TOO_LARGE_SIZE_COMPRESSED;
|
|
|
|
}
|
2016-10-25 06:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Outputs sizes of uncompressed and compressed blocks for compressed file.
|
2013-02-22 19:24:56 +00:00
|
|
|
void stat(DB::ReadBuffer & in, DB::WriteBuffer & out)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
while (!in.eof())
|
|
|
|
{
|
|
|
|
in.ignore(16); /// checksum
|
2017-02-13 21:13:10 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
char header[COMPRESSED_BLOCK_HEADER_SIZE];
|
|
|
|
in.readStrict(header, COMPRESSED_BLOCK_HEADER_SIZE);
|
2013-02-22 19:24:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
UInt32 size_compressed = unalignedLoad<UInt32>(&header[1]);
|
2016-02-03 21:26:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (size_compressed > DBMS_MAX_COMPRESSED_SIZE)
|
|
|
|
throw DB::Exception("Too large size_compressed. Most likely corrupted data.", DB::ErrorCodes::TOO_LARGE_SIZE_COMPRESSED);
|
2013-02-22 19:24:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
UInt32 size_decompressed = unalignedLoad<UInt32>(&header[5]);
|
2013-02-22 19:24:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
DB::writeText(size_decompressed, out);
|
|
|
|
DB::writeChar('\t', out);
|
|
|
|
DB::writeText(size_compressed, out);
|
|
|
|
DB::writeChar('\n', out);
|
2013-02-22 19:24:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
in.ignore(size_compressed - COMPRESSED_BLOCK_HEADER_SIZE);
|
|
|
|
}
|
2013-02-22 19:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-16 16:39:55 +00:00
|
|
|
int main(int argc, char ** argv)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
boost::program_options::options_description desc("Allowed options");
|
|
|
|
desc.add_options()
|
|
|
|
("help,h", "produce help message")
|
|
|
|
("decompress,d", "decompress")
|
|
|
|
("block-size,b", boost::program_options::value<unsigned>()->default_value(DBMS_DEFAULT_BUFFER_SIZE), "compress in blocks of specified size")
|
|
|
|
("hc", "use LZ4HC instead of LZ4")
|
|
|
|
#ifdef USE_QUICKLZ
|
|
|
|
("qlz", "use QuickLZ (level 1) instead of LZ4")
|
|
|
|
#endif
|
|
|
|
("zstd", "use ZSTD instead of LZ4")
|
|
|
|
("stat", "print block statistics of compressed data")
|
|
|
|
;
|
|
|
|
|
|
|
|
boost::program_options::variables_map options;
|
|
|
|
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), options);
|
|
|
|
|
|
|
|
if (options.count("help"))
|
|
|
|
{
|
|
|
|
std::cout << "Usage: " << argv[0] << " [options] < in > out" << std::endl;
|
|
|
|
std::cout << desc << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
bool decompress = options.count("decompress");
|
|
|
|
|
|
|
|
#ifdef USE_QUICKLZ
|
|
|
|
bool use_qlz = options.count("qlz");
|
|
|
|
#else
|
|
|
|
bool use_qlz = false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool use_lz4hc = options.count("hc");
|
|
|
|
bool use_zstd = options.count("zstd");
|
|
|
|
bool stat_mode = options.count("stat");
|
|
|
|
unsigned block_size = options["block-size"].as<unsigned>();
|
|
|
|
|
|
|
|
DB::CompressionMethod method = DB::CompressionMethod::LZ4;
|
|
|
|
|
|
|
|
if (use_qlz)
|
|
|
|
method = DB::CompressionMethod::QuickLZ;
|
|
|
|
else if (use_lz4hc)
|
|
|
|
method = DB::CompressionMethod::LZ4HC;
|
|
|
|
else if (use_zstd)
|
|
|
|
method = DB::CompressionMethod::ZSTD;
|
|
|
|
|
|
|
|
DB::ReadBufferFromFileDescriptor rb(STDIN_FILENO);
|
|
|
|
DB::WriteBufferFromFileDescriptor wb(STDOUT_FILENO);
|
|
|
|
|
|
|
|
if (stat_mode)
|
|
|
|
{
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Output statistic for compressed file.
|
2017-04-01 07:20:54 +00:00
|
|
|
stat(rb, wb);
|
|
|
|
}
|
|
|
|
else if (decompress)
|
|
|
|
{
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Decompression
|
2017-04-01 07:20:54 +00:00
|
|
|
DB::CompressedReadBuffer from(rb);
|
|
|
|
DB::copyData(from, wb);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Compression
|
2017-04-01 07:20:54 +00:00
|
|
|
DB::CompressedWriteBuffer to(wb, method, block_size);
|
|
|
|
DB::copyData(rb, to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
std::cerr << DB::getCurrentExceptionMessage(true);
|
|
|
|
return DB::getCurrentExceptionCode();
|
|
|
|
}
|
2011-05-16 16:39:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|