aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/IndexModel.java
blob: 45214d4d3df448ef2bd70038ebf4c898e0c647fa (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.yahoo.search.config.IndexInfoConfig;
import com.yahoo.container.QrSearchersConfig;

/**
 * Parameter class used for construction IndexFacts.
 *
 * @author Steinar Knutsen
 * @author bratseth
 */
public final class IndexModel {

    // Copied from MasterClustersInfoUpdater. It's a temporary workaround for IndexFacts
    private Map<String, List<String>> masterClusters;
    private Map<String, SearchDefinition> searchDefinitions;
    private SearchDefinition unionSearchDefinition;

    /**
     * Create an index model.
     */
    public IndexModel(Map<String, List<String>> masterClusters, Collection<SearchDefinition> searchDefinitions) {
        this.masterClusters = masterClusters;
        this.searchDefinitions = searchDefinitions.stream().collect(Collectors.toMap(sd -> sd.getName(), sd -> sd));
        this.unionSearchDefinition = unionOf(searchDefinitions);
    }

    /**
     * Use IndexModel as a pure wrapper for the parameters given.
     */
    // TODO: Deprecate on Vespa 7 and remove on Vespa 8
    public IndexModel(Map<String, List<String>> masterClusters,
                      Map<String, SearchDefinition> searchDefinitions,
                      SearchDefinition unionSearchDefinition) {
        this.masterClusters = masterClusters;
        this.searchDefinitions = searchDefinitions;
        this.unionSearchDefinition = unionSearchDefinition;
    }

    public IndexModel(IndexInfoConfig indexInfo, QrSearchersConfig clusters) {
        this(indexInfo, toClusters(clusters));
    }

    public IndexModel(IndexInfoConfig indexInfo, Map<String, List<String>> clusters) {
        if (indexInfo != null) {
            searchDefinitions = toSearchDefinitions(indexInfo);
            unionSearchDefinition = unionOf(searchDefinitions.values());
        } else {
            searchDefinitions = null;
            unionSearchDefinition = null;
        }
        this.masterClusters = clusters;
    }

    private static Map<String, List<String>> toClusters(QrSearchersConfig config) {
        if (config == null) return new HashMap<>();

        Map<String, List<String>> clusters = new HashMap<>();
        for (int i = 0; i < config.searchcluster().size(); ++i) {
            List<String> docTypes = new ArrayList<>();
            String clusterName = config.searchcluster(i).name();
            for (int j = 0; j < config.searchcluster(i).searchdef().size(); ++j) {
                docTypes.add(config.searchcluster(i).searchdef(j));
            }
            clusters.put(clusterName, docTypes);
        }
        return clusters;
    }

    @SuppressWarnings("deprecation")
    private static Map<String, SearchDefinition> toSearchDefinitions(IndexInfoConfig c) {
        Map<String, SearchDefinition> searchDefinitions = new HashMap<>();

        for (Iterator<IndexInfoConfig.Indexinfo> i = c.indexinfo().iterator(); i.hasNext();) {
            IndexInfoConfig.Indexinfo info = i.next();
            SearchDefinition sd = new SearchDefinition(info.name());
            for (Iterator<IndexInfoConfig.Indexinfo.Command> j = info.command().iterator(); j.hasNext();) {
                IndexInfoConfig.Indexinfo.Command command = j.next();
                sd.addCommand(command.indexname(),command.command());
            }
            sd.fillMatchGroups();
            searchDefinitions.put(info.name(), sd);
        }

        for (IndexInfoConfig.Indexinfo info : c.indexinfo()) {
            SearchDefinition sd = searchDefinitions.get(info.name());
            for (IndexInfoConfig.Indexinfo.Alias alias : info.alias()) {
                String aliasString = alias.alias();
                String indexString = alias.indexname();

                sd.addAlias(aliasString, indexString);
            }
        }
        return searchDefinitions;
    }

    @SuppressWarnings("deprecation")
    private SearchDefinition unionOf(Collection<SearchDefinition> searchDefinitions) {
        SearchDefinition union = new SearchDefinition(IndexFacts.unionName);

        for (SearchDefinition sd : searchDefinitions) {
            for (Index index : sd.indices().values()) {
                union.getOrCreateIndex(index.getName());
                for (String command : index.allCommands())
                    union.addCommand(index.getName(), command);
                for (String alias : index.aliases())
                    union.addAlias(alias, index.getName());
            }

        }
        union.fillMatchGroups();
        return union;
    }

    public Map<String, List<String>> getMasterClusters() { return masterClusters; }

    public Map<String, SearchDefinition> getSearchDefinitions() { return searchDefinitions; }

    // TODO: Deprecate on Vespa 7 and make package scope on Vespa 8
    public SearchDefinition getUnionSearchDefinition() { return unionSearchDefinition; }

}