blob: 54b022e0b97a3d8c5cf53b6a718c57d674fe096d (
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.federation.sourceref;
import com.google.common.base.Joiner;
import com.yahoo.component.ComponentId;
import com.yahoo.component.ComponentSpecification;
import com.yahoo.component.chain.model.ComponentAdaptor;
import com.yahoo.component.provider.ComponentRegistry;
import com.yahoo.processing.request.Properties;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
public class SourcesTarget extends Target {
private ComponentRegistry<ComponentAdaptor<SearchChainInvocationSpec>> providerSources = new ComponentRegistry<>() {};
private SearchChainInvocationSpec defaultProviderSource;
public SourcesTarget(ComponentId sourceId) {
super(sourceId);
}
@Override
public SearchChainInvocationSpec responsibleSearchChain(Properties queryProperties) throws UnresolvedSearchChainException {
ComponentSpecification providerSpecification = providerSpecificationForSource(queryProperties);
if (providerSpecification == null) {
return defaultProviderSource;
} else {
return lookupProviderSource(providerSpecification);
}
}
@Override
public String searchRefDescription() {
StringBuilder builder = new StringBuilder(sourceId().stringValue());
builder.append("[provider = ").
append(Joiner.on(", ").join(allProviderIdsStringValue())).
append("]");
return builder.toString();
}
private SortedSet<String> allProviderIdsStringValue() {
SortedSet<String> result = new TreeSet<>();
for (ComponentAdaptor<SearchChainInvocationSpec> providerSource : providerSources.allComponents()) {
result.add(providerSource.getId().stringValue());
}
return result;
}
private SearchChainInvocationSpec lookupProviderSource(ComponentSpecification providerSpecification)
throws UnresolvedSearchChainException {
ComponentAdaptor<SearchChainInvocationSpec> providerSource = providerSources.getComponent(providerSpecification);
if (providerSource == null)
throw UnresolvedProviderException.createForMissingProvider(sourceId(), providerSpecification);
return providerSource.model;
}
public void freeze() {
if (defaultProviderSource == null)
throw new RuntimeException("Null default provider source for source " + sourceId() + ".");
providerSources.freeze();
}
public void addSource(ComponentId providerId, SearchChainInvocationSpec searchChainInvocationSpec,
boolean isDefaultProviderForSource) {
providerSources.register(providerId, new ComponentAdaptor<>(providerId, searchChainInvocationSpec));
if (isDefaultProviderForSource) {
setDefaultProviderSource(searchChainInvocationSpec);
}
}
private void setDefaultProviderSource(SearchChainInvocationSpec searchChainInvocationSpec) {
if (defaultProviderSource != null)
throw new RuntimeException("Tried to set two default providers for source " + sourceId() + ".");
defaultProviderSource = searchChainInvocationSpec;
}
ComponentId sourceId() {
return localId;
}
/**
* Looks up source.(sourceId).provider in the query properties.
* @return null if the default provider should be used
*/
private ComponentSpecification providerSpecificationForSource(Properties queryProperties) {
String spec = queryProperties.getString("source." + sourceId().stringValue() + ".provider");
return ComponentSpecification.fromString(spec);
}
public SearchChainInvocationSpec defaultProviderSource() {
return defaultProviderSource;
}
public List<SearchChainInvocationSpec> allProviderSources() {
List<SearchChainInvocationSpec> allProviderSources = new ArrayList<>();
for (ComponentAdaptor<SearchChainInvocationSpec> component : providerSources.allComponents()) {
allProviderSources.add(component.model);
}
return allProviderSources;
}
}
|