1#include <Storages/MergeTree/MergeTreeIndexGranularityInfo.h>
2#include <Storages/MergeTree/MergeTreeData.h>
3#include <Poco/Path.h>
4#include <Poco/File.h>
5#include <Poco/DirectoryIterator.h>
6#include <iostream>
7
8namespace DB
9{
10std::optional<std::string> MergeTreeIndexGranularityInfo::getMrkExtensionFromFS(const std::string & path_to_part) const
11{
12 if (Poco::File(path_to_part).exists())
13 {
14 Poco::DirectoryIterator end;
15 for (Poco::DirectoryIterator part_it(path_to_part); part_it != end; ++part_it)
16 {
17 const auto & ext = "." + part_it.path().getExtension();
18 if (ext == getNonAdaptiveMrkExtension() || ext == getAdaptiveMrkExtension())
19 return ext;
20 }
21 }
22 return {};
23}
24
25MergeTreeIndexGranularityInfo::MergeTreeIndexGranularityInfo(
26 const MergeTreeData & storage)
27{
28 const auto storage_settings = storage.getSettings();
29 fixed_index_granularity = storage_settings->index_granularity;
30 /// Granularity is fixed
31 if (!storage.canUseAdaptiveGranularity())
32 setNonAdaptive();
33 else
34 setAdaptive(storage_settings->index_granularity_bytes);
35}
36
37
38void MergeTreeIndexGranularityInfo::changeGranularityIfRequired(const std::string & path_to_part)
39{
40 auto mrk_ext = getMrkExtensionFromFS(path_to_part);
41 if (mrk_ext && *mrk_ext == getNonAdaptiveMrkExtension())
42 setNonAdaptive();
43}
44
45void MergeTreeIndexGranularityInfo::setAdaptive(size_t index_granularity_bytes_)
46{
47 is_adaptive = true;
48 mark_size_in_bytes = getAdaptiveMrkSize();
49 marks_file_extension = getAdaptiveMrkExtension();
50 index_granularity_bytes = index_granularity_bytes_;
51}
52
53void MergeTreeIndexGranularityInfo::setNonAdaptive()
54{
55 is_adaptive = false;
56 mark_size_in_bytes = getNonAdaptiveMrkSize();
57 marks_file_extension = getNonAdaptiveMrkExtension();
58 index_granularity_bytes = 0;
59}
60
61}
62