aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/federation/sourceref/SourceRefResolver.java
blob: 2e7849dd85a4d7643ae804b3d53ebdee3455f6ae (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
// 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.yahoo.component.ComponentSpecification;
import com.yahoo.processing.request.Properties;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Maps a source reference to search chain invocation specs.
 *
 * @author Tony Vaagenes
 */
public class SourceRefResolver {

    private final SearchChainResolver searchChainResolver;
    private final Map<String, List<String>> schema2Clusters;

    public SourceRefResolver(SearchChainResolver searchChainResolver, Map<String, List<String>> schema2Clusters) {
        this.searchChainResolver = searchChainResolver;
        this.schema2Clusters = schema2Clusters;
    }

    public Set<SearchChainInvocationSpec> resolve(ComponentSpecification sourceRef,
                                                  Properties sourceToProviderMap) throws UnresolvedSearchChainException {
        try {
            return Set.of(searchChainResolver.resolve(sourceRef, sourceToProviderMap));
        } catch (UnresolvedSourceRefException e) {
            return resolveClustersWithDocument(sourceRef, sourceToProviderMap);
        }
    }

    private Set<SearchChainInvocationSpec> resolveClustersWithDocument(ComponentSpecification sourceRef,
                                                                       Properties sourceToProviderMap)
            throws UnresolvedSearchChainException {

        if (hasOnlyName(sourceRef)) {
            Set<SearchChainInvocationSpec> clusterSearchChains = new LinkedHashSet<>();

            List<String> clusters = schema2Clusters.getOrDefault(sourceRef.getName(), List.of());
            for (String cluster : clusters) {
                clusterSearchChains.add(resolveClusterSearchChain(cluster, sourceRef, sourceToProviderMap));
            }

            if ( ! clusterSearchChains.isEmpty())
                return clusterSearchChains;
        }
        throw UnresolvedSourceRefException.createForMissingSourceRef(sourceRef);
    }

    private SearchChainInvocationSpec resolveClusterSearchChain(String cluster,
                                                                ComponentSpecification sourceRef,
                                                                Properties sourceToProviderMap)
            throws UnresolvedSearchChainException {
        try {
            return searchChainResolver.resolve(new ComponentSpecification(cluster), sourceToProviderMap);
        }
        catch (UnresolvedSearchChainException e) {
            throw new UnresolvedSearchChainException("Failed to resolve cluster search chain '" + cluster +
                                                     "' when using source ref '" + sourceRef +
                                                     "' as a document name.");
        }
    }

    private boolean hasOnlyName(ComponentSpecification sourceSpec) {
        return new ComponentSpecification(sourceSpec.getName()).equals(sourceSpec);
    }

}