aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/documentmodel/NewDocumentType.java
blob: 77e123fefeff1b22dac4266e1b856c61a042c9d5 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.documentmodel;

import com.yahoo.document.DataType;
import com.yahoo.document.Document;
import com.yahoo.document.Field;
import com.yahoo.document.StructDataType;
import com.yahoo.document.StructuredDataType;
import com.yahoo.document.annotation.AnnotationType;
import com.yahoo.document.annotation.AnnotationTypeRegistry;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.schema.FieldSets;
import com.yahoo.schema.Schema;
import com.yahoo.schema.document.FieldSet;
import com.yahoo.schema.processing.BuiltInFieldSets;

import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static java.util.Collections.emptySet;

/**
 * @author baldersheim
 */
public final class NewDocumentType extends StructuredDataType implements DataTypeCollection {

    private final Name name;
    private final DataTypeRepo dataTypes = new DataTypeRepo();
    private final Map<Integer, NewDocumentType> inherits = new LinkedHashMap<>();
    private final AnnotationTypeRegistry annotations = new AnnotationTypeRegistry();
    private final StructDataType contentStruct;
    private final Set<FieldSet> fieldSets = new LinkedHashSet<>();
    private final Set<Name> documentReferences;
    // Imported fields are virtual and therefore exist outside of the SD's document field definition
    // block itself. But for features like imported fields in a non-search context (e.g. GC selections)
    // it is necessary to know that certain identifiers refer to imported fields instead of being unknown
    // document fields. To achieve this, we track the names of imported fields as part of the document
    // config itself.
    private final Set<String> importedFieldNames;

    public NewDocumentType(Name name) {
        this(name, emptySet());
    }

    public NewDocumentType(Name name, Set<Name> documentReferences, Set<String> importedFieldNames) {
        this(
                name,
                new StructDataType(name.getName() + ".header"),
                new FieldSets(Optional.empty()),
                documentReferences,
                importedFieldNames);
    }

    public NewDocumentType(Name name, Set<Name> documentReferences) {
        this(name, documentReferences, emptySet());
    }

    public NewDocumentType(Name name,
                           StructDataType contentStruct,
                           FieldSets fs,
                           Set<Name> documentReferences,
                           Set<String> importedFieldNames) {
        super(name.getName());
        this.name = name;
        this.contentStruct = contentStruct;
        if (fs != null) {
            this.fieldSets.addAll(fs.userFieldSets().values());
            for (FieldSet f : fs.builtInFieldSets().values()) {
                if ((f.getName() != BuiltInFieldSets.INTERNAL_FIELDSET_NAME) &&
                    (f.getName() != BuiltInFieldSets.SEARCH_FIELDSET_NAME)) {
                    fieldSets.add(f);
                }
            }
        }
        this.documentReferences = documentReferences;
        this.importedFieldNames = importedFieldNames;
    }

    public Name getFullName() {
        return name;
    }

    public DataType getContentStruct() { return contentStruct; }
    public Collection<NewDocumentType> getInherited() { return inherits.values(); }
    public NewDocumentType getInherited(Name inherited) { return inherits.get(inherited.getId()); }

    @Override
    public Class<Document> getValueClass() {
        return Document.class;
    }

    @Override
    public boolean isValueCompatible(FieldValue value) {
        if (!(value instanceof Document)) {
            return false;
        }
        /*
         Temporary disabled  due to clash with document and covariant return type
         Document doc = (Document) value;
         if (((NewDocumentType) doc.getDataType()).inherits(this)) {
         //the value is of this type; or the supertype of the value is of this type, etc....
         return true;
         }
         */
        return false;
    }

    private void verifyInheritance(NewDocumentType inherited) {
        for (Field f : getFields()) {
            Field inhF = inherited.getField(f.getName());
            if (inhF != null && !inhF.equals(f)) {
                 throw new IllegalArgumentException("Inherited document '" + inherited + "' already contains field '" +
                                                    inhF.getName() + "'. Can not override with '" + f.getName() + "'.");
            }
        }
        for (Field f : inherited.getAllFields()) {
            for (NewDocumentType side : inherits.values()) {
                Field sideF = side.getField(f.getName());
                if (sideF != null && !sideF.equals(f)) {
                    throw new IllegalArgumentException("Inherited document '" + side + "' already contains field '" +
                                                       sideF.getName() + "'. Document '" + inherited +
                                                       "' also defines field '" + f.getName() +
                                                       "'.Multiple inheritance must be disjunctive.");
                }
            }
        }
    }

    public void inherit(NewDocumentType inherited) {
        if ( ! inherits.containsKey(inherited.getId())) {
            verifyInheritance(inherited);
            inherits.put(inherited.getId(), inherited);
        }
    }

    public boolean inherits(NewDocumentType superType) {
        if (getId() == superType.getId()) return true;
        for (NewDocumentType type : inherits.values()) {
            if (type.inherits(superType)) return true;
        }
        return false;
    }

    @Override
    public Field getField(String name) {
        Field field = contentStruct.getField(name);
        if (field == null) {
            for (NewDocumentType inheritedType : inherits.values()) {
                field = inheritedType.getField(name);
                if (field != null) {
                    return field;
                }
            }
        }
        return field;
    }

    public boolean containsField(String fieldName) {
        return getField(fieldName) != null;
    }

    @Override
    public Field getField(int id) {
        Field field = contentStruct.getField(id);
        if (field == null) {
            for (NewDocumentType inheritedType : inherits.values()) {
                field = inheritedType.getField(id);
                if (field != null) {
                    return field;
                }
            }
        }
        return field;
    }

    public Collection<Field> getAllFields() {
        Collection<Field> collection = new LinkedList<>();

        for (NewDocumentType type : inherits.values()) {
            collection.addAll(type.getAllFields());
        }

        collection.addAll(contentStruct.getFields());
        return Collections.unmodifiableCollection(collection);
    }

    public Collection<Field> getFields() {
        return contentStruct.getFields();
    }

    @Override
    public Document createFieldValue() {
        throw new RuntimeException("Cannot create an instance of " + this);
    }

    @Override
    public Collection<DataType> getTypes() {
        return dataTypes.getTypes();
    }

    public DataTypeCollection getAllTypes() {
        DataTypeRepo repo = new DataTypeRepo();
        Set<Name> seen = new HashSet<>();
        Deque<NewDocumentType> stack = new LinkedList<>();
        stack.push(this);
        while (!stack.isEmpty()) {
            NewDocumentType docType = stack.pop();
            if (seen.contains(docType.name)) {
                continue; // base type
            }
            seen.add(docType.name);
            for (DataType dataType : docType.getTypes()) {
                if (repo.getDataType(dataType.getId()) == null) {
                    repo.add(dataType);
                }
            }
            stack.addAll(docType.inherits.values());
        }
        return repo;
    }

    public Collection<AnnotationType> getAnnotations() { return annotations.getTypes().values(); }
    public Collection<AnnotationType> getAllAnnotations() {
        Collection<AnnotationType> collection = new LinkedList<>();

        for (NewDocumentType type : inherits.values()) {
            collection.addAll(type.getAllAnnotations());
        }
        collection.addAll(getAnnotations());

        return Collections.unmodifiableCollection(collection);
    }

    public DataType getDataType(String name) {
        return dataTypes.getDataType(name);
    }
    public DataType getDataType(int id) {
        return dataTypes.getDataType(id);
    }
    public DataType getDataTypeRecursive(String name) {
        DataType a = dataTypes.getDataType(name);
        if (a != null) {
            return a;
        } else {
            for (NewDocumentType dt : getInherited()) {
                a = dt.getDataTypeRecursive(name);
                if (a != null) {
                    return a;
                }
            }
        }
        return null;
    }

    public DataType getDataTypeRecursive(int id) {
        DataType a = dataTypes.getDataType(id);
        if (a != null) {
            return a;
        } else {
            for (NewDocumentType dt : getInherited()) {
                a = dt.getDataTypeRecursive(id);
                if (a != null) {
                    return a;
                }
            }
        }
        return null;
    }

    public AnnotationType getAnnotationType(String name) {
        AnnotationType a = annotations.getType(name);
        if (a != null) {
            return a;
        } else {
            for (NewDocumentType dt : getInherited()) {
                a = dt.getAnnotationType(name);
                if (a != null) {
                    return a;
                }
            }
        }
        return null;
    }

    public NewDocumentType add(AnnotationType type) {
        annotations.register(type);
        return this;
    }
    public NewDocumentType add(DataType type) {
        dataTypes.add(type);
        return this;
    }
    public NewDocumentType replace(DataType type) {
        dataTypes.replace(type);
        return this;
    }

    /** The field sets defined for this type and its {@link Schema} */
    public Set<FieldSet> getFieldSets() {
        return Collections.unmodifiableSet(fieldSets);
    }

    public Set<Name> getDocumentReferences() {
        return documentReferences;
    }

    public Set<String> getImportedFieldNames() {
        return importedFieldNames;
    }

    public static final class Name {

        private final String name;
        private final int id;

        public Name(String name) {
            this(name.hashCode(), name);
        }

        public Name(int id, String name) {
            this.id = id;
            this.name = name;
        }

        @Override
        public String toString() { return name; }

        public String getName() { return name; }

        public int getId() { return id; }

        @Override
        public int hashCode() { return name.hashCode(); }

        @Override
        public boolean equals(Object other) {
            if ( ! (other instanceof Name)) return false;
            return name.equals(((Name)other).getName());
        }

    }

    private NewDocumentReferenceDataType refToThis = null;

    public NewDocumentReferenceDataType getReferenceDataType() {
        if (refToThis == null) {
            refToThis = new NewDocumentReferenceDataType(this);
        }
        return refToThis;
    }

}