aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/container/search/searchchain/SourceGroup.java
blob: 7abb3111afd35e17a2e6f5b9b853a5425da6175d (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.search.searchchain;

import com.yahoo.component.ComponentId;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * A set of sources with the same name, each associated with a different provider, that fills the same role.
 *
 * @author Tony Vaagenes
 */
final class SourceGroup {

    private final ComponentId id;
    private Source leader;
    private final Set<Source> participants = new LinkedHashSet<>();

    private void setLeader(Source leader) {
        assert (validMember(leader));

        if (this.leader != null)
            throw new IllegalArgumentException("There can not be two default providers for the source '" + id + "'");

        this.leader = leader;
    }

    private void addParticipant(Source source) {
        assert (validMember(source));
        assert (!source.equals(leader));

        if (!participants.add(source))
            throw new IllegalArgumentException("Source '" + source + "' added twice to the same group");
    }

    private boolean validMember(Source leader) {
        return leader.getComponentId().equals(id);
    }

    public ComponentId getComponentId() {
        return id;
    }

    public SourceGroup(ComponentId id) {
        this.id = id;
    }

    public void add(Source source) {
        if ( ! source.getComponentId().equals(getComponentId()))
            throw new IllegalStateException("Ids differ: " + source.getComponentId() + " and " + getComponentId());

        if (Source.GroupOption.leader == source.groupOption) {
            setLeader(source);
        } else {
            addParticipant(source);
        }
    }

    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Source id: ").append(id).append("\n").
                append("Leader provider: ").append(
                leader.getParentProvider().getComponentId()).append("\n").
                append("Participants:");

        for (Source participant : participants) {
            builder.append("\n").append("    Provider: ").append(
                    participant.getParentProvider().getComponentId());
        }
        return builder.toString();
    }

    public Source leader() {
        return leader;
    }

    public Collection<Source> participants() {
        return Collections.unmodifiableCollection(participants);
    }

    public void validate() {
        if (leader == null)
            throw new IllegalArgumentException("Missing leader for the source " + getComponentId() +
                                               ". One of the sources must use the attribute id instead of idref.");
    }

}