aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/config/model/producer/UserConfigRepo.java
blob: c5fdf9628be971c03f97d26ca6b25f95e28f0f67 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.model.producer;

import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.log.LogLevel;
import com.yahoo.vespa.config.*;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * A UserConfigRepo is a repository for user configs, typically for a particular config producer. The repo encapsulates
 * how the user configs are stored, and defines the methods to retrieve user configs and merge the repo with others.
 *
 * @author lulf
 * @since 5.1
 */
public class UserConfigRepo {
    private final Map<ConfigDefinitionKey, ConfigPayloadBuilder> userConfigsMap;

    public UserConfigRepo() {
        this.userConfigsMap = new LinkedHashMap<>();
    }

    @Override
    public UserConfigRepo clone() {
        return new UserConfigRepo(copyBuilders(userConfigsMap));
    }

    /**
     * Must copy the builder, because the merge method on {@link AbstractConfigProducer} might override the row's builders otherwise
     */
    private Map<ConfigDefinitionKey, ConfigPayloadBuilder> copyBuilders(Map<ConfigDefinitionKey, ConfigPayloadBuilder> source) {
        Map<ConfigDefinitionKey, ConfigPayloadBuilder> ret = new LinkedHashMap<>();
        for (Map.Entry<ConfigDefinitionKey, ConfigPayloadBuilder> e : source.entrySet()) {
            ConfigDefinitionKey key = e.getKey();
            ConfigPayloadBuilder sourceVal = e.getValue();
            ConfigPayloadBuilder destVal = new ConfigPayloadBuilder(ConfigPayload.fromBuilder(sourceVal));
            ret.put(key, destVal);
        }
        return ret;
    }

    public UserConfigRepo(Map<ConfigDefinitionKey, ConfigPayloadBuilder> map) {
        this.userConfigsMap = map;
    }

    public UserConfigRepo(UserConfigRepo userConfigRepo) {
        this.userConfigsMap = userConfigRepo.userConfigsMap;
    }

    public ConfigPayloadBuilder get(ConfigDefinitionKey key) {
        return userConfigsMap.get(key);
    }

    public void merge(UserConfigRepo newRepo) {
        for (Map.Entry<ConfigDefinitionKey, ConfigPayloadBuilder> entry : newRepo.userConfigsMap.entrySet()) {
            if (entry.getValue() == null) continue;

            ConfigDefinitionKey key = entry.getKey();
            if (userConfigsMap.containsKey(key)) {
                ConfigPayloadBuilder lhsBuilder = userConfigsMap.get(key);
                ConfigPayloadBuilder rhsBuilder = entry.getValue();
                lhsBuilder.override(rhsBuilder);
            } else {
                userConfigsMap.put(key, entry.getValue());
            }
        }
    }

    public boolean isEmpty() {
        return userConfigsMap.isEmpty();
    }

    public int size() {
        return userConfigsMap.size();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (ConfigDefinitionKey key : userConfigsMap.keySet()) {
            sb.append(key.toString());
        }
        return sb.toString();
    }

    /**
     * The keys of all the configs contained in this.
     * @return a set of ConfigDefinitionsKey
     */
    public Set<ConfigDefinitionKey> configsProduced() {
        return userConfigsMap.keySet();
    }

}