1#include "HierarchiesProvider.h"
2
3#include <IO/ReadBufferFromFile.h>
4#include <Poco/DirectoryIterator.h>
5#include <Poco/Exception.h>
6#include <Poco/Util/Application.h>
7#include "HierarchyFormatReader.h"
8
9
10bool RegionsHierarchyDataSource::isModified() const
11{
12 return updates_tracker.isModified();
13}
14
15IRegionsHierarchyReaderPtr RegionsHierarchyDataSource::createReader()
16{
17 updates_tracker.fixCurrentVersion();
18 auto file_reader = std::make_shared<DB::ReadBufferFromFile>(path);
19 return std::make_unique<RegionsHierarchyFormatReader>(std::move(file_reader));
20}
21
22
23RegionsHierarchiesDataProvider::RegionsHierarchiesDataProvider(const std::string & path_) : path(path_)
24{
25 discoverFilesWithCustomHierarchies();
26}
27
28void RegionsHierarchiesDataProvider::discoverFilesWithCustomHierarchies()
29{
30 std::string basename = Poco::Path(path).getBaseName();
31
32 Poco::Path dir_path = Poco::Path(path).absolute().parent();
33
34 Poco::DirectoryIterator dir_end;
35 for (Poco::DirectoryIterator dir_it(dir_path); dir_it != dir_end; ++dir_it)
36 {
37 std::string candidate_basename = dir_it.path().getBaseName();
38
39 if ((0 == candidate_basename.compare(0, basename.size(), basename)) && (candidate_basename.size() > basename.size() + 1)
40 && (candidate_basename[basename.size()] == '_'))
41 {
42 const std::string suffix = candidate_basename.substr(basename.size() + 1);
43 hierarchy_files.emplace(suffix, dir_it->path());
44 }
45 }
46}
47
48std::vector<std::string> RegionsHierarchiesDataProvider::listCustomHierarchies() const
49{
50 std::vector<std::string> names;
51 names.reserve(hierarchy_files.size());
52 for (const auto & it : hierarchy_files)
53 names.push_back(it.first);
54 return names;
55}
56
57IRegionsHierarchyDataSourcePtr RegionsHierarchiesDataProvider::getDefaultHierarchySource() const
58{
59 return std::make_shared<RegionsHierarchyDataSource>(path);
60}
61
62IRegionsHierarchyDataSourcePtr RegionsHierarchiesDataProvider::getHierarchySource(const std::string & name) const
63{
64 auto found = hierarchy_files.find(name);
65
66 if (found != hierarchy_files.end())
67 {
68 const auto & hierarchy_file_path = found->second;
69 return std::make_shared<RegionsHierarchyDataSource>(hierarchy_file_path);
70 }
71
72 throw Poco::Exception("Regions hierarchy '" + name + "' not found");
73}
74