ClickHouse/dbms/src/Storages/MergeTree/MergeTreeIndexGranularityInfo.cpp

111 lines
3.5 KiB
C++
Raw Normal View History

2019-06-19 10:07:56 +00:00
#include <Storages/MergeTree/MergeTreeIndexGranularityInfo.h>
2019-06-19 14:46:06 +00:00
#include <Storages/MergeTree/MergeTreeData.h>
2019-06-19 10:07:56 +00:00
#include <Poco/Path.h>
#include <Poco/File.h>
#include <Poco/DirectoryIterator.h>
#include <iostream>
namespace DB
{
2019-12-05 13:23:36 +00:00
namespace ErrorCodes
{
extern const int NOT_IMPLEMENTED;
}
2019-11-21 16:10:22 +00:00
std::optional<std::string> MergeTreeIndexGranularityInfo::getMrkExtensionFromFS(const std::string & path_to_part)
2019-06-19 10:07:56 +00:00
{
if (Poco::File(path_to_part).exists())
{
Poco::DirectoryIterator end;
for (Poco::DirectoryIterator part_it(path_to_part); part_it != end; ++part_it)
{
const auto & ext = "." + part_it.path().getExtension();
2019-11-21 16:10:22 +00:00
if (ext == getNonAdaptiveMrkExtension()
|| ext == getAdaptiveMrkExtension(MergeTreeDataPartType::WIDE)
|| ext == getAdaptiveMrkExtension(MergeTreeDataPartType::COMPACT))
2019-06-19 10:07:56 +00:00
return ext;
}
}
return {};
}
2019-11-21 16:10:22 +00:00
MergeTreeIndexGranularityInfo::MergeTreeIndexGranularityInfo(
const MergeTreeData & storage, MergeTreeDataPartType part_type, size_t columns_num)
{
initialize(storage, part_type, columns_num);
}
void MergeTreeIndexGranularityInfo::initialize(const MergeTreeData & storage, MergeTreeDataPartType part_type, size_t columns_num)
2019-06-19 10:07:56 +00:00
{
2019-11-21 16:10:22 +00:00
if (initialized)
return;
2019-10-16 18:27:53 +00:00
const auto storage_settings = storage.getSettings();
2019-08-13 10:29:31 +00:00
fixed_index_granularity = storage_settings->index_granularity;
2019-11-21 16:10:22 +00:00
2019-06-19 10:07:56 +00:00
/// Granularity is fixed
2019-10-16 18:27:53 +00:00
if (!storage.canUseAdaptiveGranularity())
2019-11-21 16:10:22 +00:00
{
if (part_type != MergeTreeDataPartType::WIDE)
2019-12-05 13:23:36 +00:00
throw Exception("Only Wide parts can be used with non-adaptive granularity.", ErrorCodes::NOT_IMPLEMENTED);
2019-06-19 10:07:56 +00:00
setNonAdaptive();
2019-11-21 16:10:22 +00:00
}
2019-06-19 10:07:56 +00:00
else
2019-11-21 16:10:22 +00:00
setAdaptive(storage_settings->index_granularity_bytes, part_type, columns_num);
2019-06-19 10:07:56 +00:00
}
2019-11-21 16:10:22 +00:00
void MergeTreeIndexGranularityInfo::changeGranularityIfRequired(const std::string & path_to_part)
2019-06-19 10:07:56 +00:00
{
2019-11-21 16:10:22 +00:00
/// FIXME check when we cant create compact part
auto mrk_ext = getMrkExtensionFromFS(path_to_part);
if (mrk_ext && *mrk_ext == getNonAdaptiveMrkExtension())
2019-06-19 10:07:56 +00:00
setNonAdaptive();
}
2019-11-21 16:10:22 +00:00
void MergeTreeIndexGranularityInfo::setAdaptive(size_t index_granularity_bytes_, MergeTreeDataPartType part_type, size_t columns_num)
2019-06-19 10:07:56 +00:00
{
2019-06-19 11:59:08 +00:00
is_adaptive = true;
2019-11-21 16:10:22 +00:00
mark_size_in_bytes = getAdaptiveMrkSize(part_type, columns_num);
2019-11-28 20:14:41 +00:00
skip_index_mark_size_in_bytes = sizeof(MarkInCompressedFile) + sizeof(UInt64);
2019-11-21 16:10:22 +00:00
marks_file_extension = getAdaptiveMrkExtension(part_type);
2019-06-19 10:07:56 +00:00
index_granularity_bytes = index_granularity_bytes_;
}
void MergeTreeIndexGranularityInfo::setNonAdaptive()
{
2019-06-19 11:59:08 +00:00
is_adaptive = false;
2019-11-28 20:14:41 +00:00
mark_size_in_bytes = skip_index_mark_size_in_bytes = getNonAdaptiveMrkSize();
2019-11-21 16:10:22 +00:00
marks_file_extension = getNonAdaptiveMrkExtension();
2019-06-19 10:07:56 +00:00
index_granularity_bytes = 0;
}
std::string getAdaptiveMrkExtension(MergeTreeDataPartType part_type)
{
switch(part_type)
{
case MergeTreeDataPartType::WIDE:
return ".mrk2";
case MergeTreeDataPartType::COMPACT:
return ".mrk3";
default:
throw Exception("Unknown part type", ErrorCodes::UNKNOWN_PART_TYPE);
}
}
size_t getAdaptiveMrkSize(MergeTreeDataPartType part_type, size_t columns_num)
{
switch(part_type)
{
case MergeTreeDataPartType::WIDE:
return sizeof(UInt64) * 3;
case MergeTreeDataPartType::COMPACT:
return sizeof(UInt64) * (columns_num * 2 + 1);
default:
throw Exception("Unknown part type", ErrorCodes::UNKNOWN_PART_TYPE);
}
}
2019-06-19 10:07:56 +00:00
}