aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/configmodel/producers/DataTypeRecognizer.java
blob: ecacfa43757eda2c02a2a5a4e0f84b457ece8f6e (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.configmodel.producers;

import com.yahoo.document.ArrayDataType;
import com.yahoo.document.DataType;
import com.yahoo.document.DocumentType;
import com.yahoo.document.MapDataType;
import com.yahoo.document.NumericDataType;
import com.yahoo.document.PositionDataType;
import com.yahoo.document.PrimitiveDataType;
import com.yahoo.document.StructDataType;
import com.yahoo.document.TensorDataType;
import com.yahoo.document.WeightedSetDataType;
import com.yahoo.document.annotation.AnnotationType;
import com.yahoo.document.annotation.AnnotationReferenceDataType;
import com.yahoo.documentmodel.DataTypeCollection;
import com.yahoo.documentmodel.NewDocumentReferenceDataType;
import com.yahoo.documentmodel.NewDocumentType;
import com.yahoo.documentmodel.OwnedStructDataType;
import com.yahoo.documentmodel.VespaDocumentType;
import com.yahoo.schema.document.annotation.SDAnnotationType;

import java.util.*;

/**
 * Class to produce unique names for DataType instances that have
 * different contents even when their getName() and getId() may be
 * equal.
 *
 * @author arnej
 **/
public class DataTypeRecognizer {
    private Map<Object, String> toUniqueNames = new IdentityHashMap<>();

    DataTypeRecognizer() {
    }

    DataTypeRecognizer(DataTypeCollection dtc) {
        for (var type : dtc.getTypes()) {
            System.err.println("added: "+nameOf(type));
        }
    }

    String nameOf(Object type) {
        return toUniqueNames.computeIfAbsent(type, t -> makeUniqueName(t));
    }

    private String makeUniqueName(Object type) {
        if (type == PositionDataType.INSTANCE) {
            return "{builtin position}";
        }
        if (type == DataType.DOCUMENT) {
            return "{builtin root document}";
        }
        if (type == VespaDocumentType.INSTANCE) {
            return "{builtin vespa document}";
        }
        var typeClass = type.getClass();
        if (typeClass == AnnotationReferenceDataType.class) {
            var t = (AnnotationReferenceDataType) type;
            var ann = t.getAnnotationType();
            return "annotationreference<" + ann.getName() + ">";
        }
        if (typeClass == AnnotationType.class) {
            var t = (AnnotationType) type;
            return "annotation<" + t.getName() + ">";
        }
        if (typeClass == SDAnnotationType.class) {
            var t = (SDAnnotationType) type;
            return "annotation<" + t.getName() + ">";
        }
        if (typeClass == ArrayDataType.class) {
            var t = (ArrayDataType) type;
            var nt = t.getNestedType();
            return "array<" + nameOf(nt) + ">";
        }
        if (typeClass == DocumentType.class) {
            var t = (DocumentType) type;
            return "{document "+t.getName()+"}";
        }
        if (typeClass == MapDataType.class) {
            var t = (MapDataType) type;
            var kt = t.getKeyType();
            var vt = t.getValueType();
            return "map<" + nameOf(kt) + ", " + nameOf(vt) + ">";
        }
        if (typeClass == NewDocumentReferenceDataType.class) {
            var t = (NewDocumentReferenceDataType) type;
            return "reference<" + t.getTargetTypeName() + ">";
        }
        if (typeClass == NewDocumentType.class) {
            var t = (NewDocumentType) type;
            return "{new-document "+t.getName()+"}";
        }
        if (typeClass == OwnedStructDataType.class) {
            var t = (OwnedStructDataType) type;
            return "{owned-struct " + t.getName() + " @ " + t.getOwnerName() + "}";
        }
        if (typeClass == NumericDataType.class) {
            var t = (NumericDataType) type;
            return "{numeric " + t.getName() + "}";
        }
        if (typeClass == PrimitiveDataType.class) {
            var t = (PrimitiveDataType) type;
            return "{primitive " + t.getName() + "}";
        }
        if (typeClass == StructDataType.class) {
            var t = (StructDataType) type;
            return "{struct "+t.getName()+"}";
        }
        if (typeClass == TensorDataType.class) {
            var t = (TensorDataType) type;
            return "{tensor" + t.getTensorType() + "}";
        }
        if (typeClass == WeightedSetDataType.class) {
            var t = (WeightedSetDataType) type;
            var nt = t.getNestedType();
            String prefix = "weightedset<";
            String cine = t.createIfNonExistent() ? " [createIfNonExistent]" : "";
            String riz = t.removeIfZero() ? " [removeIfZero]" : "";
            String suffix = ">";
            return prefix + nameOf(nt) + cine + riz + suffix;
        }
        throw new IllegalArgumentException("unknown type class: "+typeClass);
    }

}