aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/DocumentType.java
blob: 38bd3bfdeca3759e1ac01bc00872b1ba010359ea (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document;

import com.google.common.collect.ImmutableSet;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.fieldset.AllFields;
import com.yahoo.document.serialization.DocumentWriter;
import com.yahoo.vespa.objects.Ids;
import com.yahoo.vespa.objects.ObjectVisitor;
import com.yahoo.vespa.objects.Serializer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;


/**
 * <p>A document definition is a list of fields. Documents may inherit other documents,
 * implicitly acquiring their fields as it's own. If a document is not set to inherit
 * any document, it will always inherit the document "document.0".</p>
 *
 * @author Thomas Gundersen
 * @author bratseth
 */
public class DocumentType extends StructuredDataType {

    public static final String DOCUMENT = "[document]";
    public static final int classId = registerClass(Ids.document + 58, DocumentType.class);
    private StructDataType contentStructType;
    private List<DocumentType> inherits = new ArrayList<>(1);
    private final Map<String, Set<Field>> fieldSets = new HashMap<>();
    private final Set<String> importedFieldNames;
    private final Map<String, StructDataType> declaredStructTypes = new HashMap<>();

    /**
     * Creates a new document type and registers it with the document type manager.
     * This will be created as version 0 of this document type.
     * Implicitly registers this with the document type manager.
     * The document type id will be generated as a hash from the document type name.
     *
     * @param name The name of the new document type
     */
    public DocumentType(String name) {
        this(name, createContentStructType(name));
    }

    /**
     * Creates a new document type and registers it with the document type manager.
     * Implicitly registers this with the document type manager.
     * The document type id will be generated as a hash from the document type name.
     *
     * @param name       The name of the new document type
     * @param contentStructType The type of the content struct
     */
    public DocumentType(String name, StructDataType contentStructType) {
        this(name, contentStructType, Collections.emptySet());
    }

    public DocumentType(String name, StructDataType contentStructType, Set<String> importedFieldNames) {
        super(name);
        this.contentStructType = contentStructType;
        this.importedFieldNames = Collections.unmodifiableSet(importedFieldNames);
    }

    public DocumentType(String name, Set<String> importedFieldNames) {
        this(name, createContentStructType(name), importedFieldNames);
    }

    private static StructDataType createContentStructType(String name) {
        return new StructDataType(name + ".header");
    }

    @Override
    public DocumentType clone() {
        DocumentType type = (DocumentType) super.clone();
        type.contentStructType = contentStructType.clone();
        type.inherits = new ArrayList<>(inherits.size());
        type.inherits.addAll(inherits);
        return type;
    }

    @Override
    public Document createFieldValue() {
        return new Document(this, (DocumentId) null);
    }

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

    @Override
    public boolean isValueCompatible(FieldValue value) {
        if (!(value instanceof Document doc)) {
            return false;
        }
        if (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;
    }

    /**
     * Provides the Struct describing the fields in the document.
     *
     * @return a struct describing the document fields.
     */
    public StructDataType contentStruct() {
        return contentStructType;
    }

    /**
     * Get a struct declared in this document (or any inherited
     * document). Returns null if no such struct was found.
     * If multiple possible structs are found in inherited
     * documents, throws exception.
     *
     * @param name the name of the struct
     * @return reference to a struct data type, or null
     **/
    public StructDataType getStructType(String name) {
        var mine = declaredStructTypes.get(name);
        if (mine != null) {
            return mine;
        }
        for (DocumentType inheritedType : inherits) {
            var fromParent = inheritedType.getStructType(name);
            if (fromParent == null) {
                continue;
            } else if (mine == null) {
                mine = fromParent;
            } else if (mine != fromParent) {
                throw new IllegalArgumentException("Found multiple conflicting struct types for "+name);
            }
        }
        return mine;
    }

    /**
     * Get a struct declared in this document only.
     * Returns null if no such struct was found.
     *
     * @param name the name of the struct
     * @return reference to a struct data type, or null
     **/
    public StructDataType getDeclaredStructType(String name) {
        return declaredStructTypes.get(name);
    }

    /** only used during configuration */
    void addDeclaredStructType(String name, StructDataType struct) {
        var old = declaredStructTypes.put(name, struct);
        if (old != null) {
            throw new IllegalArgumentException("Already had declared struct for "+name);
        }
    }

    @Override
    protected void register(DocumentTypeManager manager, List<DataType> seenTypes) {
        seenTypes.add(this);
        for (DocumentType type : getInheritedTypes()) {
            if (!seenTypes.contains(type)) {
                type.register(manager, seenTypes);
            }
        }
        // Get parent fields into fields specified in this type
        StructDataType header = contentStructType.clone();

        header.clearFields();

        for (Field field : getAllUniqueFields()) {
            header.addField(field);
        }
        contentStructType.assign(header);

        if (!seenTypes.contains(contentStructType)) {
            contentStructType.register(manager, seenTypes);
        }
        manager.registerSingleType(this);
    }

    /**
     * Check if this document type has the given name,
     * or inherits from a type with that name.
     */
    public boolean isA(String docTypeName) {
        if (getName().equalsIgnoreCase(docTypeName)) {
            return true;
        }
        for (DocumentType parent : inherits) {
            if (parent.isA(docTypeName)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Adds an field that can be used with this document type.
     *
     * @param field the field to add
     */
    public void addField(Field field) {
        if (isRegistered()) {
            throw new IllegalStateException("You cannot add fields to a document type that is already registered.");
        }
        contentStructType.addField(field);
    }

    // Do not use, public only for testing
    public void addFieldSets(Map<String, Collection<String>> fieldSets) {
        for (Map.Entry<String, Collection<String>> entry : fieldSets.entrySet()) {

            Set<Field> fields = new LinkedHashSet<>(entry.getValue().size());
            for (DocumentType parent : inherits) {
                Set<Field> parentFieldSet = parent.fieldSet(entry.getKey());
                if (parentFieldSet != null) {
                    fields.addAll(parentFieldSet);
                }
            }
            for (Field orderedField : getAllUniqueFields()) {
                if (entry.getValue().contains(orderedField.getName())) {
                    fields.add(orderedField);
                }
            }

            this.fieldSets.put(entry.getKey(), ImmutableSet.copyOf(fields));
        }
        if ( ! this.fieldSets.containsKey(AllFields.NAME)) {
            this.fieldSets.put(AllFields.NAME, getAllUniqueFields());
        }
    }

    /**
     * Adds a new field to this document type and returns the new field object
     *
     * @param name The name of the field to add
     * @param type The datatype of the field to add
     * @return The field created
     *         TODO Fix searchdefinition so that exception can be thrown if filed is already registerd.
     */
    public Field addField(String name, DataType type) {
        if (isRegistered()) {
            throw new IllegalStateException("You cannot add fields to a document type that is already registered.");
        }
        Field field = new Field(name, type);
        contentStructType.addField(field);
        return field;
    }

    /**
     * Adds a document to the inherited document types of this.
     * If this type is already directly inherited, nothing is done
     *
     * @param type An already DocumentType object.
     */
    public void inherit(DocumentType type) {
        //TODO: There is also a check like the following in SDDocumentType addField(), try to move that to this class' addField() to get it proper,
        // as this method is called only when the doc types are exported.
        verifyTypeConsistency(type);
        if (isRegistered()) {
            throw new IllegalStateException("You cannot add inheritance to a document type that is already registered.");
        }
        if (type == null) {
            throw new IllegalArgumentException("The document type cannot be null in inherit()");
        }

        // If it inherits the exact same type
        if (inherits.contains(type)) return;

        // If we inherit a type, don't inherit the supertype
        if (inherits.size() == 1 && inherits.get(0).getDataTypeName().equals(new DataTypeName("document"))) {
            inherits.clear();
        }

        inherits.add(type);
        for (var field : type.getAllUniqueFields()) {
            if (! contentStructType.hasField(field)) {
                contentStructType.addField(field);
            }
        }
    }

    /**
     * Fail if the subtype changes the type of any equally named field.
     *
     * @param superType The supertype to verify against
     *                  TODO Add strict type checking no duplicate fields are allowed
     */
    private void verifyTypeConsistency(DocumentType superType) {
        for (Field f : getAllUniqueFields()) {
            Field supField = superType.getField(f.getName());
            if (supField != null) {
                if (!f.getDataType().equals(supField.getDataType())) {
                    throw new IllegalArgumentException("Inheritance type mismatch: field \"" + f.getName() +
                                                       "\" in datatype \"" + getName() + "\"" +
                                                       " must have same datatype as in parent document type \"" + superType.getName() + "\"");
                }
            }
        }
    }

    /**
     * Returns the DocumentNames which are directly inherited by this
     * as a read-only collection.
     * If this document type does not explicitly inherit anything, the list will
     * contain the root type 'Document'
     *
     * @return a read-only list iterator containing the name Strings of the directly
     *         inherited document types of this
     */
    public Collection<DocumentType> getInheritedTypes() {
        return Collections.unmodifiableCollection(inherits);
    }

    public ListIterator<DataTypeName> inheritedIterator() {
        List<DataTypeName> names = new ArrayList<>(inherits.size());
        for (DocumentType type : inherits) {
            names.add(type.getDataTypeName());
        }
        return List.copyOf(names).listIterator();
    }

    /**
     * Return whether this document type inherits the given document type.
     *
     * @param superType the documenttype to check if it inherits
     * @return true if it inherits the superType, false if not
     */
    public boolean inherits(DocumentType superType) {
        if (equals(superType)) return true;
        for (DocumentType type : inherits) {
            if (type.inherits(superType)) return true;
        }
        return false;
    }

    /**
     * Gets the field matching a given name.
     *
     * @param name the name of a field
     * @return returns the matching field, or null if not found
     */
    public Field getField(String name) {
        Field field = contentStructType.getField(name);
        if (field == null && !isRegistered()) {
            for (DocumentType inheritedType : inherits) {
                field = inheritedType.getField(name);
                if (field != null) break;
            }
        }
        return field;
    }

    @Override
    public Field getField(int id) {
        Field field = contentStructType.getField(id);
        if (field == null && !isRegistered()) {
            for (DocumentType inheritedType : inherits) {
                field = inheritedType.getField(id);
                if (field != null) break;
            }
        }
        return field;
    }

    /**
     * Returns whether this type defines the given field name
     *
     * @param name The name of the field to check if it has
     * @return True if there is a field with the given name.
     */
    public boolean hasField(String name) {
        return getField(name) != null;
    }

    public int getFieldCount() {
        return contentStructType.getFieldCount();
    }

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

    public boolean hasImportedField(String fieldName) {
        return importedFieldNames.contains(fieldName);
    }

    /**
     * Removes an field from the DocumentType.
     *
     * @param name The name of the field.
     * @return The field that was removed or null if it did not exist.
     */
    public Field removeField(String name) {
        if (isRegistered()) {
            throw new IllegalStateException("You cannot remove fields from a document type that is already registered.");
        }
        Field field = contentStructType.removeField(name);
        if (field == null) {
            for (DocumentType inheritedType : inherits) {
                field = inheritedType.removeField(name);
                if (field != null) break;
            }
        }
        return field;
    }

    /**
     * All fields defined in the document and its parents
     * This is for internal use
     * Use {@link #fieldSet()} instead or {@link #fieldSetAll()} if you really want all fields
     * @return All fields defined in the document and its parents
     */
    @Override
    public Collection<Field> getFields() {
        Collection<Field> collection = new LinkedList<>();

        for (DocumentType type : inherits) {
            collection.addAll(type.getFields());
        }

        collection.addAll(contentStructType.getFields());
        return List.copyOf(collection);
    }

    private Set<Field> getAllUniqueFields() {
        Map<String, Field> map = new LinkedHashMap<>();
        for (Field field : getFields()) { // Uniqify on field name
            map.put(field.getName(), field);
        }
        return ImmutableSet.copyOf(map.values());
    }

    /**
     * <p>Returns an ordered set snapshot of all fields of this documenttype,
     * <i>except the fields of Document</i>.
     * Only the overridden version will be returned for overridden fields.</p>
     *
     * <p>The fields of a document type has a well-defined order which is
     * exhibited in this set:
     * - Fields come in the order defined in the document type definition.
     * - The fields defined in inherited types come before those in
     * the document type itself.
     * - When a field in an inherited type is overridden, the value is overridden,
     * but not the ordering.
     * </p>
     *
     * @return an unmodifiable snapshot of the fields in this type
     */
    public Set<Field> fieldSet() {
        return fieldSet(DOCUMENT);
    }

    /**
     * This is identical to {@link #fieldSet()} fieldSet}, but in addition extra hidden synthetic fields are returned.
     * @return an unmodifiable snapshot of the all fields in this type
     */
    public Set<Field> fieldSetAll() {
        return fieldSet(AllFields.NAME);
    }

    public Set<Field> fieldSet(String name) {
        return fieldSets.get(name);
    }

    /**
     * Returns an iterator over all fields in this documenttype
     *
     * @return An iterator for iterating the fields in this documenttype.
     */
    public Iterator<Field> fieldIteratorThisTypeOnly() {
        return contentStructType.getFields().iterator();
    }

    public boolean equals(Object o) {
        if (!(o instanceof DocumentType other)) return false;
        // Ignore whether one of them have added inheritance to super Document.0 type
        if (super.equals(o) && contentStructType.equals(other.contentStructType)) {
            if ((inherits.size() > 1 || other.inherits.size() > 1) ||
                (inherits.size() == 1 && other.inherits.size() == 1)) {
                return inherits.equals(other.inherits);
            }
            return !(((inherits.size() == 1) && !inherits.get(0).getDataTypeName().equals(new DataTypeName("document")))
                     || ((other.inherits.size() == 1) && !other.inherits.get(0).getDataTypeName().equals(new DataTypeName("document"))));
        }
        return false;
    }

    public int hashCode() {
        return super.hashCode() + contentStructType.hashCode() + inherits.hashCode();
    }

    @Override
    public void onSerialize(Serializer target) {
        if (target instanceof DocumentWriter) {
            ((DocumentWriter) target).write(this);
        }
        // TODO: what if it's not a DocumentWriter?
    }


    @Override
    public void visitMembers(ObjectVisitor visitor) {
        super.visitMembers(visitor);
        visitor.visit("headertype", contentStructType);
        visitor.visit("inherits", inherits);
    }
}