aboutsummaryrefslogtreecommitdiffstats
path: root/slobrok/src/vespa/slobrok/cfg.cpp
blob: 12e41bac4fc0dc545426350af5752c659efba5f3 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "cfg.h"
#include <vespa/config/subscription/configsubscriber.hpp>

namespace slobrok {

namespace {

std::vector<std::string>
extract(const cloud::config::SlobroksConfig &cfg)
{
    std::vector<std::string> r;
    for (size_t i = 0; i < cfg.slobrok.size(); ++i) {
        std::string spec(cfg.slobrok[i].connectionspec);
        r.push_back(spec);
    }
    return r;
}

} // namespace <unnamed>

bool
Configurator::poll()
{
    bool retval = _subscriber->nextGenerationNow();
    if (retval) {
        std::unique_ptr<cloud::config::SlobroksConfig> cfg = _handle->getConfig();
        _target.setup(extract(*cfg));
    }
    return retval;
}


Configurator::Configurator(Configurable& target, const config::ConfigUri & uri)
    : _subscriber(std::make_unique<config::ConfigSubscriber>(uri.getContext())),
      _handle(_subscriber->subscribe<cloud::config::SlobroksConfig>(uri.getConfigId())),
      _target(target)
{
}

Configurator::~Configurator() = default;


int64_t
Configurator::getGeneration() const {
    return _subscriber->getGeneration();
}


ConfiguratorFactory::ConfiguratorFactory(const config::ConfigUri& uri)
    : _uri(uri)
{
}

ConfiguratorFactory::ConfiguratorFactory(const std::vector<std::string> & spec)
    : _uri(config::ConfigUri::createEmpty())
{
    cloud::config::SlobroksConfigBuilder builder;
    for (size_t i = 0; i < spec.size(); i++) {
        cloud::config::SlobroksConfig::Slobrok sb;
        sb.connectionspec = spec[i];
        builder.slobrok.push_back(sb);
    }
    _uri = config::ConfigUri::createFromInstance(builder);
}

Configurator::UP
ConfiguratorFactory::create(Configurable& target) const
{
    return std::make_unique<Configurator>(target, _uri);
}

} // namespace slobrok