aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/schema/parser/ConvertSchemaCollection.java
blob: ef30ec518f005f14c9714d5412eee5f76268ed22 (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
206
207
208
209
210
211
212
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.parser;

import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.config.application.api.FileRegistry;
import com.yahoo.config.model.api.ModelContext;
import com.yahoo.config.model.application.provider.BaseDeployLogger;
import com.yahoo.config.model.application.provider.MockFileRegistry;
import com.yahoo.config.model.deploy.TestProperties;
import com.yahoo.config.model.test.MockApplicationPackage;
import com.yahoo.document.DocumentTypeManager;
import com.yahoo.schema.RankProfileRegistry;
import com.yahoo.schema.Schema;

import java.util.ArrayList;
import java.util.List;

/**
 * Class converting a collection of schemas from the intermediate format.
 *
 * @author arnej27959
 */
public class ConvertSchemaCollection {

    private final IntermediateCollection input;
    private final List<ParsedSchema> orderedInput = new ArrayList<>();
    private final DocumentTypeManager docMan;
    private final ApplicationPackage applicationPackage;
    private final FileRegistry fileRegistry;
    private final DeployLogger deployLogger;
    private final ModelContext.Properties properties;
    private final RankProfileRegistry rankProfileRegistry;
    private final boolean documentsOnly;

    // for unit test
    ConvertSchemaCollection(IntermediateCollection input,
                            DocumentTypeManager documentTypeManager)
    {
        this(input, documentTypeManager,
             MockApplicationPackage.createEmpty(),
             new MockFileRegistry(),
             new BaseDeployLogger(),
             new TestProperties(),
             new RankProfileRegistry(),
             true);
    }

    public ConvertSchemaCollection(IntermediateCollection input,
                                   DocumentTypeManager documentTypeManager,
                                   ApplicationPackage applicationPackage,
                                   FileRegistry fileRegistry,
                                   DeployLogger deployLogger,
                                   ModelContext.Properties properties,
                                   RankProfileRegistry rankProfileRegistry,
                                   boolean documentsOnly)
    {
        this.input = input;
        this.docMan = documentTypeManager;
        this.applicationPackage = applicationPackage;
        this.fileRegistry = fileRegistry;
        this.deployLogger = deployLogger;
        this.properties = properties;
        this.rankProfileRegistry = rankProfileRegistry;
        this.documentsOnly = documentsOnly;

        input.resolveInternalConnections();
        order();
        pushTypesToDocuments();
    }

    void order() {
        var map = input.getParsedSchemas();
        for (var schema : map.values()) {
            findOrdering(schema);
        }
    }

    void findOrdering(ParsedSchema schema) {
        if (orderedInput.contains(schema)) return;
        for (var parent : schema.getAllResolvedInherits()) {
            findOrdering(parent);
        }
        orderedInput.add(schema);
    }

    void pushTypesToDocuments() {
        for (var schema : orderedInput) {
            for (var struct : schema.getStructs()) {
                schema.getDocument().addStruct(struct);
            }
            for (var annotation : schema.getAnnotations()) {
                schema.getDocument().addAnnotation(annotation);
            }
        }
    }

    private ConvertParsedTypes typeConverter;

    public void convertTypes() {
        typeConverter = new ConvertParsedTypes(orderedInput, docMan);
        typeConverter.convert(true);
    }

    public List<Schema> convertToSchemas() {
        resolveStructInheritance();
        resolveAnnotationInheritance();
        addMissingAnnotationStructs();
        var converter = new ConvertParsedSchemas(orderedInput,
                                                 docMan,
                                                 applicationPackage,
                                                 fileRegistry,
                                                 deployLogger,
                                                 properties,
                                                 rankProfileRegistry,
                                                 documentsOnly);
        return converter.convertToSchemas();
    }

    private void resolveStructInheritance() {
        List<ParsedStruct> all = new ArrayList<>();
        for (var schema : orderedInput) {
            var doc = schema.getDocument();
            for (var struct : doc.getStructs()) {
                for (String inherit : struct.getInherited()) {
                    var parent = doc.findParsedStruct(inherit);
                    if (parent == null) {
                        throw new IllegalArgumentException("Can not find parent for "+struct+" in "+doc);
                    }
                    struct.resolveInherit(inherit, parent);
                }
                all.add(struct);
            }
        }
        List<String> seen = new ArrayList<>();
        for (ParsedStruct struct : all) {
            inheritanceCycleCheck(struct, seen);
        }
    }

    private void resolveAnnotationInheritance() {
        List<ParsedAnnotation> all = new ArrayList();
        for (var schema : orderedInput) {
            var doc = schema.getDocument();
            for (var annotation : doc.getAnnotations()) {
                for (String inherit : annotation.getInherited()) {
                    var parent = doc.findParsedAnnotation(inherit);
                    if (parent == null) {
                        throw new IllegalArgumentException("Can not find parent for "+annotation+" in "+doc);
                    }
                    annotation.resolveInherit(inherit, parent);
                }
                all.add(annotation);
            }
        }
        List<String> seen = new ArrayList<>();
        for (ParsedAnnotation annotation : all) {
            inheritanceCycleCheck(annotation, seen);
        }
    }

    private void fixupAnnotationStruct(ParsedAnnotation parsed) {
        for (var parent : parsed.getResolvedInherits()) {
            fixupAnnotationStruct(parent);
            parent.getStruct().ifPresent(ps -> {
                    var myStruct = parsed.ensureStruct();
                    if (! myStruct.getInherited().contains(ps.name())) {
                        myStruct.inherit(ps.name());
                        myStruct.resolveInherit(ps.name(), ps);
                    }
                });
        }
    }

    private void addMissingAnnotationStructs() {
        for (var schema : orderedInput) {
            var doc = schema.getDocument();
            for (var annotation : doc.getAnnotations()) {
                fixupAnnotationStruct(annotation);
            }
        }
    }

    private void inheritanceCycleCheck(ParsedStruct struct, List<String> seen) {
        String name = struct.name();
        if (seen.contains(name)) {
            seen.add(name);
            throw new IllegalArgumentException("Inheritance/reference cycle for structs: " +
                                               String.join(" -> ", seen));
        }
        seen.add(name);
        for (ParsedStruct parent : struct.getResolvedInherits()) {
            inheritanceCycleCheck(parent, seen);
        }
        seen.remove(name);
    }

    private void inheritanceCycleCheck(ParsedAnnotation annotation, List<String> seen) {
        String name = annotation.name();
        if (seen.contains(name)) {
            seen.add(name);
            throw new IllegalArgumentException("Inheritance/reference cycle for annotations: " +
                                               String.join(" -> ", seen));
        }
        seen.add(name);
        for (ParsedAnnotation parent : annotation.getResolvedInherits()) {
            inheritanceCycleCheck(parent, seen);
        }
        seen.remove(name);
    }

}