2011-05-16 16:39:55 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2011-12-28 20:15:56 +00:00
|
|
|
#include <DB/IO/WriteBufferFromFileDescriptor.h>
|
|
|
|
#include <DB/IO/ReadBufferFromFileDescriptor.h>
|
2011-05-16 16:39:55 +00:00
|
|
|
#include <DB/IO/CompressedWriteBuffer.h>
|
|
|
|
#include <DB/IO/CompressedReadBuffer.h>
|
2013-02-22 19:24:56 +00:00
|
|
|
#include <DB/IO/WriteHelpers.h>
|
2011-05-16 16:39:55 +00:00
|
|
|
#include <DB/IO/copyData.h>
|
|
|
|
|
|
|
|
|
2013-02-22 19:24:56 +00:00
|
|
|
/// Выводит размеры разжатых и сжатых блоков для сжатого файла.
|
|
|
|
void stat(DB::ReadBuffer & in, DB::WriteBuffer & out)
|
|
|
|
{
|
2013-02-22 19:29:39 +00:00
|
|
|
while (!in.eof())
|
|
|
|
{
|
|
|
|
char header[QUICKLZ_HEADER_SIZE];
|
|
|
|
|
|
|
|
in.ignore(16); /// checksum
|
|
|
|
in.readStrict(header, QUICKLZ_HEADER_SIZE);
|
2013-02-22 19:24:56 +00:00
|
|
|
|
2013-02-22 19:29:39 +00:00
|
|
|
size_t size_compressed = qlz_size_compressed(header);
|
|
|
|
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
|
|
|
|
2013-02-22 19:29:39 +00:00
|
|
|
size_t size_decompressed = qlz_size_decompressed(header);
|
2013-02-22 19:24:56 +00:00
|
|
|
|
2013-02-22 19:29:39 +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
|
|
|
|
2013-02-22 19:29:39 +00:00
|
|
|
in.ignore(size_compressed - QUICKLZ_HEADER_SIZE);
|
|
|
|
}
|
2013-02-22 19:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-16 16:39:55 +00:00
|
|
|
int main(int argc, char ** argv)
|
|
|
|
{
|
2012-01-05 18:35:22 +00:00
|
|
|
try
|
2011-05-16 16:39:55 +00:00
|
|
|
{
|
2012-01-05 18:35:22 +00:00
|
|
|
bool decompress = false;
|
2012-04-14 00:18:07 +00:00
|
|
|
bool use_qlz = false;
|
2013-02-22 19:24:56 +00:00
|
|
|
bool stat_mode = false;
|
2011-05-16 16:39:55 +00:00
|
|
|
|
2012-01-05 18:35:22 +00:00
|
|
|
if (argc == 2)
|
|
|
|
{
|
|
|
|
decompress = 0 == strcmp(argv[1], "-d");
|
2012-04-14 00:18:07 +00:00
|
|
|
use_qlz = 0 == strcmp(argv[1], "--qlz");
|
2013-02-22 19:24:56 +00:00
|
|
|
stat_mode = 0 == strcmp(argv[1], "--stat");
|
2012-01-05 18:35:22 +00:00
|
|
|
}
|
|
|
|
|
2013-02-22 19:24:56 +00:00
|
|
|
if (argc > 2 || (argc == 2 && !decompress && !use_qlz && !stat_mode))
|
2012-01-05 18:35:22 +00:00
|
|
|
{
|
2013-02-22 19:24:56 +00:00
|
|
|
std::cerr << "Usage: " << argv[0] << " [-d|--qlz|--stat] < in > out" << std::endl;
|
2012-01-05 18:35:22 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2011-05-16 16:39:55 +00:00
|
|
|
|
2012-04-14 00:18:07 +00:00
|
|
|
DB::CompressionMethod::Enum method = use_qlz ? DB::CompressionMethod::QuickLZ : DB::CompressionMethod::LZ4;
|
2012-01-05 18:35:22 +00:00
|
|
|
|
2013-02-22 19:24:56 +00:00
|
|
|
DB::ReadBufferFromFileDescriptor rb(STDIN_FILENO);
|
|
|
|
DB::WriteBufferFromFileDescriptor wb(STDOUT_FILENO);
|
2012-01-05 18:35:22 +00:00
|
|
|
|
2013-02-22 19:24:56 +00:00
|
|
|
if (stat_mode)
|
2012-01-05 18:35:22 +00:00
|
|
|
{
|
2013-02-22 19:24:56 +00:00
|
|
|
/// Вывести статистику для сжатого файла.
|
|
|
|
stat(rb, wb);
|
2012-01-05 18:35:22 +00:00
|
|
|
}
|
2013-02-22 19:24:56 +00:00
|
|
|
else if (decompress)
|
2012-01-05 18:35:22 +00:00
|
|
|
{
|
|
|
|
/// Разжатие
|
2013-02-22 19:24:56 +00:00
|
|
|
DB::CompressedReadBuffer from(rb);
|
|
|
|
DB::copyData(from, wb);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// Сжатие
|
|
|
|
DB::CompressedWriteBuffer to(wb, method);
|
|
|
|
DB::copyData(rb, to);
|
2012-01-05 18:35:22 +00:00
|
|
|
}
|
2011-05-16 16:39:55 +00:00
|
|
|
}
|
2012-01-05 18:35:22 +00:00
|
|
|
catch (const DB::Exception & e)
|
2011-05-16 16:39:55 +00:00
|
|
|
{
|
2012-01-05 18:35:22 +00:00
|
|
|
std::cerr << e.what() << ", " << e.message() << std::endl
|
|
|
|
<< std::endl
|
|
|
|
<< "Stack trace:" << std::endl
|
|
|
|
<< e.getStackTrace().toString()
|
|
|
|
<< std::endl;
|
|
|
|
throw;
|
2011-05-16 16:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|