summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/searchdefinition/derived/DerivedConfiguration.java
blob: 4af26b7281787e37f1bc37eaa3197be55dfe76f8 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.derived;

import com.yahoo.config.ConfigInstance;
import com.yahoo.config.model.application.provider.BaseDeployLogger;
import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.document.DocumenttypesConfig;
import com.yahoo.document.config.DocumentmanagerConfig;
import com.yahoo.io.IOUtils;
import com.yahoo.protect.Validator;
import com.yahoo.search.query.profile.QueryProfileRegistry;
import com.yahoo.searchdefinition.RankProfileRegistry;
import com.yahoo.searchdefinition.Search;
import com.yahoo.searchdefinition.derived.validation.Validation;
import com.yahoo.searchlib.rankingexpression.integration.ml.ImportedModels;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

/**
 * A set of all derived configuration of a search definition. Use this as a facade to individual configurations when
 * necessary.
 *
 * @author bratseth
 */
public class DerivedConfiguration {

    private Search search;
    private Summaries summaries;
    private SummaryMap summaryMap;
    private Juniperrc juniperrc;
    private AttributeFields attributeFields;
    private RankProfileList rankProfileList;
    private IndexingScript indexingScript;
    private IndexInfo indexInfo;
    private VsmFields streamingFields;
    private VsmSummary streamingSummary;
    private IndexSchema indexSchema;
    private ImportedFields importedFields;

    /**
     * Creates a complete derived configuration from a search definition.
     *
     * @param search The search to derive a configuration from. Derived objects will be snapshots, but this argument is
     *               live. Which means that this object will be inconsistent when the given search definition is later
     *               modified.
     * @param rankProfileRegistry a {@link com.yahoo.searchdefinition.RankProfileRegistry}
     */
    public DerivedConfiguration(Search search,
                                RankProfileRegistry rankProfileRegistry,
                                QueryProfileRegistry queryProfiles,
                                ImportedModels importedModels) {
        this(search, null, new BaseDeployLogger(), rankProfileRegistry, queryProfiles, importedModels);
    }

    /**
     * Creates a complete derived configuration snapshot from a search definition.
     *
     * @param search             The search to derive a configuration from. Derived objects will be snapshots, but this
     *                           argument is live. Which means that this object will be inconsistent when the given
     *                           search definition is later modified.
     * @param abstractSearchList Search definition this one inherits from, only superclass configuration should be
     *                           generated. Null or empty list if there is none.
     * @param deployLogger       a {@link DeployLogger} for logging when
     *                           doing operations on this
     * @param rankProfileRegistry a {@link com.yahoo.searchdefinition.RankProfileRegistry}
     * @param queryProfiles      the query profiles of this application
     */
    public DerivedConfiguration(Search search,
                                List<Search> abstractSearchList,
                                DeployLogger deployLogger,
                                RankProfileRegistry rankProfileRegistry,
                                QueryProfileRegistry queryProfiles,
                                ImportedModels importedModels) {
        Validator.ensureNotNull("Search definition", search);
        if ( ! search.isProcessed()) {
            throw new IllegalArgumentException("Search '" + search.getName() + "' not processed.");
        }
        this.search = search;
        if ( ! search.isDocumentsOnly()) {
            streamingFields = new VsmFields(search);
            streamingSummary = new VsmSummary(search);
        }
        if (abstractSearchList != null) {
            for (Search abstractSearch : abstractSearchList) {
                if (!abstractSearch.isProcessed()) {
                    throw new IllegalArgumentException("Search '" + search.getName() + "' not processed.");
                }
            }
        }
        if ( ! search.isDocumentsOnly()) {
            attributeFields = new AttributeFields(search);
            summaries = new Summaries(search, deployLogger);
            summaryMap = new SummaryMap(search, summaries);
            juniperrc = new Juniperrc(search);
            rankProfileList = new RankProfileList(search, attributeFields, rankProfileRegistry, queryProfiles, importedModels);
            indexingScript = new IndexingScript(search);
            indexInfo = new IndexInfo(search);
            indexSchema = new IndexSchema(search);
            importedFields = new ImportedFields(search);
        }
        Validation.validate(this, search);
    }

    /**
     * Exports a complete set of configuration-server format config files.
     *
     * @param toDirectory  the directory to export to, current dir if null
     * @throws IOException if exporting fails, some files may still be created
     */
    public void export(String toDirectory) throws IOException {
        if (!search.isDocumentsOnly()) {
            summaries.export(toDirectory);
            summaryMap.export(toDirectory);
            juniperrc.export(toDirectory);
            attributeFields.export(toDirectory);
            streamingFields.export(toDirectory);
            streamingSummary.export(toDirectory);
            indexSchema.export(toDirectory);
            rankProfileList.export(toDirectory);
            indexingScript.export(toDirectory);
            indexInfo.export(toDirectory);
            importedFields.export(toDirectory);
        }
    }

    public static void exportDocuments(DocumentmanagerConfig.Builder documentManagerCfg, String toDirectory) throws IOException {
        exportCfg(new DocumentmanagerConfig(documentManagerCfg), toDirectory + "/" + "documentmanager.cfg");
    }

    public static void exportDocuments(DocumenttypesConfig.Builder documentTypesCfg, String toDirectory) throws IOException {
        exportCfg(new DocumenttypesConfig(documentTypesCfg), toDirectory + "/" + "documenttypes.cfg");
    }

    private static void exportCfg(ConfigInstance instance, String fileName) throws IOException {
        Writer writer = null;
        try {
            writer = IOUtils.createWriter(fileName, false);
            if (writer != null) {
                writer.write(instance.toString());
                writer.write("\n");
            }
        } finally {
            if (writer != null) {
                IOUtils.closeWriter(writer);
            }
        }
    }

    public Summaries getSummaries() {
        return summaries;
    }

    public AttributeFields getAttributeFields() {
        return attributeFields;
    }

    public IndexingScript getIndexingScript() {
        return indexingScript;
    }

    public IndexInfo getIndexInfo() {
        return indexInfo;
    }

    public void setIndexingScript(IndexingScript script) {
        this.indexingScript = script;
    }

    public Search getSearch() {
        return search;
    }

    public RankProfileList getRankProfileList() {
        return rankProfileList;
    }

    public VsmSummary getVsmSummary() {
        return streamingSummary;
    }

    public VsmFields getVsmFields() {
        return streamingFields;
    }

    public IndexSchema getIndexSchema() {
        return indexSchema;
    }

    public Juniperrc getJuniperrc() {
        return juniperrc;
    }

    public SummaryMap getSummaryMap() {
        return summaryMap;
    }

    public ImportedFields getImportedFields() {
        return importedFields;
    }
}