aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/searchdefinition/document/SDDocumentType.java
blob: c7b698f583506174aad30f46ebca8771984252db (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.document;

import com.yahoo.document.CompressionConfig;
import com.yahoo.document.DataType;
import com.yahoo.document.DataTypeName;
import com.yahoo.document.DocumentType;
import com.yahoo.document.Field;
import com.yahoo.document.PositionDataType;
import com.yahoo.document.StructDataType;
import com.yahoo.document.annotation.AnnotationType;
import com.yahoo.document.annotation.AnnotationTypeRegistry;
import com.yahoo.documentmodel.NewDocumentType;
import com.yahoo.documentmodel.VespaDocumentType;
import com.yahoo.searchdefinition.DocumentReferences;
import com.yahoo.searchdefinition.FieldSets;
import com.yahoo.searchdefinition.Search;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Optional;
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  <a href="thomasg@yahoo-inc.com">Thomas Gundersen</a>
   @author  bratseth
*/
public class SDDocumentType implements Cloneable, Serializable {
    public static final SDDocumentType VESPA_DOCUMENT;
    private Map<DataTypeName, SDDocumentType> inheritedTypes = new HashMap<>();
    private Map<NewDocumentType.Name, SDDocumentType> ownedTypes = new HashMap<>();
    private AnnotationTypeRegistry annotationTypes = new AnnotationTypeRegistry();
    private DocumentType docType;    
    private DataType structType;
    // The field sets here are set from the processing step in SD, to ensure that the full Search and this SDDocumentType is built first.
    private FieldSets fieldSets;
    // Document references
    private Optional<DocumentReferences> documentReferences = Optional.empty();

    static {
        VESPA_DOCUMENT = new SDDocumentType(VespaDocumentType.INSTANCE.getFullName().getName());
        VESPA_DOCUMENT.addType(createSDDocumentType(PositionDataType.INSTANCE));
    }

    public SDDocumentType clone() throws CloneNotSupportedException {
        SDDocumentType type = (SDDocumentType) super.clone();
        type.docType = docType.clone();
        type.inheritedTypes.putAll(inheritedTypes);
        type.structType = structType;
        return type;
    }

    /**
     * For adding structs defined in document scope
     *
     * @param dt The struct to add.
     * @return self, for chaining
     */
    public SDDocumentType addType(SDDocumentType dt) {
        NewDocumentType.Name name = new NewDocumentType.Name(dt.getName());
        if (getType(name) != null) {
            throw new IllegalArgumentException("Data type '" + name.toString() + "' has already been used.");
        }
        if (name.getName() == docType.getName()) {
            throw new IllegalArgumentException("Data type '" + name.toString() + "' can not have same name as its defining document.");
        }
        ownedTypes.put(name, dt);
        return this;
    }
    public final SDDocumentType getOwnedType(String name) {
         return getOwnedType(new NewDocumentType.Name(name));
    }
    public SDDocumentType getOwnedType(DataTypeName name) {
        return getOwnedType(name.getName());
    }

    public SDDocumentType getOwnedType(NewDocumentType.Name name) {
        return ownedTypes.get(name);
    }

    public final SDDocumentType getType(String name) {
        return getType(new NewDocumentType.Name(name));
    }

    public SDDocumentType getType(NewDocumentType.Name name) {
        SDDocumentType type = ownedTypes.get(name);
        if (type == null) {
            for (SDDocumentType inherited : inheritedTypes.values()) {
                type = inherited.getType(name);
                if (type != null) {
                    return type;
                }
            }
        }
        return type;
    }

    public SDDocumentType addAnnotation(AnnotationType annotation) {
        annotationTypes.register(annotation);
        return this;
    }

    /**
     * Access to all owned datatypes.
     * @return all types
     */
    public Collection<SDDocumentType> getTypes() { return ownedTypes.values(); }
    public Collection<AnnotationType> getAnnotations() { return annotationTypes.getTypes().values(); }
    public AnnotationType findAnnotation(String name) { return annotationTypes.getType(name); }

    public Collection<SDDocumentType> getAllTypes() {
        Collection<SDDocumentType> list = new ArrayList<>();
        list.addAll(getTypes());
        for (SDDocumentType inherited : inheritedTypes.values()) {
            list.addAll(inherited.getAllTypes());
        }
        return list;
    }

    /**
     * Creates a new document type.
     * 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 SDDocumentType(String name) {
        this(name,null);
    }

    public SDDocumentType(DataTypeName name) {
        this(name.getName());
    }

    /**
     * Creates a new document type.
     * 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 search check for type ID collisions in this search definition
     */
    @SuppressWarnings("deprecation")
    public SDDocumentType(String name, Search search) {
        docType = new DocumentType(name);
        docType.contentStruct().setCompressionConfig(new CompressionConfig());
        docType.getBodyType().setCompressionConfig(new CompressionConfig());
        validateId(search);
        inherit(VESPA_DOCUMENT);
    }

    public boolean isStruct() { return getStruct() != null; }
    public DataType getStruct() { return structType; }
    public SDDocumentType setStruct(DataType structType) {
        if (structType != null) {
            this.structType = structType;
            inheritedTypes.clear();
        } else {
            if (docType.contentStruct() != null) {
                this.structType = docType.contentStruct();
                inheritedTypes.clear();
            } else {
                throw new IllegalArgumentException("You can not set a null struct");
            }
        }
        return this;
    }

    public String getName() { return docType.getName(); }
    public DataTypeName getDocumentName() { return docType.getDataTypeName(); }
    public DocumentType getDocumentType() { return docType; }

    public void inherit(DataTypeName name) {
        if ( ! inheritedTypes.containsKey(name)) {
            inheritedTypes.put(name, new TemporarySDDocumentType(name));
        }
    }

    public void inherit(SDDocumentType type) {
        if (type != null) {
            if (!inheritedTypes.containsKey(type.getDocumentName()) || (inheritedTypes.get(type.getDocumentName()) instanceof TemporarySDDocumentType)) {
                inheritedTypes.put(type.getDocumentName(), type);
            }
        }
    }

    public Collection<SDDocumentType>  getInheritedTypes() { return inheritedTypes.values(); }

    protected void validateId(Search search) {
        if (search == null) return;
        if (search.getDocument(getName()) == null) return;
        SDDocumentType doc = search.getDocument();
        throw new IllegalArgumentException("Failed creating document type '" + getName() + "', " +
                "document type '" + doc.getName() + "' already uses ID '" + doc.getName() + "'");
    }

    public void setFieldId(SDField field, int id) {
        field.setId(id, docType);
    }

    /** Override getField, as it may need to ask inherited types that isn't registered in document type.
     * @param name Name of field to get.
     * @return The field found.
     */
    public Field getField(String name) {
        if (name.contains(".")) {
            String superFieldName = name.substring(0,name.indexOf("."));
            String subFieldName = name.substring(name.indexOf(".")+1);
            Field f = docType.getField(superFieldName);
            if (f != null) {
                if (f instanceof SDField) {
                    SDField superField = (SDField)f;
                    return superField.getStructField(subFieldName);
                } else {
                    throw new IllegalArgumentException("Field "+f.getName()+" is not SDField");
                }
            }
        }
        Field f = docType.getField(name);
        if (f == null) {
            for(SDDocumentType parent : inheritedTypes.values()) {
                f = parent.getField(name);
                if (f != null) return f;
            }
        }
        return f;
    }

    public void addField(Field field) {
    	verifyInheritance(field);
        for (Iterator<Field> i = docType.fieldIteratorThisTypeOnly(); i.hasNext(); ) {
            if (field.getName().equalsIgnoreCase((i.next()).getName())) {
                throw new IllegalArgumentException("Duplicate (case insensitively) " + field + " in " + this);
            }
        }
        docType.addField(field);
    }

    /**
     * This is SD parse-time inheritance check.
     *
     * @param field The field being verified.
     */
    private void verifyInheritance(Field field) {
    	for (SDDocumentType parent : inheritedTypes.values()) {
            for (Field pField : parent.fieldSet()) {
            	if (pField.getName().equals(field.getName())) {
            		if (!pField.getDataType().equals(field.getDataType())) {
            			throw new IllegalArgumentException("For search '"+getName()+"', field '"+field.getName()+"': " +
            					"datatype can't be different from that of same field in supertype '"+parent.getName()+"'.");
            		}
            	}
            }
    	}
	}

	public SDField addField(String string, DataType dataType) {
        SDField field = new SDField(this, string, dataType);
        addField(field);
        return field;
    }

    public Field addField(String string, DataType dataType, boolean header, int code) {
        SDField field = new SDField(this, string, code, dataType, header);
        addField(field);
        return field;
    }

    private Map<String, Field> fieldsInherited() {
    	Map<String, Field> map = new LinkedHashMap<>();
        for (SDDocumentType parent : inheritedTypes.values()) {
            for (Field field : parent.fieldSet()) {
                map.put(field.getName(), field);
            }
        }
        return map;
    }

    public Set<Field> fieldSet() {
        Map<String, Field> map = fieldsInherited();
        Iterator<Field> it = docType.fieldIteratorThisTypeOnly();
        while (it.hasNext()) {
            Field field = it.next();
            map.put(field.getName(), field);
        }
        return new LinkedHashSet<>(map.values());
    }

    public Iterator<Field> fieldIterator() {
        return fieldSet().iterator();
    }

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

    public String toString() {
        return "SD document type '" + docType.getName() + "'";
    }

    private static SDDocumentType createSDDocumentType(StructDataType structType) {
        SDDocumentType docType = new SDDocumentType(structType.getName());
        for (Field field : structType.getFields()) {
            docType.addField(new SDField(docType, field.getName(), field.getDataType()));
        }
        docType.setStruct(structType);
        return docType;
    }
    
    /**
     * The field sets defined for this type and its {@link Search}
     * @return fieldsets 
     */
    public FieldSets getFieldSets() {
        return fieldSets;
    }

    /**
     * Sets the field sets for this
     * @param fieldSets field sets to set for this object
     */
    public void setFieldSets(FieldSets fieldSets) {
        this.fieldSets = fieldSets;
    }

    public Optional<DocumentReferences> getDocumentReferences() {
        return documentReferences;
    }

    public void setDocumentReferences(DocumentReferences documentReferences) {
        this.documentReferences = Optional.of(documentReferences);
    }

}