aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/schema/derived/DerivedConfiguration.java
blob: bc1c097ea4b92244bdb40d1c37da6b812d546ce7 (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
203
204
205
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.derived;

import com.yahoo.config.ConfigInstance;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.document.config.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.schema.RankProfileRegistry;
import com.yahoo.schema.Schema;
import com.yahoo.schema.derived.validation.Validation;
import com.yahoo.vespa.config.search.AttributesConfig;
import com.yahoo.vespa.config.search.core.RankingConstantsConfig;
import com.yahoo.vespa.model.container.search.QueryProfiles;

import java.io.IOException;
import java.io.Writer;

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

    private final Schema schema;
    private Summaries summaries;
    private Juniperrc juniperrc;
    private AttributeFields attributeFields;
    private RankProfileList rankProfileList;
    private IndexingScript indexingScript;
    private IndexInfo indexInfo;
    private SchemaInfo schemaInfo;
    private VsmFields streamingFields;
    private VsmSummary streamingSummary;
    private IndexSchema indexSchema;
    private ImportedFields importedFields;
    private final QueryProfileRegistry queryProfiles;
    private final long maxUncommittedMemory;

    /**
     * Creates a complete derived configuration from a search definition.
     * Only used in tests.
     *
     * @param schema 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.schema.RankProfileRegistry}
     */
    public DerivedConfiguration(Schema schema, RankProfileRegistry rankProfileRegistry) {
        this(schema, rankProfileRegistry, new QueryProfileRegistry());
    }

    DerivedConfiguration(Schema schema, RankProfileRegistry rankProfileRegistry, QueryProfileRegistry queryProfiles) {
        this(schema, new DeployState.Builder().rankProfileRegistry(rankProfileRegistry).queryProfiles(queryProfiles).build(), false);
    }

    /**
     * Creates a complete derived configuration snapshot from a schema.
     *
     * @param schema the schema to derive a configuration from. Derived objects will be snapshots, but this
     *               argument is live. Which means that this object will be inconsistent if the given
     *               schema is later modified.
     */
    public DerivedConfiguration(Schema schema, DeployState deployState, boolean isStreaming) {
        try {
            Validator.ensureNotNull("Schema", schema);
            this.schema = schema;
            this.queryProfiles = deployState.getQueryProfiles().getRegistry();
            this.maxUncommittedMemory = deployState.getProperties().featureFlags().maxUnCommittedMemory();
            if (!schema.isDocumentsOnly()) {
                streamingFields = new VsmFields(schema);
                streamingSummary = new VsmSummary(schema);
                attributeFields = new AttributeFields(schema);
                summaries = new Summaries(schema, deployState.getDeployLogger(), deployState.getProperties().featureFlags());
                juniperrc = new Juniperrc(schema);
                rankProfileList = new RankProfileList(schema, schema.rankExpressionFiles(), attributeFields, deployState);
                indexingScript = new IndexingScript(schema, isStreaming);
                indexInfo = new IndexInfo(schema, isStreaming);
                schemaInfo = new SchemaInfo(schema, deployState.rankProfileRegistry(), summaries);
                indexSchema = new IndexSchema(schema);
                importedFields = new ImportedFields(schema);
            }
            Validation.validate(this, schema);
        }
        catch (IllegalArgumentException|IllegalStateException e) {
            throw new IllegalArgumentException("Invalid " + schema, e);
        }
    }

    /**
     * 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 (!schema.isDocumentsOnly()) {
            summaries.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);
            schemaInfo.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");
    }

    public static void exportQueryProfiles(QueryProfileRegistry queryProfileRegistry, String toDirectory) throws IOException {
        exportCfg(new QueryProfiles(queryProfileRegistry, (level, message) -> {}).getConfig(), toDirectory + "/" + "query-profiles.cfg");
    }

    public void exportConstants(String toDirectory) throws IOException {
        RankingConstantsConfig.Builder b = new RankingConstantsConfig.Builder();
        rankProfileList.getConfig(b);
        exportCfg(b.build(), toDirectory + "/" + "ranking-constants.cfg");
    }

    private static void exportCfg(ConfigInstance instance, String fileName) throws IOException {
        Writer writer = null;
        try {
            writer = IOUtils.createWriter(fileName, false);
            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 void getConfig(AttributesConfig.Builder builder) {
        getConfig(builder, AttributeFields.FieldSet.ALL);
    }

    public void getConfig(AttributesConfig.Builder builder, AttributeFields.FieldSet fs) {
        attributeFields.getConfig(builder, fs, maxUncommittedMemory);
    }

    public IndexingScript getIndexingScript() {
        return indexingScript;
    }

    public IndexInfo getIndexInfo() {
        return indexInfo;
    }

    public SchemaInfo getSchemaInfo() { return schemaInfo; }

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

    public Schema getSchema() { return schema; }

    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 ImportedFields getImportedFields() {
        return importedFields;
    }

    public QueryProfileRegistry getQueryProfiles() { return queryProfiles; }

}