aboutsummaryrefslogtreecommitdiffstats
path: root/slobrok/src/vespa/slobrok/server/union_service_map.h
blob: 050a87ec02a048a4ae948c80b51033a5bf0de9ca (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "map_listener.h"
#include "map_source.h"
#include "proxy_map_source.h"

#include <map>
#include <vector>

namespace slobrok {

/**
 * Listens to events from multiple maps and publishes the union of them.
 **/
class UnionServiceMap : public ProxyMapSource
{
private:
    struct CountedSpec {
        vespalib::string spec;
        size_t count;
        CountedSpec(const vespalib::string &spec_in,
                    size_t count_in) noexcept
            : spec(spec_in),
              count(count_in)
        {
        }
    };
    using Mappings = std::vector<CountedSpec>;
    std::map<vespalib::string, Mappings> _mappings;

public:
    UnionServiceMap();
    virtual ~UnionServiceMap();

    ServiceMappingList currentConsensus() const;

    bool wouldConflict(const ServiceMapping &mapping) const;

    void add(const ServiceMapping &mapping) override;
    void remove(const ServiceMapping &mapping) override;
    void update(const ServiceMapping &old_mapping,
                const ServiceMapping &new_mapping) override;
};

} // namespace slobrok