aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Dictionaries/Embedded/GeodataProviders/HierarchiesProvider.cpp
blob: 210459da0be9620d8dcf849892ca0e215a7ce876 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "HierarchiesProvider.h"

#include <IO/ReadBufferFromFile.h>
#include <Poco/DirectoryIterator.h>
#include <Poco/Exception.h>
#include <Poco/Util/Application.h>
#include "HierarchyFormatReader.h"
#include <filesystem>

namespace fs = std::filesystem;

bool RegionsHierarchyDataSource::isModified() const
{
    return updates_tracker.isModified();
}

IRegionsHierarchyReaderPtr RegionsHierarchyDataSource::createReader()
{
    updates_tracker.fixCurrentVersion();
    auto file_reader = std::make_shared<DB::ReadBufferFromFile>(path);
    return std::make_unique<RegionsHierarchyFormatReader>(std::move(file_reader));
}


RegionsHierarchiesDataProvider::RegionsHierarchiesDataProvider(const std::string & path_) : path(path_)
{
    discoverFilesWithCustomHierarchies();
}

void RegionsHierarchiesDataProvider::discoverFilesWithCustomHierarchies()
{
    std::string basename = fs::path(path).stem();
    fs::path dir_path = fs::canonical(path).parent_path();

    fs::directory_iterator dir_end;
    for (fs::directory_iterator dir_it(dir_path); dir_it != dir_end; ++dir_it)
    {
        std::string candidate_basename = dir_it->path().stem();

        if (candidate_basename.starts_with(basename)
            && (candidate_basename.size() > basename.size() + 1)
            && (candidate_basename[basename.size()] == '_'))
        {
            const std::string suffix = candidate_basename.substr(basename.size() + 1);
            hierarchy_files.emplace(suffix, dir_it->path());
        }
    }
}

std::vector<std::string> RegionsHierarchiesDataProvider::listCustomHierarchies() const
{
    std::vector<std::string> names;
    names.reserve(hierarchy_files.size());
    for (const auto & it : hierarchy_files)
        names.push_back(it.first);
    return names;
}

IRegionsHierarchyDataSourcePtr RegionsHierarchiesDataProvider::getDefaultHierarchySource() const
{
    return std::make_shared<RegionsHierarchyDataSource>(path);
}

IRegionsHierarchyDataSourcePtr RegionsHierarchiesDataProvider::getHierarchySource(const std::string & name) const
{
    auto found = hierarchy_files.find(name);

    if (found != hierarchy_files.end())
    {
        const auto & hierarchy_file_path = found->second;
        return std::make_shared<RegionsHierarchyDataSource>(hierarchy_file_path);
    }

    throw Poco::Exception("Regions hierarchy '" + name + "' not found");
}