aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/DocumentTypeManager.java
blob: 5fad35a22873118cadfe493ee6ae15b54e822975 (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
// 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.google.inject.Inject;
import com.yahoo.config.subscription.ConfigSubscriber;
import com.yahoo.document.annotation.AnnotationReferenceDataType;
import com.yahoo.document.annotation.AnnotationType;
import com.yahoo.document.annotation.AnnotationTypeRegistry;
import com.yahoo.document.annotation.AnnotationTypes;
import com.yahoo.document.config.DocumentmanagerConfig;
import com.yahoo.document.serialization.DocumentDeserializer;
import com.yahoo.document.serialization.DocumentDeserializerFactory;
import com.yahoo.io.GrowableByteBuffer;
import com.yahoo.tensor.TensorType;

import java.lang.reflect.Modifier;
import java.util.*;
import java.util.logging.Logger;

/**
 * The DocumentTypeManager keeps track of the document types registered in
 * the Vespa common repository.
 * <p>
 * The DocumentTypeManager is also responsible for registering a FieldValue
 * factory for each data type a field can have. The Document object
 * uses this factory to serialize and deserialize the various datatypes.
 * The factory could also be used to expand the functionality of various
 * datatypes, for instance displaying the data type in human-readable form
 * or as XML.
 *
 * @author Thomas Gundersen
 */
public class DocumentTypeManager {

    private final static Logger log = Logger.getLogger(DocumentTypeManager.class.getName());
    private ConfigSubscriber subscriber;

    // *Configured data types* (not built-in/primitive) indexed by their id
    //
    // *Primitive* data types are always available and have a single id.
    //
    // *Built-in dynamic* types: The tensor type.
    // Any tensor type has the same id and is always available just like primitive types.
    // However, unlike primitive types, each tensor type is a separate DataType instance
    // (which carries additional type information (detailedType) supplied at runtime).
    // The reason for this is that we want the data type to stay the same when we change the detailed tensor type
    // to maintain compatibility on (some) tensor type changes.
    private Map<Integer, DataType> dataTypes = new LinkedHashMap<>();
    private Map<DataTypeName, DocumentType> documentTypes = new LinkedHashMap<>();
    private AnnotationTypeRegistry annotationTypeRegistry = new AnnotationTypeRegistry();

    public DocumentTypeManager() {
        registerDefaultDataTypes();
    }

    @Inject
    public DocumentTypeManager(DocumentmanagerConfig config) {
        this();
        DocumentTypeManagerConfigurer.configureNewManager(config, this);
    }

    public void assign(DocumentTypeManager other) {
        dataTypes = other.dataTypes;
        documentTypes = other.documentTypes;
        annotationTypeRegistry = other.annotationTypeRegistry;
    }

    public DocumentTypeManager configure(String configId) {
        subscriber = DocumentTypeManagerConfigurer.configure(this, configId);
        return this;
    }

    private void registerDefaultDataTypes() {
        DocumentType superDocType = DataType.DOCUMENT;
        dataTypes.put(superDocType.getId(), superDocType);
        documentTypes.put(superDocType.getDataTypeName(), superDocType);

        Class<? extends DataType> dataTypeClass = DataType.class;
        for (java.lang.reflect.Field field : dataTypeClass.getFields()) {
            if (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) {
                if (DataType.class.isAssignableFrom(field.getType())) {
                    try {
                        //these are all static final DataTypes listed in DataType:
                        DataType type = (DataType) field.get(null);
                        register(type);
                    } catch (IllegalAccessException e) {
                        //ignore
                    }
                }
            }
        }
        for (AnnotationType type : AnnotationTypes.ALL_TYPES) {
            annotationTypeRegistry.register(type);
        }
    }

    public boolean hasDataType(String name) {
        if (name.startsWith("tensor(")) return true; // built-in dynamic: Always present
        for (DataType type : dataTypes.values()) {
            if (type.getName().equalsIgnoreCase(name)) {
                return true;
            }
        }
        return false;
    }

    public boolean hasDataType(int code) {
        if (code == DataType.tensorDataTypeCode) return true; // built-in dynamic: Always present
        return dataTypes.containsKey(code);
    }

    public DataType getDataType(String name) {
        if (name.startsWith("tensor(")) // built-in dynamic
            return new TensorDataType(TensorType.fromSpec(name));

        List<DataType> foundTypes = new ArrayList<>();
        for (DataType type : dataTypes.values()) {
            if (type.getName().equalsIgnoreCase(name)) {
                foundTypes.add(type);
            }
        }
        if (foundTypes.isEmpty()) {
            throw new IllegalArgumentException("No datatype named " + name);
        } else if (foundTypes.size() == 1) {
            return foundTypes.get(0);
        } else {
            //the found types are probably documents or structs, sort them by type
            Collections.sort(foundTypes, new Comparator<DataType>() {
                public int compare(DataType first, DataType second) {
                    if (first instanceof StructuredDataType && !(second instanceof StructuredDataType)) {
                        return 1;
                    } else if (!(first instanceof StructuredDataType) && second instanceof StructuredDataType) {
                        return -1;
                    } else {
                        return 0;
                    }
                }
            });
        }
        return foundTypes.get(0);
    }

    public DataType getDataType(int code) { return getDataType(code, ""); }

    /**
     * Return a data type instance
     *
     * @param code the code of the data type to return, which must be either built in or present in this manager
     * @param detailedType detailed type information, or the empty string if none
     * @return the appropriate DataType instance
     */
    public DataType getDataType(int code, String detailedType) {
        if (code == DataType.tensorDataTypeCode) // built-in dynamic
            return new TensorDataType(TensorType.fromSpec(detailedType));

        DataType type = dataTypes.get(code);
        if (type == null) {
            StringBuilder types=new StringBuilder();
            for (Integer key : dataTypes.keySet()) {
                types.append(key).append(" ");
            }
            throw new IllegalArgumentException("No datatype with code " + code + ". Registered type ids: " + types);
        } else {
            return type;
        }
    }

    DataType getDataTypeAndReturnTemporary(int code, String detailedType) {
        if (hasDataType(code)) {
            return getDataType(code, detailedType);
        }
        return new TemporaryDataType(code, detailedType);
    }

    /**
     * Register a data type of any sort, including document types.
     * @param type The datatype to register
     * TODO Give unique ids to document types
     */
    public void register(DataType type) {
        type.register(this); // Recursively walk through all nested types and call registerSingleType for each one
    }

    /**
     * Register a single datatype. Re-registering an existing, but equal, datatype is ok.
     *
     * @param type The datatype to register
     */
    void registerSingleType(DataType type) {
        if (type instanceof TensorDataType) return; // built-in dynamic: Created on the fly
        if (dataTypes.containsKey(type.getId())) {
            DataType existingType = dataTypes.get(type.getId());
            if (((type instanceof TemporaryDataType) || (type instanceof TemporaryStructuredDataType))
                && !((existingType instanceof TemporaryDataType) || (existingType instanceof TemporaryStructuredDataType))) {
                //we're trying to register a temporary type over a permanent one, don't do that:
                return;
            } else if ((existingType == type || existingType.equals(type))
                    && !(existingType instanceof TemporaryDataType)
                    && !(type instanceof TemporaryDataType)
                    && !(existingType instanceof TemporaryStructuredDataType)
                    && !(type instanceof TemporaryStructuredDataType)) { // Shortcut to improve speed
                // Oki. Already registered.
                return;
            } else if (type instanceof DocumentType && dataTypes.get(type.getId()) instanceof DocumentType) {
                /*
                DocumentType newInstance = (DocumentType) type;
                DocumentType oldInstance = (DocumentType) dataTypes.get(type.getId());
                TODO fix tests
                */
                log.warning("Document type " + existingType + " is not equal to document type attempted registered " + type
                        + ", but have same name. OVERWRITING TYPE as many tests currently does this. "
                        + "Fix tests so we can throw exception here.");
                //throw new IllegalStateException("Datatype " + existingType + " is not equal to datatype attempted registered "
                //        + type + ", but already uses id " + type.getId());
            } else if ((existingType instanceof TemporaryDataType) || (existingType instanceof TemporaryStructuredDataType)) {
                //removing temporary type to be able to register correct type
                dataTypes.remove(existingType.getId());
            } else {
                throw new IllegalStateException("Datatype " + existingType + " is not equal to datatype attempted registered "
                                                + type + ", but already uses id " + type.getId());
            }
        }

        if (type instanceof DocumentType) {
            DocumentType docType = (DocumentType) type;
            if (docType.getInheritedTypes().size() == 0) {
                docType.inherit(documentTypes.get(new DataTypeName("document")));
            }
            documentTypes.put(docType.getDataTypeName(), docType);
        }
        dataTypes.put(type.getId(), type);
        type.setRegistered();
    }

    /**
     * Registers a document type. Typically called by someone
     * importing the document types from the common Vespa repository.
     *
     * @param docType The document type to register.
     * @return the previously registered type, or null if none was registered
     */
    public DocumentType registerDocumentType(DocumentType docType) {
        register(docType);
        return docType;
    }

    /**
     * Gets a registered document.
     *
     * @param name the document name of the type
     * @return returns the document type found,
     *         or null if there is no type with this name
     */
    public DocumentType getDocumentType(DataTypeName name) {
        return documentTypes.get(name);
    }

    /**
     * Returns a registered document type
     *
     * @param name    the type name of the document type
     * @return returns the document type having this name, or null if none
     */
    public DocumentType getDocumentType(String name) {
        return documentTypes.get(new DataTypeName(name));
    }

    final public Document createDocument(GrowableByteBuffer buf) {
        DocumentDeserializer data = DocumentDeserializerFactory.create42(this, buf);
        return new Document(data);
    }
    public Document createDocument(DocumentDeserializer data) {
        return new Document(data);
    }

    public Document createDocument(GrowableByteBuffer header, GrowableByteBuffer body) {
        DocumentDeserializer data = DocumentDeserializerFactory.create42(this, header, body);
        return new Document(data);
    }

    /**
     * Returns a read only view of the registered data types
     *
     * @return collection of types
     */
    public Collection<DataType> getDataTypes() {
        return Collections.unmodifiableCollection(dataTypes.values());
    }

    /**
     * A read only view of the registered document types
     * @return map of types
     */
    public Map<DataTypeName, DocumentType> getDocumentTypes() {
        return Collections.unmodifiableMap(documentTypes);
    }

    public Iterator<DocumentType> documentTypeIterator() {
        return documentTypes.values().iterator();
    }

    /**
     * Clears the DocumentTypeManager. After this operation,
     * only the default document type and data types are available.
     */
    public void clear() {
        documentTypes.clear();
        dataTypes.clear();
        registerDefaultDataTypes();
    }

    public AnnotationTypeRegistry getAnnotationTypeRegistry() {
        return annotationTypeRegistry;
    }

    void replaceTemporaryTypes() {
        for (DataType type : dataTypes.values()) {
            List<DataType> seenStructs = new LinkedList<>();
            replaceTemporaryTypes(type, seenStructs);
        }
    }

    private void replaceTemporaryTypes(DataType type, List<DataType> seenStructs) {
        if (type instanceof WeightedSetDataType) {
            replaceTemporaryTypesInWeightedSet((WeightedSetDataType) type, seenStructs);
        } else if (type instanceof MapDataType) {
            replaceTemporaryTypesInMap((MapDataType) type, seenStructs);
        } else if (type instanceof CollectionDataType) {
            replaceTemporaryTypesInCollection((CollectionDataType) type, seenStructs);
        } else if (type instanceof StructDataType) {
            replaceTemporaryTypesInStruct((StructDataType) type, seenStructs);
        } else if (type instanceof PrimitiveDataType) {
            //OK because these types are always present
        } else if (type instanceof AnnotationReferenceDataType) {
            //OK because this type is always present
        } else if (type instanceof DocumentType) {
            //OK because this type is always present
        } else if (type instanceof TensorDataType) {
            //OK because this type is always present
        } else if (type instanceof ReferenceDataType) {
            replaceTemporaryTypeInReference((ReferenceDataType) type);
        } else if (type instanceof TemporaryDataType) {
            throw new IllegalStateException("TemporaryDataType registered in DocumentTypeManager, BUG!!");
        } else {
            log.warning("Don't know how to replace temporary data types in " + type);
        }
    }

    @SuppressWarnings("deprecation")
    private void replaceTemporaryTypesInStruct(StructDataType structDataType, List<DataType> seenStructs) {
        seenStructs.add(structDataType);
        for (Field field : structDataType.getFieldsThisTypeOnly()) {
            DataType fieldType = field.getDataType();
            if (fieldType instanceof TemporaryDataType) {
                field.setDataType(getDataType(fieldType.getCode(), ((TemporaryDataType)fieldType).getDetailedType()));
            } else {
                if (!seenStructs.contains(fieldType)) {
                    replaceTemporaryTypes(fieldType, seenStructs);
                }
            }
        }
    }

    private void replaceTemporaryTypeInReference(ReferenceDataType referenceDataType) {
        if (referenceDataType.getTargetType() instanceof TemporaryStructuredDataType) {
            referenceDataType.setTargetType((DocumentType) getDataType(referenceDataType.getTargetType().getId()));
        }
        // TODO should we recursively invoke replaceTemporaryTypes for the target type? It should only ever be a doc type
    }

    private void replaceTemporaryTypesInCollection(CollectionDataType collectionDataType, List<DataType> seenStructs) {
        if (collectionDataType.getNestedType() instanceof TemporaryDataType) {
            collectionDataType.setNestedType(getDataType(collectionDataType.getNestedType().getCode(), ""));
        } else {
            replaceTemporaryTypes(collectionDataType.getNestedType(), seenStructs);
        }
    }

    private void replaceTemporaryTypesInMap(MapDataType mapDataType, List<DataType> seenStructs) {
        if (mapDataType.getValueType() instanceof TemporaryDataType) {
            mapDataType.setValueType(getDataType(mapDataType.getValueType().getCode(), ""));
        } else {
            replaceTemporaryTypes(mapDataType.getValueType(), seenStructs);
        }

        if (mapDataType.getKeyType() instanceof TemporaryDataType) {
            mapDataType.setKeyType(getDataType(mapDataType.getKeyType().getCode(), ""));
        } else {
            replaceTemporaryTypes(mapDataType.getKeyType(), seenStructs);
        }
    }

    private void replaceTemporaryTypesInWeightedSet(WeightedSetDataType weightedSetDataType, List<DataType> seenStructs) {
        if (weightedSetDataType.getNestedType() instanceof TemporaryDataType) {
            weightedSetDataType.setNestedType(getDataType(weightedSetDataType.getNestedType().getCode(), ""));
        } else {
            replaceTemporaryTypes(weightedSetDataType.getNestedType(), seenStructs);
        }
    }

    public void shutdown() {
        if (subscriber!=null) subscriber.close();
    }
}