aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/federation/sourceref/SourceRefResolver.java
blob: f80444a689a7eebfe93f049da44fe29a4dd60243 (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
// Copyright Yahoo. 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.prelude.IndexFacts;
import com.yahoo.processing.request.Properties;

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

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

    private final SearchChainResolver searchChainResolver;

    public SourceRefResolver(SearchChainResolver searchChainResolver) {
        this.searchChainResolver = searchChainResolver;
    }

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

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

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

            List<String> clusters = indexFacts.clustersHavingSearchDefinition(sourceRef.getName());
            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);
    }

}