aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/IO/SchedulerNodeFactory.h
blob: 5c31534a9b88933818971424a9e625d2a6392df2 (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
#pragma once

#include <Common/ErrorCodes.h>
#include <Common/Exception.h>

#include <IO/ISchedulerNode.h>

#include <Poco/Util/AbstractConfiguration.h>

#include <boost/noncopyable.hpp>

#include <memory>
#include <mutex>
#include <unordered_map>

namespace DB
{

namespace ErrorCodes
{
    extern const int INVALID_SCHEDULER_NODE;
}

class SchedulerNodeFactory : private boost::noncopyable
{
public:
    static SchedulerNodeFactory & instance()
    {
        static SchedulerNodeFactory ret;
        return ret;
    }

    SchedulerNodePtr get(const String & name, EventQueue * event_queue, const Poco::Util::AbstractConfiguration & config, const String & config_prefix)
    {
        std::lock_guard lock{mutex};
        if (auto iter = methods.find(name); iter != methods.end())
            return iter->second(event_queue, config, config_prefix);
        throw Exception(ErrorCodes::INVALID_SCHEDULER_NODE, "Unknown scheduler node type: {}", name);
    }

    template <class TDerived>
    void registerMethod(const String & name)
    {
        std::lock_guard lock{mutex};
        methods[name] = [] (EventQueue * event_queue, const Poco::Util::AbstractConfiguration & config, const String & config_prefix)
        {
            return std::make_shared<TDerived>(event_queue, config, config_prefix);
        };
    }

private:
    std::mutex mutex;
    using Method = std::function<SchedulerNodePtr(EventQueue * event_queue, const Poco::Util::AbstractConfiguration & config, const String & config_prefix)>;
    std::unordered_map<String, Method> methods;
};

}