aboutsummaryrefslogtreecommitdiffstats
path: root/docproc/src/main/java/com/yahoo/docproc/AbstractConcreteDocumentFactory.java
blob: bc008c713bf3c1a7ac59bfa8e64057e9a0c2ab16 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.docproc;

import java.util.Map;

import com.yahoo.document.DataType;
import com.yahoo.document.Document;
import com.yahoo.document.DocumentId;
import com.yahoo.document.Field;
import com.yahoo.document.annotation.Annotation;
import com.yahoo.document.datatypes.Array;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.MapFieldValue;
import com.yahoo.document.datatypes.Struct;
import com.yahoo.document.datatypes.StructuredFieldValue;

/**
 * Subtyped by factory classes for concrete document types. The factory classes are auto-generated
 * by vespa-documentgen-plugin. This superclass is used to manage the factories in OSGI.
 *
 * @author vegardh
 */
public abstract class AbstractConcreteDocumentFactory extends com.yahoo.component.AbstractComponent {

    public abstract Map<String, Class<? extends Document>> documentTypes();
    public abstract Map<String, Class<? extends Struct>> structTypes();
    public abstract Map<String, Class<? extends Annotation>> annotationTypes();

    /**
     * Used by the docproc framework to get an instance of a concrete document type without resorting to reflection in a bundle
     *
     * @return a concrete document instance
     */
    public abstract Document getDocumentCopy(java.lang.String type, StructuredFieldValue src, DocumentId id);

    /**
     * If the FieldValue is a StructuredFieldValue it will upgrade to the concrete type
     * @param field to upgrade
     * @param fv value to upgrade
     * @return fv or upgraded fv
     */
    public FieldValue optionallyUpgrade(Field field, FieldValue fv) {
        return optionallyUpgrade(field.getDataType(), fv);
    }

    @SuppressWarnings({"unchecked"})
    private FieldValue optionallyUpgrade(DataType dataType, FieldValue fv) {
        if (fv instanceof StructuredFieldValue) {
            try {
                return structTypes().get(dataType.getName())
                        .getConstructor(StructuredFieldValue.class)
                        .newInstance(fv);
            } catch (java.lang.Exception ex) {
                throw new RuntimeException(ex);
            }
        } else if (fv instanceof Array) {
            Array<FieldValue> array = (Array<FieldValue>) fv;
            DataType nestedType = array.getDataType().getNestedType();
            if (nestedType.getPrimitiveType() == null) {
                array.replaceAll((item) -> optionallyUpgrade(nestedType, item));
            }
        } else if (fv instanceof MapFieldValue) {
            MapFieldValue<FieldValue, FieldValue> map = (MapFieldValue<FieldValue, FieldValue>) fv;
            DataType valueTypeType = map.getDataType().getValueType();
            if (valueTypeType.getPrimitiveType() == null) {
                map.replaceAll((key, value) -> optionallyUpgrade(valueTypeType, value));
            }
        }
        return fv;
    }

}