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

import com.yahoo.compress.CompressionType;
import com.yahoo.config.subscription.ConfigSubscriber;
import com.yahoo.document.config.DocumentmanagerConfig;
import com.yahoo.document.annotation.AnnotationReferenceDataType;
import com.yahoo.document.annotation.AnnotationType;
import com.yahoo.log.LogLevel;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * Configures the Vespa document manager from a config id.
 *
 * @author Einar M R Rosenvinge
 */
public class DocumentTypeManagerConfigurer implements ConfigSubscriber.SingleSubscriber<DocumentmanagerConfig>{

    private final static Logger log = Logger.getLogger(DocumentTypeManagerConfigurer.class.getName());

    private DocumentTypeManager managerToConfigure;

    public DocumentTypeManagerConfigurer(DocumentTypeManager manager) {
        this.managerToConfigure = manager;
    }

    private static CompressionConfig makeCompressionConfig(DocumentmanagerConfig.Datatype.Structtype cfg) {
        return new CompressionConfig(toCompressorType(cfg.compresstype()), cfg.compresslevel(),
                                     cfg.compressthreshold(), cfg.compressminsize());
    }

    public static CompressionType toCompressorType(DocumentmanagerConfig.Datatype.Structtype.Compresstype.Enum value) {
        switch (value) {
            case NONE: return CompressionType.NONE;
            case LZ4: return CompressionType.LZ4;
            case UNCOMPRESSABLE: return CompressionType.INCOMPRESSIBLE;
        }
        throw new IllegalArgumentException("Compression type " + value + " is not supported");
    }

    /**
     * <p>Makes the DocumentTypeManager subscribe on its config.</p>
     *
     * <p>Proper Vespa setups will use a config id which looks up the document manager config
     * at the document server, but it is also possible to read config from a file containing
     * a document manager configuration by using
     * <code>file:path-to-document-manager.cfg</code>.</p>
     *
     * @param configId the config ID to use
     */
    public static ConfigSubscriber configure(DocumentTypeManager manager, String configId) {
        return new DocumentTypeManagerConfigurer(manager).configure(configId);
    }

    public ConfigSubscriber configure(String configId) {
        ConfigSubscriber subscriber = new ConfigSubscriber();
        subscriber.subscribe(this, DocumentmanagerConfig.class, configId);
        return subscriber;
    }

    static void configureNewManager(DocumentmanagerConfig config, DocumentTypeManager manager) {
        if (config == null) {
            return;
        }

        setupAnnotationTypesWithoutPayloads(config, manager);
        setupAnnotationRefTypes(config, manager);

        log.log(LogLevel.DEBUG, "Configuring document manager with " + config.datatype().size() + " data types.");
        ArrayList<DocumentmanagerConfig.Datatype> failed = new ArrayList<>();
        failed.addAll(config.datatype());
        while (!failed.isEmpty()) {
            ArrayList<DocumentmanagerConfig.Datatype> tmp = failed;
            failed = new ArrayList<>();
            for (int i = 0; i < tmp.size(); i++) {
                DocumentmanagerConfig.Datatype thisDataType = tmp.get(i);
                int id = thisDataType.id();
                try {
                    registerTypeIdMapping(config, manager, thisDataType, id);
                } catch (IllegalArgumentException e) {
                    failed.add(thisDataType);
                }
            }
        }
        addStructInheritance(config, manager);
        addAnnotationTypePayloads(config, manager);
        addAnnotationTypeInheritance(config, manager);

        manager.replaceTemporaryTypes();
    }

    private static void registerTypeIdMapping(DocumentmanagerConfig config, DocumentTypeManager manager, DocumentmanagerConfig.Datatype thisDataType, int id) {
        for (Object o : thisDataType.arraytype()) {
            registerArrayType(manager, id, (DocumentmanagerConfig.Datatype.Arraytype) o);
        }
        for (Object o : thisDataType.maptype()) {
            registerMapType(manager, id, (DocumentmanagerConfig.Datatype.Maptype) o);
        }
        for (Object o : thisDataType.weightedsettype()) {
            registerWeightedSetType(manager, id, (DocumentmanagerConfig.Datatype.Weightedsettype) o);
        }
        for (Object o : thisDataType.structtype()) {
            registerStructType(config, manager, id, (DocumentmanagerConfig.Datatype.Structtype) o);
        }
        for (Object o : thisDataType.documenttype()) {
            registerDocumentType(manager, (DocumentmanagerConfig.Datatype.Documenttype) o);
        }
        for (Object o : thisDataType.referencetype()) {
            registerReferenceType(manager, id, (DocumentmanagerConfig.Datatype.Referencetype) o);
        }
    }

    private static void registerArrayType(DocumentTypeManager manager, int id,
                                          DocumentmanagerConfig.Datatype.Arraytype array) {
        DataType nestedType = manager.getDataType(array.datatype(), "");
        ArrayDataType type = new ArrayDataType(nestedType, id);
        manager.register(type);
    }

    private static void registerMapType(DocumentTypeManager manager, int id,
                                        DocumentmanagerConfig.Datatype.Maptype map) {
        DataType keyType = manager.getDataType(map.keytype(), "");
        DataType valType = manager.getDataType(map.valtype(), "");
        MapDataType type = new MapDataType(keyType, valType, id);
        manager.register(type);
    }

    private static void registerWeightedSetType(DocumentTypeManager manager, int id,
                                                DocumentmanagerConfig.Datatype.Weightedsettype wset) {
        DataType nestedType = manager.getDataType(wset.datatype(), "");
        WeightedSetDataType type = new WeightedSetDataType(
                nestedType, wset.createifnonexistant(), wset.removeifzero(), id);
        manager.register(type);
    }

    @SuppressWarnings("deprecation")
    private static void registerDocumentType(DocumentTypeManager manager, DocumentmanagerConfig.Datatype.Documenttype doc) {
        StructDataType header = (StructDataType) manager.getDataType(doc.headerstruct(), "");
        StructDataType body = (StructDataType) manager.getDataType(doc.bodystruct(), "");
        for (Field field : body.getFields()) {
            field.setHeader(false);
        }
        DocumentType type = new DocumentType(doc.name(), header, body);
        for (Object j : doc.inherits()) {
            DocumentmanagerConfig.Datatype.Documenttype.Inherits parent =
                    (DocumentmanagerConfig.Datatype.Documenttype.Inherits) j;
            DataTypeName name = new DataTypeName(parent.name());
            DocumentType parentType = manager.getDocumentType(name);
            if (parentType == null) {
                throw new IllegalArgumentException("Could not find document type '" + name.toString() + "'.");
            }
            type.inherit(parentType);
        }
        Map<String, Collection<String>> fieldSets = new HashMap<>(doc.fieldsets().size());
        for (Map.Entry<String, DocumentmanagerConfig.Datatype.Documenttype.Fieldsets> entry: doc.fieldsets().entrySet()) {
            fieldSets.put(entry.getKey(), entry.getValue().fields());
        }
        type.addFieldSets(fieldSets);
        manager.register(type);
    }

    private static void registerStructType(DocumentmanagerConfig config, DocumentTypeManager manager, int id,
                                           DocumentmanagerConfig.Datatype.Structtype struct) {
        StructDataType type = new StructDataType(id, struct.name());

        if (config.enablecompression()) {
            CompressionConfig comp = makeCompressionConfig(struct);
            type.setCompressionConfig(comp);
        }

        for (Object j : struct.field()) {
            DocumentmanagerConfig.Datatype.Structtype.Field field =
                    (DocumentmanagerConfig.Datatype.Structtype.Field) j;
            DataType fieldType = (field.datatype() == id)
                               ? manager.getDataTypeAndReturnTemporary(field.datatype(), field.detailedtype())
                               : manager.getDataType(field.datatype(), field.detailedtype());

            if (field.id().size() == 1) {
                type.addField(new Field(field.name(), field.id().get(0).id(), fieldType));
            } else {
                type.addField(new Field(field.name(), fieldType));
            }
        }
        manager.register(type);
    }

    private static void registerReferenceType(DocumentTypeManager manager, int id,
                                              DocumentmanagerConfig.Datatype.Referencetype refType) {
        ReferenceDataType referenceType;
        if (manager.hasDataType(refType.target_type_id())) {
            DocumentType targetDocType = (DocumentType)manager.getDataType(refType.target_type_id());
            referenceType = new ReferenceDataType(targetDocType, id);
        } else {
            TemporaryStructuredDataType temporaryTargetType = TemporaryStructuredDataType.createById(refType.target_type_id());
            referenceType = new ReferenceDataType(temporaryTargetType, id);
        }
        // Note: can't combine the above new-statements, as they call different constructors.
        manager.register(referenceType);
    }

    public static DocumentTypeManager configureNewManager(DocumentmanagerConfig config) {
        DocumentTypeManager manager = new DocumentTypeManager();
        if (config == null) {
            return manager;
        }
        configureNewManager(config, manager);
        return manager;
    }

    /**
     * Called by the configuration system to register document types based on documentmanager.cfg.
     *
     * @param config the instance representing config in documentmanager.cfg.
     */
    @Override
    public void configure(DocumentmanagerConfig config) {
        DocumentTypeManager manager = configureNewManager(config);
        int defaultTypeCount = new DocumentTypeManager().getDataTypes().size();
        if (this.managerToConfigure.getDataTypes().size() != defaultTypeCount) {
            log.log(LogLevel.DEBUG, "Live document config overwritten with new config.");
        }
        managerToConfigure.assign(manager);
    }

    private static void setupAnnotationRefTypes(DocumentmanagerConfig config, DocumentTypeManager manager) {
        for (int i = 0; i < config.datatype().size(); i++) {
            DocumentmanagerConfig.Datatype thisDataType = config.datatype(i);
            int id = thisDataType.id();
            for (Object o : thisDataType.annotationreftype()) {
                DocumentmanagerConfig.Datatype.Annotationreftype annRefType = (DocumentmanagerConfig.Datatype.Annotationreftype) o;
                AnnotationType annotationType = manager.getAnnotationTypeRegistry().getType(annRefType.annotation());
                if (annotationType == null) {
                    throw new IllegalArgumentException("Found reference to " + annRefType.annotation() + ", which does not exist!");
                }
                AnnotationReferenceDataType type = new AnnotationReferenceDataType(annotationType, id);
                manager.register(type);
            }
        }
    }

    private static void setupAnnotationTypesWithoutPayloads(DocumentmanagerConfig config, DocumentTypeManager manager) {
        for (DocumentmanagerConfig.Annotationtype annType : config.annotationtype()) {
            AnnotationType annotationType = new AnnotationType(annType.name(), annType.id());
            manager.getAnnotationTypeRegistry().register(annotationType);
        }
    }

    private static void addAnnotationTypePayloads(DocumentmanagerConfig config, DocumentTypeManager manager) {
        for (DocumentmanagerConfig.Annotationtype annType : config.annotationtype()) {
            AnnotationType annotationType = manager.getAnnotationTypeRegistry().getType(annType.id());
            DataType payload = manager.getDataType(annType.datatype(), "");
            if (!payload.equals(DataType.NONE)) {
                annotationType.setDataType(payload);
            }
        }

    }

    private static void addAnnotationTypeInheritance(DocumentmanagerConfig config, DocumentTypeManager manager) {
        for (DocumentmanagerConfig.Annotationtype annType : config.annotationtype()) {
            if (annType.inherits().size() > 0) {
                AnnotationType inheritedType = manager.getAnnotationTypeRegistry().getType(annType.inherits(0).id());
                AnnotationType type = manager.getAnnotationTypeRegistry().getType(annType.id());
                type.inherit(inheritedType);
            }
        }
    }

    private static void addStructInheritance(DocumentmanagerConfig config, DocumentTypeManager manager) {
        for (int i = 0; i < config.datatype().size(); i++) {
            DocumentmanagerConfig.Datatype thisDataType = config.datatype(i);
            int id = thisDataType.id();
            for (Object o : thisDataType.structtype()) {
                DocumentmanagerConfig.Datatype.Structtype struct = (DocumentmanagerConfig.Datatype.Structtype) o;
                StructDataType thisStruct = (StructDataType) manager.getDataType(id, "");

                for (DocumentmanagerConfig.Datatype.Structtype.Inherits parent : struct.inherits()) {
                    StructDataType parentStruct = (StructDataType) manager.getDataType(parent.name());
                    thisStruct.inherit(parentStruct);
                }
            }
        }
    }
}