summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/container/search/searchchain/SourceGroup.java
diff options
context:
space:
mode:
Diffstat (limited to 'config-model/src/main/java/com/yahoo/vespa/model/container/search/searchchain/SourceGroup.java')
-rw-r--r--config-model/src/main/java/com/yahoo/vespa/model/container/search/searchchain/SourceGroup.java95
1 files changed, 95 insertions, 0 deletions
diff --git a/config-model/src/main/java/com/yahoo/vespa/model/container/search/searchchain/SourceGroup.java b/config-model/src/main/java/com/yahoo/vespa/model/container/search/searchchain/SourceGroup.java
new file mode 100644
index 00000000000..799bba45b04
--- /dev/null
+++ b/config-model/src/main/java/com/yahoo/vespa/model/container/search/searchchain/SourceGroup.java
@@ -0,0 +1,95 @@
+// Copyright 2016 Yahoo Inc. 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 tonytv
+ */
+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 IllegalStateException(
+ "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 RuntimeException("Source added twice to the same group "
+ + source);
+ }
+ }
+
+ 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) {
+ assert source.getComponentId().equals(getComponentId()):
+ "Ids differ: " + source.getComponentId() + " -- " + 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 IllegalStateException("Missing leader for the source " + getComponentId() +
+ ". One of the sources must use the attribute id instead of idref.");
+ }
+}