aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/configchange/ReindexActions.java
blob: 7bab2adacaa1b08247565f4c63e1001ca95e2da4 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.configchange;

import com.yahoo.config.model.api.ConfigChangeAction;
import com.yahoo.config.model.api.ConfigChangeReindexAction;
import com.yahoo.config.model.api.ServiceInfo;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * @author bjorncs
 */
public class ReindexActions {

    private final Map<String, ReindexActions.Entry> actions = new TreeMap<>();

    public ReindexActions() {}

    public ReindexActions(List<ConfigChangeAction> actions) {
        for (ConfigChangeAction action : actions) {
            if (action.getType().equals(ConfigChangeAction.Type.REINDEX)) {
                ConfigChangeReindexAction reindexChange = (ConfigChangeReindexAction) action;
                for (ServiceInfo service : reindexChange.getServices()) {
                    addEntry(reindexChange.name(), reindexChange.getDocumentType(), service).
                            addService(service).
                            addMessage(action.getMessage());
                }
            }
        }
    }

    private Entry addEntry(String name, String documentType, ServiceInfo service) {
        String clusterName = service.getProperty("clustername").orElse("");
        String entryId = name + "." + "." + clusterName + "." + documentType;
        Entry entry = actions.get(entryId);
        if (entry == null) {
            entry = new Entry(name, documentType, clusterName);
            actions.put(entryId, entry);
        }
        return entry;
    }

    public List<Entry> getEntries() { return new ArrayList<>(actions.values()); }
    public String format() { return new ReindexActionsFormatter(this).format(); }
    public boolean isEmpty() { return getEntries().isEmpty(); }

    public static class Entry {

        private final String name;
        private final String documentType;
        private final String clusterName;
        private final Set<ServiceInfo> services = new LinkedHashSet<>();
        private final Set<String> messages = new TreeSet<>();

        private Entry(String name, String documentType, String clusterName) {
            this.name = name;
            this.documentType = documentType;
            this.clusterName = clusterName;
        }

        private Entry addService(ServiceInfo service) {
            services.add(service);
            return this;
        }

        private Entry addMessage(String message) {
            messages.add(message);
            return this;
        }

        public String name() { return name; }
        public String getDocumentType() { return documentType; }
        public String getClusterName() { return clusterName; }
        public Set<ServiceInfo> getServices() { return services; }
        public Set<String> getMessages() { return messages; }

    }
}