aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/federation/sourceref/SourcesTarget.java
blob: a3c0328290d7ecc24fcc0e617ad7ba8d392588bb (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
// 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 final ComponentRegistry<ComponentAdaptor<SearchChainInvocationSpec>> providerSources = new ComponentRegistry<>() {};

    private SearchChainInvocationSpec defaultProviderSource;

    public SourcesTarget(ComponentId sourceId) {
        super(sourceId);
    }

    @Override
    public ResolveResult responsibleSearchChain(Properties queryProperties) {
        ComponentSpecification providerSpecification = providerSpecificationForSource(queryProperties);
        if (providerSpecification == null) {
            return new ResolveResult(defaultProviderSource);
        } else {
            return lookupProviderSource(providerSpecification);
        }
    }

    @Override
    public String searchRefDescription() {
        return sourceId().stringValue() + "[provider = " + Joiner.on(", ").join(allProviderIdsStringValue()) + "]";
    }

    private SortedSet<String> allProviderIdsStringValue() {
        SortedSet<String> result = new TreeSet<>();
        for (ComponentAdaptor<SearchChainInvocationSpec> providerSource : providerSources.allComponents()) {
            result.add(providerSource.getId().stringValue());
        }
        return result;
    }

    private ResolveResult lookupProviderSource(ComponentSpecification providerSpecification) {
        ComponentAdaptor<SearchChainInvocationSpec> providerSource = providerSources.getComponent(providerSpecification);

        if (providerSource == null)
            return new ResolveResult("No provider '" + sourceId() + "' for source '" + providerSpecification + "'.");

        return new ResolveResult(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;
    }
}